AppWindowAnimator.java revision de63d441d7daf0503bcc6d5fd3f4f7efe06e23d3
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 thumbnailX; 81 int thumbnailY; 82 int thumbnailLayer; 83 int thumbnailForceAboveLayer; 84 Animation thumbnailAnimation; 85 final Transformation thumbnailTransformation = new Transformation(); 86 // This flag indicates that the destruction of the thumbnail surface is synchronized with 87 // another animation, so defer the destruction of this thumbnail surface for a single frame 88 // after the secondary animation completes. 89 boolean deferThumbnailDestruction; 90 // This flag is set if the animator has deferThumbnailDestruction set and has reached the final 91 // frame of animation. It will extend the animation by one frame and then clean up afterwards. 92 boolean deferFinalFrameCleanup; 93 // If true when the animation hits the last frame, it will keep running on that last frame. 94 // This is used to synchronize animation with Recents and we wait for Recents to tell us to 95 // finish or for a new animation be set as fail-safe mechanism. 96 private int mProlongAnimation; 97 // Whether the prolong animation can be removed when animation is set. The purpose of this is 98 // that if recents doesn't tell us to remove the prolonged animation, we will get rid of it 99 // when new animation is set. 100 private boolean mClearProlongedAnimation; 101 102 /** WindowStateAnimator from mAppAnimator.allAppWindows as of last performLayout */ 103 ArrayList<WindowStateAnimator> mAllAppWinAnimators = new ArrayList<>(); 104 105 /** True if the current animation was transferred from another AppWindowAnimator. 106 * See {@link #transferCurrentAnimation}*/ 107 boolean usingTransferredAnimation = false; 108 109 private boolean mSkipFirstFrame = false; 110 private int mStackClip = STACK_CLIP_BEFORE_ANIM; 111 112 static final Animation sDummyAnimation = new DummyAnimation(); 113 114 public AppWindowAnimator(final AppWindowToken atoken) { 115 mAppToken = atoken; 116 mService = atoken.service; 117 mAnimator = mService.mAnimator; 118 } 119 120 public void setAnimation(Animation anim, int width, int height, boolean skipFirstFrame, 121 int stackClip) { 122 if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting animation in " + mAppToken 123 + ": " + anim + " wxh=" + width + "x" + height 124 + " isVisible=" + mAppToken.isVisible()); 125 animation = anim; 126 animating = false; 127 if (!anim.isInitialized()) { 128 anim.initialize(width, height, width, height); 129 } 130 anim.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION); 131 anim.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked()); 132 int zorder = anim.getZAdjustment(); 133 int adj = 0; 134 if (zorder == Animation.ZORDER_TOP) { 135 adj = TYPE_LAYER_OFFSET; 136 } else if (zorder == Animation.ZORDER_BOTTOM) { 137 adj = -TYPE_LAYER_OFFSET; 138 } 139 140 if (animLayerAdjustment != adj) { 141 animLayerAdjustment = adj; 142 updateLayers(); 143 } 144 // Start out animation gone if window is gone, or visible if window is visible. 145 transformation.clear(); 146 transformation.setAlpha(mAppToken.isVisible() ? 1 : 0); 147 hasTransformation = true; 148 mStackClip = stackClip; 149 150 this.mSkipFirstFrame = skipFirstFrame; 151 152 if (!mAppToken.appFullscreen) { 153 anim.setBackgroundColor(0); 154 } 155 if (mClearProlongedAnimation) { 156 mProlongAnimation = PROLONG_ANIMATION_DISABLED; 157 } else { 158 mClearProlongedAnimation = true; 159 } 160 161 // Since we are finally starting our animation, we don't need the logic anymore to prevent 162 // the app from showing again if we just moved between stacks. See 163 // {@link WindowState#notifyMovedInStack}. 164 for (int i = mAppToken.allAppWindows.size() - 1; i >= 0; i--) { 165 mAppToken.allAppWindows.get(i).resetJustMovedInStack(); 166 } 167 } 168 169 public void setDummyAnimation() { 170 if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting dummy animation in " + mAppToken 171 + " isVisible=" + mAppToken.isVisible()); 172 animation = sDummyAnimation; 173 hasTransformation = true; 174 transformation.clear(); 175 transformation.setAlpha(mAppToken.isVisible() ? 1 : 0); 176 } 177 178 void setNullAnimation() { 179 animation = null; 180 usingTransferredAnimation = false; 181 } 182 183 public void clearAnimation() { 184 if (animation != null) { 185 animating = true; 186 } 187 clearThumbnail(); 188 setNullAnimation(); 189 if (mAppToken.deferClearAllDrawn) { 190 mAppToken.allDrawn = false; 191 mAppToken.deferClearAllDrawn = false; 192 } 193 mStackClip = STACK_CLIP_BEFORE_ANIM; 194 } 195 196 public boolean isAnimating() { 197 return animation != null || mAppToken.inPendingTransaction; 198 } 199 200 public void clearThumbnail() { 201 if (thumbnail != null) { 202 thumbnail.hide(); 203 mService.mWindowPlacerLocked.destroyAfterTransaction(thumbnail); 204 thumbnail = null; 205 } 206 deferThumbnailDestruction = false; 207 } 208 209 int getStackClip() { 210 return mStackClip; 211 } 212 213 void transferCurrentAnimation( 214 AppWindowAnimator toAppAnimator, WindowStateAnimator transferWinAnimator) { 215 216 if (animation != null) { 217 toAppAnimator.animation = animation; 218 toAppAnimator.animating = animating; 219 toAppAnimator.animLayerAdjustment = animLayerAdjustment; 220 setNullAnimation(); 221 animLayerAdjustment = 0; 222 toAppAnimator.updateLayers(); 223 updateLayers(); 224 toAppAnimator.usingTransferredAnimation = true; 225 } 226 if (transferWinAnimator != null) { 227 mAllAppWinAnimators.remove(transferWinAnimator); 228 toAppAnimator.mAllAppWinAnimators.add(transferWinAnimator); 229 transferWinAnimator.mAppAnimator = toAppAnimator; 230 } 231 } 232 233 void updateLayers() { 234 final int windowCount = mAppToken.allAppWindows.size(); 235 final int adj = animLayerAdjustment; 236 thumbnailLayer = -1; 237 final WallpaperController wallpaperController = mService.mWallpaperControllerLocked; 238 for (int i = 0; i < windowCount; i++) { 239 final WindowState w = mAppToken.allAppWindows.get(i); 240 final WindowStateAnimator winAnimator = w.mWinAnimator; 241 winAnimator.mAnimLayer = w.mLayer + adj; 242 if (winAnimator.mAnimLayer > thumbnailLayer) { 243 thumbnailLayer = winAnimator.mAnimLayer; 244 } 245 if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": " + winAnimator.mAnimLayer); 246 if (w == mService.mInputMethodTarget && !mService.mInputMethodTargetWaitingAnim) { 247 mService.mLayersController.setInputMethodAnimLayerAdjustment(adj); 248 } 249 wallpaperController.setAnimLayerAdjustment(w, adj); 250 } 251 } 252 253 private void stepThumbnailAnimation(long currentTime) { 254 thumbnailTransformation.clear(); 255 final long animationFrameTime = getAnimationFrameTime(thumbnailAnimation, currentTime); 256 thumbnailAnimation.getTransformation(animationFrameTime, thumbnailTransformation); 257 thumbnailTransformation.getMatrix().preTranslate(thumbnailX, thumbnailY); 258 259 ScreenRotationAnimation screenRotationAnimation = 260 mAnimator.getScreenRotationAnimationLocked(Display.DEFAULT_DISPLAY); 261 final boolean screenAnimation = screenRotationAnimation != null 262 && screenRotationAnimation.isAnimating(); 263 if (screenAnimation) { 264 thumbnailTransformation.postCompose(screenRotationAnimation.getEnterTransformation()); 265 } 266 // cache often used attributes locally 267 final float tmpFloats[] = mService.mTmpFloats; 268 thumbnailTransformation.getMatrix().getValues(tmpFloats); 269 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail, 270 "thumbnail", "POS " + tmpFloats[Matrix.MTRANS_X] 271 + ", " + tmpFloats[Matrix.MTRANS_Y]); 272 thumbnail.setPosition(tmpFloats[Matrix.MTRANS_X], tmpFloats[Matrix.MTRANS_Y]); 273 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail, 274 "thumbnail", "alpha=" + thumbnailTransformation.getAlpha() 275 + " layer=" + thumbnailLayer 276 + " matrix=[" + tmpFloats[Matrix.MSCALE_X] 277 + "," + tmpFloats[Matrix.MSKEW_Y] 278 + "][" + tmpFloats[Matrix.MSKEW_X] 279 + "," + tmpFloats[Matrix.MSCALE_Y] + "]"); 280 thumbnail.setAlpha(thumbnailTransformation.getAlpha()); 281 if (thumbnailForceAboveLayer > 0) { 282 thumbnail.setLayer(thumbnailForceAboveLayer + 1); 283 } else { 284 // The thumbnail is layered below the window immediately above this 285 // token's anim layer. 286 thumbnail.setLayer(thumbnailLayer + WindowManagerService.WINDOW_LAYER_MULTIPLIER 287 - WindowManagerService.LAYER_OFFSET_THUMBNAIL); 288 } 289 thumbnail.setMatrix(tmpFloats[Matrix.MSCALE_X], tmpFloats[Matrix.MSKEW_Y], 290 tmpFloats[Matrix.MSKEW_X], tmpFloats[Matrix.MSCALE_Y]); 291 thumbnail.setWindowCrop(thumbnailTransformation.getClipRect()); 292 } 293 294 /** 295 * Sometimes we need to synchronize the first frame of animation with some external event, e.g. 296 * Recents hiding some of its content. To achieve this, we prolong the start of the animaiton 297 * and keep producing the first frame of the animation. 298 */ 299 private long getAnimationFrameTime(Animation animation, long currentTime) { 300 if (mProlongAnimation == PROLONG_ANIMATION_AT_START) { 301 animation.setStartTime(currentTime); 302 return currentTime + 1; 303 } 304 return currentTime; 305 } 306 307 private boolean stepAnimation(long currentTime) { 308 if (animation == null) { 309 return false; 310 } 311 transformation.clear(); 312 final long animationFrameTime = getAnimationFrameTime(animation, currentTime); 313 boolean hasMoreFrames = animation.getTransformation(animationFrameTime, transformation); 314 if (!hasMoreFrames) { 315 if (deferThumbnailDestruction && !deferFinalFrameCleanup) { 316 // We are deferring the thumbnail destruction, so extend the animation for one more 317 // (dummy) frame before we clean up 318 deferFinalFrameCleanup = true; 319 hasMoreFrames = true; 320 } else { 321 if (false && DEBUG_ANIM) Slog.v(TAG, 322 "Stepped animation in " + mAppToken + ": more=" + hasMoreFrames + 323 ", xform=" + transformation + ", mProlongAnimation=" + mProlongAnimation); 324 deferFinalFrameCleanup = false; 325 if (mProlongAnimation == PROLONG_ANIMATION_AT_END) { 326 hasMoreFrames = true; 327 } else { 328 setNullAnimation(); 329 clearThumbnail(); 330 if (DEBUG_ANIM) Slog.v(TAG, "Finished animation in " + mAppToken + " @ " 331 + currentTime); 332 } 333 } 334 } 335 hasTransformation = hasMoreFrames; 336 return hasMoreFrames; 337 } 338 339 private long getStartTimeCorrection() { 340 if (mSkipFirstFrame) { 341 342 // If the transition is an animation in which the first frame doesn't change the screen 343 // contents at all, we can just skip it and start at the second frame. So we shift the 344 // start time of the animation forward by minus the frame duration. 345 return -Choreographer.getInstance().getFrameIntervalNanos() / TimeUtils.NANOS_PER_MS; 346 } else { 347 return 0; 348 } 349 } 350 351 // This must be called while inside a transaction. 352 boolean stepAnimationLocked(long currentTime, final int displayId) { 353 if (mService.okToDisplay()) { 354 // We will run animations as long as the display isn't frozen. 355 356 if (animation == sDummyAnimation) { 357 // This guy is going to animate, but not yet. For now count 358 // it as not animating for purposes of scheduling transactions; 359 // when it is really time to animate, this will be set to 360 // a real animation and the next call will execute normally. 361 return false; 362 } 363 364 if ((mAppToken.allDrawn || animating || mAppToken.startingDisplayed) 365 && animation != null) { 366 if (!animating) { 367 if (DEBUG_ANIM) Slog.v(TAG, 368 "Starting animation in " + mAppToken + 369 " @ " + currentTime + " scale=" 370 + mService.getTransitionAnimationScaleLocked() 371 + " allDrawn=" + mAppToken.allDrawn + " animating=" + animating); 372 long correction = getStartTimeCorrection(); 373 animation.setStartTime(currentTime + correction); 374 animating = true; 375 if (thumbnail != null) { 376 thumbnail.show(); 377 thumbnailAnimation.setStartTime(currentTime + correction); 378 } 379 mSkipFirstFrame = false; 380 } 381 if (stepAnimation(currentTime)) { 382 // animation isn't over, step any thumbnail and that's 383 // it for now. 384 if (thumbnail != null) { 385 stepThumbnailAnimation(currentTime); 386 } 387 return true; 388 } 389 } 390 } else if (animation != null) { 391 // If the display is frozen, and there is a pending animation, 392 // clear it and make sure we run the cleanup code. 393 animating = true; 394 animation = null; 395 } 396 397 hasTransformation = false; 398 399 if (!animating && animation == null) { 400 return false; 401 } 402 403 mAnimator.setAppLayoutChanges(this, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM, 404 "AppWindowToken", displayId); 405 406 clearAnimation(); 407 animating = false; 408 if (animLayerAdjustment != 0) { 409 animLayerAdjustment = 0; 410 updateLayers(); 411 } 412 if (mService.mInputMethodTarget != null 413 && mService.mInputMethodTarget.mAppToken == mAppToken) { 414 mService.moveInputMethodWindowsIfNeededLocked(true); 415 } 416 417 if (DEBUG_ANIM) Slog.v(TAG, 418 "Animation done in " + mAppToken 419 + ": reportedVisible=" + mAppToken.reportedVisible); 420 421 transformation.clear(); 422 423 final int numAllAppWinAnimators = mAllAppWinAnimators.size(); 424 for (int i = 0; i < numAllAppWinAnimators; i++) { 425 mAllAppWinAnimators.get(i).finishExit(); 426 } 427 mService.mAppTransition.notifyAppTransitionFinishedLocked(mAppToken.token); 428 return false; 429 } 430 431 // This must be called while inside a transaction. 432 boolean showAllWindowsLocked() { 433 boolean isAnimating = false; 434 final int NW = mAllAppWinAnimators.size(); 435 for (int i=0; i<NW; i++) { 436 WindowStateAnimator winAnimator = mAllAppWinAnimators.get(i); 437 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + winAnimator); 438 winAnimator.performShowLocked(); 439 isAnimating |= winAnimator.isAnimating(); 440 } 441 return isAnimating; 442 } 443 444 void dump(PrintWriter pw, String prefix, boolean dumpAll) { 445 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken); 446 pw.print(prefix); pw.print("mAnimator="); pw.println(mAnimator); 447 pw.print(prefix); pw.print("freezingScreen="); pw.print(freezingScreen); 448 pw.print(" allDrawn="); pw.print(allDrawn); 449 pw.print(" animLayerAdjustment="); pw.println(animLayerAdjustment); 450 if (lastFreezeDuration != 0) { 451 pw.print(prefix); pw.print("lastFreezeDuration="); 452 TimeUtils.formatDuration(lastFreezeDuration, pw); pw.println(); 453 } 454 if (animating || animation != null) { 455 pw.print(prefix); pw.print("animating="); pw.println(animating); 456 pw.print(prefix); pw.print("animation="); pw.println(animation); 457 } 458 if (hasTransformation) { 459 pw.print(prefix); pw.print("XForm: "); 460 transformation.printShortString(pw); 461 pw.println(); 462 } 463 if (thumbnail != null) { 464 pw.print(prefix); pw.print("thumbnail="); pw.print(thumbnail); 465 pw.print(" x="); pw.print(thumbnailX); 466 pw.print(" y="); pw.print(thumbnailY); 467 pw.print(" layer="); pw.println(thumbnailLayer); 468 pw.print(prefix); pw.print("thumbnailAnimation="); pw.println(thumbnailAnimation); 469 pw.print(prefix); pw.print("thumbnailTransformation="); 470 pw.println(thumbnailTransformation.toShortString()); 471 } 472 for (int i=0; i<mAllAppWinAnimators.size(); i++) { 473 WindowStateAnimator wanim = mAllAppWinAnimators.get(i); 474 pw.print(prefix); pw.print("App Win Anim #"); pw.print(i); 475 pw.print(": "); pw.println(wanim); 476 } 477 } 478 479 void startProlongAnimation(int prolongType) { 480 mProlongAnimation = prolongType; 481 mClearProlongedAnimation = false; 482 } 483 484 void endProlongedAnimation() { 485 mProlongAnimation = PROLONG_ANIMATION_DISABLED; 486 } 487 488 // This is an animation that does nothing: it just immediately finishes 489 // itself every time it is called. It is used as a stub animation in cases 490 // where we want to synchronize multiple things that may be animating. 491 static final class DummyAnimation extends Animation { 492 @Override 493 public boolean getTransformation(long currentTime, Transformation outTransformation) { 494 return false; 495 } 496 } 497 498} 499