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.layers.BaseLayerTraceEntry
29 import com.android.server.wm.traces.common.windowmanager.WindowManagerState
30
31 internal const val LOG_TAG = "AMWM_FLICKER"
32
33 fun Rect.toAndroidRect(): android.graphics.Rect {
34 return android.graphics.Rect(left, top, right, bottom)
35 }
36
toFlickerRectnull37 fun android.graphics.Rect.toFlickerRect(): Rect {
38 return Rect(left, top, right, bottom)
39 }
40
executeCommandnull41 private fun executeCommand(uiAutomation: UiAutomation, cmd: String): ByteArray {
42 val fileDescriptor = uiAutomation.executeShellCommand(cmd)
43 ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor).use { inputStream ->
44 return inputStream.readBytes()
45 }
46 }
47
getCurrentWindowManagerStatenull48 private fun getCurrentWindowManagerState(uiAutomation: UiAutomation) =
49 executeCommand(uiAutomation, "dumpsys window --proto")
50
51 private fun getCurrentLayersState(uiAutomation: UiAutomation) =
52 executeCommand(uiAutomation, "dumpsys SurfaceFlinger --proto")
53
54 @JvmOverloads
55 fun getCurrentState(
56 uiAutomation: UiAutomation,
57 @WmStateDumpFlags dumpFlags: Int = FLAG_STATE_DUMP_FLAG_WM.or(FLAG_STATE_DUMP_FLAG_LAYERS)
58 ): Pair<ByteArray, ByteArray> {
59 if (dumpFlags == 0) {
60 throw IllegalArgumentException("No dump specified")
61 }
62
63 Log.d(LOG_TAG, "Requesting new device state dump")
64 val wmTraceData = if (dumpFlags.and(FLAG_STATE_DUMP_FLAG_WM) > 0) {
65 getCurrentWindowManagerState(uiAutomation)
66 } else {
67 ByteArray(0)
68 }
69 val layersTraceData = if (dumpFlags.and(FLAG_STATE_DUMP_FLAG_LAYERS) > 0) {
70 getCurrentLayersState(uiAutomation)
71 } else {
72 ByteArray(0)
73 }
74
75 return Pair(wmTraceData, layersTraceData)
76 }
77
78 @JvmOverloads
getCurrentStateDumpnull79 fun getCurrentStateDump(
80 uiAutomation: UiAutomation,
81 @WmStateDumpFlags dumpFlags: Int = FLAG_STATE_DUMP_FLAG_WM.or(FLAG_STATE_DUMP_FLAG_LAYERS)
82 ): DeviceStateDump<WindowManagerState?, BaseLayerTraceEntry?> {
83 val currentStateDump = getCurrentState(uiAutomation, dumpFlags)
84 val wmTraceData = currentStateDump.first
85 val layersTraceData = currentStateDump.second
86 return DeviceDumpParser.fromDump(wmTraceData, layersTraceData)
87 }
88
89 /**
90 * Converts an Android [ComponentName] into a flicker [FlickerComponentName]
91 */
toFlickerComponentnull92 fun ComponentName.toFlickerComponent(): FlickerComponentName =
93 FlickerComponentName(this.packageName, this.className)
94