blob: 2d5c97f9a3a588c2945b796c65e97106ab2d0cb1 [file] [log] [blame]
Dianne Hackborna1111872010-11-23 20:55:11 -08001/*
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 Hackborn6e1eb762011-02-17 16:07:28 -080017package com.android.server.wm;
Dianne Hackborna1111872010-11-23 20:55:11 -080018
Peiyong Lin67f3ba12018-08-23 10:26:45 -070019import static com.android.server.wm.ScreenRotationAnimationProto.ANIMATION_RUNNING;
20import static com.android.server.wm.ScreenRotationAnimationProto.STARTED;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080021import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
22import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang97782b42015-10-07 16:01:23 -070025import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
Chong Zhang97782b42015-10-07 16:01:23 -070026import static com.android.server.wm.WindowStateAnimator.WINDOW_FREEZE_LAYER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080027
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080028import android.content.Context;
Dianne Hackborna1111872010-11-23 20:55:11 -080029import android.graphics.Matrix;
Dianne Hackborna1111872010-11-23 20:55:11 -080030import android.graphics.Rect;
Dianne Hackborna1111872010-11-23 20:55:11 -080031import android.util.Slog;
Steven Timotiusf2d68892017-08-28 17:00:01 -070032import android.util.proto.ProtoOutputStream;
Craig Mautner6881a102012-07-27 13:04:51 -070033import android.view.Display;
Craig Mautner46ac6fa2013-08-01 10:06:34 -070034import android.view.DisplayInfo;
Dianne Hackborna1111872010-11-23 20:55:11 -080035import android.view.Surface;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080036import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037import android.view.SurfaceControl;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080038import android.view.animation.Animation;
39import android.view.animation.AnimationUtils;
40import android.view.animation.Transformation;
Dianne Hackborna1111872010-11-23 20:55:11 -080041
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080042import java.io.PrintWriter;
43
Craig Mautnere32c3072012-03-12 15:25:35 -070044class ScreenRotationAnimation {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080045 static final String TAG = TAG_WITH_CLASS_NAME ? "ScreenRotationAnimation" : TAG_WM;
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -080046 static final boolean DEBUG_STATE = false;
47 static final boolean DEBUG_TRANSFORMS = false;
Dianne Hackborn187ae2102012-04-11 18:12:06 -070048 static final boolean TWO_PHASE_ANIMATION = false;
Dianne Hackbornd6b32b62012-03-16 11:54:51 -070049 static final boolean USE_CUSTOM_BLACK_FRAME = false;
Dianne Hackborna1111872010-11-23 20:55:11 -080050
Chong Zhang97782b42015-10-07 16:01:23 -070051 /*
52 * Layers for screen rotation animation. We put these layers above
53 * WINDOW_FREEZE_LAYER so that screen freeze will cover all windows.
54 */
55 static final int SCREEN_FREEZE_LAYER_BASE = WINDOW_FREEZE_LAYER + TYPE_LAYER_MULTIPLIER;
56 static final int SCREEN_FREEZE_LAYER_ENTER = SCREEN_FREEZE_LAYER_BASE;
57 static final int SCREEN_FREEZE_LAYER_SCREENSHOT = SCREEN_FREEZE_LAYER_BASE + 1;
58 static final int SCREEN_FREEZE_LAYER_EXIT = SCREEN_FREEZE_LAYER_BASE + 2;
59 static final int SCREEN_FREEZE_LAYER_CUSTOM = SCREEN_FREEZE_LAYER_BASE + 3;
Dianne Hackborn50660e22011-02-02 17:12:25 -080060
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080061 final Context mContext;
Craig Mautner46ac6fa2013-08-01 10:06:34 -070062 final DisplayContent mDisplayContent;
Mathias Agopian29479eb2013-02-14 14:36:04 -080063 SurfaceControl mSurfaceControl;
Dianne Hackbornd6b32b62012-03-16 11:54:51 -070064 BlackFrame mCustomBlackFrame;
65 BlackFrame mExitingBlackFrame;
66 BlackFrame mEnteringBlackFrame;
Dianne Hackborna1111872010-11-23 20:55:11 -080067 int mWidth, mHeight;
68
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080069 int mOriginalRotation;
70 int mOriginalWidth, mOriginalHeight;
Dianne Hackborna1111872010-11-23 20:55:11 -080071 int mCurRotation;
Craig Mautner46ac6fa2013-08-01 10:06:34 -070072 Rect mOriginalDisplayRect = new Rect();
73 Rect mCurrentDisplayRect = new Rect();
Dianne Hackborna1111872010-11-23 20:55:11 -080074
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -080075 // For all animations, "exit" is for the UI elements that are going
76 // away (that is the snapshot of the old screen), and "enter" is for
77 // the new UI elements that are appearing (that is the active windows
78 // in their final orientation).
79
80 // The starting animation for the exiting and entering elements. This
81 // animation applies a transformation while the rotation is in progress.
82 // It is started immediately, before the new entering UI is ready.
83 Animation mStartExitAnimation;
84 final Transformation mStartExitTransformation = new Transformation();
85 Animation mStartEnterAnimation;
86 final Transformation mStartEnterTransformation = new Transformation();
Dianne Hackborn9fd74802012-03-01 19:26:31 -080087 Animation mStartFrameAnimation;
88 final Transformation mStartFrameTransformation = new Transformation();
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -080089
90 // The finishing animation for the exiting and entering elements. This
91 // animation needs to undo the transformation of the starting animation.
92 // It starts running once the new rotation UI elements are ready to be
93 // displayed.
94 Animation mFinishExitAnimation;
95 final Transformation mFinishExitTransformation = new Transformation();
96 Animation mFinishEnterAnimation;
97 final Transformation mFinishEnterTransformation = new Transformation();
Dianne Hackborn9fd74802012-03-01 19:26:31 -080098 Animation mFinishFrameAnimation;
99 final Transformation mFinishFrameTransformation = new Transformation();
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800100
101 // The current active animation to move from the old to the new rotated
102 // state. Which animation is run here will depend on the old and new
103 // rotations.
104 Animation mRotateExitAnimation;
105 final Transformation mRotateExitTransformation = new Transformation();
106 Animation mRotateEnterAnimation;
107 final Transformation mRotateEnterTransformation = new Transformation();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800108 Animation mRotateFrameAnimation;
109 final Transformation mRotateFrameTransformation = new Transformation();
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800110
111 // A previously running rotate animation. This will be used if we need
112 // to switch to a new rotation before finishing the previous one.
113 Animation mLastRotateExitAnimation;
114 final Transformation mLastRotateExitTransformation = new Transformation();
115 Animation mLastRotateEnterAnimation;
116 final Transformation mLastRotateEnterTransformation = new Transformation();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800117 Animation mLastRotateFrameAnimation;
118 final Transformation mLastRotateFrameTransformation = new Transformation();
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800119
120 // Complete transformations being applied.
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800121 final Transformation mExitTransformation = new Transformation();
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800122 final Transformation mEnterTransformation = new Transformation();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800123 final Transformation mFrameTransformation = new Transformation();
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800124
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800125 boolean mStarted;
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800126 boolean mAnimRunning;
127 boolean mFinishAnimReady;
128 long mFinishAnimStartTime;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700129 boolean mForceDefaultOrientation;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800130
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800131 final Matrix mFrameInitialMatrix = new Matrix();
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800132 final Matrix mSnapshotInitialMatrix = new Matrix();
133 final Matrix mSnapshotFinalMatrix = new Matrix();
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700134 final Matrix mExitFrameFinalMatrix = new Matrix();
Dianne Hackborn50660e22011-02-02 17:12:25 -0800135 final Matrix mTmpMatrix = new Matrix();
Dianne Hackborna1111872010-11-23 20:55:11 -0800136 final float[] mTmpFloats = new float[9];
Craig Mautnerdbb79912012-03-01 18:59:14 -0800137 private boolean mMoreRotateEnter;
138 private boolean mMoreRotateExit;
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800139 private boolean mMoreRotateFrame;
Craig Mautnerdbb79912012-03-01 18:59:14 -0800140 private boolean mMoreFinishEnter;
141 private boolean mMoreFinishExit;
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800142 private boolean mMoreFinishFrame;
Craig Mautnerdbb79912012-03-01 18:59:14 -0800143 private boolean mMoreStartEnter;
144 private boolean mMoreStartExit;
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800145 private boolean mMoreStartFrame;
Craig Mautner3255a282012-04-16 15:42:47 -0700146 long mHalfwayPoint;
Dianne Hackborna1111872010-11-23 20:55:11 -0800147
Robert Carr68e5c9e2016-09-14 10:50:09 -0700148 private final WindowManagerService mService;
149
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800150 public void printTo(String prefix, PrintWriter pw) {
Mathias Agopian29479eb2013-02-14 14:36:04 -0800151 pw.print(prefix); pw.print("mSurface="); pw.print(mSurfaceControl);
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800152 pw.print(" mWidth="); pw.print(mWidth);
153 pw.print(" mHeight="); pw.println(mHeight);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700154 if (USE_CUSTOM_BLACK_FRAME) {
155 pw.print(prefix); pw.print("mCustomBlackFrame="); pw.println(mCustomBlackFrame);
156 if (mCustomBlackFrame != null) {
157 mCustomBlackFrame.printTo(prefix + " ", pw);
158 }
159 }
160 pw.print(prefix); pw.print("mExitingBlackFrame="); pw.println(mExitingBlackFrame);
161 if (mExitingBlackFrame != null) {
162 mExitingBlackFrame.printTo(prefix + " ", pw);
163 }
164 pw.print(prefix); pw.print("mEnteringBlackFrame="); pw.println(mEnteringBlackFrame);
165 if (mEnteringBlackFrame != null) {
166 mEnteringBlackFrame.printTo(prefix + " ", pw);
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800167 }
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700168 pw.print(prefix); pw.print("mCurRotation="); pw.print(mCurRotation);
169 pw.print(" mOriginalRotation="); pw.println(mOriginalRotation);
170 pw.print(prefix); pw.print("mOriginalWidth="); pw.print(mOriginalWidth);
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800171 pw.print(" mOriginalHeight="); pw.println(mOriginalHeight);
172 pw.print(prefix); pw.print("mStarted="); pw.print(mStarted);
173 pw.print(" mAnimRunning="); pw.print(mAnimRunning);
174 pw.print(" mFinishAnimReady="); pw.print(mFinishAnimReady);
175 pw.print(" mFinishAnimStartTime="); pw.println(mFinishAnimStartTime);
176 pw.print(prefix); pw.print("mStartExitAnimation="); pw.print(mStartExitAnimation);
177 pw.print(" "); mStartExitTransformation.printShortString(pw); pw.println();
178 pw.print(prefix); pw.print("mStartEnterAnimation="); pw.print(mStartEnterAnimation);
179 pw.print(" "); mStartEnterTransformation.printShortString(pw); pw.println();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800180 pw.print(prefix); pw.print("mStartFrameAnimation="); pw.print(mStartFrameAnimation);
181 pw.print(" "); mStartFrameTransformation.printShortString(pw); pw.println();
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800182 pw.print(prefix); pw.print("mFinishExitAnimation="); pw.print(mFinishExitAnimation);
183 pw.print(" "); mFinishExitTransformation.printShortString(pw); pw.println();
184 pw.print(prefix); pw.print("mFinishEnterAnimation="); pw.print(mFinishEnterAnimation);
185 pw.print(" "); mFinishEnterTransformation.printShortString(pw); pw.println();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800186 pw.print(prefix); pw.print("mFinishFrameAnimation="); pw.print(mFinishFrameAnimation);
187 pw.print(" "); mFinishFrameTransformation.printShortString(pw); pw.println();
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800188 pw.print(prefix); pw.print("mRotateExitAnimation="); pw.print(mRotateExitAnimation);
189 pw.print(" "); mRotateExitTransformation.printShortString(pw); pw.println();
190 pw.print(prefix); pw.print("mRotateEnterAnimation="); pw.print(mRotateEnterAnimation);
191 pw.print(" "); mRotateEnterTransformation.printShortString(pw); pw.println();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800192 pw.print(prefix); pw.print("mRotateFrameAnimation="); pw.print(mRotateFrameAnimation);
193 pw.print(" "); mRotateFrameTransformation.printShortString(pw); pw.println();
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800194 pw.print(prefix); pw.print("mExitTransformation=");
195 mExitTransformation.printShortString(pw); pw.println();
196 pw.print(prefix); pw.print("mEnterTransformation=");
197 mEnterTransformation.printShortString(pw); pw.println();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800198 pw.print(prefix); pw.print("mFrameTransformation=");
Steven Timotius8304ef42017-08-28 16:59:14 -0700199 mFrameTransformation.printShortString(pw); pw.println();
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800200 pw.print(prefix); pw.print("mFrameInitialMatrix=");
201 mFrameInitialMatrix.printShortString(pw);
202 pw.println();
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800203 pw.print(prefix); pw.print("mSnapshotInitialMatrix=");
204 mSnapshotInitialMatrix.printShortString(pw);
205 pw.print(" mSnapshotFinalMatrix="); mSnapshotFinalMatrix.printShortString(pw);
206 pw.println();
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700207 pw.print(prefix); pw.print("mExitFrameFinalMatrix=");
208 mExitFrameFinalMatrix.printShortString(pw);
209 pw.println();
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700210 pw.print(prefix); pw.print("mForceDefaultOrientation="); pw.print(mForceDefaultOrientation);
211 if (mForceDefaultOrientation) {
212 pw.print(" mOriginalDisplayRect="); pw.print(mOriginalDisplayRect.toShortString());
213 pw.print(" mCurrentDisplayRect="); pw.println(mCurrentDisplayRect.toShortString());
214 }
Dianne Hackborn4dcece82012-02-10 14:50:32 -0800215 }
216
Steven Timotiusf2d68892017-08-28 17:00:01 -0700217 public void writeToProto(ProtoOutputStream proto, long fieldId) {
218 final long token = proto.start(fieldId);
219 proto.write(STARTED, mStarted);
220 proto.write(ANIMATION_RUNNING, mAnimRunning);
221 proto.end(token);
222 }
223
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700224 public ScreenRotationAnimation(Context context, DisplayContent displayContent,
Garfield Tanff362222018-11-14 17:52:32 -0800225 boolean fixedToUserRotation, boolean isSecure, WindowManagerService service) {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700226 mService = service;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800227 mContext = context;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700228 mDisplayContent = displayContent;
Bryce Leef3c6a472017-11-14 14:53:06 -0800229 displayContent.getBounds(mOriginalDisplayRect);
Dianne Hackborna1111872010-11-23 20:55:11 -0800230
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800231 // Screenshot does NOT include rotation!
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700232 final Display display = displayContent.getDisplay();
233 int originalRotation = display.getRotation();
234 final int originalWidth;
235 final int originalHeight;
236 DisplayInfo displayInfo = displayContent.getDisplayInfo();
Garfield Tanff362222018-11-14 17:52:32 -0800237 if (fixedToUserRotation) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700238 // Emulated orientation.
239 mForceDefaultOrientation = true;
240 originalWidth = displayContent.mBaseDisplayWidth;
241 originalHeight = displayContent.mBaseDisplayHeight;
242 } else {
243 // Normal situation
244 originalWidth = displayInfo.logicalWidth;
245 originalHeight = displayInfo.logicalHeight;
246 }
Mathias Agopian0ab84ef2011-10-13 16:02:48 -0700247 if (originalRotation == Surface.ROTATION_90
248 || originalRotation == Surface.ROTATION_270) {
249 mWidth = originalHeight;
250 mHeight = originalWidth;
251 } else {
252 mWidth = originalWidth;
253 mHeight = originalHeight;
254 }
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800255
Jeff Brownbc68a592011-07-25 12:58:12 -0700256 mOriginalRotation = originalRotation;
257 mOriginalWidth = originalWidth;
258 mOriginalHeight = originalHeight;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800259
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800260 final SurfaceControl.Transaction t = mService.mTransactionFactory.make();
Dianne Hackborna1111872010-11-23 20:55:11 -0800261 try {
Robert Carrae606b42018-02-15 15:36:23 -0800262 mSurfaceControl = displayContent.makeOverlay()
263 .setName("ScreenshotSurface")
Vishnu Naire86bd982018-11-28 13:23:17 -0800264 .setBufferSize(mWidth, mHeight)
Robert Carrae606b42018-02-15 15:36:23 -0800265 .setSecure(isSecure)
266 .build();
Robert Carre13b58e2017-08-31 14:50:44 -0700267
Peiyong Lin67f3ba12018-08-23 10:26:45 -0700268 // In case display bounds change, screenshot buffer and surface may mismatch so set a
269 // scaling mode.
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800270 SurfaceControl.Transaction t2 = mService.mTransactionFactory.make();
Peiyong Lin67f3ba12018-08-23 10:26:45 -0700271 t2.setOverrideScalingMode(mSurfaceControl, Surface.SCALING_MODE_SCALE_TO_WINDOW);
272 t2.apply(true /* sync */);
273
Riddle Hsu654a6f92018-07-13 22:59:36 +0800274 // Capture a screenshot into the surface we just created.
275 final int displayId = display.getDisplayId();
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800276 final Surface surface = mService.mSurfaceFactory.make();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800277 surface.copyFrom(mSurfaceControl);
278 if (mService.mDisplayManagerInternal.screenshot(displayId, surface)) {
Garfield Tanf4ac3072018-03-16 13:03:52 -0700279 t.setLayer(mSurfaceControl, SCREEN_FREEZE_LAYER_SCREENSHOT);
280 t.setAlpha(mSurfaceControl, 0);
281 t.show(mSurfaceControl);
Garfield Tanf4ac3072018-03-16 13:03:52 -0700282 } else {
Riddle Hsu654a6f92018-07-13 22:59:36 +0800283 Slog.w(TAG, "Unable to take screenshot of display " + displayId);
Garfield Tanf4ac3072018-03-16 13:03:52 -0700284 }
Riddle Hsu654a6f92018-07-13 22:59:36 +0800285 surface.destroy();
Robert Carrae606b42018-02-15 15:36:23 -0800286 } catch (OutOfResourcesException e) {
287 Slog.w(TAG, "Unable to allocate freeze surface", e);
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800288 }
Robert Carrae606b42018-02-15 15:36:23 -0800289
290 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG_WM,
291 " FREEZE " + mSurfaceControl + ": CREATE");
292 setRotation(t, originalRotation);
293 t.apply();
Dianne Hackborna1111872010-11-23 20:55:11 -0800294 }
295
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800296 boolean hasScreenshot() {
Mathias Agopian29479eb2013-02-14 14:36:04 -0800297 return mSurfaceControl != null;
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800298 }
299
Robert Carrae606b42018-02-15 15:36:23 -0800300 private void setSnapshotTransform(SurfaceControl.Transaction t, Matrix matrix, float alpha) {
Mathias Agopian29479eb2013-02-14 14:36:04 -0800301 if (mSurfaceControl != null) {
Dianne Hackborn352cc982011-01-04 11:34:18 -0800302 matrix.getValues(mTmpFloats);
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700303 float x = mTmpFloats[Matrix.MTRANS_X];
304 float y = mTmpFloats[Matrix.MTRANS_Y];
305 if (mForceDefaultOrientation) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800306 mDisplayContent.getBounds(mCurrentDisplayRect);
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700307 x -= mCurrentDisplayRect.left;
308 y -= mCurrentDisplayRect.top;
309 }
Robert Carrae606b42018-02-15 15:36:23 -0800310 t.setPosition(mSurfaceControl, x, y);
311 t.setMatrix(mSurfaceControl,
Dianne Hackborn352cc982011-01-04 11:34:18 -0800312 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
313 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
Robert Carrae606b42018-02-15 15:36:23 -0800314 t.setAlpha(mSurfaceControl, alpha);
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800315 if (DEBUG_TRANSFORMS) {
Dianne Hackborn352cc982011-01-04 11:34:18 -0800316 float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
317 float[] dstPnts = new float[4];
318 matrix.mapPoints(dstPnts, srcPnts);
319 Slog.i(TAG, "Original : (" + srcPnts[0] + "," + srcPnts[1]
320 + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")");
321 Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1]
322 + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")");
323 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800324 }
325 }
326
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800327 public static void createRotationMatrix(int rotation, int width, int height,
328 Matrix outMatrix) {
329 switch (rotation) {
330 case Surface.ROTATION_0:
331 outMatrix.reset();
332 break;
333 case Surface.ROTATION_90:
334 outMatrix.setRotate(90, 0, 0);
335 outMatrix.postTranslate(height, 0);
336 break;
337 case Surface.ROTATION_180:
338 outMatrix.setRotate(180, 0, 0);
339 outMatrix.postTranslate(width, height);
340 break;
341 case Surface.ROTATION_270:
342 outMatrix.setRotate(270, 0, 0);
343 outMatrix.postTranslate(0, width);
344 break;
345 }
346 }
347
Robert Carrae606b42018-02-15 15:36:23 -0800348 private void setRotation(SurfaceControl.Transaction t, int rotation) {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800349 mCurRotation = rotation;
350
351 // Compute the transformation matrix that must be applied
352 // to the snapshot to make it stay in the same original position
353 // with the current screen rotation.
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800354 int delta = DisplayContent.deltaRotation(rotation, Surface.ROTATION_0);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800355 createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800356
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800357 if (DEBUG_STATE) Slog.v(TAG, "**** ROTATION: " + delta);
Robert Carrae606b42018-02-15 15:36:23 -0800358 setSnapshotTransform(t, mSnapshotInitialMatrix, 1.0f);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800359 }
360
Robert Carrae606b42018-02-15 15:36:23 -0800361 public boolean setRotation(SurfaceControl.Transaction t, int rotation,
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800362 long maxAnimationDuration, float animationScale, int finalWidth, int finalHeight) {
Robert Carrae606b42018-02-15 15:36:23 -0800363 setRotation(t, rotation);
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700364 if (TWO_PHASE_ANIMATION) {
Robert Carrae606b42018-02-15 15:36:23 -0800365 return startAnimation(t, maxAnimationDuration, animationScale,
Craig Mautner3c174372013-02-21 17:54:37 -0800366 finalWidth, finalHeight, false, 0, 0);
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700367 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700368
369 // Don't start animation yet.
370 return false;
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800371 }
372
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800373 /**
374 * Returns true if animating.
375 */
Robert Carrae606b42018-02-15 15:36:23 -0800376 private boolean startAnimation(SurfaceControl.Transaction t, long maxAnimationDuration,
Craig Mautner3c174372013-02-21 17:54:37 -0800377 float animationScale, int finalWidth, int finalHeight, boolean dismissing,
378 int exitAnim, int enterAnim) {
Mathias Agopian29479eb2013-02-14 14:36:04 -0800379 if (mSurfaceControl == null) {
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800380 // Can't do animation.
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800381 return false;
382 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800383 if (mStarted) {
384 return true;
385 }
386
387 mStarted = true;
388
389 boolean firstStart = false;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800390
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800391 // Figure out how the screen has moved from the original rotation.
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800392 int delta = DisplayContent.deltaRotation(mCurRotation, mOriginalRotation);
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800393
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700394 if (TWO_PHASE_ANIMATION && mFinishExitAnimation == null
395 && (!dismissing || delta != Surface.ROTATION_0)) {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800396 if (DEBUG_STATE) Slog.v(TAG, "Creating start and finish animations");
397 firstStart = true;
398 mStartExitAnimation = AnimationUtils.loadAnimation(mContext,
399 com.android.internal.R.anim.screen_rotate_start_exit);
400 mStartEnterAnimation = AnimationUtils.loadAnimation(mContext,
401 com.android.internal.R.anim.screen_rotate_start_enter);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700402 if (USE_CUSTOM_BLACK_FRAME) {
403 mStartFrameAnimation = AnimationUtils.loadAnimation(mContext,
404 com.android.internal.R.anim.screen_rotate_start_frame);
405 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800406 mFinishExitAnimation = AnimationUtils.loadAnimation(mContext,
407 com.android.internal.R.anim.screen_rotate_finish_exit);
408 mFinishEnterAnimation = AnimationUtils.loadAnimation(mContext,
409 com.android.internal.R.anim.screen_rotate_finish_enter);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700410 if (USE_CUSTOM_BLACK_FRAME) {
411 mFinishFrameAnimation = AnimationUtils.loadAnimation(mContext,
412 com.android.internal.R.anim.screen_rotate_finish_frame);
413 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800414 }
415
416 if (DEBUG_STATE) Slog.v(TAG, "Rotation delta: " + delta + " finalWidth="
417 + finalWidth + " finalHeight=" + finalHeight
418 + " origWidth=" + mOriginalWidth + " origHeight=" + mOriginalHeight);
419
Dianne Hackborn9d9ece32012-09-10 15:33:52 -0700420 final boolean customAnim;
Craig Mautner3c174372013-02-21 17:54:37 -0800421 if (exitAnim != 0 && enterAnim != 0) {
Dianne Hackborn9d9ece32012-09-10 15:33:52 -0700422 customAnim = true;
Craig Mautner3c174372013-02-21 17:54:37 -0800423 mRotateExitAnimation = AnimationUtils.loadAnimation(mContext, exitAnim);
424 mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext, enterAnim);
Dianne Hackborn9d9ece32012-09-10 15:33:52 -0700425 } else {
426 customAnim = false;
427 switch (delta) {
428 case Surface.ROTATION_0:
429 mRotateExitAnimation = AnimationUtils.loadAnimation(mContext,
430 com.android.internal.R.anim.screen_rotate_0_exit);
431 mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext,
432 com.android.internal.R.anim.screen_rotate_0_enter);
433 if (USE_CUSTOM_BLACK_FRAME) {
434 mRotateFrameAnimation = AnimationUtils.loadAnimation(mContext,
435 com.android.internal.R.anim.screen_rotate_0_frame);
436 }
437 break;
438 case Surface.ROTATION_90:
439 mRotateExitAnimation = AnimationUtils.loadAnimation(mContext,
440 com.android.internal.R.anim.screen_rotate_plus_90_exit);
441 mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext,
442 com.android.internal.R.anim.screen_rotate_plus_90_enter);
443 if (USE_CUSTOM_BLACK_FRAME) {
444 mRotateFrameAnimation = AnimationUtils.loadAnimation(mContext,
445 com.android.internal.R.anim.screen_rotate_plus_90_frame);
446 }
447 break;
448 case Surface.ROTATION_180:
449 mRotateExitAnimation = AnimationUtils.loadAnimation(mContext,
450 com.android.internal.R.anim.screen_rotate_180_exit);
451 mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext,
452 com.android.internal.R.anim.screen_rotate_180_enter);
453 if (USE_CUSTOM_BLACK_FRAME) {
454 mRotateFrameAnimation = AnimationUtils.loadAnimation(mContext,
455 com.android.internal.R.anim.screen_rotate_180_frame);
456 }
457 break;
458 case Surface.ROTATION_270:
459 mRotateExitAnimation = AnimationUtils.loadAnimation(mContext,
460 com.android.internal.R.anim.screen_rotate_minus_90_exit);
461 mRotateEnterAnimation = AnimationUtils.loadAnimation(mContext,
462 com.android.internal.R.anim.screen_rotate_minus_90_enter);
463 if (USE_CUSTOM_BLACK_FRAME) {
464 mRotateFrameAnimation = AnimationUtils.loadAnimation(mContext,
465 com.android.internal.R.anim.screen_rotate_minus_90_frame);
466 }
467 break;
468 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800469 }
470
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800471 // Initialize the animations. This is a hack, redefining what "parent"
472 // means to allow supplying the last and next size. In this definition
473 // "%p" is the original (let's call it "previous") size, and "%" is the
474 // screen's current/new size.
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700475 if (TWO_PHASE_ANIMATION && firstStart) {
Dianne Hackborn9d9ece32012-09-10 15:33:52 -0700476 // Compute partial steps between original and final sizes. These
477 // are used for the dimensions of the exiting and entering elements,
478 // so they are never stretched too significantly.
479 final int halfWidth = (finalWidth + mOriginalWidth) / 2;
480 final int halfHeight = (finalHeight + mOriginalHeight) / 2;
481
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800482 if (DEBUG_STATE) Slog.v(TAG, "Initializing start and finish animations");
483 mStartEnterAnimation.initialize(finalWidth, finalHeight,
Dianne Hackborn191874e32012-03-09 11:03:36 -0800484 halfWidth, halfHeight);
485 mStartExitAnimation.initialize(halfWidth, halfHeight,
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800486 mOriginalWidth, mOriginalHeight);
487 mFinishEnterAnimation.initialize(finalWidth, finalHeight,
Dianne Hackborn191874e32012-03-09 11:03:36 -0800488 halfWidth, halfHeight);
489 mFinishExitAnimation.initialize(halfWidth, halfHeight,
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800490 mOriginalWidth, mOriginalHeight);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700491 if (USE_CUSTOM_BLACK_FRAME) {
492 mStartFrameAnimation.initialize(finalWidth, finalHeight,
493 mOriginalWidth, mOriginalHeight);
494 mFinishFrameAnimation.initialize(finalWidth, finalHeight,
495 mOriginalWidth, mOriginalHeight);
496 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800497 }
498 mRotateEnterAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight);
499 mRotateExitAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700500 if (USE_CUSTOM_BLACK_FRAME) {
501 mRotateFrameAnimation.initialize(finalWidth, finalHeight, mOriginalWidth,
502 mOriginalHeight);
503 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800504 mAnimRunning = false;
505 mFinishAnimReady = false;
506 mFinishAnimStartTime = -1;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800507
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700508 if (TWO_PHASE_ANIMATION && firstStart) {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800509 mStartExitAnimation.restrictDuration(maxAnimationDuration);
510 mStartExitAnimation.scaleCurrentDuration(animationScale);
511 mStartEnterAnimation.restrictDuration(maxAnimationDuration);
512 mStartEnterAnimation.scaleCurrentDuration(animationScale);
513 mFinishExitAnimation.restrictDuration(maxAnimationDuration);
514 mFinishExitAnimation.scaleCurrentDuration(animationScale);
515 mFinishEnterAnimation.restrictDuration(maxAnimationDuration);
516 mFinishEnterAnimation.scaleCurrentDuration(animationScale);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700517 if (USE_CUSTOM_BLACK_FRAME) {
518 mStartFrameAnimation.restrictDuration(maxAnimationDuration);
519 mStartFrameAnimation.scaleCurrentDuration(animationScale);
520 mFinishFrameAnimation.restrictDuration(maxAnimationDuration);
521 mFinishFrameAnimation.scaleCurrentDuration(animationScale);
522 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800523 }
524 mRotateExitAnimation.restrictDuration(maxAnimationDuration);
525 mRotateExitAnimation.scaleCurrentDuration(animationScale);
526 mRotateEnterAnimation.restrictDuration(maxAnimationDuration);
527 mRotateEnterAnimation.scaleCurrentDuration(animationScale);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700528 if (USE_CUSTOM_BLACK_FRAME) {
529 mRotateFrameAnimation.restrictDuration(maxAnimationDuration);
530 mRotateFrameAnimation.scaleCurrentDuration(animationScale);
531 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800532
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700533 final int layerStack = mDisplayContent.getDisplay().getLayerStack();
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700534 if (USE_CUSTOM_BLACK_FRAME && mCustomBlackFrame == null) {
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800535 // Compute the transformation matrix that must be applied
536 // the the black frame to make it stay in the initial position
537 // before the new screen rotation. This is different than the
538 // snapshot transformation because the snapshot is always based
539 // of the native orientation of the screen, not the orientation
540 // we were last in.
541 createRotationMatrix(delta, mOriginalWidth, mOriginalHeight, mFrameInitialMatrix);
542
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800543 try {
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800544 Rect outer = new Rect(-mOriginalWidth*1, -mOriginalHeight*1,
545 mOriginalWidth*2, mOriginalHeight*2);
546 Rect inner = new Rect(0, 0, mOriginalWidth, mOriginalHeight);
Robert Carrae606b42018-02-15 15:36:23 -0800547 mCustomBlackFrame = new BlackFrame(t, outer, inner,
Robert Carrb1579c82017-09-05 14:54:47 -0700548 SCREEN_FREEZE_LAYER_CUSTOM, mDisplayContent, false);
Robert Carrae606b42018-02-15 15:36:23 -0800549 mCustomBlackFrame.setMatrix(t, mFrameInitialMatrix);
Igor Murashkina86ab6402013-08-30 12:58:36 -0700550 } catch (OutOfResourcesException e) {
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700551 Slog.w(TAG, "Unable to allocate black surface", e);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700552 }
553 }
554
Dianne Hackborn9d9ece32012-09-10 15:33:52 -0700555 if (!customAnim && mExitingBlackFrame == null) {
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700556 try {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700557 // Compute the transformation matrix that must be applied
558 // the the black frame to make it stay in the initial position
559 // before the new screen rotation. This is different than the
560 // snapshot transformation because the snapshot is always based
561 // of the native orientation of the screen, not the orientation
562 // we were last in.
563 createRotationMatrix(delta, mOriginalWidth, mOriginalHeight, mFrameInitialMatrix);
564
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700565 final Rect outer;
566 final Rect inner;
567 if (mForceDefaultOrientation) {
568 // Going from a smaller Display to a larger Display, add curtains to sides
569 // or top and bottom. Going from a larger to smaller display will result in
570 // no BlackSurfaces being constructed.
571 outer = mCurrentDisplayRect;
572 inner = mOriginalDisplayRect;
573 } else {
574 outer = new Rect(-mOriginalWidth*1, -mOriginalHeight*1,
575 mOriginalWidth*2, mOriginalHeight*2);
576 inner = new Rect(0, 0, mOriginalWidth, mOriginalHeight);
577 }
Robert Carrae606b42018-02-15 15:36:23 -0800578 mExitingBlackFrame = new BlackFrame(t, outer, inner,
Robert Carrb1579c82017-09-05 14:54:47 -0700579 SCREEN_FREEZE_LAYER_EXIT, mDisplayContent, mForceDefaultOrientation);
Robert Carrae606b42018-02-15 15:36:23 -0800580 mExitingBlackFrame.setMatrix(t, mFrameInitialMatrix);
Igor Murashkina86ab6402013-08-30 12:58:36 -0700581 } catch (OutOfResourcesException e) {
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700582 Slog.w(TAG, "Unable to allocate black surface", e);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700583 }
584 }
585
Dianne Hackborn9d9ece32012-09-10 15:33:52 -0700586 if (customAnim && mEnteringBlackFrame == null) {
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700587 try {
588 Rect outer = new Rect(-finalWidth*1, -finalHeight*1,
589 finalWidth*2, finalHeight*2);
590 Rect inner = new Rect(0, 0, finalWidth, finalHeight);
Robert Carrae606b42018-02-15 15:36:23 -0800591 mEnteringBlackFrame = new BlackFrame(t, outer, inner,
Robert Carrb1579c82017-09-05 14:54:47 -0700592 SCREEN_FREEZE_LAYER_ENTER, mDisplayContent, false);
Igor Murashkina86ab6402013-08-30 12:58:36 -0700593 } catch (OutOfResourcesException e) {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800594 Slog.w(TAG, "Unable to allocate black surface", e);
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800595 }
Dianne Hackborn50660e22011-02-02 17:12:25 -0800596 }
597
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800598 return true;
599 }
600
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800601 /**
602 * Returns true if animating.
603 */
Robert Carrae606b42018-02-15 15:36:23 -0800604 public boolean dismiss(SurfaceControl.Transaction t, long maxAnimationDuration,
Craig Mautner3c174372013-02-21 17:54:37 -0800605 float animationScale, int finalWidth, int finalHeight, int exitAnim, int enterAnim) {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800606 if (DEBUG_STATE) Slog.v(TAG, "Dismiss!");
Mathias Agopian29479eb2013-02-14 14:36:04 -0800607 if (mSurfaceControl == null) {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800608 // Can't do animation.
609 return false;
610 }
611 if (!mStarted) {
Robert Carrae606b42018-02-15 15:36:23 -0800612 startAnimation(t, maxAnimationDuration, animationScale, finalWidth, finalHeight,
Craig Mautner3c174372013-02-21 17:54:37 -0800613 true, exitAnim, enterAnim);
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800614 }
615 if (!mStarted) {
616 return false;
617 }
618 if (DEBUG_STATE) Slog.v(TAG, "Setting mFinishAnimReady = true");
619 mFinishAnimReady = true;
620 return true;
621 }
622
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800623 public void kill() {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800624 if (DEBUG_STATE) Slog.v(TAG, "Kill!");
Mathias Agopian29479eb2013-02-14 14:36:04 -0800625 if (mSurfaceControl != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800626 if (SHOW_TRANSACTIONS ||
627 SHOW_SURFACE_ALLOC) Slog.i(TAG_WM,
Mathias Agopian29479eb2013-02-14 14:36:04 -0800628 " FREEZE " + mSurfaceControl + ": DESTROY");
Robert Carr5ea304d2019-02-04 16:04:55 -0800629 mSurfaceControl.remove();
Mathias Agopian29479eb2013-02-14 14:36:04 -0800630 mSurfaceControl = null;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800631 }
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700632 if (mCustomBlackFrame != null) {
633 mCustomBlackFrame.kill();
634 mCustomBlackFrame = null;
635 }
636 if (mExitingBlackFrame != null) {
637 mExitingBlackFrame.kill();
638 mExitingBlackFrame = null;
639 }
640 if (mEnteringBlackFrame != null) {
641 mEnteringBlackFrame.kill();
642 mEnteringBlackFrame = null;
Dianne Hackborn352cc982011-01-04 11:34:18 -0800643 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700644 if (TWO_PHASE_ANIMATION) {
645 if (mStartExitAnimation != null) {
646 mStartExitAnimation.cancel();
647 mStartExitAnimation = null;
648 }
649 if (mStartEnterAnimation != null) {
650 mStartEnterAnimation.cancel();
651 mStartEnterAnimation = null;
652 }
653 if (mFinishExitAnimation != null) {
654 mFinishExitAnimation.cancel();
655 mFinishExitAnimation = null;
656 }
657 if (mFinishEnterAnimation != null) {
658 mFinishEnterAnimation.cancel();
659 mFinishEnterAnimation = null;
660 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800661 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700662 if (USE_CUSTOM_BLACK_FRAME) {
663 if (mStartFrameAnimation != null) {
664 mStartFrameAnimation.cancel();
665 mStartFrameAnimation = null;
666 }
667 if (mRotateFrameAnimation != null) {
668 mRotateFrameAnimation.cancel();
669 mRotateFrameAnimation = null;
670 }
671 if (mFinishFrameAnimation != null) {
672 mFinishFrameAnimation.cancel();
673 mFinishFrameAnimation = null;
674 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800675 }
676 if (mRotateExitAnimation != null) {
677 mRotateExitAnimation.cancel();
678 mRotateExitAnimation = null;
679 }
680 if (mRotateEnterAnimation != null) {
681 mRotateEnterAnimation.cancel();
682 mRotateEnterAnimation = null;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800683 }
684 }
685
686 public boolean isAnimating() {
Craig Mautner7d8df392012-04-06 15:26:23 -0700687 return hasAnimations() || (TWO_PHASE_ANIMATION && mFinishAnimReady);
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700688 }
689
Dianne Hackborn4b169692012-11-29 17:51:24 -0800690 public boolean isRotating() {
691 return mCurRotation != mOriginalRotation;
692 }
693
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700694 private boolean hasAnimations() {
Craig Mautner7d8df392012-04-06 15:26:23 -0700695 return (TWO_PHASE_ANIMATION &&
696 (mStartEnterAnimation != null || mStartExitAnimation != null
697 || mFinishEnterAnimation != null || mFinishExitAnimation != null))
698 || (USE_CUSTOM_BLACK_FRAME &&
699 (mStartFrameAnimation != null || mRotateFrameAnimation != null
700 || mFinishFrameAnimation != null))
701 || mRotateEnterAnimation != null || mRotateExitAnimation != null;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800702 }
703
Craig Mautnere32c3072012-03-12 15:25:35 -0700704 private boolean stepAnimation(long now) {
Craig Mautner3255a282012-04-16 15:42:47 -0700705 if (now > mHalfwayPoint) {
706 mHalfwayPoint = Long.MAX_VALUE;
707 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800708 if (mFinishAnimReady && mFinishAnimStartTime < 0) {
709 if (DEBUG_STATE) Slog.v(TAG, "Step: finish anim now ready");
710 mFinishAnimStartTime = now;
711 }
712
Craig Mautner7d8df392012-04-06 15:26:23 -0700713 if (TWO_PHASE_ANIMATION) {
714 mMoreStartExit = false;
715 if (mStartExitAnimation != null) {
716 mMoreStartExit = mStartExitAnimation.getTransformation(now, mStartExitTransformation);
717 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped start exit: " + mStartExitTransformation);
718 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800719
Craig Mautner7d8df392012-04-06 15:26:23 -0700720 mMoreStartEnter = false;
721 if (mStartEnterAnimation != null) {
722 mMoreStartEnter = mStartEnterAnimation.getTransformation(now, mStartEnterTransformation);
723 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped start enter: " + mStartEnterTransformation);
724 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800725 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700726 if (USE_CUSTOM_BLACK_FRAME) {
727 mMoreStartFrame = false;
728 if (mStartFrameAnimation != null) {
729 mMoreStartFrame = mStartFrameAnimation.getTransformation(now, mStartFrameTransformation);
730 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped start frame: " + mStartFrameTransformation);
731 }
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800732 }
733
Craig Mautnerdbb79912012-03-01 18:59:14 -0800734 long finishNow = mFinishAnimReady ? (now - mFinishAnimStartTime) : 0;
735 if (DEBUG_STATE) Slog.v(TAG, "Step: finishNow=" + finishNow);
736
Craig Mautner7d8df392012-04-06 15:26:23 -0700737 if (TWO_PHASE_ANIMATION) {
738 mMoreFinishExit = false;
739 if (mFinishExitAnimation != null) {
740 mMoreFinishExit = mFinishExitAnimation.getTransformation(finishNow, mFinishExitTransformation);
741 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped finish exit: " + mFinishExitTransformation);
742 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800743
Craig Mautner7d8df392012-04-06 15:26:23 -0700744 mMoreFinishEnter = false;
745 if (mFinishEnterAnimation != null) {
746 mMoreFinishEnter = mFinishEnterAnimation.getTransformation(finishNow, mFinishEnterTransformation);
747 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped finish enter: " + mFinishEnterTransformation);
748 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800749 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700750 if (USE_CUSTOM_BLACK_FRAME) {
751 mMoreFinishFrame = false;
752 if (mFinishFrameAnimation != null) {
753 mMoreFinishFrame = mFinishFrameAnimation.getTransformation(finishNow, mFinishFrameTransformation);
754 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped finish frame: " + mFinishFrameTransformation);
755 }
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800756 }
757
Craig Mautnerdbb79912012-03-01 18:59:14 -0800758 mMoreRotateExit = false;
759 if (mRotateExitAnimation != null) {
760 mMoreRotateExit = mRotateExitAnimation.getTransformation(now, mRotateExitTransformation);
761 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped rotate exit: " + mRotateExitTransformation);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700762 }
763
764 mMoreRotateEnter = false;
765 if (mRotateEnterAnimation != null) {
766 mMoreRotateEnter = mRotateEnterAnimation.getTransformation(now, mRotateEnterTransformation);
767 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped rotate enter: " + mRotateEnterTransformation);
768 }
769
Craig Mautner7d8df392012-04-06 15:26:23 -0700770 if (USE_CUSTOM_BLACK_FRAME) {
771 mMoreRotateFrame = false;
772 if (mRotateFrameAnimation != null) {
773 mMoreRotateFrame = mRotateFrameAnimation.getTransformation(now, mRotateFrameTransformation);
774 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Stepped rotate frame: " + mRotateFrameTransformation);
775 }
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700776 }
777
Craig Mautner7d8df392012-04-06 15:26:23 -0700778 if (!mMoreRotateExit && (!TWO_PHASE_ANIMATION || (!mMoreStartExit && !mMoreFinishExit))) {
779 if (TWO_PHASE_ANIMATION) {
780 if (mStartExitAnimation != null) {
781 if (DEBUG_STATE) Slog.v(TAG, "Exit animations done, clearing start exit anim!");
782 mStartExitAnimation.cancel();
783 mStartExitAnimation = null;
784 mStartExitTransformation.clear();
785 }
786 if (mFinishExitAnimation != null) {
787 if (DEBUG_STATE) Slog.v(TAG, "Exit animations done, clearing finish exit anim!");
788 mFinishExitAnimation.cancel();
789 mFinishExitAnimation = null;
790 mFinishExitTransformation.clear();
791 }
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700792 }
793 if (mRotateExitAnimation != null) {
794 if (DEBUG_STATE) Slog.v(TAG, "Exit animations done, clearing rotate exit anim!");
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800795 mRotateExitAnimation.cancel();
796 mRotateExitAnimation = null;
797 mRotateExitTransformation.clear();
798 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800799 }
800
Craig Mautner7d8df392012-04-06 15:26:23 -0700801 if (!mMoreRotateEnter && (!TWO_PHASE_ANIMATION || (!mMoreStartEnter && !mMoreFinishEnter))) {
802 if (TWO_PHASE_ANIMATION) {
803 if (mStartEnterAnimation != null) {
804 if (DEBUG_STATE) Slog.v(TAG, "Enter animations done, clearing start enter anim!");
805 mStartEnterAnimation.cancel();
806 mStartEnterAnimation = null;
807 mStartEnterTransformation.clear();
808 }
809 if (mFinishEnterAnimation != null) {
810 if (DEBUG_STATE) Slog.v(TAG, "Enter animations done, clearing finish enter anim!");
811 mFinishEnterAnimation.cancel();
812 mFinishEnterAnimation = null;
813 mFinishEnterTransformation.clear();
814 }
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700815 }
816 if (mRotateEnterAnimation != null) {
817 if (DEBUG_STATE) Slog.v(TAG, "Enter animations done, clearing rotate enter anim!");
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800818 mRotateEnterAnimation.cancel();
819 mRotateEnterAnimation = null;
820 mRotateEnterTransformation.clear();
821 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800822 }
823
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700824 if (USE_CUSTOM_BLACK_FRAME && !mMoreStartFrame && !mMoreRotateFrame && !mMoreFinishFrame) {
825 if (mStartFrameAnimation != null) {
826 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, clearing start frame anim!");
827 mStartFrameAnimation.cancel();
828 mStartFrameAnimation = null;
829 mStartFrameTransformation.clear();
830 }
831 if (mFinishFrameAnimation != null) {
832 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, clearing finish frame anim!");
833 mFinishFrameAnimation.cancel();
834 mFinishFrameAnimation = null;
835 mFinishFrameTransformation.clear();
836 }
837 if (mRotateFrameAnimation != null) {
838 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, clearing rotate frame anim!");
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800839 mRotateFrameAnimation.cancel();
840 mRotateFrameAnimation = null;
841 mRotateFrameTransformation.clear();
842 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800843 }
844
845 mExitTransformation.set(mRotateExitTransformation);
Craig Mautnerdbb79912012-03-01 18:59:14 -0800846 mEnterTransformation.set(mRotateEnterTransformation);
Craig Mautner7d8df392012-04-06 15:26:23 -0700847 if (TWO_PHASE_ANIMATION) {
848 mExitTransformation.compose(mStartExitTransformation);
849 mExitTransformation.compose(mFinishExitTransformation);
850
851 mEnterTransformation.compose(mStartEnterTransformation);
852 mEnterTransformation.compose(mFinishEnterTransformation);
853 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800854
855 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Final exit: " + mExitTransformation);
856 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Final enter: " + mEnterTransformation);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700857
858 if (USE_CUSTOM_BLACK_FRAME) {
859 //mFrameTransformation.set(mRotateExitTransformation);
860 //mFrameTransformation.compose(mStartExitTransformation);
861 //mFrameTransformation.compose(mFinishExitTransformation);
862 mFrameTransformation.set(mRotateFrameTransformation);
863 mFrameTransformation.compose(mStartFrameTransformation);
864 mFrameTransformation.compose(mFinishFrameTransformation);
865 mFrameTransformation.getMatrix().preConcat(mFrameInitialMatrix);
866 if (DEBUG_TRANSFORMS) Slog.v(TAG, "Final frame: " + mFrameTransformation);
867 }
Craig Mautnerdbb79912012-03-01 18:59:14 -0800868
Craig Mautner7d8df392012-04-06 15:26:23 -0700869 final boolean more = (TWO_PHASE_ANIMATION
870 && (mMoreStartEnter || mMoreStartExit || mMoreFinishEnter || mMoreFinishExit))
871 || (USE_CUSTOM_BLACK_FRAME
872 && (mMoreStartFrame || mMoreRotateFrame || mMoreFinishFrame))
Igor Murashkina86ab6402013-08-30 12:58:36 -0700873 || mMoreRotateEnter || mMoreRotateExit
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800874 || !mFinishAnimReady;
Craig Mautnerdbb79912012-03-01 18:59:14 -0800875
876 mSnapshotFinalMatrix.setConcat(mExitTransformation.getMatrix(), mSnapshotInitialMatrix);
877
878 if (DEBUG_STATE) Slog.v(TAG, "Step: more=" + more);
879
880 return more;
881 }
882
Robert Carrae606b42018-02-15 15:36:23 -0800883 void updateSurfaces(SurfaceControl.Transaction t) {
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700884 if (!mStarted) {
885 return;
886 }
887
Mathias Agopian29479eb2013-02-14 14:36:04 -0800888 if (mSurfaceControl != null) {
Craig Mautnerbf90eaa2012-03-15 11:28:53 -0700889 if (!mMoreStartExit && !mMoreFinishExit && !mMoreRotateExit) {
Craig Mautnerdbb79912012-03-01 18:59:14 -0800890 if (DEBUG_STATE) Slog.v(TAG, "Exit animations done, hiding screenshot surface");
Robert Carrae606b42018-02-15 15:36:23 -0800891 t.hide(mSurfaceControl);
Craig Mautnerdbb79912012-03-01 18:59:14 -0800892 }
893 }
894
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700895 if (mCustomBlackFrame != null) {
Craig Mautnerbf90eaa2012-03-15 11:28:53 -0700896 if (!mMoreStartFrame && !mMoreFinishFrame && !mMoreRotateFrame) {
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800897 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, hiding black frame");
Robert Carrae606b42018-02-15 15:36:23 -0800898 mCustomBlackFrame.hide(t);
Craig Mautnerbf90eaa2012-03-15 11:28:53 -0700899 } else {
Robert Carrae606b42018-02-15 15:36:23 -0800900 mCustomBlackFrame.setMatrix(t, mFrameTransformation.getMatrix());
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700901 }
902 }
903
904 if (mExitingBlackFrame != null) {
905 if (!mMoreStartExit && !mMoreFinishExit && !mMoreRotateExit) {
906 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, hiding exiting frame");
Robert Carrae606b42018-02-15 15:36:23 -0800907 mExitingBlackFrame.hide(t);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700908 } else {
909 mExitFrameFinalMatrix.setConcat(mExitTransformation.getMatrix(), mFrameInitialMatrix);
Robert Carrae606b42018-02-15 15:36:23 -0800910 mExitingBlackFrame.setMatrix(t, mExitFrameFinalMatrix);
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700911 if (mForceDefaultOrientation) {
Robert Carrae606b42018-02-15 15:36:23 -0800912 mExitingBlackFrame.setAlpha(t, mExitTransformation.getAlpha());
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700913 }
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700914 }
915 }
916
917 if (mEnteringBlackFrame != null) {
918 if (!mMoreStartEnter && !mMoreFinishEnter && !mMoreRotateEnter) {
919 if (DEBUG_STATE) Slog.v(TAG, "Frame animations done, hiding entering frame");
Robert Carrae606b42018-02-15 15:36:23 -0800920 mEnteringBlackFrame.hide(t);
Dianne Hackbornd6b32b62012-03-16 11:54:51 -0700921 } else {
Robert Carrae606b42018-02-15 15:36:23 -0800922 mEnteringBlackFrame.setMatrix(t, mEnterTransformation.getMatrix());
Craig Mautnerdbb79912012-03-01 18:59:14 -0800923 }
924 }
925
Robert Carrae606b42018-02-15 15:36:23 -0800926 setSnapshotTransform(t, mSnapshotFinalMatrix, mExitTransformation.getAlpha());
Craig Mautnerdbb79912012-03-01 18:59:14 -0800927 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700928
Craig Mautnere32c3072012-03-12 15:25:35 -0700929 public boolean stepAnimationLocked(long now) {
Dianne Hackborn187ae2102012-04-11 18:12:06 -0700930 if (!hasAnimations()) {
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800931 if (DEBUG_STATE) Slog.v(TAG, "Step: no animations running");
Craig Mautnera731cd32012-03-02 15:23:55 -0800932 mFinishAnimReady = false;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800933 return false;
934 }
935
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800936 if (!mAnimRunning) {
937 if (DEBUG_STATE) Slog.v(TAG, "Step: starting start, finish, rotate");
Craig Mautner7d8df392012-04-06 15:26:23 -0700938 if (TWO_PHASE_ANIMATION) {
939 if (mStartEnterAnimation != null) {
940 mStartEnterAnimation.setStartTime(now);
941 }
942 if (mStartExitAnimation != null) {
943 mStartExitAnimation.setStartTime(now);
944 }
945 if (mFinishEnterAnimation != null) {
946 mFinishEnterAnimation.setStartTime(0);
947 }
948 if (mFinishExitAnimation != null) {
949 mFinishExitAnimation.setStartTime(0);
950 }
Dianne Hackborn89620282011-09-11 12:47:45 -0700951 }
Craig Mautner7d8df392012-04-06 15:26:23 -0700952 if (USE_CUSTOM_BLACK_FRAME) {
953 if (mStartFrameAnimation != null) {
954 mStartFrameAnimation.setStartTime(now);
955 }
956 if (mFinishFrameAnimation != null) {
957 mFinishFrameAnimation.setStartTime(0);
958 }
959 if (mRotateFrameAnimation != null) {
960 mRotateFrameAnimation.setStartTime(now);
961 }
Dianne Hackborn9fd74802012-03-01 19:26:31 -0800962 }
Dianne Hackbornfd1c5ed2012-01-13 13:09:16 -0800963 if (mRotateEnterAnimation != null) {
964 mRotateEnterAnimation.setStartTime(now);
965 }
966 if (mRotateExitAnimation != null) {
967 mRotateExitAnimation.setStartTime(now);
968 }
969 mAnimRunning = true;
Craig Mautner3255a282012-04-16 15:42:47 -0700970 mHalfwayPoint = now + mRotateEnterAnimation.getDuration() / 2;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800971 }
Craig Mautnere32c3072012-03-12 15:25:35 -0700972
973 return stepAnimation(now);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800974 }
975
976 public Transformation getEnterTransformation() {
977 return mEnterTransformation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800978 }
979}