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 {LayerCompositionType} from 'trace/layer_composition_type';
18import {Operation} from 'trace/tree_node/operations/operation';
19import {
20  DUPLICATE_CHIP,
21  GPU_CHIP,
22  HWC_CHIP,
23  MISSING_Z_PARENT_CHIP,
24  RELATIVE_Z_CHIP,
25  RELATIVE_Z_PARENT_CHIP,
26  VISIBLE_CHIP,
27} from 'viewers/common/chip';
28import {UiHierarchyTreeNode} from 'viewers/common/ui_hierarchy_tree_node';
29
30export class AddChips implements Operation<UiHierarchyTreeNode> {
31  private relZParentIds: string[] = [];
32
33  apply(node: UiHierarchyTreeNode): void {
34    this.addAllChipsExceptRelZParent(node);
35    this.addRelZParentChips(node);
36  }
37
38  private addAllChipsExceptRelZParent(node: UiHierarchyTreeNode) {
39    if (!node.isRoot()) {
40      const compositionType = node
41        .getEagerPropertyByName('compositionType')
42        ?.getValue();
43      if (compositionType === LayerCompositionType.GPU) {
44        node.addChip(GPU_CHIP);
45      } else if (compositionType === LayerCompositionType.HWC) {
46        node.addChip(HWC_CHIP);
47      }
48
49      if (node.getEagerPropertyByName('isComputedVisible')?.getValue()) {
50        node.addChip(VISIBLE_CHIP);
51      }
52
53      if (node.getEagerPropertyByName('isDuplicate')?.getValue()) {
54        node.addChip(DUPLICATE_CHIP);
55      }
56
57      const zOrderRelativeOfId = node
58        .getEagerPropertyByName('zOrderRelativeOf')
59        ?.getValue();
60      if (zOrderRelativeOfId && zOrderRelativeOfId !== -1) {
61        node.addChip(RELATIVE_Z_CHIP);
62        this.relZParentIds.push(zOrderRelativeOfId);
63
64        if (node.getEagerPropertyByName('isMissingZParent')?.getValue()) {
65          node.addChip(MISSING_Z_PARENT_CHIP);
66        }
67      }
68    }
69
70    node
71      .getAllChildren()
72      .forEach((child) => this.addAllChipsExceptRelZParent(child));
73  }
74
75  private addRelZParentChips(node: UiHierarchyTreeNode) {
76    const treeLayerId = node.getEagerPropertyByName('id')?.getValue();
77    if (this.relZParentIds.includes(treeLayerId)) {
78      node.addChip(RELATIVE_Z_PARENT_CHIP);
79    }
80
81    node.getAllChildren().forEach((child) => this.addRelZParentChips(child));
82  }
83}
84