blob: 6d0921e7a7f0a0a72c2c621e9103a449c8ca6dee [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
Craig Mautnerd09cc4b2012-04-04 10:23:31 -07008import static com.android.server.wm.WindowManagerService.LayoutFields.CLEAR_ORIENTATION_CHANGE_COMPLETE;
9
Craig Mautnerc2f9be02012-03-27 17:32:29 -070010import android.content.Context;
11import android.graphics.Matrix;
12import android.graphics.PixelFormat;
13import android.graphics.Rect;
Craig Mautner48ba1e72012-04-02 13:18:16 -070014import android.graphics.Region;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070015import android.os.RemoteException;
Craig Mautnera2c77052012-03-26 12:14:43 -070016import android.util.Slog;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070017import android.view.Surface;
Craig Mautnera2c77052012-03-26 12:14:43 -070018import android.view.WindowManager;
19import android.view.WindowManagerPolicy;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070020import android.view.WindowManager.LayoutParams;
Craig Mautnera2c77052012-03-26 12:14:43 -070021import android.view.animation.Animation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070022import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070023import android.view.animation.Transformation;
24
25import com.android.server.wm.WindowManagerService.H;
26
27import java.io.PrintWriter;
28
29/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070030 * Keep track of animations and surface operations for a single WindowState.
31 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070032class WindowStateAnimator {
Craig Mautnerc2f9be02012-03-27 17:32:29 -070033 static final boolean DEBUG_VISIBILITY = WindowManagerService.DEBUG_VISIBILITY;
34 static final boolean DEBUG_ANIM = WindowManagerService.DEBUG_ANIM;
35 static final boolean DEBUG_LAYERS = WindowManagerService.DEBUG_LAYERS;
36 static final boolean DEBUG_STARTING_WINDOW = WindowManagerService.DEBUG_STARTING_WINDOW;
37 static final boolean SHOW_TRANSACTIONS = WindowManagerService.SHOW_TRANSACTIONS;
38 static final boolean SHOW_LIGHT_TRANSACTIONS = WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
39 static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
40 static final boolean localLOGV = WindowManagerService.localLOGV;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -070041 static final boolean DEBUG_ORIENTATION = WindowManagerService.DEBUG_ORIENTATION;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070042
43 static final String TAG = "WindowStateAnimator";
Craig Mautnera2c77052012-03-26 12:14:43 -070044
45 final WindowManagerService mService;
46 final WindowState mWin;
47 final WindowState mAttachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -070048 final WindowAnimator mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070049 final Session mSession;
50 final WindowManagerPolicy mPolicy;
51 final Context mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -070052
53 // Currently running animation.
54 boolean mAnimating;
55 boolean mLocalAnimating;
56 Animation mAnimation;
57 boolean mAnimationIsEntrance;
58 boolean mHasTransformation;
59 boolean mHasLocalTransformation;
60 final Transformation mTransformation = new Transformation();
61 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -070062 int mAnimLayer;
63 int mLastLayer;
64
65 Surface mSurface;
66 Surface mPendingDestroySurface;
67 boolean mReportDestroySurface;
68 boolean mSurfacePendingDestroy;
69
70 /**
71 * Set when we have changed the size of the surface, to know that
72 * we must tell them application to resize (and thus redraw itself).
73 */
74 boolean mSurfaceResized;
75
76 /**
77 * Set if the client has asked that the destroy of its surface be delayed
78 * until it explicitly says it is okay.
79 */
80 boolean mSurfaceDestroyDeferred;
81
82 float mShownAlpha = 1;
83 float mAlpha = 1;
84 float mLastAlpha = 1;
85
86 // Used to save animation distances between the time they are calculated and when they are
87 // used.
88 int mAnimDw;
89 int mAnimDh;
90 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
91 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
92
93 boolean mHaveMatrix;
94
95 // For debugging, this is the last information given to the surface flinger.
96 boolean mSurfaceShown;
97 float mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
98 int mSurfaceLayer;
99 float mSurfaceAlpha;
100
101 // Set to true if, when the window gets displayed, it should perform
102 // an enter animation.
103 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700104
Craig Mautner749a7bb2012-04-02 13:49:53 -0700105 /** This is set when there is no Surface */
106 static final int NO_SURFACE = 0;
107 /** This is set after the Surface has been created but before the window has been drawn. During
108 * this time the surface is hidden. */
109 static final int DRAW_PENDING = 1;
110 /** This is set after the window has finished drawing for the first time but before its surface
111 * is shown. The surface will be displayed when the next layout is run. */
112 static final int COMMIT_DRAW_PENDING = 2;
113 /** This is set during the time after the window's drawing has been committed, and before its
114 * surface is actually shown. It is used to delay showing the surface until all windows in a
115 * token are ready to be shown. */
116 static final int READY_TO_SHOW = 3;
117 /** Set when the window has been shown in the screen the first time. */
118 static final int HAS_DRAWN = 4;
119 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700120
Craig Mautner749a7bb2012-04-02 13:49:53 -0700121 /** Was this window last hidden? */
122 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700123
Craig Mautnera2c77052012-03-26 12:14:43 -0700124 public WindowStateAnimator(final WindowManagerService service, final WindowState win,
125 final WindowState attachedWindow) {
126 mService = service;
127 mWin = win;
128 mAttachedWindow = attachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700129 mAnimator = mService.mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700130 mSession = win.mSession;
131 mPolicy = mService.mPolicy;
132 mContext = mService.mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -0700133 }
134
135 public void setAnimation(Animation anim) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700136 if (localLOGV) Slog.v(
137 TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700138 mAnimating = false;
139 mLocalAnimating = false;
140 mAnimation = anim;
141 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
142 mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
143 // Start out animation gone if window is gone, or visible if window is visible.
144 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700145 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700146 mHasLocalTransformation = true;
147 }
148
149 public void clearAnimation() {
150 if (mAnimation != null) {
151 mAnimating = true;
152 mLocalAnimating = false;
153 mAnimation.cancel();
154 mAnimation = null;
155 }
156 }
157
158 /** Is the window or its container currently animating? */
159 boolean isAnimating() {
160 final WindowState attached = mAttachedWindow;
161 final AppWindowToken atoken = mWin.mAppToken;
162 return mAnimation != null
163 || (attached != null && attached.mWinAnimator.mAnimation != null)
164 || (atoken != null &&
Craig Mautner59431632012-04-04 11:56:44 -0700165 (atoken.mAppAnimator.animation != null
Craig Mautnera2c77052012-03-26 12:14:43 -0700166 || atoken.inPendingTransaction));
167 }
168
169 /** Is this window currently animating? */
170 boolean isWindowAnimating() {
171 return mAnimation != null;
172 }
173
Craig Mautnera2c77052012-03-26 12:14:43 -0700174 void cancelExitAnimationForNextAnimationLocked() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700175 if (mAnimation != null) {
176 mAnimation.cancel();
177 mAnimation = null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700178 destroySurfaceLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700179 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700180 }
181
182 private boolean stepAnimation(long currentTime) {
183 if ((mAnimation == null) || !mLocalAnimating) {
184 return false;
185 }
186 mTransformation.clear();
187 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700188 if (DEBUG_ANIM) Slog.v(
189 TAG, "Stepped animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700190 ": more=" + more + ", xform=" + mTransformation);
191 return more;
192 }
193
194 // This must be called while inside a transaction. Returns true if
195 // there is more animation to run.
196 boolean stepAnimationLocked(long currentTime) {
197 // Save the animation state as it was before this step so WindowManagerService can tell if
198 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
199 mWasAnimating = mAnimating;
200 if (mService.okToDisplay()) {
201 // We will run animations as long as the display isn't frozen.
202
203 if (mWin.isDrawnLw() && mAnimation != null) {
204 mHasTransformation = true;
205 mHasLocalTransformation = true;
206 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700207 if (DEBUG_ANIM) Slog.v(
208 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700209 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
210 " wh=" + mWin.mFrame.height() +
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700211 " dw=" + mAnimDw + " dh=" + mAnimDh +
Craig Mautnera2c77052012-03-26 12:14:43 -0700212 " scale=" + mService.mWindowAnimationScale);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700213 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
214 mAnimDw, mAnimDh);
Craig Mautnera2c77052012-03-26 12:14:43 -0700215 mAnimation.setStartTime(currentTime);
216 mLocalAnimating = true;
217 mAnimating = true;
218 }
219 if ((mAnimation != null) && mLocalAnimating) {
220 if (stepAnimation(currentTime)) {
221 return true;
222 }
223 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700224 if (DEBUG_ANIM) Slog.v(
225 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700226 " @ " + currentTime);
227 //WindowManagerService.this.dump();
228 }
229 mHasLocalTransformation = false;
230 if ((!mLocalAnimating || mAnimationIsEntrance) && mWin.mAppToken != null
Craig Mautner59431632012-04-04 11:56:44 -0700231 && mWin.mAppToken.mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700232 // When our app token is animating, we kind-of pretend like
233 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
234 // part of this check means that we will only do this if
235 // our window is not currently exiting, or it is not
236 // locally animating itself. The idea being that one that
237 // is exiting and doing a local animation should be removed
238 // once that animation is done.
239 mAnimating = true;
240 mHasTransformation = true;
241 mTransformation.clear();
242 return false;
243 } else if (mHasTransformation) {
244 // Little trick to get through the path below to act like
245 // we have finished an animation.
246 mAnimating = true;
247 } else if (isAnimating()) {
248 mAnimating = true;
249 }
250 } else if (mAnimation != null) {
251 // If the display is frozen, and there is a pending animation,
252 // clear it and make sure we run the cleanup code.
253 mAnimating = true;
254 mLocalAnimating = true;
255 mAnimation.cancel();
256 mAnimation = null;
257 }
258
259 if (!mAnimating && !mLocalAnimating) {
260 return false;
261 }
262
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700263 // Done animating, clean up.
264 if (DEBUG_ANIM) Slog.v(
265 TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
Craig Mautnera2c77052012-03-26 12:14:43 -0700266 + ", reportedVisible="
267 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
268
269 mAnimating = false;
270 mLocalAnimating = false;
271 if (mAnimation != null) {
272 mAnimation.cancel();
273 mAnimation = null;
274 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700275 if (mAnimator.mWindowDetachedWallpaper == mWin) {
276 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700278 mAnimLayer = mWin.mLayer;
Craig Mautnera2c77052012-03-26 12:14:43 -0700279 if (mWin.mIsImWindow) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700280 mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700281 } else if (mWin.mIsWallpaper) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700282 mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700283 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700284 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
285 + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700286 mHasTransformation = false;
287 mHasLocalTransformation = false;
288 if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
289 if (WindowState.DEBUG_VISIBILITY) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700290 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
Craig Mautnera2c77052012-03-26 12:14:43 -0700291 + mWin.mPolicyVisibilityAfterAnim);
292 }
293 mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
294 mService.mLayoutNeeded = true;
295 if (!mWin.mPolicyVisibility) {
296 if (mService.mCurrentFocus == mWin) {
297 mService.mFocusMayChange = true;
298 }
299 // Window is no longer visible -- make sure if we were waiting
300 // for it to be displayed before enabling the display, that
301 // we allow the display to be enabled now.
302 mService.enableScreenIfNeededLocked();
303 }
304 }
305 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700306 if (mDrawState == HAS_DRAWN
Craig Mautnera2c77052012-03-26 12:14:43 -0700307 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
308 && mWin.mAppToken != null
309 && mWin.mAppToken.firstWindowDrawn
310 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700311 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700312 + mWin.mToken + ": first real window done animating");
313 mService.mFinishedStarting.add(mWin.mAppToken);
314 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
315 }
316
317 finishExit();
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700318 mAnimator.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
319 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
320 "WindowStateAnimator", mAnimator.mPendingLayoutChanges);
Craig Mautnera2c77052012-03-26 12:14:43 -0700321
322 if (mWin.mAppToken != null) {
323 mWin.mAppToken.updateReportedVisibilityLocked();
324 }
325
326 return false;
327 }
328
329 void finishExit() {
330 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700331 TAG, "finishExit in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700332 + ": exiting=" + mWin.mExiting
333 + " remove=" + mWin.mRemoveOnExit
334 + " windowAnimating=" + isWindowAnimating());
335
336 final int N = mWin.mChildWindows.size();
337 for (int i=0; i<N; i++) {
338 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
339 }
340
341 if (!mWin.mExiting) {
342 return;
343 }
344
345 if (isWindowAnimating()) {
346 return;
347 }
348
349 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700350 TAG, "Exit animation finished in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700351 + ": remove=" + mWin.mRemoveOnExit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700352 if (mSurface != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700353 mService.mDestroySurface.add(mWin);
354 mWin.mDestroying = true;
355 if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
356 mWin, "HIDE (finishExit)", null);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700357 mSurfaceShown = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700358 try {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700359 mSurface.hide();
Craig Mautnera2c77052012-03-26 12:14:43 -0700360 } catch (RuntimeException e) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700361 Slog.w(TAG, "Error hiding surface in " + this, e);
Craig Mautnera2c77052012-03-26 12:14:43 -0700362 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700363 mLastHidden = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700364 }
365 mWin.mExiting = false;
366 if (mWin.mRemoveOnExit) {
367 mService.mPendingRemove.add(mWin);
368 mWin.mRemoveOnExit = false;
369 }
370 }
371
Craig Mautnera608b882012-03-30 13:03:49 -0700372 boolean finishDrawingLocked() {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700373 if (mDrawState == DRAW_PENDING) {
Craig Mautner48ba1e72012-04-02 13:18:16 -0700374 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
Craig Mautnera608b882012-03-30 13:03:49 -0700375 TAG, "finishDrawingLocked: " + this + " in " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700376 mDrawState = COMMIT_DRAW_PENDING;
Craig Mautnera608b882012-03-30 13:03:49 -0700377 return true;
378 }
379 return false;
380 }
381
382 // This must be called while inside a transaction.
383 boolean commitFinishDrawingLocked(long currentTime) {
384 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700385 if (mDrawState != COMMIT_DRAW_PENDING) {
Craig Mautnera608b882012-03-30 13:03:49 -0700386 return false;
387 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700388 mDrawState = READY_TO_SHOW;
Craig Mautnera608b882012-03-30 13:03:49 -0700389 final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
390 final AppWindowToken atoken = mWin.mAppToken;
391 if (atoken == null || atoken.allDrawn || starting) {
392 performShowLocked();
393 }
394 return true;
395 }
396
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700397 Surface createSurfaceLocked() {
398 if (mSurface == null) {
399 mReportDestroySurface = false;
400 mSurfacePendingDestroy = false;
Craig Mautner48ba1e72012-04-02 13:18:16 -0700401 if (DEBUG_ORIENTATION) Slog.i(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700402 "createSurface " + this + ": DRAW NOW PENDING");
Craig Mautner749a7bb2012-04-02 13:49:53 -0700403 mDrawState = DRAW_PENDING;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700404 if (mWin.mAppToken != null) {
405 mWin.mAppToken.allDrawn = false;
406 }
407
408 mService.makeWindowFreezingScreenIfNeededLocked(mWin);
409
410 int flags = 0;
411 final WindowManager.LayoutParams attrs = mWin.mAttrs;
412
413 if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
414 flags |= Surface.SECURE;
415 }
416 if (WindowState.DEBUG_VISIBILITY) Slog.v(
417 TAG, "Creating surface in session "
418 + mSession.mSurfaceSession + " window " + this
419 + " w=" + mWin.mCompatFrame.width()
420 + " h=" + mWin.mCompatFrame.height() + " format="
421 + attrs.format + " flags=" + flags);
422
423 int w = mWin.mCompatFrame.width();
424 int h = mWin.mCompatFrame.height();
425 if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
426 // for a scaled surface, we always want the requested
427 // size.
428 w = mWin.mRequestedWidth;
429 h = mWin.mRequestedHeight;
430 }
431
432 // Something is wrong and SurfaceFlinger will not like this,
433 // try to revert to sane values
434 if (w <= 0) w = 1;
435 if (h <= 0) h = 1;
436
437 mSurfaceShown = false;
438 mSurfaceLayer = 0;
439 mSurfaceAlpha = 1;
440 mSurfaceX = 0;
441 mSurfaceY = 0;
442 mSurfaceW = w;
443 mSurfaceH = h;
444 try {
445 final boolean isHwAccelerated = (attrs.flags &
446 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
447 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
448 if (!PixelFormat.formatHasAlpha(attrs.format)) {
449 flags |= Surface.OPAQUE;
450 }
451 mSurface = new Surface(
452 mSession.mSurfaceSession, mSession.mPid,
453 attrs.getTitle().toString(),
454 0, w, h, format, flags);
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700455 mWin.mHasSurface = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700456 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
457 " CREATE SURFACE "
458 + mSurface + " IN SESSION "
459 + mSession.mSurfaceSession
460 + ": pid=" + mSession.mPid + " format="
461 + attrs.format + " flags=0x"
462 + Integer.toHexString(flags)
463 + " / " + this);
464 } catch (Surface.OutOfResourcesException e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700465 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700466 Slog.w(TAG, "OutOfResourcesException creating surface");
467 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700468 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700469 return null;
470 } catch (Exception e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700471 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700472 Slog.e(TAG, "Exception creating surface", e);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700473 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700474 return null;
475 }
476
477 if (WindowManagerService.localLOGV) Slog.v(
478 TAG, "Got surface: " + mSurface
479 + ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
480 + ", animLayer=" + mAnimLayer);
481 if (SHOW_LIGHT_TRANSACTIONS) {
482 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
483 WindowManagerService.logSurface(mWin, "CREATE pos=("
484 + mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
485 + mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
486 + "), layer=" + mAnimLayer + " HIDE", null);
487 }
488 Surface.openTransaction();
489 try {
490 try {
491 mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
492 mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
493 mSurface.setPosition(mSurfaceX, mSurfaceY);
494 mSurfaceLayer = mAnimLayer;
495 mSurface.setLayer(mAnimLayer);
496 mSurfaceShown = false;
497 mSurface.hide();
498 if ((mWin.mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
499 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "DITHER", null);
500 mSurface.setFlags(Surface.SURFACE_DITHER, Surface.SURFACE_DITHER);
501 }
502 } catch (RuntimeException e) {
503 Slog.w(TAG, "Error creating surface in " + w, e);
504 mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
505 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700506 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700507 } finally {
508 Surface.closeTransaction();
509 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
510 "<<< CLOSE TRANSACTION createSurfaceLocked");
511 }
512 if (WindowManagerService.localLOGV) Slog.v(
513 TAG, "Created surface " + this);
514 }
515 return mSurface;
516 }
517
518 void destroySurfaceLocked() {
519 if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
520 mWin.mAppToken.startingDisplayed = false;
521 }
522
Craig Mautner749a7bb2012-04-02 13:49:53 -0700523 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700524 if (mSurface != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700525
526 int i = mWin.mChildWindows.size();
527 while (i > 0) {
528 i--;
529 WindowState c = mWin.mChildWindows.get(i);
530 c.mAttachedHidden = true;
531 }
532
533 if (mReportDestroySurface) {
534 mReportDestroySurface = false;
535 mSurfacePendingDestroy = true;
536 try {
537 mWin.mClient.dispatchGetNewSurface();
538 // We'll really destroy on the next time around.
539 return;
540 } catch (RemoteException e) {
541 }
542 }
543
544 try {
545 if (DEBUG_VISIBILITY) {
546 RuntimeException e = null;
547 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
548 e = new RuntimeException();
549 e.fillInStackTrace();
550 }
551 Slog.w(TAG, "Window " + this + " destroying surface "
552 + mSurface + ", session " + mSession, e);
553 }
554 if (mSurfaceDestroyDeferred) {
555 if (mSurface != null && mPendingDestroySurface != mSurface) {
556 if (mPendingDestroySurface != null) {
557 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
558 RuntimeException e = null;
559 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
560 e = new RuntimeException();
561 e.fillInStackTrace();
562 }
563 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
564 }
565 mPendingDestroySurface.destroy();
566 }
567 mPendingDestroySurface = mSurface;
568 }
569 } else {
570 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
571 RuntimeException e = null;
572 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
573 e = new RuntimeException();
574 e.fillInStackTrace();
575 }
576 WindowManagerService.logSurface(mWin, "DESTROY", e);
577 }
578 mSurface.destroy();
579 }
580 } catch (RuntimeException e) {
581 Slog.w(TAG, "Exception thrown when destroying Window " + this
582 + " surface " + mSurface + " session " + mSession
583 + ": " + e.toString());
584 }
585
586 mSurfaceShown = false;
587 mSurface = null;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700588 mWin.mHasSurface =false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700589 }
590 }
591
592 void destroyDeferredSurfaceLocked() {
593 try {
594 if (mPendingDestroySurface != null) {
595 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
596 RuntimeException e = null;
597 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
598 e = new RuntimeException();
599 e.fillInStackTrace();
600 }
601 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
602 }
603 mPendingDestroySurface.destroy();
604 }
605 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700606 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700607 + this + " surface " + mPendingDestroySurface
608 + " session " + mSession + ": " + e.toString());
609 }
610 mSurfaceDestroyDeferred = false;
611 mPendingDestroySurface = null;
612 }
613
614 void computeShownFrameLocked() {
615 final boolean selfTransformation = mHasLocalTransformation;
616 Transformation attachedTransformation =
617 (mAttachedWindow != null && mAttachedWindow.mWinAnimator.mHasLocalTransformation)
618 ? mAttachedWindow.mWinAnimator.mTransformation : null;
Craig Mautner59431632012-04-04 11:56:44 -0700619 final AppWindowAnimator appAnimator =
620 mWin.mAppToken == null ? null : mWin.mAppToken.mAppAnimator;
621 Transformation appTransformation = (appAnimator != null && appAnimator.hasTransformation)
622 ? appAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700623
624 // Wallpapers are animated based on the "real" window they
625 // are currently targeting.
626 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
627 && mService.mWallpaperTarget != null) {
628 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
629 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
630 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
631 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
632 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
633 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
634 }
635 }
Craig Mautner59431632012-04-04 11:56:44 -0700636 final AppWindowAnimator wpAppAnimator = mService.mWallpaperTarget.mAppToken == null
637 ? null : mService.mWallpaperTarget.mAppToken.mAppAnimator;
638 if (wpAppAnimator != null &&
639 wpAppAnimator.hasTransformation &&
640 wpAppAnimator.animation != null &&
641 !wpAppAnimator.animation.getDetachWallpaper()) {
642 appTransformation = wpAppAnimator.transformation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700643 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
644 Slog.v(TAG, "WP target app xform: " + appTransformation);
645 }
646 }
647 }
648
649 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
650 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
651 if (selfTransformation || attachedTransformation != null
652 || appTransformation != null || screenAnimation) {
653 // cache often used attributes locally
654 final Rect frame = mWin.mFrame;
655 final float tmpFloats[] = mService.mTmpFloats;
656 final Matrix tmpMatrix = mWin.mTmpMatrix;
657
658 // Compute the desired transformation.
659 if (screenAnimation) {
660 // If we are doing a screen animation, the global rotation
661 // applied to windows can result in windows that are carefully
662 // aligned with each other to slightly separate, allowing you
663 // to see what is behind them. An unsightly mess. This...
664 // thing... magically makes it call good: scale each window
665 // slightly (two pixels larger in each dimension, from the
666 // window's center).
667 final float w = frame.width();
668 final float h = frame.height();
669 if (w>=1 && h>=1) {
670 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
671 } else {
672 tmpMatrix.reset();
673 }
674 } else {
675 tmpMatrix.reset();
676 }
677 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
678 if (selfTransformation) {
679 tmpMatrix.postConcat(mTransformation.getMatrix());
680 }
681 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
682 if (attachedTransformation != null) {
683 tmpMatrix.postConcat(attachedTransformation.getMatrix());
684 }
685 if (appTransformation != null) {
686 tmpMatrix.postConcat(appTransformation.getMatrix());
687 }
688 if (screenAnimation) {
689 tmpMatrix.postConcat(
690 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
691 }
692
693 // "convert" it into SurfaceFlinger's format
694 // (a 2x2 matrix + an offset)
695 // Here we must not transform the position of the surface
696 // since it is already included in the transformation.
697 //Slog.i(TAG, "Transform: " + matrix);
698
699 mHaveMatrix = true;
700 tmpMatrix.getValues(tmpFloats);
701 mDsDx = tmpFloats[Matrix.MSCALE_X];
702 mDtDx = tmpFloats[Matrix.MSKEW_Y];
703 mDsDy = tmpFloats[Matrix.MSKEW_X];
704 mDtDy = tmpFloats[Matrix.MSCALE_Y];
705 float x = tmpFloats[Matrix.MTRANS_X];
706 float y = tmpFloats[Matrix.MTRANS_Y];
707 int w = frame.width();
708 int h = frame.height();
709 mWin.mShownFrame.set(x, y, x+w, y+h);
710
711 // Now set the alpha... but because our current hardware
712 // can't do alpha transformation on a non-opaque surface,
713 // turn it off if we are running an animation that is also
714 // transforming since it is more important to have that
715 // animation be smooth.
716 mShownAlpha = mAlpha;
717 if (!mService.mLimitedAlphaCompositing
718 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
719 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
720 && x == frame.left && y == frame.top))) {
721 //Slog.i(TAG, "Applying alpha transform");
722 if (selfTransformation) {
723 mShownAlpha *= mTransformation.getAlpha();
724 }
725 if (attachedTransformation != null) {
726 mShownAlpha *= attachedTransformation.getAlpha();
727 }
728 if (appTransformation != null) {
729 mShownAlpha *= appTransformation.getAlpha();
730 }
731 if (screenAnimation) {
732 mShownAlpha *=
733 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
734 }
735 } else {
736 //Slog.i(TAG, "Not applying alpha transform");
737 }
738
739 if (WindowManagerService.localLOGV) Slog.v(
740 TAG, "computeShownFrameLocked: Animating " + this +
741 ": " + mWin.mShownFrame +
742 ", alpha=" + mTransformation.getAlpha() + ", mShownAlpha=" + mShownAlpha);
743 return;
744 }
745
746 if (WindowManagerService.localLOGV) Slog.v(
747 TAG, "computeShownFrameLocked: " + this +
748 " not attached, mAlpha=" + mAlpha);
749 mWin.mShownFrame.set(mWin.mFrame);
750 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
751 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
752 }
753 mShownAlpha = mAlpha;
754 mHaveMatrix = false;
755 mDsDx = mWin.mGlobalScale;
756 mDtDx = 0;
757 mDsDy = 0;
758 mDtDy = mWin.mGlobalScale;
759 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700760
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700761 public void prepareSurfaceLocked(final boolean recoveringMemory) {
762 final WindowState w = mWin;
763 if (mSurface == null) {
764 if (w.mOrientationChanging) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700765 if (DEBUG_ORIENTATION) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700766 Slog.v(TAG, "Orientation change skips hidden " + w);
767 }
768 w.mOrientationChanging = false;
769 }
770 return;
771 }
772
773 boolean displayed = false;
774
775 computeShownFrameLocked();
776
777 int width, height;
778 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
779 // for a scaled surface, we just want to use
780 // the requested size.
781 width = w.mRequestedWidth;
782 height = w.mRequestedHeight;
783 } else {
784 width = w.mCompatFrame.width();
785 height = w.mCompatFrame.height();
786 }
787
788 if (width < 1) {
789 width = 1;
790 }
791 if (height < 1) {
792 height = 1;
793 }
794 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
795 if (surfaceResized) {
796 mSurfaceW = width;
797 mSurfaceH = height;
798 }
799
800 if (mSurfaceX != w.mShownFrame.left
801 || mSurfaceY != w.mShownFrame.top) {
802 try {
803 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
804 "POS " + w.mShownFrame.left
805 + ", " + w.mShownFrame.top, null);
806 mSurfaceX = w.mShownFrame.left;
807 mSurfaceY = w.mShownFrame.top;
808 mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
809 } catch (RuntimeException e) {
810 Slog.w(TAG, "Error positioning surface of " + w
811 + " pos=(" + w.mShownFrame.left
812 + "," + w.mShownFrame.top + ")", e);
813 if (!recoveringMemory) {
814 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
815 }
816 }
817 }
818
819 if (surfaceResized) {
820 try {
821 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
822 "SIZE " + width + "x" + height, null);
823 mSurfaceResized = true;
824 mSurface.setSize(width, height);
825 } catch (RuntimeException e) {
826 // If something goes wrong with the surface (such
827 // as running out of memory), don't take down the
828 // entire system.
829 Slog.e(TAG, "Error resizing surface of " + w
830 + " size=(" + width + "x" + height + ")", e);
831 if (!recoveringMemory) {
832 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
833 }
834 }
835 }
836
837 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700838 if (!mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700839 //dump();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700840 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700841 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
842 "HIDE (performLayout)", null);
843 if (mSurface != null) {
844 mSurfaceShown = false;
845 try {
846 mSurface.hide();
847 } catch (RuntimeException e) {
848 Slog.w(TAG, "Exception hiding surface in " + w);
849 }
850 }
851 }
852 // If we are waiting for this window to handle an
853 // orientation change, well, it is hidden, so
854 // doesn't really matter. Note that this does
855 // introduce a potential glitch if the window
856 // becomes unhidden before it has drawn for the
857 // new orientation.
858 if (w.mOrientationChanging) {
859 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700860 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700861 "Orientation change skips hidden " + w);
862 }
863 } else if (mLastLayer != mAnimLayer
864 || mLastAlpha != mShownAlpha
865 || mLastDsDx != mDsDx
866 || mLastDtDx != mDtDx
867 || mLastDsDy != mDsDy
868 || mLastDtDy != mDtDy
869 || w.mLastHScale != w.mHScale
870 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -0700871 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700872 displayed = true;
873 mLastAlpha = mShownAlpha;
874 mLastLayer = mAnimLayer;
875 mLastDsDx = mDsDx;
876 mLastDtDx = mDtDx;
877 mLastDsDy = mDsDy;
878 mLastDtDy = mDtDy;
879 w.mLastHScale = w.mHScale;
880 w.mLastVScale = w.mVScale;
881 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
882 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
883 + " matrix=[" + (mDsDx*w.mHScale)
884 + "," + (mDtDx*w.mVScale)
885 + "][" + (mDsDy*w.mHScale)
886 + "," + (mDtDy*w.mVScale) + "]", null);
887 if (mSurface != null) {
888 try {
889 mSurfaceAlpha = mShownAlpha;
890 mSurface.setAlpha(mShownAlpha);
891 mSurfaceLayer = w.mWinAnimator.mAnimLayer;
892 mSurface.setLayer(w.mWinAnimator.mAnimLayer);
893 mSurface.setMatrix(
894 mDsDx*w.mHScale, mDtDx*w.mVScale,
895 mDsDy*w.mHScale, mDtDy*w.mVScale);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700896
897 if (mLastHidden && mDrawState == HAS_DRAWN) {
898 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
899 "SHOW (performLayout)", null);
900 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
901 + " during relayout");
902 if (showSurfaceRobustlyLocked()) {
903 mLastHidden = false;
904 } else {
905 w.mOrientationChanging = false;
906 }
907 }
908 if (mSurface != null) {
909 w.mToken.hasVisible = true;
910 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700911 } catch (RuntimeException e) {
912 Slog.w(TAG, "Error updating surface in " + w, e);
913 if (!recoveringMemory) {
914 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
915 }
916 }
917 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700918 } else {
919 displayed = true;
920 }
921
922 if (displayed) {
923 if (w.mOrientationChanging) {
924 if (!w.isDrawnLw()) {
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700925 mAnimator.mBulkUpdateParams |= CLEAR_ORIENTATION_CHANGE_COMPLETE;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700926 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700927 "Orientation continue waiting for draw in " + w);
928 } else {
929 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700930 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700931 }
932 }
933 w.mToken.hasVisible = true;
934 }
935 }
936
Craig Mautner48ba1e72012-04-02 13:18:16 -0700937 void setTransparentRegionHint(final Region region) {
938 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
939 ">>> OPEN TRANSACTION setTransparentRegion");
940 Surface.openTransaction();
941 try {
942 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
943 "transparentRegionHint=" + region, null);
944 mSurface.setTransparentRegionHint(region);
945 } finally {
946 Surface.closeTransaction();
947 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
948 "<<< CLOSE TRANSACTION setTransparentRegion");
949 }
950 }
951
952 void setWallpaperOffset(int left, int top) {
953 Surface.openTransaction();
954 try {
955 mSurfaceX = left;
956 mSurfaceY = top;
957 mSurface.setPosition(left, top);
958 } catch (RuntimeException e) {
959 Slog.w(TAG, "Error positioning surface of " + mWin
960 + " pos=(" + left + "," + top + ")", e);
961 }
962 Surface.closeTransaction();
963 }
964
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700965 // This must be called while inside a transaction.
966 boolean performShowLocked() {
967 if (DEBUG_VISIBILITY) {
968 RuntimeException e = null;
969 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
970 e = new RuntimeException();
971 e.fillInStackTrace();
972 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700973 Slog.v(TAG, "performShow on " + this
Craig Mautner749a7bb2012-04-02 13:49:53 -0700974 + ": mDrawState=" + mDrawState + " readyForDisplay="
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700975 + mWin.isReadyForDisplay()
976 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
977 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700978 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplay()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700979 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700980 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -0700981 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700982 + " during animation: policyVis=" + mWin.mPolicyVisibility
983 + " attHidden=" + mWin.mAttachedHidden
984 + " tok.hiddenRequested="
985 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
986 + " tok.hidden="
987 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
988 + " animating=" + mAnimating
989 + " tok animating="
Craig Mautner59431632012-04-04 11:56:44 -0700990 + (mWin.mAppToken != null ? mWin.mAppToken.mAppAnimator.animating : false));
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700991 if (!showSurfaceRobustlyLocked()) {
992 return false;
993 }
994
995 mService.enableScreenIfNeededLocked();
996
997 applyEnterAnimationLocked();
998
999 mLastAlpha = -1;
Craig Mautner749a7bb2012-04-02 13:49:53 -07001000 mLastHidden = false;
1001 mDrawState = HAS_DRAWN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001002
1003 int i = mWin.mChildWindows.size();
1004 while (i > 0) {
1005 i--;
1006 WindowState c = mWin.mChildWindows.get(i);
1007 if (c.mAttachedHidden) {
1008 c.mAttachedHidden = false;
1009 if (c.mWinAnimator.mSurface != null) {
1010 c.mWinAnimator.performShowLocked();
1011 // It hadn't been shown, which means layout not
1012 // performed on it, so now we want to make sure to
1013 // do a layout. If called from within the transaction
1014 // loop, this will cause it to restart with a new
1015 // layout.
1016 mService.mLayoutNeeded = true;
1017 }
1018 }
1019 }
1020
1021 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1022 && mWin.mAppToken != null) {
1023 mWin.mAppToken.firstWindowDrawn = true;
1024
1025 if (mWin.mAppToken.startingData != null) {
1026 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001027 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001028 "Finish starting " + mWin.mToken
1029 + ": first real window is shown, no animation");
1030 // If this initial window is animating, stop it -- we
1031 // will do an animation to reveal it from behind the
1032 // starting window, so there is no need for it to also
1033 // be doing its own stuff.
1034 if (mAnimation != null) {
1035 mAnimation.cancel();
1036 mAnimation = null;
1037 // Make sure we clean up the animation.
1038 mAnimating = true;
1039 }
1040 mService.mFinishedStarting.add(mWin.mAppToken);
1041 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1042 }
1043 mWin.mAppToken.updateReportedVisibilityLocked();
1044 }
1045
1046 return true;
1047 }
1048
1049 return false;
1050 }
1051
1052 /**
1053 * Have the surface flinger show a surface, robustly dealing with
1054 * error conditions. In particular, if there is not enough memory
1055 * to show the surface, then we will try to get rid of other surfaces
1056 * in order to succeed.
1057 *
1058 * @return Returns true if the surface was successfully shown.
1059 */
1060 boolean showSurfaceRobustlyLocked() {
1061 try {
1062 if (mSurface != null) {
1063 mSurfaceShown = true;
1064 mSurface.show();
1065 if (mWin.mTurnOnScreen) {
1066 if (DEBUG_VISIBILITY) Slog.v(TAG,
1067 "Show surface turning screen on: " + mWin);
1068 mWin.mTurnOnScreen = false;
1069 mService.mTurnOnScreen = true;
1070 }
1071 }
1072 return true;
1073 } catch (RuntimeException e) {
1074 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1075 }
1076
1077 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1078
1079 return false;
1080 }
1081
1082 void applyEnterAnimationLocked() {
1083 final int transit;
1084 if (mEnterAnimationPending) {
1085 mEnterAnimationPending = false;
1086 transit = WindowManagerPolicy.TRANSIT_ENTER;
1087 } else {
1088 transit = WindowManagerPolicy.TRANSIT_SHOW;
1089 }
1090
1091 applyAnimationLocked(transit, true);
1092 }
1093
Craig Mautner48ba1e72012-04-02 13:18:16 -07001094 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001095 /**
1096 * Choose the correct animation and set it to the passed WindowState.
1097 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1098 * then the animation will be app_starting_exit. Any other value loads the animation from
1099 * the switch statement below.
1100 * @param isEntrance The animation type the last time this was called. Used to keep from
1101 * loading the same animation twice.
1102 * @return true if an animation has been loaded.
1103 */
1104 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1105 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1106 // If we are trying to apply an animation, but already running
1107 // an animation of the same type, then just leave that one alone.
1108 return true;
1109 }
1110
1111 // Only apply an animation if the display isn't frozen. If it is
1112 // frozen, there is no reason to animate and it can cause strange
1113 // artifacts when we unfreeze the display if some different animation
1114 // is running.
1115 if (mService.okToDisplay()) {
1116 int anim = mPolicy.selectAnimationLw(mWin, transit);
1117 int attr = -1;
1118 Animation a = null;
1119 if (anim != 0) {
1120 a = AnimationUtils.loadAnimation(mContext, anim);
1121 } else {
1122 switch (transit) {
1123 case WindowManagerPolicy.TRANSIT_ENTER:
1124 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1125 break;
1126 case WindowManagerPolicy.TRANSIT_EXIT:
1127 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1128 break;
1129 case WindowManagerPolicy.TRANSIT_SHOW:
1130 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1131 break;
1132 case WindowManagerPolicy.TRANSIT_HIDE:
1133 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1134 break;
1135 }
1136 if (attr >= 0) {
1137 a = mService.loadAnimation(mWin.mAttrs, attr);
1138 }
1139 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001140 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001141 "applyAnimation: win=" + this
1142 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1143 + " mAnimation=" + mAnimation
1144 + " isEntrance=" + isEntrance);
1145 if (a != null) {
1146 if (WindowManagerService.DEBUG_ANIM) {
1147 RuntimeException e = null;
1148 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1149 e = new RuntimeException();
1150 e.fillInStackTrace();
1151 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001152 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001153 }
1154 setAnimation(a);
1155 mAnimationIsEntrance = isEntrance;
1156 }
1157 } else {
1158 clearAnimation();
1159 }
1160
1161 return mAnimation != null;
1162 }
1163
Craig Mautnera2c77052012-03-26 12:14:43 -07001164 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1165 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1166 || mAnimation != null) {
1167 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1168 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1169 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1170 pw.print(" mAnimation="); pw.println(mAnimation);
1171 }
1172 if (mHasTransformation || mHasLocalTransformation) {
1173 pw.print(prefix); pw.print("XForm: has=");
1174 pw.print(mHasTransformation);
1175 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1176 pw.print(" "); mTransformation.printShortString(pw);
1177 pw.println();
1178 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001179 if (mSurface != null) {
1180 if (dumpAll) {
1181 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001182 pw.print(prefix); pw.print("mDrawState="); pw.print(mDrawState);
1183 pw.print(" mLastHidden="); pw.println(mLastHidden);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001184 }
1185 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1186 pw.print(" layer="); pw.print(mSurfaceLayer);
1187 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1188 pw.print(" rect=("); pw.print(mSurfaceX);
1189 pw.print(","); pw.print(mSurfaceY);
1190 pw.print(") "); pw.print(mSurfaceW);
1191 pw.print(" x "); pw.println(mSurfaceH);
1192 }
1193 if (mPendingDestroySurface != null) {
1194 pw.print(prefix); pw.print("mPendingDestroySurface=");
1195 pw.println(mPendingDestroySurface);
1196 }
1197 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1198 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1199 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1200 }
1201 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1202 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1203 pw.print(" mAlpha="); pw.print(mAlpha);
1204 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1205 }
1206 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1207 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1208 pw.print(" mDsDx="); pw.print(mDsDx);
1209 pw.print(" mDtDx="); pw.print(mDtDx);
1210 pw.print(" mDsDy="); pw.print(mDsDy);
1211 pw.print(" mDtDy="); pw.println(mDtDy);
1212 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001213 }
1214
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001215 @Override
1216 public String toString() {
1217 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1218 sb.append(mWin.mLastTitle + "): ");
1219 sb.append("mSurface " + mSurface);
1220 sb.append(", mAnimation " + mAnimation);
1221 return sb.toString();
1222 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001223}