Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 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 | |
| 25 | import com.android.server.wm.utils.CoordinateTransforms; |
| 26 | |
Adrian Roos | 2cadc5c | 2018-07-06 02:39:54 -0700 | [diff] [blame] | 27 | import java.io.PrintWriter; |
| 28 | import java.io.StringWriter; |
| 29 | |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 30 | /** |
| 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 | */ |
| 38 | public class ForcedSeamlessRotator { |
| 39 | |
| 40 | private final Matrix mTransform = new Matrix(); |
| 41 | private final float[] mFloat9 = new float[9]; |
Adrian Roos | 2cadc5c | 2018-07-06 02:39:54 -0700 | [diff] [blame] | 42 | private final int mOldRotation; |
| 43 | private final int mNewRotation; |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 44 | |
| 45 | public ForcedSeamlessRotator(int oldRotation, int newRotation, DisplayInfo info) { |
Adrian Roos | 2cadc5c | 2018-07-06 02:39:54 -0700 | [diff] [blame] | 46 | mOldRotation = oldRotation; |
| 47 | mNewRotation = newRotation; |
| 48 | |
Adrian Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 49 | 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 Nair | 4a17e47 | 2018-11-20 08:01:57 -0800 | [diff] [blame] | 78 | 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 Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 86 | } |
Adrian Roos | 2cadc5c | 2018-07-06 02:39:54 -0700 | [diff] [blame] | 87 | |
| 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 Roos | 28c25e2 | 2018-05-31 18:07:28 +0200 | [diff] [blame] | 99 | } |