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.common.service.processors
18 
19 import com.android.server.wm.traces.common.DeviceStateDump
20 import com.android.server.wm.traces.common.layers.LayerTraceEntry
21 import com.android.server.wm.traces.common.tags.Tag
22 import com.android.server.wm.traces.common.tags.Transition
23 import com.android.server.wm.traces.common.windowmanager.WindowManagerState
24 import kotlin.math.max
25 import kotlin.math.min
26 
27 /**
28  * Represents the Finite State Machine used by tagging processors and implements adding start and
29  * end tags.
30  * @param tags represents the map of timestamps associated with tag(s).
31  */
32 abstract class FSMState(protected val tags: MutableMap<Long, MutableList<Tag>>) {
processnull33     abstract fun process(
34         previous: DeviceStateDump<WindowManagerState, LayerTraceEntry>?,
35         current: DeviceStateDump<WindowManagerState, LayerTraceEntry>,
36         next: DeviceStateDump<WindowManagerState, LayerTraceEntry>?
37     ): FSMState?
38 
39     protected fun addStartTransitionTag(
40         state: DeviceStateDump<WindowManagerState, LayerTraceEntry>,
41         transition: Transition,
42         layerId: Int = 0,
43         windowToken: String = "",
44         taskId: Int = 0,
45         timestamp: Long = min(state.wmState.timestamp, state.layerState.timestamp)
46     ) {
47         val tagId = ++lastTagId
48         val startTag = Tag(id = tagId, transition, isStartTag = true, layerId = layerId,
49             windowToken = windowToken, taskId = taskId)
50         if (!tags.containsKey(timestamp)) {
51             tags[timestamp] = mutableListOf()
52         }
53         tags.getValue(timestamp).add(startTag)
54     }
55 
addEndTransitionTagnull56     protected fun addEndTransitionTag(
57         state: DeviceStateDump<WindowManagerState, LayerTraceEntry>,
58         transition: Transition,
59         layerId: Int = 0,
60         windowToken: String = "",
61         taskId: Int = 0,
62         timestamp: Long = max(state.wmState.timestamp, state.layerState.timestamp)
63     ) {
64         val endTag = Tag(id = lastTagId, transition, isStartTag = false, layerId = layerId,
65             windowToken = windowToken, taskId = taskId)
66         if (!tags.containsKey(timestamp)) {
67             tags[timestamp] = mutableListOf()
68         }
69         tags.getValue(timestamp).add(endTag)
70     }
71 
hasOpenTagnull72     protected fun hasOpenTag() = tags.values.flatten().size % 2 != 0
73 
74     companion object {
75         private var lastTagId = -1
76     }
77 }
78