xref: /aosp_15_r20/development/tools/winscope/src/parsers/window_manager/hierarchy_tree_builder_wm.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
22
23export class HierarchyTreeBuilderWm extends HierarchyTreeBuilder {
24  protected override buildIdentifierToChildrenMap(
25    containers: PropertiesProvider[],
26  ): Map<string, readonly HierarchyTreeNode[]> {
27    const map = containers.reduce((map, container) => {
28      const containerProperties = container.getEagerProperties();
29      const containerNode = this.makeNode(
30        containerProperties.id,
31        this.getSubtreeName(containerProperties.name),
32        container,
33      );
34      const token = assertDefined(
35        containerProperties.getChildByName('token'),
36      ).getValue();
37      map.set(token, [containerNode]);
38      return map;
39    }, new Map<string, HierarchyTreeNode[]>());
40    return map;
41  }
42
43  protected override assignParentChildRelationships(
44    node: HierarchyTreeNode,
45    identifierToChildren: Map<string | number, HierarchyTreeNode[]>,
46    isRoot?: boolean,
47  ): void {
48    let childrenTokens: readonly PropertyTreeNode[] | undefined;
49    if (isRoot) {
50      const rootWindowContainerProps = assertDefined(this.children)
51        .at(0)
52        ?.getEagerProperties();
53      childrenTokens =
54        rootWindowContainerProps
55          ?.getChildByName('children')
56          ?.getAllChildren() ?? [];
57    } else {
58      childrenTokens =
59        node.getEagerPropertyByName('children')?.getAllChildren() ?? [];
60    }
61    for (const childToken of childrenTokens) {
62      const child = identifierToChildren.get(childToken.getValue())?.at(0);
63      if (child) {
64        this.setParentChildRelationship(node, child);
65        this.assignParentChildRelationships(child, identifierToChildren);
66      }
67    }
68  }
69
70  private getSubtreeName(tokenAndName: string): string {
71    const splitId = tokenAndName.split(' ');
72    return splitId.slice(1, splitId.length).join(' ');
73  }
74}
75