1 /* 2 * Copyright (C) 2023 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 package android.tools.device.traces.parsers 18 19 import android.tools.common.Logger 20 import android.tools.common.traces.DeviceStateDump 21 import android.tools.common.traces.DeviceTraceDump 22 import android.tools.common.traces.NullableDeviceStateDump 23 import android.tools.common.traces.surfaceflinger.LayerTraceEntry 24 import android.tools.common.traces.surfaceflinger.LayersTrace 25 import android.tools.common.traces.wm.WindowManagerState 26 import android.tools.common.traces.wm.WindowManagerTrace 27 import android.tools.device.traces.parsers.surfaceflinger.LayersTraceParser 28 import android.tools.device.traces.parsers.wm.WindowManagerDumpParser 29 import android.tools.device.traces.parsers.wm.WindowManagerTraceParser 30 31 /** 32 * Represents a state dump containing the [WindowManagerTrace] and the [LayersTrace] both parsed and 33 * in raw (byte) data. 34 */ 35 class DeviceDumpParser { 36 companion object { 37 /** 38 * Creates a device state dump containing the [WindowManagerTrace] and [LayersTrace] 39 * obtained from a `dumpsys` command. The parsed traces will contain a single 40 * [WindowManagerState] or [LayerTraceEntry]. 41 * 42 * @param wmTraceData [WindowManagerTrace] content 43 * @param layersTraceData [LayersTrace] content 44 * @param clearCacheAfterParsing If the caching used while parsing the proto should be 45 * 46 * ``` 47 * cleared or remain in memory 48 * ``` 49 */ 50 @JvmStatic fromNullableDumpnull51 fun fromNullableDump( 52 wmTraceData: ByteArray, 53 layersTraceData: ByteArray, 54 clearCacheAfterParsing: Boolean 55 ): NullableDeviceStateDump { 56 return Logger.withTracing("fromNullableDump") { 57 NullableDeviceStateDump( 58 wmState = 59 if (wmTraceData.isNotEmpty()) { 60 WindowManagerDumpParser() 61 .parse(wmTraceData, clearCache = clearCacheAfterParsing) 62 .entries 63 .first() 64 } else { 65 null 66 }, 67 layerState = 68 if (layersTraceData.isNotEmpty()) { 69 LayersTraceParser() 70 .parse(layersTraceData, clearCache = clearCacheAfterParsing) 71 .entries 72 .first() 73 } else { 74 null 75 } 76 ) 77 } 78 } 79 80 /** See [fromNullableDump] */ 81 @JvmStatic fromDumpnull82 fun fromDump( 83 wmTraceData: ByteArray, 84 layersTraceData: ByteArray, 85 clearCacheAfterParsing: Boolean 86 ): DeviceStateDump { 87 return Logger.withTracing("fromDump") { 88 val nullableDump = 89 fromNullableDump(wmTraceData, layersTraceData, clearCacheAfterParsing) 90 DeviceStateDump( 91 nullableDump.wmState ?: error("WMState dump missing"), 92 nullableDump.layerState ?: error("Layer State dump missing") 93 ) 94 } 95 } 96 97 /** 98 * Creates a device state dump containing the WindowManager and Layers trace obtained from a 99 * regular trace. The parsed traces may contain a multiple [WindowManagerState] or 100 * [LayerTraceEntry]. 101 * 102 * @param wmTraceData [WindowManagerTrace] content 103 * @param layersTraceData [LayersTrace] content 104 * @param clearCache If the caching used while parsing the proto should be 105 * 106 * ``` 107 * cleared or remain in memory 108 * ``` 109 */ 110 @JvmStatic fromTracenull111 fun fromTrace( 112 wmTraceData: ByteArray, 113 layersTraceData: ByteArray, 114 clearCache: Boolean 115 ): DeviceTraceDump { 116 return Logger.withTracing("fromTrace") { 117 DeviceTraceDump( 118 wmTrace = 119 if (wmTraceData.isNotEmpty()) { 120 WindowManagerTraceParser().parse(wmTraceData, clearCache = clearCache) 121 } else { 122 null 123 }, 124 layersTrace = 125 if (layersTraceData.isNotEmpty()) { 126 LayersTraceParser().parse(layersTraceData, clearCache = clearCache) 127 } else { 128 null 129 } 130 ) 131 } 132 } 133 } 134 } 135