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.surfaceflinger
18 
19 import android.tools.Timestamp
20 import android.tools.Trace
21 
22 /**
23  * Contains a collection of parsed Layers trace entries and assertions to apply over a single entry.
24  *
25  * Each entry is parsed into a list of [LayerTraceEntry] objects.
26  *
27  * This is a generic object that is reused by both Flicker and Winscope and cannot access internal
28  * Java/Android functionality
29  */
30 data class LayersTrace(override val entries: Collection<LayerTraceEntry>) : Trace<LayerTraceEntry> {
31     override fun toString(): String {
32         return "LayersTrace(Start: ${entries.firstOrNull()}, " + "End: ${entries.lastOrNull()})"
33     }
34 
35     fun vSyncSlice(from: Int, to: Int): LayersTrace {
36         return LayersTrace(
37             this.entries.dropWhile { it.vSyncId < from }.dropLastWhile { it.vSyncId > to }
38         )
39     }
40 
41     override fun slice(startTimestamp: Timestamp, endTimestamp: Timestamp): LayersTrace {
42         return LayersTrace(
43             entries
44                 .dropWhile { it.timestamp < startTimestamp }
45                 .dropLastWhile { it.timestamp > endTimestamp }
46         )
47     }
48 
49     fun getEntryForTransaction(transaction: Transaction): LayerTraceEntry {
50         require(
51             this.entries.first().vSyncId <= transaction.appliedVSyncId &&
52                 transaction.appliedVSyncId <= this.entries.last().vSyncId
53         ) {
54             "Finish transaction not in layer trace"
55         }
56         return this.entries.first { it.vSyncId >= transaction.appliedVSyncId }
57     }
58 
59     fun getFirstEntryWithOnDisplayAfter(timestamp: Timestamp): LayerTraceEntry {
60         return this.entries.firstOrNull {
61             it.timestamp >= timestamp && it.displays.any { display -> display.isOn }
62         }
63             ?: error("No entry after $timestamp in layer trace with on display.")
64     }
65 
66     fun getLastEntryWithOnDisplayBefore(timestamp: Timestamp): LayerTraceEntry {
67         return this.entries.lastOrNull {
68             it.timestamp <= timestamp && it.displays.any { display -> display.isOn }
69         }
70             ?: error("No entry before $timestamp in layer trace with on display.")
71     }
72 
73     fun getLayerDescriptorById(layerId: Int): LayerDescriptor? {
74         for (entry in this.entries) {
75             val layer = entry.getLayerById(layerId)
76             if (layer != null) {
77                 return LayerDescriptor(layer)
78             }
79         }
80 
81         return null
82     }
83 }
84