xref: /aosp_15_r20/development/tools/winscope/src/parsers/screenshot/parser_screenshot.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright (C) 2024 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 {Timestamp} from 'common/time';
18import {AbstractParser} from 'parsers/legacy/abstract_parser';
19import {CoarseVersion} from 'trace/coarse_version';
20import {MediaBasedTraceEntry} from 'trace/media_based_trace_entry';
21import {TraceType} from 'trace/trace_type';
22
23class ParserScreenshot extends AbstractParser<MediaBasedTraceEntry> {
24  private static readonly MAGIC_NUMBER = [
25    0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
26  ]; // currently only support png files
27
28  override getTraceType(): TraceType {
29    return TraceType.SCREENSHOT;
30  }
31
32  override getCoarseVersion(): CoarseVersion {
33    return CoarseVersion.LATEST;
34  }
35
36  override getMagicNumber(): number[] | undefined {
37    return ParserScreenshot.MAGIC_NUMBER;
38  }
39
40  override getRealToMonotonicTimeOffsetNs(): bigint | undefined {
41    return undefined;
42  }
43
44  override getRealToBootTimeOffsetNs(): bigint | undefined {
45    return undefined;
46  }
47
48  protected override getTimestamp(decodedEntry: number): Timestamp {
49    return this.timestampConverter.makeZeroTimestamp();
50  }
51
52  override decodeTrace(screenshotData: Uint8Array): number[] {
53    return [0]; // require a non-empty array to be returned so trace can provide timestamps
54  }
55
56  override processDecodedEntry(
57    index: number,
58    entry: number,
59  ): MediaBasedTraceEntry {
60    const screenshotData = this.traceFile.file;
61    return new MediaBasedTraceEntry(0, screenshotData, true);
62  }
63}
64
65export {ParserScreenshot};
66