blob: bebc5656c2843eb83b1fd83edb251933e9d70cf3 [file] [log] [blame]
Adrian Roos28c25e22018-05-31 18:07:28 +02001/*
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
17package com.android.server.wm;
18
19import static android.view.Surface.ROTATION_270;
20import static android.view.Surface.ROTATION_90;
21
22import android.graphics.Matrix;
23import android.view.DisplayInfo;
24
25import com.android.server.wm.utils.CoordinateTransforms;
26
Adrian Roos2cadc5c2018-07-06 02:39:54 -070027import java.io.PrintWriter;
28import java.io.StringWriter;
29
Adrian Roos28c25e22018-05-31 18:07:28 +020030/**
31 * Helper class for forced seamless rotation.
32 *
33 * Works by transforming the window token back into the old display rotation.
34 *
35 * Uses deferTransactionUntil instead of latching on the buffer size to allow for seamless 180
36 * degree rotations.
37 */
38public class ForcedSeamlessRotator {
39
40 private final Matrix mTransform = new Matrix();
41 private final float[] mFloat9 = new float[9];
Adrian Roos2cadc5c2018-07-06 02:39:54 -070042 private final int mOldRotation;
43 private final int mNewRotation;
Adrian Roos28c25e22018-05-31 18:07:28 +020044
45 public ForcedSeamlessRotator(int oldRotation, int newRotation, DisplayInfo info) {
Adrian Roos2cadc5c2018-07-06 02:39:54 -070046 mOldRotation = oldRotation;
47 mNewRotation = newRotation;
48
Adrian Roos28c25e22018-05-31 18:07:28 +020049 final boolean flipped = info.rotation == ROTATION_90 || info.rotation == ROTATION_270;
50 final int h = flipped ? info.logicalWidth : info.logicalHeight;
51 final int w = flipped ? info.logicalHeight : info.logicalWidth;
52
53 final Matrix tmp = new Matrix();
54 CoordinateTransforms.transformLogicalToPhysicalCoordinates(oldRotation, w, h, mTransform);
55 CoordinateTransforms.transformPhysicalToLogicalCoordinates(newRotation, w, h, tmp);
56 mTransform.postConcat(tmp);
57 }
58
59 /**
60 * Applies a transform to the window token's surface that undoes the effect of the global
61 * display rotation.
62 */
63 public void unrotate(WindowToken token) {
64 token.getPendingTransaction().setMatrix(token.getSurfaceControl(), mTransform, mFloat9);
65 }
66
67 /**
68 * Removes the transform to the window token's surface that undoes the effect of the global
69 * display rotation.
70 *
71 * Removing the transform and the result of the WindowState's layout are both tied to the
72 * WindowState's next frame, such that they apply at the same time the client draws the
73 * window in the new orientation.
74 */
75 public void finish(WindowToken token, WindowState win) {
76 mTransform.reset();
77 token.getPendingTransaction().setMatrix(token.mSurfaceControl, mTransform, mFloat9);
Vishnu Nair4a17e472018-11-20 08:01:57 -080078 if (win.mWinAnimator.mSurfaceController != null) {
79 token.getPendingTransaction().deferTransactionUntil(token.mSurfaceControl,
80 win.mWinAnimator.mSurfaceController.mSurfaceControl.getHandle(),
81 win.getFrameNumber());
82 win.getPendingTransaction().deferTransactionUntil(win.mSurfaceControl,
83 win.mWinAnimator.mSurfaceController.mSurfaceControl.getHandle(),
84 win.getFrameNumber());
85 }
Adrian Roos28c25e22018-05-31 18:07:28 +020086 }
Adrian Roos2cadc5c2018-07-06 02:39:54 -070087
88 public void dump(PrintWriter pw) {
89 pw.print("{old="); pw.print(mOldRotation); pw.print(", new="); pw.print(mNewRotation);
90 pw.print("}");
91 }
92
93 @Override
94 public String toString() {
95 StringWriter sw = new StringWriter();
96 dump(new PrintWriter(sw));
97 return "ForcedSeamlessRotator" + sw.toString();
98 }
Adrian Roos28c25e22018-05-31 18:07:28 +020099}