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 {assertDefined} from 'common/assert_utils';
18import {Computation} from 'trace/tree_node/computation';
19import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
20import {DEFAULT_PROPERTY_TREE_NODE_FACTORY} from 'trace/tree_node/property_tree_node_factory';
21
22export class VisibilityComputation implements Computation {
23  private static readonly VISIBLE = 0;
24
25  private root: HierarchyTreeNode | undefined;
26
27  setRoot(value: HierarchyTreeNode): VisibilityComputation {
28    this.root = value;
29    return this;
30  }
31
32  executeInPlace(): void {
33    if (!this.root) {
34      throw new Error('root not set in VC visibility computation');
35    }
36
37    this.root.forEachNodeDfs((node) => {
38      const isVisible =
39        assertDefined(node.getEagerPropertyByName('visibility')).getValue() ===
40          VisibilityComputation.VISIBLE &&
41        (node.isRoot() ||
42          node
43            .getZParent()
44            ?.getEagerPropertyByName('isComputedVisible')
45            ?.getValue());
46
47      node.addEagerProperty(
48        DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeCalculatedProperty(
49          node.id,
50          'isComputedVisible',
51          isVisible,
52        ),
53      );
54    });
55  }
56}
57