xref: /aosp_15_r20/development/tools/winscope/src/parsers/surface_flinger/operations/update_transforms.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 {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.updateDeprecatedTransform(
25      value.getChildByName('transform'),
26      value.getChildByName('position'),
27    );
28
29    this.updateDeprecatedTransform(
30      value.getChildByName('requestedTransform'),
31      value.getChildByName('requestedPosition'),
32    );
33
34    this.updateDeprecatedTransform(
35      value.getChildByName('bufferTransform'),
36      undefined,
37    );
38
39    const inputWindowInfo = value.getChildByName('inputWindowInfo');
40    if (inputWindowInfo) {
41      this.updateDeprecatedTransform(
42        inputWindowInfo.getChildByName('transform'),
43        undefined,
44      );
45    }
46  }
47
48  private updateDeprecatedTransform(
49    transformNode: PropertyTreeNode | undefined,
50    positionNode: PropertyTreeNode | undefined,
51  ) {
52    if (!transformNode) return;
53    if (transformNode.getChildByName('matrix')) return;
54
55    this.adjustDeprecatedTransformNode(transformNode);
56
57    const newMatrix = Transform.from(transformNode, positionNode).matrix;
58    transformNode.addOrReplaceChild(
59      DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeCalculatedProperty(
60        transformNode.id,
61        'matrix',
62        newMatrix,
63      ),
64    );
65    transformNode.removeChild(`${transformNode.id}.dsdx`);
66    transformNode.removeChild(`${transformNode.id}.dtdx`);
67    transformNode.removeChild(`${transformNode.id}.dsdy`);
68    transformNode.removeChild(`${transformNode.id}.dtdy`);
69  }
70
71  // Adjust the transform node to correct for the transforms that were incorrectly
72  // written to the proto. Any transforms that are newly logged from the platform should
73  // be carefully written to the proto correctly, and should not be adjusted here.
74  private adjustDeprecatedTransformNode(transformNode: PropertyTreeNode) {
75    // Get the corrected values from the transform node.
76    const dsdx = transformNode.getChildByName('dsdx')?.getValue() ?? 0;
77    const dtdx = transformNode.getChildByName('dsdy')?.getValue() ?? 0;
78    const dtdy = transformNode.getChildByName('dtdx')?.getValue() ?? 0;
79    const dsdy = transformNode.getChildByName('dtdy')?.getValue() ?? 0;
80
81    const id = transformNode.id;
82    transformNode.addOrReplaceChild(
83      DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeProtoProperty(id, 'dsdx', dsdx),
84    );
85    transformNode.addOrReplaceChild(
86      DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeProtoProperty(id, 'dtdx', dtdx),
87    );
88    transformNode.addOrReplaceChild(
89      DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeProtoProperty(id, 'dtdy', dtdy),
90    );
91    transformNode.addOrReplaceChild(
92      DEFAULT_PROPERTY_TREE_NODE_FACTORY.makeProtoProperty(id, 'dsdy', dsdy),
93    );
94  }
95}
96