xref: /aosp_15_r20/platform_testing/libraries/flicker/utils/src/android/tools/traces/parsers/DeviceDumpParser.kt (revision dd0948b35e70be4c0246aabd6c72554a5eb8b22a)
1 /*
<lambda>null2  * 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.traces.parsers
18 
19 import android.tools.traces.DeviceStateDump
20 import android.tools.traces.NullableDeviceStateDump
21 import android.tools.traces.parsers.perfetto.LayersTraceParser
22 import android.tools.traces.parsers.perfetto.TraceProcessorSession
23 import android.tools.traces.parsers.perfetto.WindowManagerTraceParser
24 import android.tools.traces.parsers.wm.WindowManagerDumpParser
25 import android.tools.traces.surfaceflinger.LayerTraceEntry
26 import android.tools.traces.surfaceflinger.LayersTrace
27 import android.tools.traces.wm.WindowManagerState
28 import android.tools.traces.wm.WindowManagerTrace
29 import android.tools.withTracing
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         var lastWmTraceData = ByteArray(0)
38         var lastLayersTraceData = ByteArray(0)
39 
40         /**
41          * Creates a device state dump containing the [WindowManagerTrace] and [LayersTrace]
42          * obtained from a `dumpsys` command. The parsed traces will contain a single
43          * [WindowManagerState] or [LayerTraceEntry].
44          *
45          * @param wmTraceData [WindowManagerTrace] content
46          * @param layersTraceData [LayersTrace] content
47          * @param clearCacheAfterParsing If the caching used while parsing the proto should be
48          *
49          * ```
50          *                               cleared or remain in memory
51          * ```
52          */
53         @JvmStatic
54         fun fromNullableDump(
55             wmTraceData: ByteArray,
56             layersTraceData: ByteArray,
57             clearCacheAfterParsing: Boolean,
58         ): NullableDeviceStateDump {
59             return withTracing("fromNullableDump") {
60                     val hasSfDump = layersTraceData.isNotEmpty()
61                     val hasWmDump = wmTraceData.isNotEmpty()
62 
63                     // If android.tracing.Flags.perfettoWmDump() is enabled, layersTraceData and
64                     // wmTraceData correspond to the same perfetto trace file
65                     val perfettoTrace =
66                         if (hasSfDump) {
67                             layersTraceData
68                         } else if (android.tracing.Flags.perfettoWmDump() && hasWmDump) {
69                             wmTraceData
70                         } else {
71                             null
72                         }
73 
74                     var wmState: WindowManagerState? = null
75                     var layerState: LayerTraceEntry? = null
76 
77                     perfettoTrace?.let {
78                         TraceProcessorSession.loadPerfettoTrace(it) { session ->
79                             if (hasSfDump) {
80                                 layerState =
81                                     LayersTraceParser()
82                                         .parse(session, clearCache = clearCacheAfterParsing)
83                                         .entries
84                                         .first()
85                             }
86 
87                             if (android.tracing.Flags.perfettoWmDump() && hasWmDump) {
88                                 wmState =
89                                     WindowManagerTraceParser()
90                                         .parse(session, clearCache = clearCacheAfterParsing)
91                                         .entries
92                                         .first()
93                             }
94                         }
95                     }
96 
97                     if (!android.tracing.Flags.perfettoWmDump() && hasWmDump) {
98                         wmState =
99                             WindowManagerDumpParser()
100                                 .parse(wmTraceData, clearCache = clearCacheAfterParsing)
101                                 .entries
102                                 .first()
103                     }
104 
105                     NullableDeviceStateDump(wmState = wmState, layerState = layerState)
106                 }
107                 .also {
108                     lastWmTraceData = wmTraceData
109                     lastLayersTraceData = layersTraceData
110                 }
111         }
112 
113         /** See [fromNullableDump] */
114         @JvmStatic
115         fun fromDump(
116             wmTraceData: ByteArray,
117             layersTraceData: ByteArray,
118             clearCacheAfterParsing: Boolean,
119         ): DeviceStateDump {
120             return withTracing("fromDump") {
121                     val nullableDump =
122                         fromNullableDump(wmTraceData, layersTraceData, clearCacheAfterParsing)
123                     DeviceStateDump(
124                         nullableDump.wmState ?: error("WMState dump missing"),
125                         nullableDump.layerState ?: error("Layer State dump missing"),
126                     )
127                 }
128                 .also {
129                     lastWmTraceData = wmTraceData
130                     lastLayersTraceData = layersTraceData
131                 }
132         }
133     }
134 }
135