xref: /aosp_15_r20/development/tools/winscope/src/trace/geometry_factory.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 {Rect} from 'common/geometry/rect';
18import {Region} from 'common/geometry/region';
19import {PropertyTreeNode} from './tree_node/property_tree_node';
20
21export class GeometryFactory {
22  static makeRect(node: PropertyTreeNode): Rect {
23    const left = node.getChildByName('left')?.getValue() ?? 0;
24    const top = node.getChildByName('top')?.getValue() ?? 0;
25    const right = node.getChildByName('right')?.getValue() ?? 0;
26    const bottom = node.getChildByName('bottom')?.getValue() ?? 0;
27    return new Rect(left, top, right - left, bottom - top);
28  }
29
30  static makeRegion(node: PropertyTreeNode): Region {
31    const rects =
32      node
33        .getChildByName('rect')
34        ?.getAllChildren()
35        .map((rectNode) => GeometryFactory.makeRect(rectNode)) ?? [];
36    return new Region(rects);
37  }
38}
39