blob: 0dec5b22eb0334bd4daede306d586aa3c8c12926 [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 &&
165 (atoken.animation != null
166 || 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
231 && mWin.mAppToken.animation != null) {
232 // 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;
619 Transformation appTransformation =
620 (mWin.mAppToken != null && mWin.mAppToken.hasTransformation)
621 ? mWin.mAppToken.transformation : null;
622
623 // Wallpapers are animated based on the "real" window they
624 // are currently targeting.
625 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
626 && mService.mWallpaperTarget != null) {
627 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
628 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
629 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
630 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
631 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
632 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
633 }
634 }
635 if (mService.mWallpaperTarget.mAppToken != null &&
636 mService.mWallpaperTarget.mAppToken.hasTransformation &&
637 mService.mWallpaperTarget.mAppToken.animation != null &&
638 !mService.mWallpaperTarget.mAppToken.animation.getDetachWallpaper()) {
639 appTransformation = mService.mWallpaperTarget.mAppToken.transformation;
640 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
641 Slog.v(TAG, "WP target app xform: " + appTransformation);
642 }
643 }
644 }
645
646 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
647 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
648 if (selfTransformation || attachedTransformation != null
649 || appTransformation != null || screenAnimation) {
650 // cache often used attributes locally
651 final Rect frame = mWin.mFrame;
652 final float tmpFloats[] = mService.mTmpFloats;
653 final Matrix tmpMatrix = mWin.mTmpMatrix;
654
655 // Compute the desired transformation.
656 if (screenAnimation) {
657 // If we are doing a screen animation, the global rotation
658 // applied to windows can result in windows that are carefully
659 // aligned with each other to slightly separate, allowing you
660 // to see what is behind them. An unsightly mess. This...
661 // thing... magically makes it call good: scale each window
662 // slightly (two pixels larger in each dimension, from the
663 // window's center).
664 final float w = frame.width();
665 final float h = frame.height();
666 if (w>=1 && h>=1) {
667 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
668 } else {
669 tmpMatrix.reset();
670 }
671 } else {
672 tmpMatrix.reset();
673 }
674 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
675 if (selfTransformation) {
676 tmpMatrix.postConcat(mTransformation.getMatrix());
677 }
678 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
679 if (attachedTransformation != null) {
680 tmpMatrix.postConcat(attachedTransformation.getMatrix());
681 }
682 if (appTransformation != null) {
683 tmpMatrix.postConcat(appTransformation.getMatrix());
684 }
685 if (screenAnimation) {
686 tmpMatrix.postConcat(
687 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
688 }
689
690 // "convert" it into SurfaceFlinger's format
691 // (a 2x2 matrix + an offset)
692 // Here we must not transform the position of the surface
693 // since it is already included in the transformation.
694 //Slog.i(TAG, "Transform: " + matrix);
695
696 mHaveMatrix = true;
697 tmpMatrix.getValues(tmpFloats);
698 mDsDx = tmpFloats[Matrix.MSCALE_X];
699 mDtDx = tmpFloats[Matrix.MSKEW_Y];
700 mDsDy = tmpFloats[Matrix.MSKEW_X];
701 mDtDy = tmpFloats[Matrix.MSCALE_Y];
702 float x = tmpFloats[Matrix.MTRANS_X];
703 float y = tmpFloats[Matrix.MTRANS_Y];
704 int w = frame.width();
705 int h = frame.height();
706 mWin.mShownFrame.set(x, y, x+w, y+h);
707
708 // Now set the alpha... but because our current hardware
709 // can't do alpha transformation on a non-opaque surface,
710 // turn it off if we are running an animation that is also
711 // transforming since it is more important to have that
712 // animation be smooth.
713 mShownAlpha = mAlpha;
714 if (!mService.mLimitedAlphaCompositing
715 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
716 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
717 && x == frame.left && y == frame.top))) {
718 //Slog.i(TAG, "Applying alpha transform");
719 if (selfTransformation) {
720 mShownAlpha *= mTransformation.getAlpha();
721 }
722 if (attachedTransformation != null) {
723 mShownAlpha *= attachedTransformation.getAlpha();
724 }
725 if (appTransformation != null) {
726 mShownAlpha *= appTransformation.getAlpha();
727 }
728 if (screenAnimation) {
729 mShownAlpha *=
730 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
731 }
732 } else {
733 //Slog.i(TAG, "Not applying alpha transform");
734 }
735
736 if (WindowManagerService.localLOGV) Slog.v(
737 TAG, "computeShownFrameLocked: Animating " + this +
738 ": " + mWin.mShownFrame +
739 ", alpha=" + mTransformation.getAlpha() + ", mShownAlpha=" + mShownAlpha);
740 return;
741 }
742
743 if (WindowManagerService.localLOGV) Slog.v(
744 TAG, "computeShownFrameLocked: " + this +
745 " not attached, mAlpha=" + mAlpha);
746 mWin.mShownFrame.set(mWin.mFrame);
747 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
748 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
749 }
750 mShownAlpha = mAlpha;
751 mHaveMatrix = false;
752 mDsDx = mWin.mGlobalScale;
753 mDtDx = 0;
754 mDsDy = 0;
755 mDtDy = mWin.mGlobalScale;
756 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700757
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700758 public void prepareSurfaceLocked(final boolean recoveringMemory) {
759 final WindowState w = mWin;
760 if (mSurface == null) {
761 if (w.mOrientationChanging) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700762 if (DEBUG_ORIENTATION) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700763 Slog.v(TAG, "Orientation change skips hidden " + w);
764 }
765 w.mOrientationChanging = false;
766 }
767 return;
768 }
769
770 boolean displayed = false;
771
772 computeShownFrameLocked();
773
774 int width, height;
775 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
776 // for a scaled surface, we just want to use
777 // the requested size.
778 width = w.mRequestedWidth;
779 height = w.mRequestedHeight;
780 } else {
781 width = w.mCompatFrame.width();
782 height = w.mCompatFrame.height();
783 }
784
785 if (width < 1) {
786 width = 1;
787 }
788 if (height < 1) {
789 height = 1;
790 }
791 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
792 if (surfaceResized) {
793 mSurfaceW = width;
794 mSurfaceH = height;
795 }
796
797 if (mSurfaceX != w.mShownFrame.left
798 || mSurfaceY != w.mShownFrame.top) {
799 try {
800 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
801 "POS " + w.mShownFrame.left
802 + ", " + w.mShownFrame.top, null);
803 mSurfaceX = w.mShownFrame.left;
804 mSurfaceY = w.mShownFrame.top;
805 mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
806 } catch (RuntimeException e) {
807 Slog.w(TAG, "Error positioning surface of " + w
808 + " pos=(" + w.mShownFrame.left
809 + "," + w.mShownFrame.top + ")", e);
810 if (!recoveringMemory) {
811 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
812 }
813 }
814 }
815
816 if (surfaceResized) {
817 try {
818 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
819 "SIZE " + width + "x" + height, null);
820 mSurfaceResized = true;
821 mSurface.setSize(width, height);
822 } catch (RuntimeException e) {
823 // If something goes wrong with the surface (such
824 // as running out of memory), don't take down the
825 // entire system.
826 Slog.e(TAG, "Error resizing surface of " + w
827 + " size=(" + width + "x" + height + ")", e);
828 if (!recoveringMemory) {
829 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
830 }
831 }
832 }
833
834 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700835 if (!mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700836 //dump();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700837 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700838 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
839 "HIDE (performLayout)", null);
840 if (mSurface != null) {
841 mSurfaceShown = false;
842 try {
843 mSurface.hide();
844 } catch (RuntimeException e) {
845 Slog.w(TAG, "Exception hiding surface in " + w);
846 }
847 }
848 }
849 // If we are waiting for this window to handle an
850 // orientation change, well, it is hidden, so
851 // doesn't really matter. Note that this does
852 // introduce a potential glitch if the window
853 // becomes unhidden before it has drawn for the
854 // new orientation.
855 if (w.mOrientationChanging) {
856 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700857 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700858 "Orientation change skips hidden " + w);
859 }
860 } else if (mLastLayer != mAnimLayer
861 || mLastAlpha != mShownAlpha
862 || mLastDsDx != mDsDx
863 || mLastDtDx != mDtDx
864 || mLastDsDy != mDsDy
865 || mLastDtDy != mDtDy
866 || w.mLastHScale != w.mHScale
867 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -0700868 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700869 displayed = true;
870 mLastAlpha = mShownAlpha;
871 mLastLayer = mAnimLayer;
872 mLastDsDx = mDsDx;
873 mLastDtDx = mDtDx;
874 mLastDsDy = mDsDy;
875 mLastDtDy = mDtDy;
876 w.mLastHScale = w.mHScale;
877 w.mLastVScale = w.mVScale;
878 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
879 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
880 + " matrix=[" + (mDsDx*w.mHScale)
881 + "," + (mDtDx*w.mVScale)
882 + "][" + (mDsDy*w.mHScale)
883 + "," + (mDtDy*w.mVScale) + "]", null);
884 if (mSurface != null) {
885 try {
886 mSurfaceAlpha = mShownAlpha;
887 mSurface.setAlpha(mShownAlpha);
888 mSurfaceLayer = w.mWinAnimator.mAnimLayer;
889 mSurface.setLayer(w.mWinAnimator.mAnimLayer);
890 mSurface.setMatrix(
891 mDsDx*w.mHScale, mDtDx*w.mVScale,
892 mDsDy*w.mHScale, mDtDy*w.mVScale);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700893
894 if (mLastHidden && mDrawState == HAS_DRAWN) {
895 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
896 "SHOW (performLayout)", null);
897 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
898 + " during relayout");
899 if (showSurfaceRobustlyLocked()) {
900 mLastHidden = false;
901 } else {
902 w.mOrientationChanging = false;
903 }
904 }
905 if (mSurface != null) {
906 w.mToken.hasVisible = true;
907 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700908 } catch (RuntimeException e) {
909 Slog.w(TAG, "Error updating surface in " + w, e);
910 if (!recoveringMemory) {
911 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
912 }
913 }
914 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700915 } else {
916 displayed = true;
917 }
918
919 if (displayed) {
920 if (w.mOrientationChanging) {
921 if (!w.isDrawnLw()) {
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700922 mAnimator.mBulkUpdateParams |= CLEAR_ORIENTATION_CHANGE_COMPLETE;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700923 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700924 "Orientation continue waiting for draw in " + w);
925 } else {
926 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700927 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700928 }
929 }
930 w.mToken.hasVisible = true;
931 }
932 }
933
Craig Mautner48ba1e72012-04-02 13:18:16 -0700934 void setTransparentRegionHint(final Region region) {
935 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
936 ">>> OPEN TRANSACTION setTransparentRegion");
937 Surface.openTransaction();
938 try {
939 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
940 "transparentRegionHint=" + region, null);
941 mSurface.setTransparentRegionHint(region);
942 } finally {
943 Surface.closeTransaction();
944 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
945 "<<< CLOSE TRANSACTION setTransparentRegion");
946 }
947 }
948
949 void setWallpaperOffset(int left, int top) {
950 Surface.openTransaction();
951 try {
952 mSurfaceX = left;
953 mSurfaceY = top;
954 mSurface.setPosition(left, top);
955 } catch (RuntimeException e) {
956 Slog.w(TAG, "Error positioning surface of " + mWin
957 + " pos=(" + left + "," + top + ")", e);
958 }
959 Surface.closeTransaction();
960 }
961
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700962 // This must be called while inside a transaction.
963 boolean performShowLocked() {
964 if (DEBUG_VISIBILITY) {
965 RuntimeException e = null;
966 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
967 e = new RuntimeException();
968 e.fillInStackTrace();
969 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700970 Slog.v(TAG, "performShow on " + this
Craig Mautner749a7bb2012-04-02 13:49:53 -0700971 + ": mDrawState=" + mDrawState + " readyForDisplay="
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700972 + mWin.isReadyForDisplay()
973 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
974 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700975 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplay()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700976 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700977 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -0700978 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700979 + " during animation: policyVis=" + mWin.mPolicyVisibility
980 + " attHidden=" + mWin.mAttachedHidden
981 + " tok.hiddenRequested="
982 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
983 + " tok.hidden="
984 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
985 + " animating=" + mAnimating
986 + " tok animating="
987 + (mWin.mAppToken != null ? mWin.mAppToken.animating : false));
988 if (!showSurfaceRobustlyLocked()) {
989 return false;
990 }
991
992 mService.enableScreenIfNeededLocked();
993
994 applyEnterAnimationLocked();
995
996 mLastAlpha = -1;
Craig Mautner749a7bb2012-04-02 13:49:53 -0700997 mLastHidden = false;
998 mDrawState = HAS_DRAWN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700999
1000 int i = mWin.mChildWindows.size();
1001 while (i > 0) {
1002 i--;
1003 WindowState c = mWin.mChildWindows.get(i);
1004 if (c.mAttachedHidden) {
1005 c.mAttachedHidden = false;
1006 if (c.mWinAnimator.mSurface != null) {
1007 c.mWinAnimator.performShowLocked();
1008 // It hadn't been shown, which means layout not
1009 // performed on it, so now we want to make sure to
1010 // do a layout. If called from within the transaction
1011 // loop, this will cause it to restart with a new
1012 // layout.
1013 mService.mLayoutNeeded = true;
1014 }
1015 }
1016 }
1017
1018 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1019 && mWin.mAppToken != null) {
1020 mWin.mAppToken.firstWindowDrawn = true;
1021
1022 if (mWin.mAppToken.startingData != null) {
1023 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001024 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001025 "Finish starting " + mWin.mToken
1026 + ": first real window is shown, no animation");
1027 // If this initial window is animating, stop it -- we
1028 // will do an animation to reveal it from behind the
1029 // starting window, so there is no need for it to also
1030 // be doing its own stuff.
1031 if (mAnimation != null) {
1032 mAnimation.cancel();
1033 mAnimation = null;
1034 // Make sure we clean up the animation.
1035 mAnimating = true;
1036 }
1037 mService.mFinishedStarting.add(mWin.mAppToken);
1038 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1039 }
1040 mWin.mAppToken.updateReportedVisibilityLocked();
1041 }
1042
1043 return true;
1044 }
1045
1046 return false;
1047 }
1048
1049 /**
1050 * Have the surface flinger show a surface, robustly dealing with
1051 * error conditions. In particular, if there is not enough memory
1052 * to show the surface, then we will try to get rid of other surfaces
1053 * in order to succeed.
1054 *
1055 * @return Returns true if the surface was successfully shown.
1056 */
1057 boolean showSurfaceRobustlyLocked() {
1058 try {
1059 if (mSurface != null) {
1060 mSurfaceShown = true;
1061 mSurface.show();
1062 if (mWin.mTurnOnScreen) {
1063 if (DEBUG_VISIBILITY) Slog.v(TAG,
1064 "Show surface turning screen on: " + mWin);
1065 mWin.mTurnOnScreen = false;
1066 mService.mTurnOnScreen = true;
1067 }
1068 }
1069 return true;
1070 } catch (RuntimeException e) {
1071 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1072 }
1073
1074 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1075
1076 return false;
1077 }
1078
1079 void applyEnterAnimationLocked() {
1080 final int transit;
1081 if (mEnterAnimationPending) {
1082 mEnterAnimationPending = false;
1083 transit = WindowManagerPolicy.TRANSIT_ENTER;
1084 } else {
1085 transit = WindowManagerPolicy.TRANSIT_SHOW;
1086 }
1087
1088 applyAnimationLocked(transit, true);
1089 }
1090
Craig Mautner48ba1e72012-04-02 13:18:16 -07001091 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001092 /**
1093 * Choose the correct animation and set it to the passed WindowState.
1094 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1095 * then the animation will be app_starting_exit. Any other value loads the animation from
1096 * the switch statement below.
1097 * @param isEntrance The animation type the last time this was called. Used to keep from
1098 * loading the same animation twice.
1099 * @return true if an animation has been loaded.
1100 */
1101 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1102 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1103 // If we are trying to apply an animation, but already running
1104 // an animation of the same type, then just leave that one alone.
1105 return true;
1106 }
1107
1108 // Only apply an animation if the display isn't frozen. If it is
1109 // frozen, there is no reason to animate and it can cause strange
1110 // artifacts when we unfreeze the display if some different animation
1111 // is running.
1112 if (mService.okToDisplay()) {
1113 int anim = mPolicy.selectAnimationLw(mWin, transit);
1114 int attr = -1;
1115 Animation a = null;
1116 if (anim != 0) {
1117 a = AnimationUtils.loadAnimation(mContext, anim);
1118 } else {
1119 switch (transit) {
1120 case WindowManagerPolicy.TRANSIT_ENTER:
1121 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1122 break;
1123 case WindowManagerPolicy.TRANSIT_EXIT:
1124 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1125 break;
1126 case WindowManagerPolicy.TRANSIT_SHOW:
1127 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1128 break;
1129 case WindowManagerPolicy.TRANSIT_HIDE:
1130 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1131 break;
1132 }
1133 if (attr >= 0) {
1134 a = mService.loadAnimation(mWin.mAttrs, attr);
1135 }
1136 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001137 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001138 "applyAnimation: win=" + this
1139 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1140 + " mAnimation=" + mAnimation
1141 + " isEntrance=" + isEntrance);
1142 if (a != null) {
1143 if (WindowManagerService.DEBUG_ANIM) {
1144 RuntimeException e = null;
1145 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1146 e = new RuntimeException();
1147 e.fillInStackTrace();
1148 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001149 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001150 }
1151 setAnimation(a);
1152 mAnimationIsEntrance = isEntrance;
1153 }
1154 } else {
1155 clearAnimation();
1156 }
1157
1158 return mAnimation != null;
1159 }
1160
Craig Mautnera2c77052012-03-26 12:14:43 -07001161 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1162 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1163 || mAnimation != null) {
1164 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1165 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1166 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1167 pw.print(" mAnimation="); pw.println(mAnimation);
1168 }
1169 if (mHasTransformation || mHasLocalTransformation) {
1170 pw.print(prefix); pw.print("XForm: has=");
1171 pw.print(mHasTransformation);
1172 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1173 pw.print(" "); mTransformation.printShortString(pw);
1174 pw.println();
1175 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001176 if (mSurface != null) {
1177 if (dumpAll) {
1178 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001179 pw.print(prefix); pw.print("mDrawState="); pw.print(mDrawState);
1180 pw.print(" mLastHidden="); pw.println(mLastHidden);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001181 }
1182 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1183 pw.print(" layer="); pw.print(mSurfaceLayer);
1184 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1185 pw.print(" rect=("); pw.print(mSurfaceX);
1186 pw.print(","); pw.print(mSurfaceY);
1187 pw.print(") "); pw.print(mSurfaceW);
1188 pw.print(" x "); pw.println(mSurfaceH);
1189 }
1190 if (mPendingDestroySurface != null) {
1191 pw.print(prefix); pw.print("mPendingDestroySurface=");
1192 pw.println(mPendingDestroySurface);
1193 }
1194 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1195 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1196 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1197 }
1198 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1199 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1200 pw.print(" mAlpha="); pw.print(mAlpha);
1201 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1202 }
1203 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1204 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1205 pw.print(" mDsDx="); pw.print(mDsDx);
1206 pw.print(" mDtDx="); pw.print(mDtDx);
1207 pw.print(" mDsDy="); pw.print(mDsDy);
1208 pw.print(" mDtDy="); pw.println(mDtDy);
1209 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001210 }
1211
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001212 @Override
1213 public String toString() {
1214 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1215 sb.append(mWin.mLastTitle + "): ");
1216 sb.append("mSurface " + mSurface);
1217 sb.append(", mAnimation " + mAnimation);
1218 return sb.toString();
1219 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001220}