blob: 4979a4c31b5b0cb22b259c5714632423c5a57eda [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 Mautner749a7bb2012-04-02 13:49:53 -0700103 /** This is set when there is no Surface */
104 static final int NO_SURFACE = 0;
105 /** This is set after the Surface has been created but before the window has been drawn. During
106 * this time the surface is hidden. */
107 static final int DRAW_PENDING = 1;
108 /** This is set after the window has finished drawing for the first time but before its surface
109 * is shown. The surface will be displayed when the next layout is run. */
110 static final int COMMIT_DRAW_PENDING = 2;
111 /** This is set during the time after the window's drawing has been committed, and before its
112 * surface is actually shown. It is used to delay showing the surface until all windows in a
113 * token are ready to be shown. */
114 static final int READY_TO_SHOW = 3;
115 /** Set when the window has been shown in the screen the first time. */
116 static final int HAS_DRAWN = 4;
117 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700118
Craig Mautner749a7bb2012-04-02 13:49:53 -0700119 /** Was this window last hidden? */
120 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700121
Craig Mautnera2c77052012-03-26 12:14:43 -0700122 public WindowStateAnimator(final WindowManagerService service, final WindowState win,
123 final WindowState attachedWindow) {
124 mService = service;
125 mWin = win;
126 mAttachedWindow = attachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700127 mAnimator = mService.mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700128 mSession = win.mSession;
129 mPolicy = mService.mPolicy;
130 mContext = mService.mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -0700131 }
132
133 public void setAnimation(Animation anim) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700134 if (localLOGV) Slog.v(
135 TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700136 mAnimating = false;
137 mLocalAnimating = false;
138 mAnimation = anim;
139 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
140 mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
141 // Start out animation gone if window is gone, or visible if window is visible.
142 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700143 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700144 mHasLocalTransformation = true;
145 }
146
147 public void clearAnimation() {
148 if (mAnimation != null) {
149 mAnimating = true;
150 mLocalAnimating = false;
151 mAnimation.cancel();
152 mAnimation = null;
153 }
154 }
155
156 /** Is the window or its container currently animating? */
157 boolean isAnimating() {
158 final WindowState attached = mAttachedWindow;
159 final AppWindowToken atoken = mWin.mAppToken;
160 return mAnimation != null
161 || (attached != null && attached.mWinAnimator.mAnimation != null)
162 || (atoken != null &&
163 (atoken.animation != null
164 || atoken.inPendingTransaction));
165 }
166
167 /** Is this window currently animating? */
168 boolean isWindowAnimating() {
169 return mAnimation != null;
170 }
171
Craig Mautnera2c77052012-03-26 12:14:43 -0700172 void cancelExitAnimationForNextAnimationLocked() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700173 if (mAnimation != null) {
174 mAnimation.cancel();
175 mAnimation = null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700176 destroySurfaceLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700177 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700178 }
179
180 private boolean stepAnimation(long currentTime) {
181 if ((mAnimation == null) || !mLocalAnimating) {
182 return false;
183 }
184 mTransformation.clear();
185 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700186 if (DEBUG_ANIM) Slog.v(
187 TAG, "Stepped animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700188 ": more=" + more + ", xform=" + mTransformation);
189 return more;
190 }
191
192 // This must be called while inside a transaction. Returns true if
193 // there is more animation to run.
194 boolean stepAnimationLocked(long currentTime) {
195 // Save the animation state as it was before this step so WindowManagerService can tell if
196 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
197 mWasAnimating = mAnimating;
198 if (mService.okToDisplay()) {
199 // We will run animations as long as the display isn't frozen.
200
201 if (mWin.isDrawnLw() && mAnimation != null) {
202 mHasTransformation = true;
203 mHasLocalTransformation = true;
204 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700205 if (DEBUG_ANIM) Slog.v(
206 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700207 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
208 " wh=" + mWin.mFrame.height() +
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700209 " dw=" + mAnimDw + " dh=" + mAnimDh +
Craig Mautnera2c77052012-03-26 12:14:43 -0700210 " scale=" + mService.mWindowAnimationScale);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700211 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
212 mAnimDw, mAnimDh);
Craig Mautnera2c77052012-03-26 12:14:43 -0700213 mAnimation.setStartTime(currentTime);
214 mLocalAnimating = true;
215 mAnimating = true;
216 }
217 if ((mAnimation != null) && mLocalAnimating) {
218 if (stepAnimation(currentTime)) {
219 return true;
220 }
221 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700222 if (DEBUG_ANIM) Slog.v(
223 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700224 " @ " + currentTime);
225 //WindowManagerService.this.dump();
226 }
227 mHasLocalTransformation = false;
228 if ((!mLocalAnimating || mAnimationIsEntrance) && mWin.mAppToken != null
229 && mWin.mAppToken.animation != null) {
230 // When our app token is animating, we kind-of pretend like
231 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
232 // part of this check means that we will only do this if
233 // our window is not currently exiting, or it is not
234 // locally animating itself. The idea being that one that
235 // is exiting and doing a local animation should be removed
236 // once that animation is done.
237 mAnimating = true;
238 mHasTransformation = true;
239 mTransformation.clear();
240 return false;
241 } else if (mHasTransformation) {
242 // Little trick to get through the path below to act like
243 // we have finished an animation.
244 mAnimating = true;
245 } else if (isAnimating()) {
246 mAnimating = true;
247 }
248 } else if (mAnimation != null) {
249 // If the display is frozen, and there is a pending animation,
250 // clear it and make sure we run the cleanup code.
251 mAnimating = true;
252 mLocalAnimating = true;
253 mAnimation.cancel();
254 mAnimation = null;
255 }
256
257 if (!mAnimating && !mLocalAnimating) {
258 return false;
259 }
260
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700261 // Done animating, clean up.
262 if (DEBUG_ANIM) Slog.v(
263 TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
Craig Mautnera2c77052012-03-26 12:14:43 -0700264 + ", reportedVisible="
265 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
266
267 mAnimating = false;
268 mLocalAnimating = false;
269 if (mAnimation != null) {
270 mAnimation.cancel();
271 mAnimation = null;
272 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700273 if (mAnimator.mWindowDetachedWallpaper == mWin) {
274 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700275 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700276 mAnimLayer = mWin.mLayer;
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 if (mWin.mIsImWindow) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700278 mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700279 } else if (mWin.mIsWallpaper) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700280 mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700281 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700282 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
283 + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700284 mHasTransformation = false;
285 mHasLocalTransformation = false;
286 if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
287 if (WindowState.DEBUG_VISIBILITY) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700288 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
Craig Mautnera2c77052012-03-26 12:14:43 -0700289 + mWin.mPolicyVisibilityAfterAnim);
290 }
291 mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
292 mService.mLayoutNeeded = true;
293 if (!mWin.mPolicyVisibility) {
294 if (mService.mCurrentFocus == mWin) {
295 mService.mFocusMayChange = true;
296 }
297 // Window is no longer visible -- make sure if we were waiting
298 // for it to be displayed before enabling the display, that
299 // we allow the display to be enabled now.
300 mService.enableScreenIfNeededLocked();
301 }
302 }
303 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700304 if (mDrawState == HAS_DRAWN
Craig Mautnera2c77052012-03-26 12:14:43 -0700305 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
306 && mWin.mAppToken != null
307 && mWin.mAppToken.firstWindowDrawn
308 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700309 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700310 + mWin.mToken + ": first real window done animating");
311 mService.mFinishedStarting.add(mWin.mAppToken);
312 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
313 }
314
315 finishExit();
316 mService.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
317 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats("WindowState");
318
319 if (mWin.mAppToken != null) {
320 mWin.mAppToken.updateReportedVisibilityLocked();
321 }
322
323 return false;
324 }
325
326 void finishExit() {
327 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700328 TAG, "finishExit in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700329 + ": exiting=" + mWin.mExiting
330 + " remove=" + mWin.mRemoveOnExit
331 + " windowAnimating=" + isWindowAnimating());
332
333 final int N = mWin.mChildWindows.size();
334 for (int i=0; i<N; i++) {
335 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
336 }
337
338 if (!mWin.mExiting) {
339 return;
340 }
341
342 if (isWindowAnimating()) {
343 return;
344 }
345
346 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700347 TAG, "Exit animation finished in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700348 + ": remove=" + mWin.mRemoveOnExit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700349 if (mSurface != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700350 mService.mDestroySurface.add(mWin);
351 mWin.mDestroying = true;
352 if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
353 mWin, "HIDE (finishExit)", null);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700354 mSurfaceShown = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700355 try {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700356 mSurface.hide();
Craig Mautnera2c77052012-03-26 12:14:43 -0700357 } catch (RuntimeException e) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700358 Slog.w(TAG, "Error hiding surface in " + this, e);
Craig Mautnera2c77052012-03-26 12:14:43 -0700359 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700360 mLastHidden = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700361 }
362 mWin.mExiting = false;
363 if (mWin.mRemoveOnExit) {
364 mService.mPendingRemove.add(mWin);
365 mWin.mRemoveOnExit = false;
366 }
367 }
368
Craig Mautnera608b882012-03-30 13:03:49 -0700369 boolean finishDrawingLocked() {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700370 if (mDrawState == DRAW_PENDING) {
Craig Mautner48ba1e72012-04-02 13:18:16 -0700371 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
Craig Mautnera608b882012-03-30 13:03:49 -0700372 TAG, "finishDrawingLocked: " + this + " in " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700373 mDrawState = COMMIT_DRAW_PENDING;
Craig Mautnera608b882012-03-30 13:03:49 -0700374 return true;
375 }
376 return false;
377 }
378
379 // This must be called while inside a transaction.
380 boolean commitFinishDrawingLocked(long currentTime) {
381 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700382 if (mDrawState != COMMIT_DRAW_PENDING) {
Craig Mautnera608b882012-03-30 13:03:49 -0700383 return false;
384 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700385 mDrawState = READY_TO_SHOW;
Craig Mautnera608b882012-03-30 13:03:49 -0700386 final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
387 final AppWindowToken atoken = mWin.mAppToken;
388 if (atoken == null || atoken.allDrawn || starting) {
389 performShowLocked();
390 }
391 return true;
392 }
393
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700394 Surface createSurfaceLocked() {
395 if (mSurface == null) {
396 mReportDestroySurface = false;
397 mSurfacePendingDestroy = false;
Craig Mautner48ba1e72012-04-02 13:18:16 -0700398 if (DEBUG_ORIENTATION) Slog.i(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700399 "createSurface " + this + ": DRAW NOW PENDING");
Craig Mautner749a7bb2012-04-02 13:49:53 -0700400 mDrawState = DRAW_PENDING;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700401 if (mWin.mAppToken != null) {
402 mWin.mAppToken.allDrawn = false;
403 }
404
405 mService.makeWindowFreezingScreenIfNeededLocked(mWin);
406
407 int flags = 0;
408 final WindowManager.LayoutParams attrs = mWin.mAttrs;
409
410 if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
411 flags |= Surface.SECURE;
412 }
413 if (WindowState.DEBUG_VISIBILITY) Slog.v(
414 TAG, "Creating surface in session "
415 + mSession.mSurfaceSession + " window " + this
416 + " w=" + mWin.mCompatFrame.width()
417 + " h=" + mWin.mCompatFrame.height() + " format="
418 + attrs.format + " flags=" + flags);
419
420 int w = mWin.mCompatFrame.width();
421 int h = mWin.mCompatFrame.height();
422 if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
423 // for a scaled surface, we always want the requested
424 // size.
425 w = mWin.mRequestedWidth;
426 h = mWin.mRequestedHeight;
427 }
428
429 // Something is wrong and SurfaceFlinger will not like this,
430 // try to revert to sane values
431 if (w <= 0) w = 1;
432 if (h <= 0) h = 1;
433
434 mSurfaceShown = false;
435 mSurfaceLayer = 0;
436 mSurfaceAlpha = 1;
437 mSurfaceX = 0;
438 mSurfaceY = 0;
439 mSurfaceW = w;
440 mSurfaceH = h;
441 try {
442 final boolean isHwAccelerated = (attrs.flags &
443 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
444 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
445 if (!PixelFormat.formatHasAlpha(attrs.format)) {
446 flags |= Surface.OPAQUE;
447 }
448 mSurface = new Surface(
449 mSession.mSurfaceSession, mSession.mPid,
450 attrs.getTitle().toString(),
451 0, w, h, format, flags);
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700452 mWin.mHasSurface = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700453 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
454 " CREATE SURFACE "
455 + mSurface + " IN SESSION "
456 + mSession.mSurfaceSession
457 + ": pid=" + mSession.mPid + " format="
458 + attrs.format + " flags=0x"
459 + Integer.toHexString(flags)
460 + " / " + this);
461 } catch (Surface.OutOfResourcesException e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700462 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700463 Slog.w(TAG, "OutOfResourcesException creating surface");
464 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700465 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700466 return null;
467 } catch (Exception e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700468 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700469 Slog.e(TAG, "Exception creating surface", e);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700470 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700471 return null;
472 }
473
474 if (WindowManagerService.localLOGV) Slog.v(
475 TAG, "Got surface: " + mSurface
476 + ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
477 + ", animLayer=" + mAnimLayer);
478 if (SHOW_LIGHT_TRANSACTIONS) {
479 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
480 WindowManagerService.logSurface(mWin, "CREATE pos=("
481 + mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
482 + mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
483 + "), layer=" + mAnimLayer + " HIDE", null);
484 }
485 Surface.openTransaction();
486 try {
487 try {
488 mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
489 mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
490 mSurface.setPosition(mSurfaceX, mSurfaceY);
491 mSurfaceLayer = mAnimLayer;
492 mSurface.setLayer(mAnimLayer);
493 mSurfaceShown = false;
494 mSurface.hide();
495 if ((mWin.mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
496 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "DITHER", null);
497 mSurface.setFlags(Surface.SURFACE_DITHER, Surface.SURFACE_DITHER);
498 }
499 } catch (RuntimeException e) {
500 Slog.w(TAG, "Error creating surface in " + w, e);
501 mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
502 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700503 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700504 } finally {
505 Surface.closeTransaction();
506 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
507 "<<< CLOSE TRANSACTION createSurfaceLocked");
508 }
509 if (WindowManagerService.localLOGV) Slog.v(
510 TAG, "Created surface " + this);
511 }
512 return mSurface;
513 }
514
515 void destroySurfaceLocked() {
516 if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
517 mWin.mAppToken.startingDisplayed = false;
518 }
519
Craig Mautner749a7bb2012-04-02 13:49:53 -0700520 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700521 if (mSurface != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700522
523 int i = mWin.mChildWindows.size();
524 while (i > 0) {
525 i--;
526 WindowState c = mWin.mChildWindows.get(i);
527 c.mAttachedHidden = true;
528 }
529
530 if (mReportDestroySurface) {
531 mReportDestroySurface = false;
532 mSurfacePendingDestroy = true;
533 try {
534 mWin.mClient.dispatchGetNewSurface();
535 // We'll really destroy on the next time around.
536 return;
537 } catch (RemoteException e) {
538 }
539 }
540
541 try {
542 if (DEBUG_VISIBILITY) {
543 RuntimeException e = null;
544 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
545 e = new RuntimeException();
546 e.fillInStackTrace();
547 }
548 Slog.w(TAG, "Window " + this + " destroying surface "
549 + mSurface + ", session " + mSession, e);
550 }
551 if (mSurfaceDestroyDeferred) {
552 if (mSurface != null && mPendingDestroySurface != mSurface) {
553 if (mPendingDestroySurface != null) {
554 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
555 RuntimeException e = null;
556 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
557 e = new RuntimeException();
558 e.fillInStackTrace();
559 }
560 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
561 }
562 mPendingDestroySurface.destroy();
563 }
564 mPendingDestroySurface = mSurface;
565 }
566 } else {
567 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
568 RuntimeException e = null;
569 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
570 e = new RuntimeException();
571 e.fillInStackTrace();
572 }
573 WindowManagerService.logSurface(mWin, "DESTROY", e);
574 }
575 mSurface.destroy();
576 }
577 } catch (RuntimeException e) {
578 Slog.w(TAG, "Exception thrown when destroying Window " + this
579 + " surface " + mSurface + " session " + mSession
580 + ": " + e.toString());
581 }
582
583 mSurfaceShown = false;
584 mSurface = null;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700585 mWin.mHasSurface =false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700586 }
587 }
588
589 void destroyDeferredSurfaceLocked() {
590 try {
591 if (mPendingDestroySurface != null) {
592 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
593 RuntimeException e = null;
594 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
595 e = new RuntimeException();
596 e.fillInStackTrace();
597 }
598 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
599 }
600 mPendingDestroySurface.destroy();
601 }
602 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700603 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700604 + this + " surface " + mPendingDestroySurface
605 + " session " + mSession + ": " + e.toString());
606 }
607 mSurfaceDestroyDeferred = false;
608 mPendingDestroySurface = null;
609 }
610
611 void computeShownFrameLocked() {
612 final boolean selfTransformation = mHasLocalTransformation;
613 Transformation attachedTransformation =
614 (mAttachedWindow != null && mAttachedWindow.mWinAnimator.mHasLocalTransformation)
615 ? mAttachedWindow.mWinAnimator.mTransformation : null;
616 Transformation appTransformation =
617 (mWin.mAppToken != null && mWin.mAppToken.hasTransformation)
618 ? mWin.mAppToken.transformation : null;
619
620 // Wallpapers are animated based on the "real" window they
621 // are currently targeting.
622 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
623 && mService.mWallpaperTarget != null) {
624 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
625 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
626 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
627 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
628 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
629 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
630 }
631 }
632 if (mService.mWallpaperTarget.mAppToken != null &&
633 mService.mWallpaperTarget.mAppToken.hasTransformation &&
634 mService.mWallpaperTarget.mAppToken.animation != null &&
635 !mService.mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
636 appTransformation = mService.mWallpaperTarget.mAppToken.transformation;
637 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
638 Slog.v(TAG, "WP target app xform: " + appTransformation);
639 }
640 }
641 }
642
643 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
644 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
645 if (selfTransformation || attachedTransformation != null
646 || appTransformation != null || screenAnimation) {
647 // cache often used attributes locally
648 final Rect frame = mWin.mFrame;
649 final float tmpFloats[] = mService.mTmpFloats;
650 final Matrix tmpMatrix = mWin.mTmpMatrix;
651
652 // Compute the desired transformation.
653 if (screenAnimation) {
654 // If we are doing a screen animation, the global rotation
655 // applied to windows can result in windows that are carefully
656 // aligned with each other to slightly separate, allowing you
657 // to see what is behind them. An unsightly mess. This...
658 // thing... magically makes it call good: scale each window
659 // slightly (two pixels larger in each dimension, from the
660 // window's center).
661 final float w = frame.width();
662 final float h = frame.height();
663 if (w>=1 && h>=1) {
664 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
665 } else {
666 tmpMatrix.reset();
667 }
668 } else {
669 tmpMatrix.reset();
670 }
671 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
672 if (selfTransformation) {
673 tmpMatrix.postConcat(mTransformation.getMatrix());
674 }
675 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
676 if (attachedTransformation != null) {
677 tmpMatrix.postConcat(attachedTransformation.getMatrix());
678 }
679 if (appTransformation != null) {
680 tmpMatrix.postConcat(appTransformation.getMatrix());
681 }
682 if (screenAnimation) {
683 tmpMatrix.postConcat(
684 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
685 }
686
687 // "convert" it into SurfaceFlinger's format
688 // (a 2x2 matrix + an offset)
689 // Here we must not transform the position of the surface
690 // since it is already included in the transformation.
691 //Slog.i(TAG, "Transform: " + matrix);
692
693 mHaveMatrix = true;
694 tmpMatrix.getValues(tmpFloats);
695 mDsDx = tmpFloats[Matrix.MSCALE_X];
696 mDtDx = tmpFloats[Matrix.MSKEW_Y];
697 mDsDy = tmpFloats[Matrix.MSKEW_X];
698 mDtDy = tmpFloats[Matrix.MSCALE_Y];
699 float x = tmpFloats[Matrix.MTRANS_X];
700 float y = tmpFloats[Matrix.MTRANS_Y];
701 int w = frame.width();
702 int h = frame.height();
703 mWin.mShownFrame.set(x, y, x+w, y+h);
704
705 // Now set the alpha... but because our current hardware
706 // can't do alpha transformation on a non-opaque surface,
707 // turn it off if we are running an animation that is also
708 // transforming since it is more important to have that
709 // animation be smooth.
710 mShownAlpha = mAlpha;
711 if (!mService.mLimitedAlphaCompositing
712 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
713 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
714 && x == frame.left && y == frame.top))) {
715 //Slog.i(TAG, "Applying alpha transform");
716 if (selfTransformation) {
717 mShownAlpha *= mTransformation.getAlpha();
718 }
719 if (attachedTransformation != null) {
720 mShownAlpha *= attachedTransformation.getAlpha();
721 }
722 if (appTransformation != null) {
723 mShownAlpha *= appTransformation.getAlpha();
724 }
725 if (screenAnimation) {
726 mShownAlpha *=
727 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
728 }
729 } else {
730 //Slog.i(TAG, "Not applying alpha transform");
731 }
732
733 if (WindowManagerService.localLOGV) Slog.v(
734 TAG, "computeShownFrameLocked: Animating " + this +
735 ": " + mWin.mShownFrame +
736 ", alpha=" + mTransformation.getAlpha() + ", mShownAlpha=" + mShownAlpha);
737 return;
738 }
739
740 if (WindowManagerService.localLOGV) Slog.v(
741 TAG, "computeShownFrameLocked: " + this +
742 " not attached, mAlpha=" + mAlpha);
743 mWin.mShownFrame.set(mWin.mFrame);
744 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
745 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
746 }
747 mShownAlpha = mAlpha;
748 mHaveMatrix = false;
749 mDsDx = mWin.mGlobalScale;
750 mDtDx = 0;
751 mDsDy = 0;
752 mDtDy = mWin.mGlobalScale;
753 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700754
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700755 public void prepareSurfaceLocked(final boolean recoveringMemory) {
756 final WindowState w = mWin;
757 if (mSurface == null) {
758 if (w.mOrientationChanging) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700759 if (DEBUG_ORIENTATION) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700760 Slog.v(TAG, "Orientation change skips hidden " + w);
761 }
762 w.mOrientationChanging = false;
763 }
764 return;
765 }
766
767 boolean displayed = false;
768
769 computeShownFrameLocked();
770
771 int width, height;
772 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
773 // for a scaled surface, we just want to use
774 // the requested size.
775 width = w.mRequestedWidth;
776 height = w.mRequestedHeight;
777 } else {
778 width = w.mCompatFrame.width();
779 height = w.mCompatFrame.height();
780 }
781
782 if (width < 1) {
783 width = 1;
784 }
785 if (height < 1) {
786 height = 1;
787 }
788 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
789 if (surfaceResized) {
790 mSurfaceW = width;
791 mSurfaceH = height;
792 }
793
794 if (mSurfaceX != w.mShownFrame.left
795 || mSurfaceY != w.mShownFrame.top) {
796 try {
797 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
798 "POS " + w.mShownFrame.left
799 + ", " + w.mShownFrame.top, null);
800 mSurfaceX = w.mShownFrame.left;
801 mSurfaceY = w.mShownFrame.top;
802 mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
803 } catch (RuntimeException e) {
804 Slog.w(TAG, "Error positioning surface of " + w
805 + " pos=(" + w.mShownFrame.left
806 + "," + w.mShownFrame.top + ")", e);
807 if (!recoveringMemory) {
808 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
809 }
810 }
811 }
812
813 if (surfaceResized) {
814 try {
815 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
816 "SIZE " + width + "x" + height, null);
817 mSurfaceResized = true;
818 mSurface.setSize(width, height);
819 } catch (RuntimeException e) {
820 // If something goes wrong with the surface (such
821 // as running out of memory), don't take down the
822 // entire system.
823 Slog.e(TAG, "Error resizing surface of " + w
824 + " size=(" + width + "x" + height + ")", e);
825 if (!recoveringMemory) {
826 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
827 }
828 }
829 }
830
831 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700832 if (!mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700833 //dump();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700834 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700835 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
836 "HIDE (performLayout)", null);
837 if (mSurface != null) {
838 mSurfaceShown = false;
839 try {
840 mSurface.hide();
841 } catch (RuntimeException e) {
842 Slog.w(TAG, "Exception hiding surface in " + w);
843 }
844 }
845 }
846 // If we are waiting for this window to handle an
847 // orientation change, well, it is hidden, so
848 // doesn't really matter. Note that this does
849 // introduce a potential glitch if the window
850 // becomes unhidden before it has drawn for the
851 // new orientation.
852 if (w.mOrientationChanging) {
853 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700854 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700855 "Orientation change skips hidden " + w);
856 }
857 } else if (mLastLayer != mAnimLayer
858 || mLastAlpha != mShownAlpha
859 || mLastDsDx != mDsDx
860 || mLastDtDx != mDtDx
861 || mLastDsDy != mDsDy
862 || mLastDtDy != mDtDy
863 || w.mLastHScale != w.mHScale
864 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -0700865 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700866 displayed = true;
867 mLastAlpha = mShownAlpha;
868 mLastLayer = mAnimLayer;
869 mLastDsDx = mDsDx;
870 mLastDtDx = mDtDx;
871 mLastDsDy = mDsDy;
872 mLastDtDy = mDtDy;
873 w.mLastHScale = w.mHScale;
874 w.mLastVScale = w.mVScale;
875 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
876 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
877 + " matrix=[" + (mDsDx*w.mHScale)
878 + "," + (mDtDx*w.mVScale)
879 + "][" + (mDsDy*w.mHScale)
880 + "," + (mDtDy*w.mVScale) + "]", null);
881 if (mSurface != null) {
882 try {
883 mSurfaceAlpha = mShownAlpha;
884 mSurface.setAlpha(mShownAlpha);
885 mSurfaceLayer = w.mWinAnimator.mAnimLayer;
886 mSurface.setLayer(w.mWinAnimator.mAnimLayer);
887 mSurface.setMatrix(
888 mDsDx*w.mHScale, mDtDx*w.mVScale,
889 mDsDy*w.mHScale, mDtDy*w.mVScale);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700890
891 if (mLastHidden && mDrawState == HAS_DRAWN) {
892 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
893 "SHOW (performLayout)", null);
894 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
895 + " during relayout");
896 if (showSurfaceRobustlyLocked()) {
897 mLastHidden = false;
898 } else {
899 w.mOrientationChanging = false;
900 }
901 }
902 if (mSurface != null) {
903 w.mToken.hasVisible = true;
904 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700905 } catch (RuntimeException e) {
906 Slog.w(TAG, "Error updating surface in " + w, e);
907 if (!recoveringMemory) {
908 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
909 }
910 }
911 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700912 } else {
913 displayed = true;
914 }
915
916 if (displayed) {
917 if (w.mOrientationChanging) {
918 if (!w.isDrawnLw()) {
919 mService.mInnerFields.mOrientationChangeComplete = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700920 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700921 "Orientation continue waiting for draw in " + w);
922 } else {
923 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700924 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700925 }
926 }
927 w.mToken.hasVisible = true;
928 }
929 }
930
Craig Mautner48ba1e72012-04-02 13:18:16 -0700931 void setTransparentRegionHint(final Region region) {
932 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
933 ">>> OPEN TRANSACTION setTransparentRegion");
934 Surface.openTransaction();
935 try {
936 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
937 "transparentRegionHint=" + region, null);
938 mSurface.setTransparentRegionHint(region);
939 } finally {
940 Surface.closeTransaction();
941 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
942 "<<< CLOSE TRANSACTION setTransparentRegion");
943 }
944 }
945
946 void setWallpaperOffset(int left, int top) {
947 Surface.openTransaction();
948 try {
949 mSurfaceX = left;
950 mSurfaceY = top;
951 mSurface.setPosition(left, top);
952 } catch (RuntimeException e) {
953 Slog.w(TAG, "Error positioning surface of " + mWin
954 + " pos=(" + left + "," + top + ")", e);
955 }
956 Surface.closeTransaction();
957 }
958
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700959 // This must be called while inside a transaction.
960 boolean performShowLocked() {
961 if (DEBUG_VISIBILITY) {
962 RuntimeException e = null;
963 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
964 e = new RuntimeException();
965 e.fillInStackTrace();
966 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700967 Slog.v(TAG, "performShow on " + this
Craig Mautner749a7bb2012-04-02 13:49:53 -0700968 + ": mDrawState=" + mDrawState + " readyForDisplay="
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700969 + mWin.isReadyForDisplay()
970 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
971 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700972 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplay()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700973 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700974 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -0700975 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700976 + " during animation: policyVis=" + mWin.mPolicyVisibility
977 + " attHidden=" + mWin.mAttachedHidden
978 + " tok.hiddenRequested="
979 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
980 + " tok.hidden="
981 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
982 + " animating=" + mAnimating
983 + " tok animating="
984 + (mWin.mAppToken != null ? mWin.mAppToken.animating : false));
985 if (!showSurfaceRobustlyLocked()) {
986 return false;
987 }
988
989 mService.enableScreenIfNeededLocked();
990
991 applyEnterAnimationLocked();
992
993 mLastAlpha = -1;
Craig Mautner749a7bb2012-04-02 13:49:53 -0700994 mLastHidden = false;
995 mDrawState = HAS_DRAWN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700996
997 int i = mWin.mChildWindows.size();
998 while (i > 0) {
999 i--;
1000 WindowState c = mWin.mChildWindows.get(i);
1001 if (c.mAttachedHidden) {
1002 c.mAttachedHidden = false;
1003 if (c.mWinAnimator.mSurface != null) {
1004 c.mWinAnimator.performShowLocked();
1005 // It hadn't been shown, which means layout not
1006 // performed on it, so now we want to make sure to
1007 // do a layout. If called from within the transaction
1008 // loop, this will cause it to restart with a new
1009 // layout.
1010 mService.mLayoutNeeded = true;
1011 }
1012 }
1013 }
1014
1015 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1016 && mWin.mAppToken != null) {
1017 mWin.mAppToken.firstWindowDrawn = true;
1018
1019 if (mWin.mAppToken.startingData != null) {
1020 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001021 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001022 "Finish starting " + mWin.mToken
1023 + ": first real window is shown, no animation");
1024 // If this initial window is animating, stop it -- we
1025 // will do an animation to reveal it from behind the
1026 // starting window, so there is no need for it to also
1027 // be doing its own stuff.
1028 if (mAnimation != null) {
1029 mAnimation.cancel();
1030 mAnimation = null;
1031 // Make sure we clean up the animation.
1032 mAnimating = true;
1033 }
1034 mService.mFinishedStarting.add(mWin.mAppToken);
1035 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1036 }
1037 mWin.mAppToken.updateReportedVisibilityLocked();
1038 }
1039
1040 return true;
1041 }
1042
1043 return false;
1044 }
1045
1046 /**
1047 * Have the surface flinger show a surface, robustly dealing with
1048 * error conditions. In particular, if there is not enough memory
1049 * to show the surface, then we will try to get rid of other surfaces
1050 * in order to succeed.
1051 *
1052 * @return Returns true if the surface was successfully shown.
1053 */
1054 boolean showSurfaceRobustlyLocked() {
1055 try {
1056 if (mSurface != null) {
1057 mSurfaceShown = true;
1058 mSurface.show();
1059 if (mWin.mTurnOnScreen) {
1060 if (DEBUG_VISIBILITY) Slog.v(TAG,
1061 "Show surface turning screen on: " + mWin);
1062 mWin.mTurnOnScreen = false;
1063 mService.mTurnOnScreen = true;
1064 }
1065 }
1066 return true;
1067 } catch (RuntimeException e) {
1068 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1069 }
1070
1071 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1072
1073 return false;
1074 }
1075
1076 void applyEnterAnimationLocked() {
1077 final int transit;
1078 if (mEnterAnimationPending) {
1079 mEnterAnimationPending = false;
1080 transit = WindowManagerPolicy.TRANSIT_ENTER;
1081 } else {
1082 transit = WindowManagerPolicy.TRANSIT_SHOW;
1083 }
1084
1085 applyAnimationLocked(transit, true);
1086 }
1087
Craig Mautner48ba1e72012-04-02 13:18:16 -07001088 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001089 /**
1090 * Choose the correct animation and set it to the passed WindowState.
1091 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1092 * then the animation will be app_starting_exit. Any other value loads the animation from
1093 * the switch statement below.
1094 * @param isEntrance The animation type the last time this was called. Used to keep from
1095 * loading the same animation twice.
1096 * @return true if an animation has been loaded.
1097 */
1098 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1099 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1100 // If we are trying to apply an animation, but already running
1101 // an animation of the same type, then just leave that one alone.
1102 return true;
1103 }
1104
1105 // Only apply an animation if the display isn't frozen. If it is
1106 // frozen, there is no reason to animate and it can cause strange
1107 // artifacts when we unfreeze the display if some different animation
1108 // is running.
1109 if (mService.okToDisplay()) {
1110 int anim = mPolicy.selectAnimationLw(mWin, transit);
1111 int attr = -1;
1112 Animation a = null;
1113 if (anim != 0) {
1114 a = AnimationUtils.loadAnimation(mContext, anim);
1115 } else {
1116 switch (transit) {
1117 case WindowManagerPolicy.TRANSIT_ENTER:
1118 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1119 break;
1120 case WindowManagerPolicy.TRANSIT_EXIT:
1121 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1122 break;
1123 case WindowManagerPolicy.TRANSIT_SHOW:
1124 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1125 break;
1126 case WindowManagerPolicy.TRANSIT_HIDE:
1127 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1128 break;
1129 }
1130 if (attr >= 0) {
1131 a = mService.loadAnimation(mWin.mAttrs, attr);
1132 }
1133 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001134 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001135 "applyAnimation: win=" + this
1136 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1137 + " mAnimation=" + mAnimation
1138 + " isEntrance=" + isEntrance);
1139 if (a != null) {
1140 if (WindowManagerService.DEBUG_ANIM) {
1141 RuntimeException e = null;
1142 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1143 e = new RuntimeException();
1144 e.fillInStackTrace();
1145 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001146 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001147 }
1148 setAnimation(a);
1149 mAnimationIsEntrance = isEntrance;
1150 }
1151 } else {
1152 clearAnimation();
1153 }
1154
1155 return mAnimation != null;
1156 }
1157
Craig Mautnera2c77052012-03-26 12:14:43 -07001158 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1159 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1160 || mAnimation != null) {
1161 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1162 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1163 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1164 pw.print(" mAnimation="); pw.println(mAnimation);
1165 }
1166 if (mHasTransformation || mHasLocalTransformation) {
1167 pw.print(prefix); pw.print("XForm: has=");
1168 pw.print(mHasTransformation);
1169 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1170 pw.print(" "); mTransformation.printShortString(pw);
1171 pw.println();
1172 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001173 if (mSurface != null) {
1174 if (dumpAll) {
1175 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001176 pw.print(prefix); pw.print("mDrawState="); pw.print(mDrawState);
1177 pw.print(" mLastHidden="); pw.println(mLastHidden);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001178 }
1179 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1180 pw.print(" layer="); pw.print(mSurfaceLayer);
1181 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1182 pw.print(" rect=("); pw.print(mSurfaceX);
1183 pw.print(","); pw.print(mSurfaceY);
1184 pw.print(") "); pw.print(mSurfaceW);
1185 pw.print(" x "); pw.println(mSurfaceH);
1186 }
1187 if (mPendingDestroySurface != null) {
1188 pw.print(prefix); pw.print("mPendingDestroySurface=");
1189 pw.println(mPendingDestroySurface);
1190 }
1191 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1192 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1193 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1194 }
1195 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1196 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1197 pw.print(" mAlpha="); pw.print(mAlpha);
1198 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1199 }
1200 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1201 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1202 pw.print(" mDsDx="); pw.print(mDsDx);
1203 pw.print(" mDtDx="); pw.print(mDtDx);
1204 pw.print(" mDsDy="); pw.print(mDsDy);
1205 pw.print(" mDtDy="); pw.println(mDtDy);
1206 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001207 }
1208
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001209 @Override
1210 public String toString() {
1211 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1212 sb.append(mWin.mLastTitle + "): ");
1213 sb.append("mSurface " + mSurface);
1214 sb.append(", mAnimation " + mAnimation);
1215 return sb.toString();
1216 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001217}