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 17import {Trace, TraceEntry} from './trace'; 18import {TracePosition} from './trace_position'; 19import {TraceType} from './trace_type'; 20 21export class TraceEntryFinder { 22 static readonly UI_PIPELINE_ORDER = [ 23 TraceType.INPUT_METHOD_CLIENTS, 24 TraceType.INPUT_METHOD_SERVICE, 25 TraceType.INPUT_METHOD_MANAGER_SERVICE, 26 TraceType.PROTO_LOG, 27 TraceType.WINDOW_MANAGER, 28 TraceType.TRANSACTIONS, 29 TraceType.SURFACE_FLINGER, 30 TraceType.SCREEN_RECORDING, 31 ]; 32 33 static findCorrespondingEntry<T>( 34 trace: Trace<T>, 35 position: TracePosition 36 ): TraceEntry<T> | undefined { 37 if (position.entry?.getFullTrace().type === trace.type) { 38 return position.entry as TraceEntry<T>; 39 } 40 41 if (position.frame !== undefined && trace.hasFrameInfo()) { 42 const frame = trace.getFrame(position.frame); 43 if (frame.lengthEntries > 0) { 44 return frame.getEntry(0); 45 } 46 } 47 48 if (position.entry) { 49 const entryTraceType = position.entry.getFullTrace().type; 50 51 const indexPosition = TraceEntryFinder.UI_PIPELINE_ORDER.findIndex((type) => { 52 return type === entryTraceType; 53 }); 54 const indexTrace = TraceEntryFinder.UI_PIPELINE_ORDER.findIndex((type) => { 55 return type === trace.type; 56 }); 57 58 if (indexPosition !== undefined && indexTrace !== undefined) { 59 if (indexPosition < indexTrace) { 60 return trace.findFirstGreaterEntry(position.entry.getTimestamp()); 61 } else { 62 return trace.findLastLowerEntry(position.entry.getTimestamp()); 63 } 64 } 65 } 66 67 return trace.findLastLowerOrEqualEntry(position.timestamp); 68 } 69} 70