1/*
2 * Copyright 2021, 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 { Display, LayerTraceEntry, LayerTraceEntryBuilder, toRect, toSize, toTransform } from "../common"
18import Layer from './Layer'
19import { VISIBLE_CHIP, RELATIVE_Z_PARENT_CHIP, MISSING_LAYER } from '../treeview/Chips'
20
21LayerTraceEntry.fromProto = function (protos: any[], displayProtos: any[],
22        timestamp: number, hwcBlob: string, where: string = ''): LayerTraceEntry {
23    const layers = protos.map(it => Layer.fromProto(it));
24    const displays = (displayProtos || []).map(it => newDisplay(it));
25    const builder = new LayerTraceEntryBuilder(timestamp, layers, displays, hwcBlob, where);
26    const entry: LayerTraceEntry = builder.build();
27
28    updateChildren(entry);
29    addAttributes(entry, protos);
30    return entry;
31}
32
33function addAttributes(entry: LayerTraceEntry, protos: any) {
34    entry.kind = "entry"
35    // There no JVM/JS translation for Longs yet
36    entry.timestampMs = entry.timestamp.toString()
37    entry.rects = entry.visibleLayers
38        .sort((a, b) => (b.absoluteZ > a.absoluteZ) ? 1 : (a.absoluteZ == b.absoluteZ) ? 0 : -1)
39        .map(it => it.rect);
40
41    // Avoid parsing the entry root because it is an array of layers
42    // containing all trace information, this slows down the property tree.
43    // Instead parse only key properties for debugging
44    const entryIds = {}
45    protos.forEach(it =>
46        entryIds[it.id] = `\nparent=${it.parent}\ntype=${it.type}\nname=${it.name}`
47    );
48    entry.proto = entryIds;
49    entry.shortName = entry.name;
50    entry.chips = [];
51    entry.isVisible = true;
52}
53
54function updateChildren(entry: LayerTraceEntry) {
55    entry.flattenedLayers.forEach(it => {
56        if (it.isVisible) {
57            it.chips.push(VISIBLE_CHIP);
58        }
59        if (it.zOrderRelativeOf) {
60            it.chips.push(RELATIVE_Z_PARENT_CHIP);
61        }
62        if (it.isMissing) {
63            it.chips.push(MISSING_LAYER);
64        }
65    });
66}
67
68function newDisplay(proto: any): Display {
69    return new Display(
70        proto.id,
71        proto.name,
72        proto.layerStack,
73        toSize(proto.size),
74        toRect(proto.layerStackSpaceRect),
75        toTransform(proto.transform)
76    )
77}
78
79export default LayerTraceEntry;
80