WindowStateAnimator.java revision 1ca6a33f36357281b3445e85d9e67cacd1a12ede
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 android.app.ActivityManager.StackId; 20import static android.view.Display.DEFAULT_DISPLAY; 21import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; 22import static android.view.WindowManager.LayoutParams.FLAG_SCALED; 23import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; 24import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; 25import static com.android.server.wm.AppWindowAnimator.sDummyAnimation; 26import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM; 27import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE; 28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; 29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS; 30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS; 31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION; 32import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW; 33import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE; 34import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY; 35import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER; 36import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_CROP; 37import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS; 38import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC; 39import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; 40import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; 41import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; 42import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER; 43import static com.android.server.wm.WindowManagerService.localLOGV; 44import static com.android.server.wm.WindowManagerService.logWithStack; 45import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE; 46import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN; 47 48import android.content.Context; 49import android.graphics.Matrix; 50import android.graphics.PixelFormat; 51import android.graphics.Point; 52import android.graphics.Rect; 53import android.graphics.Region; 54import android.os.Debug; 55import android.os.RemoteException; 56import android.util.Slog; 57import android.view.DisplayInfo; 58import android.view.MagnificationSpec; 59import android.view.Surface.OutOfResourcesException; 60import android.view.SurfaceControl; 61import android.view.WindowManager; 62import android.view.WindowManager.LayoutParams; 63import android.view.WindowManagerPolicy; 64import android.view.animation.Animation; 65import android.view.animation.AnimationSet; 66import android.view.animation.AnimationUtils; 67import android.view.animation.Transformation; 68 69import com.android.server.wm.WindowManagerService.H; 70 71import java.io.PrintWriter; 72 73/** 74 * Keep track of animations and surface operations for a single WindowState. 75 **/ 76class WindowStateAnimator { 77 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowStateAnimator" : TAG_WM; 78 static final int WINDOW_FREEZE_LAYER = TYPE_LAYER_MULTIPLIER * 200; 79 80 /** 81 * Mode how the window gets clipped by the stack bounds during an animation: The clipping should 82 * be applied after applying the animation transformation, i.e. the stack bounds don't move 83 * during the animation. 84 */ 85 static final int STACK_CLIP_AFTER_ANIM = 0; 86 87 /** 88 * Mode how the window gets clipped by the stack bounds: The clipping should be applied before 89 * applying the animation transformation, i.e. the stack bounds move with the window. 90 */ 91 static final int STACK_CLIP_BEFORE_ANIM = 1; 92 93 /** 94 * Mode how window gets clipped by the stack bounds during an animation: Don't clip the window 95 * by the stack bounds. 96 */ 97 static final int STACK_CLIP_NONE = 2; 98 99 // Unchanging local convenience fields. 100 final WindowManagerService mService; 101 final WindowState mWin; 102 final WindowStateAnimator mAttachedWinAnimator; 103 final WindowAnimator mAnimator; 104 AppWindowAnimator mAppAnimator; 105 final Session mSession; 106 final WindowManagerPolicy mPolicy; 107 final Context mContext; 108 final boolean mIsWallpaper; 109 final WallpaperController mWallpaperControllerLocked; 110 111 // Currently running animation. 112 boolean mAnimating; 113 boolean mLocalAnimating; 114 Animation mAnimation; 115 boolean mAnimationIsEntrance; 116 boolean mHasTransformation; 117 boolean mHasLocalTransformation; 118 final Transformation mTransformation = new Transformation(); 119 boolean mWasAnimating; // Were we animating going into the most recent animation step? 120 int mAnimLayer; 121 int mLastLayer; 122 long mAnimationStartTime; 123 long mLastAnimationTime; 124 int mStackClip = STACK_CLIP_BEFORE_ANIM; 125 126 /** 127 * Set when we have changed the size of the surface, to know that 128 * we must tell them application to resize (and thus redraw itself). 129 */ 130 boolean mSurfaceResized; 131 /** 132 * Whether we should inform the client on next relayoutWindow that 133 * the surface has been resized since last time. 134 */ 135 boolean mReportSurfaceResized; 136 WindowSurfaceController mSurfaceController; 137 private WindowSurfaceController mPendingDestroySurface; 138 139 /** 140 * Set if the client has asked that the destroy of its surface be delayed 141 * until it explicitly says it is okay. 142 */ 143 boolean mSurfaceDestroyDeferred; 144 145 private boolean mDestroyPreservedSurfaceUponRedraw; 146 float mShownAlpha = 0; 147 float mAlpha = 0; 148 float mLastAlpha = 0; 149 150 boolean mHasClipRect; 151 Rect mClipRect = new Rect(); 152 Rect mTmpClipRect = new Rect(); 153 Rect mTmpFinalClipRect = new Rect(); 154 Rect mLastClipRect = new Rect(); 155 Rect mLastFinalClipRect = new Rect(); 156 Rect mTmpStackBounds = new Rect(); 157 158 /** 159 * This is rectangle of the window's surface that is not covered by 160 * system decorations. 161 */ 162 private final Rect mSystemDecorRect = new Rect(); 163 private final Rect mLastSystemDecorRect = new Rect(); 164 165 // Used to save animation distances between the time they are calculated and when they are used. 166 private int mAnimDx; 167 private int mAnimDy; 168 169 /** Is the next animation to be started a window move animation? */ 170 private boolean mAnimateMove = false; 171 172 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1; 173 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1; 174 175 boolean mHaveMatrix; 176 177 // Set to true if, when the window gets displayed, it should perform 178 // an enter animation. 179 boolean mEnterAnimationPending; 180 181 /** Used to indicate that this window is undergoing an enter animation. Used for system 182 * windows to make the callback to View.dispatchOnWindowShownCallback(). Set when the 183 * window is first added or shown, cleared when the callback has been made. */ 184 boolean mEnteringAnimation; 185 186 private boolean mAnimationStartDelayed; 187 188 boolean mKeyguardGoingAwayAnimation; 189 boolean mKeyguardGoingAwayWithWallpaper; 190 191 /** The pixel format of the underlying SurfaceControl */ 192 int mSurfaceFormat; 193 194 /** This is set when there is no Surface */ 195 static final int NO_SURFACE = 0; 196 /** This is set after the Surface has been created but before the window has been drawn. During 197 * this time the surface is hidden. */ 198 static final int DRAW_PENDING = 1; 199 /** This is set after the window has finished drawing for the first time but before its surface 200 * is shown. The surface will be displayed when the next layout is run. */ 201 static final int COMMIT_DRAW_PENDING = 2; 202 /** This is set during the time after the window's drawing has been committed, and before its 203 * surface is actually shown. It is used to delay showing the surface until all windows in a 204 * token are ready to be shown. */ 205 static final int READY_TO_SHOW = 3; 206 /** Set when the window has been shown in the screen the first time. */ 207 static final int HAS_DRAWN = 4; 208 209 String drawStateToString() { 210 switch (mDrawState) { 211 case NO_SURFACE: return "NO_SURFACE"; 212 case DRAW_PENDING: return "DRAW_PENDING"; 213 case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING"; 214 case READY_TO_SHOW: return "READY_TO_SHOW"; 215 case HAS_DRAWN: return "HAS_DRAWN"; 216 default: return Integer.toString(mDrawState); 217 } 218 } 219 int mDrawState; 220 221 /** Was this window last hidden? */ 222 boolean mLastHidden; 223 224 int mAttrType; 225 226 static final long PENDING_TRANSACTION_FINISH_WAIT_TIME = 100; 227 long mDeferTransactionUntilFrame = -1; 228 long mDeferTransactionTime = -1; 229 230 private final Rect mTmpSize = new Rect(); 231 232 WindowStateAnimator(final WindowState win) { 233 final WindowManagerService service = win.mService; 234 235 mService = service; 236 mAnimator = service.mAnimator; 237 mPolicy = service.mPolicy; 238 mContext = service.mContext; 239 final DisplayContent displayContent = win.getDisplayContent(); 240 if (displayContent != null) { 241 final DisplayInfo displayInfo = displayContent.getDisplayInfo(); 242 mAnimDx = displayInfo.appWidth; 243 mAnimDy = displayInfo.appHeight; 244 } else { 245 Slog.w(TAG, "WindowStateAnimator ctor: Display has been removed"); 246 // This is checked on return and dealt with. 247 } 248 249 mWin = win; 250 mAttachedWinAnimator = win.mAttachedWindow == null 251 ? null : win.mAttachedWindow.mWinAnimator; 252 mAppAnimator = win.mAppToken == null ? null : win.mAppToken.mAppAnimator; 253 mSession = win.mSession; 254 mAttrType = win.mAttrs.type; 255 mIsWallpaper = win.mIsWallpaper; 256 mWallpaperControllerLocked = mService.mWallpaperControllerLocked; 257 } 258 259 public void setAnimation(Animation anim, long startTime, int stackClip) { 260 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim); 261 mAnimating = false; 262 mLocalAnimating = false; 263 mAnimation = anim; 264 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION); 265 mAnimation.scaleCurrentDuration(mService.getWindowAnimationScaleLocked()); 266 // Start out animation gone if window is gone, or visible if window is visible. 267 mTransformation.clear(); 268 mTransformation.setAlpha(mLastHidden ? 0 : 1); 269 mHasLocalTransformation = true; 270 mAnimationStartTime = startTime; 271 mStackClip = stackClip; 272 } 273 274 public void setAnimation(Animation anim, int stackClip) { 275 setAnimation(anim, -1, stackClip); 276 } 277 278 public void setAnimation(Animation anim) { 279 setAnimation(anim, -1, STACK_CLIP_AFTER_ANIM); 280 } 281 282 public void clearAnimation() { 283 if (mAnimation != null) { 284 mAnimating = true; 285 mLocalAnimating = false; 286 mAnimation.cancel(); 287 mAnimation = null; 288 mKeyguardGoingAwayAnimation = false; 289 mKeyguardGoingAwayWithWallpaper = false; 290 mStackClip = STACK_CLIP_BEFORE_ANIM; 291 } 292 } 293 294 /** Is the window or its container currently animating? */ 295 boolean isAnimating() { 296 return mAnimation != null 297 || (mAttachedWinAnimator != null && mAttachedWinAnimator.mAnimation != null) 298 || (mAppAnimator != null && mAppAnimator.isAnimating()); 299 } 300 301 /** Is the window animating the DummyAnimation? */ 302 boolean isDummyAnimation() { 303 return mAppAnimator != null 304 && mAppAnimator.animation == sDummyAnimation; 305 } 306 307 /** Is this window currently set to animate or currently animating? */ 308 boolean isWindowAnimating() { 309 return mAnimation != null; 310 } 311 312 void cancelExitAnimationForNextAnimationLocked() { 313 if (DEBUG_ANIM) Slog.d(TAG, 314 "cancelExitAnimationForNextAnimationLocked: " + mWin); 315 316 if (mAnimation != null) { 317 mAnimation.cancel(); 318 mAnimation = null; 319 mLocalAnimating = false; 320 mWin.destroyOrSaveSurface(); 321 } 322 } 323 324 private boolean stepAnimation(long currentTime) { 325 if ((mAnimation == null) || !mLocalAnimating) { 326 return false; 327 } 328 currentTime = getAnimationFrameTime(mAnimation, currentTime); 329 mTransformation.clear(); 330 final boolean more = mAnimation.getTransformation(currentTime, mTransformation); 331 if (mAnimationStartDelayed && mAnimationIsEntrance) { 332 mTransformation.setAlpha(0f); 333 } 334 if (false && DEBUG_ANIM) Slog.v(TAG, "Stepped animation in " + this + ": more=" + more 335 + ", xform=" + mTransformation); 336 return more; 337 } 338 339 // This must be called while inside a transaction. Returns true if 340 // there is more animation to run. 341 boolean stepAnimationLocked(long currentTime) { 342 // Save the animation state as it was before this step so WindowManagerService can tell if 343 // we just started or just stopped animating by comparing mWasAnimating with isAnimating(). 344 mWasAnimating = mAnimating; 345 final DisplayContent displayContent = mWin.getDisplayContent(); 346 if (displayContent != null && mService.okToDisplay()) { 347 // We will run animations as long as the display isn't frozen. 348 349 if (mWin.isDrawnLw() && mAnimation != null) { 350 mHasTransformation = true; 351 mHasLocalTransformation = true; 352 if (!mLocalAnimating) { 353 if (DEBUG_ANIM) Slog.v( 354 TAG, "Starting animation in " + this + 355 " @ " + currentTime + ": ww=" + mWin.mFrame.width() + 356 " wh=" + mWin.mFrame.height() + 357 " dx=" + mAnimDx + " dy=" + mAnimDy + 358 " scale=" + mService.getWindowAnimationScaleLocked()); 359 final DisplayInfo displayInfo = displayContent.getDisplayInfo(); 360 if (mAnimateMove) { 361 mAnimateMove = false; 362 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(), 363 mAnimDx, mAnimDy); 364 } else { 365 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(), 366 displayInfo.appWidth, displayInfo.appHeight); 367 } 368 mAnimDx = displayInfo.appWidth; 369 mAnimDy = displayInfo.appHeight; 370 mAnimation.setStartTime(mAnimationStartTime != -1 371 ? mAnimationStartTime 372 : currentTime); 373 mLocalAnimating = true; 374 mAnimating = true; 375 } 376 if ((mAnimation != null) && mLocalAnimating) { 377 mLastAnimationTime = currentTime; 378 if (stepAnimation(currentTime)) { 379 return true; 380 } 381 } 382 if (DEBUG_ANIM) Slog.v( 383 TAG, "Finished animation in " + this + 384 " @ " + currentTime); 385 //WindowManagerService.this.dump(); 386 } 387 mHasLocalTransformation = false; 388 if ((!mLocalAnimating || mAnimationIsEntrance) && mAppAnimator != null 389 && mAppAnimator.animation != null) { 390 // When our app token is animating, we kind-of pretend like 391 // we are as well. Note the mLocalAnimating mAnimationIsEntrance 392 // part of this check means that we will only do this if 393 // our window is not currently exiting, or it is not 394 // locally animating itself. The idea being that one that 395 // is exiting and doing a local animation should be removed 396 // once that animation is done. 397 mAnimating = true; 398 mHasTransformation = true; 399 mTransformation.clear(); 400 return false; 401 } else if (mHasTransformation) { 402 // Little trick to get through the path below to act like 403 // we have finished an animation. 404 mAnimating = true; 405 } else if (isAnimating()) { 406 mAnimating = true; 407 } 408 } else if (mAnimation != null) { 409 // If the display is frozen, and there is a pending animation, 410 // clear it and make sure we run the cleanup code. 411 mAnimating = true; 412 } 413 414 if (!mAnimating && !mLocalAnimating) { 415 return false; 416 } 417 418 // Done animating, clean up. 419 if (DEBUG_ANIM) Slog.v( 420 TAG, "Animation done in " + this + ": exiting=" + mWin.mAnimatingExit 421 + ", reportedVisible=" 422 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false)); 423 424 mAnimating = false; 425 mKeyguardGoingAwayAnimation = false; 426 mKeyguardGoingAwayWithWallpaper = false; 427 mLocalAnimating = false; 428 if (mAnimation != null) { 429 mAnimation.cancel(); 430 mAnimation = null; 431 } 432 if (mAnimator.mWindowDetachedWallpaper == mWin) { 433 mAnimator.mWindowDetachedWallpaper = null; 434 } 435 mAnimLayer = mWin.mLayer 436 + mService.mLayersController.getSpecialWindowAnimLayerAdjustment(mWin); 437 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this + " anim layer: " + mAnimLayer); 438 mHasTransformation = false; 439 mHasLocalTransformation = false; 440 mStackClip = STACK_CLIP_BEFORE_ANIM; 441 mWin.checkPolicyVisibilityChange(); 442 mTransformation.clear(); 443 if (mDrawState == HAS_DRAWN 444 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING 445 && mWin.mAppToken != null 446 && mWin.mAppToken.firstWindowDrawn 447 && mWin.mAppToken.startingData != null) { 448 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting " 449 + mWin.mToken + ": first real window done animating"); 450 mService.mFinishedStarting.add(mWin.mAppToken); 451 mService.mH.sendEmptyMessage(H.FINISHED_STARTING); 452 } else if (mAttrType == LayoutParams.TYPE_STATUS_BAR && mWin.mPolicyVisibility) { 453 // Upon completion of a not-visible to visible status bar animation a relayout is 454 // required. 455 if (displayContent != null) { 456 displayContent.layoutNeeded = true; 457 } 458 } 459 460 finishExit(); 461 final int displayId = mWin.getDisplayId(); 462 mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM); 463 if (DEBUG_LAYOUT_REPEATS) 464 mService.mWindowPlacerLocked.debugLayoutRepeats( 465 "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId)); 466 467 if (mWin.mAppToken != null) { 468 mWin.mAppToken.updateReportedVisibilityLocked(); 469 } 470 471 return false; 472 } 473 474 void finishExit() { 475 if (DEBUG_ANIM) Slog.v( 476 TAG, "finishExit in " + this 477 + ": exiting=" + mWin.mAnimatingExit 478 + " remove=" + mWin.mRemoveOnExit 479 + " windowAnimating=" + isWindowAnimating()); 480 481 if (!mWin.mChildWindows.isEmpty()) { 482 // Copying to a different list as multiple children can be removed. 483 final WindowList childWindows = new WindowList(mWin.mChildWindows); 484 for (int i = childWindows.size() - 1; i >= 0; i--) { 485 childWindows.get(i).mWinAnimator.finishExit(); 486 } 487 } 488 489 if (mEnteringAnimation) { 490 mEnteringAnimation = false; 491 mService.requestTraversal(); 492 // System windows don't have an activity and an app token as a result, but need a way 493 // to be informed about their entrance animation end. 494 if (mWin.mAppToken == null) { 495 try { 496 mWin.mClient.dispatchWindowShown(); 497 } catch (RemoteException e) { 498 } 499 } 500 } 501 502 if (!isWindowAnimating()) { 503 //TODO (multidisplay): Accessibility is supported only for the default display. 504 if (mService.mAccessibilityController != null 505 && mWin.getDisplayId() == DEFAULT_DISPLAY) { 506 mService.mAccessibilityController.onSomeWindowResizedOrMovedLocked(); 507 } 508 } 509 510 if (!mWin.mAnimatingExit) { 511 return; 512 } 513 514 if (isWindowAnimating()) { 515 return; 516 } 517 518 if (WindowManagerService.localLOGV || DEBUG_ADD_REMOVE) Slog.v(TAG, 519 "Exit animation finished in " + this + ": remove=" + mWin.mRemoveOnExit); 520 521 522 mWin.mDestroying = true; 523 524 final boolean hasSurface = hasSurface(); 525 if (hasSurface) { 526 hide("finishExit"); 527 } 528 529 // If we have an app token, we ask it to destroy the surface for us, 530 // so that it can take care to ensure the activity has actually stopped 531 // and the surface is not still in use. Otherwise we add the service to 532 // mDestroySurface and allow it to be processed in our next transaction. 533 if (mWin.mAppToken != null) { 534 mWin.mAppToken.destroySurfaces(); 535 } else { 536 if (hasSurface) { 537 mService.mDestroySurface.add(mWin); 538 } 539 if (mWin.mRemoveOnExit) { 540 mService.mPendingRemove.add(mWin); 541 mWin.mRemoveOnExit = false; 542 } 543 } 544 mWin.mAnimatingExit = false; 545 mWallpaperControllerLocked.hideWallpapers(mWin); 546 } 547 548 void hide(String reason) { 549 if (!mLastHidden) { 550 //dump(); 551 mLastHidden = true; 552 if (mSurfaceController != null) { 553 mSurfaceController.hideInTransaction(reason); 554 } 555 } 556 } 557 558 boolean finishDrawingLocked() { 559 final boolean startingWindow = 560 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; 561 if (DEBUG_STARTING_WINDOW && startingWindow) { 562 Slog.v(TAG, "Finishing drawing window " + mWin + ": mDrawState=" 563 + drawStateToString()); 564 } 565 566 if (mWin.mAppToken != null && mWin.mAppToken.mAnimatingWithSavedSurface) { 567 // App has drawn something to its windows, we're no longer animating with 568 // the saved surfaces. If the user exits now, we only want to save again 569 // if allDrawn is true. 570 if (DEBUG_ANIM) Slog.d(TAG, 571 "finishDrawingLocked: mAnimatingWithSavedSurface=false " + mWin); 572 mWin.mAppToken.mAnimatingWithSavedSurface = false; 573 } 574 if (mDrawState == DRAW_PENDING) { 575 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION) 576 Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + mWin + " in " 577 + mSurfaceController); 578 if (DEBUG_STARTING_WINDOW && startingWindow) { 579 Slog.v(TAG, "Draw state now committed in " + mWin); 580 } 581 mDrawState = COMMIT_DRAW_PENDING; 582 return true; 583 } 584 585 return false; 586 } 587 588 // This must be called while inside a transaction. 589 boolean commitFinishDrawingLocked() { 590 if (DEBUG_STARTING_WINDOW && 591 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) { 592 Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState=" 593 + drawStateToString()); 594 } 595 if (mDrawState != COMMIT_DRAW_PENDING && mDrawState != READY_TO_SHOW) { 596 return false; 597 } 598 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) { 599 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurfaceController); 600 } 601 mDrawState = READY_TO_SHOW; 602 boolean result = false; 603 final AppWindowToken atoken = mWin.mAppToken; 604 if (atoken == null || atoken.allDrawn || mWin.mAttrs.type == TYPE_APPLICATION_STARTING) { 605 result = performShowLocked(); 606 } 607 return result; 608 } 609 610 void preserveSurfaceLocked() { 611 if (mDestroyPreservedSurfaceUponRedraw) { 612 // This could happen when switching the surface mode very fast. For example, 613 // we preserved a surface when dragResizing changed to true. Then before the 614 // preserved surface is removed, dragResizing changed to false again. 615 // In this case, we need to leave the preserved surface alone, and destroy 616 // the actual surface, so that the createSurface call could create a surface 617 // of the proper size. The preserved surface will still be removed when client 618 // finishes drawing to the new surface. 619 mSurfaceDestroyDeferred = false; 620 destroySurfaceLocked(); 621 mSurfaceDestroyDeferred = true; 622 return; 623 } 624 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "SET FREEZE LAYER", false); 625 if (mSurfaceController != null) { 626 mSurfaceController.setLayer(mAnimLayer + 1); 627 } 628 mDestroyPreservedSurfaceUponRedraw = true; 629 mSurfaceDestroyDeferred = true; 630 destroySurfaceLocked(); 631 } 632 633 void destroyPreservedSurfaceLocked() { 634 if (!mDestroyPreservedSurfaceUponRedraw) { 635 return; 636 } 637 destroyDeferredSurfaceLocked(); 638 mDestroyPreservedSurfaceUponRedraw = false; 639 } 640 641 WindowSurfaceController createSurfaceLocked() { 642 final WindowState w = mWin; 643 if (w.hasSavedSurface()) { 644 if (DEBUG_ANIM) Slog.i(TAG, 645 "createSurface: " + this + ": called when we had a saved surface"); 646 w.restoreSavedSurface(); 647 return mSurfaceController; 648 } 649 650 if (mSurfaceController != null) { 651 return mSurfaceController; 652 } 653 654 w.setHasSurface(false); 655 656 if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG, 657 "createSurface " + this + ": mDrawState=DRAW_PENDING"); 658 659 mDrawState = DRAW_PENDING; 660 if (w.mAppToken != null) { 661 if (w.mAppToken.mAppAnimator.animation == null) { 662 w.mAppToken.allDrawn = false; 663 w.mAppToken.deferClearAllDrawn = false; 664 } else { 665 // Currently animating, persist current state of allDrawn until animation 666 // is complete. 667 w.mAppToken.deferClearAllDrawn = true; 668 } 669 } 670 671 mService.makeWindowFreezingScreenIfNeededLocked(w); 672 673 int flags = SurfaceControl.HIDDEN; 674 final WindowManager.LayoutParams attrs = w.mAttrs; 675 676 if (mService.isSecureLocked(w)) { 677 flags |= SurfaceControl.SECURE; 678 } 679 680 mTmpSize.set(w.mFrame.left + w.mXOffset, w.mFrame.top + w.mYOffset, 0, 0); 681 calculateSurfaceBounds(w, attrs); 682 final int width = mTmpSize.width(); 683 final int height = mTmpSize.height(); 684 685 if (DEBUG_VISIBILITY) { 686 Slog.v(TAG, "Creating surface in session " 687 + mSession.mSurfaceSession + " window " + this 688 + " w=" + width + " h=" + height 689 + " x=" + mTmpSize.left + " y=" + mTmpSize.top 690 + " format=" + attrs.format + " flags=" + flags); 691 } 692 693 // We may abort, so initialize to defaults. 694 mLastSystemDecorRect.set(0, 0, 0, 0); 695 mHasClipRect = false; 696 mClipRect.set(0, 0, 0, 0); 697 mLastClipRect.set(0, 0, 0, 0); 698 699 // Set up surface control with initial size. 700 try { 701 702 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0; 703 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format; 704 if (!PixelFormat.formatHasAlpha(attrs.format) 705 // Don't make surface with surfaceInsets opaque as they display a 706 // translucent shadow. 707 && attrs.surfaceInsets.left == 0 708 && attrs.surfaceInsets.top == 0 709 && attrs.surfaceInsets.right == 0 710 && attrs.surfaceInsets.bottom == 0 711 // Don't make surface opaque when resizing to reduce the amount of 712 // artifacts shown in areas the app isn't drawing content to. 713 && !w.isDragResizing()) { 714 flags |= SurfaceControl.OPAQUE; 715 } 716 717 mSurfaceController = new WindowSurfaceController(mSession.mSurfaceSession, 718 attrs.getTitle().toString(), 719 width, height, format, flags, this); 720 721 w.setHasSurface(true); 722 723 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { 724 Slog.i(TAG, " CREATE SURFACE " 725 + mSurfaceController + " IN SESSION " 726 + mSession.mSurfaceSession 727 + ": pid=" + mSession.mPid + " format=" 728 + attrs.format + " flags=0x" 729 + Integer.toHexString(flags) 730 + " / " + this); 731 } 732 } catch (OutOfResourcesException e) { 733 Slog.w(TAG, "OutOfResourcesException creating surface"); 734 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true); 735 mDrawState = NO_SURFACE; 736 return null; 737 } catch (Exception e) { 738 Slog.e(TAG, "Exception creating surface", e); 739 mDrawState = NO_SURFACE; 740 return null; 741 } 742 743 if (WindowManagerService.localLOGV) Slog.v(TAG, "Got surface: " + mSurfaceController 744 + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top 745 + ", animLayer=" + mAnimLayer); 746 747 if (SHOW_LIGHT_TRANSACTIONS) { 748 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked"); 749 WindowManagerService.logSurface(w, "CREATE pos=(" 750 + w.mFrame.left + "," + w.mFrame.top + ") (" 751 + width + "x" + height + "), layer=" + mAnimLayer + " HIDE", false); 752 } 753 754 // Start a new transaction and apply position & offset. 755 final int layerStack = w.getDisplayContent().getDisplay().getLayerStack(); 756 mSurfaceController.setPositionAndLayer(mTmpSize.left, mTmpSize.top, layerStack, mAnimLayer); 757 mLastHidden = true; 758 759 if (WindowManagerService.localLOGV) Slog.v(TAG, "Created surface " + this); 760 return mSurfaceController; 761 } 762 763 private void calculateSurfaceBounds(WindowState w, LayoutParams attrs) { 764 if ((attrs.flags & FLAG_SCALED) != 0) { 765 // For a scaled surface, we always want the requested size. 766 mTmpSize.right = mTmpSize.left + w.mRequestedWidth; 767 mTmpSize.bottom = mTmpSize.top + w.mRequestedHeight; 768 } else { 769 // When we're doing a drag-resizing, request a surface that's fullscreen size, 770 // so that we don't need to reallocate during the process. This also prevents 771 // buffer drops due to size mismatch. 772 if (w.isDragResizing()) { 773 if (w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM) { 774 mTmpSize.left = 0; 775 mTmpSize.top = 0; 776 } 777 final DisplayInfo displayInfo = w.getDisplayInfo(); 778 mTmpSize.right = mTmpSize.left + displayInfo.logicalWidth; 779 mTmpSize.bottom = mTmpSize.top + displayInfo.logicalHeight; 780 } else { 781 mTmpSize.right = mTmpSize.left + w.mCompatFrame.width(); 782 mTmpSize.bottom = mTmpSize.top + w.mCompatFrame.height(); 783 } 784 } 785 786 // Something is wrong and SurfaceFlinger will not like this, try to revert to sane values. 787 // This doesn't necessarily mean that there is an error in the system. The sizes might be 788 // incorrect, because it is before the first layout or draw. 789 if (mTmpSize.width() < 1) { 790 mTmpSize.right = mTmpSize.left + 1; 791 } 792 if (mTmpSize.height() < 1) { 793 mTmpSize.bottom = mTmpSize.top + 1; 794 } 795 796 final int displayId = w.getDisplayId(); 797 float scale = 1.0f; 798 // Magnification is supported only for the default display. 799 if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) { 800 final MagnificationSpec spec = 801 mService.mAccessibilityController.getMagnificationSpecForWindowLocked(w); 802 if (spec != null && !spec.isNop()) { 803 scale = spec.scale; 804 } 805 } 806 807 // Adjust for surface insets. 808 mTmpSize.left -= scale * attrs.surfaceInsets.left; 809 mTmpSize.top -= scale * attrs.surfaceInsets.top; 810 mTmpSize.right += scale * (attrs.surfaceInsets.left + attrs.surfaceInsets.right); 811 mTmpSize.bottom += scale * (attrs.surfaceInsets.top + attrs.surfaceInsets.bottom); 812 } 813 814 boolean hasSurface() { 815 return !mWin.mSurfaceSaved 816 && mSurfaceController != null && mSurfaceController.hasSurface(); 817 } 818 819 void destroySurfaceLocked() { 820 final AppWindowToken wtoken = mWin.mAppToken; 821 if (wtoken != null) { 822 wtoken.mAnimatingWithSavedSurface = false; 823 if (mWin == wtoken.startingWindow) { 824 wtoken.startingDisplayed = false; 825 } 826 } 827 828 mWin.mSurfaceSaved = false; 829 830 if (mSurfaceController == null) { 831 return; 832 } 833 834 int i = mWin.mChildWindows.size(); 835 // When destroying a surface we want to make sure child windows are hidden. If we are 836 // preserving the surface until redraw though we intend to swap it out with another surface 837 // for resizing. In this case the window always remains visible to the user and the child 838 // windows should likewise remain visible. 839 while (!mDestroyPreservedSurfaceUponRedraw && i > 0) { 840 i--; 841 WindowState c = mWin.mChildWindows.get(i); 842 c.mAttachedHidden = true; 843 } 844 845 try { 846 if (DEBUG_VISIBILITY) logWithStack(TAG, "Window " + this + " destroying surface " 847 + mSurfaceController + ", session " + mSession); 848 if (mSurfaceDestroyDeferred) { 849 if (mSurfaceController != null && mPendingDestroySurface != mSurfaceController) { 850 if (mPendingDestroySurface != null) { 851 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { 852 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true); 853 } 854 mPendingDestroySurface.destroyInTransaction(); 855 } 856 mPendingDestroySurface = mSurfaceController; 857 } 858 } else { 859 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { 860 WindowManagerService.logSurface(mWin, "DESTROY", true); 861 } 862 destroySurface(); 863 } 864 // Don't hide wallpaper if we're deferring the surface destroy 865 // because of a surface change. 866 if (!mDestroyPreservedSurfaceUponRedraw) { 867 mWallpaperControllerLocked.hideWallpapers(mWin); 868 } 869 } catch (RuntimeException e) { 870 Slog.w(TAG, "Exception thrown when destroying Window " + this 871 + " surface " + mSurfaceController + " session " + mSession + ": " + e.toString()); 872 } 873 874 // Whether the surface was preserved (and copied to mPendingDestroySurface) or not, it 875 // needs to be cleared to match the WindowState.mHasSurface state. It is also necessary 876 // so it can be recreated successfully in mPendingDestroySurface case. 877 mWin.setHasSurface(false); 878 if (mSurfaceController != null) { 879 mSurfaceController.setShown(false); 880 } 881 mSurfaceController = null; 882 mDrawState = NO_SURFACE; 883 } 884 885 void destroyDeferredSurfaceLocked() { 886 try { 887 if (mPendingDestroySurface != null) { 888 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { 889 WindowManagerService.logSurface(mWin, "DESTROY PENDING", true); 890 } 891 mPendingDestroySurface.destroyInTransaction(); 892 // Don't hide wallpaper if we're destroying a deferred surface 893 // after a surface mode change. 894 if (!mDestroyPreservedSurfaceUponRedraw) { 895 mWallpaperControllerLocked.hideWallpapers(mWin); 896 } 897 } 898 } catch (RuntimeException e) { 899 Slog.w(TAG, "Exception thrown when destroying Window " 900 + this + " surface " + mPendingDestroySurface 901 + " session " + mSession + ": " + e.toString()); 902 } 903 mSurfaceDestroyDeferred = false; 904 mPendingDestroySurface = null; 905 } 906 907 void computeShownFrameLocked() { 908 final boolean selfTransformation = mHasLocalTransformation; 909 Transformation attachedTransformation = 910 (mAttachedWinAnimator != null && mAttachedWinAnimator.mHasLocalTransformation) 911 ? mAttachedWinAnimator.mTransformation : null; 912 Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation) 913 ? mAppAnimator.transformation : null; 914 915 // Wallpapers are animated based on the "real" window they 916 // are currently targeting. 917 final WindowState wallpaperTarget = mWallpaperControllerLocked.getWallpaperTarget(); 918 if (mIsWallpaper && wallpaperTarget != null && mService.mAnimateWallpaperWithTarget) { 919 final WindowStateAnimator wallpaperAnimator = wallpaperTarget.mWinAnimator; 920 if (wallpaperAnimator.mHasLocalTransformation && 921 wallpaperAnimator.mAnimation != null && 922 !wallpaperAnimator.mAnimation.getDetachWallpaper()) { 923 attachedTransformation = wallpaperAnimator.mTransformation; 924 if (DEBUG_WALLPAPER && attachedTransformation != null) { 925 Slog.v(TAG, "WP target attached xform: " + attachedTransformation); 926 } 927 } 928 final AppWindowAnimator wpAppAnimator = wallpaperTarget.mAppToken == null ? 929 null : wallpaperTarget.mAppToken.mAppAnimator; 930 if (wpAppAnimator != null && wpAppAnimator.hasTransformation 931 && wpAppAnimator.animation != null 932 && !wpAppAnimator.animation.getDetachWallpaper()) { 933 appTransformation = wpAppAnimator.transformation; 934 if (DEBUG_WALLPAPER && appTransformation != null) { 935 Slog.v(TAG, "WP target app xform: " + appTransformation); 936 } 937 } 938 } 939 940 final int displayId = mWin.getDisplayId(); 941 final ScreenRotationAnimation screenRotationAnimation = 942 mAnimator.getScreenRotationAnimationLocked(displayId); 943 final boolean screenAnimation = 944 screenRotationAnimation != null && screenRotationAnimation.isAnimating(); 945 946 mHasClipRect = false; 947 if (selfTransformation || attachedTransformation != null 948 || appTransformation != null || screenAnimation) { 949 // cache often used attributes locally 950 final Rect frame = mWin.mFrame; 951 final float tmpFloats[] = mService.mTmpFloats; 952 final Matrix tmpMatrix = mWin.mTmpMatrix; 953 954 // Compute the desired transformation. 955 if (screenAnimation && screenRotationAnimation.isRotating()) { 956 // If we are doing a screen animation, the global rotation 957 // applied to windows can result in windows that are carefully 958 // aligned with each other to slightly separate, allowing you 959 // to see what is behind them. An unsightly mess. This... 960 // thing... magically makes it call good: scale each window 961 // slightly (two pixels larger in each dimension, from the 962 // window's center). 963 final float w = frame.width(); 964 final float h = frame.height(); 965 if (w>=1 && h>=1) { 966 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2); 967 } else { 968 tmpMatrix.reset(); 969 } 970 } else { 971 tmpMatrix.reset(); 972 } 973 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale); 974 if (selfTransformation) { 975 tmpMatrix.postConcat(mTransformation.getMatrix()); 976 } 977 if (attachedTransformation != null) { 978 tmpMatrix.postConcat(attachedTransformation.getMatrix()); 979 } 980 if (appTransformation != null) { 981 tmpMatrix.postConcat(appTransformation.getMatrix()); 982 } 983 984 // The translation that applies the position of the window needs to be applied at the 985 // end in case that other translations include scaling. Otherwise the scaling will 986 // affect this translation. But it needs to be set before the screen rotation animation 987 // so the pivot point is at the center of the screen for all windows. 988 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset); 989 if (screenAnimation) { 990 tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix()); 991 } 992 993 //TODO (multidisplay): Magnification is supported only for the default display. 994 if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) { 995 MagnificationSpec spec = mService.mAccessibilityController 996 .getMagnificationSpecForWindowLocked(mWin); 997 if (spec != null && !spec.isNop()) { 998 tmpMatrix.postScale(spec.scale, spec.scale); 999 tmpMatrix.postTranslate(spec.offsetX, spec.offsetY); 1000 } 1001 } 1002 1003 // "convert" it into SurfaceFlinger's format 1004 // (a 2x2 matrix + an offset) 1005 // Here we must not transform the position of the surface 1006 // since it is already included in the transformation. 1007 //Slog.i(TAG_WM, "Transform: " + matrix); 1008 1009 mHaveMatrix = true; 1010 tmpMatrix.getValues(tmpFloats); 1011 mDsDx = tmpFloats[Matrix.MSCALE_X]; 1012 mDtDx = tmpFloats[Matrix.MSKEW_Y]; 1013 mDsDy = tmpFloats[Matrix.MSKEW_X]; 1014 mDtDy = tmpFloats[Matrix.MSCALE_Y]; 1015 float x = tmpFloats[Matrix.MTRANS_X]; 1016 float y = tmpFloats[Matrix.MTRANS_Y]; 1017 mWin.mShownPosition.set((int) x, (int) y); 1018 1019 // Now set the alpha... but because our current hardware 1020 // can't do alpha transformation on a non-opaque surface, 1021 // turn it off if we are running an animation that is also 1022 // transforming since it is more important to have that 1023 // animation be smooth. 1024 mShownAlpha = mAlpha; 1025 if (!mService.mLimitedAlphaCompositing 1026 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format) 1027 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy) 1028 && x == frame.left && y == frame.top))) { 1029 //Slog.i(TAG_WM, "Applying alpha transform"); 1030 if (selfTransformation) { 1031 mShownAlpha *= mTransformation.getAlpha(); 1032 } 1033 if (attachedTransformation != null) { 1034 mShownAlpha *= attachedTransformation.getAlpha(); 1035 } 1036 if (appTransformation != null) { 1037 mShownAlpha *= appTransformation.getAlpha(); 1038 if (appTransformation.hasClipRect()) { 1039 mClipRect.set(appTransformation.getClipRect()); 1040 mHasClipRect = true; 1041 // The app transformation clip will be in the coordinate space of the main 1042 // activity window, which the animation correctly assumes will be placed at 1043 // (0,0)+(insets) relative to the containing frame. This isn't necessarily 1044 // true for child windows though which can have an arbitrary frame position 1045 // relative to their containing frame. We need to offset the difference 1046 // between the containing frame as used to calculate the crop and our 1047 // bounds to compensate for this. 1048 if (mWin.layoutInParentFrame()) { 1049 mClipRect.offset( (mWin.mContainingFrame.left - mWin.mFrame.left), 1050 mWin.mContainingFrame.top - mWin.mFrame.top ); 1051 } 1052 } 1053 } 1054 if (screenAnimation) { 1055 mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha(); 1056 } 1057 } else { 1058 //Slog.i(TAG_WM, "Not applying alpha transform"); 1059 } 1060 1061 if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV) 1062 && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v( 1063 TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha 1064 + " self=" + (selfTransformation ? mTransformation.getAlpha() : "null") 1065 + " attached=" + (attachedTransformation == null ? 1066 "null" : attachedTransformation.getAlpha()) 1067 + " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha()) 1068 + " screen=" + (screenAnimation ? 1069 screenRotationAnimation.getEnterTransformation().getAlpha() : "null")); 1070 return; 1071 } else if (mIsWallpaper && mService.mWindowPlacerLocked.mWallpaperActionPending) { 1072 return; 1073 } else if (mWin.isDragResizeChanged()) { 1074 // This window is awaiting a relayout because user just started (or ended) 1075 // drag-resizing. The shown frame (which affects surface size and pos) 1076 // should not be updated until we get next finished draw with the new surface. 1077 // Otherwise one or two frames rendered with old settings would be displayed 1078 // with new geometry. 1079 return; 1080 } 1081 1082 if (WindowManagerService.localLOGV) Slog.v( 1083 TAG, "computeShownFrameLocked: " + this + 1084 " not attached, mAlpha=" + mAlpha); 1085 1086 MagnificationSpec spec = null; 1087 //TODO (multidisplay): Magnification is supported only for the default display. 1088 if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) { 1089 spec = mService.mAccessibilityController.getMagnificationSpecForWindowLocked(mWin); 1090 } 1091 if (spec != null) { 1092 final Rect frame = mWin.mFrame; 1093 final float tmpFloats[] = mService.mTmpFloats; 1094 final Matrix tmpMatrix = mWin.mTmpMatrix; 1095 1096 tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale); 1097 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset); 1098 1099 if (spec != null && !spec.isNop()) { 1100 tmpMatrix.postScale(spec.scale, spec.scale); 1101 tmpMatrix.postTranslate(spec.offsetX, spec.offsetY); 1102 } 1103 1104 tmpMatrix.getValues(tmpFloats); 1105 1106 mHaveMatrix = true; 1107 mDsDx = tmpFloats[Matrix.MSCALE_X]; 1108 mDtDx = tmpFloats[Matrix.MSKEW_Y]; 1109 mDsDy = tmpFloats[Matrix.MSKEW_X]; 1110 mDtDy = tmpFloats[Matrix.MSCALE_Y]; 1111 float x = tmpFloats[Matrix.MTRANS_X]; 1112 float y = tmpFloats[Matrix.MTRANS_Y]; 1113 mWin.mShownPosition.set((int) x, (int) y); 1114 1115 mShownAlpha = mAlpha; 1116 } else { 1117 mWin.mShownPosition.set(mWin.mFrame.left, mWin.mFrame.top); 1118 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) { 1119 mWin.mShownPosition.offset(mWin.mXOffset, mWin.mYOffset); 1120 } 1121 mShownAlpha = mAlpha; 1122 mHaveMatrix = false; 1123 mDsDx = mWin.mGlobalScale; 1124 mDtDx = 0; 1125 mDsDy = 0; 1126 mDtDy = mWin.mGlobalScale; 1127 } 1128 } 1129 1130 private void calculateSystemDecorRect() { 1131 final WindowState w = mWin; 1132 final Rect decorRect = w.mDecorFrame; 1133 final int width = w.mFrame.width(); 1134 final int height = w.mFrame.height(); 1135 1136 // Compute the offset of the window in relation to the decor rect. 1137 final int left = w.mXOffset + w.mFrame.left; 1138 final int top = w.mYOffset + w.mFrame.top; 1139 1140 // Initialize the decor rect to the entire frame. 1141 if (w.isDockedResizing() || 1142 (w.isChildWindow() && w.mAttachedWindow.isDockedResizing())) { 1143 1144 // If we are resizing with the divider, the task bounds might be smaller than the 1145 // stack bounds. The system decor is used to clip to the task bounds, which we don't 1146 // want in this case in order to avoid holes. 1147 // 1148 // We take care to not shrink the width, for surfaces which are larger than 1149 // the display region. Of course this area will not eventually be visible 1150 // but if we truncate the width now, we will calculate incorrectly 1151 // when adjusting to the stack bounds. 1152 final DisplayInfo displayInfo = w.getDisplayContent().getDisplayInfo(); 1153 mSystemDecorRect.set(0, 0, 1154 Math.max(width, displayInfo.logicalWidth), 1155 Math.max(height, displayInfo.logicalHeight)); 1156 } else { 1157 mSystemDecorRect.set(0, 0, width, height); 1158 } 1159 1160 // If a freeform window is animating from a position where it would be cutoff, it would be 1161 // cutoff during the animation. We don't want that, so for the duration of the animation 1162 // we ignore the decor cropping and depend on layering to position windows correctly. 1163 final boolean cropToDecor = !(w.inFreeformWorkspace() && w.isAnimatingLw()); 1164 if (cropToDecor) { 1165 // Intersect with the decor rect, offsetted by window position. 1166 mSystemDecorRect.intersect(decorRect.left - left, decorRect.top - top, 1167 decorRect.right - left, decorRect.bottom - top); 1168 } 1169 1170 // If size compatibility is being applied to the window, the 1171 // surface is scaled relative to the screen. Also apply this 1172 // scaling to the crop rect. We aren't using the standard rect 1173 // scale function because we want to round things to make the crop 1174 // always round to a larger rect to ensure we don't crop too 1175 // much and hide part of the window that should be seen. 1176 if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) { 1177 final float scale = w.mInvGlobalScale; 1178 mSystemDecorRect.left = (int) (mSystemDecorRect.left * scale - 0.5f); 1179 mSystemDecorRect.top = (int) (mSystemDecorRect.top * scale - 0.5f); 1180 mSystemDecorRect.right = (int) ((mSystemDecorRect.right + 1) * scale - 0.5f); 1181 mSystemDecorRect.bottom = (int) ((mSystemDecorRect.bottom + 1) * scale - 0.5f); 1182 } 1183 } 1184 1185 void calculateSurfaceWindowCrop(Rect clipRect, Rect finalClipRect) { 1186 final WindowState w = mWin; 1187 final DisplayContent displayContent = w.getDisplayContent(); 1188 if (displayContent == null) { 1189 clipRect.setEmpty(); 1190 finalClipRect.setEmpty(); 1191 return; 1192 } 1193 final DisplayInfo displayInfo = displayContent.getDisplayInfo(); 1194 if (DEBUG_WINDOW_CROP) Slog.d(TAG, 1195 "Updating crop win=" + w + " mLastCrop=" + mLastClipRect); 1196 1197 // Need to recompute a new system decor rect each time. 1198 if (!w.isDefaultDisplay()) { 1199 // On a different display there is no system decor. Crop the window 1200 // by the screen boundaries. 1201 mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height()); 1202 mSystemDecorRect.intersect(-w.mCompatFrame.left, -w.mCompatFrame.top, 1203 displayInfo.logicalWidth - w.mCompatFrame.left, 1204 displayInfo.logicalHeight - w.mCompatFrame.top); 1205 } else if (w.mLayer >= mService.mSystemDecorLayer) { 1206 // Above the decor layer is easy, just use the entire window. 1207 mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height()); 1208 } else if (w.mDecorFrame.isEmpty()) { 1209 // Windows without policy decor aren't cropped. 1210 mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height()); 1211 } else if (w.mAttrs.type == LayoutParams.TYPE_WALLPAPER && mAnimator.isAnimating()) { 1212 // If we're animating, the wallpaper crop should only be updated at the end of the 1213 // animation. 1214 mTmpClipRect.set(mSystemDecorRect); 1215 calculateSystemDecorRect(); 1216 mSystemDecorRect.union(mTmpClipRect); 1217 } else { 1218 // Crop to the system decor specified by policy. 1219 calculateSystemDecorRect(); 1220 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "Applying decor to crop win=" + w + " mDecorFrame=" 1221 + w.mDecorFrame + " mSystemDecorRect=" + mSystemDecorRect); 1222 } 1223 1224 final boolean fullscreen = w.isFrameFullscreen(displayInfo); 1225 final boolean isFreeformResizing = 1226 w.isDragResizing() && w.getResizeMode() == DRAG_RESIZE_MODE_FREEFORM; 1227 1228 // We use the clip rect as provided by the tranformation for non-fullscreen windows to 1229 // avoid premature clipping with the system decor rect. 1230 clipRect.set((mHasClipRect && !fullscreen) ? mClipRect : mSystemDecorRect); 1231 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "win=" + w + " Initial clip rect: " + clipRect 1232 + " mHasClipRect=" + mHasClipRect + " fullscreen=" + fullscreen); 1233 1234 if (isFreeformResizing && !w.isChildWindow()) { 1235 // For freeform resizing non child windows, we are using the big surface positioned 1236 // at 0,0. Thus we must express the crop in that coordinate space. 1237 clipRect.offset(w.mShownPosition.x, w.mShownPosition.y); 1238 } 1239 1240 // Expand the clip rect for surface insets. 1241 final WindowManager.LayoutParams attrs = w.mAttrs; 1242 clipRect.left -= attrs.surfaceInsets.left; 1243 clipRect.top -= attrs.surfaceInsets.top; 1244 clipRect.right += attrs.surfaceInsets.right; 1245 clipRect.bottom += attrs.surfaceInsets.bottom; 1246 1247 if (mHasClipRect && fullscreen) { 1248 // We intersect the clip rect specified by the transformation with the expanded system 1249 // decor rect to prevent artifacts from drawing during animation if the transformation 1250 // clip rect extends outside the system decor rect. 1251 clipRect.intersect(mClipRect); 1252 } 1253 // The clip rect was generated assuming (0,0) as the window origin, 1254 // so we need to translate to match the actual surface coordinates. 1255 clipRect.offset(attrs.surfaceInsets.left, attrs.surfaceInsets.top); 1256 1257 finalClipRect.setEmpty(); 1258 adjustCropToStackBounds(w, clipRect, finalClipRect, isFreeformResizing); 1259 if (DEBUG_WINDOW_CROP) Slog.d(TAG, 1260 "win=" + w + " Clip rect after stack adjustment=" + clipRect); 1261 1262 w.transformFromScreenToSurfaceSpace(clipRect); 1263 1264 // See {@link WindowState#notifyMovedInStack} for why this is necessary. 1265 if (w.hasJustMovedInStack() && mLastClipRect.isEmpty() && !clipRect.isEmpty()) { 1266 clipRect.setEmpty(); 1267 } 1268 } 1269 1270 void updateSurfaceWindowCrop(Rect clipRect, Rect finalClipRect, boolean recoveringMemory) { 1271 if (DEBUG_WINDOW_CROP) Slog.d(TAG, "updateSurfaceWindowCrop: win=" + mWin 1272 + " clipRect=" + clipRect + " finalClipRect=" + finalClipRect); 1273 if (!clipRect.equals(mLastClipRect)) { 1274 mLastClipRect.set(clipRect); 1275 mSurfaceController.setCropInTransaction(clipRect, recoveringMemory); 1276 } 1277 if (!finalClipRect.equals(mLastFinalClipRect)) { 1278 mLastFinalClipRect.set(finalClipRect); 1279 mSurfaceController.setFinalCropInTransaction(finalClipRect); 1280 } 1281 } 1282 1283 private int resolveStackClip() { 1284 1285 // App animation overrides window animation stack clip mode. 1286 if (mAppAnimator != null && mAppAnimator.animation != null) { 1287 return mAppAnimator.getStackClip(); 1288 } else { 1289 return mStackClip; 1290 } 1291 } 1292 private void adjustCropToStackBounds(WindowState w, Rect clipRect, Rect finalClipRect, 1293 boolean isFreeformResizing) { 1294 final Task task = w.getTask(); 1295 if (task == null || !task.cropWindowsToStackBounds()) { 1296 return; 1297 } 1298 1299 final int stackClip = resolveStackClip(); 1300 1301 // It's animating and we don't want to clip it to stack bounds during animation - abort. 1302 if (isAnimating() && stackClip == STACK_CLIP_NONE) { 1303 return; 1304 } 1305 1306 final WindowState winShowWhenLocked = (WindowState) mPolicy.getWinShowWhenLockedLw(); 1307 if (w == winShowWhenLocked && mPolicy.isKeyguardShowingOrOccluded()) { 1308 return; 1309 } 1310 1311 final TaskStack stack = task.mStack; 1312 stack.getDimBounds(mTmpStackBounds); 1313 final Rect surfaceInsets = w.getAttrs().surfaceInsets; 1314 // When we resize we use the big surface approach, which means we can't trust the 1315 // window frame bounds anymore. Instead, the window will be placed at 0, 0, but to avoid 1316 // hardcoding it, we use surface coordinates. 1317 final int frameX = isFreeformResizing ? (int) mSurfaceController.getX() : 1318 w.mFrame.left + mWin.mXOffset - surfaceInsets.left; 1319 final int frameY = isFreeformResizing ? (int) mSurfaceController.getY() : 1320 w.mFrame.top + mWin.mYOffset - surfaceInsets.top; 1321 1322 // If we are animating, we either apply the clip before applying all the animation 1323 // transformation or after all the transformation. 1324 final boolean useFinalClipRect = isAnimating() && stackClip == STACK_CLIP_AFTER_ANIM; 1325 1326 // We need to do some acrobatics with surface position, because their clip region is 1327 // relative to the inside of the surface, but the stack bounds aren't. 1328 if (useFinalClipRect) { 1329 finalClipRect.set(mTmpStackBounds); 1330 } else { 1331 if (StackId.hasWindowShadow(stack.mStackId) 1332 && !StackId.isTaskResizeAllowed(stack.mStackId)) { 1333 // The windows in this stack display drop shadows and the fill the entire stack 1334 // area. Adjust the stack bounds we will use to cropping take into account the 1335 // offsets we use to display the drop shadow so it doesn't get cropped. 1336 mTmpStackBounds.inset(-surfaceInsets.left, -surfaceInsets.top, 1337 -surfaceInsets.right, -surfaceInsets.bottom); 1338 } 1339 1340 clipRect.left = Math.max(0, 1341 Math.max(mTmpStackBounds.left, frameX + clipRect.left) - frameX); 1342 clipRect.top = Math.max(0, 1343 Math.max(mTmpStackBounds.top, frameY + clipRect.top) - frameY); 1344 clipRect.right = Math.max(0, 1345 Math.min(mTmpStackBounds.right, frameX + clipRect.right) - frameX); 1346 clipRect.bottom = Math.max(0, 1347 Math.min(mTmpStackBounds.bottom, frameY + clipRect.bottom) - frameY); 1348 } 1349 } 1350 1351 void setSurfaceBoundariesLocked(final boolean recoveringMemory) { 1352 final WindowState w = mWin; 1353 final Task task = w.getTask(); 1354 1355 // We got resized, so block all updates until we got the new surface. 1356 if (w.mResizedWhileNotDragResizing) { 1357 return; 1358 } 1359 1360 mTmpSize.set(w.mShownPosition.x, w.mShownPosition.y, 0, 0); 1361 calculateSurfaceBounds(w, w.getAttrs()); 1362 1363 float extraHScale = (float) 1.0; 1364 float extraVScale = (float) 1.0; 1365 1366 calculateSurfaceWindowCrop(mTmpClipRect, mTmpFinalClipRect); 1367 if (task != null && task.mStack.getForceScaleToCrop()) { 1368 extraHScale = mTmpClipRect.width() / (float)mTmpSize.width(); 1369 extraVScale = mTmpClipRect.height() / (float)mTmpSize.height(); 1370 1371 // In the case of ForceScaleToCrop we scale entire tasks together, 1372 // and so we need to scale our offsets relative to the task bounds 1373 // or parent and child windows would fall out of alignment. 1374 int posX = (int) (mTmpSize.left - w.mAttrs.x * (1 - extraHScale)); 1375 int posY = (int) (mTmpSize.top - w.mAttrs.y * (1 - extraVScale)); 1376 posX += w.getAttrs().surfaceInsets.left * (1 - extraHScale); 1377 posY += w.getAttrs().surfaceInsets.top * (1 - extraVScale); 1378 mSurfaceController.setPositionInTransaction(posX, posY, recoveringMemory); 1379 1380 // Since we are scaled to fit in our previously desired crop, we can now 1381 // expose the whole window in buffer space, and not risk extending 1382 // past where the system would have cropped us 1383 mTmpClipRect.set(0, 0, mTmpSize.width(), mTmpSize.height()); 1384 mTmpFinalClipRect.setEmpty(); 1385 } else { 1386 mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top, 1387 recoveringMemory); 1388 } 1389 1390 updateSurfaceWindowCrop(mTmpClipRect, mTmpFinalClipRect, recoveringMemory); 1391 1392 mSurfaceController.setMatrixInTransaction(mDsDx * w.mHScale * extraHScale, 1393 mDtDx * w.mVScale * extraVScale, 1394 mDsDy * w.mHScale * extraHScale, 1395 mDtDy * w.mVScale * extraVScale, recoveringMemory); 1396 mSurfaceResized = mSurfaceController.setSizeInTransaction( 1397 mTmpSize.width(), mTmpSize.height(), recoveringMemory); 1398 1399 if (mSurfaceResized) { 1400 mReportSurfaceResized = true; 1401 mAnimator.setPendingLayoutChanges(w.getDisplayId(), 1402 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER); 1403 w.applyDimLayerIfNeeded(); 1404 } 1405 1406 } 1407 1408 void prepareSurfaceLocked(final boolean recoveringMemory) { 1409 final WindowState w = mWin; 1410 if (!hasSurface()) { 1411 if (w.mOrientationChanging) { 1412 if (DEBUG_ORIENTATION) { 1413 Slog.v(TAG, "Orientation change skips hidden " + w); 1414 } 1415 w.mOrientationChanging = false; 1416 } 1417 return; 1418 } 1419 1420 boolean displayed = false; 1421 1422 computeShownFrameLocked(); 1423 1424 setSurfaceBoundariesLocked(recoveringMemory); 1425 1426 if (mIsWallpaper && !mWin.mWallpaperVisible) { 1427 // Wallpaper is no longer visible and there is no wp target => hide it. 1428 hide("prepareSurfaceLocked"); 1429 } else if (w.mAttachedHidden || !w.isOnScreen()) { 1430 hide("prepareSurfaceLocked"); 1431 mWallpaperControllerLocked.hideWallpapers(w); 1432 1433 // If we are waiting for this window to handle an 1434 // orientation change, well, it is hidden, so 1435 // doesn't really matter. Note that this does 1436 // introduce a potential glitch if the window 1437 // becomes unhidden before it has drawn for the 1438 // new orientation. 1439 if (w.mOrientationChanging) { 1440 w.mOrientationChanging = false; 1441 if (DEBUG_ORIENTATION) Slog.v(TAG, 1442 "Orientation change skips hidden " + w); 1443 } 1444 } else if (mLastLayer != mAnimLayer 1445 || mLastAlpha != mShownAlpha 1446 || mLastDsDx != mDsDx 1447 || mLastDtDx != mDtDx 1448 || mLastDsDy != mDsDy 1449 || mLastDtDy != mDtDy 1450 || w.mLastHScale != w.mHScale 1451 || w.mLastVScale != w.mVScale 1452 || mLastHidden) { 1453 displayed = true; 1454 mLastAlpha = mShownAlpha; 1455 mLastLayer = mAnimLayer; 1456 mLastDsDx = mDsDx; 1457 mLastDtDx = mDtDx; 1458 mLastDsDy = mDsDy; 1459 mLastDtDy = mDtDy; 1460 w.mLastHScale = w.mHScale; 1461 w.mLastVScale = w.mVScale; 1462 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(w, 1463 "controller=" + mSurfaceController + 1464 "alpha=" + mShownAlpha + " layer=" + mAnimLayer 1465 + " matrix=[" + mDsDx + "*" + w.mHScale 1466 + "," + mDtDx + "*" + w.mVScale 1467 + "][" + mDsDy + "*" + w.mHScale 1468 + "," + mDtDy + "*" + w.mVScale + "]", false); 1469 1470 boolean prepared = 1471 mSurfaceController.prepareToShowInTransaction(mShownAlpha, mAnimLayer, 1472 mDsDx * w.mHScale, mDtDx * w.mVScale, 1473 mDsDy * w.mHScale, mDtDy * w.mVScale, 1474 recoveringMemory); 1475 1476 if (prepared && mLastHidden && mDrawState == HAS_DRAWN) { 1477 if (showSurfaceRobustlyLocked()) { 1478 if (mDestroyPreservedSurfaceUponRedraw) { 1479 mService.mDestroyPreservedSurface.add(mWin); 1480 } 1481 mAnimator.requestRemovalOfReplacedWindows(w); 1482 mLastHidden = false; 1483 if (mIsWallpaper) { 1484 mWallpaperControllerLocked.dispatchWallpaperVisibility(w, true); 1485 } 1486 // This draw means the difference between unique content and mirroring. 1487 // Run another pass through performLayout to set mHasContent in the 1488 // LogicalDisplay. 1489 mAnimator.setPendingLayoutChanges(w.getDisplayId(), 1490 WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM); 1491 } else { 1492 w.mOrientationChanging = false; 1493 } 1494 } 1495 if (hasSurface()) { 1496 w.mToken.hasVisible = true; 1497 } 1498 } else { 1499 if (DEBUG_ANIM && isAnimating()) { 1500 Slog.v(TAG, "prepareSurface: No changes in animation for " + this); 1501 } 1502 displayed = true; 1503 } 1504 1505 if (displayed) { 1506 if (w.mOrientationChanging) { 1507 if (!w.isDrawnLw()) { 1508 mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE; 1509 mAnimator.mLastWindowFreezeSource = w; 1510 if (DEBUG_ORIENTATION) Slog.v(TAG, 1511 "Orientation continue waiting for draw in " + w); 1512 } else { 1513 w.mOrientationChanging = false; 1514 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w); 1515 } 1516 } 1517 w.mToken.hasVisible = true; 1518 } 1519 } 1520 1521 void setTransparentRegionHintLocked(final Region region) { 1522 if (mSurfaceController == null) { 1523 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true"); 1524 return; 1525 } 1526 mSurfaceController.setTransparentRegionHint(region); 1527 } 1528 1529 void setWallpaperOffset(Point shownPosition) { 1530 final LayoutParams attrs = mWin.getAttrs(); 1531 final int left = shownPosition.x - attrs.surfaceInsets.left; 1532 final int top = shownPosition.y - attrs.surfaceInsets.top; 1533 1534 try { 1535 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset"); 1536 SurfaceControl.openTransaction(); 1537 mSurfaceController.setPositionInTransaction(mWin.mFrame.left + left, 1538 mWin.mFrame.top + top, false); 1539 calculateSurfaceWindowCrop(mTmpClipRect, mTmpFinalClipRect); 1540 updateSurfaceWindowCrop(mTmpClipRect, mTmpFinalClipRect, false); 1541 } catch (RuntimeException e) { 1542 Slog.w(TAG, "Error positioning surface of " + mWin 1543 + " pos=(" + left + "," + top + ")", e); 1544 } finally { 1545 SurfaceControl.closeTransaction(); 1546 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, 1547 "<<< CLOSE TRANSACTION setWallpaperOffset"); 1548 } 1549 } 1550 1551 /** 1552 * Try to change the pixel format without recreating the surface. This 1553 * will be common in the case of changing from PixelFormat.OPAQUE to 1554 * PixelFormat.TRANSLUCENT in the hardware-accelerated case as both 1555 * requested formats resolve to the same underlying SurfaceControl format 1556 * @return True if format was succesfully changed, false otherwise 1557 */ 1558 boolean tryChangeFormatInPlaceLocked() { 1559 if (mSurfaceController == null) { 1560 return false; 1561 } 1562 final LayoutParams attrs = mWin.getAttrs(); 1563 final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0; 1564 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format; 1565 if (format == mSurfaceFormat) { 1566 setOpaqueLocked(!PixelFormat.formatHasAlpha(attrs.format)); 1567 return true; 1568 } 1569 return false; 1570 } 1571 1572 void setOpaqueLocked(boolean isOpaque) { 1573 if (mSurfaceController == null) { 1574 return; 1575 } 1576 mSurfaceController.setOpaque(isOpaque); 1577 } 1578 1579 void setSecureLocked(boolean isSecure) { 1580 if (mSurfaceController == null) { 1581 return; 1582 } 1583 mSurfaceController.setSecure(isSecure); 1584 } 1585 1586 // This must be called while inside a transaction. 1587 boolean performShowLocked() { 1588 if (mWin.isHiddenFromUserLocked()) { 1589 if (DEBUG_VISIBILITY) Slog.w(TAG, "hiding " + mWin + ", belonging to " + mWin.mOwnerUid); 1590 mWin.hideLw(false); 1591 return false; 1592 } 1593 if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW && 1594 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) { 1595 Slog.v(TAG, "performShow on " + this 1596 + ": mDrawState=" + drawStateToString() + " readyForDisplay=" 1597 + mWin.isReadyForDisplayIgnoringKeyguard() 1598 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING) 1599 + " during animation: policyVis=" + mWin.mPolicyVisibility 1600 + " attHidden=" + mWin.mAttachedHidden 1601 + " tok.hiddenRequested=" 1602 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false) 1603 + " tok.hidden=" 1604 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false) 1605 + " animating=" + mAnimating 1606 + " tok animating=" 1607 + (mAppAnimator != null ? mAppAnimator.animating : false) + " Callers=" 1608 + Debug.getCallers(3)); 1609 } 1610 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplayIgnoringKeyguard()) { 1611 if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW && 1612 mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) { 1613 Slog.v(TAG, "Showing " + this 1614 + " during animation: policyVis=" + mWin.mPolicyVisibility 1615 + " attHidden=" + mWin.mAttachedHidden 1616 + " tok.hiddenRequested=" 1617 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false) 1618 + " tok.hidden=" 1619 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false) 1620 + " animating=" + mAnimating 1621 + " tok animating=" 1622 + (mAppAnimator != null ? mAppAnimator.animating : false)); 1623 } 1624 1625 mService.enableScreenIfNeededLocked(); 1626 1627 applyEnterAnimationLocked(); 1628 1629 // Force the show in the next prepareSurfaceLocked() call. 1630 mLastAlpha = -1; 1631 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) 1632 Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + mWin); 1633 mDrawState = HAS_DRAWN; 1634 mService.scheduleAnimationLocked(); 1635 1636 int i = mWin.mChildWindows.size(); 1637 while (i > 0) { 1638 i--; 1639 WindowState c = mWin.mChildWindows.get(i); 1640 if (c.mAttachedHidden) { 1641 c.mAttachedHidden = false; 1642 if (c.mWinAnimator.mSurfaceController != null) { 1643 c.mWinAnimator.performShowLocked(); 1644 // It hadn't been shown, which means layout not 1645 // performed on it, so now we want to make sure to 1646 // do a layout. If called from within the transaction 1647 // loop, this will cause it to restart with a new 1648 // layout. 1649 final DisplayContent displayContent = c.getDisplayContent(); 1650 if (displayContent != null) { 1651 displayContent.layoutNeeded = true; 1652 } 1653 } 1654 } 1655 } 1656 1657 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING && mWin.mAppToken != null) { 1658 mWin.mAppToken.onFirstWindowDrawn(mWin, this); 1659 } 1660 1661 return true; 1662 } 1663 return false; 1664 } 1665 1666 /** 1667 * Have the surface flinger show a surface, robustly dealing with 1668 * error conditions. In particular, if there is not enough memory 1669 * to show the surface, then we will try to get rid of other surfaces 1670 * in order to succeed. 1671 * 1672 * @return Returns true if the surface was successfully shown. 1673 */ 1674 private boolean showSurfaceRobustlyLocked() { 1675 boolean shown = mSurfaceController.showRobustlyInTransaction(); 1676 if (!shown) 1677 return false; 1678 1679 if (mWin.mTurnOnScreen) { 1680 if (DEBUG_VISIBILITY) Slog.v(TAG, "Show surface turning screen on: " + mWin); 1681 mWin.mTurnOnScreen = false; 1682 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN; 1683 } 1684 return true; 1685 } 1686 1687 void applyEnterAnimationLocked() { 1688 // If we are the new part of a window replacement transition and we have requested 1689 // not to animate, we instead want to make it seamless, so we don't want to apply 1690 // an enter transition. 1691 if (mWin.mSkipEnterAnimationForSeamlessReplacement) { 1692 return; 1693 } 1694 final int transit; 1695 if (mEnterAnimationPending) { 1696 mEnterAnimationPending = false; 1697 transit = WindowManagerPolicy.TRANSIT_ENTER; 1698 } else { 1699 transit = WindowManagerPolicy.TRANSIT_SHOW; 1700 } 1701 applyAnimationLocked(transit, true); 1702 //TODO (multidisplay): Magnification is supported only for the default display. 1703 if (mService.mAccessibilityController != null 1704 && mWin.getDisplayId() == DEFAULT_DISPLAY) { 1705 mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit); 1706 } 1707 } 1708 1709 /** 1710 * Choose the correct animation and set it to the passed WindowState. 1711 * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn 1712 * then the animation will be app_starting_exit. Any other value loads the animation from 1713 * the switch statement below. 1714 * @param isEntrance The animation type the last time this was called. Used to keep from 1715 * loading the same animation twice. 1716 * @return true if an animation has been loaded. 1717 */ 1718 boolean applyAnimationLocked(int transit, boolean isEntrance) { 1719 if ((mLocalAnimating && mAnimationIsEntrance == isEntrance) 1720 || mKeyguardGoingAwayAnimation) { 1721 // If we are trying to apply an animation, but already running 1722 // an animation of the same type, then just leave that one alone. 1723 1724 // If we are in a keyguard exit animation, and the window should animate away, modify 1725 // keyguard exit animation such that it also fades out. 1726 if (mAnimation != null && mKeyguardGoingAwayAnimation 1727 && transit == WindowManagerPolicy.TRANSIT_PREVIEW_DONE) { 1728 applyFadeoutDuringKeyguardExitAnimation(); 1729 } 1730 return true; 1731 } 1732 1733 // Only apply an animation if the display isn't frozen. If it is 1734 // frozen, there is no reason to animate and it can cause strange 1735 // artifacts when we unfreeze the display if some different animation 1736 // is running. 1737 if (mService.okToDisplay()) { 1738 int anim = mPolicy.selectAnimationLw(mWin, transit); 1739 int attr = -1; 1740 Animation a = null; 1741 if (anim != 0) { 1742 a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null; 1743 } else { 1744 switch (transit) { 1745 case WindowManagerPolicy.TRANSIT_ENTER: 1746 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation; 1747 break; 1748 case WindowManagerPolicy.TRANSIT_EXIT: 1749 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation; 1750 break; 1751 case WindowManagerPolicy.TRANSIT_SHOW: 1752 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation; 1753 break; 1754 case WindowManagerPolicy.TRANSIT_HIDE: 1755 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation; 1756 break; 1757 } 1758 if (attr >= 0) { 1759 a = mService.mAppTransition.loadAnimationAttr(mWin.mAttrs, attr); 1760 } 1761 } 1762 if (DEBUG_ANIM) Slog.v(TAG, 1763 "applyAnimation: win=" + this 1764 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr) 1765 + " a=" + a 1766 + " transit=" + transit 1767 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3)); 1768 if (a != null) { 1769 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this); 1770 setAnimation(a); 1771 mAnimationIsEntrance = isEntrance; 1772 } 1773 } else { 1774 clearAnimation(); 1775 } 1776 if (mWin.mAttrs.type == TYPE_INPUT_METHOD) { 1777 mService.adjustForImeIfNeeded(mWin.mDisplayContent); 1778 if (isEntrance) { 1779 mWin.setDisplayLayoutNeeded(); 1780 mService.mWindowPlacerLocked.requestTraversal(); 1781 } 1782 } 1783 return mAnimation != null; 1784 } 1785 1786 private void applyFadeoutDuringKeyguardExitAnimation() { 1787 long startTime = mAnimation.getStartTime(); 1788 long duration = mAnimation.getDuration(); 1789 long elapsed = mLastAnimationTime - startTime; 1790 long fadeDuration = duration - elapsed; 1791 if (fadeDuration <= 0) { 1792 // Never mind, this would be no visible animation, so abort the animation change. 1793 return; 1794 } 1795 AnimationSet newAnimation = new AnimationSet(false /* shareInterpolator */); 1796 newAnimation.setDuration(duration); 1797 newAnimation.setStartTime(startTime); 1798 newAnimation.addAnimation(mAnimation); 1799 Animation fadeOut = AnimationUtils.loadAnimation( 1800 mContext, com.android.internal.R.anim.app_starting_exit); 1801 fadeOut.setDuration(fadeDuration); 1802 fadeOut.setStartOffset(elapsed); 1803 newAnimation.addAnimation(fadeOut); 1804 newAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(), mAnimDx, mAnimDy); 1805 mAnimation = newAnimation; 1806 } 1807 1808 public void dump(PrintWriter pw, String prefix, boolean dumpAll) { 1809 if (mAnimating || mLocalAnimating || mAnimationIsEntrance 1810 || mAnimation != null) { 1811 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating); 1812 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating); 1813 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance); 1814 pw.print(" mAnimation="); pw.print(mAnimation); 1815 pw.print(" mStackClip="); pw.println(mStackClip); 1816 } 1817 if (mHasTransformation || mHasLocalTransformation) { 1818 pw.print(prefix); pw.print("XForm: has="); 1819 pw.print(mHasTransformation); 1820 pw.print(" hasLocal="); pw.print(mHasLocalTransformation); 1821 pw.print(" "); mTransformation.printShortString(pw); 1822 pw.println(); 1823 } 1824 if (mSurfaceController != null) { 1825 mSurfaceController.dump(pw, prefix, dumpAll); 1826 } 1827 if (dumpAll) { 1828 pw.print(prefix); pw.print("mDrawState="); pw.print(drawStateToString()); 1829 pw.print(prefix); pw.print(" mLastHidden="); pw.println(mLastHidden); 1830 pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw); 1831 pw.print(" last="); mLastSystemDecorRect.printShortString(pw); 1832 pw.print(" mHasClipRect="); pw.print(mHasClipRect); 1833 pw.print(" mLastClipRect="); mLastClipRect.printShortString(pw); 1834 1835 if (!mLastFinalClipRect.isEmpty()) { 1836 pw.print(" mLastFinalClipRect="); mLastFinalClipRect.printShortString(pw); 1837 } 1838 pw.println(); 1839 } 1840 1841 if (mPendingDestroySurface != null) { 1842 pw.print(prefix); pw.print("mPendingDestroySurface="); 1843 pw.println(mPendingDestroySurface); 1844 } 1845 if (mSurfaceResized || mSurfaceDestroyDeferred) { 1846 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized); 1847 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred); 1848 } 1849 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) { 1850 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha); 1851 pw.print(" mAlpha="); pw.print(mAlpha); 1852 pw.print(" mLastAlpha="); pw.println(mLastAlpha); 1853 } 1854 if (mHaveMatrix || mWin.mGlobalScale != 1) { 1855 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale); 1856 pw.print(" mDsDx="); pw.print(mDsDx); 1857 pw.print(" mDtDx="); pw.print(mDtDx); 1858 pw.print(" mDsDy="); pw.print(mDsDy); 1859 pw.print(" mDtDy="); pw.println(mDtDy); 1860 } 1861 if (mAnimationStartDelayed) { 1862 pw.print(prefix); pw.print("mAnimationStartDelayed="); pw.print(mAnimationStartDelayed); 1863 } 1864 } 1865 1866 @Override 1867 public String toString() { 1868 StringBuffer sb = new StringBuffer("WindowStateAnimator{"); 1869 sb.append(Integer.toHexString(System.identityHashCode(this))); 1870 sb.append(' '); 1871 sb.append(mWin.mAttrs.getTitle()); 1872 sb.append('}'); 1873 return sb.toString(); 1874 } 1875 1876 void reclaimSomeSurfaceMemory(String operation, boolean secure) { 1877 mService.reclaimSomeSurfaceMemoryLocked(this, operation, secure); 1878 } 1879 1880 boolean getShown() { 1881 if (mSurfaceController != null) { 1882 return mSurfaceController.getShown(); 1883 } 1884 return false; 1885 } 1886 1887 void destroySurface() { 1888 try { 1889 if (mSurfaceController != null) { 1890 mSurfaceController.destroyInTransaction(); 1891 } 1892 } catch (RuntimeException e) { 1893 Slog.w(TAG, "Exception thrown when destroying surface " + this 1894 + " surface " + mSurfaceController + " session " + mSession + ": " + e); 1895 } finally { 1896 mWin.setHasSurface(false); 1897 mSurfaceController = null; 1898 mDrawState = NO_SURFACE; 1899 } 1900 } 1901 1902 void setMoveAnimation(int left, int top) { 1903 final Animation a = AnimationUtils.loadAnimation(mContext, 1904 com.android.internal.R.anim.window_move_from_decor); 1905 setAnimation(a); 1906 mAnimDx = mWin.mLastFrame.left - left; 1907 mAnimDy = mWin.mLastFrame.top - top; 1908 mAnimateMove = true; 1909 } 1910 1911 void deferTransactionUntilParentFrame(long frameNumber) { 1912 if (!mWin.isChildWindow()) { 1913 return; 1914 } 1915 mDeferTransactionUntilFrame = frameNumber; 1916 mDeferTransactionTime = System.currentTimeMillis(); 1917 mSurfaceController.deferTransactionUntil( 1918 mWin.mAttachedWindow.mWinAnimator.mSurfaceController.getHandle(), 1919 frameNumber); 1920 } 1921 1922 // Defer the current transaction to the frame number of the last saved transaction. 1923 // We do this to avoid shooting through an unsynchronized transaction while something is 1924 // pending. This is generally fine, as either we will get in on the synchronization, 1925 // or SurfaceFlinger will see that the frame has already occured. The only 1926 // potential problem is in frame number resets so we reset things with a timeout 1927 // every so often to be careful. 1928 void deferToPendingTransaction() { 1929 if (mDeferTransactionUntilFrame < 0) { 1930 return; 1931 } 1932 long time = System.currentTimeMillis(); 1933 if (time > mDeferTransactionTime + PENDING_TRANSACTION_FINISH_WAIT_TIME) { 1934 mDeferTransactionTime = -1; 1935 mDeferTransactionUntilFrame = -1; 1936 } else { 1937 mSurfaceController.deferTransactionUntil( 1938 mWin.mAttachedWindow.mWinAnimator.mSurfaceController.getHandle(), 1939 mDeferTransactionUntilFrame); 1940 } 1941 } 1942 1943 /** 1944 * Sometimes we need to synchronize the first frame of animation with some external event. 1945 * To achieve this, we prolong the start of the animation and keep producing the first frame of 1946 * the animation. 1947 */ 1948 private long getAnimationFrameTime(Animation animation, long currentTime) { 1949 if (mAnimationStartDelayed) { 1950 animation.setStartTime(currentTime); 1951 return currentTime + 1; 1952 } 1953 return currentTime; 1954 } 1955 1956 void startDelayingAnimationStart() { 1957 mAnimationStartDelayed = true; 1958 } 1959 1960 void endDelayingAnimationStart() { 1961 mAnimationStartDelayed = false; 1962 } 1963} 1964