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.utils
18 
19 import android.graphics.Rect
20 import android.tools.datatypes.Size
21 import android.tools.traces.surfaceflinger.Display
22 import android.tools.traces.surfaceflinger.Layer
23 import android.tools.traces.surfaceflinger.LayerTraceEntry
24 import android.tools.traces.surfaceflinger.Transform
25 
26 class MockLayerTraceEntryBuilder() {
27     private val displays = mutableListOf<Display>()
28     private val layers = mutableListOf<Layer>()
29     private val bounds = Rect(0, 0, 1080, 1920)
30     var timestamp = -1L
31         private set
32 
33     constructor(timestamp: Long) : this() {
34         setTimestamp(timestamp)
35     }
36 
37     init {
38         if (timestamp <= 0L) {
39             timestamp = ++lastTimestamp
40         }
41     }
42 
<lambda>null43     fun addDisplay(rootLayers: List<MockLayerBuilder>): MockLayerTraceEntryBuilder = apply {
44         val displayLayer =
45             MockLayerBuilder("Display").setAbsoluteBounds(bounds).addChildren(rootLayers).build()
46         val displayId = 1L
47         val stackId = 1
48         this.displays.add(
49             Display.from(
50                 id = displayId,
51                 name = "Display",
52                 layerStackId = stackId,
53                 size = Size.from(bounds.width(), bounds.height()),
54                 layerStackSpace = bounds,
55                 transform = Transform.EMPTY,
56                 isVirtual = false,
57                 dpiX = 416.0,
58                 dpiY = 416.0
59             )
60         )
61         this.layers.add(displayLayer)
62     }
63 
<lambda>null64     fun setTimestamp(timestamp: Long): MockLayerTraceEntryBuilder = apply {
65         require(timestamp > 0) { "Timestamp must be a positive value." }
66         this.timestamp = timestamp
67         lastTimestamp = timestamp
68     }
69 
buildnull70     fun build(): LayerTraceEntry {
71         return LayerTraceEntry(
72             elapsedTimestamp = timestamp,
73             clockTimestamp = null,
74             hwcBlob = "",
75             where = "",
76             displays = displays,
77             vSyncId = 100,
78             _rootLayers = layers
79         )
80     }
81 
82     companion object {
83         private var lastTimestamp = 1L
84     }
85 }
86