1 /*
<lambda>null2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 @file:JvmName("Extensions")
18 
19 package com.android.server.wm.traces.parser
20 
21 import android.app.UiAutomation
22 import android.content.ComponentName
23 import android.os.ParcelFileDescriptor
24 import android.util.Log
25 import com.android.server.wm.traces.common.DeviceStateDump
26 import com.android.server.wm.traces.common.FlickerComponentName
27 import com.android.server.wm.traces.common.Rect
28 import com.android.server.wm.traces.common.Region
29 import com.android.server.wm.traces.common.layers.LayerTraceEntry
30 import com.android.server.wm.traces.common.windowmanager.WindowManagerState
31 
32 internal const val LOG_TAG = "AMWM_FLICKER"
33 
34 fun Region.toAndroidRegion(): android.graphics.Region {
35     return android.graphics.Region(left, top, right, bottom)
36 }
37 
Rectnull38 fun Rect.toAndroidRect(): android.graphics.Rect {
39     return android.graphics.Rect(left, top, right, bottom)
40 }
41 
42 /**
43  * Subtracts [other] region from this [this] region
44  */
minusnull45 fun Region.minus(other: Region): android.graphics.Region = minus(other.toAndroidRegion())
46 
47 /**
48  * Subtracts [other] region from this [this] region
49  */
50 fun Region.minus(other: android.graphics.Region): android.graphics.Region {
51     val thisRegion = this.toAndroidRegion()
52     thisRegion.op(other, android.graphics.Region.Op.XOR)
53     return thisRegion
54 }
55 
56 /**
57  * Adds [other] region to this [this] region
58  */
Regionnull59 fun Region.plus(other: Region): android.graphics.Region = plus(other.toAndroidRegion())
60 
61 /**
62  * Adds [other] region to this [this] region
63  */
64 fun Region.plus(other: android.graphics.Region): android.graphics.Region {
65     val thisRegion = this.toAndroidRegion()
66     thisRegion.op(other, android.graphics.Region.Op.XOR)
67     return thisRegion
68 }
69 
executeCommandnull70 private fun executeCommand(uiAutomation: UiAutomation, cmd: String): ByteArray {
71     val fileDescriptor = uiAutomation.executeShellCommand(cmd)
72     ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor).use { inputStream ->
73         return inputStream.readBytes()
74     }
75 }
76 
getCurrentWindowManagerStatenull77 private fun getCurrentWindowManagerState(uiAutomation: UiAutomation) =
78     executeCommand(uiAutomation, "dumpsys window --proto")
79 
80 private fun getCurrentLayersState(uiAutomation: UiAutomation) =
81     executeCommand(uiAutomation, "dumpsys SurfaceFlinger --proto")
82 
83 @JvmOverloads
84 fun getCurrentState(
85     uiAutomation: UiAutomation,
86     @WmStateDumpFlags dumpFlags: Int = FLAG_STATE_DUMP_FLAG_WM.or(FLAG_STATE_DUMP_FLAG_LAYERS)
87 ): Pair<ByteArray, ByteArray> {
88     if (dumpFlags == 0) {
89         throw IllegalArgumentException("No dump specified")
90     }
91 
92     Log.d(LOG_TAG, "Requesting new device state dump")
93     val wmTraceData = if (dumpFlags.and(FLAG_STATE_DUMP_FLAG_WM) > 0) {
94         getCurrentWindowManagerState(uiAutomation)
95     } else {
96         ByteArray(0)
97     }
98     val layersTraceData = if (dumpFlags.and(FLAG_STATE_DUMP_FLAG_LAYERS) > 0) {
99         getCurrentLayersState(uiAutomation)
100     } else {
101         ByteArray(0)
102     }
103 
104     return Pair(wmTraceData, layersTraceData)
105 }
106 
107 @JvmOverloads
getCurrentStateDumpnull108 fun getCurrentStateDump(
109     uiAutomation: UiAutomation,
110     @WmStateDumpFlags dumpFlags: Int = FLAG_STATE_DUMP_FLAG_WM.or(FLAG_STATE_DUMP_FLAG_LAYERS)
111 ): DeviceStateDump<WindowManagerState?, LayerTraceEntry?> {
112     val currentStateDump = getCurrentState(uiAutomation, dumpFlags)
113     val wmTraceData = currentStateDump.first
114     val layersTraceData = currentStateDump.second
115     return DeviceDumpParser.fromDump(wmTraceData, layersTraceData)
116 }
117 
118 /**
119  * Converts an Android [ComponentName] into a flicker [FlickerComponentName]
120  */
toFlickerComponentnull121 fun ComponentName.toFlickerComponent(): FlickerComponentName =
122     FlickerComponentName(this.packageName, this.className)
123