blob: 8066e00752439e6daad0def39c7149f5775865bd [file] [log] [blame]
Craig Mautnera2c77052012-03-26 12:14:43 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2
3package com.android.server.wm;
4
Craig Mautnerc2f9be02012-03-27 17:32:29 -07005import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
6import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
7
8import android.content.Context;
9import android.graphics.Matrix;
10import android.graphics.PixelFormat;
11import android.graphics.Rect;
Craig Mautner48ba1e72012-04-02 13:18:16 -070012import android.graphics.Region;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070013import android.os.RemoteException;
Craig Mautnera2c77052012-03-26 12:14:43 -070014import android.util.Slog;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070015import android.view.Surface;
Craig Mautnera2c77052012-03-26 12:14:43 -070016import android.view.WindowManager;
17import android.view.WindowManagerPolicy;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070018import android.view.WindowManager.LayoutParams;
Craig Mautnera2c77052012-03-26 12:14:43 -070019import android.view.animation.Animation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070020import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070021import android.view.animation.Transformation;
22
23import com.android.server.wm.WindowManagerService.H;
24
25import java.io.PrintWriter;
26
27/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070028 * Keep track of animations and surface operations for a single WindowState.
29 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070030class WindowStateAnimator {
Craig Mautnerc2f9be02012-03-27 17:32:29 -070031 static final boolean DEBUG_VISIBILITY = WindowManagerService.DEBUG_VISIBILITY;
32 static final boolean DEBUG_ANIM = WindowManagerService.DEBUG_ANIM;
33 static final boolean DEBUG_LAYERS = WindowManagerService.DEBUG_LAYERS;
34 static final boolean DEBUG_STARTING_WINDOW = WindowManagerService.DEBUG_STARTING_WINDOW;
35 static final boolean SHOW_TRANSACTIONS = WindowManagerService.SHOW_TRANSACTIONS;
36 static final boolean SHOW_LIGHT_TRANSACTIONS = WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
37 static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
38 static final boolean localLOGV = WindowManagerService.localLOGV;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -070039 static final boolean DEBUG_ORIENTATION = WindowManagerService.DEBUG_ORIENTATION;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070040
41 static final String TAG = "WindowStateAnimator";
Craig Mautnera2c77052012-03-26 12:14:43 -070042
43 final WindowManagerService mService;
44 final WindowState mWin;
45 final WindowState mAttachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -070046 final WindowAnimator mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070047 final Session mSession;
48 final WindowManagerPolicy mPolicy;
49 final Context mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -070050
51 // Currently running animation.
52 boolean mAnimating;
53 boolean mLocalAnimating;
54 Animation mAnimation;
55 boolean mAnimationIsEntrance;
56 boolean mHasTransformation;
57 boolean mHasLocalTransformation;
58 final Transformation mTransformation = new Transformation();
59 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -070060 int mAnimLayer;
61 int mLastLayer;
62
63 Surface mSurface;
64 Surface mPendingDestroySurface;
65 boolean mReportDestroySurface;
66 boolean mSurfacePendingDestroy;
67
68 /**
69 * Set when we have changed the size of the surface, to know that
70 * we must tell them application to resize (and thus redraw itself).
71 */
72 boolean mSurfaceResized;
73
74 /**
75 * Set if the client has asked that the destroy of its surface be delayed
76 * until it explicitly says it is okay.
77 */
78 boolean mSurfaceDestroyDeferred;
79
80 float mShownAlpha = 1;
81 float mAlpha = 1;
82 float mLastAlpha = 1;
83
84 // Used to save animation distances between the time they are calculated and when they are
85 // used.
86 int mAnimDw;
87 int mAnimDh;
88 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
89 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
90
91 boolean mHaveMatrix;
92
93 // For debugging, this is the last information given to the surface flinger.
94 boolean mSurfaceShown;
95 float mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
96 int mSurfaceLayer;
97 float mSurfaceAlpha;
98
99 // Set to true if, when the window gets displayed, it should perform
100 // an enter animation.
101 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700102
Craig Mautnera608b882012-03-30 13:03:49 -0700103 // This is set after the Surface has been created but before the
104 // window has been drawn. During this time the surface is hidden.
105 boolean mDrawPending;
106
107 // This is set after the window has finished drawing for the first
108 // time but before its surface is shown. The surface will be
109 // displayed when the next layout is run.
110 boolean mCommitDrawPending;
111
Craig Mautnera2c77052012-03-26 12:14:43 -0700112 public WindowStateAnimator(final WindowManagerService service, final WindowState win,
113 final WindowState attachedWindow) {
114 mService = service;
115 mWin = win;
116 mAttachedWindow = attachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700117 mAnimator = mService.mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700118 mSession = win.mSession;
119 mPolicy = mService.mPolicy;
120 mContext = mService.mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -0700121 }
122
123 public void setAnimation(Animation anim) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700124 if (localLOGV) Slog.v(
125 TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700126 mAnimating = false;
127 mLocalAnimating = false;
128 mAnimation = anim;
129 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
130 mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
131 // Start out animation gone if window is gone, or visible if window is visible.
132 mTransformation.clear();
133 mTransformation.setAlpha(mWin.mLastHidden ? 0 : 1);
134 mHasLocalTransformation = true;
135 }
136
137 public void clearAnimation() {
138 if (mAnimation != null) {
139 mAnimating = true;
140 mLocalAnimating = false;
141 mAnimation.cancel();
142 mAnimation = null;
143 }
144 }
145
146 /** Is the window or its container currently animating? */
147 boolean isAnimating() {
148 final WindowState attached = mAttachedWindow;
149 final AppWindowToken atoken = mWin.mAppToken;
150 return mAnimation != null
151 || (attached != null && attached.mWinAnimator.mAnimation != null)
152 || (atoken != null &&
153 (atoken.animation != null
154 || atoken.inPendingTransaction));
155 }
156
157 /** Is this window currently animating? */
158 boolean isWindowAnimating() {
159 return mAnimation != null;
160 }
161
Craig Mautnera2c77052012-03-26 12:14:43 -0700162 void cancelExitAnimationForNextAnimationLocked() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700163 if (mAnimation != null) {
164 mAnimation.cancel();
165 mAnimation = null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700166 destroySurfaceLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700167 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700168 }
169
170 private boolean stepAnimation(long currentTime) {
171 if ((mAnimation == null) || !mLocalAnimating) {
172 return false;
173 }
174 mTransformation.clear();
175 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700176 if (DEBUG_ANIM) Slog.v(
177 TAG, "Stepped animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700178 ": more=" + more + ", xform=" + mTransformation);
179 return more;
180 }
181
182 // This must be called while inside a transaction. Returns true if
183 // there is more animation to run.
184 boolean stepAnimationLocked(long currentTime) {
185 // Save the animation state as it was before this step so WindowManagerService can tell if
186 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
187 mWasAnimating = mAnimating;
188 if (mService.okToDisplay()) {
189 // We will run animations as long as the display isn't frozen.
190
191 if (mWin.isDrawnLw() && mAnimation != null) {
192 mHasTransformation = true;
193 mHasLocalTransformation = true;
194 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700195 if (DEBUG_ANIM) Slog.v(
196 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700197 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
198 " wh=" + mWin.mFrame.height() +
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700199 " dw=" + mAnimDw + " dh=" + mAnimDh +
Craig Mautnera2c77052012-03-26 12:14:43 -0700200 " scale=" + mService.mWindowAnimationScale);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700201 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
202 mAnimDw, mAnimDh);
Craig Mautnera2c77052012-03-26 12:14:43 -0700203 mAnimation.setStartTime(currentTime);
204 mLocalAnimating = true;
205 mAnimating = true;
206 }
207 if ((mAnimation != null) && mLocalAnimating) {
208 if (stepAnimation(currentTime)) {
209 return true;
210 }
211 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700212 if (DEBUG_ANIM) Slog.v(
213 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700214 " @ " + currentTime);
215 //WindowManagerService.this.dump();
216 }
217 mHasLocalTransformation = false;
218 if ((!mLocalAnimating || mAnimationIsEntrance) && mWin.mAppToken != null
219 && mWin.mAppToken.animation != null) {
220 // When our app token is animating, we kind-of pretend like
221 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
222 // part of this check means that we will only do this if
223 // our window is not currently exiting, or it is not
224 // locally animating itself. The idea being that one that
225 // is exiting and doing a local animation should be removed
226 // once that animation is done.
227 mAnimating = true;
228 mHasTransformation = true;
229 mTransformation.clear();
230 return false;
231 } else if (mHasTransformation) {
232 // Little trick to get through the path below to act like
233 // we have finished an animation.
234 mAnimating = true;
235 } else if (isAnimating()) {
236 mAnimating = true;
237 }
238 } else if (mAnimation != null) {
239 // If the display is frozen, and there is a pending animation,
240 // clear it and make sure we run the cleanup code.
241 mAnimating = true;
242 mLocalAnimating = true;
243 mAnimation.cancel();
244 mAnimation = null;
245 }
246
247 if (!mAnimating && !mLocalAnimating) {
248 return false;
249 }
250
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700251 // Done animating, clean up.
252 if (DEBUG_ANIM) Slog.v(
253 TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
Craig Mautnera2c77052012-03-26 12:14:43 -0700254 + ", reportedVisible="
255 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
256
257 mAnimating = false;
258 mLocalAnimating = false;
259 if (mAnimation != null) {
260 mAnimation.cancel();
261 mAnimation = null;
262 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700263 if (mAnimator.mWindowDetachedWallpaper == mWin) {
264 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700265 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700266 mAnimLayer = mWin.mLayer;
Craig Mautnera2c77052012-03-26 12:14:43 -0700267 if (mWin.mIsImWindow) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700268 mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700269 } else if (mWin.mIsWallpaper) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700270 mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700271 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700272 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
273 + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700274 mHasTransformation = false;
275 mHasLocalTransformation = false;
276 if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
277 if (WindowState.DEBUG_VISIBILITY) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700278 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
Craig Mautnera2c77052012-03-26 12:14:43 -0700279 + mWin.mPolicyVisibilityAfterAnim);
280 }
281 mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
282 mService.mLayoutNeeded = true;
283 if (!mWin.mPolicyVisibility) {
284 if (mService.mCurrentFocus == mWin) {
285 mService.mFocusMayChange = true;
286 }
287 // Window is no longer visible -- make sure if we were waiting
288 // for it to be displayed before enabling the display, that
289 // we allow the display to be enabled now.
290 mService.enableScreenIfNeededLocked();
291 }
292 }
293 mTransformation.clear();
294 if (mWin.mHasDrawn
295 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
296 && mWin.mAppToken != null
297 && mWin.mAppToken.firstWindowDrawn
298 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700299 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700300 + mWin.mToken + ": first real window done animating");
301 mService.mFinishedStarting.add(mWin.mAppToken);
302 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
303 }
304
305 finishExit();
306 mService.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
307 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats("WindowState");
308
309 if (mWin.mAppToken != null) {
310 mWin.mAppToken.updateReportedVisibilityLocked();
311 }
312
313 return false;
314 }
315
316 void finishExit() {
317 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700318 TAG, "finishExit in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700319 + ": exiting=" + mWin.mExiting
320 + " remove=" + mWin.mRemoveOnExit
321 + " windowAnimating=" + isWindowAnimating());
322
323 final int N = mWin.mChildWindows.size();
324 for (int i=0; i<N; i++) {
325 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
326 }
327
328 if (!mWin.mExiting) {
329 return;
330 }
331
332 if (isWindowAnimating()) {
333 return;
334 }
335
336 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700337 TAG, "Exit animation finished in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700338 + ": remove=" + mWin.mRemoveOnExit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700339 if (mSurface != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700340 mService.mDestroySurface.add(mWin);
341 mWin.mDestroying = true;
342 if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
343 mWin, "HIDE (finishExit)", null);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700344 mSurfaceShown = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700345 try {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700346 mSurface.hide();
Craig Mautnera2c77052012-03-26 12:14:43 -0700347 } catch (RuntimeException e) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700348 Slog.w(TAG, "Error hiding surface in " + this, e);
Craig Mautnera2c77052012-03-26 12:14:43 -0700349 }
350 mWin.mLastHidden = true;
351 }
352 mWin.mExiting = false;
353 if (mWin.mRemoveOnExit) {
354 mService.mPendingRemove.add(mWin);
355 mWin.mRemoveOnExit = false;
356 }
357 }
358
Craig Mautnera608b882012-03-30 13:03:49 -0700359 boolean finishDrawingLocked() {
360 if (mDrawPending) {
Craig Mautner48ba1e72012-04-02 13:18:16 -0700361 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
Craig Mautnera608b882012-03-30 13:03:49 -0700362 TAG, "finishDrawingLocked: " + this + " in " + mSurface);
363 mCommitDrawPending = true;
364 mDrawPending = false;
365 return true;
366 }
367 return false;
368 }
369
370 // This must be called while inside a transaction.
371 boolean commitFinishDrawingLocked(long currentTime) {
372 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
373 if (!mCommitDrawPending) {
374 return false;
375 }
376 mCommitDrawPending = false;
377 mWin.mReadyToShow = true;
378 final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
379 final AppWindowToken atoken = mWin.mAppToken;
380 if (atoken == null || atoken.allDrawn || starting) {
381 performShowLocked();
382 }
383 return true;
384 }
385
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700386 Surface createSurfaceLocked() {
387 if (mSurface == null) {
388 mReportDestroySurface = false;
389 mSurfacePendingDestroy = false;
Craig Mautner48ba1e72012-04-02 13:18:16 -0700390 if (DEBUG_ORIENTATION) Slog.i(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700391 "createSurface " + this + ": DRAW NOW PENDING");
Craig Mautnera608b882012-03-30 13:03:49 -0700392 mDrawPending = true;
393 mCommitDrawPending = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700394 mWin.mReadyToShow = false;
395 if (mWin.mAppToken != null) {
396 mWin.mAppToken.allDrawn = false;
397 }
398
399 mService.makeWindowFreezingScreenIfNeededLocked(mWin);
400
401 int flags = 0;
402 final WindowManager.LayoutParams attrs = mWin.mAttrs;
403
404 if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
405 flags |= Surface.SECURE;
406 }
407 if (WindowState.DEBUG_VISIBILITY) Slog.v(
408 TAG, "Creating surface in session "
409 + mSession.mSurfaceSession + " window " + this
410 + " w=" + mWin.mCompatFrame.width()
411 + " h=" + mWin.mCompatFrame.height() + " format="
412 + attrs.format + " flags=" + flags);
413
414 int w = mWin.mCompatFrame.width();
415 int h = mWin.mCompatFrame.height();
416 if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
417 // for a scaled surface, we always want the requested
418 // size.
419 w = mWin.mRequestedWidth;
420 h = mWin.mRequestedHeight;
421 }
422
423 // Something is wrong and SurfaceFlinger will not like this,
424 // try to revert to sane values
425 if (w <= 0) w = 1;
426 if (h <= 0) h = 1;
427
428 mSurfaceShown = false;
429 mSurfaceLayer = 0;
430 mSurfaceAlpha = 1;
431 mSurfaceX = 0;
432 mSurfaceY = 0;
433 mSurfaceW = w;
434 mSurfaceH = h;
435 try {
436 final boolean isHwAccelerated = (attrs.flags &
437 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
438 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
439 if (!PixelFormat.formatHasAlpha(attrs.format)) {
440 flags |= Surface.OPAQUE;
441 }
442 mSurface = new Surface(
443 mSession.mSurfaceSession, mSession.mPid,
444 attrs.getTitle().toString(),
445 0, w, h, format, flags);
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700446 mWin.mHasSurface = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700447 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
448 " CREATE SURFACE "
449 + mSurface + " IN SESSION "
450 + mSession.mSurfaceSession
451 + ": pid=" + mSession.mPid + " format="
452 + attrs.format + " flags=0x"
453 + Integer.toHexString(flags)
454 + " / " + this);
455 } catch (Surface.OutOfResourcesException e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700456 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700457 Slog.w(TAG, "OutOfResourcesException creating surface");
458 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
459 return null;
460 } catch (Exception e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700461 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700462 Slog.e(TAG, "Exception creating surface", e);
463 return null;
464 }
465
466 if (WindowManagerService.localLOGV) Slog.v(
467 TAG, "Got surface: " + mSurface
468 + ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
469 + ", animLayer=" + mAnimLayer);
470 if (SHOW_LIGHT_TRANSACTIONS) {
471 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
472 WindowManagerService.logSurface(mWin, "CREATE pos=("
473 + mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
474 + mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
475 + "), layer=" + mAnimLayer + " HIDE", null);
476 }
477 Surface.openTransaction();
478 try {
479 try {
480 mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
481 mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
482 mSurface.setPosition(mSurfaceX, mSurfaceY);
483 mSurfaceLayer = mAnimLayer;
484 mSurface.setLayer(mAnimLayer);
485 mSurfaceShown = false;
486 mSurface.hide();
487 if ((mWin.mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
488 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "DITHER", null);
489 mSurface.setFlags(Surface.SURFACE_DITHER, Surface.SURFACE_DITHER);
490 }
491 } catch (RuntimeException e) {
492 Slog.w(TAG, "Error creating surface in " + w, e);
493 mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
494 }
495 mWin.mLastHidden = true;
496 } finally {
497 Surface.closeTransaction();
498 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
499 "<<< CLOSE TRANSACTION createSurfaceLocked");
500 }
501 if (WindowManagerService.localLOGV) Slog.v(
502 TAG, "Created surface " + this);
503 }
504 return mSurface;
505 }
506
507 void destroySurfaceLocked() {
508 if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
509 mWin.mAppToken.startingDisplayed = false;
510 }
511
512 if (mSurface != null) {
Craig Mautnera608b882012-03-30 13:03:49 -0700513 mDrawPending = false;
514 mCommitDrawPending = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700515 mWin.mReadyToShow = false;
516
517 int i = mWin.mChildWindows.size();
518 while (i > 0) {
519 i--;
520 WindowState c = mWin.mChildWindows.get(i);
521 c.mAttachedHidden = true;
522 }
523
524 if (mReportDestroySurface) {
525 mReportDestroySurface = false;
526 mSurfacePendingDestroy = true;
527 try {
528 mWin.mClient.dispatchGetNewSurface();
529 // We'll really destroy on the next time around.
530 return;
531 } catch (RemoteException e) {
532 }
533 }
534
535 try {
536 if (DEBUG_VISIBILITY) {
537 RuntimeException e = null;
538 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
539 e = new RuntimeException();
540 e.fillInStackTrace();
541 }
542 Slog.w(TAG, "Window " + this + " destroying surface "
543 + mSurface + ", session " + mSession, e);
544 }
545 if (mSurfaceDestroyDeferred) {
546 if (mSurface != null && mPendingDestroySurface != mSurface) {
547 if (mPendingDestroySurface != null) {
548 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
549 RuntimeException e = null;
550 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
551 e = new RuntimeException();
552 e.fillInStackTrace();
553 }
554 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
555 }
556 mPendingDestroySurface.destroy();
557 }
558 mPendingDestroySurface = mSurface;
559 }
560 } else {
561 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
562 RuntimeException e = null;
563 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
564 e = new RuntimeException();
565 e.fillInStackTrace();
566 }
567 WindowManagerService.logSurface(mWin, "DESTROY", e);
568 }
569 mSurface.destroy();
570 }
571 } catch (RuntimeException e) {
572 Slog.w(TAG, "Exception thrown when destroying Window " + this
573 + " surface " + mSurface + " session " + mSession
574 + ": " + e.toString());
575 }
576
577 mSurfaceShown = false;
578 mSurface = null;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700579 mWin.mHasSurface =false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700580 }
581 }
582
583 void destroyDeferredSurfaceLocked() {
584 try {
585 if (mPendingDestroySurface != null) {
586 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
587 RuntimeException e = null;
588 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
589 e = new RuntimeException();
590 e.fillInStackTrace();
591 }
592 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
593 }
594 mPendingDestroySurface.destroy();
595 }
596 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700597 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700598 + this + " surface " + mPendingDestroySurface
599 + " session " + mSession + ": " + e.toString());
600 }
601 mSurfaceDestroyDeferred = false;
602 mPendingDestroySurface = null;
603 }
604
605 void computeShownFrameLocked() {
606 final boolean selfTransformation = mHasLocalTransformation;
607 Transformation attachedTransformation =
608 (mAttachedWindow != null && mAttachedWindow.mWinAnimator.mHasLocalTransformation)
609 ? mAttachedWindow.mWinAnimator.mTransformation : null;
610 Transformation appTransformation =
611 (mWin.mAppToken != null && mWin.mAppToken.hasTransformation)
612 ? mWin.mAppToken.transformation : null;
613
614 // Wallpapers are animated based on the "real" window they
615 // are currently targeting.
616 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
617 && mService.mWallpaperTarget != null) {
618 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
619 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
620 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
621 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
622 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
623 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
624 }
625 }
626 if (mService.mWallpaperTarget.mAppToken != null &&
627 mService.mWallpaperTarget.mAppToken.hasTransformation &&
628 mService.mWallpaperTarget.mAppToken.animation != null &&
629 !mService.mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
630 appTransformation = mService.mWallpaperTarget.mAppToken.transformation;
631 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
632 Slog.v(TAG, "WP target app xform: " + appTransformation);
633 }
634 }
635 }
636
637 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
638 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
639 if (selfTransformation || attachedTransformation != null
640 || appTransformation != null || screenAnimation) {
641 // cache often used attributes locally
642 final Rect frame = mWin.mFrame;
643 final float tmpFloats[] = mService.mTmpFloats;
644 final Matrix tmpMatrix = mWin.mTmpMatrix;
645
646 // Compute the desired transformation.
647 if (screenAnimation) {
648 // If we are doing a screen animation, the global rotation
649 // applied to windows can result in windows that are carefully
650 // aligned with each other to slightly separate, allowing you
651 // to see what is behind them. An unsightly mess. This...
652 // thing... magically makes it call good: scale each window
653 // slightly (two pixels larger in each dimension, from the
654 // window's center).
655 final float w = frame.width();
656 final float h = frame.height();
657 if (w>=1 && h>=1) {
658 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
659 } else {
660 tmpMatrix.reset();
661 }
662 } else {
663 tmpMatrix.reset();
664 }
665 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
666 if (selfTransformation) {
667 tmpMatrix.postConcat(mTransformation.getMatrix());
668 }
669 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
670 if (attachedTransformation != null) {
671 tmpMatrix.postConcat(attachedTransformation.getMatrix());
672 }
673 if (appTransformation != null) {
674 tmpMatrix.postConcat(appTransformation.getMatrix());
675 }
676 if (screenAnimation) {
677 tmpMatrix.postConcat(
678 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
679 }
680
681 // "convert" it into SurfaceFlinger's format
682 // (a 2x2 matrix + an offset)
683 // Here we must not transform the position of the surface
684 // since it is already included in the transformation.
685 //Slog.i(TAG, "Transform: " + matrix);
686
687 mHaveMatrix = true;
688 tmpMatrix.getValues(tmpFloats);
689 mDsDx = tmpFloats[Matrix.MSCALE_X];
690 mDtDx = tmpFloats[Matrix.MSKEW_Y];
691 mDsDy = tmpFloats[Matrix.MSKEW_X];
692 mDtDy = tmpFloats[Matrix.MSCALE_Y];
693 float x = tmpFloats[Matrix.MTRANS_X];
694 float y = tmpFloats[Matrix.MTRANS_Y];
695 int w = frame.width();
696 int h = frame.height();
697 mWin.mShownFrame.set(x, y, x+w, y+h);
698
699 // Now set the alpha... but because our current hardware
700 // can't do alpha transformation on a non-opaque surface,
701 // turn it off if we are running an animation that is also
702 // transforming since it is more important to have that
703 // animation be smooth.
704 mShownAlpha = mAlpha;
705 if (!mService.mLimitedAlphaCompositing
706 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
707 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
708 && x == frame.left && y == frame.top))) {
709 //Slog.i(TAG, "Applying alpha transform");
710 if (selfTransformation) {
711 mShownAlpha *= mTransformation.getAlpha();
712 }
713 if (attachedTransformation != null) {
714 mShownAlpha *= attachedTransformation.getAlpha();
715 }
716 if (appTransformation != null) {
717 mShownAlpha *= appTransformation.getAlpha();
718 }
719 if (screenAnimation) {
720 mShownAlpha *=
721 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
722 }
723 } else {
724 //Slog.i(TAG, "Not applying alpha transform");
725 }
726
727 if (WindowManagerService.localLOGV) Slog.v(
728 TAG, "computeShownFrameLocked: Animating " + this +
729 ": " + mWin.mShownFrame +
730 ", alpha=" + mTransformation.getAlpha() + ", mShownAlpha=" + mShownAlpha);
731 return;
732 }
733
734 if (WindowManagerService.localLOGV) Slog.v(
735 TAG, "computeShownFrameLocked: " + this +
736 " not attached, mAlpha=" + mAlpha);
737 mWin.mShownFrame.set(mWin.mFrame);
738 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
739 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
740 }
741 mShownAlpha = mAlpha;
742 mHaveMatrix = false;
743 mDsDx = mWin.mGlobalScale;
744 mDtDx = 0;
745 mDsDy = 0;
746 mDtDy = mWin.mGlobalScale;
747 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700748
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700749 public void prepareSurfaceLocked(final boolean recoveringMemory) {
750 final WindowState w = mWin;
751 if (mSurface == null) {
752 if (w.mOrientationChanging) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700753 if (DEBUG_ORIENTATION) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700754 Slog.v(TAG, "Orientation change skips hidden " + w);
755 }
756 w.mOrientationChanging = false;
757 }
758 return;
759 }
760
761 boolean displayed = false;
762
763 computeShownFrameLocked();
764
765 int width, height;
766 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
767 // for a scaled surface, we just want to use
768 // the requested size.
769 width = w.mRequestedWidth;
770 height = w.mRequestedHeight;
771 } else {
772 width = w.mCompatFrame.width();
773 height = w.mCompatFrame.height();
774 }
775
776 if (width < 1) {
777 width = 1;
778 }
779 if (height < 1) {
780 height = 1;
781 }
782 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
783 if (surfaceResized) {
784 mSurfaceW = width;
785 mSurfaceH = height;
786 }
787
788 if (mSurfaceX != w.mShownFrame.left
789 || mSurfaceY != w.mShownFrame.top) {
790 try {
791 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
792 "POS " + w.mShownFrame.left
793 + ", " + w.mShownFrame.top, null);
794 mSurfaceX = w.mShownFrame.left;
795 mSurfaceY = w.mShownFrame.top;
796 mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
797 } catch (RuntimeException e) {
798 Slog.w(TAG, "Error positioning surface of " + w
799 + " pos=(" + w.mShownFrame.left
800 + "," + w.mShownFrame.top + ")", e);
801 if (!recoveringMemory) {
802 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
803 }
804 }
805 }
806
807 if (surfaceResized) {
808 try {
809 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
810 "SIZE " + width + "x" + height, null);
811 mSurfaceResized = true;
812 mSurface.setSize(width, height);
813 } catch (RuntimeException e) {
814 // If something goes wrong with the surface (such
815 // as running out of memory), don't take down the
816 // entire system.
817 Slog.e(TAG, "Error resizing surface of " + w
818 + " size=(" + width + "x" + height + ")", e);
819 if (!recoveringMemory) {
820 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
821 }
822 }
823 }
824
825 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
826 if (!w.mLastHidden) {
827 //dump();
828 w.mLastHidden = true;
829 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
830 "HIDE (performLayout)", null);
831 if (mSurface != null) {
832 mSurfaceShown = false;
833 try {
834 mSurface.hide();
835 } catch (RuntimeException e) {
836 Slog.w(TAG, "Exception hiding surface in " + w);
837 }
838 }
839 }
840 // If we are waiting for this window to handle an
841 // orientation change, well, it is hidden, so
842 // doesn't really matter. Note that this does
843 // introduce a potential glitch if the window
844 // becomes unhidden before it has drawn for the
845 // new orientation.
846 if (w.mOrientationChanging) {
847 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700848 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700849 "Orientation change skips hidden " + w);
850 }
851 } else if (mLastLayer != mAnimLayer
852 || mLastAlpha != mShownAlpha
853 || mLastDsDx != mDsDx
854 || mLastDtDx != mDtDx
855 || mLastDsDy != mDsDy
856 || mLastDtDy != mDtDy
857 || w.mLastHScale != w.mHScale
858 || w.mLastVScale != w.mVScale
859 || w.mLastHidden) {
860 displayed = true;
861 mLastAlpha = mShownAlpha;
862 mLastLayer = mAnimLayer;
863 mLastDsDx = mDsDx;
864 mLastDtDx = mDtDx;
865 mLastDsDy = mDsDy;
866 mLastDtDy = mDtDy;
867 w.mLastHScale = w.mHScale;
868 w.mLastVScale = w.mVScale;
869 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
870 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
871 + " matrix=[" + (mDsDx*w.mHScale)
872 + "," + (mDtDx*w.mVScale)
873 + "][" + (mDsDy*w.mHScale)
874 + "," + (mDtDy*w.mVScale) + "]", null);
875 if (mSurface != null) {
876 try {
877 mSurfaceAlpha = mShownAlpha;
878 mSurface.setAlpha(mShownAlpha);
879 mSurfaceLayer = w.mWinAnimator.mAnimLayer;
880 mSurface.setLayer(w.mWinAnimator.mAnimLayer);
881 mSurface.setMatrix(
882 mDsDx*w.mHScale, mDtDx*w.mVScale,
883 mDsDy*w.mHScale, mDtDy*w.mVScale);
884 } catch (RuntimeException e) {
885 Slog.w(TAG, "Error updating surface in " + w, e);
886 if (!recoveringMemory) {
887 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
888 }
889 }
890 }
891
892 if (w.mLastHidden && w.isDrawnLw()
893 && !w.mReadyToShow) {
894 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
895 "SHOW (performLayout)", null);
896 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
897 + " during relayout");
898 if (showSurfaceRobustlyLocked()) {
899 w.mHasDrawn = true;
900 w.mLastHidden = false;
901 } else {
902 w.mOrientationChanging = false;
903 }
904 }
905 if (mSurface != null) {
906 w.mToken.hasVisible = true;
907 }
908 } else {
909 displayed = true;
910 }
911
912 if (displayed) {
913 if (w.mOrientationChanging) {
914 if (!w.isDrawnLw()) {
915 mService.mInnerFields.mOrientationChangeComplete = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700916 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700917 "Orientation continue waiting for draw in " + w);
918 } else {
919 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700920 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700921 }
922 }
923 w.mToken.hasVisible = true;
924 }
925 }
926
Craig Mautner48ba1e72012-04-02 13:18:16 -0700927 void setTransparentRegionHint(final Region region) {
928 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
929 ">>> OPEN TRANSACTION setTransparentRegion");
930 Surface.openTransaction();
931 try {
932 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
933 "transparentRegionHint=" + region, null);
934 mSurface.setTransparentRegionHint(region);
935 } finally {
936 Surface.closeTransaction();
937 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
938 "<<< CLOSE TRANSACTION setTransparentRegion");
939 }
940 }
941
942 void setWallpaperOffset(int left, int top) {
943 Surface.openTransaction();
944 try {
945 mSurfaceX = left;
946 mSurfaceY = top;
947 mSurface.setPosition(left, top);
948 } catch (RuntimeException e) {
949 Slog.w(TAG, "Error positioning surface of " + mWin
950 + " pos=(" + left + "," + top + ")", e);
951 }
952 Surface.closeTransaction();
953 }
954
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700955 // This must be called while inside a transaction.
956 boolean performShowLocked() {
957 if (DEBUG_VISIBILITY) {
958 RuntimeException e = null;
959 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
960 e = new RuntimeException();
961 e.fillInStackTrace();
962 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700963 Slog.v(TAG, "performShow on " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700964 + ": readyToShow=" + mWin.mReadyToShow + " readyForDisplay="
965 + mWin.isReadyForDisplay()
966 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
967 }
968 if (mWin.mReadyToShow && mWin.isReadyForDisplay()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700969 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700970 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -0700971 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700972 + " during animation: policyVis=" + mWin.mPolicyVisibility
973 + " attHidden=" + mWin.mAttachedHidden
974 + " tok.hiddenRequested="
975 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
976 + " tok.hidden="
977 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
978 + " animating=" + mAnimating
979 + " tok animating="
980 + (mWin.mAppToken != null ? mWin.mAppToken.animating : false));
981 if (!showSurfaceRobustlyLocked()) {
982 return false;
983 }
984
985 mService.enableScreenIfNeededLocked();
986
987 applyEnterAnimationLocked();
988
989 mLastAlpha = -1;
990 mWin.mHasDrawn = true;
991 mWin.mLastHidden = false;
992 mWin.mReadyToShow = false;
993
994 int i = mWin.mChildWindows.size();
995 while (i > 0) {
996 i--;
997 WindowState c = mWin.mChildWindows.get(i);
998 if (c.mAttachedHidden) {
999 c.mAttachedHidden = false;
1000 if (c.mWinAnimator.mSurface != null) {
1001 c.mWinAnimator.performShowLocked();
1002 // It hadn't been shown, which means layout not
1003 // performed on it, so now we want to make sure to
1004 // do a layout. If called from within the transaction
1005 // loop, this will cause it to restart with a new
1006 // layout.
1007 mService.mLayoutNeeded = true;
1008 }
1009 }
1010 }
1011
1012 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1013 && mWin.mAppToken != null) {
1014 mWin.mAppToken.firstWindowDrawn = true;
1015
1016 if (mWin.mAppToken.startingData != null) {
1017 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001018 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001019 "Finish starting " + mWin.mToken
1020 + ": first real window is shown, no animation");
1021 // If this initial window is animating, stop it -- we
1022 // will do an animation to reveal it from behind the
1023 // starting window, so there is no need for it to also
1024 // be doing its own stuff.
1025 if (mAnimation != null) {
1026 mAnimation.cancel();
1027 mAnimation = null;
1028 // Make sure we clean up the animation.
1029 mAnimating = true;
1030 }
1031 mService.mFinishedStarting.add(mWin.mAppToken);
1032 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1033 }
1034 mWin.mAppToken.updateReportedVisibilityLocked();
1035 }
1036
1037 return true;
1038 }
1039
1040 return false;
1041 }
1042
1043 /**
1044 * Have the surface flinger show a surface, robustly dealing with
1045 * error conditions. In particular, if there is not enough memory
1046 * to show the surface, then we will try to get rid of other surfaces
1047 * in order to succeed.
1048 *
1049 * @return Returns true if the surface was successfully shown.
1050 */
1051 boolean showSurfaceRobustlyLocked() {
1052 try {
1053 if (mSurface != null) {
1054 mSurfaceShown = true;
1055 mSurface.show();
1056 if (mWin.mTurnOnScreen) {
1057 if (DEBUG_VISIBILITY) Slog.v(TAG,
1058 "Show surface turning screen on: " + mWin);
1059 mWin.mTurnOnScreen = false;
1060 mService.mTurnOnScreen = true;
1061 }
1062 }
1063 return true;
1064 } catch (RuntimeException e) {
1065 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1066 }
1067
1068 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1069
1070 return false;
1071 }
1072
1073 void applyEnterAnimationLocked() {
1074 final int transit;
1075 if (mEnterAnimationPending) {
1076 mEnterAnimationPending = false;
1077 transit = WindowManagerPolicy.TRANSIT_ENTER;
1078 } else {
1079 transit = WindowManagerPolicy.TRANSIT_SHOW;
1080 }
1081
1082 applyAnimationLocked(transit, true);
1083 }
1084
Craig Mautner48ba1e72012-04-02 13:18:16 -07001085 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001086 /**
1087 * Choose the correct animation and set it to the passed WindowState.
1088 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1089 * then the animation will be app_starting_exit. Any other value loads the animation from
1090 * the switch statement below.
1091 * @param isEntrance The animation type the last time this was called. Used to keep from
1092 * loading the same animation twice.
1093 * @return true if an animation has been loaded.
1094 */
1095 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1096 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1097 // If we are trying to apply an animation, but already running
1098 // an animation of the same type, then just leave that one alone.
1099 return true;
1100 }
1101
1102 // Only apply an animation if the display isn't frozen. If it is
1103 // frozen, there is no reason to animate and it can cause strange
1104 // artifacts when we unfreeze the display if some different animation
1105 // is running.
1106 if (mService.okToDisplay()) {
1107 int anim = mPolicy.selectAnimationLw(mWin, transit);
1108 int attr = -1;
1109 Animation a = null;
1110 if (anim != 0) {
1111 a = AnimationUtils.loadAnimation(mContext, anim);
1112 } else {
1113 switch (transit) {
1114 case WindowManagerPolicy.TRANSIT_ENTER:
1115 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1116 break;
1117 case WindowManagerPolicy.TRANSIT_EXIT:
1118 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1119 break;
1120 case WindowManagerPolicy.TRANSIT_SHOW:
1121 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1122 break;
1123 case WindowManagerPolicy.TRANSIT_HIDE:
1124 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1125 break;
1126 }
1127 if (attr >= 0) {
1128 a = mService.loadAnimation(mWin.mAttrs, attr);
1129 }
1130 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001131 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001132 "applyAnimation: win=" + this
1133 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1134 + " mAnimation=" + mAnimation
1135 + " isEntrance=" + isEntrance);
1136 if (a != null) {
1137 if (WindowManagerService.DEBUG_ANIM) {
1138 RuntimeException e = null;
1139 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1140 e = new RuntimeException();
1141 e.fillInStackTrace();
1142 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001143 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001144 }
1145 setAnimation(a);
1146 mAnimationIsEntrance = isEntrance;
1147 }
1148 } else {
1149 clearAnimation();
1150 }
1151
1152 return mAnimation != null;
1153 }
1154
Craig Mautnera2c77052012-03-26 12:14:43 -07001155 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1156 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1157 || mAnimation != null) {
1158 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1159 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1160 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1161 pw.print(" mAnimation="); pw.println(mAnimation);
1162 }
1163 if (mHasTransformation || mHasLocalTransformation) {
1164 pw.print(prefix); pw.print("XForm: has=");
1165 pw.print(mHasTransformation);
1166 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1167 pw.print(" "); mTransformation.printShortString(pw);
1168 pw.println();
1169 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001170 if (mSurface != null) {
1171 if (dumpAll) {
1172 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
1173 }
1174 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1175 pw.print(" layer="); pw.print(mSurfaceLayer);
1176 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1177 pw.print(" rect=("); pw.print(mSurfaceX);
1178 pw.print(","); pw.print(mSurfaceY);
1179 pw.print(") "); pw.print(mSurfaceW);
1180 pw.print(" x "); pw.println(mSurfaceH);
1181 }
1182 if (mPendingDestroySurface != null) {
1183 pw.print(prefix); pw.print("mPendingDestroySurface=");
1184 pw.println(mPendingDestroySurface);
1185 }
1186 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1187 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1188 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1189 }
1190 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1191 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1192 pw.print(" mAlpha="); pw.print(mAlpha);
1193 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1194 }
1195 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1196 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1197 pw.print(" mDsDx="); pw.print(mDsDx);
1198 pw.print(" mDtDx="); pw.print(mDtDx);
1199 pw.print(" mDsDy="); pw.print(mDsDy);
1200 pw.print(" mDtDy="); pw.println(mDtDy);
1201 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001202 }
1203
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001204 @Override
1205 public String toString() {
1206 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1207 sb.append(mWin.mLastTitle + "): ");
1208 sb.append("mSurface " + mSurface);
1209 sb.append(", mAnimation " + mAnimation);
1210 return sb.toString();
1211 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001212}