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/rect';
18import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
19
20export class RawDataUtils {
21  static isEmptyObj(obj: PropertyTreeNode): boolean {
22    if (RawDataUtils.isColor(obj)) {
23      return RawDataUtils.isEmptyColor(obj);
24    }
25
26    if (RawDataUtils.isRect(obj)) {
27      return Rect.from(obj).isEmpty();
28    }
29
30    return false;
31  }
32
33  static isColor(obj: PropertyTreeNode): boolean {
34    return (
35      (obj.getChildByName('r') !== undefined &&
36        obj.getChildByName('g') !== undefined &&
37        obj.getChildByName('b') !== undefined) ||
38      obj.getChildByName('a') !== undefined
39    );
40  }
41
42  static isRect(obj: PropertyTreeNode): boolean {
43    return (
44      (obj.getChildByName('right') !== undefined &&
45        obj.getChildByName('bottom') !== undefined) ||
46      (obj.getChildByName('left') !== undefined &&
47        obj.getChildByName('top') !== undefined)
48    );
49  }
50
51  static isBuffer(obj: PropertyTreeNode): boolean {
52    return (
53      obj.getChildByName('stride') !== undefined &&
54      obj.getChildByName('format') !== undefined
55    );
56  }
57
58  static isSize(obj: PropertyTreeNode): boolean {
59    return (
60      obj.getAllChildren().length <= 2 &&
61      (obj.getChildByName('w') !== undefined ||
62        obj.getChildByName('h') !== undefined)
63    );
64  }
65
66  static isPosition(obj: PropertyTreeNode): boolean {
67    return (
68      obj.getAllChildren().length <= 2 &&
69      (obj.getChildByName('x') !== undefined ||
70        obj.getChildByName('y') !== undefined)
71    );
72  }
73
74  static isRegion(obj: PropertyTreeNode): boolean {
75    const rect = obj.getChildByName('rect');
76    return (
77      rect !== undefined &&
78      rect
79        .getAllChildren()
80        .every((innerRect: PropertyTreeNode) => RawDataUtils.isRect(innerRect))
81    );
82  }
83
84  static isMatrix(obj: PropertyTreeNode): boolean {
85    return (
86      !obj.getChildByName('type') &&
87      (obj.getChildByName('dsdx') !== undefined ||
88        obj.getChildByName('dtdx') !== undefined ||
89        obj.getChildByName('dsdy') !== undefined ||
90        obj.getChildByName('dtdy') !== undefined)
91    );
92  }
93
94  private static isEmptyColor(color: PropertyTreeNode): boolean {
95    const [r, g, b, a] = [
96      color.getChildByName('r')?.getValue() ?? 0,
97      color.getChildByName('g')?.getValue() ?? 0,
98      color.getChildByName('b')?.getValue() ?? 0,
99      color.getChildByName('a')?.getValue() ?? 0,
100    ];
101    if (a === 0) return true;
102    return r < 0 || g < 0 || b < 0;
103  }
104}
105