blob: 1804ec0ee38e96747834ea136b5cf88dc36289ac [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;
Craig Mautner7d8df392012-04-06 15:26:23 -07009import static com.android.server.wm.WindowManagerService.LayoutFields.SET_TURN_ON_SCREEN;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070010
Craig Mautnerc2f9be02012-03-27 17:32:29 -070011import android.content.Context;
12import android.graphics.Matrix;
13import android.graphics.PixelFormat;
Craig Mautner7358fbf2012-04-12 21:06:33 -070014import android.graphics.Point;
15import android.graphics.PointF;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070016import android.graphics.Rect;
Craig Mautner48ba1e72012-04-02 13:18:16 -070017import android.graphics.Region;
Craig Mautnera51a9562012-04-17 17:05:26 -070018import android.os.Debug;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070019import android.os.RemoteException;
Craig Mautnera2c77052012-03-26 12:14:43 -070020import android.util.Slog;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070021import android.view.Surface;
Craig Mautner7358fbf2012-04-12 21:06:33 -070022import android.view.SurfaceSession;
Craig Mautnera2c77052012-03-26 12:14:43 -070023import android.view.WindowManager;
24import android.view.WindowManagerPolicy;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070025import android.view.WindowManager.LayoutParams;
Craig Mautnera2c77052012-03-26 12:14:43 -070026import android.view.animation.Animation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070027import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070028import android.view.animation.Transformation;
29
30import com.android.server.wm.WindowManagerService.H;
31
32import java.io.PrintWriter;
Craig Mautner7358fbf2012-04-12 21:06:33 -070033import java.util.ArrayList;
Craig Mautnera2c77052012-03-26 12:14:43 -070034
35/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070036 * Keep track of animations and surface operations for a single WindowState.
37 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070038class WindowStateAnimator {
Craig Mautnerc2f9be02012-03-27 17:32:29 -070039 static final boolean DEBUG_VISIBILITY = WindowManagerService.DEBUG_VISIBILITY;
40 static final boolean DEBUG_ANIM = WindowManagerService.DEBUG_ANIM;
41 static final boolean DEBUG_LAYERS = WindowManagerService.DEBUG_LAYERS;
42 static final boolean DEBUG_STARTING_WINDOW = WindowManagerService.DEBUG_STARTING_WINDOW;
43 static final boolean SHOW_TRANSACTIONS = WindowManagerService.SHOW_TRANSACTIONS;
44 static final boolean SHOW_LIGHT_TRANSACTIONS = WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
45 static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
46 static final boolean localLOGV = WindowManagerService.localLOGV;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -070047 static final boolean DEBUG_ORIENTATION = WindowManagerService.DEBUG_ORIENTATION;
Craig Mautner7358fbf2012-04-12 21:06:33 -070048 static final boolean DEBUG_SURFACE_TRACE = WindowManagerService.DEBUG_SURFACE_TRACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070049
50 static final String TAG = "WindowStateAnimator";
Craig Mautnera2c77052012-03-26 12:14:43 -070051
52 final WindowManagerService mService;
53 final WindowState mWin;
54 final WindowState mAttachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -070055 final WindowAnimator mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070056 final Session mSession;
57 final WindowManagerPolicy mPolicy;
58 final Context mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -070059
60 // Currently running animation.
61 boolean mAnimating;
62 boolean mLocalAnimating;
63 Animation mAnimation;
64 boolean mAnimationIsEntrance;
65 boolean mHasTransformation;
66 boolean mHasLocalTransformation;
67 final Transformation mTransformation = new Transformation();
68 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -070069 int mAnimLayer;
70 int mLastLayer;
71
72 Surface mSurface;
73 Surface mPendingDestroySurface;
74 boolean mReportDestroySurface;
75 boolean mSurfacePendingDestroy;
76
77 /**
78 * Set when we have changed the size of the surface, to know that
79 * we must tell them application to resize (and thus redraw itself).
80 */
81 boolean mSurfaceResized;
82
83 /**
84 * Set if the client has asked that the destroy of its surface be delayed
85 * until it explicitly says it is okay.
86 */
87 boolean mSurfaceDestroyDeferred;
88
Craig Mautner7d8df392012-04-06 15:26:23 -070089 float mShownAlpha = 0;
90 float mAlpha = 0;
91 float mLastAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070092
93 // Used to save animation distances between the time they are calculated and when they are
94 // used.
95 int mAnimDw;
96 int mAnimDh;
97 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
98 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
99
100 boolean mHaveMatrix;
101
102 // For debugging, this is the last information given to the surface flinger.
103 boolean mSurfaceShown;
104 float mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
105 int mSurfaceLayer;
106 float mSurfaceAlpha;
107
108 // Set to true if, when the window gets displayed, it should perform
109 // an enter animation.
110 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700111
Craig Mautner749a7bb2012-04-02 13:49:53 -0700112 /** This is set when there is no Surface */
113 static final int NO_SURFACE = 0;
114 /** This is set after the Surface has been created but before the window has been drawn. During
115 * this time the surface is hidden. */
116 static final int DRAW_PENDING = 1;
117 /** This is set after the window has finished drawing for the first time but before its surface
118 * is shown. The surface will be displayed when the next layout is run. */
119 static final int COMMIT_DRAW_PENDING = 2;
120 /** This is set during the time after the window's drawing has been committed, and before its
121 * surface is actually shown. It is used to delay showing the surface until all windows in a
122 * token are ready to be shown. */
123 static final int READY_TO_SHOW = 3;
124 /** Set when the window has been shown in the screen the first time. */
125 static final int HAS_DRAWN = 4;
126 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700127
Craig Mautner749a7bb2012-04-02 13:49:53 -0700128 /** Was this window last hidden? */
129 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700130
Craig Mautnerbec53f72012-04-05 11:49:05 -0700131 int mAttrFlags;
132 int mAttrType;
133
Craig Mautnera2c77052012-03-26 12:14:43 -0700134 public WindowStateAnimator(final WindowManagerService service, final WindowState win,
135 final WindowState attachedWindow) {
136 mService = service;
137 mWin = win;
138 mAttachedWindow = attachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700139 mAnimator = mService.mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700140 mSession = win.mSession;
141 mPolicy = mService.mPolicy;
142 mContext = mService.mContext;
Craig Mautnerbec53f72012-04-05 11:49:05 -0700143 mAttrFlags = win.mAttrs.flags;
144 mAttrType = win.mAttrs.type;
Craig Mautner9aa69582012-05-09 17:41:13 -0700145 mAnimDw = service.mAppDisplayWidth;
146 mAnimDh = service.mAppDisplayHeight;
Craig Mautnera2c77052012-03-26 12:14:43 -0700147 }
148
149 public void setAnimation(Animation anim) {
Craig Mautnerbec53f72012-04-05 11:49:05 -0700150 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700151 mAnimating = false;
152 mLocalAnimating = false;
153 mAnimation = anim;
154 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
155 mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
156 // Start out animation gone if window is gone, or visible if window is visible.
157 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700158 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700159 mHasLocalTransformation = true;
160 }
161
162 public void clearAnimation() {
163 if (mAnimation != null) {
164 mAnimating = true;
165 mLocalAnimating = false;
166 mAnimation.cancel();
167 mAnimation = null;
168 }
169 }
170
171 /** Is the window or its container currently animating? */
172 boolean isAnimating() {
173 final WindowState attached = mAttachedWindow;
174 final AppWindowToken atoken = mWin.mAppToken;
175 return mAnimation != null
176 || (attached != null && attached.mWinAnimator.mAnimation != null)
177 || (atoken != null &&
Craig Mautner59431632012-04-04 11:56:44 -0700178 (atoken.mAppAnimator.animation != null
Craig Mautnera2c77052012-03-26 12:14:43 -0700179 || atoken.inPendingTransaction));
180 }
181
Craig Mautner0afddcb2012-05-08 15:38:00 -0700182 /** Is the window animating the DummyAnimation? */
183 boolean isDummyAnimation() {
184 final AppWindowToken atoken = mWin.mAppToken;
185 return atoken != null
186 && atoken.mAppAnimator.animation == AppWindowAnimator.sDummyAnimation;
187 }
188
Craig Mautnera2c77052012-03-26 12:14:43 -0700189 /** Is this window currently animating? */
190 boolean isWindowAnimating() {
191 return mAnimation != null;
192 }
193
Craig Mautnera2c77052012-03-26 12:14:43 -0700194 void cancelExitAnimationForNextAnimationLocked() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700195 if (mAnimation != null) {
196 mAnimation.cancel();
197 mAnimation = null;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700198 mLocalAnimating = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700199 destroySurfaceLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700200 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700201 }
202
203 private boolean stepAnimation(long currentTime) {
204 if ((mAnimation == null) || !mLocalAnimating) {
205 return false;
206 }
207 mTransformation.clear();
208 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700209 if (DEBUG_ANIM) Slog.v(
210 TAG, "Stepped animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700211 ": more=" + more + ", xform=" + mTransformation);
212 return more;
213 }
214
215 // This must be called while inside a transaction. Returns true if
216 // there is more animation to run.
217 boolean stepAnimationLocked(long currentTime) {
218 // Save the animation state as it was before this step so WindowManagerService can tell if
219 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
220 mWasAnimating = mAnimating;
221 if (mService.okToDisplay()) {
222 // We will run animations as long as the display isn't frozen.
223
224 if (mWin.isDrawnLw() && mAnimation != null) {
225 mHasTransformation = true;
226 mHasLocalTransformation = true;
227 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700228 if (DEBUG_ANIM) Slog.v(
229 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700230 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
231 " wh=" + mWin.mFrame.height() +
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700232 " dw=" + mAnimDw + " dh=" + mAnimDh +
Craig Mautnera2c77052012-03-26 12:14:43 -0700233 " scale=" + mService.mWindowAnimationScale);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700234 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
235 mAnimDw, mAnimDh);
Craig Mautner9aa69582012-05-09 17:41:13 -0700236 mAnimDw = mService.mAppDisplayWidth;
237 mAnimDh = mService.mAppDisplayHeight;
Craig Mautnera2c77052012-03-26 12:14:43 -0700238 mAnimation.setStartTime(currentTime);
239 mLocalAnimating = true;
240 mAnimating = true;
241 }
242 if ((mAnimation != null) && mLocalAnimating) {
243 if (stepAnimation(currentTime)) {
244 return true;
245 }
246 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700247 if (DEBUG_ANIM) Slog.v(
248 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700249 " @ " + currentTime);
250 //WindowManagerService.this.dump();
251 }
252 mHasLocalTransformation = false;
253 if ((!mLocalAnimating || mAnimationIsEntrance) && mWin.mAppToken != null
Craig Mautner59431632012-04-04 11:56:44 -0700254 && mWin.mAppToken.mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700255 // When our app token is animating, we kind-of pretend like
256 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
257 // part of this check means that we will only do this if
258 // our window is not currently exiting, or it is not
259 // locally animating itself. The idea being that one that
260 // is exiting and doing a local animation should be removed
261 // once that animation is done.
262 mAnimating = true;
263 mHasTransformation = true;
264 mTransformation.clear();
265 return false;
266 } else if (mHasTransformation) {
267 // Little trick to get through the path below to act like
268 // we have finished an animation.
269 mAnimating = true;
270 } else if (isAnimating()) {
271 mAnimating = true;
272 }
273 } else if (mAnimation != null) {
274 // If the display is frozen, and there is a pending animation,
275 // clear it and make sure we run the cleanup code.
276 mAnimating = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 }
278
279 if (!mAnimating && !mLocalAnimating) {
280 return false;
281 }
282
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700283 // Done animating, clean up.
284 if (DEBUG_ANIM) Slog.v(
285 TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
Craig Mautnera2c77052012-03-26 12:14:43 -0700286 + ", reportedVisible="
287 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
288
289 mAnimating = false;
290 mLocalAnimating = false;
291 if (mAnimation != null) {
292 mAnimation.cancel();
293 mAnimation = null;
294 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700295 if (mAnimator.mWindowDetachedWallpaper == mWin) {
296 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700297 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700298 mAnimLayer = mWin.mLayer;
Craig Mautnera2c77052012-03-26 12:14:43 -0700299 if (mWin.mIsImWindow) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700300 mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700301 } else if (mWin.mIsWallpaper) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700302 mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700303 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700304 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
305 + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700306 mHasTransformation = false;
307 mHasLocalTransformation = false;
308 if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
309 if (WindowState.DEBUG_VISIBILITY) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700310 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
Craig Mautnera2c77052012-03-26 12:14:43 -0700311 + mWin.mPolicyVisibilityAfterAnim);
312 }
313 mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
314 mService.mLayoutNeeded = true;
315 if (!mWin.mPolicyVisibility) {
316 if (mService.mCurrentFocus == mWin) {
317 mService.mFocusMayChange = true;
318 }
319 // Window is no longer visible -- make sure if we were waiting
320 // for it to be displayed before enabling the display, that
321 // we allow the display to be enabled now.
322 mService.enableScreenIfNeededLocked();
323 }
324 }
325 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700326 if (mDrawState == HAS_DRAWN
Craig Mautnera2c77052012-03-26 12:14:43 -0700327 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
328 && mWin.mAppToken != null
329 && mWin.mAppToken.firstWindowDrawn
330 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700331 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700332 + mWin.mToken + ": first real window done animating");
333 mService.mFinishedStarting.add(mWin.mAppToken);
334 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
335 }
336
337 finishExit();
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700338 mAnimator.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
339 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
340 "WindowStateAnimator", mAnimator.mPendingLayoutChanges);
Craig Mautnera2c77052012-03-26 12:14:43 -0700341
342 if (mWin.mAppToken != null) {
343 mWin.mAppToken.updateReportedVisibilityLocked();
344 }
345
346 return false;
347 }
348
349 void finishExit() {
350 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700351 TAG, "finishExit in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700352 + ": exiting=" + mWin.mExiting
353 + " remove=" + mWin.mRemoveOnExit
354 + " windowAnimating=" + isWindowAnimating());
355
356 final int N = mWin.mChildWindows.size();
357 for (int i=0; i<N; i++) {
358 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
359 }
360
361 if (!mWin.mExiting) {
362 return;
363 }
364
365 if (isWindowAnimating()) {
366 return;
367 }
368
369 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700370 TAG, "Exit animation finished in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700371 + ": remove=" + mWin.mRemoveOnExit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700372 if (mSurface != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700373 mService.mDestroySurface.add(mWin);
374 mWin.mDestroying = true;
375 if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
376 mWin, "HIDE (finishExit)", null);
Craig Mautner0afddcb2012-05-08 15:38:00 -0700377 hide();
Craig Mautnera2c77052012-03-26 12:14:43 -0700378 }
379 mWin.mExiting = false;
380 if (mWin.mRemoveOnExit) {
381 mService.mPendingRemove.add(mWin);
382 mWin.mRemoveOnExit = false;
383 }
Craig Mautner9aa69582012-05-09 17:41:13 -0700384 if (mService.mWallpaperTarget == mWin && mService.mLowerWallpaperTarget == null) {
Craig Mautner0afddcb2012-05-08 15:38:00 -0700385 mAnimator.hideWallpapersLocked();
386 }
387 }
388
389 void hide() {
390 if (!mLastHidden) {
391 //dump();
392 mLastHidden = true;
393 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
394 "HIDE (performLayout)", null);
395 if (mSurface != null) {
396 mSurfaceShown = false;
397 try {
398 mSurface.hide();
399 } catch (RuntimeException e) {
400 Slog.w(TAG, "Exception hiding surface in " + mWin);
401 }
402 }
403 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700404 }
405
Craig Mautnera608b882012-03-30 13:03:49 -0700406 boolean finishDrawingLocked() {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700407 if (mDrawState == DRAW_PENDING) {
Craig Mautneref25d7a2012-05-15 23:01:47 -0700408 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
409 Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + this + " in "
Craig Mautner83339b42012-05-01 22:13:23 -0700410 + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700411 mDrawState = COMMIT_DRAW_PENDING;
Craig Mautnera608b882012-03-30 13:03:49 -0700412 return true;
413 }
414 return false;
415 }
416
417 // This must be called while inside a transaction.
418 boolean commitFinishDrawingLocked(long currentTime) {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700419 if (mDrawState != COMMIT_DRAW_PENDING) {
Craig Mautnera608b882012-03-30 13:03:49 -0700420 return false;
421 }
Craig Mautneref25d7a2012-05-15 23:01:47 -0700422 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
Craig Mautner83339b42012-05-01 22:13:23 -0700423 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700424 mDrawState = READY_TO_SHOW;
Craig Mautnera608b882012-03-30 13:03:49 -0700425 final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
426 final AppWindowToken atoken = mWin.mAppToken;
427 if (atoken == null || atoken.allDrawn || starting) {
428 performShowLocked();
429 }
430 return true;
431 }
432
Craig Mautner7d8df392012-04-06 15:26:23 -0700433 static class SurfaceTrace extends Surface {
434 private final static String SURFACE_TAG = "SurfaceTrace";
435 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700436
Craig Mautner7d8df392012-04-06 15:26:23 -0700437 private float mSurfaceTraceAlpha = 0;
Craig Mautner7358fbf2012-04-12 21:06:33 -0700438 private int mLayer;
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700439 private final PointF mPosition = new PointF();
440 private final Point mSize = new Point();
441 private final Rect mWindowCrop = new Rect();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700442 private boolean mShown = false;
443 private String mName = "Not named";
444
Craig Mautner7d8df392012-04-06 15:26:23 -0700445 public SurfaceTrace(SurfaceSession s,
Craig Mautner7358fbf2012-04-12 21:06:33 -0700446 int pid, int display, int w, int h, int format, int flags) throws
447 OutOfResourcesException {
448 super(s, pid, display, w, h, format, flags);
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700449 mSize.set(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700450 Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700451 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700452 }
453
Craig Mautner7d8df392012-04-06 15:26:23 -0700454 public SurfaceTrace(SurfaceSession s,
Craig Mautner7358fbf2012-04-12 21:06:33 -0700455 int pid, String name, int display, int w, int h, int format, int flags)
456 throws OutOfResourcesException {
457 super(s, pid, name, display, w, h, format, flags);
458 mName = name;
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700459 mSize.set(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700460 Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700461 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700462 }
463
464 @Override
465 public void setAlpha(float alpha) {
466 super.setAlpha(alpha);
Craig Mautner7d8df392012-04-06 15:26:23 -0700467 mSurfaceTraceAlpha = alpha;
468 Slog.v(SURFACE_TAG, "setAlpha: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700469 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700470 }
471
472 @Override
473 public void setLayer(int zorder) {
474 super.setLayer(zorder);
475 mLayer = zorder;
Craig Mautner7d8df392012-04-06 15:26:23 -0700476 Slog.v(SURFACE_TAG, "setLayer: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700477 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700478
479 sSurfaces.remove(this);
480 int i;
481 for (i = sSurfaces.size() - 1; i >= 0; i--) {
Craig Mautner7d8df392012-04-06 15:26:23 -0700482 SurfaceTrace s = sSurfaces.get(i);
Craig Mautner7358fbf2012-04-12 21:06:33 -0700483 if (s.mLayer < zorder) {
484 break;
485 }
486 }
487 sSurfaces.add(i + 1, this);
488 }
489
490 @Override
491 public void setPosition(float x, float y) {
492 super.setPosition(x, y);
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700493 mPosition.set(x, y);
Craig Mautner7d8df392012-04-06 15:26:23 -0700494 Slog.v(SURFACE_TAG, "setPosition: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700495 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700496 }
497
498 @Override
499 public void setSize(int w, int h) {
500 super.setSize(w, h);
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700501 mSize.set(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700502 Slog.v(SURFACE_TAG, "setSize: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700503 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700504 }
505
506 @Override
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700507 public void setWindowCrop(Rect crop) {
508 super.setWindowCrop(crop);
Craig Mautneref25d7a2012-05-15 23:01:47 -0700509 if (crop != null) {
510 mWindowCrop.set(crop);
511 }
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700512 Slog.v(SURFACE_TAG, "setWindowCrop: " + this + ". Called by "
513 + Debug.getCallers(3));
514 }
515
516 @Override
Craig Mautner7358fbf2012-04-12 21:06:33 -0700517 public void hide() {
518 super.hide();
519 mShown = false;
Craig Mautner7d8df392012-04-06 15:26:23 -0700520 Slog.v(SURFACE_TAG, "hide: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700521 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700522 }
523 @Override
524 public void show() {
525 super.show();
526 mShown = true;
Craig Mautner7d8df392012-04-06 15:26:23 -0700527 Slog.v(SURFACE_TAG, "show: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700528 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700529 }
530
531 @Override
532 public void destroy() {
533 super.destroy();
Craig Mautner7d8df392012-04-06 15:26:23 -0700534 Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700535 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700536 sSurfaces.remove(this);
537 }
538
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700539 @Override
540 public void release() {
541 super.release();
542 Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700543 + Debug.getCallers(3));
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700544 sSurfaces.remove(this);
545 }
546
Craig Mautner7358fbf2012-04-12 21:06:33 -0700547 static void dumpAllSurfaces() {
548 final int N = sSurfaces.size();
549 for (int i = 0; i < N; i++) {
550 Slog.i(TAG, "SurfaceDump: " + sSurfaces.get(i));
551 }
552 }
553
554 @Override
555 public String toString() {
Craig Mautnerfbf378c2012-04-23 17:24:21 -0700556 return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
557 + mName + ": shown=" + mShown + " layer=" + mLayer
Craig Mautner7d8df392012-04-06 15:26:23 -0700558 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700559 + " " + mSize.x + "x" + mSize.y
560 + " crop=" + mWindowCrop.toShortString();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700561 }
562 }
563
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700564 Surface createSurfaceLocked() {
565 if (mSurface == null) {
566 mReportDestroySurface = false;
567 mSurfacePendingDestroy = false;
Craig Mautner83339b42012-05-01 22:13:23 -0700568 if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
569 "createSurface " + this + ": mDrawState=DRAW_PENDING");
Craig Mautner749a7bb2012-04-02 13:49:53 -0700570 mDrawState = DRAW_PENDING;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700571 if (mWin.mAppToken != null) {
572 mWin.mAppToken.allDrawn = false;
573 }
574
575 mService.makeWindowFreezingScreenIfNeededLocked(mWin);
576
577 int flags = 0;
578 final WindowManager.LayoutParams attrs = mWin.mAttrs;
579
580 if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
581 flags |= Surface.SECURE;
582 }
583 if (WindowState.DEBUG_VISIBILITY) Slog.v(
584 TAG, "Creating surface in session "
585 + mSession.mSurfaceSession + " window " + this
586 + " w=" + mWin.mCompatFrame.width()
587 + " h=" + mWin.mCompatFrame.height() + " format="
588 + attrs.format + " flags=" + flags);
589
590 int w = mWin.mCompatFrame.width();
591 int h = mWin.mCompatFrame.height();
592 if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
593 // for a scaled surface, we always want the requested
594 // size.
595 w = mWin.mRequestedWidth;
596 h = mWin.mRequestedHeight;
597 }
598
599 // Something is wrong and SurfaceFlinger will not like this,
600 // try to revert to sane values
601 if (w <= 0) w = 1;
602 if (h <= 0) h = 1;
603
604 mSurfaceShown = false;
605 mSurfaceLayer = 0;
Craig Mautner7d8df392012-04-06 15:26:23 -0700606 mSurfaceAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700607 mSurfaceX = 0;
608 mSurfaceY = 0;
609 mSurfaceW = w;
610 mSurfaceH = h;
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700611 mWin.mLastSystemDecorRect.set(0, 0, 0, 0);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700612 try {
613 final boolean isHwAccelerated = (attrs.flags &
614 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
615 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
616 if (!PixelFormat.formatHasAlpha(attrs.format)) {
617 flags |= Surface.OPAQUE;
618 }
Craig Mautner7358fbf2012-04-12 21:06:33 -0700619 if (DEBUG_SURFACE_TRACE) {
Craig Mautner7d8df392012-04-06 15:26:23 -0700620 mSurface = new SurfaceTrace(
Craig Mautner7358fbf2012-04-12 21:06:33 -0700621 mSession.mSurfaceSession, mSession.mPid,
622 attrs.getTitle().toString(),
623 0, w, h, format, flags);
624 } else {
625 mSurface = new Surface(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700626 mSession.mSurfaceSession, mSession.mPid,
627 attrs.getTitle().toString(),
628 0, w, h, format, flags);
Craig Mautner7358fbf2012-04-12 21:06:33 -0700629 }
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700630 mWin.mHasSurface = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700631 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
632 " CREATE SURFACE "
633 + mSurface + " IN SESSION "
634 + mSession.mSurfaceSession
635 + ": pid=" + mSession.mPid + " format="
636 + attrs.format + " flags=0x"
637 + Integer.toHexString(flags)
638 + " / " + this);
639 } catch (Surface.OutOfResourcesException e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700640 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700641 Slog.w(TAG, "OutOfResourcesException creating surface");
642 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700643 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700644 return null;
645 } catch (Exception e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700646 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700647 Slog.e(TAG, "Exception creating surface", e);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700648 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700649 return null;
650 }
651
652 if (WindowManagerService.localLOGV) Slog.v(
653 TAG, "Got surface: " + mSurface
654 + ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
655 + ", animLayer=" + mAnimLayer);
656 if (SHOW_LIGHT_TRANSACTIONS) {
657 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
658 WindowManagerService.logSurface(mWin, "CREATE pos=("
659 + mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
660 + mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
661 + "), layer=" + mAnimLayer + " HIDE", null);
662 }
663 Surface.openTransaction();
664 try {
665 try {
666 mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
667 mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
668 mSurface.setPosition(mSurfaceX, mSurfaceY);
669 mSurfaceLayer = mAnimLayer;
670 mSurface.setLayer(mAnimLayer);
Craig Mautner7d8df392012-04-06 15:26:23 -0700671 mSurface.setAlpha(0);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700672 mSurfaceShown = false;
673 mSurface.hide();
674 if ((mWin.mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
675 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "DITHER", null);
676 mSurface.setFlags(Surface.SURFACE_DITHER, Surface.SURFACE_DITHER);
677 }
678 } catch (RuntimeException e) {
679 Slog.w(TAG, "Error creating surface in " + w, e);
680 mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
681 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700682 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700683 } finally {
684 Surface.closeTransaction();
685 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
686 "<<< CLOSE TRANSACTION createSurfaceLocked");
687 }
688 if (WindowManagerService.localLOGV) Slog.v(
689 TAG, "Created surface " + this);
690 }
691 return mSurface;
692 }
693
694 void destroySurfaceLocked() {
695 if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
696 mWin.mAppToken.startingDisplayed = false;
697 }
698
Craig Mautner749a7bb2012-04-02 13:49:53 -0700699 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700700 if (mSurface != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700701
702 int i = mWin.mChildWindows.size();
703 while (i > 0) {
704 i--;
705 WindowState c = mWin.mChildWindows.get(i);
706 c.mAttachedHidden = true;
707 }
708
709 if (mReportDestroySurface) {
710 mReportDestroySurface = false;
711 mSurfacePendingDestroy = true;
712 try {
713 mWin.mClient.dispatchGetNewSurface();
714 // We'll really destroy on the next time around.
715 return;
716 } catch (RemoteException e) {
717 }
718 }
719
720 try {
721 if (DEBUG_VISIBILITY) {
722 RuntimeException e = null;
723 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
724 e = new RuntimeException();
725 e.fillInStackTrace();
726 }
727 Slog.w(TAG, "Window " + this + " destroying surface "
728 + mSurface + ", session " + mSession, e);
729 }
730 if (mSurfaceDestroyDeferred) {
731 if (mSurface != null && mPendingDestroySurface != mSurface) {
732 if (mPendingDestroySurface != null) {
733 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
734 RuntimeException e = null;
735 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
736 e = new RuntimeException();
737 e.fillInStackTrace();
738 }
739 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
740 }
741 mPendingDestroySurface.destroy();
742 }
743 mPendingDestroySurface = mSurface;
744 }
745 } else {
746 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
747 RuntimeException e = null;
748 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
749 e = new RuntimeException();
750 e.fillInStackTrace();
751 }
752 WindowManagerService.logSurface(mWin, "DESTROY", e);
753 }
754 mSurface.destroy();
755 }
756 } catch (RuntimeException e) {
757 Slog.w(TAG, "Exception thrown when destroying Window " + this
758 + " surface " + mSurface + " session " + mSession
759 + ": " + e.toString());
760 }
761
762 mSurfaceShown = false;
763 mSurface = null;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700764 mWin.mHasSurface =false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700765 }
766 }
767
768 void destroyDeferredSurfaceLocked() {
769 try {
770 if (mPendingDestroySurface != null) {
771 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
772 RuntimeException e = null;
773 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
774 e = new RuntimeException();
775 e.fillInStackTrace();
776 }
777 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
778 }
779 mPendingDestroySurface.destroy();
780 }
781 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700782 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700783 + this + " surface " + mPendingDestroySurface
784 + " session " + mSession + ": " + e.toString());
785 }
786 mSurfaceDestroyDeferred = false;
787 mPendingDestroySurface = null;
788 }
789
790 void computeShownFrameLocked() {
791 final boolean selfTransformation = mHasLocalTransformation;
792 Transformation attachedTransformation =
793 (mAttachedWindow != null && mAttachedWindow.mWinAnimator.mHasLocalTransformation)
794 ? mAttachedWindow.mWinAnimator.mTransformation : null;
Craig Mautner59431632012-04-04 11:56:44 -0700795 final AppWindowAnimator appAnimator =
796 mWin.mAppToken == null ? null : mWin.mAppToken.mAppAnimator;
797 Transformation appTransformation = (appAnimator != null && appAnimator.hasTransformation)
798 ? appAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700799
800 // Wallpapers are animated based on the "real" window they
801 // are currently targeting.
802 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
803 && mService.mWallpaperTarget != null) {
804 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
805 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
806 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
807 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
808 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
809 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
810 }
811 }
Craig Mautner59431632012-04-04 11:56:44 -0700812 final AppWindowAnimator wpAppAnimator = mService.mWallpaperTarget.mAppToken == null
813 ? null : mService.mWallpaperTarget.mAppToken.mAppAnimator;
814 if (wpAppAnimator != null &&
815 wpAppAnimator.hasTransformation &&
816 wpAppAnimator.animation != null &&
817 !wpAppAnimator.animation.getDetachWallpaper()) {
818 appTransformation = wpAppAnimator.transformation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700819 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
820 Slog.v(TAG, "WP target app xform: " + appTransformation);
821 }
822 }
823 }
824
825 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
826 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
827 if (selfTransformation || attachedTransformation != null
828 || appTransformation != null || screenAnimation) {
829 // cache often used attributes locally
830 final Rect frame = mWin.mFrame;
831 final float tmpFloats[] = mService.mTmpFloats;
832 final Matrix tmpMatrix = mWin.mTmpMatrix;
833
834 // Compute the desired transformation.
835 if (screenAnimation) {
836 // If we are doing a screen animation, the global rotation
837 // applied to windows can result in windows that are carefully
838 // aligned with each other to slightly separate, allowing you
839 // to see what is behind them. An unsightly mess. This...
840 // thing... magically makes it call good: scale each window
841 // slightly (two pixels larger in each dimension, from the
842 // window's center).
843 final float w = frame.width();
844 final float h = frame.height();
845 if (w>=1 && h>=1) {
846 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
847 } else {
848 tmpMatrix.reset();
849 }
850 } else {
851 tmpMatrix.reset();
852 }
853 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
854 if (selfTransformation) {
855 tmpMatrix.postConcat(mTransformation.getMatrix());
856 }
857 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
858 if (attachedTransformation != null) {
859 tmpMatrix.postConcat(attachedTransformation.getMatrix());
860 }
861 if (appTransformation != null) {
862 tmpMatrix.postConcat(appTransformation.getMatrix());
863 }
864 if (screenAnimation) {
865 tmpMatrix.postConcat(
866 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
867 }
868
869 // "convert" it into SurfaceFlinger's format
870 // (a 2x2 matrix + an offset)
871 // Here we must not transform the position of the surface
872 // since it is already included in the transformation.
873 //Slog.i(TAG, "Transform: " + matrix);
874
875 mHaveMatrix = true;
876 tmpMatrix.getValues(tmpFloats);
877 mDsDx = tmpFloats[Matrix.MSCALE_X];
878 mDtDx = tmpFloats[Matrix.MSKEW_Y];
879 mDsDy = tmpFloats[Matrix.MSKEW_X];
880 mDtDy = tmpFloats[Matrix.MSCALE_Y];
881 float x = tmpFloats[Matrix.MTRANS_X];
882 float y = tmpFloats[Matrix.MTRANS_Y];
883 int w = frame.width();
884 int h = frame.height();
885 mWin.mShownFrame.set(x, y, x+w, y+h);
886
887 // Now set the alpha... but because our current hardware
888 // can't do alpha transformation on a non-opaque surface,
889 // turn it off if we are running an animation that is also
890 // transforming since it is more important to have that
891 // animation be smooth.
892 mShownAlpha = mAlpha;
893 if (!mService.mLimitedAlphaCompositing
894 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
895 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
896 && x == frame.left && y == frame.top))) {
897 //Slog.i(TAG, "Applying alpha transform");
898 if (selfTransformation) {
899 mShownAlpha *= mTransformation.getAlpha();
900 }
901 if (attachedTransformation != null) {
902 mShownAlpha *= attachedTransformation.getAlpha();
903 }
904 if (appTransformation != null) {
905 mShownAlpha *= appTransformation.getAlpha();
906 }
907 if (screenAnimation) {
908 mShownAlpha *=
909 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
910 }
911 } else {
912 //Slog.i(TAG, "Not applying alpha transform");
913 }
914
915 if (WindowManagerService.localLOGV) Slog.v(
916 TAG, "computeShownFrameLocked: Animating " + this +
917 ": " + mWin.mShownFrame +
918 ", alpha=" + mTransformation.getAlpha() + ", mShownAlpha=" + mShownAlpha);
919 return;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700920 } else if (mWin.mIsWallpaper &&
921 (mAnimator.mPendingActions & WindowAnimator.WALLPAPER_ACTION_PENDING) != 0) {
922 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700923 }
924
925 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautner4d7349b2012-04-20 14:52:47 -0700926 TAG, "computeShownFrameLocked: " + this +
927 " not attached, mAlpha=" + mAlpha);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700928 mWin.mShownFrame.set(mWin.mFrame);
929 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
930 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
931 }
932 mShownAlpha = mAlpha;
933 mHaveMatrix = false;
934 mDsDx = mWin.mGlobalScale;
935 mDtDx = 0;
936 mDsDy = 0;
937 mDtDy = mWin.mGlobalScale;
938 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700939
Dianne Hackborn3e52fc22012-05-15 17:58:02 -0700940 void updateSurfaceWindowCrop(final boolean recoveringMemory) {
941 final WindowState w = mWin;
942
943 // Need to recompute a new system decor rect each time.
944 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
945 // Currently can't do this cropping for scaled windows. We'll
946 // just keep the crop rect the same as the source surface.
947 w.mSystemDecorRect.set(0, 0, w.mRequestedWidth, w.mRequestedHeight);
948 } else if (w.mLayer >= mService.mSystemDecorLayer) {
949 // Above the decor layer is easy, just use the entire window.
950 w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(),
951 w.mCompatFrame.height());
952 } else {
953 final Rect decorRect = mService.mSystemDecorRect;
954 // Compute the offset of the window in relation to the decor rect.
955 final int offX = w.mXOffset + w.mFrame.left;
956 final int offY = w.mYOffset + w.mFrame.top;
957 // Initialize the decor rect to the entire frame.
958 w.mSystemDecorRect.set(0, 0, w.mFrame.width(), w.mFrame.height());
959 // Intersect with the decor rect, offsetted by window position.
960 w.mSystemDecorRect.intersect(decorRect.left-offX, decorRect.top-offY,
961 decorRect.right-offX, decorRect.bottom-offY);
962 // If size compatibility is being applied to the window, the
963 // surface is scaled relative to the screen. Also apply this
964 // scaling to the crop rect. We aren't using the standard rect
965 // scale function because we want to round things to make the crop
966 // always round to a larger rect to ensure we don't crop too
967 // much and hide part of the window that should be seen.
968 if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
969 final float scale = w.mInvGlobalScale;
970 w.mSystemDecorRect.left = (int) (w.mSystemDecorRect.left * scale - 0.5f);
971 w.mSystemDecorRect.top = (int) (w.mSystemDecorRect.top * scale - 0.5f);
972 w.mSystemDecorRect.right = (int) ((w.mSystemDecorRect.right+1) * scale - 0.5f);
973 w.mSystemDecorRect.bottom = (int) ((w.mSystemDecorRect.bottom+1) * scale - 0.5f);
974 }
975 }
976
977 if (!w.mSystemDecorRect.equals(w.mLastSystemDecorRect)) {
978 w.mLastSystemDecorRect.set(w.mSystemDecorRect);
979 try {
980 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
981 "CROP " + w.mSystemDecorRect.toShortString(), null);
982 mSurface.setWindowCrop(w.mSystemDecorRect);
983 } catch (RuntimeException e) {
984 Slog.w(TAG, "Error setting crop surface of " + w
985 + " crop=" + w.mSystemDecorRect.toShortString(), e);
986 if (!recoveringMemory) {
987 mService.reclaimSomeSurfaceMemoryLocked(this, "crop", true);
988 }
989 }
990 }
991 }
992
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700993 void setSurfaceBoundaries(final boolean recoveringMemory) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700994 final WindowState w = mWin;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700995 int width, height;
996 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
997 // for a scaled surface, we just want to use
998 // the requested size.
999 width = w.mRequestedWidth;
1000 height = w.mRequestedHeight;
1001 } else {
1002 width = w.mCompatFrame.width();
1003 height = w.mCompatFrame.height();
1004 }
1005
1006 if (width < 1) {
1007 width = 1;
1008 }
1009 if (height < 1) {
1010 height = 1;
1011 }
1012 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
1013 if (surfaceResized) {
1014 mSurfaceW = width;
1015 mSurfaceH = height;
1016 }
1017
Craig Mautner4d7349b2012-04-20 14:52:47 -07001018 final float left = w.mShownFrame.left;
1019 final float top = w.mShownFrame.top;
1020 if (mSurfaceX != left || mSurfaceY != top) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001021 try {
1022 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
Craig Mautner4d7349b2012-04-20 14:52:47 -07001023 "POS " + left + ", " + top, null);
1024 mSurfaceX = left;
1025 mSurfaceY = top;
1026 mSurface.setPosition(left, top);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001027 } catch (RuntimeException e) {
1028 Slog.w(TAG, "Error positioning surface of " + w
Craig Mautner4d7349b2012-04-20 14:52:47 -07001029 + " pos=(" + left
1030 + "," + top + ")", e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001031 if (!recoveringMemory) {
1032 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
1033 }
1034 }
1035 }
1036
1037 if (surfaceResized) {
1038 try {
1039 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1040 "SIZE " + width + "x" + height, null);
1041 mSurfaceResized = true;
1042 mSurface.setSize(width, height);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001043 mAnimator.mPendingLayoutChanges |=
1044 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Craig Mautner3a67f352012-05-07 11:21:33 -07001045 if ((w.mAttrs.flags & LayoutParams.FLAG_DIM_BEHIND) != 0) {
1046 mAnimator.startDimming(this, w.mExiting ? 0 : w.mAttrs.dimAmount,
1047 mService.mAppDisplayWidth, mService.mAppDisplayHeight);
1048 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001049 } catch (RuntimeException e) {
1050 // If something goes wrong with the surface (such
1051 // as running out of memory), don't take down the
1052 // entire system.
1053 Slog.e(TAG, "Error resizing surface of " + w
1054 + " size=(" + width + "x" + height + ")", e);
1055 if (!recoveringMemory) {
1056 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
1057 }
1058 }
1059 }
Dianne Hackborn85afd1b2012-05-13 13:31:06 -07001060
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001061 updateSurfaceWindowCrop(recoveringMemory);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001062 }
1063
1064 public void prepareSurfaceLocked(final boolean recoveringMemory) {
1065 final WindowState w = mWin;
1066 if (mSurface == null) {
1067 if (w.mOrientationChanging) {
1068 if (DEBUG_ORIENTATION) {
1069 Slog.v(TAG, "Orientation change skips hidden " + w);
1070 }
1071 w.mOrientationChanging = false;
1072 }
1073 return;
1074 }
1075
1076 boolean displayed = false;
1077
1078 computeShownFrameLocked();
1079
1080 setSurfaceBoundaries(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001081
Craig Mautnerde6198e2012-04-19 09:59:31 -07001082 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
Craig Mautner0afddcb2012-05-08 15:38:00 -07001083 hide();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001084 // If we are waiting for this window to handle an
1085 // orientation change, well, it is hidden, so
1086 // doesn't really matter. Note that this does
1087 // introduce a potential glitch if the window
1088 // becomes unhidden before it has drawn for the
1089 // new orientation.
1090 if (w.mOrientationChanging) {
1091 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001092 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001093 "Orientation change skips hidden " + w);
1094 }
1095 } else if (mLastLayer != mAnimLayer
1096 || mLastAlpha != mShownAlpha
1097 || mLastDsDx != mDsDx
1098 || mLastDtDx != mDtDx
1099 || mLastDsDy != mDsDy
1100 || mLastDtDy != mDtDy
1101 || w.mLastHScale != w.mHScale
1102 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001103 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001104 displayed = true;
1105 mLastAlpha = mShownAlpha;
1106 mLastLayer = mAnimLayer;
1107 mLastDsDx = mDsDx;
1108 mLastDtDx = mDtDx;
1109 mLastDsDy = mDsDy;
1110 mLastDtDy = mDtDy;
1111 w.mLastHScale = w.mHScale;
1112 w.mLastVScale = w.mVScale;
1113 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1114 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
1115 + " matrix=[" + (mDsDx*w.mHScale)
1116 + "," + (mDtDx*w.mVScale)
1117 + "][" + (mDsDy*w.mHScale)
1118 + "," + (mDtDy*w.mVScale) + "]", null);
1119 if (mSurface != null) {
1120 try {
1121 mSurfaceAlpha = mShownAlpha;
1122 mSurface.setAlpha(mShownAlpha);
1123 mSurfaceLayer = w.mWinAnimator.mAnimLayer;
1124 mSurface.setLayer(w.mWinAnimator.mAnimLayer);
1125 mSurface.setMatrix(
1126 mDsDx*w.mHScale, mDtDx*w.mVScale,
1127 mDsDy*w.mHScale, mDtDy*w.mVScale);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001128
1129 if (mLastHidden && mDrawState == HAS_DRAWN) {
1130 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1131 "SHOW (performLayout)", null);
1132 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
1133 + " during relayout");
1134 if (showSurfaceRobustlyLocked()) {
1135 mLastHidden = false;
1136 } else {
1137 w.mOrientationChanging = false;
1138 }
1139 }
1140 if (mSurface != null) {
1141 w.mToken.hasVisible = true;
1142 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001143 } catch (RuntimeException e) {
1144 Slog.w(TAG, "Error updating surface in " + w, e);
1145 if (!recoveringMemory) {
1146 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
1147 }
1148 }
1149 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001150 } else {
Craig Mautner83339b42012-05-01 22:13:23 -07001151 if (DEBUG_ANIM) {
1152 Slog.v(TAG, "prepareSurface: No changes in animation for " + mWin);
1153 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001154 displayed = true;
1155 }
1156
1157 if (displayed) {
1158 if (w.mOrientationChanging) {
1159 if (!w.isDrawnLw()) {
Craig Mautnerd09cc4b2012-04-04 10:23:31 -07001160 mAnimator.mBulkUpdateParams |= CLEAR_ORIENTATION_CHANGE_COMPLETE;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001161 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001162 "Orientation continue waiting for draw in " + w);
1163 } else {
1164 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001165 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001166 }
1167 }
1168 w.mToken.hasVisible = true;
1169 }
1170 }
1171
Craig Mautner48ba1e72012-04-02 13:18:16 -07001172 void setTransparentRegionHint(final Region region) {
Craig Mautner1f4e0cc2012-04-10 14:24:38 -07001173 if (mSurface == null) {
1174 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1175 return;
1176 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001177 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1178 ">>> OPEN TRANSACTION setTransparentRegion");
1179 Surface.openTransaction();
1180 try {
1181 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1182 "transparentRegionHint=" + region, null);
1183 mSurface.setTransparentRegionHint(region);
1184 } finally {
1185 Surface.closeTransaction();
1186 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1187 "<<< CLOSE TRANSACTION setTransparentRegion");
1188 }
1189 }
1190
1191 void setWallpaperOffset(int left, int top) {
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001192 mSurfaceX = left;
1193 mSurfaceY = top;
1194 if (mAnimating) {
1195 // If this window (or its app token) is animating, then the position
1196 // of the surface will be re-computed on the next animation frame.
1197 // We can't poke it directly here because it depends on whatever
1198 // transformation is being applied by the animation.
1199 return;
1200 }
1201 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1202 ">>> OPEN TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001203 Surface.openTransaction();
1204 try {
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001205 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1206 "POS " + left + ", " + top, null);
1207 mSurface.setPosition(mWin.mFrame.left + left, mWin.mFrame.top + top);
1208 updateSurfaceWindowCrop(false);
Craig Mautner48ba1e72012-04-02 13:18:16 -07001209 } catch (RuntimeException e) {
1210 Slog.w(TAG, "Error positioning surface of " + mWin
1211 + " pos=(" + left + "," + top + ")", e);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001212 } finally {
1213 Surface.closeTransaction();
1214 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1215 "<<< CLOSE TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001216 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001217 }
1218
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001219 // This must be called while inside a transaction.
1220 boolean performShowLocked() {
1221 if (DEBUG_VISIBILITY) {
1222 RuntimeException e = null;
1223 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1224 e = new RuntimeException();
1225 e.fillInStackTrace();
1226 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001227 Slog.v(TAG, "performShow on " + this
Craig Mautner749a7bb2012-04-02 13:49:53 -07001228 + ": mDrawState=" + mDrawState + " readyForDisplay="
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001229 + mWin.isReadyForDisplay()
1230 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
1231 }
Craig Mautner749a7bb2012-04-02 13:49:53 -07001232 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplay()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001233 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001234 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -07001235 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001236 + " during animation: policyVis=" + mWin.mPolicyVisibility
1237 + " attHidden=" + mWin.mAttachedHidden
1238 + " tok.hiddenRequested="
1239 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1240 + " tok.hidden="
1241 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1242 + " animating=" + mAnimating
1243 + " tok animating="
Craig Mautner59431632012-04-04 11:56:44 -07001244 + (mWin.mAppToken != null ? mWin.mAppToken.mAppAnimator.animating : false));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001245
1246 mService.enableScreenIfNeededLocked();
1247
1248 applyEnterAnimationLocked();
1249
Craig Mautnerde6198e2012-04-19 09:59:31 -07001250 // Force the show in the next prepareSurfaceLocked() call.
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001251 mLastAlpha = -1;
Craig Mautneref25d7a2012-05-15 23:01:47 -07001252 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
1253 Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + this);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001254 mDrawState = HAS_DRAWN;
Craig Mautnerde6198e2012-04-19 09:59:31 -07001255 mService.scheduleAnimationLocked();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001256
1257 int i = mWin.mChildWindows.size();
1258 while (i > 0) {
1259 i--;
1260 WindowState c = mWin.mChildWindows.get(i);
1261 if (c.mAttachedHidden) {
1262 c.mAttachedHidden = false;
1263 if (c.mWinAnimator.mSurface != null) {
1264 c.mWinAnimator.performShowLocked();
1265 // It hadn't been shown, which means layout not
1266 // performed on it, so now we want to make sure to
1267 // do a layout. If called from within the transaction
1268 // loop, this will cause it to restart with a new
1269 // layout.
1270 mService.mLayoutNeeded = true;
1271 }
1272 }
1273 }
1274
1275 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1276 && mWin.mAppToken != null) {
1277 mWin.mAppToken.firstWindowDrawn = true;
1278
1279 if (mWin.mAppToken.startingData != null) {
1280 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001281 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001282 "Finish starting " + mWin.mToken
1283 + ": first real window is shown, no animation");
1284 // If this initial window is animating, stop it -- we
1285 // will do an animation to reveal it from behind the
1286 // starting window, so there is no need for it to also
1287 // be doing its own stuff.
Craig Mautner4d7349b2012-04-20 14:52:47 -07001288 clearAnimation();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001289 mService.mFinishedStarting.add(mWin.mAppToken);
1290 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1291 }
1292 mWin.mAppToken.updateReportedVisibilityLocked();
1293 }
1294
1295 return true;
1296 }
1297
1298 return false;
1299 }
1300
1301 /**
1302 * Have the surface flinger show a surface, robustly dealing with
1303 * error conditions. In particular, if there is not enough memory
1304 * to show the surface, then we will try to get rid of other surfaces
1305 * in order to succeed.
1306 *
1307 * @return Returns true if the surface was successfully shown.
1308 */
1309 boolean showSurfaceRobustlyLocked() {
1310 try {
1311 if (mSurface != null) {
1312 mSurfaceShown = true;
1313 mSurface.show();
1314 if (mWin.mTurnOnScreen) {
1315 if (DEBUG_VISIBILITY) Slog.v(TAG,
1316 "Show surface turning screen on: " + mWin);
1317 mWin.mTurnOnScreen = false;
Craig Mautner7d8df392012-04-06 15:26:23 -07001318 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001319 }
1320 }
1321 return true;
1322 } catch (RuntimeException e) {
1323 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1324 }
1325
1326 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1327
1328 return false;
1329 }
1330
1331 void applyEnterAnimationLocked() {
1332 final int transit;
1333 if (mEnterAnimationPending) {
1334 mEnterAnimationPending = false;
1335 transit = WindowManagerPolicy.TRANSIT_ENTER;
1336 } else {
1337 transit = WindowManagerPolicy.TRANSIT_SHOW;
1338 }
1339
1340 applyAnimationLocked(transit, true);
1341 }
1342
Craig Mautner48ba1e72012-04-02 13:18:16 -07001343 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001344 /**
1345 * Choose the correct animation and set it to the passed WindowState.
1346 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1347 * then the animation will be app_starting_exit. Any other value loads the animation from
1348 * the switch statement below.
1349 * @param isEntrance The animation type the last time this was called. Used to keep from
1350 * loading the same animation twice.
1351 * @return true if an animation has been loaded.
1352 */
1353 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1354 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1355 // If we are trying to apply an animation, but already running
1356 // an animation of the same type, then just leave that one alone.
1357 return true;
1358 }
1359
1360 // Only apply an animation if the display isn't frozen. If it is
1361 // frozen, there is no reason to animate and it can cause strange
1362 // artifacts when we unfreeze the display if some different animation
1363 // is running.
1364 if (mService.okToDisplay()) {
1365 int anim = mPolicy.selectAnimationLw(mWin, transit);
1366 int attr = -1;
1367 Animation a = null;
1368 if (anim != 0) {
1369 a = AnimationUtils.loadAnimation(mContext, anim);
1370 } else {
1371 switch (transit) {
1372 case WindowManagerPolicy.TRANSIT_ENTER:
1373 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1374 break;
1375 case WindowManagerPolicy.TRANSIT_EXIT:
1376 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1377 break;
1378 case WindowManagerPolicy.TRANSIT_SHOW:
1379 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1380 break;
1381 case WindowManagerPolicy.TRANSIT_HIDE:
1382 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1383 break;
1384 }
1385 if (attr >= 0) {
1386 a = mService.loadAnimation(mWin.mAttrs, attr);
1387 }
1388 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001389 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001390 "applyAnimation: win=" + this
1391 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
Craig Mautner4d7349b2012-04-20 14:52:47 -07001392 + " a=" + a
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001393 + " mAnimation=" + mAnimation
Craig Mautner83339b42012-05-01 22:13:23 -07001394 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001395 if (a != null) {
1396 if (WindowManagerService.DEBUG_ANIM) {
1397 RuntimeException e = null;
1398 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1399 e = new RuntimeException();
1400 e.fillInStackTrace();
1401 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001402 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001403 }
1404 setAnimation(a);
1405 mAnimationIsEntrance = isEntrance;
1406 }
1407 } else {
1408 clearAnimation();
1409 }
1410
1411 return mAnimation != null;
1412 }
1413
Craig Mautnera2c77052012-03-26 12:14:43 -07001414 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1415 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1416 || mAnimation != null) {
1417 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1418 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1419 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1420 pw.print(" mAnimation="); pw.println(mAnimation);
1421 }
1422 if (mHasTransformation || mHasLocalTransformation) {
1423 pw.print(prefix); pw.print("XForm: has=");
1424 pw.print(mHasTransformation);
1425 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1426 pw.print(" "); mTransformation.printShortString(pw);
1427 pw.println();
1428 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001429 if (mSurface != null) {
1430 if (dumpAll) {
1431 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001432 pw.print(prefix); pw.print("mDrawState="); pw.print(mDrawState);
1433 pw.print(" mLastHidden="); pw.println(mLastHidden);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001434 }
1435 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1436 pw.print(" layer="); pw.print(mSurfaceLayer);
1437 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1438 pw.print(" rect=("); pw.print(mSurfaceX);
1439 pw.print(","); pw.print(mSurfaceY);
1440 pw.print(") "); pw.print(mSurfaceW);
1441 pw.print(" x "); pw.println(mSurfaceH);
1442 }
1443 if (mPendingDestroySurface != null) {
1444 pw.print(prefix); pw.print("mPendingDestroySurface=");
1445 pw.println(mPendingDestroySurface);
1446 }
1447 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1448 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1449 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1450 }
1451 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1452 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1453 pw.print(" mAlpha="); pw.print(mAlpha);
1454 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1455 }
1456 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1457 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1458 pw.print(" mDsDx="); pw.print(mDsDx);
1459 pw.print(" mDtDx="); pw.print(mDtDx);
1460 pw.print(" mDsDy="); pw.print(mDsDy);
1461 pw.print(" mDtDy="); pw.println(mDtDy);
1462 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001463 }
1464
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001465 @Override
1466 public String toString() {
1467 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1468 sb.append(mWin.mLastTitle + "): ");
1469 sb.append("mSurface " + mSurface);
1470 sb.append(", mAnimation " + mAnimation);
1471 return sb.toString();
1472 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001473}