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 
25 /**
26  * Base state for the FSM, check if there are more WM and SF states to process
27  * and ensure there is always a 1:1 correspondence between start and end tags.
28  * If the location of the end of the transition wasn't found, add an end tag at end of trace.
29  */
30 abstract class BaseFsmState(
31     tags: MutableMap<Long, MutableList<Tag>>,
32     internal val logger: (String) -> Unit,
33     internal val transition: Transition
34 ) : FSMState(tags) {
doProcessStatenull35     protected abstract fun doProcessState(
36         previous: DeviceStateDump<WindowManagerState, LayerTraceEntry>?,
37         current: DeviceStateDump<WindowManagerState, LayerTraceEntry>,
38         next: DeviceStateDump<WindowManagerState, LayerTraceEntry>
39     ): FSMState
40 
41     override fun process(
42         previous: DeviceStateDump<WindowManagerState, LayerTraceEntry>?,
43         current: DeviceStateDump<WindowManagerState, LayerTraceEntry>,
44         next: DeviceStateDump<WindowManagerState, LayerTraceEntry>?
45     ): FSMState? {
46         return if (next == null) {
47             // last state
48             val timestamp = current.layerState.timestamp
49             logger.invoke("($timestamp) Trace has reached the end")
50             if (hasOpenTag()) {
51                 logger.invoke("($timestamp) Has an open tag, closing it on the last SF state")
52                 addEndTransitionTag(current, transition)
53             }
54             null
55         } else {
56             doProcessState(previous, current, next)
57         }
58     }
59 }