• 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 */
16import {WindowManagerState} from 'trace/flickerlib/windows/WindowManagerState';
17import {Timestamp, TimestampType} from 'trace/timestamp';
18import {TraceFile} from 'trace/trace_file';
19import {TraceType} from 'trace/trace_type';
20import {AbstractParser} from './abstract_parser';
21import {WindowManagerTraceFileProto} from './proto_types';
22
23class ParserWindowManager extends AbstractParser {
24  constructor(trace: TraceFile) {
25    super(trace);
26    this.realToElapsedTimeOffsetNs = undefined;
27  }
28
29  override getTraceType(): TraceType {
30    return TraceType.WINDOW_MANAGER;
31  }
32
33  override getMagicNumber(): number[] {
34    return ParserWindowManager.MAGIC_NUMBER;
35  }
36
37  override decodeTrace(buffer: Uint8Array): any[] {
38    const decoded = WindowManagerTraceFileProto.decode(buffer) as any;
39    if (Object.prototype.hasOwnProperty.call(decoded, 'realToElapsedTimeOffsetNanos')) {
40      this.realToElapsedTimeOffsetNs = BigInt(decoded.realToElapsedTimeOffsetNanos);
41    } else {
42      this.realToElapsedTimeOffsetNs = undefined;
43    }
44    return decoded.entry;
45  }
46
47  override getTimestamp(type: TimestampType, entryProto: any): undefined | Timestamp {
48    if (type === TimestampType.ELAPSED) {
49      return new Timestamp(type, BigInt(entryProto.elapsedRealtimeNanos));
50    } else if (type === TimestampType.REAL && this.realToElapsedTimeOffsetNs !== undefined) {
51      return new Timestamp(
52        type,
53        this.realToElapsedTimeOffsetNs + BigInt(entryProto.elapsedRealtimeNanos)
54      );
55    }
56    return undefined;
57  }
58
59  override processDecodedEntry(
60    index: number,
61    timestampType: TimestampType,
62    entryProto: any
63  ): WindowManagerState {
64    return WindowManagerState.fromProto(
65      entryProto.windowManagerService,
66      BigInt(entryProto.elapsedRealtimeNanos.toString()),
67      entryProto.where,
68      this.realToElapsedTimeOffsetNs,
69      timestampType === TimestampType.ELAPSED /*useElapsedTime*/
70    );
71  }
72
73  private realToElapsedTimeOffsetNs: undefined | bigint;
74  private static readonly MAGIC_NUMBER = [0x09, 0x57, 0x49, 0x4e, 0x54, 0x52, 0x41, 0x43, 0x45]; // .WINTRACE
75}
76
77export {ParserWindowManager};
78