1 /*
2  * Copyright (C) 2018 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 
17 package com.android.server.wm.utils;
18 
19 import static android.view.Surface.ROTATION_0;
20 import static android.view.Surface.ROTATION_180;
21 import static android.view.Surface.ROTATION_270;
22 import static android.view.Surface.ROTATION_90;
23 
24 import android.annotation.Dimension;
25 import android.graphics.Matrix;
26 import android.view.DisplayInfo;
27 import android.view.Surface;
28 import android.view.Surface.Rotation;
29 
30 public class CoordinateTransforms {
31 
CoordinateTransforms()32     private CoordinateTransforms() {
33     }
34 
35     /**
36      * Sets a matrix such that given a rotation, it transforms physical display
37      * coordinates to that rotation's logical coordinates.
38      *
39      * @param rotation the rotation to which the matrix should transform
40      * @param out      the matrix to be set
41      */
transformPhysicalToLogicalCoordinates(@otation int rotation, @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out)42     public static void transformPhysicalToLogicalCoordinates(@Rotation int rotation,
43             @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) {
44         switch (rotation) {
45             case ROTATION_0:
46                 out.reset();
47                 break;
48             case ROTATION_90:
49                 out.setRotate(270);
50                 out.postTranslate(0, physicalWidth);
51                 break;
52             case ROTATION_180:
53                 out.setRotate(180);
54                 out.postTranslate(physicalWidth, physicalHeight);
55                 break;
56             case ROTATION_270:
57                 out.setRotate(90);
58                 out.postTranslate(physicalHeight, 0);
59                 break;
60             default:
61                 throw new IllegalArgumentException("Unknown rotation: " + rotation);
62         }
63     }
64 
65     /**
66      * Sets a matrix such that given a rotation, it transforms that rotation's logical coordinates
67      * to physical coordinates.
68      *
69      * @param rotation the rotation to which the matrix should transform
70      * @param out      the matrix to be set
71      */
transformLogicalToPhysicalCoordinates(@otation int rotation, @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out)72     public static void transformLogicalToPhysicalCoordinates(@Rotation int rotation,
73             @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) {
74         switch (rotation) {
75             case ROTATION_0:
76                 out.reset();
77                 break;
78             case ROTATION_90:
79                 out.setRotate(90);
80                 out.preTranslate(0, -physicalWidth);
81                 break;
82             case ROTATION_180:
83                 out.setRotate(180);
84                 out.preTranslate(-physicalWidth, -physicalHeight);
85                 break;
86             case ROTATION_270:
87                 out.setRotate(270);
88                 out.preTranslate(-physicalHeight, 0);
89                 break;
90             default:
91                 throw new IllegalArgumentException("Unknown rotation: " + rotation);
92         }
93     }
94 
95     /**
96      * Sets a matrix such that given a two rotations, that it transforms coordinates given in the
97      * old rotation to coordinates that refer to the same physical location in the new rotation.
98      *
99      * @param oldRotation the rotation to transform from
100      * @param newRotation the rotation to transform to
101      * @param info the display info
102      * @param out a matrix that will be set to the transform
103      */
transformToRotation(@otation int oldRotation, @Rotation int newRotation, DisplayInfo info, Matrix out)104     public static void transformToRotation(@Rotation int oldRotation,
105             @Rotation int newRotation, DisplayInfo info, Matrix out) {
106         final boolean flipped = info.rotation == ROTATION_90 || info.rotation == ROTATION_270;
107         final int h = flipped ? info.logicalWidth : info.logicalHeight;
108         final int w = flipped ? info.logicalHeight : info.logicalWidth;
109 
110         final Matrix tmp = new Matrix();
111         transformLogicalToPhysicalCoordinates(oldRotation, w, h, out);
112         transformPhysicalToLogicalCoordinates(newRotation, w, h, tmp);
113         out.postConcat(tmp);
114     }
115 
116     /**
117      * Sets a matrix such that given a two rotations, that it transforms coordinates given in the
118      * old rotation to coordinates that refer to the same physical location in the new rotation.
119      *
120      * @param oldRotation the rotation to transform from
121      * @param newRotation the rotation to transform to
122      * @param newWidth the width of the area to transform, in the new rotation
123      * @param newHeight the height of the area to transform, in the new rotation
124      * @param out a matrix that will be set to the transform
125      */
transformToRotation(@otation int oldRotation, @Rotation int newRotation, int newWidth, int newHeight, Matrix out)126     public static void transformToRotation(@Rotation int oldRotation,
127             @Rotation int newRotation, int newWidth, int newHeight, Matrix out) {
128         final boolean flipped = newRotation == ROTATION_90 || newRotation == ROTATION_270;
129         final int h = flipped ? newWidth : newHeight;
130         final int w = flipped ? newHeight : newWidth;
131 
132         final Matrix tmp = new Matrix();
133         transformLogicalToPhysicalCoordinates(oldRotation, w, h, out);
134         transformPhysicalToLogicalCoordinates(newRotation, w, h, tmp);
135         out.postConcat(tmp);
136     }
137 
138     /** Computes the matrix that rotates the original w x h by the rotation delta. */
computeRotationMatrix(int rotationDelta, int w, int h, Matrix outMatrix)139     public static void computeRotationMatrix(int rotationDelta, int w, int h, Matrix outMatrix) {
140         switch (rotationDelta) {
141             case Surface.ROTATION_0:
142                 outMatrix.reset();
143                 break;
144             case Surface.ROTATION_90:
145                 outMatrix.setRotate(90);
146                 outMatrix.postTranslate(h, 0);
147                 break;
148             case Surface.ROTATION_180:
149                 outMatrix.setRotate(180);
150                 outMatrix.postTranslate(w, h);
151                 break;
152             case Surface.ROTATION_270:
153                 outMatrix.setRotate(270);
154                 outMatrix.postTranslate(0, w);
155                 break;
156         }
157     }
158 }
159