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