blob: 3d0c6dec774d1c7892be79494da7fa30c296957e [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 Mautnera2c77052012-03-26 12:14:43 -070019import android.util.Slog;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070020import android.view.Surface;
Craig Mautner7358fbf2012-04-12 21:06:33 -070021import android.view.SurfaceSession;
Craig Mautnera2c77052012-03-26 12:14:43 -070022import android.view.WindowManager;
23import android.view.WindowManagerPolicy;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070024import android.view.WindowManager.LayoutParams;
Craig Mautnera2c77052012-03-26 12:14:43 -070025import android.view.animation.Animation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070026import android.view.animation.AnimationUtils;
Craig Mautnera2c77052012-03-26 12:14:43 -070027import android.view.animation.Transformation;
28
29import com.android.server.wm.WindowManagerService.H;
30
31import java.io.PrintWriter;
Craig Mautner7358fbf2012-04-12 21:06:33 -070032import java.util.ArrayList;
Craig Mautnera2c77052012-03-26 12:14:43 -070033
34/**
Craig Mautnerc2f9be02012-03-27 17:32:29 -070035 * Keep track of animations and surface operations for a single WindowState.
36 **/
Craig Mautnera2c77052012-03-26 12:14:43 -070037class WindowStateAnimator {
Craig Mautnerc2f9be02012-03-27 17:32:29 -070038 static final boolean DEBUG_VISIBILITY = WindowManagerService.DEBUG_VISIBILITY;
39 static final boolean DEBUG_ANIM = WindowManagerService.DEBUG_ANIM;
40 static final boolean DEBUG_LAYERS = WindowManagerService.DEBUG_LAYERS;
41 static final boolean DEBUG_STARTING_WINDOW = WindowManagerService.DEBUG_STARTING_WINDOW;
42 static final boolean SHOW_TRANSACTIONS = WindowManagerService.SHOW_TRANSACTIONS;
43 static final boolean SHOW_LIGHT_TRANSACTIONS = WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
44 static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
45 static final boolean localLOGV = WindowManagerService.localLOGV;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -070046 static final boolean DEBUG_ORIENTATION = WindowManagerService.DEBUG_ORIENTATION;
Craig Mautner7358fbf2012-04-12 21:06:33 -070047 static final boolean DEBUG_SURFACE_TRACE = WindowManagerService.DEBUG_SURFACE_TRACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070048
49 static final String TAG = "WindowStateAnimator";
Craig Mautnera2c77052012-03-26 12:14:43 -070050
51 final WindowManagerService mService;
52 final WindowState mWin;
53 final WindowState mAttachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -070054 final WindowAnimator mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070055 final Session mSession;
56 final WindowManagerPolicy mPolicy;
57 final Context mContext;
Craig Mautnera2c77052012-03-26 12:14:43 -070058
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -070059 // If this is a universe background window, this is the transformation
60 // it is applying to the rest of the universe.
61 final Transformation mUniverseTransform = new Transformation();
62
Craig Mautnera2c77052012-03-26 12:14:43 -070063 // Currently running animation.
64 boolean mAnimating;
65 boolean mLocalAnimating;
66 Animation mAnimation;
67 boolean mAnimationIsEntrance;
68 boolean mHasTransformation;
69 boolean mHasLocalTransformation;
70 final Transformation mTransformation = new Transformation();
71 boolean mWasAnimating; // Were we animating going into the most recent animation step?
Craig Mautnerc2f9be02012-03-27 17:32:29 -070072 int mAnimLayer;
73 int mLastLayer;
74
75 Surface mSurface;
76 Surface mPendingDestroySurface;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070077
78 /**
79 * Set when we have changed the size of the surface, to know that
80 * we must tell them application to resize (and thus redraw itself).
81 */
82 boolean mSurfaceResized;
83
84 /**
85 * Set if the client has asked that the destroy of its surface be delayed
86 * until it explicitly says it is okay.
87 */
88 boolean mSurfaceDestroyDeferred;
89
Craig Mautner7d8df392012-04-06 15:26:23 -070090 float mShownAlpha = 0;
91 float mAlpha = 0;
92 float mLastAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -070093
94 // Used to save animation distances between the time they are calculated and when they are
95 // used.
96 int mAnimDw;
97 int mAnimDh;
98 float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
99 float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
100
101 boolean mHaveMatrix;
102
103 // For debugging, this is the last information given to the surface flinger.
104 boolean mSurfaceShown;
105 float mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
106 int mSurfaceLayer;
107 float mSurfaceAlpha;
108
109 // Set to true if, when the window gets displayed, it should perform
110 // an enter animation.
111 boolean mEnterAnimationPending;
Craig Mautnera2c77052012-03-26 12:14:43 -0700112
Craig Mautner749a7bb2012-04-02 13:49:53 -0700113 /** This is set when there is no Surface */
114 static final int NO_SURFACE = 0;
115 /** This is set after the Surface has been created but before the window has been drawn. During
116 * this time the surface is hidden. */
117 static final int DRAW_PENDING = 1;
118 /** This is set after the window has finished drawing for the first time but before its surface
119 * is shown. The surface will be displayed when the next layout is run. */
120 static final int COMMIT_DRAW_PENDING = 2;
121 /** This is set during the time after the window's drawing has been committed, and before its
122 * surface is actually shown. It is used to delay showing the surface until all windows in a
123 * token are ready to be shown. */
124 static final int READY_TO_SHOW = 3;
125 /** Set when the window has been shown in the screen the first time. */
126 static final int HAS_DRAWN = 4;
127 int mDrawState;
Craig Mautnera608b882012-03-30 13:03:49 -0700128
Craig Mautner749a7bb2012-04-02 13:49:53 -0700129 /** Was this window last hidden? */
130 boolean mLastHidden;
Craig Mautnera608b882012-03-30 13:03:49 -0700131
Craig Mautnerbec53f72012-04-05 11:49:05 -0700132 int mAttrFlags;
133 int mAttrType;
134
Craig Mautnera2c77052012-03-26 12:14:43 -0700135 public WindowStateAnimator(final WindowManagerService service, final WindowState win,
136 final WindowState attachedWindow) {
137 mService = service;
138 mWin = win;
139 mAttachedWindow = attachedWindow;
Craig Mautnere7ae2502012-03-26 17:11:19 -0700140 mAnimator = mService.mAnimator;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700141 mSession = win.mSession;
142 mPolicy = mService.mPolicy;
143 mContext = mService.mContext;
Craig Mautnerbec53f72012-04-05 11:49:05 -0700144 mAttrFlags = win.mAttrs.flags;
145 mAttrType = win.mAttrs.type;
Craig Mautner9aa69582012-05-09 17:41:13 -0700146 mAnimDw = service.mAppDisplayWidth;
147 mAnimDh = service.mAppDisplayHeight;
Craig Mautnera2c77052012-03-26 12:14:43 -0700148 }
149
150 public void setAnimation(Animation anim) {
Craig Mautnerbec53f72012-04-05 11:49:05 -0700151 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700152 mAnimating = false;
153 mLocalAnimating = false;
154 mAnimation = anim;
155 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
156 mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
157 // Start out animation gone if window is gone, or visible if window is visible.
158 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700159 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700160 mHasLocalTransformation = true;
161 }
162
163 public void clearAnimation() {
164 if (mAnimation != null) {
165 mAnimating = true;
166 mLocalAnimating = false;
167 mAnimation.cancel();
168 mAnimation = null;
169 }
170 }
171
172 /** Is the window or its container currently animating? */
173 boolean isAnimating() {
174 final WindowState attached = mAttachedWindow;
175 final AppWindowToken atoken = mWin.mAppToken;
176 return mAnimation != null
177 || (attached != null && attached.mWinAnimator.mAnimation != null)
178 || (atoken != null &&
Craig Mautner59431632012-04-04 11:56:44 -0700179 (atoken.mAppAnimator.animation != null
Craig Mautnera2c77052012-03-26 12:14:43 -0700180 || atoken.inPendingTransaction));
181 }
182
Craig Mautner0afddcb2012-05-08 15:38:00 -0700183 /** Is the window animating the DummyAnimation? */
184 boolean isDummyAnimation() {
185 final AppWindowToken atoken = mWin.mAppToken;
186 return atoken != null
187 && atoken.mAppAnimator.animation == AppWindowAnimator.sDummyAnimation;
188 }
189
Craig Mautnera2c77052012-03-26 12:14:43 -0700190 /** Is this window currently animating? */
191 boolean isWindowAnimating() {
192 return mAnimation != null;
193 }
194
Craig Mautnera2c77052012-03-26 12:14:43 -0700195 void cancelExitAnimationForNextAnimationLocked() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700196 if (mAnimation != null) {
197 mAnimation.cancel();
198 mAnimation = null;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700199 mLocalAnimating = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700200 destroySurfaceLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700201 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700202 }
203
204 private boolean stepAnimation(long currentTime) {
205 if ((mAnimation == null) || !mLocalAnimating) {
206 return false;
207 }
208 mTransformation.clear();
209 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Craig Mautner9e809442012-06-22 17:13:04 -0700210 if (false && DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700211 TAG, "Stepped animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700212 ": more=" + more + ", xform=" + mTransformation);
213 return more;
214 }
215
216 // This must be called while inside a transaction. Returns true if
217 // there is more animation to run.
218 boolean stepAnimationLocked(long currentTime) {
219 // Save the animation state as it was before this step so WindowManagerService can tell if
220 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
221 mWasAnimating = mAnimating;
222 if (mService.okToDisplay()) {
223 // We will run animations as long as the display isn't frozen.
224
225 if (mWin.isDrawnLw() && mAnimation != null) {
226 mHasTransformation = true;
227 mHasLocalTransformation = true;
228 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700229 if (DEBUG_ANIM) Slog.v(
230 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700231 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
232 " wh=" + mWin.mFrame.height() +
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700233 " dw=" + mAnimDw + " dh=" + mAnimDh +
Craig Mautnera2c77052012-03-26 12:14:43 -0700234 " scale=" + mService.mWindowAnimationScale);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700235 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
236 mAnimDw, mAnimDh);
Craig Mautner9aa69582012-05-09 17:41:13 -0700237 mAnimDw = mService.mAppDisplayWidth;
238 mAnimDh = mService.mAppDisplayHeight;
Craig Mautnera2c77052012-03-26 12:14:43 -0700239 mAnimation.setStartTime(currentTime);
240 mLocalAnimating = true;
241 mAnimating = true;
242 }
243 if ((mAnimation != null) && mLocalAnimating) {
244 if (stepAnimation(currentTime)) {
245 return true;
246 }
247 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700248 if (DEBUG_ANIM) Slog.v(
249 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700250 " @ " + currentTime);
251 //WindowManagerService.this.dump();
252 }
253 mHasLocalTransformation = false;
254 if ((!mLocalAnimating || mAnimationIsEntrance) && mWin.mAppToken != null
Craig Mautner59431632012-04-04 11:56:44 -0700255 && mWin.mAppToken.mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700256 // When our app token is animating, we kind-of pretend like
257 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
258 // part of this check means that we will only do this if
259 // our window is not currently exiting, or it is not
260 // locally animating itself. The idea being that one that
261 // is exiting and doing a local animation should be removed
262 // once that animation is done.
263 mAnimating = true;
264 mHasTransformation = true;
265 mTransformation.clear();
266 return false;
267 } else if (mHasTransformation) {
268 // Little trick to get through the path below to act like
269 // we have finished an animation.
270 mAnimating = true;
271 } else if (isAnimating()) {
272 mAnimating = true;
273 }
274 } else if (mAnimation != null) {
275 // If the display is frozen, and there is a pending animation,
276 // clear it and make sure we run the cleanup code.
277 mAnimating = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700278 }
279
280 if (!mAnimating && !mLocalAnimating) {
281 return false;
282 }
283
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700284 // Done animating, clean up.
285 if (DEBUG_ANIM) Slog.v(
286 TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
Craig Mautnera2c77052012-03-26 12:14:43 -0700287 + ", reportedVisible="
288 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
289
290 mAnimating = false;
291 mLocalAnimating = false;
292 if (mAnimation != null) {
293 mAnimation.cancel();
294 mAnimation = null;
295 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700296 if (mAnimator.mWindowDetachedWallpaper == mWin) {
297 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700298 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700299 mAnimLayer = mWin.mLayer;
Craig Mautnera2c77052012-03-26 12:14:43 -0700300 if (mWin.mIsImWindow) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700301 mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700302 } else if (mWin.mIsWallpaper) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700303 mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700304 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700305 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
306 + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700307 mHasTransformation = false;
308 mHasLocalTransformation = false;
309 if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
310 if (WindowState.DEBUG_VISIBILITY) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700311 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
Craig Mautnera2c77052012-03-26 12:14:43 -0700312 + mWin.mPolicyVisibilityAfterAnim);
313 }
314 mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
315 mService.mLayoutNeeded = true;
316 if (!mWin.mPolicyVisibility) {
317 if (mService.mCurrentFocus == mWin) {
318 mService.mFocusMayChange = true;
319 }
320 // Window is no longer visible -- make sure if we were waiting
321 // for it to be displayed before enabling the display, that
322 // we allow the display to be enabled now.
323 mService.enableScreenIfNeededLocked();
324 }
325 }
326 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700327 if (mDrawState == HAS_DRAWN
Craig Mautnera2c77052012-03-26 12:14:43 -0700328 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
329 && mWin.mAppToken != null
330 && mWin.mAppToken.firstWindowDrawn
331 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700332 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700333 + mWin.mToken + ": first real window done animating");
334 mService.mFinishedStarting.add(mWin.mAppToken);
335 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
336 }
337
338 finishExit();
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700339 mAnimator.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
340 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
341 "WindowStateAnimator", mAnimator.mPendingLayoutChanges);
Craig Mautnera2c77052012-03-26 12:14:43 -0700342
343 if (mWin.mAppToken != null) {
344 mWin.mAppToken.updateReportedVisibilityLocked();
345 }
346
347 return false;
348 }
349
350 void finishExit() {
351 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700352 TAG, "finishExit in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700353 + ": exiting=" + mWin.mExiting
354 + " remove=" + mWin.mRemoveOnExit
355 + " windowAnimating=" + isWindowAnimating());
356
357 final int N = mWin.mChildWindows.size();
358 for (int i=0; i<N; i++) {
359 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
360 }
361
362 if (!mWin.mExiting) {
363 return;
364 }
365
366 if (isWindowAnimating()) {
367 return;
368 }
369
370 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700371 TAG, "Exit animation finished in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700372 + ": remove=" + mWin.mRemoveOnExit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700373 if (mSurface != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700374 mService.mDestroySurface.add(mWin);
375 mWin.mDestroying = true;
376 if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
377 mWin, "HIDE (finishExit)", null);
Craig Mautner0afddcb2012-05-08 15:38:00 -0700378 hide();
Craig Mautnera2c77052012-03-26 12:14:43 -0700379 }
380 mWin.mExiting = false;
381 if (mWin.mRemoveOnExit) {
382 mService.mPendingRemove.add(mWin);
383 mWin.mRemoveOnExit = false;
384 }
Craig Mautnerb9836b92012-06-11 11:40:09 -0700385 mAnimator.hideWallpapersLocked(mWin);
Craig Mautner0afddcb2012-05-08 15:38:00 -0700386 }
387
388 void hide() {
389 if (!mLastHidden) {
390 //dump();
391 mLastHidden = true;
392 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
393 "HIDE (performLayout)", null);
394 if (mSurface != null) {
395 mSurfaceShown = false;
396 try {
397 mSurface.hide();
398 } catch (RuntimeException e) {
399 Slog.w(TAG, "Exception hiding surface in " + mWin);
400 }
401 }
402 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700403 }
404
Craig Mautnera608b882012-03-30 13:03:49 -0700405 boolean finishDrawingLocked() {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700406 if (mDrawState == DRAW_PENDING) {
Craig Mautneref25d7a2012-05-15 23:01:47 -0700407 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
408 Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + this + " in "
Craig Mautner83339b42012-05-01 22:13:23 -0700409 + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700410 mDrawState = COMMIT_DRAW_PENDING;
Craig Mautnera608b882012-03-30 13:03:49 -0700411 return true;
412 }
413 return false;
414 }
415
416 // This must be called while inside a transaction.
417 boolean commitFinishDrawingLocked(long currentTime) {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700418 if (mDrawState != COMMIT_DRAW_PENDING) {
Craig Mautnera608b882012-03-30 13:03:49 -0700419 return false;
420 }
Craig Mautneref25d7a2012-05-15 23:01:47 -0700421 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
Craig Mautner83339b42012-05-01 22:13:23 -0700422 Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700423 mDrawState = READY_TO_SHOW;
Craig Mautnera608b882012-03-30 13:03:49 -0700424 final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
425 final AppWindowToken atoken = mWin.mAppToken;
426 if (atoken == null || atoken.allDrawn || starting) {
427 performShowLocked();
428 }
429 return true;
430 }
431
Craig Mautner7d8df392012-04-06 15:26:23 -0700432 static class SurfaceTrace extends Surface {
433 private final static String SURFACE_TAG = "SurfaceTrace";
434 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700435
Craig Mautner7d8df392012-04-06 15:26:23 -0700436 private float mSurfaceTraceAlpha = 0;
Craig Mautner7358fbf2012-04-12 21:06:33 -0700437 private int mLayer;
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700438 private final PointF mPosition = new PointF();
439 private final Point mSize = new Point();
440 private final Rect mWindowCrop = new Rect();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700441 private boolean mShown = false;
442 private String mName = "Not named";
443
Craig Mautner7d8df392012-04-06 15:26:23 -0700444 public SurfaceTrace(SurfaceSession s,
Craig Mautner7358fbf2012-04-12 21:06:33 -0700445 int pid, int display, int w, int h, int format, int flags) throws
446 OutOfResourcesException {
447 super(s, pid, display, w, h, format, flags);
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700448 mSize.set(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700449 Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700450 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700451 }
452
Craig Mautner7d8df392012-04-06 15:26:23 -0700453 public SurfaceTrace(SurfaceSession s,
Craig Mautner7358fbf2012-04-12 21:06:33 -0700454 int pid, String name, int display, int w, int h, int format, int flags)
455 throws OutOfResourcesException {
456 super(s, pid, name, display, w, h, format, flags);
457 mName = name;
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700458 mSize.set(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700459 Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700460 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700461 }
462
463 @Override
464 public void setAlpha(float alpha) {
465 super.setAlpha(alpha);
Craig Mautner7d8df392012-04-06 15:26:23 -0700466 mSurfaceTraceAlpha = alpha;
467 Slog.v(SURFACE_TAG, "setAlpha: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700468 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700469 }
470
471 @Override
472 public void setLayer(int zorder) {
473 super.setLayer(zorder);
474 mLayer = zorder;
Craig Mautner7d8df392012-04-06 15:26:23 -0700475 Slog.v(SURFACE_TAG, "setLayer: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700476 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700477
478 sSurfaces.remove(this);
479 int i;
480 for (i = sSurfaces.size() - 1; i >= 0; i--) {
Craig Mautner7d8df392012-04-06 15:26:23 -0700481 SurfaceTrace s = sSurfaces.get(i);
Craig Mautner7358fbf2012-04-12 21:06:33 -0700482 if (s.mLayer < zorder) {
483 break;
484 }
485 }
486 sSurfaces.add(i + 1, this);
487 }
488
489 @Override
490 public void setPosition(float x, float y) {
491 super.setPosition(x, y);
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700492 mPosition.set(x, y);
Craig Mautner7d8df392012-04-06 15:26:23 -0700493 Slog.v(SURFACE_TAG, "setPosition: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700494 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700495 }
496
497 @Override
498 public void setSize(int w, int h) {
499 super.setSize(w, h);
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700500 mSize.set(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700501 Slog.v(SURFACE_TAG, "setSize: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700502 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700503 }
504
505 @Override
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700506 public void setWindowCrop(Rect crop) {
507 super.setWindowCrop(crop);
Craig Mautneref25d7a2012-05-15 23:01:47 -0700508 if (crop != null) {
509 mWindowCrop.set(crop);
510 }
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700511 Slog.v(SURFACE_TAG, "setWindowCrop: " + this + ". Called by "
512 + Debug.getCallers(3));
513 }
514
515 @Override
Craig Mautner7358fbf2012-04-12 21:06:33 -0700516 public void hide() {
517 super.hide();
518 mShown = false;
Craig Mautner7d8df392012-04-06 15:26:23 -0700519 Slog.v(SURFACE_TAG, "hide: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700520 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700521 }
522 @Override
523 public void show() {
524 super.show();
525 mShown = true;
Craig Mautner7d8df392012-04-06 15:26:23 -0700526 Slog.v(SURFACE_TAG, "show: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700527 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700528 }
529
530 @Override
531 public void destroy() {
532 super.destroy();
Craig Mautner7d8df392012-04-06 15:26:23 -0700533 Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700534 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700535 sSurfaces.remove(this);
536 }
537
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700538 @Override
539 public void release() {
540 super.release();
541 Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700542 + Debug.getCallers(3));
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700543 sSurfaces.remove(this);
544 }
545
Craig Mautner7358fbf2012-04-12 21:06:33 -0700546 static void dumpAllSurfaces() {
547 final int N = sSurfaces.size();
548 for (int i = 0; i < N; i++) {
549 Slog.i(TAG, "SurfaceDump: " + sSurfaces.get(i));
550 }
551 }
552
553 @Override
554 public String toString() {
Craig Mautnerfbf378c2012-04-23 17:24:21 -0700555 return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
556 + mName + ": shown=" + mShown + " layer=" + mLayer
Craig Mautner7d8df392012-04-06 15:26:23 -0700557 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700558 + " " + mSize.x + "x" + mSize.y
559 + " crop=" + mWindowCrop.toShortString();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700560 }
561 }
562
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700563 Surface createSurfaceLocked() {
564 if (mSurface == null) {
Craig Mautner83339b42012-05-01 22:13:23 -0700565 if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
566 "createSurface " + this + ": mDrawState=DRAW_PENDING");
Craig Mautner749a7bb2012-04-02 13:49:53 -0700567 mDrawState = DRAW_PENDING;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700568 if (mWin.mAppToken != null) {
569 mWin.mAppToken.allDrawn = false;
570 }
571
572 mService.makeWindowFreezingScreenIfNeededLocked(mWin);
573
574 int flags = 0;
575 final WindowManager.LayoutParams attrs = mWin.mAttrs;
576
577 if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
578 flags |= Surface.SECURE;
579 }
580 if (WindowState.DEBUG_VISIBILITY) Slog.v(
581 TAG, "Creating surface in session "
582 + mSession.mSurfaceSession + " window " + this
583 + " w=" + mWin.mCompatFrame.width()
584 + " h=" + mWin.mCompatFrame.height() + " format="
585 + attrs.format + " flags=" + flags);
586
587 int w = mWin.mCompatFrame.width();
588 int h = mWin.mCompatFrame.height();
589 if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
590 // for a scaled surface, we always want the requested
591 // size.
592 w = mWin.mRequestedWidth;
593 h = mWin.mRequestedHeight;
594 }
595
596 // Something is wrong and SurfaceFlinger will not like this,
597 // try to revert to sane values
598 if (w <= 0) w = 1;
599 if (h <= 0) h = 1;
600
601 mSurfaceShown = false;
602 mSurfaceLayer = 0;
Craig Mautner7d8df392012-04-06 15:26:23 -0700603 mSurfaceAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700604 mSurfaceX = 0;
605 mSurfaceY = 0;
606 mSurfaceW = w;
607 mSurfaceH = h;
Dianne Hackborn85afd1b2012-05-13 13:31:06 -0700608 mWin.mLastSystemDecorRect.set(0, 0, 0, 0);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700609 try {
610 final boolean isHwAccelerated = (attrs.flags &
611 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
612 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
613 if (!PixelFormat.formatHasAlpha(attrs.format)) {
614 flags |= Surface.OPAQUE;
615 }
Craig Mautner7358fbf2012-04-12 21:06:33 -0700616 if (DEBUG_SURFACE_TRACE) {
Craig Mautner7d8df392012-04-06 15:26:23 -0700617 mSurface = new SurfaceTrace(
Craig Mautner7358fbf2012-04-12 21:06:33 -0700618 mSession.mSurfaceSession, mSession.mPid,
619 attrs.getTitle().toString(),
620 0, w, h, format, flags);
621 } else {
622 mSurface = new Surface(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700623 mSession.mSurfaceSession, mSession.mPid,
624 attrs.getTitle().toString(),
625 0, w, h, format, flags);
Craig Mautner7358fbf2012-04-12 21:06:33 -0700626 }
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700627 mWin.mHasSurface = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700628 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
629 " CREATE SURFACE "
630 + mSurface + " IN SESSION "
631 + mSession.mSurfaceSession
632 + ": pid=" + mSession.mPid + " format="
633 + attrs.format + " flags=0x"
634 + Integer.toHexString(flags)
635 + " / " + this);
636 } catch (Surface.OutOfResourcesException e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700637 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700638 Slog.w(TAG, "OutOfResourcesException creating surface");
639 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700640 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700641 return null;
642 } catch (Exception e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700643 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700644 Slog.e(TAG, "Exception creating surface", e);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700645 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700646 return null;
647 }
648
649 if (WindowManagerService.localLOGV) Slog.v(
650 TAG, "Got surface: " + mSurface
651 + ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
652 + ", animLayer=" + mAnimLayer);
653 if (SHOW_LIGHT_TRANSACTIONS) {
654 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
655 WindowManagerService.logSurface(mWin, "CREATE pos=("
656 + mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
657 + mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
658 + "), layer=" + mAnimLayer + " HIDE", null);
659 }
660 Surface.openTransaction();
661 try {
662 try {
663 mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
664 mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
665 mSurface.setPosition(mSurfaceX, mSurfaceY);
666 mSurfaceLayer = mAnimLayer;
667 mSurface.setLayer(mAnimLayer);
Craig Mautner7d8df392012-04-06 15:26:23 -0700668 mSurface.setAlpha(0);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700669 mSurfaceShown = false;
670 mSurface.hide();
671 if ((mWin.mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
672 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "DITHER", null);
673 mSurface.setFlags(Surface.SURFACE_DITHER, Surface.SURFACE_DITHER);
674 }
675 } catch (RuntimeException e) {
676 Slog.w(TAG, "Error creating surface in " + w, e);
677 mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
678 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700679 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700680 } finally {
681 Surface.closeTransaction();
682 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
683 "<<< CLOSE TRANSACTION createSurfaceLocked");
684 }
685 if (WindowManagerService.localLOGV) Slog.v(
686 TAG, "Created surface " + this);
687 }
688 return mSurface;
689 }
690
691 void destroySurfaceLocked() {
692 if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
693 mWin.mAppToken.startingDisplayed = false;
694 }
695
696 if (mSurface != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700697
698 int i = mWin.mChildWindows.size();
699 while (i > 0) {
700 i--;
701 WindowState c = mWin.mChildWindows.get(i);
702 c.mAttachedHidden = true;
703 }
704
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700705 try {
706 if (DEBUG_VISIBILITY) {
707 RuntimeException e = null;
708 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
709 e = new RuntimeException();
710 e.fillInStackTrace();
711 }
712 Slog.w(TAG, "Window " + this + " destroying surface "
713 + mSurface + ", session " + mSession, e);
714 }
715 if (mSurfaceDestroyDeferred) {
716 if (mSurface != null && mPendingDestroySurface != mSurface) {
717 if (mPendingDestroySurface != null) {
718 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
719 RuntimeException e = null;
720 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
721 e = new RuntimeException();
722 e.fillInStackTrace();
723 }
724 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
725 }
726 mPendingDestroySurface.destroy();
727 }
728 mPendingDestroySurface = mSurface;
729 }
730 } else {
731 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
732 RuntimeException e = null;
733 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
734 e = new RuntimeException();
735 e.fillInStackTrace();
736 }
737 WindowManagerService.logSurface(mWin, "DESTROY", e);
738 }
739 mSurface.destroy();
740 }
Craig Mautnerb9836b92012-06-11 11:40:09 -0700741 mAnimator.hideWallpapersLocked(mWin);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700742 } catch (RuntimeException e) {
743 Slog.w(TAG, "Exception thrown when destroying Window " + this
744 + " surface " + mSurface + " session " + mSession
745 + ": " + e.toString());
746 }
747
748 mSurfaceShown = false;
749 mSurface = null;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700750 mWin.mHasSurface =false;
Craig Mautnerbf08af32012-05-16 19:43:42 -0700751 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700752 }
753 }
754
755 void destroyDeferredSurfaceLocked() {
756 try {
757 if (mPendingDestroySurface != null) {
758 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
759 RuntimeException e = null;
760 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
761 e = new RuntimeException();
762 e.fillInStackTrace();
763 }
764 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
765 }
766 mPendingDestroySurface.destroy();
Craig Mautnerb9836b92012-06-11 11:40:09 -0700767 mAnimator.hideWallpapersLocked(mWin);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700768 }
769 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700770 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700771 + this + " surface " + mPendingDestroySurface
772 + " session " + mSession + ": " + e.toString());
773 }
774 mSurfaceDestroyDeferred = false;
775 mPendingDestroySurface = null;
776 }
777
778 void computeShownFrameLocked() {
779 final boolean selfTransformation = mHasLocalTransformation;
780 Transformation attachedTransformation =
781 (mAttachedWindow != null && mAttachedWindow.mWinAnimator.mHasLocalTransformation)
782 ? mAttachedWindow.mWinAnimator.mTransformation : null;
Craig Mautner59431632012-04-04 11:56:44 -0700783 final AppWindowAnimator appAnimator =
784 mWin.mAppToken == null ? null : mWin.mAppToken.mAppAnimator;
785 Transformation appTransformation = (appAnimator != null && appAnimator.hasTransformation)
786 ? appAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700787
788 // Wallpapers are animated based on the "real" window they
789 // are currently targeting.
790 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
791 && mService.mWallpaperTarget != null) {
792 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
793 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
794 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
795 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
796 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
797 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
798 }
799 }
Craig Mautner59431632012-04-04 11:56:44 -0700800 final AppWindowAnimator wpAppAnimator = mService.mWallpaperTarget.mAppToken == null
801 ? null : mService.mWallpaperTarget.mAppToken.mAppAnimator;
802 if (wpAppAnimator != null &&
803 wpAppAnimator.hasTransformation &&
804 wpAppAnimator.animation != null &&
805 !wpAppAnimator.animation.getDetachWallpaper()) {
806 appTransformation = wpAppAnimator.transformation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700807 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
808 Slog.v(TAG, "WP target app xform: " + appTransformation);
809 }
810 }
811 }
812
813 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
814 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
815 if (selfTransformation || attachedTransformation != null
816 || appTransformation != null || screenAnimation) {
817 // cache often used attributes locally
818 final Rect frame = mWin.mFrame;
819 final float tmpFloats[] = mService.mTmpFloats;
820 final Matrix tmpMatrix = mWin.mTmpMatrix;
821
822 // Compute the desired transformation.
823 if (screenAnimation) {
824 // If we are doing a screen animation, the global rotation
825 // applied to windows can result in windows that are carefully
826 // aligned with each other to slightly separate, allowing you
827 // to see what is behind them. An unsightly mess. This...
828 // thing... magically makes it call good: scale each window
829 // slightly (two pixels larger in each dimension, from the
830 // window's center).
831 final float w = frame.width();
832 final float h = frame.height();
833 if (w>=1 && h>=1) {
834 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
835 } else {
836 tmpMatrix.reset();
837 }
838 } else {
839 tmpMatrix.reset();
840 }
841 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
842 if (selfTransformation) {
843 tmpMatrix.postConcat(mTransformation.getMatrix());
844 }
845 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
846 if (attachedTransformation != null) {
847 tmpMatrix.postConcat(attachedTransformation.getMatrix());
848 }
849 if (appTransformation != null) {
850 tmpMatrix.postConcat(appTransformation.getMatrix());
851 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700852 if (mAnimator.mUniverseBackground != null) {
853 tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
854 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700855 if (screenAnimation) {
856 tmpMatrix.postConcat(
857 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
858 }
859
860 // "convert" it into SurfaceFlinger's format
861 // (a 2x2 matrix + an offset)
862 // Here we must not transform the position of the surface
863 // since it is already included in the transformation.
864 //Slog.i(TAG, "Transform: " + matrix);
865
866 mHaveMatrix = true;
867 tmpMatrix.getValues(tmpFloats);
868 mDsDx = tmpFloats[Matrix.MSCALE_X];
869 mDtDx = tmpFloats[Matrix.MSKEW_Y];
870 mDsDy = tmpFloats[Matrix.MSKEW_X];
871 mDtDy = tmpFloats[Matrix.MSCALE_Y];
872 float x = tmpFloats[Matrix.MTRANS_X];
873 float y = tmpFloats[Matrix.MTRANS_Y];
874 int w = frame.width();
875 int h = frame.height();
876 mWin.mShownFrame.set(x, y, x+w, y+h);
877
878 // Now set the alpha... but because our current hardware
879 // can't do alpha transformation on a non-opaque surface,
880 // turn it off if we are running an animation that is also
881 // transforming since it is more important to have that
882 // animation be smooth.
883 mShownAlpha = mAlpha;
884 if (!mService.mLimitedAlphaCompositing
885 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
886 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
887 && x == frame.left && y == frame.top))) {
888 //Slog.i(TAG, "Applying alpha transform");
889 if (selfTransformation) {
890 mShownAlpha *= mTransformation.getAlpha();
891 }
892 if (attachedTransformation != null) {
893 mShownAlpha *= attachedTransformation.getAlpha();
894 }
895 if (appTransformation != null) {
896 mShownAlpha *= appTransformation.getAlpha();
897 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700898 if (mAnimator.mUniverseBackground != null) {
899 mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
900 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700901 if (screenAnimation) {
902 mShownAlpha *=
903 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
904 }
905 } else {
906 //Slog.i(TAG, "Not applying alpha transform");
907 }
908
Craig Mautner9e809442012-06-22 17:13:04 -0700909 if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV) && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700910 TAG, "computeShownFrameLocked: Animating " + this +
Craig Mautnerf4120952012-06-21 18:25:39 -0700911 " mAlpha=" + mAlpha +
912 " self=" + (selfTransformation ? mTransformation.getAlpha() : "null") +
913 " attached=" + (attachedTransformation == null ? "null" : attachedTransformation.getAlpha()) +
914 " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha()) +
915 " screen=" + (screenAnimation ? mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha()
916 : "null"));
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700917 return;
Craig Mautner4d7349b2012-04-20 14:52:47 -0700918 } else if (mWin.mIsWallpaper &&
919 (mAnimator.mPendingActions & WindowAnimator.WALLPAPER_ACTION_PENDING) != 0) {
920 return;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700921 }
922
923 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautner4d7349b2012-04-20 14:52:47 -0700924 TAG, "computeShownFrameLocked: " + this +
925 " not attached, mAlpha=" + mAlpha);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700926 if (mAnimator.mUniverseBackground != null &&
927 mWin.mAttrs.type != WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
928 && mWin.mBaseLayer < mAnimator.mAboveUniverseLayer) {
929 final Rect frame = mWin.mFrame;
930 final float tmpFloats[] = mService.mTmpFloats;
931 final Matrix tmpMatrix = mWin.mTmpMatrix;
932 tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
933 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
934 tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
935 tmpMatrix.getValues(tmpFloats);
936 mHaveMatrix = true;
937 mDsDx = tmpFloats[Matrix.MSCALE_X];
938 mDtDx = tmpFloats[Matrix.MSKEW_Y];
939 mDsDy = tmpFloats[Matrix.MSKEW_X];
940 mDtDy = tmpFloats[Matrix.MSCALE_Y];
941 float x = tmpFloats[Matrix.MTRANS_X];
942 float y = tmpFloats[Matrix.MTRANS_Y];
943 int w = frame.width();
944 int h = frame.height();
945 mWin.mShownFrame.set(x, y, x+w, y+h);
946 mShownAlpha = mAlpha * mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
947 } else {
948 mWin.mShownFrame.set(mWin.mFrame);
949 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
950 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
951 }
952 mShownAlpha = mAlpha;
953 mHaveMatrix = false;
954 mDsDx = mWin.mGlobalScale;
955 mDtDx = 0;
956 mDsDy = 0;
957 mDtDy = mWin.mGlobalScale;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700958 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700959 }
960
961 void applyDecorRect(final Rect decorRect) {
962 final WindowState w = mWin;
963 // Compute the offset of the window in relation to the decor rect.
964 final int offX = w.mXOffset + w.mFrame.left;
965 final int offY = w.mYOffset + w.mFrame.top;
966 // Initialize the decor rect to the entire frame.
967 w.mSystemDecorRect.set(0, 0, w.mFrame.width(), w.mFrame.height());
968 // Intersect with the decor rect, offsetted by window position.
969 w.mSystemDecorRect.intersect(decorRect.left-offX, decorRect.top-offY,
970 decorRect.right-offX, decorRect.bottom-offY);
971 // If size compatibility is being applied to the window, the
972 // surface is scaled relative to the screen. Also apply this
973 // scaling to the crop rect. We aren't using the standard rect
974 // scale function because we want to round things to make the crop
975 // always round to a larger rect to ensure we don't crop too
976 // much and hide part of the window that should be seen.
977 if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
978 final float scale = w.mInvGlobalScale;
979 w.mSystemDecorRect.left = (int) (w.mSystemDecorRect.left * scale - 0.5f);
980 w.mSystemDecorRect.top = (int) (w.mSystemDecorRect.top * scale - 0.5f);
981 w.mSystemDecorRect.right = (int) ((w.mSystemDecorRect.right+1) * scale - 0.5f);
982 w.mSystemDecorRect.bottom = (int) ((w.mSystemDecorRect.bottom+1) * scale - 0.5f);
983 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700984 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700985
Dianne Hackborn3e52fc22012-05-15 17:58:02 -0700986 void updateSurfaceWindowCrop(final boolean recoveringMemory) {
987 final WindowState w = mWin;
988
989 // Need to recompute a new system decor rect each time.
990 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
991 // Currently can't do this cropping for scaled windows. We'll
992 // just keep the crop rect the same as the source surface.
993 w.mSystemDecorRect.set(0, 0, w.mRequestedWidth, w.mRequestedHeight);
994 } else if (w.mLayer >= mService.mSystemDecorLayer) {
995 // Above the decor layer is easy, just use the entire window.
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700996 // Unless we have a universe background... in which case all the
997 // windows need to be cropped by the screen, so they don't cover
998 // the universe background.
999 if (mAnimator.mUniverseBackground == null) {
1000 w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(),
1001 w.mCompatFrame.height());
1002 } else {
1003 applyDecorRect(mService.mScreenRect);
1004 }
1005 } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND) {
1006 // The universe background isn't cropped.
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001007 w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(),
1008 w.mCompatFrame.height());
1009 } else {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001010 applyDecorRect(mService.mSystemDecorRect);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001011 }
1012
1013 if (!w.mSystemDecorRect.equals(w.mLastSystemDecorRect)) {
1014 w.mLastSystemDecorRect.set(w.mSystemDecorRect);
1015 try {
1016 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1017 "CROP " + w.mSystemDecorRect.toShortString(), null);
1018 mSurface.setWindowCrop(w.mSystemDecorRect);
1019 } catch (RuntimeException e) {
1020 Slog.w(TAG, "Error setting crop surface of " + w
1021 + " crop=" + w.mSystemDecorRect.toShortString(), e);
1022 if (!recoveringMemory) {
1023 mService.reclaimSomeSurfaceMemoryLocked(this, "crop", true);
1024 }
1025 }
1026 }
1027 }
1028
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001029 void setSurfaceBoundaries(final boolean recoveringMemory) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001030 final WindowState w = mWin;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001031 int width, height;
1032 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
1033 // for a scaled surface, we just want to use
1034 // the requested size.
1035 width = w.mRequestedWidth;
1036 height = w.mRequestedHeight;
1037 } else {
1038 width = w.mCompatFrame.width();
1039 height = w.mCompatFrame.height();
1040 }
1041
1042 if (width < 1) {
1043 width = 1;
1044 }
1045 if (height < 1) {
1046 height = 1;
1047 }
1048 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
1049 if (surfaceResized) {
1050 mSurfaceW = width;
1051 mSurfaceH = height;
1052 }
1053
Craig Mautner4d7349b2012-04-20 14:52:47 -07001054 final float left = w.mShownFrame.left;
1055 final float top = w.mShownFrame.top;
1056 if (mSurfaceX != left || mSurfaceY != top) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001057 try {
1058 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
Craig Mautner4d7349b2012-04-20 14:52:47 -07001059 "POS " + left + ", " + top, null);
1060 mSurfaceX = left;
1061 mSurfaceY = top;
1062 mSurface.setPosition(left, top);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001063 } catch (RuntimeException e) {
1064 Slog.w(TAG, "Error positioning surface of " + w
Craig Mautner4d7349b2012-04-20 14:52:47 -07001065 + " pos=(" + left
1066 + "," + top + ")", e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001067 if (!recoveringMemory) {
1068 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
1069 }
1070 }
1071 }
1072
1073 if (surfaceResized) {
1074 try {
1075 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1076 "SIZE " + width + "x" + height, null);
1077 mSurfaceResized = true;
1078 mSurface.setSize(width, height);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001079 mAnimator.mPendingLayoutChanges |=
1080 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Craig Mautner3a67f352012-05-07 11:21:33 -07001081 if ((w.mAttrs.flags & LayoutParams.FLAG_DIM_BEHIND) != 0) {
1082 mAnimator.startDimming(this, w.mExiting ? 0 : w.mAttrs.dimAmount,
1083 mService.mAppDisplayWidth, mService.mAppDisplayHeight);
1084 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001085 } catch (RuntimeException e) {
1086 // If something goes wrong with the surface (such
1087 // as running out of memory), don't take down the
1088 // entire system.
1089 Slog.e(TAG, "Error resizing surface of " + w
1090 + " size=(" + width + "x" + height + ")", e);
1091 if (!recoveringMemory) {
1092 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
1093 }
1094 }
1095 }
Dianne Hackborn85afd1b2012-05-13 13:31:06 -07001096
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001097 updateSurfaceWindowCrop(recoveringMemory);
Craig Mautneracaf9cc2012-04-17 11:45:25 -07001098 }
1099
1100 public void prepareSurfaceLocked(final boolean recoveringMemory) {
1101 final WindowState w = mWin;
1102 if (mSurface == null) {
1103 if (w.mOrientationChanging) {
1104 if (DEBUG_ORIENTATION) {
1105 Slog.v(TAG, "Orientation change skips hidden " + w);
1106 }
1107 w.mOrientationChanging = false;
1108 }
1109 return;
1110 }
1111
1112 boolean displayed = false;
1113
1114 computeShownFrameLocked();
1115
1116 setSurfaceBoundaries(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001117
Craig Mautner0fa77c12012-06-11 15:57:19 -07001118 if (mWin.mIsWallpaper && !mWin.mWallpaperVisible) {
1119 // Wallpaper is no longer visible and there is no wp target => hide it.
1120 hide();
1121 } else if (w.mAttachedHidden || !w.isReadyForDisplay()) {
Craig Mautner0afddcb2012-05-08 15:38:00 -07001122 hide();
Craig Mautnerb9836b92012-06-11 11:40:09 -07001123 mAnimator.hideWallpapersLocked(w);
1124
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001125 // If we are waiting for this window to handle an
1126 // orientation change, well, it is hidden, so
1127 // doesn't really matter. Note that this does
1128 // introduce a potential glitch if the window
1129 // becomes unhidden before it has drawn for the
1130 // new orientation.
1131 if (w.mOrientationChanging) {
1132 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001133 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001134 "Orientation change skips hidden " + w);
1135 }
1136 } else if (mLastLayer != mAnimLayer
1137 || mLastAlpha != mShownAlpha
1138 || mLastDsDx != mDsDx
1139 || mLastDtDx != mDtDx
1140 || mLastDsDy != mDsDy
1141 || mLastDtDy != mDtDy
1142 || w.mLastHScale != w.mHScale
1143 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001144 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001145 displayed = true;
1146 mLastAlpha = mShownAlpha;
1147 mLastLayer = mAnimLayer;
1148 mLastDsDx = mDsDx;
1149 mLastDtDx = mDtDx;
1150 mLastDsDy = mDsDy;
1151 mLastDtDy = mDtDy;
1152 w.mLastHScale = w.mHScale;
1153 w.mLastVScale = w.mVScale;
1154 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1155 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
1156 + " matrix=[" + (mDsDx*w.mHScale)
1157 + "," + (mDtDx*w.mVScale)
1158 + "][" + (mDsDy*w.mHScale)
1159 + "," + (mDtDy*w.mVScale) + "]", null);
1160 if (mSurface != null) {
1161 try {
1162 mSurfaceAlpha = mShownAlpha;
1163 mSurface.setAlpha(mShownAlpha);
Craig Mautnerad5725d2012-06-05 10:20:56 -07001164 mSurfaceLayer = mAnimLayer;
1165 mSurface.setLayer(mAnimLayer);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001166 mSurface.setMatrix(
1167 mDsDx*w.mHScale, mDtDx*w.mVScale,
1168 mDsDy*w.mHScale, mDtDy*w.mVScale);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001169
1170 if (mLastHidden && mDrawState == HAS_DRAWN) {
1171 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1172 "SHOW (performLayout)", null);
1173 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
1174 + " during relayout");
1175 if (showSurfaceRobustlyLocked()) {
1176 mLastHidden = false;
Craig Mautner507a2ee2012-06-13 08:39:38 -07001177 if (w.mIsWallpaper) {
1178 mService.dispatchWallpaperVisibility(w, true);
1179 }
Craig Mautner749a7bb2012-04-02 13:49:53 -07001180 } else {
1181 w.mOrientationChanging = false;
1182 }
1183 }
1184 if (mSurface != null) {
1185 w.mToken.hasVisible = true;
1186 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001187 } catch (RuntimeException e) {
1188 Slog.w(TAG, "Error updating surface in " + w, e);
1189 if (!recoveringMemory) {
1190 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
1191 }
1192 }
1193 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001194 } else {
Craig Mautner9e809442012-06-22 17:13:04 -07001195 if (DEBUG_ANIM && isAnimating()) {
1196 Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
Craig Mautner83339b42012-05-01 22:13:23 -07001197 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001198 displayed = true;
1199 }
1200
1201 if (displayed) {
1202 if (w.mOrientationChanging) {
1203 if (!w.isDrawnLw()) {
Craig Mautnerd09cc4b2012-04-04 10:23:31 -07001204 mAnimator.mBulkUpdateParams |= CLEAR_ORIENTATION_CHANGE_COMPLETE;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001205 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001206 "Orientation continue waiting for draw in " + w);
1207 } else {
1208 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001209 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001210 }
1211 }
1212 w.mToken.hasVisible = true;
1213 }
1214 }
1215
Craig Mautner48ba1e72012-04-02 13:18:16 -07001216 void setTransparentRegionHint(final Region region) {
Craig Mautner1f4e0cc2012-04-10 14:24:38 -07001217 if (mSurface == null) {
1218 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1219 return;
1220 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001221 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1222 ">>> OPEN TRANSACTION setTransparentRegion");
1223 Surface.openTransaction();
1224 try {
1225 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1226 "transparentRegionHint=" + region, null);
1227 mSurface.setTransparentRegionHint(region);
1228 } finally {
1229 Surface.closeTransaction();
1230 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1231 "<<< CLOSE TRANSACTION setTransparentRegion");
1232 }
1233 }
1234
1235 void setWallpaperOffset(int left, int top) {
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001236 mSurfaceX = left;
1237 mSurfaceY = top;
1238 if (mAnimating) {
1239 // If this window (or its app token) is animating, then the position
1240 // of the surface will be re-computed on the next animation frame.
1241 // We can't poke it directly here because it depends on whatever
1242 // transformation is being applied by the animation.
1243 return;
1244 }
1245 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1246 ">>> OPEN TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001247 Surface.openTransaction();
1248 try {
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001249 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1250 "POS " + left + ", " + top, null);
1251 mSurface.setPosition(mWin.mFrame.left + left, mWin.mFrame.top + top);
1252 updateSurfaceWindowCrop(false);
Craig Mautner48ba1e72012-04-02 13:18:16 -07001253 } catch (RuntimeException e) {
1254 Slog.w(TAG, "Error positioning surface of " + mWin
1255 + " pos=(" + left + "," + top + ")", e);
Dianne Hackborn3e52fc22012-05-15 17:58:02 -07001256 } finally {
1257 Surface.closeTransaction();
1258 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1259 "<<< CLOSE TRANSACTION setWallpaperOffset");
Craig Mautner48ba1e72012-04-02 13:18:16 -07001260 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001261 }
1262
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001263 // This must be called while inside a transaction.
1264 boolean performShowLocked() {
1265 if (DEBUG_VISIBILITY) {
1266 RuntimeException e = null;
1267 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1268 e = new RuntimeException();
1269 e.fillInStackTrace();
1270 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001271 Slog.v(TAG, "performShow on " + this
Craig Mautner749a7bb2012-04-02 13:49:53 -07001272 + ": mDrawState=" + mDrawState + " readyForDisplay="
Dianne Hackborn6e2281d2012-06-19 17:48:32 -07001273 + mWin.isReadyForDisplayIgnoringKeyguard()
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001274 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
1275 }
Dianne Hackborn6e2281d2012-06-19 17:48:32 -07001276 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplayIgnoringKeyguard()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001277 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001278 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -07001279 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001280 + " during animation: policyVis=" + mWin.mPolicyVisibility
1281 + " attHidden=" + mWin.mAttachedHidden
1282 + " tok.hiddenRequested="
1283 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1284 + " tok.hidden="
1285 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1286 + " animating=" + mAnimating
1287 + " tok animating="
Craig Mautner59431632012-04-04 11:56:44 -07001288 + (mWin.mAppToken != null ? mWin.mAppToken.mAppAnimator.animating : false));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001289
1290 mService.enableScreenIfNeededLocked();
1291
1292 applyEnterAnimationLocked();
1293
Craig Mautnerde6198e2012-04-19 09:59:31 -07001294 // Force the show in the next prepareSurfaceLocked() call.
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001295 mLastAlpha = -1;
Craig Mautneref25d7a2012-05-15 23:01:47 -07001296 if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
1297 Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + this);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001298 mDrawState = HAS_DRAWN;
Craig Mautnerde6198e2012-04-19 09:59:31 -07001299 mService.scheduleAnimationLocked();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001300
1301 int i = mWin.mChildWindows.size();
1302 while (i > 0) {
1303 i--;
1304 WindowState c = mWin.mChildWindows.get(i);
1305 if (c.mAttachedHidden) {
1306 c.mAttachedHidden = false;
1307 if (c.mWinAnimator.mSurface != null) {
1308 c.mWinAnimator.performShowLocked();
1309 // It hadn't been shown, which means layout not
1310 // performed on it, so now we want to make sure to
1311 // do a layout. If called from within the transaction
1312 // loop, this will cause it to restart with a new
1313 // layout.
1314 mService.mLayoutNeeded = true;
1315 }
1316 }
1317 }
1318
1319 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1320 && mWin.mAppToken != null) {
1321 mWin.mAppToken.firstWindowDrawn = true;
1322
1323 if (mWin.mAppToken.startingData != null) {
1324 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001325 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001326 "Finish starting " + mWin.mToken
1327 + ": first real window is shown, no animation");
1328 // If this initial window is animating, stop it -- we
1329 // will do an animation to reveal it from behind the
1330 // starting window, so there is no need for it to also
1331 // be doing its own stuff.
Craig Mautner4d7349b2012-04-20 14:52:47 -07001332 clearAnimation();
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001333 mService.mFinishedStarting.add(mWin.mAppToken);
1334 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1335 }
1336 mWin.mAppToken.updateReportedVisibilityLocked();
1337 }
1338
1339 return true;
1340 }
1341
1342 return false;
1343 }
1344
1345 /**
1346 * Have the surface flinger show a surface, robustly dealing with
1347 * error conditions. In particular, if there is not enough memory
1348 * to show the surface, then we will try to get rid of other surfaces
1349 * in order to succeed.
1350 *
1351 * @return Returns true if the surface was successfully shown.
1352 */
1353 boolean showSurfaceRobustlyLocked() {
1354 try {
1355 if (mSurface != null) {
1356 mSurfaceShown = true;
1357 mSurface.show();
1358 if (mWin.mTurnOnScreen) {
1359 if (DEBUG_VISIBILITY) Slog.v(TAG,
1360 "Show surface turning screen on: " + mWin);
1361 mWin.mTurnOnScreen = false;
Craig Mautner7d8df392012-04-06 15:26:23 -07001362 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001363 }
1364 }
1365 return true;
1366 } catch (RuntimeException e) {
1367 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1368 }
1369
1370 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1371
1372 return false;
1373 }
1374
1375 void applyEnterAnimationLocked() {
1376 final int transit;
1377 if (mEnterAnimationPending) {
1378 mEnterAnimationPending = false;
1379 transit = WindowManagerPolicy.TRANSIT_ENTER;
1380 } else {
1381 transit = WindowManagerPolicy.TRANSIT_SHOW;
1382 }
1383
1384 applyAnimationLocked(transit, true);
1385 }
1386
Craig Mautner48ba1e72012-04-02 13:18:16 -07001387 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001388 /**
1389 * Choose the correct animation and set it to the passed WindowState.
1390 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1391 * then the animation will be app_starting_exit. Any other value loads the animation from
1392 * the switch statement below.
1393 * @param isEntrance The animation type the last time this was called. Used to keep from
1394 * loading the same animation twice.
1395 * @return true if an animation has been loaded.
1396 */
1397 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1398 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1399 // If we are trying to apply an animation, but already running
1400 // an animation of the same type, then just leave that one alone.
1401 return true;
1402 }
1403
1404 // Only apply an animation if the display isn't frozen. If it is
1405 // frozen, there is no reason to animate and it can cause strange
1406 // artifacts when we unfreeze the display if some different animation
1407 // is running.
1408 if (mService.okToDisplay()) {
1409 int anim = mPolicy.selectAnimationLw(mWin, transit);
1410 int attr = -1;
1411 Animation a = null;
1412 if (anim != 0) {
1413 a = AnimationUtils.loadAnimation(mContext, anim);
1414 } else {
1415 switch (transit) {
1416 case WindowManagerPolicy.TRANSIT_ENTER:
1417 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1418 break;
1419 case WindowManagerPolicy.TRANSIT_EXIT:
1420 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1421 break;
1422 case WindowManagerPolicy.TRANSIT_SHOW:
1423 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1424 break;
1425 case WindowManagerPolicy.TRANSIT_HIDE:
1426 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1427 break;
1428 }
1429 if (attr >= 0) {
1430 a = mService.loadAnimation(mWin.mAttrs, attr);
1431 }
1432 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001433 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001434 "applyAnimation: win=" + this
1435 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
Craig Mautner4d7349b2012-04-20 14:52:47 -07001436 + " a=" + a
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001437 + " mAnimation=" + mAnimation
Craig Mautner83339b42012-05-01 22:13:23 -07001438 + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001439 if (a != null) {
1440 if (WindowManagerService.DEBUG_ANIM) {
1441 RuntimeException e = null;
1442 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1443 e = new RuntimeException();
1444 e.fillInStackTrace();
1445 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001446 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001447 }
1448 setAnimation(a);
1449 mAnimationIsEntrance = isEntrance;
1450 }
1451 } else {
1452 clearAnimation();
1453 }
1454
1455 return mAnimation != null;
1456 }
1457
Craig Mautnera2c77052012-03-26 12:14:43 -07001458 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1459 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1460 || mAnimation != null) {
1461 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1462 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1463 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1464 pw.print(" mAnimation="); pw.println(mAnimation);
1465 }
1466 if (mHasTransformation || mHasLocalTransformation) {
1467 pw.print(prefix); pw.print("XForm: has=");
1468 pw.print(mHasTransformation);
1469 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1470 pw.print(" "); mTransformation.printShortString(pw);
1471 pw.println();
1472 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001473 if (mSurface != null) {
1474 if (dumpAll) {
1475 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001476 pw.print(prefix); pw.print("mDrawState="); pw.print(mDrawState);
1477 pw.print(" mLastHidden="); pw.println(mLastHidden);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001478 }
1479 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1480 pw.print(" layer="); pw.print(mSurfaceLayer);
1481 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1482 pw.print(" rect=("); pw.print(mSurfaceX);
1483 pw.print(","); pw.print(mSurfaceY);
1484 pw.print(") "); pw.print(mSurfaceW);
1485 pw.print(" x "); pw.println(mSurfaceH);
1486 }
1487 if (mPendingDestroySurface != null) {
1488 pw.print(prefix); pw.print("mPendingDestroySurface=");
1489 pw.println(mPendingDestroySurface);
1490 }
1491 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1492 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1493 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1494 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -07001495 if (mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND) {
1496 pw.print(prefix); pw.print("mUniverseTransform=");
1497 mUniverseTransform.printShortString(pw);
1498 pw.println();
1499 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001500 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1501 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1502 pw.print(" mAlpha="); pw.print(mAlpha);
1503 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1504 }
1505 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1506 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1507 pw.print(" mDsDx="); pw.print(mDsDx);
1508 pw.print(" mDtDx="); pw.print(mDtDx);
1509 pw.print(" mDsDy="); pw.print(mDsDy);
1510 pw.print(" mDtDy="); pw.println(mDtDy);
1511 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001512 }
1513
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001514 @Override
1515 public String toString() {
1516 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1517 sb.append(mWin.mLastTitle + "): ");
1518 sb.append("mSurface " + mSurface);
1519 sb.append(", mAnimation " + mAnimation);
1520 return sb.toString();
1521 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001522}