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