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 ZOrderPathsComputation implements Computation { 23 private root: HierarchyTreeNode | undefined; 24 25 setRoot(value: HierarchyTreeNode): ZOrderPathsComputation { 26 this.root = value; 27 return this; 28 } 29 30 executeInPlace(): void { 31 if (!this.root) { 32 throw Error('root not set'); 33 } 34 35 this.updateZOrderParents(this.root); 36 this.root.forEachNodeDfs((node) => { 37 if (node.id === 'LayerTraceEntry root') return; 38 const zOrderPath = this.getZOrderPath(node); 39 node.addEagerProperty( 40 DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeCalculatedProperty( 41 `${node.id}`, 42 'zOrderPath', 43 zOrderPath, 44 ), 45 ); 46 }); 47 } 48 49 private updateZOrderParents(root: HierarchyTreeNode) { 50 const layerIdToTreeNode = new Map<number, HierarchyTreeNode>(); 51 root.forEachNodeDfs((node) => { 52 if (node.isRoot()) return; 53 layerIdToTreeNode.set( 54 assertDefined(node.getEagerPropertyByName('id')).getValue(), 55 node, 56 ); 57 }); 58 59 root.forEachNodeDfs((node) => { 60 const zOrderRelativeOf = node 61 .getEagerPropertyByName('zOrderRelativeOf') 62 ?.getValue(); 63 if (zOrderRelativeOf && zOrderRelativeOf !== -1) { 64 const zParent = layerIdToTreeNode.get(zOrderRelativeOf); 65 if (!zParent) { 66 node.addEagerProperty( 67 DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeCalculatedProperty( 68 node.id, 69 'isMissingZParent', 70 true, 71 ), 72 ); 73 return; 74 } 75 node.setZParent(zParent); 76 } 77 }); 78 } 79 80 private getZOrderPath(node: HierarchyTreeNode | undefined): number[] { 81 if (!node) return []; 82 83 const zOrderPath = this.getZOrderPath(node.getZParent()); 84 const z = node.getEagerPropertyByName('z')?.getValue(); 85 if (z !== undefined) zOrderPath.push(z); 86 return zOrderPath; 87 } 88} 89