AppWindowAnimator.java revision 2049dbf0e9f658c1f5234fb0a1f0af6942b7eb58
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.wm; 18 19import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; 20import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS; 21import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY; 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; 25import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET; 26import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_BEFORE_ANIM; 27 28import android.graphics.Matrix; 29import android.util.Slog; 30import android.util.TimeUtils; 31import android.view.Choreographer; 32import android.view.Display; 33import android.view.SurfaceControl; 34import android.view.WindowManagerPolicy; 35import android.view.animation.Animation; 36import android.view.animation.Transformation; 37 38import java.io.PrintWriter; 39import java.util.ArrayList; 40 41public class AppWindowAnimator { 42 static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowAnimator" : TAG_WM; 43 44 private static final int PROLONG_ANIMATION_DISABLED = 0; 45 static final int PROLONG_ANIMATION_AT_END = 1; 46 static final int PROLONG_ANIMATION_AT_START = 2; 47 48 final AppWindowToken mAppToken; 49 final WindowManagerService mService; 50 final WindowAnimator mAnimator; 51 52 boolean animating; 53 boolean wasAnimating; 54 Animation animation; 55 boolean hasTransformation; 56 final Transformation transformation = new Transformation(); 57 58 // Have we been asked to have this token keep the screen frozen? 59 // Protect with mAnimator. 60 boolean freezingScreen; 61 62 /** 63 * How long we last kept the screen frozen. 64 */ 65 int lastFreezeDuration; 66 67 // Offset to the window of all layers in the token, for use by 68 // AppWindowToken animations. 69 int animLayerAdjustment; 70 71 // Propagated from AppWindowToken.allDrawn, to determine when 72 // the state changes. 73 boolean allDrawn; 74 75 // Special surface for thumbnail animation. If deferThumbnailDestruction is enabled, then we 76 // will make sure that the thumbnail is destroyed after the other surface is completed. This 77 // requires that the duration of the two animations are the same. 78 SurfaceControl thumbnail; 79 int thumbnailTransactionSeq; 80 int thumbnailLayer; 81 int thumbnailForceAboveLayer; 82 Animation thumbnailAnimation; 83 final Transformation thumbnailTransformation = new Transformation(); 84 // This flag indicates that the destruction of the thumbnail surface is synchronized with 85 // another animation, so defer the destruction of this thumbnail surface for a single frame 86 // after the secondary animation completes. 87 boolean deferThumbnailDestruction; 88 // This flag is set if the animator has deferThumbnailDestruction set and has reached the final 89 // frame of animation. It will extend the animation by one frame and then clean up afterwards. 90 boolean deferFinalFrameCleanup; 91 // If true when the animation hits the last frame, it will keep running on that last frame. 92 // This is used to synchronize animation with Recents and we wait for Recents to tell us to 93 // finish or for a new animation be set as fail-safe mechanism. 94 private int mProlongAnimation; 95 // Whether the prolong animation can be removed when animation is set. The purpose of this is 96 // that if recents doesn't tell us to remove the prolonged animation, we will get rid of it 97 // when new animation is set. 98 private boolean mClearProlongedAnimation; 99 100 /** WindowStateAnimator from mAppAnimator.allAppWindows as of last performLayout */ 101 ArrayList<WindowStateAnimator> mAllAppWinAnimators = new ArrayList<>(); 102 103 /** True if the current animation was transferred from another AppWindowAnimator. 104 * See {@link #transferCurrentAnimation}*/ 105 boolean usingTransferredAnimation = false; 106 107 private boolean mSkipFirstFrame = false; 108 private int mStackClip = STACK_CLIP_BEFORE_ANIM; 109 110 static final Animation sDummyAnimation = new DummyAnimation(); 111 112 public AppWindowAnimator(final AppWindowToken atoken, WindowManagerService service) { 113 mAppToken = atoken; 114 mService = service; 115 mAnimator = mService.mAnimator; 116 } 117 118 public void setAnimation(Animation anim, int width, int height, boolean skipFirstFrame, 119 int stackClip) { 120 if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting animation in " + mAppToken 121 + ": " + anim + " wxh=" + width + "x" + height 122 + " isVisible=" + mAppToken.isVisible()); 123 animation = anim; 124 animating = false; 125 if (!anim.isInitialized()) { 126 anim.initialize(width, height, width, height); 127 } 128 anim.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION); 129 anim.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked()); 130 int zorder = anim.getZAdjustment(); 131 int adj = 0; 132 if (zorder == Animation.ZORDER_TOP) { 133 adj = TYPE_LAYER_OFFSET; 134 } else if (zorder == Animation.ZORDER_BOTTOM) { 135 adj = -TYPE_LAYER_OFFSET; 136 } 137 138 if (animLayerAdjustment != adj) { 139 animLayerAdjustment = adj; 140 updateLayers(); 141 } 142 // Start out animation gone if window is gone, or visible if window is visible. 143 transformation.clear(); 144 transformation.setAlpha(mAppToken.isVisible() ? 1 : 0); 145 hasTransformation = true; 146 mStackClip = stackClip; 147 148 this.mSkipFirstFrame = skipFirstFrame; 149 150 if (!mAppToken.appFullscreen) { 151 anim.setBackgroundColor(0); 152 } 153 if (mClearProlongedAnimation) { 154 mProlongAnimation = PROLONG_ANIMATION_DISABLED; 155 } else { 156 mClearProlongedAnimation = true; 157 } 158 159 // Since we are finally starting our animation, we don't need the logic anymore to prevent 160 // the app from showing again if we just moved between stacks. 161 // See {@link WindowState#notifyMovedInStack}. 162 mAppToken.resetJustMovedInStack(); 163 } 164 165 public void setDummyAnimation() { 166 if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting dummy animation in " + mAppToken 167 + " isVisible=" + mAppToken.isVisible()); 168 animation = sDummyAnimation; 169 hasTransformation = true; 170 transformation.clear(); 171 transformation.setAlpha(mAppToken.isVisible() ? 1 : 0); 172 } 173 174 void setNullAnimation() { 175 animation = null; 176 usingTransferredAnimation = false; 177 } 178 179 public void clearAnimation() { 180 if (animation != null) { 181 animating = true; 182 } 183 clearThumbnail(); 184 setNullAnimation(); 185 if (mAppToken.deferClearAllDrawn) { 186 mAppToken.clearAllDrawn(); 187 } 188 mStackClip = STACK_CLIP_BEFORE_ANIM; 189 } 190 191 public boolean isAnimating() { 192 return animation != null || mAppToken.inPendingTransaction; 193 } 194 195 public void clearThumbnail() { 196 if (thumbnail != null) { 197 thumbnail.hide(); 198 mService.mWindowPlacerLocked.destroyAfterTransaction(thumbnail); 199 thumbnail = null; 200 } 201 deferThumbnailDestruction = false; 202 } 203 204 int getStackClip() { 205 return mStackClip; 206 } 207 208 void transferCurrentAnimation( 209 AppWindowAnimator toAppAnimator, WindowStateAnimator transferWinAnimator) { 210 211 if (animation != null) { 212 toAppAnimator.animation = animation; 213 toAppAnimator.animating = animating; 214 toAppAnimator.animLayerAdjustment = animLayerAdjustment; 215 setNullAnimation(); 216 animLayerAdjustment = 0; 217 toAppAnimator.updateLayers(); 218 updateLayers(); 219 toAppAnimator.usingTransferredAnimation = true; 220 } 221 if (transferWinAnimator != null) { 222 mAllAppWinAnimators.remove(transferWinAnimator); 223 toAppAnimator.mAllAppWinAnimators.add(transferWinAnimator); 224 toAppAnimator.hasTransformation = transferWinAnimator.mAppAnimator.hasTransformation; 225 if (toAppAnimator.hasTransformation) { 226 toAppAnimator.transformation.set(transferWinAnimator.mAppAnimator.transformation); 227 } else { 228 toAppAnimator.transformation.clear(); 229 } 230 transferWinAnimator.mAppAnimator = toAppAnimator; 231 } 232 } 233 234 void updateLayers() { 235 thumbnailLayer = mAppToken.adjustAnimLayer(animLayerAdjustment); 236 } 237 238 private void stepThumbnailAnimation(long currentTime) { 239 thumbnailTransformation.clear(); 240 final long animationFrameTime = getAnimationFrameTime(thumbnailAnimation, currentTime); 241 thumbnailAnimation.getTransformation(animationFrameTime, thumbnailTransformation); 242 243 ScreenRotationAnimation screenRotationAnimation = 244 mAnimator.getScreenRotationAnimationLocked(Display.DEFAULT_DISPLAY); 245 final boolean screenAnimation = screenRotationAnimation != null 246 && screenRotationAnimation.isAnimating(); 247 if (screenAnimation) { 248 thumbnailTransformation.postCompose(screenRotationAnimation.getEnterTransformation()); 249 } 250 // cache often used attributes locally 251 final float tmpFloats[] = mService.mTmpFloats; 252 thumbnailTransformation.getMatrix().getValues(tmpFloats); 253 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail, 254 "thumbnail", "POS " + tmpFloats[Matrix.MTRANS_X] 255 + ", " + tmpFloats[Matrix.MTRANS_Y]); 256 thumbnail.setPosition(tmpFloats[Matrix.MTRANS_X], tmpFloats[Matrix.MTRANS_Y]); 257 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail, 258 "thumbnail", "alpha=" + thumbnailTransformation.getAlpha() 259 + " layer=" + thumbnailLayer 260 + " matrix=[" + tmpFloats[Matrix.MSCALE_X] 261 + "," + tmpFloats[Matrix.MSKEW_Y] 262 + "][" + tmpFloats[Matrix.MSKEW_X] 263 + "," + tmpFloats[Matrix.MSCALE_Y] + "]"); 264 thumbnail.setAlpha(thumbnailTransformation.getAlpha()); 265 if (thumbnailForceAboveLayer > 0) { 266 thumbnail.setLayer(thumbnailForceAboveLayer + 1); 267 } else { 268 // The thumbnail is layered below the window immediately above this 269 // token's anim layer. 270 thumbnail.setLayer(thumbnailLayer + WindowManagerService.WINDOW_LAYER_MULTIPLIER 271 - WindowManagerService.LAYER_OFFSET_THUMBNAIL); 272 } 273 thumbnail.setMatrix(tmpFloats[Matrix.MSCALE_X], tmpFloats[Matrix.MSKEW_Y], 274 tmpFloats[Matrix.MSKEW_X], tmpFloats[Matrix.MSCALE_Y]); 275 thumbnail.setWindowCrop(thumbnailTransformation.getClipRect()); 276 } 277 278 /** 279 * Sometimes we need to synchronize the first frame of animation with some external event, e.g. 280 * Recents hiding some of its content. To achieve this, we prolong the start of the animaiton 281 * and keep producing the first frame of the animation. 282 */ 283 private long getAnimationFrameTime(Animation animation, long currentTime) { 284 if (mProlongAnimation == PROLONG_ANIMATION_AT_START) { 285 animation.setStartTime(currentTime); 286 return currentTime + 1; 287 } 288 return currentTime; 289 } 290 291 private boolean stepAnimation(long currentTime) { 292 if (animation == null) { 293 return false; 294 } 295 transformation.clear(); 296 final long animationFrameTime = getAnimationFrameTime(animation, currentTime); 297 boolean hasMoreFrames = animation.getTransformation(animationFrameTime, transformation); 298 if (!hasMoreFrames) { 299 if (deferThumbnailDestruction && !deferFinalFrameCleanup) { 300 // We are deferring the thumbnail destruction, so extend the animation for one more 301 // (dummy) frame before we clean up 302 deferFinalFrameCleanup = true; 303 hasMoreFrames = true; 304 } else { 305 if (false && DEBUG_ANIM) Slog.v(TAG, 306 "Stepped animation in " + mAppToken + ": more=" + hasMoreFrames + 307 ", xform=" + transformation + ", mProlongAnimation=" + mProlongAnimation); 308 deferFinalFrameCleanup = false; 309 if (mProlongAnimation == PROLONG_ANIMATION_AT_END) { 310 hasMoreFrames = true; 311 } else { 312 setNullAnimation(); 313 clearThumbnail(); 314 if (DEBUG_ANIM) Slog.v(TAG, "Finished animation in " + mAppToken + " @ " 315 + currentTime); 316 } 317 } 318 } 319 hasTransformation = hasMoreFrames; 320 return hasMoreFrames; 321 } 322 323 private long getStartTimeCorrection() { 324 if (mSkipFirstFrame) { 325 326 // If the transition is an animation in which the first frame doesn't change the screen 327 // contents at all, we can just skip it and start at the second frame. So we shift the 328 // start time of the animation forward by minus the frame duration. 329 return -Choreographer.getInstance().getFrameIntervalNanos() / TimeUtils.NANOS_PER_MS; 330 } else { 331 return 0; 332 } 333 } 334 335 // This must be called while inside a transaction. 336 boolean stepAnimationLocked(long currentTime, final int displayId) { 337 if (mService.okToDisplay()) { 338 // We will run animations as long as the display isn't frozen. 339 340 if (animation == sDummyAnimation) { 341 // This guy is going to animate, but not yet. For now count 342 // it as not animating for purposes of scheduling transactions; 343 // when it is really time to animate, this will be set to 344 // a real animation and the next call will execute normally. 345 return false; 346 } 347 348 if ((mAppToken.allDrawn || animating || mAppToken.startingDisplayed) 349 && animation != null) { 350 if (!animating) { 351 if (DEBUG_ANIM) Slog.v(TAG, 352 "Starting animation in " + mAppToken + 353 " @ " + currentTime + " scale=" 354 + mService.getTransitionAnimationScaleLocked() 355 + " allDrawn=" + mAppToken.allDrawn + " animating=" + animating); 356 long correction = getStartTimeCorrection(); 357 animation.setStartTime(currentTime + correction); 358 animating = true; 359 if (thumbnail != null) { 360 thumbnail.show(); 361 thumbnailAnimation.setStartTime(currentTime + correction); 362 } 363 mSkipFirstFrame = false; 364 } 365 if (stepAnimation(currentTime)) { 366 // animation isn't over, step any thumbnail and that's 367 // it for now. 368 if (thumbnail != null) { 369 stepThumbnailAnimation(currentTime); 370 } 371 return true; 372 } 373 } 374 } else if (animation != null) { 375 // If the display is frozen, and there is a pending animation, 376 // clear it and make sure we run the cleanup code. 377 animating = true; 378 animation = null; 379 } 380 381 hasTransformation = false; 382 383 if (!animating && animation == null) { 384 return false; 385 } 386 387 mAnimator.setAppLayoutChanges(this, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM, 388 "AppWindowToken", displayId); 389 390 clearAnimation(); 391 animating = false; 392 if (animLayerAdjustment != 0) { 393 animLayerAdjustment = 0; 394 updateLayers(); 395 } 396 if (mService.mInputMethodTarget != null 397 && mService.mInputMethodTarget.mAppToken == mAppToken) { 398 mService.moveInputMethodWindowsIfNeededLocked(true); 399 } 400 401 if (DEBUG_ANIM) Slog.v(TAG, 402 "Animation done in " + mAppToken 403 + ": reportedVisible=" + mAppToken.reportedVisible); 404 405 transformation.clear(); 406 407 final int numAllAppWinAnimators = mAllAppWinAnimators.size(); 408 for (int i = 0; i < numAllAppWinAnimators; i++) { 409 mAllAppWinAnimators.get(i).mWin.onExitAnimationDone(); 410 } 411 mService.mAppTransition.notifyAppTransitionFinishedLocked(mAppToken.token); 412 return false; 413 } 414 415 // This must be called while inside a transaction. 416 boolean showAllWindowsLocked() { 417 boolean isAnimating = false; 418 final int NW = mAllAppWinAnimators.size(); 419 for (int i=0; i<NW; i++) { 420 WindowStateAnimator winAnimator = mAllAppWinAnimators.get(i); 421 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + winAnimator); 422 winAnimator.mWin.performShowLocked(); 423 isAnimating |= winAnimator.isAnimationSet(); 424 } 425 return isAnimating; 426 } 427 428 void dump(PrintWriter pw, String prefix, boolean dumpAll) { 429 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken); 430 pw.print(prefix); pw.print("mAnimator="); pw.println(mAnimator); 431 pw.print(prefix); pw.print("freezingScreen="); pw.print(freezingScreen); 432 pw.print(" allDrawn="); pw.print(allDrawn); 433 pw.print(" animLayerAdjustment="); pw.println(animLayerAdjustment); 434 if (lastFreezeDuration != 0) { 435 pw.print(prefix); pw.print("lastFreezeDuration="); 436 TimeUtils.formatDuration(lastFreezeDuration, pw); pw.println(); 437 } 438 if (animating || animation != null) { 439 pw.print(prefix); pw.print("animating="); pw.println(animating); 440 pw.print(prefix); pw.print("animation="); pw.println(animation); 441 } 442 if (hasTransformation) { 443 pw.print(prefix); pw.print("XForm: "); 444 transformation.printShortString(pw); 445 pw.println(); 446 } 447 if (thumbnail != null) { 448 pw.print(prefix); pw.print("thumbnail="); pw.print(thumbnail); 449 pw.print(" layer="); pw.println(thumbnailLayer); 450 pw.print(prefix); pw.print("thumbnailAnimation="); pw.println(thumbnailAnimation); 451 pw.print(prefix); pw.print("thumbnailTransformation="); 452 pw.println(thumbnailTransformation.toShortString()); 453 } 454 for (int i=0; i<mAllAppWinAnimators.size(); i++) { 455 WindowStateAnimator wanim = mAllAppWinAnimators.get(i); 456 pw.print(prefix); pw.print("App Win Anim #"); pw.print(i); 457 pw.print(": "); pw.println(wanim); 458 } 459 } 460 461 void startProlongAnimation(int prolongType) { 462 mProlongAnimation = prolongType; 463 mClearProlongedAnimation = false; 464 } 465 466 void endProlongedAnimation() { 467 mProlongAnimation = PROLONG_ANIMATION_DISABLED; 468 } 469 470 // This is an animation that does nothing: it just immediately finishes 471 // itself every time it is called. It is used as a stub animation in cases 472 // where we want to synchronize multiple things that may be animating. 473 static final class DummyAnimation extends Animation { 474 @Override 475 public boolean getTransformation(long currentTime, Transformation outTransformation) { 476 return false; 477 } 478 } 479 480} 481