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.Rect
26 import com.android.server.wm.traces.common.Region
27
28 internal const val LOG_TAG = "AMWM_FLICKER"
29
30 fun Region.toAndroidRegion(): android.graphics.Region {
31 return android.graphics.Region(left, top, right, bottom)
32 }
33
Rectnull34 fun Rect.toAndroidRect(): android.graphics.Rect {
35 return android.graphics.Rect(left, top, right, bottom)
36 }
37
toActivityNamenull38 fun ComponentName.toActivityName(): String = this.flattenToShortString()
39
40 fun ComponentName.toWindowName(): String = this.flattenToString()
41
42 private fun executeCommand(uiAutomation: UiAutomation, cmd: String): ByteArray {
43 val fileDescriptor = uiAutomation.executeShellCommand(cmd)
44 ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor).use { inputStream ->
45 return inputStream.readBytes()
46 }
47 }
48
getCurrentWindowManagerStatenull49 private fun getCurrentWindowManagerState(uiAutomation: UiAutomation) =
50 executeCommand(uiAutomation, "dumpsys window --proto")
51
52 private fun getCurrentLayersState(uiAutomation: UiAutomation) =
53 executeCommand(uiAutomation, "dumpsys SurfaceFlinger --proto")
54
55 @JvmOverloads
56 fun getCurrentState(
57 uiAutomation: UiAutomation,
58 @WmStateDumpFlags dumpFlags: Int = FLAG_STATE_DUMP_FLAG_WM.or(FLAG_STATE_DUMP_FLAG_LAYERS)
59 ): Pair<ByteArray, ByteArray> {
60 if (dumpFlags == 0) {
61 throw IllegalArgumentException("No dump specified")
62 }
63
64 Log.d(LOG_TAG, "Requesting new device state dump")
65 val wmTraceData = if (dumpFlags.and(FLAG_STATE_DUMP_FLAG_WM) > 0) {
66 getCurrentWindowManagerState(uiAutomation)
67 } else {
68 ByteArray(0)
69 }
70 val layersTraceData = if (dumpFlags.and(FLAG_STATE_DUMP_FLAG_LAYERS) > 0) {
71 getCurrentLayersState(uiAutomation)
72 } else {
73 ByteArray(0)
74 }
75
76 return Pair(wmTraceData, layersTraceData)
77 }
78
79 @JvmOverloads
getCurrentStateDumpnull80 fun getCurrentStateDump(
81 uiAutomation: UiAutomation,
82 @WmStateDumpFlags dumpFlags: Int = FLAG_STATE_DUMP_FLAG_WM.or(FLAG_STATE_DUMP_FLAG_LAYERS)
83 ): DeviceStateDump {
84 val currentStateDump = getCurrentState(uiAutomation, dumpFlags)
85 val wmTraceData = currentStateDump.first
86 val layersTraceData = currentStateDump.second
87 return DeviceStateDump.fromDump(wmTraceData, layersTraceData)
88 }
89