• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import static android.view.Surface.ROTATION_270;
20 import static android.view.Surface.ROTATION_90;
21 
22 import android.graphics.Matrix;
23 import android.view.DisplayInfo;
24 import android.view.Surface.Rotation;
25 import android.view.SurfaceControl;
26 import android.view.SurfaceControl.Transaction;
27 
28 import com.android.server.wm.utils.CoordinateTransforms;
29 
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32 
33 /**
34  * Helper class for seamless rotation.
35  *
36  * Works by transforming the {@link WindowState} back into the old display rotation.
37  */
38 public class SeamlessRotator {
39 
40     private final Matrix mTransform = new Matrix();
41     private final float[] mFloat9 = new float[9];
42     private final int mOldRotation;
43     private final int mNewRotation;
44     /* If the seamless rotator is used to rotate part of the hierarchy, then provide a transform
45      * hint based on the display orientation if the entire display was rotated. When the display
46      * orientation matches the hierarchy orientation, the fixed transform hint will be removed.
47      * This will prevent allocating different buffer sizes by the graphic producers when the
48      * orientation of a layer changes.
49      */
50     private final boolean mApplyFixedTransformHint;
51     private final int mFixedTransformHint;
52 
53 
SeamlessRotator(@otation int oldRotation, @Rotation int newRotation, DisplayInfo info, boolean applyFixedTransformationHint)54     public SeamlessRotator(@Rotation int oldRotation, @Rotation int newRotation, DisplayInfo info,
55             boolean applyFixedTransformationHint) {
56         mOldRotation = oldRotation;
57         mNewRotation = newRotation;
58         mApplyFixedTransformHint = applyFixedTransformationHint;
59         mFixedTransformHint = oldRotation;
60         final boolean flipped = info.rotation == ROTATION_90 || info.rotation == ROTATION_270;
61         final int pH = flipped ? info.logicalWidth : info.logicalHeight;
62         final int pW = flipped ? info.logicalHeight : info.logicalWidth;
63         // Initialize transform matrix by physical size.
64         final Matrix tmp = new Matrix();
65         CoordinateTransforms.transformLogicalToPhysicalCoordinates(oldRotation, pW, pH, mTransform);
66         CoordinateTransforms.transformPhysicalToLogicalCoordinates(newRotation, pW, pH, tmp);
67         mTransform.postConcat(tmp);
68     }
69 
70     /**
71      * Applies a transform to the {@link WindowContainer} surface that undoes the effect of the
72      * global display rotation.
73      */
unrotate(Transaction transaction, WindowContainer win)74     public void unrotate(Transaction transaction, WindowContainer win) {
75         applyTransform(transaction, win.getSurfaceControl());
76         // WindowState sets the position of the window so transform the position and update it.
77         final float[] winSurfacePos = {win.mLastSurfacePosition.x, win.mLastSurfacePosition.y};
78         mTransform.mapPoints(winSurfacePos);
79         transaction.setPosition(win.getSurfaceControl(), winSurfacePos[0], winSurfacePos[1]);
80         if (mApplyFixedTransformHint) {
81             transaction.setFixedTransformHint(win.mSurfaceControl, mFixedTransformHint);
82         }
83     }
84 
applyTransform(Transaction t, SurfaceControl sc)85     void applyTransform(Transaction t, SurfaceControl sc) {
86         t.setMatrix(sc, mTransform, mFloat9);
87     }
88 
89     /**
90      * Returns the rotation of the display before it started rotating.
91      *
92      * @return the old rotation of the display
93      */
94     @Rotation
getOldRotation()95     public int getOldRotation() {
96         return mOldRotation;
97     }
98 
99     /**
100      * Removes the transform and sets the previously known surface position for {@link WindowState}
101      * surface that undoes the effect of the global display rotation.
102      *
103      * Removing the transform and the result of the {@link WindowState} layout are both tied to the
104      * {@link WindowState} next frame, such that they apply at the same time the client draws the
105      * window in the new orientation.
106      */
finish(Transaction t, WindowContainer win)107     void finish(Transaction t, WindowContainer win) {
108         if (win.mSurfaceControl == null || !win.mSurfaceControl.isValid()) {
109             return;
110         }
111 
112         setIdentityMatrix(t, win.mSurfaceControl);
113         t.setPosition(win.mSurfaceControl, win.mLastSurfacePosition.x, win.mLastSurfacePosition.y);
114         if (mApplyFixedTransformHint) {
115             t.unsetFixedTransformHint(win.mSurfaceControl);
116         }
117     }
118 
setIdentityMatrix(Transaction t, SurfaceControl sc)119     void setIdentityMatrix(Transaction t, SurfaceControl sc) {
120         t.setMatrix(sc, Matrix.IDENTITY_MATRIX, mFloat9);
121     }
122 
dump(PrintWriter pw)123     public void dump(PrintWriter pw) {
124         pw.print("{old="); pw.print(mOldRotation); pw.print(", new="); pw.print(mNewRotation);
125         pw.print("}");
126     }
127 
128     @Override
toString()129     public String toString() {
130         StringWriter sw = new StringWriter();
131         dump(new PrintWriter(sw));
132         return "ForcedSeamlessRotator" + sw.toString();
133     }
134 }
135