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 {HierarchyTreeBuilder} from 'parsers/hierarchy_tree_builder'; 19import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node'; 20import {PropertiesProvider} from 'trace/tree_node/properties_provider'; 21import { 22 PropertySource, 23 PropertyTreeNode, 24} from 'trace/tree_node/property_tree_node'; 25import {DEFAULT_PROPERTY_TREE_NODE_FACTORY} from 'trace/tree_node/property_tree_node_factory'; 26 27export class HierarchyTreeBuilderSf extends HierarchyTreeBuilder { 28 protected override buildIdentifierToChildrenMap( 29 layers: PropertiesProvider[], 30 ): Map<string | number, readonly HierarchyTreeNode[]> { 31 const map = layers.reduce((map, layer) => { 32 const layerProperties = layer.getEagerProperties(); 33 const layerNode = this.makeNode( 34 layerProperties.id, 35 layerProperties.name, 36 layer, 37 ); 38 const layerId = assertDefined( 39 layerProperties.getChildByName('id'), 40 ).getValue(); 41 42 const curr = map.get(layerId); 43 if (curr) { 44 curr.push(layerNode); 45 layer.addEagerProperty( 46 DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeCalculatedProperty( 47 layerProperties.id, 48 'isDuplicate', 49 true, 50 ), 51 ); 52 } else { 53 map.set(layerId, [layerNode]); 54 } 55 return map; 56 }, new Map<string | number, HierarchyTreeNode[]>()); 57 return map; 58 } 59 60 protected override assignParentChildRelationships( 61 root: HierarchyTreeNode, 62 identifierToChildren: Map<string | number, HierarchyTreeNode[]>, 63 isRoot?: boolean, 64 ): void { 65 for (const children of identifierToChildren.values()) { 66 children.forEach((child) => { 67 const parentIdNode = assertDefined( 68 child.getEagerPropertyByName('parent'), 69 ); 70 const isDefault = parentIdNode.source === PropertySource.DEFAULT; 71 const parentId = this.getIdentifierValue(parentIdNode); 72 const parent = identifierToChildren.get(parentId)?.at(0); 73 if (!isDefault && parent) { 74 this.setParentChildRelationship(parent, child); 75 } else { 76 this.setParentChildRelationship(root, child); 77 } 78 }); 79 } 80 } 81 82 private getIdentifierValue(identifier: PropertyTreeNode): number { 83 return Number(identifier.getValue()); 84 } 85} 86