Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 17 | package com.android.server.wm; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 18 | |
Vadim Caen | 311fef7 | 2019-11-05 14:53:06 +0100 | [diff] [blame] | 19 | import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 20 | import static com.android.server.wm.ProtoLogGroup.WM_SHOW_SURFACE_ALLOC; |
Peiyong Lin | 67f3ba1 | 2018-08-23 10:26:45 -0700 | [diff] [blame] | 21 | import static com.android.server.wm.ScreenRotationAnimationProto.ANIMATION_RUNNING; |
| 22 | import static com.android.server.wm.ScreenRotationAnimationProto.STARTED; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Chong Zhang | 97782b4 | 2015-10-07 16:01:23 -0700 | [diff] [blame] | 25 | import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER; |
Chong Zhang | 97782b4 | 2015-10-07 16:01:23 -0700 | [diff] [blame] | 26 | import static com.android.server.wm.WindowStateAnimator.WINDOW_FREEZE_LAYER; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 27 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 29 | import android.graphics.Matrix; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 30 | import android.graphics.Point; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 31 | import android.graphics.Rect; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 32 | import android.util.Slog; |
Steven Timotius | f2d6889 | 2017-08-28 17:00:01 -0700 | [diff] [blame] | 33 | import android.util.proto.ProtoOutputStream; |
Craig Mautner | 6881a10 | 2012-07-27 13:04:51 -0700 | [diff] [blame] | 34 | import android.view.Display; |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 35 | import android.view.DisplayInfo; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 36 | import android.view.Surface; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 37 | import android.view.Surface.OutOfResourcesException; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 38 | import android.view.SurfaceControl; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 39 | import android.view.animation.Animation; |
| 40 | import android.view.animation.AnimationUtils; |
| 41 | import android.view.animation.Transformation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 42 | |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 43 | import com.android.server.protolog.common.ProtoLog; |
| 44 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 45 | import java.io.PrintWriter; |
| 46 | |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 47 | /** |
| 48 | * This class handles the rotation animation when the device is rotated. |
| 49 | * |
| 50 | * <p> |
| 51 | * The screen rotation animation is composed of 4 different part: |
| 52 | * <ul> |
| 53 | * <li> The screenshot: <p> |
| 54 | * A screenshot of the whole screen prior the change of orientation is taken to hide the |
| 55 | * element resizing below. The screenshot is then animated to rotate and cross-fade to |
| 56 | * the new orientation with the content in the new orientation. |
| 57 | * |
| 58 | * <li> The windows on the display: <p>y |
| 59 | * Once the device is rotated, the screen and its content are in the new orientation. The |
| 60 | * animation first rotate the new content into the old orientation to then be able to |
| 61 | * animate to the new orientation |
| 62 | * |
| 63 | * <li> The exiting Blackframe: <p> |
| 64 | * Because the change of orientation might change the width and height of the content (i.e |
| 65 | * when rotating from portrait to landscape) we "crop" the new content using black frames |
| 66 | * around the screenshot so the new content does not go beyond the screenshot's bounds |
| 67 | * |
| 68 | * <li> The entering Blackframe: <p> |
| 69 | * The enter Blackframe is similar to the exit Blackframe but is only used when a custom |
| 70 | * rotation animation is used and matches the new content size instead of the screenshot. |
| 71 | * </ul> |
| 72 | * |
| 73 | * Each part has its own Surface which are then animated by {@link SurfaceAnimator}s. |
| 74 | */ |
Craig Mautner | e32c307 | 2012-03-12 15:25:35 -0700 | [diff] [blame] | 75 | class ScreenRotationAnimation { |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 76 | private static final String TAG = TAG_WITH_CLASS_NAME ? "ScreenRotationAnimation" : TAG_WM; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 77 | |
Chong Zhang | 97782b4 | 2015-10-07 16:01:23 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Layers for screen rotation animation. We put these layers above |
| 80 | * WINDOW_FREEZE_LAYER so that screen freeze will cover all windows. |
| 81 | */ |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 82 | private static final int SCREEN_FREEZE_LAYER_BASE = WINDOW_FREEZE_LAYER + TYPE_LAYER_MULTIPLIER; |
| 83 | private static final int SCREEN_FREEZE_LAYER_ENTER = SCREEN_FREEZE_LAYER_BASE; |
| 84 | private static final int SCREEN_FREEZE_LAYER_SCREENSHOT = SCREEN_FREEZE_LAYER_BASE + 1; |
| 85 | private static final int SCREEN_FREEZE_LAYER_EXIT = SCREEN_FREEZE_LAYER_BASE + 2; |
Dianne Hackborn | 50660e2 | 2011-02-02 17:12:25 -0800 | [diff] [blame] | 86 | |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 87 | private final Context mContext; |
| 88 | private final DisplayContent mDisplayContent; |
| 89 | private final float[] mTmpFloats = new float[9]; |
| 90 | private final Transformation mRotateExitTransformation = new Transformation(); |
| 91 | private final Transformation mRotateEnterTransformation = new Transformation(); |
| 92 | // Complete transformations being applied. |
| 93 | private final Transformation mExitTransformation = new Transformation(); |
| 94 | private final Transformation mEnterTransformation = new Transformation(); |
| 95 | private final Matrix mFrameInitialMatrix = new Matrix(); |
| 96 | private final Matrix mSnapshotInitialMatrix = new Matrix(); |
| 97 | private final Matrix mSnapshotFinalMatrix = new Matrix(); |
| 98 | private final Matrix mExitFrameFinalMatrix = new Matrix(); |
| 99 | private final WindowManagerService mService; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 100 | private SurfaceControl mEnterBlackFrameLayer; |
| 101 | private SurfaceControl mRotationLayer; |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 102 | private SurfaceControl mSurfaceControl; |
| 103 | private BlackFrame mEnteringBlackFrame; |
| 104 | private int mWidth, mHeight; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 105 | |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 106 | private int mOriginalRotation; |
| 107 | private int mOriginalWidth, mOriginalHeight; |
| 108 | private int mCurRotation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 109 | |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 110 | private Rect mOriginalDisplayRect = new Rect(); |
| 111 | private Rect mCurrentDisplayRect = new Rect(); |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 112 | // The current active animation to move from the old to the new rotated |
| 113 | // state. Which animation is run here will depend on the old and new |
| 114 | // rotations. |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 115 | private Animation mRotateExitAnimation; |
| 116 | private Animation mRotateEnterAnimation; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 117 | private Animation mRotateAlphaAnimation; |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 118 | private boolean mStarted; |
| 119 | private boolean mAnimRunning; |
| 120 | private boolean mFinishAnimReady; |
| 121 | private long mFinishAnimStartTime; |
| 122 | private boolean mForceDefaultOrientation; |
| 123 | private BlackFrame mExitingBlackFrame; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 124 | private SurfaceRotationAnimationController mSurfaceRotationAnimationController; |
Steven Timotius | f2d6889 | 2017-08-28 17:00:01 -0700 | [diff] [blame] | 125 | |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 126 | public ScreenRotationAnimation(Context context, DisplayContent displayContent, |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 127 | boolean fixedToUserRotation, boolean isSecure, WindowManagerService service) { |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 128 | mService = service; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 129 | mContext = context; |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 130 | mDisplayContent = displayContent; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 131 | displayContent.getBounds(mOriginalDisplayRect); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 132 | |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 133 | // Screenshot does NOT include rotation! |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 134 | final Display display = displayContent.getDisplay(); |
| 135 | int originalRotation = display.getRotation(); |
| 136 | final int originalWidth; |
| 137 | final int originalHeight; |
| 138 | DisplayInfo displayInfo = displayContent.getDisplayInfo(); |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 139 | if (fixedToUserRotation) { |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 140 | // Emulated orientation. |
| 141 | mForceDefaultOrientation = true; |
| 142 | originalWidth = displayContent.mBaseDisplayWidth; |
| 143 | originalHeight = displayContent.mBaseDisplayHeight; |
| 144 | } else { |
| 145 | // Normal situation |
| 146 | originalWidth = displayInfo.logicalWidth; |
| 147 | originalHeight = displayInfo.logicalHeight; |
| 148 | } |
Mathias Agopian | 0ab84ef | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 149 | if (originalRotation == Surface.ROTATION_90 |
| 150 | || originalRotation == Surface.ROTATION_270) { |
| 151 | mWidth = originalHeight; |
| 152 | mHeight = originalWidth; |
| 153 | } else { |
| 154 | mWidth = originalWidth; |
| 155 | mHeight = originalHeight; |
| 156 | } |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 157 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 158 | mOriginalRotation = originalRotation; |
| 159 | mOriginalWidth = originalWidth; |
| 160 | mOriginalHeight = originalHeight; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 161 | mSurfaceRotationAnimationController = new SurfaceRotationAnimationController(); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 162 | |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 163 | final SurfaceControl.Transaction t = mService.mTransactionFactory.get(); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 164 | try { |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 165 | mRotationLayer = displayContent.makeOverlay() |
| 166 | .setName("RotationLayer") |
| 167 | .setContainerLayer() |
| 168 | .build(); |
| 169 | |
| 170 | mEnterBlackFrameLayer = displayContent.makeOverlay() |
| 171 | .setName("EnterBlackFrameLayer") |
| 172 | .setContainerLayer() |
| 173 | .build(); |
| 174 | |
chaviw | 8065f44 | 2019-11-18 13:20:58 -0800 | [diff] [blame] | 175 | mSurfaceControl = mService.makeSurfaceBuilder(null) |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 176 | .setName("ScreenshotSurface") |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 177 | .setParent(mRotationLayer) |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 178 | .setBufferSize(mWidth, mHeight) |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 179 | .setSecure(isSecure) |
| 180 | .build(); |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 181 | |
Peiyong Lin | 67f3ba1 | 2018-08-23 10:26:45 -0700 | [diff] [blame] | 182 | // In case display bounds change, screenshot buffer and surface may mismatch so set a |
| 183 | // scaling mode. |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 184 | SurfaceControl.Transaction t2 = mService.mTransactionFactory.get(); |
Peiyong Lin | 67f3ba1 | 2018-08-23 10:26:45 -0700 | [diff] [blame] | 185 | t2.setOverrideScalingMode(mSurfaceControl, Surface.SCALING_MODE_SCALE_TO_WINDOW); |
| 186 | t2.apply(true /* sync */); |
| 187 | |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 188 | // Capture a screenshot into the surface we just created. |
| 189 | final int displayId = display.getDisplayId(); |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 190 | final Surface surface = mService.mSurfaceFactory.get(); |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 191 | surface.copyFrom(mSurfaceControl); |
Robert Carr | 66b5664f | 2019-04-02 14:18:56 -0700 | [diff] [blame] | 192 | SurfaceControl.ScreenshotGraphicBuffer gb = |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 193 | mService.mDisplayManagerInternal.screenshot(displayId); |
Robert Carr | 66b5664f | 2019-04-02 14:18:56 -0700 | [diff] [blame] | 194 | if (gb != null) { |
| 195 | try { |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 196 | surface.attachAndQueueBufferWithColorSpace(gb.getGraphicBuffer(), |
| 197 | gb.getColorSpace()); |
Robert Carr | 66b5664f | 2019-04-02 14:18:56 -0700 | [diff] [blame] | 198 | } catch (RuntimeException e) { |
| 199 | Slog.w(TAG, "Failed to attach screenshot - " + e.getMessage()); |
| 200 | } |
| 201 | // If the screenshot contains secure layers, we have to make sure the |
| 202 | // screenshot surface we display it in also has FLAG_SECURE so that |
| 203 | // the user can not screenshot secure layers via the screenshot surface. |
| 204 | if (gb.containsSecureLayers()) { |
| 205 | t.setSecure(mSurfaceControl, true); |
| 206 | } |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 207 | t.setLayer(mRotationLayer, SCREEN_FREEZE_LAYER_BASE); |
Garfield Tan | f4ac307 | 2018-03-16 13:03:52 -0700 | [diff] [blame] | 208 | t.setLayer(mSurfaceControl, SCREEN_FREEZE_LAYER_SCREENSHOT); |
| 209 | t.setAlpha(mSurfaceControl, 0); |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 210 | t.show(mRotationLayer); |
Garfield Tan | f4ac307 | 2018-03-16 13:03:52 -0700 | [diff] [blame] | 211 | t.show(mSurfaceControl); |
Garfield Tan | f4ac307 | 2018-03-16 13:03:52 -0700 | [diff] [blame] | 212 | } else { |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 213 | Slog.w(TAG, "Unable to take screenshot of display " + displayId); |
Garfield Tan | f4ac307 | 2018-03-16 13:03:52 -0700 | [diff] [blame] | 214 | } |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 215 | surface.destroy(); |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 216 | } catch (OutOfResourcesException e) { |
| 217 | Slog.w(TAG, "Unable to allocate freeze surface", e); |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 218 | } |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 219 | |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 220 | ProtoLog.i(WM_SHOW_SURFACE_ALLOC, |
| 221 | " FREEZE %s: CREATE", mSurfaceControl); |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 222 | setRotation(t, originalRotation); |
| 223 | t.apply(); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 226 | private static void createRotationMatrix(int rotation, int width, int height, |
| 227 | Matrix outMatrix) { |
| 228 | switch (rotation) { |
| 229 | case Surface.ROTATION_0: |
| 230 | outMatrix.reset(); |
| 231 | break; |
| 232 | case Surface.ROTATION_90: |
| 233 | outMatrix.setRotate(90, 0, 0); |
| 234 | outMatrix.postTranslate(height, 0); |
| 235 | break; |
| 236 | case Surface.ROTATION_180: |
| 237 | outMatrix.setRotate(180, 0, 0); |
| 238 | outMatrix.postTranslate(width, height); |
| 239 | break; |
| 240 | case Surface.ROTATION_270: |
| 241 | outMatrix.setRotate(270, 0, 0); |
| 242 | outMatrix.postTranslate(0, width); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 247 | public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 248 | final long token = proto.start(fieldId); |
| 249 | proto.write(STARTED, mStarted); |
| 250 | proto.write(ANIMATION_RUNNING, mAnimRunning); |
| 251 | proto.end(token); |
| 252 | } |
| 253 | |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 254 | boolean hasScreenshot() { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 255 | return mSurfaceControl != null; |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 258 | private void setRotationTransform(SurfaceControl.Transaction t, Matrix matrix) { |
| 259 | if (mRotationLayer == null) { |
| 260 | return; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 261 | } |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 262 | matrix.getValues(mTmpFloats); |
| 263 | float x = mTmpFloats[Matrix.MTRANS_X]; |
| 264 | float y = mTmpFloats[Matrix.MTRANS_Y]; |
| 265 | if (mForceDefaultOrientation) { |
| 266 | mDisplayContent.getBounds(mCurrentDisplayRect); |
| 267 | x -= mCurrentDisplayRect.left; |
| 268 | y -= mCurrentDisplayRect.top; |
| 269 | } |
| 270 | t.setPosition(mRotationLayer, x, y); |
| 271 | t.setMatrix(mRotationLayer, |
| 272 | mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y], |
| 273 | mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]); |
| 274 | |
| 275 | t.setAlpha(mSurfaceControl, (float) 1.0); |
| 276 | t.setAlpha(mRotationLayer, (float) 1.0); |
| 277 | t.show(mRotationLayer); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 280 | public void printTo(String prefix, PrintWriter pw) { |
| 281 | pw.print(prefix); pw.print("mSurface="); pw.print(mSurfaceControl); |
| 282 | pw.print(" mWidth="); pw.print(mWidth); |
| 283 | pw.print(" mHeight="); pw.println(mHeight); |
| 284 | pw.print(prefix); pw.print("mExitingBlackFrame="); pw.println(mExitingBlackFrame); |
| 285 | if (mExitingBlackFrame != null) { |
| 286 | mExitingBlackFrame.printTo(prefix + " ", pw); |
| 287 | } |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 288 | pw.print(prefix); |
| 289 | pw.print("mEnteringBlackFrame="); |
| 290 | pw.println(mEnteringBlackFrame); |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 291 | if (mEnteringBlackFrame != null) { |
| 292 | mEnteringBlackFrame.printTo(prefix + " ", pw); |
| 293 | } |
| 294 | pw.print(prefix); pw.print("mCurRotation="); pw.print(mCurRotation); |
| 295 | pw.print(" mOriginalRotation="); pw.println(mOriginalRotation); |
| 296 | pw.print(prefix); pw.print("mOriginalWidth="); pw.print(mOriginalWidth); |
| 297 | pw.print(" mOriginalHeight="); pw.println(mOriginalHeight); |
| 298 | pw.print(prefix); pw.print("mStarted="); pw.print(mStarted); |
| 299 | pw.print(" mAnimRunning="); pw.print(mAnimRunning); |
| 300 | pw.print(" mFinishAnimReady="); pw.print(mFinishAnimReady); |
| 301 | pw.print(" mFinishAnimStartTime="); pw.println(mFinishAnimStartTime); |
| 302 | pw.print(prefix); pw.print("mRotateExitAnimation="); pw.print(mRotateExitAnimation); |
| 303 | pw.print(" "); mRotateExitTransformation.printShortString(pw); pw.println(); |
| 304 | pw.print(prefix); pw.print("mRotateEnterAnimation="); pw.print(mRotateEnterAnimation); |
| 305 | pw.print(" "); mRotateEnterTransformation.printShortString(pw); pw.println(); |
| 306 | pw.print(prefix); pw.print("mExitTransformation="); |
| 307 | mExitTransformation.printShortString(pw); pw.println(); |
| 308 | pw.print(prefix); pw.print("mEnterTransformation="); |
| 309 | mEnterTransformation.printShortString(pw); pw.println(); |
| 310 | pw.print(prefix); pw.print("mFrameInitialMatrix="); |
| 311 | mFrameInitialMatrix.printShortString(pw); |
| 312 | pw.println(); |
| 313 | pw.print(prefix); pw.print("mSnapshotInitialMatrix="); |
| 314 | mSnapshotInitialMatrix.printShortString(pw); |
| 315 | pw.print(" mSnapshotFinalMatrix="); mSnapshotFinalMatrix.printShortString(pw); |
| 316 | pw.println(); |
| 317 | pw.print(prefix); pw.print("mExitFrameFinalMatrix="); |
| 318 | mExitFrameFinalMatrix.printShortString(pw); |
| 319 | pw.println(); |
| 320 | pw.print(prefix); pw.print("mForceDefaultOrientation="); pw.print(mForceDefaultOrientation); |
| 321 | if (mForceDefaultOrientation) { |
| 322 | pw.print(" mOriginalDisplayRect="); pw.print(mOriginalDisplayRect.toShortString()); |
| 323 | pw.print(" mCurrentDisplayRect="); pw.println(mCurrentDisplayRect.toShortString()); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 327 | public void setRotation(SurfaceControl.Transaction t, int rotation) { |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 328 | mCurRotation = rotation; |
| 329 | |
| 330 | // Compute the transformation matrix that must be applied |
| 331 | // to the snapshot to make it stay in the same original position |
| 332 | // with the current screen rotation. |
Wale Ogunwale | 4a02d81 | 2015-02-12 23:01:38 -0800 | [diff] [blame] | 333 | int delta = DisplayContent.deltaRotation(rotation, Surface.ROTATION_0); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 334 | createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 335 | |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 336 | setRotationTransform(t, mSnapshotInitialMatrix); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Returns true if animating. |
| 341 | */ |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 342 | private boolean startAnimation(SurfaceControl.Transaction t, long maxAnimationDuration, |
Vadim Caen | ba4fd6c | 2019-08-05 16:45:46 +0200 | [diff] [blame] | 343 | float animationScale, int finalWidth, int finalHeight, int exitAnim, int enterAnim) { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 344 | if (mSurfaceControl == null) { |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 345 | // Can't do animation. |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 346 | return false; |
| 347 | } |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 348 | if (mStarted) { |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | mStarted = true; |
| 353 | |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 354 | // Figure out how the screen has moved from the original rotation. |
Wale Ogunwale | 4a02d81 | 2015-02-12 23:01:38 -0800 | [diff] [blame] | 355 | int delta = DisplayContent.deltaRotation(mCurRotation, mOriginalRotation); |
Dianne Hackborn | de75cb4 | 2011-03-02 17:11:21 -0800 | [diff] [blame] | 356 | |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 357 | mRotateAlphaAnimation = AnimationUtils.loadAnimation(mContext, |
| 358 | com.android.internal.R.anim.screen_rotate_alpha); |
| 359 | |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 360 | final boolean customAnim; |
Craig Mautner | 3c17437 | 2013-02-21 17:54:37 -0800 | [diff] [blame] | 361 | if (exitAnim != 0 && enterAnim != 0) { |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 362 | customAnim = true; |
Craig Mautner | 3c17437 | 2013-02-21 17:54:37 -0800 | [diff] [blame] | 363 | mRotateExitAnimation = AnimationUtils.loadAnimation(mContext, exitAnim); |
| 364 | mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext, enterAnim); |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 365 | } else { |
| 366 | customAnim = false; |
| 367 | switch (delta) { |
| 368 | case Surface.ROTATION_0: |
| 369 | mRotateExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 370 | com.android.internal.R.anim.screen_rotate_0_exit); |
| 371 | mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 372 | com.android.internal.R.anim.screen_rotate_0_enter); |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 373 | break; |
| 374 | case Surface.ROTATION_90: |
| 375 | mRotateExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 376 | com.android.internal.R.anim.screen_rotate_plus_90_exit); |
| 377 | mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 378 | com.android.internal.R.anim.screen_rotate_plus_90_enter); |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 379 | break; |
| 380 | case Surface.ROTATION_180: |
| 381 | mRotateExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 382 | com.android.internal.R.anim.screen_rotate_180_exit); |
| 383 | mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 384 | com.android.internal.R.anim.screen_rotate_180_enter); |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 385 | break; |
| 386 | case Surface.ROTATION_270: |
| 387 | mRotateExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 388 | com.android.internal.R.anim.screen_rotate_minus_90_exit); |
| 389 | mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 390 | com.android.internal.R.anim.screen_rotate_minus_90_enter); |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 391 | break; |
| 392 | } |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 395 | // Initialize the animations. This is a hack, redefining what "parent" |
| 396 | // means to allow supplying the last and next size. In this definition |
| 397 | // "%p" is the original (let's call it "previous") size, and "%" is the |
| 398 | // screen's current/new size. |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 399 | mRotateEnterAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight); |
| 400 | mRotateExitAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight); |
| 401 | mAnimRunning = false; |
| 402 | mFinishAnimReady = false; |
| 403 | mFinishAnimStartTime = -1; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 404 | |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 405 | mRotateExitAnimation.restrictDuration(maxAnimationDuration); |
| 406 | mRotateExitAnimation.scaleCurrentDuration(animationScale); |
| 407 | mRotateEnterAnimation.restrictDuration(maxAnimationDuration); |
| 408 | mRotateEnterAnimation.scaleCurrentDuration(animationScale); |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 409 | mRotateAlphaAnimation.restrictDuration(maxAnimationDuration); |
| 410 | mRotateAlphaAnimation.scaleCurrentDuration(animationScale); |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 411 | |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 412 | if (!customAnim && mExitingBlackFrame == null) { |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 413 | try { |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 414 | // Compute the transformation matrix that must be applied |
| 415 | // the the black frame to make it stay in the initial position |
| 416 | // before the new screen rotation. This is different than the |
| 417 | // snapshot transformation because the snapshot is always based |
| 418 | // of the native orientation of the screen, not the orientation |
| 419 | // we were last in. |
| 420 | createRotationMatrix(delta, mOriginalWidth, mOriginalHeight, mFrameInitialMatrix); |
| 421 | |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 422 | final Rect outer; |
| 423 | final Rect inner; |
| 424 | if (mForceDefaultOrientation) { |
| 425 | // Going from a smaller Display to a larger Display, add curtains to sides |
| 426 | // or top and bottom. Going from a larger to smaller display will result in |
| 427 | // no BlackSurfaces being constructed. |
| 428 | outer = mCurrentDisplayRect; |
| 429 | inner = mOriginalDisplayRect; |
| 430 | } else { |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 431 | outer = new Rect(-mWidth, -mHeight, mWidth * 2, mHeight * 2); |
| 432 | inner = new Rect(0, 0, mWidth, mHeight); |
Craig Mautner | 46ac6fa | 2013-08-01 10:06:34 -0700 | [diff] [blame] | 433 | } |
chaviw | 9f6171e | 2019-06-07 16:33:50 -0700 | [diff] [blame] | 434 | mExitingBlackFrame = new BlackFrame(mService.mTransactionFactory, t, outer, inner, |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 435 | SCREEN_FREEZE_LAYER_EXIT, mDisplayContent, mForceDefaultOrientation, |
| 436 | mRotationLayer); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 437 | } catch (OutOfResourcesException e) { |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 438 | Slog.w(TAG, "Unable to allocate black surface", e); |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Dianne Hackborn | 9d9ece3 | 2012-09-10 15:33:52 -0700 | [diff] [blame] | 442 | if (customAnim && mEnteringBlackFrame == null) { |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 443 | try { |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 444 | Rect outer = new Rect(-finalWidth, -finalHeight, |
Vadim Caen | 2ada9a9 | 2019-07-30 11:39:44 +0200 | [diff] [blame] | 445 | finalWidth * 2, finalHeight * 2); |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 446 | Rect inner = new Rect(0, 0, finalWidth, finalHeight); |
chaviw | 9f6171e | 2019-06-07 16:33:50 -0700 | [diff] [blame] | 447 | mEnteringBlackFrame = new BlackFrame(mService.mTransactionFactory, t, outer, inner, |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 448 | SCREEN_FREEZE_LAYER_ENTER, mDisplayContent, false, mEnterBlackFrameLayer); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 449 | } catch (OutOfResourcesException e) { |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 450 | Slog.w(TAG, "Unable to allocate black surface", e); |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 451 | } |
Dianne Hackborn | 50660e2 | 2011-02-02 17:12:25 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 454 | mSurfaceRotationAnimationController.startAnimation(); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 458 | /** |
| 459 | * Returns true if animating. |
| 460 | */ |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 461 | public boolean dismiss(SurfaceControl.Transaction t, long maxAnimationDuration, |
Craig Mautner | 3c17437 | 2013-02-21 17:54:37 -0800 | [diff] [blame] | 462 | float animationScale, int finalWidth, int finalHeight, int exitAnim, int enterAnim) { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 463 | if (mSurfaceControl == null) { |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 464 | // Can't do animation. |
| 465 | return false; |
| 466 | } |
| 467 | if (!mStarted) { |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 468 | startAnimation(t, maxAnimationDuration, animationScale, finalWidth, finalHeight, |
Vadim Caen | ba4fd6c | 2019-08-05 16:45:46 +0200 | [diff] [blame] | 469 | exitAnim, enterAnim); |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 470 | } |
| 471 | if (!mStarted) { |
| 472 | return false; |
| 473 | } |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 474 | mFinishAnimReady = true; |
| 475 | return true; |
| 476 | } |
| 477 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 478 | public void kill() { |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 479 | if (mSurfaceRotationAnimationController != null) { |
| 480 | mSurfaceRotationAnimationController.cancel(); |
| 481 | mSurfaceRotationAnimationController = null; |
| 482 | } |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 483 | if (mSurfaceControl != null) { |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 484 | ProtoLog.i(WM_SHOW_SURFACE_ALLOC, " FREEZE %s: DESTROY", mSurfaceControl); |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 485 | mSurfaceControl = null; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 486 | SurfaceControl.Transaction t = mService.mTransactionFactory.get(); |
| 487 | if (mRotationLayer != null) { |
| 488 | if (mRotationLayer.isValid()) { |
| 489 | t.remove(mRotationLayer); |
| 490 | } |
| 491 | mRotationLayer = null; |
| 492 | } |
| 493 | if (mEnterBlackFrameLayer != null) { |
| 494 | if (mEnterBlackFrameLayer.isValid()) { |
| 495 | t.remove(mEnterBlackFrameLayer); |
| 496 | } |
| 497 | mEnterBlackFrameLayer = null; |
| 498 | } |
| 499 | t.apply(); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 500 | } |
Dianne Hackborn | d6b32b6 | 2012-03-16 11:54:51 -0700 | [diff] [blame] | 501 | if (mExitingBlackFrame != null) { |
| 502 | mExitingBlackFrame.kill(); |
| 503 | mExitingBlackFrame = null; |
| 504 | } |
| 505 | if (mEnteringBlackFrame != null) { |
| 506 | mEnteringBlackFrame.kill(); |
| 507 | mEnteringBlackFrame = null; |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 508 | } |
Dianne Hackborn | fd1c5ed | 2012-01-13 13:09:16 -0800 | [diff] [blame] | 509 | if (mRotateExitAnimation != null) { |
| 510 | mRotateExitAnimation.cancel(); |
| 511 | mRotateExitAnimation = null; |
| 512 | } |
| 513 | if (mRotateEnterAnimation != null) { |
| 514 | mRotateEnterAnimation.cancel(); |
| 515 | mRotateEnterAnimation = null; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 516 | } |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 517 | if (mRotateAlphaAnimation != null) { |
| 518 | mRotateAlphaAnimation.cancel(); |
| 519 | mRotateAlphaAnimation = null; |
| 520 | } |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | public boolean isAnimating() { |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 524 | return mSurfaceRotationAnimationController != null |
| 525 | && mSurfaceRotationAnimationController.isAnimating(); |
Dianne Hackborn | 187ae210 | 2012-04-11 18:12:06 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Dianne Hackborn | 4b16969 | 2012-11-29 17:51:24 -0800 | [diff] [blame] | 528 | public boolean isRotating() { |
| 529 | return mCurRotation != mOriginalRotation; |
| 530 | } |
| 531 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 532 | public Transformation getEnterTransformation() { |
| 533 | return mEnterTransformation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 534 | } |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 535 | |
| 536 | /** |
| 537 | * Utility class that runs a {@link ScreenRotationAnimation} on the {@link |
| 538 | * SurfaceAnimationRunner}. |
| 539 | * <p> |
| 540 | * The rotation animation is divided into the following hierarchy: |
| 541 | * <ul> |
| 542 | * <li> A first rotation layer, containing the blackframes. This layer is animated by the |
| 543 | * "screen_rotate_X_exit" that applies a scale and rotate and where X is value of the rotation. |
| 544 | * <ul> |
| 545 | * <li> A child layer containing the screenshot on which is added an animation of it's |
| 546 | * alpha channel ("screen_rotate_alpha") and that will rotate with his parent layer.</li> |
| 547 | * </ul> |
| 548 | * <li> A second rotation layer used when custom animations are passed in |
| 549 | * {@link ScreenRotationAnimation#startAnimation( |
| 550 | * SurfaceControl.Transaction, long, float, int, int, int, int)}. |
| 551 | * </ul> |
| 552 | * <p> |
| 553 | * Thus an {@link LocalAnimationAdapter.AnimationSpec} is created for each of |
| 554 | * this three {@link SurfaceControl}s which then delegates the animation to the |
| 555 | * {@link ScreenRotationAnimation}. |
| 556 | */ |
| 557 | class SurfaceRotationAnimationController { |
| 558 | private SurfaceAnimator mDisplayAnimator; |
| 559 | private SurfaceAnimator mEnterBlackFrameAnimator; |
| 560 | private SurfaceAnimator mScreenshotRotationAnimator; |
| 561 | private SurfaceAnimator mRotateScreenAnimator; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * Start the rotation animation of the display and the screenshot on the |
| 565 | * {@link SurfaceAnimationRunner}. |
| 566 | */ |
| 567 | void startAnimation() { |
| 568 | mRotateScreenAnimator = startScreenshotAlphaAnimation(); |
| 569 | mDisplayAnimator = startDisplayRotation(); |
| 570 | if (mExitingBlackFrame != null) { |
| 571 | mScreenshotRotationAnimator = startScreenshotRotationAnimation(); |
| 572 | } |
| 573 | if (mEnteringBlackFrame != null) { |
| 574 | mEnterBlackFrameAnimator = startEnterBlackFrameAnimation(); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | private SimpleSurfaceAnimatable.Builder initializeBuilder() { |
| 579 | return new SimpleSurfaceAnimatable.Builder() |
| 580 | .setPendingTransactionSupplier(mDisplayContent::getPendingTransaction) |
| 581 | .setCommitTransactionRunnable(mDisplayContent::commitPendingTransaction) |
| 582 | .setAnimationLeashSupplier(mDisplayContent::makeOverlay); |
| 583 | } |
| 584 | |
| 585 | private SurfaceAnimator startDisplayRotation() { |
| 586 | return startAnimation(initializeBuilder() |
| 587 | .setAnimationLeashParent(mDisplayContent.getSurfaceControl()) |
| 588 | .setSurfaceControl(mDisplayContent.getWindowingLayer()) |
| 589 | .setParentSurfaceControl(mDisplayContent.getSurfaceControl()) |
| 590 | .setWidth(mDisplayContent.getSurfaceWidth()) |
| 591 | .setHeight(mDisplayContent.getSurfaceHeight()) |
| 592 | .build(), |
| 593 | createWindowAnimationSpec(mRotateEnterAnimation), |
Vadim Caen | 311fef7 | 2019-11-05 14:53:06 +0100 | [diff] [blame] | 594 | this::onAnimationEnd); |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | private SurfaceAnimator startScreenshotAlphaAnimation() { |
| 598 | return startAnimation(initializeBuilder() |
| 599 | .setSurfaceControl(mSurfaceControl) |
| 600 | .setAnimationLeashParent(mRotationLayer) |
| 601 | .setWidth(mWidth) |
| 602 | .setHeight(mHeight) |
| 603 | .build(), |
| 604 | createWindowAnimationSpec(mRotateAlphaAnimation), |
Vadim Caen | 311fef7 | 2019-11-05 14:53:06 +0100 | [diff] [blame] | 605 | this::onAnimationEnd); |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | private SurfaceAnimator startEnterBlackFrameAnimation() { |
| 609 | return startAnimation(initializeBuilder() |
| 610 | .setSurfaceControl(mEnterBlackFrameLayer) |
| 611 | .setAnimationLeashParent(mDisplayContent.getOverlayLayer()) |
| 612 | .build(), |
| 613 | createWindowAnimationSpec(mRotateEnterAnimation), |
Vadim Caen | 311fef7 | 2019-11-05 14:53:06 +0100 | [diff] [blame] | 614 | this::onAnimationEnd); |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | private SurfaceAnimator startScreenshotRotationAnimation() { |
| 618 | return startAnimation(initializeBuilder() |
| 619 | .setSurfaceControl(mRotationLayer) |
| 620 | .setAnimationLeashParent(mDisplayContent.getOverlayLayer()) |
| 621 | .build(), |
| 622 | createWindowAnimationSpec(mRotateExitAnimation), |
| 623 | this::onAnimationEnd); |
| 624 | } |
| 625 | |
| 626 | private WindowAnimationSpec createWindowAnimationSpec(Animation mAnimation) { |
| 627 | return new WindowAnimationSpec(mAnimation, new Point(0, 0) /* position */, |
| 628 | false /* canSkipFirstFrame */, 0 /* WindowCornerRadius */); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Start an animation defined by animationSpec on a new {@link SurfaceAnimator}. |
| 633 | * |
| 634 | * @param animatable The animatable used for the animation. |
| 635 | * @param animationSpec The spec of the animation. |
| 636 | * @param animationFinishedCallback Callback passed to the {@link SurfaceAnimator} and |
| 637 | * called when the animation finishes. |
| 638 | * @return The newly created {@link SurfaceAnimator} that as been started. |
| 639 | */ |
| 640 | private SurfaceAnimator startAnimation( |
| 641 | SurfaceAnimator.Animatable animatable, |
| 642 | LocalAnimationAdapter.AnimationSpec animationSpec, |
| 643 | Runnable animationFinishedCallback) { |
| 644 | SurfaceAnimator animator = new SurfaceAnimator( |
| 645 | animatable, animationFinishedCallback, mService); |
| 646 | |
| 647 | LocalAnimationAdapter localAnimationAdapter = new LocalAnimationAdapter( |
| 648 | animationSpec, mService.mSurfaceAnimationRunner); |
| 649 | |
| 650 | animator.startAnimation(mDisplayContent.getPendingTransaction(), |
| 651 | localAnimationAdapter, false); |
| 652 | return animator; |
| 653 | } |
| 654 | |
| 655 | private void onAnimationEnd() { |
Vadim Caen | 311fef7 | 2019-11-05 14:53:06 +0100 | [diff] [blame] | 656 | synchronized (mService.mGlobalLock) { |
| 657 | if (isAnimating()) { |
| 658 | ProtoLog.v(WM_DEBUG_ORIENTATION, |
| 659 | "ScreenRotation sill animating: mDisplayAnimator: %s\n" |
| 660 | + "mEnterBlackFrameAnimator: " |
| 661 | + "%s\nmRotateScreenAnimator: %s\n" |
| 662 | + "mScreenshotRotationAnimator: %s", |
| 663 | mDisplayAnimator != null |
| 664 | ? mDisplayAnimator.isAnimating() : null, |
| 665 | mEnterBlackFrameAnimator != null |
| 666 | ? mEnterBlackFrameAnimator.isAnimating() : null, |
| 667 | mRotateScreenAnimator != null |
| 668 | ? mRotateScreenAnimator.isAnimating() : null, |
| 669 | mScreenshotRotationAnimator != null |
| 670 | ? mScreenshotRotationAnimator.isAnimating() : null |
| 671 | ); |
| 672 | return; |
| 673 | } |
| 674 | ProtoLog.d(WM_DEBUG_ORIENTATION, "ScreenRotationAnimation onAnimationEnd"); |
| 675 | mEnterBlackFrameAnimator = null; |
| 676 | mScreenshotRotationAnimator = null; |
| 677 | mRotateScreenAnimator = null; |
| 678 | mService.mAnimator.mBulkUpdateParams |= WindowSurfacePlacer.SET_UPDATE_ROTATION; |
| 679 | kill(); |
| 680 | mService.updateRotation(false, false); |
| 681 | AccessibilityController accessibilityController = mService.mAccessibilityController; |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 682 | |
Vadim Caen | 311fef7 | 2019-11-05 14:53:06 +0100 | [diff] [blame] | 683 | if (accessibilityController != null) { |
| 684 | // We just finished rotation animation which means we did not |
| 685 | // announce the rotation and waited for it to end, announce now. |
| 686 | accessibilityController.onRotationChangedLocked(mDisplayContent); |
| 687 | } |
Vadim Caen | f847426 | 2019-08-12 17:52:01 +0200 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | public void cancel() { |
| 692 | if (mEnterBlackFrameAnimator != null) { |
| 693 | mEnterBlackFrameAnimator.cancelAnimation(); |
| 694 | } |
| 695 | |
| 696 | if (mScreenshotRotationAnimator != null) { |
| 697 | mScreenshotRotationAnimator.cancelAnimation(); |
| 698 | } |
| 699 | |
| 700 | if (mRotateScreenAnimator != null) { |
| 701 | mRotateScreenAnimator.cancelAnimation(); |
| 702 | } |
| 703 | |
| 704 | if (mDisplayAnimator != null) { |
| 705 | mDisplayAnimator.cancelAnimation(); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | public boolean isAnimating() { |
| 710 | return mDisplayAnimator != null && mDisplayAnimator.isAnimating() |
| 711 | || mEnterBlackFrameAnimator != null && mEnterBlackFrameAnimator.isAnimating() |
| 712 | || mRotateScreenAnimator != null && mRotateScreenAnimator.isAnimating() |
| 713 | || mScreenshotRotationAnimator != null |
| 714 | && mScreenshotRotationAnimator.isAnimating(); |
| 715 | } |
| 716 | } |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 717 | } |