1 /*
2  * Copyright (C) 2021 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 com.android.server.wm.traces.parser
18 
19 import com.android.server.wm.traces.common.DeviceStateDump
20 import com.android.server.wm.traces.common.DeviceTraceDump
21 import com.android.server.wm.traces.common.layers.LayersTrace
22 import com.android.server.wm.traces.common.layers.LayerTraceEntry
23 import com.android.server.wm.traces.common.windowmanager.WindowManagerTrace
24 import com.android.server.wm.traces.common.windowmanager.WindowManagerState
25 import com.android.server.wm.traces.parser.layers.LayersTraceParser
26 import com.android.server.wm.traces.parser.windowmanager.WindowManagerTraceParser
27 
28 /**
29  * Represents a state dump containing the [WindowManagerTrace] and the [LayersTrace] both parsed
30  * and in raw (byte) data.
31  */
32 class DeviceDumpParser {
33     companion object {
34         /**
35          * Creates a device state dump containing the [WindowManagerTrace] and [LayersTrace]
36          * obtained from a `dumpsys` command. The parsed traces will contain a single
37          * [WindowManagerState] or [LayerTraceEntry].
38          *
39          * @param wmTraceData [WindowManagerTrace] content
40          * @param layersTraceData [LayersTrace] content
41          */
42         @JvmStatic
fromDumpnull43         fun fromDump(
44             wmTraceData: ByteArray,
45             layersTraceData: ByteArray
46         ): DeviceStateDump<WindowManagerState?, LayerTraceEntry?> {
47             return DeviceStateDump(
48                 wmState = if (wmTraceData.isNotEmpty()) {
49                     WindowManagerTraceParser.parseFromDump(wmTraceData).first()
50                 } else {
51                     null
52                 },
53                 layerState = if (layersTraceData.isNotEmpty()) {
54                     LayersTraceParser.parseFromTrace(layersTraceData).first()
55                 } else {
56                     null
57                 }
58             )
59         }
60 
61         /**
62          * Creates a device state dump containing the WindowManager and Layers trace
63          * obtained from a regular trace. The parsed traces may contain a multiple
64          * [WindowManagerState] or [LayerTraceEntry].
65          *
66          * @param wmTraceData [WindowManagerTrace] content
67          * @param layersTraceData [LayersTrace] content
68          */
69         @JvmStatic
fromTracenull70         fun fromTrace(wmTraceData: ByteArray, layersTraceData: ByteArray): DeviceTraceDump {
71             return DeviceTraceDump(
72                 wmTrace = if (wmTraceData.isNotEmpty()) {
73                     WindowManagerTraceParser.parseFromTrace(wmTraceData)
74                 } else {
75                     null
76                 },
77                 layersTrace = if (layersTraceData.isNotEmpty()) {
78                     LayersTraceParser.parseFromTrace(layersTraceData)
79                 } else {
80                     null
81                 }
82             )
83         }
84     }
85 }
86