• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 {LayerTraceEntry} from 'trace/flickerlib/layers/LayerTraceEntry';
18import {Timestamp, TimestampType} from 'trace/timestamp';
19import {TraceFile} from 'trace/trace_file';
20import {TraceType} from 'trace/trace_type';
21import {AbstractParser} from './abstract_parser';
22import {LayersTraceFileProto} from './proto_types';
23
24class ParserSurfaceFlinger extends AbstractParser {
25  constructor(trace: TraceFile) {
26    super(trace);
27    this.realToElapsedTimeOffsetNs = undefined;
28  }
29
30  override getTraceType(): TraceType {
31    return TraceType.SURFACE_FLINGER;
32  }
33
34  override getMagicNumber(): number[] {
35    return ParserSurfaceFlinger.MAGIC_NUMBER;
36  }
37
38  override decodeTrace(buffer: Uint8Array): any[] {
39    const decoded = LayersTraceFileProto.decode(buffer) as any;
40    if (Object.prototype.hasOwnProperty.call(decoded, 'realToElapsedTimeOffsetNanos')) {
41      this.realToElapsedTimeOffsetNs = BigInt(decoded.realToElapsedTimeOffsetNanos);
42    } else {
43      console.warn('Missing realToElapsedTimeOffsetNanos property on SF trace proto');
44      this.realToElapsedTimeOffsetNs = undefined;
45    }
46    return decoded.entry;
47  }
48
49  override getTimestamp(type: TimestampType, entryProto: any): undefined | Timestamp {
50    const isDump = !Object.prototype.hasOwnProperty.call(entryProto, 'elapsedRealtimeNanos');
51    if (type === TimestampType.ELAPSED) {
52      return isDump
53        ? new Timestamp(type, 0n)
54        : new Timestamp(type, BigInt(entryProto.elapsedRealtimeNanos));
55    } else if (type === TimestampType.REAL && this.realToElapsedTimeOffsetNs !== undefined) {
56      return isDump
57        ? new Timestamp(type, 0n)
58        : new Timestamp(
59            type,
60            this.realToElapsedTimeOffsetNs + BigInt(entryProto.elapsedRealtimeNanos)
61          );
62    }
63    return undefined;
64  }
65
66  override processDecodedEntry(
67    index: number,
68    timestampType: TimestampType,
69    entryProto: any
70  ): LayerTraceEntry {
71    return LayerTraceEntry.fromProto(
72      entryProto.layers.layers,
73      entryProto.displays,
74      BigInt(entryProto.elapsedRealtimeNanos.toString()),
75      entryProto.vsyncId,
76      entryProto.hwcBlob,
77      entryProto.where,
78      this.realToElapsedTimeOffsetNs,
79      timestampType === TimestampType.ELAPSED /*useElapsedTime*/,
80      entryProto.excludesCompositionState ?? false
81    );
82  }
83
84  private realToElapsedTimeOffsetNs: undefined | bigint;
85  private static readonly MAGIC_NUMBER = [0x09, 0x4c, 0x59, 0x52, 0x54, 0x52, 0x41, 0x43, 0x45]; // .LYRTRACE
86}
87
88export {ParserSurfaceFlinger};
89