• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {TraceRect} from 'trace/trace_rect';
19import {TraceRectBuilder} from 'trace/trace_rect_builder';
20import {Computation} from 'trace/tree_node/computation';
21import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
22
23class RectWmFactory {
24  makeDisplayRect(display: HierarchyTreeNode, absoluteZ: number): TraceRect {
25    const displayInfo = display.getEagerPropertyByName('displayInfo');
26    const displayRectWidth =
27      displayInfo?.getChildByName('logicalWidth')?.getValue() ?? 0;
28    const displayRectHeight =
29      displayInfo?.getChildByName('logicalHeight')?.getValue() ?? 0;
30
31    return new TraceRectBuilder()
32      .setX(0)
33      .setY(0)
34      .setWidth(displayRectWidth)
35      .setHeight(displayRectHeight)
36      .setId(display.id)
37      .setName(`Display - ${display.name}`)
38      .setCornerRadius(0)
39      .setGroupId(
40        assertDefined(display.getEagerPropertyByName('id')).getValue(),
41      )
42      .setIsVisible(false)
43      .setIsDisplay(true)
44      .setIsVirtual(false)
45      .setDepth(absoluteZ)
46      .build();
47  }
48
49  makeWindowStateRect(
50    container: HierarchyTreeNode,
51    absoluteZ: number,
52  ): TraceRect | undefined {
53    const displayId = container.getEagerPropertyByName('displayId')?.getValue();
54    if (displayId === undefined) {
55      return undefined;
56    }
57
58    const isVisible =
59      container.getEagerPropertyByName('isComputedVisible')?.getValue() ??
60      false;
61
62    const alpha =
63      container
64        .getEagerPropertyByName('attributes')
65        ?.getChildByName('alpha')
66        ?.getValue() ?? 1;
67
68    const frame = container
69      .getEagerPropertyByName('windowFrames')
70      ?.getChildByName('frame');
71    if (frame === undefined || frame.getAllChildren().length === 0) {
72      return undefined;
73    }
74
75    const rectLeft = assertDefined(frame.getChildByName('left')).getValue();
76    const rectTop = assertDefined(frame.getChildByName('top')).getValue();
77    const rectRight = assertDefined(frame.getChildByName('right')).getValue();
78    const rectBottom = assertDefined(frame.getChildByName('bottom')).getValue();
79
80    return new TraceRectBuilder()
81      .setX(rectLeft)
82      .setY(rectTop)
83      .setWidth(rectRight - rectLeft)
84      .setHeight(rectBottom - rectTop)
85      .setId(container.id)
86      .setName(container.name)
87      .setCornerRadius(0)
88      .setGroupId(displayId)
89      .setIsVisible(isVisible)
90      .setIsDisplay(false)
91      .setIsVirtual(false)
92      .setDepth(absoluteZ)
93      .setOpacity(alpha)
94      .build();
95  }
96}
97
98export class RectsComputation implements Computation {
99  private root: HierarchyTreeNode | undefined;
100  private readonly rectsFactory = new RectWmFactory();
101
102  setRoot(value: HierarchyTreeNode): this {
103    this.root = value;
104    return this;
105  }
106
107  executeInPlace(): void {
108    if (!this.root) {
109      throw Error('root not set');
110    }
111
112    this.root.getAllChildren().forEach((displayContent) => {
113      const displayRect = this.rectsFactory.makeDisplayRect(displayContent, 0);
114      displayContent.setRects([displayRect]);
115
116      let absoluteZ = 1;
117      displayContent.getAllChildren().forEach((child) => {
118        child.forEachNodeDfs((container) => {
119          if (!container.id.startsWith('WindowState ')) return;
120
121          const rect = this.rectsFactory.makeWindowStateRect(
122            container,
123            absoluteZ,
124          );
125          if (!rect) {
126            return;
127          }
128          container.setRects([rect]);
129          absoluteZ++;
130        });
131      });
132    });
133  }
134}
135