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 {Transform} from 'parsers/surface_flinger/transform_utils';
18import {Operation} from 'trace/tree_node/operations/operation';
19import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
20import {DEFAULT_PROPERTY_TREE_NODE_FACTORY} from 'trace/tree_node/property_tree_node_factory';
21
22export class UpdateTransforms implements Operation<PropertyTreeNode> {
23  apply(value: PropertyTreeNode): void {
24    this.updateTransform(
25      value.getChildByName('transform'),
26      value.getChildByName('position'),
27    );
28
29    this.updateTransform(
30      value.getChildByName('requestedTransform'),
31      value.getChildByName('requestedPosition'),
32    );
33
34    this.updateTransform(value.getChildByName('bufferTransform'), undefined);
35
36    const inputWindowInfo = value.getChildByName('inputWindowInfo');
37    if (inputWindowInfo) {
38      this.updateTransform(
39        inputWindowInfo.getChildByName('transform'),
40        undefined,
41      );
42    }
43  }
44
45  private updateTransform(
46    transformNode: PropertyTreeNode | undefined,
47    positionNode: PropertyTreeNode | undefined,
48  ) {
49    if (!transformNode) return;
50    if (transformNode.getChildByName('matrix')) return;
51
52    const newMatrix = Transform.from(transformNode, positionNode).matrix;
53    transformNode.addOrReplaceChild(
54      DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeCalculatedProperty(
55        transformNode.id,
56        'matrix',
57        newMatrix,
58      ),
59    );
60    transformNode.removeChild(`${transformNode.id}.dsdx`);
61    transformNode.removeChild(`${transformNode.id}.dtdx`);
62    transformNode.removeChild(`${transformNode.id}.dsdy`);
63    transformNode.removeChild(`${transformNode.id}.dtdy`);
64  }
65}
66