blob: 0a7e7fd5a0f7c6245c98d227cee72ebf6944a001 [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 Mautnera2c77052012-03-26 12:14:43 -0700145 }
146
147 public void setAnimation(Animation anim) {
Craig Mautnerbec53f72012-04-05 11:49:05 -0700148 if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
Craig Mautnera2c77052012-03-26 12:14:43 -0700149 mAnimating = false;
150 mLocalAnimating = false;
151 mAnimation = anim;
152 mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
153 mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
154 // Start out animation gone if window is gone, or visible if window is visible.
155 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700156 mTransformation.setAlpha(mLastHidden ? 0 : 1);
Craig Mautnera2c77052012-03-26 12:14:43 -0700157 mHasLocalTransformation = true;
158 }
159
160 public void clearAnimation() {
161 if (mAnimation != null) {
162 mAnimating = true;
163 mLocalAnimating = false;
164 mAnimation.cancel();
165 mAnimation = null;
166 }
167 }
168
169 /** Is the window or its container currently animating? */
170 boolean isAnimating() {
171 final WindowState attached = mAttachedWindow;
172 final AppWindowToken atoken = mWin.mAppToken;
173 return mAnimation != null
174 || (attached != null && attached.mWinAnimator.mAnimation != null)
175 || (atoken != null &&
Craig Mautner59431632012-04-04 11:56:44 -0700176 (atoken.mAppAnimator.animation != null
Craig Mautnera2c77052012-03-26 12:14:43 -0700177 || atoken.inPendingTransaction));
178 }
179
180 /** Is this window currently animating? */
181 boolean isWindowAnimating() {
182 return mAnimation != null;
183 }
184
Craig Mautnera2c77052012-03-26 12:14:43 -0700185 void cancelExitAnimationForNextAnimationLocked() {
Craig Mautnera2c77052012-03-26 12:14:43 -0700186 if (mAnimation != null) {
187 mAnimation.cancel();
188 mAnimation = null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700189 destroySurfaceLocked();
Craig Mautnera2c77052012-03-26 12:14:43 -0700190 }
Craig Mautnera2c77052012-03-26 12:14:43 -0700191 }
192
193 private boolean stepAnimation(long currentTime) {
194 if ((mAnimation == null) || !mLocalAnimating) {
195 return false;
196 }
197 mTransformation.clear();
198 final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700199 if (DEBUG_ANIM) Slog.v(
200 TAG, "Stepped animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700201 ": more=" + more + ", xform=" + mTransformation);
202 return more;
203 }
204
205 // This must be called while inside a transaction. Returns true if
206 // there is more animation to run.
207 boolean stepAnimationLocked(long currentTime) {
208 // Save the animation state as it was before this step so WindowManagerService can tell if
209 // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
210 mWasAnimating = mAnimating;
211 if (mService.okToDisplay()) {
212 // We will run animations as long as the display isn't frozen.
213
214 if (mWin.isDrawnLw() && mAnimation != null) {
215 mHasTransformation = true;
216 mHasLocalTransformation = true;
217 if (!mLocalAnimating) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700218 if (DEBUG_ANIM) Slog.v(
219 TAG, "Starting animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700220 " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
221 " wh=" + mWin.mFrame.height() +
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700222 " dw=" + mAnimDw + " dh=" + mAnimDh +
Craig Mautnera2c77052012-03-26 12:14:43 -0700223 " scale=" + mService.mWindowAnimationScale);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700224 mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
225 mAnimDw, mAnimDh);
Craig Mautnera2c77052012-03-26 12:14:43 -0700226 mAnimation.setStartTime(currentTime);
227 mLocalAnimating = true;
228 mAnimating = true;
229 }
230 if ((mAnimation != null) && mLocalAnimating) {
231 if (stepAnimation(currentTime)) {
232 return true;
233 }
234 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700235 if (DEBUG_ANIM) Slog.v(
236 TAG, "Finished animation in " + this +
Craig Mautnera2c77052012-03-26 12:14:43 -0700237 " @ " + currentTime);
238 //WindowManagerService.this.dump();
239 }
240 mHasLocalTransformation = false;
241 if ((!mLocalAnimating || mAnimationIsEntrance) && mWin.mAppToken != null
Craig Mautner59431632012-04-04 11:56:44 -0700242 && mWin.mAppToken.mAppAnimator.animation != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700243 // When our app token is animating, we kind-of pretend like
244 // we are as well. Note the mLocalAnimating mAnimationIsEntrance
245 // part of this check means that we will only do this if
246 // our window is not currently exiting, or it is not
247 // locally animating itself. The idea being that one that
248 // is exiting and doing a local animation should be removed
249 // once that animation is done.
250 mAnimating = true;
251 mHasTransformation = true;
252 mTransformation.clear();
253 return false;
254 } else if (mHasTransformation) {
255 // Little trick to get through the path below to act like
256 // we have finished an animation.
257 mAnimating = true;
258 } else if (isAnimating()) {
259 mAnimating = true;
260 }
261 } else if (mAnimation != null) {
262 // If the display is frozen, and there is a pending animation,
263 // clear it and make sure we run the cleanup code.
264 mAnimating = true;
265 mLocalAnimating = true;
266 mAnimation.cancel();
267 mAnimation = null;
268 }
269
270 if (!mAnimating && !mLocalAnimating) {
271 return false;
272 }
273
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700274 // Done animating, clean up.
275 if (DEBUG_ANIM) Slog.v(
276 TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
Craig Mautnera2c77052012-03-26 12:14:43 -0700277 + ", reportedVisible="
278 + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
279
280 mAnimating = false;
281 mLocalAnimating = false;
282 if (mAnimation != null) {
283 mAnimation.cancel();
284 mAnimation = null;
285 }
Craig Mautnere7ae2502012-03-26 17:11:19 -0700286 if (mAnimator.mWindowDetachedWallpaper == mWin) {
287 mAnimator.mWindowDetachedWallpaper = null;
Craig Mautnera2c77052012-03-26 12:14:43 -0700288 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700289 mAnimLayer = mWin.mLayer;
Craig Mautnera2c77052012-03-26 12:14:43 -0700290 if (mWin.mIsImWindow) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700291 mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700292 } else if (mWin.mIsWallpaper) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700293 mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
Craig Mautnera2c77052012-03-26 12:14:43 -0700294 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700295 if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
296 + " anim layer: " + mAnimLayer);
Craig Mautnera2c77052012-03-26 12:14:43 -0700297 mHasTransformation = false;
298 mHasLocalTransformation = false;
299 if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
300 if (WindowState.DEBUG_VISIBILITY) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700301 Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
Craig Mautnera2c77052012-03-26 12:14:43 -0700302 + mWin.mPolicyVisibilityAfterAnim);
303 }
304 mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
305 mService.mLayoutNeeded = true;
306 if (!mWin.mPolicyVisibility) {
307 if (mService.mCurrentFocus == mWin) {
308 mService.mFocusMayChange = true;
309 }
310 // Window is no longer visible -- make sure if we were waiting
311 // for it to be displayed before enabling the display, that
312 // we allow the display to be enabled now.
313 mService.enableScreenIfNeededLocked();
314 }
315 }
316 mTransformation.clear();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700317 if (mDrawState == HAS_DRAWN
Craig Mautnera2c77052012-03-26 12:14:43 -0700318 && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
319 && mWin.mAppToken != null
320 && mWin.mAppToken.firstWindowDrawn
321 && mWin.mAppToken.startingData != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700322 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
Craig Mautnera2c77052012-03-26 12:14:43 -0700323 + mWin.mToken + ": first real window done animating");
324 mService.mFinishedStarting.add(mWin.mAppToken);
325 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
326 }
327
328 finishExit();
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700329 mAnimator.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
330 if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
331 "WindowStateAnimator", mAnimator.mPendingLayoutChanges);
Craig Mautnera2c77052012-03-26 12:14:43 -0700332
333 if (mWin.mAppToken != null) {
334 mWin.mAppToken.updateReportedVisibilityLocked();
335 }
336
337 return false;
338 }
339
340 void finishExit() {
341 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700342 TAG, "finishExit in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700343 + ": exiting=" + mWin.mExiting
344 + " remove=" + mWin.mRemoveOnExit
345 + " windowAnimating=" + isWindowAnimating());
346
347 final int N = mWin.mChildWindows.size();
348 for (int i=0; i<N; i++) {
349 mWin.mChildWindows.get(i).mWinAnimator.finishExit();
350 }
351
352 if (!mWin.mExiting) {
353 return;
354 }
355
356 if (isWindowAnimating()) {
357 return;
358 }
359
360 if (WindowManagerService.localLOGV) Slog.v(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700361 TAG, "Exit animation finished in " + this
Craig Mautnera2c77052012-03-26 12:14:43 -0700362 + ": remove=" + mWin.mRemoveOnExit);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700363 if (mSurface != null) {
Craig Mautnera2c77052012-03-26 12:14:43 -0700364 mService.mDestroySurface.add(mWin);
365 mWin.mDestroying = true;
366 if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
367 mWin, "HIDE (finishExit)", null);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700368 mSurfaceShown = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700369 try {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700370 mSurface.hide();
Craig Mautnera2c77052012-03-26 12:14:43 -0700371 } catch (RuntimeException e) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700372 Slog.w(TAG, "Error hiding surface in " + this, e);
Craig Mautnera2c77052012-03-26 12:14:43 -0700373 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700374 mLastHidden = true;
Craig Mautnera2c77052012-03-26 12:14:43 -0700375 }
376 mWin.mExiting = false;
377 if (mWin.mRemoveOnExit) {
378 mService.mPendingRemove.add(mWin);
379 mWin.mRemoveOnExit = false;
380 }
381 }
382
Craig Mautnera608b882012-03-30 13:03:49 -0700383 boolean finishDrawingLocked() {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700384 if (mDrawState == DRAW_PENDING) {
Craig Mautner48ba1e72012-04-02 13:18:16 -0700385 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION) Slog.v(
Craig Mautnera608b882012-03-30 13:03:49 -0700386 TAG, "finishDrawingLocked: " + this + " in " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700387 mDrawState = COMMIT_DRAW_PENDING;
Craig Mautnera608b882012-03-30 13:03:49 -0700388 return true;
389 }
390 return false;
391 }
392
393 // This must be called while inside a transaction.
394 boolean commitFinishDrawingLocked(long currentTime) {
395 //Slog.i(TAG, "commitFinishDrawingLocked: " + mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700396 if (mDrawState != COMMIT_DRAW_PENDING) {
Craig Mautnera608b882012-03-30 13:03:49 -0700397 return false;
398 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700399 mDrawState = READY_TO_SHOW;
Craig Mautnera608b882012-03-30 13:03:49 -0700400 final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
401 final AppWindowToken atoken = mWin.mAppToken;
402 if (atoken == null || atoken.allDrawn || starting) {
403 performShowLocked();
404 }
405 return true;
406 }
407
Craig Mautner7d8df392012-04-06 15:26:23 -0700408 static class SurfaceTrace extends Surface {
409 private final static String SURFACE_TAG = "SurfaceTrace";
410 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
Craig Mautner7358fbf2012-04-12 21:06:33 -0700411
Craig Mautner7d8df392012-04-06 15:26:23 -0700412 private float mSurfaceTraceAlpha = 0;
Craig Mautner7358fbf2012-04-12 21:06:33 -0700413 private int mLayer;
414 private PointF mPosition = new PointF();
Craig Mautner7d8df392012-04-06 15:26:23 -0700415 private Point mSize;
Craig Mautner7358fbf2012-04-12 21:06:33 -0700416 private boolean mShown = false;
417 private String mName = "Not named";
418
Craig Mautner7d8df392012-04-06 15:26:23 -0700419 public SurfaceTrace(SurfaceSession s,
Craig Mautner7358fbf2012-04-12 21:06:33 -0700420 int pid, int display, int w, int h, int format, int flags) throws
421 OutOfResourcesException {
422 super(s, pid, display, w, h, format, flags);
423 mSize = new Point(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700424 Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700425 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700426 }
427
Craig Mautner7d8df392012-04-06 15:26:23 -0700428 public SurfaceTrace(SurfaceSession s,
Craig Mautner7358fbf2012-04-12 21:06:33 -0700429 int pid, String name, int display, int w, int h, int format, int flags)
430 throws OutOfResourcesException {
431 super(s, pid, name, display, w, h, format, flags);
432 mName = name;
433 mSize = new Point(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700434 Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700435 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700436 }
437
438 @Override
439 public void setAlpha(float alpha) {
440 super.setAlpha(alpha);
Craig Mautner7d8df392012-04-06 15:26:23 -0700441 mSurfaceTraceAlpha = alpha;
442 Slog.v(SURFACE_TAG, "setAlpha: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700443 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700444 }
445
446 @Override
447 public void setLayer(int zorder) {
448 super.setLayer(zorder);
449 mLayer = zorder;
Craig Mautner7d8df392012-04-06 15:26:23 -0700450 Slog.v(SURFACE_TAG, "setLayer: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700451 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700452
453 sSurfaces.remove(this);
454 int i;
455 for (i = sSurfaces.size() - 1; i >= 0; i--) {
Craig Mautner7d8df392012-04-06 15:26:23 -0700456 SurfaceTrace s = sSurfaces.get(i);
Craig Mautner7358fbf2012-04-12 21:06:33 -0700457 if (s.mLayer < zorder) {
458 break;
459 }
460 }
461 sSurfaces.add(i + 1, this);
462 }
463
464 @Override
465 public void setPosition(float x, float y) {
466 super.setPosition(x, y);
467 mPosition = new PointF(x, y);
Craig Mautner7d8df392012-04-06 15:26:23 -0700468 Slog.v(SURFACE_TAG, "setPosition: " + 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 setSize(int w, int h) {
474 super.setSize(w, h);
475 mSize = new Point(w, h);
Craig Mautner7d8df392012-04-06 15:26:23 -0700476 Slog.v(SURFACE_TAG, "setSize: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700477 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700478 }
479
480 @Override
481 public void hide() {
482 super.hide();
483 mShown = false;
Craig Mautner7d8df392012-04-06 15:26:23 -0700484 Slog.v(SURFACE_TAG, "hide: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700485 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700486 }
487 @Override
488 public void show() {
489 super.show();
490 mShown = true;
Craig Mautner7d8df392012-04-06 15:26:23 -0700491 Slog.v(SURFACE_TAG, "show: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700492 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700493 }
494
495 @Override
496 public void destroy() {
497 super.destroy();
Craig Mautner7d8df392012-04-06 15:26:23 -0700498 Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700499 + Debug.getCallers(3));
Craig Mautner7358fbf2012-04-12 21:06:33 -0700500 sSurfaces.remove(this);
501 }
502
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700503 @Override
504 public void release() {
505 super.release();
506 Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
Craig Mautnera51a9562012-04-17 17:05:26 -0700507 + Debug.getCallers(3));
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700508 sSurfaces.remove(this);
509 }
510
Craig Mautner7358fbf2012-04-12 21:06:33 -0700511 static void dumpAllSurfaces() {
512 final int N = sSurfaces.size();
513 for (int i = 0; i < N; i++) {
514 Slog.i(TAG, "SurfaceDump: " + sSurfaces.get(i));
515 }
516 }
517
518 @Override
519 public String toString() {
520 return "Surface " + mName + ": shown=" + mShown + " layer=" + mLayer
Craig Mautner7d8df392012-04-06 15:26:23 -0700521 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
Craig Mautner7358fbf2012-04-12 21:06:33 -0700522 + " " + mSize.x + "x" + mSize.y;
523 }
524 }
525
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700526 Surface createSurfaceLocked() {
527 if (mSurface == null) {
528 mReportDestroySurface = false;
529 mSurfacePendingDestroy = false;
Craig Mautner48ba1e72012-04-02 13:18:16 -0700530 if (DEBUG_ORIENTATION) Slog.i(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700531 "createSurface " + this + ": DRAW NOW PENDING");
Craig Mautner749a7bb2012-04-02 13:49:53 -0700532 mDrawState = DRAW_PENDING;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700533 if (mWin.mAppToken != null) {
534 mWin.mAppToken.allDrawn = false;
535 }
536
537 mService.makeWindowFreezingScreenIfNeededLocked(mWin);
538
539 int flags = 0;
540 final WindowManager.LayoutParams attrs = mWin.mAttrs;
541
542 if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
543 flags |= Surface.SECURE;
544 }
545 if (WindowState.DEBUG_VISIBILITY) Slog.v(
546 TAG, "Creating surface in session "
547 + mSession.mSurfaceSession + " window " + this
548 + " w=" + mWin.mCompatFrame.width()
549 + " h=" + mWin.mCompatFrame.height() + " format="
550 + attrs.format + " flags=" + flags);
551
552 int w = mWin.mCompatFrame.width();
553 int h = mWin.mCompatFrame.height();
554 if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
555 // for a scaled surface, we always want the requested
556 // size.
557 w = mWin.mRequestedWidth;
558 h = mWin.mRequestedHeight;
559 }
560
561 // Something is wrong and SurfaceFlinger will not like this,
562 // try to revert to sane values
563 if (w <= 0) w = 1;
564 if (h <= 0) h = 1;
565
566 mSurfaceShown = false;
567 mSurfaceLayer = 0;
Craig Mautner7d8df392012-04-06 15:26:23 -0700568 mSurfaceAlpha = 0;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700569 mSurfaceX = 0;
570 mSurfaceY = 0;
571 mSurfaceW = w;
572 mSurfaceH = h;
573 try {
574 final boolean isHwAccelerated = (attrs.flags &
575 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
576 final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
577 if (!PixelFormat.formatHasAlpha(attrs.format)) {
578 flags |= Surface.OPAQUE;
579 }
Craig Mautner7358fbf2012-04-12 21:06:33 -0700580 if (DEBUG_SURFACE_TRACE) {
Craig Mautner7d8df392012-04-06 15:26:23 -0700581 mSurface = new SurfaceTrace(
Craig Mautner7358fbf2012-04-12 21:06:33 -0700582 mSession.mSurfaceSession, mSession.mPid,
583 attrs.getTitle().toString(),
584 0, w, h, format, flags);
585 } else {
586 mSurface = new Surface(
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700587 mSession.mSurfaceSession, mSession.mPid,
588 attrs.getTitle().toString(),
589 0, w, h, format, flags);
Craig Mautner7358fbf2012-04-12 21:06:33 -0700590 }
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700591 mWin.mHasSurface = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700592 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
593 " CREATE SURFACE "
594 + mSurface + " IN SESSION "
595 + mSession.mSurfaceSession
596 + ": pid=" + mSession.mPid + " format="
597 + attrs.format + " flags=0x"
598 + Integer.toHexString(flags)
599 + " / " + this);
600 } catch (Surface.OutOfResourcesException e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700601 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700602 Slog.w(TAG, "OutOfResourcesException creating surface");
603 mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700604 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700605 return null;
606 } catch (Exception e) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700607 mWin.mHasSurface = false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700608 Slog.e(TAG, "Exception creating surface", e);
Craig Mautner749a7bb2012-04-02 13:49:53 -0700609 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700610 return null;
611 }
612
613 if (WindowManagerService.localLOGV) Slog.v(
614 TAG, "Got surface: " + mSurface
615 + ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
616 + ", animLayer=" + mAnimLayer);
617 if (SHOW_LIGHT_TRANSACTIONS) {
618 Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
619 WindowManagerService.logSurface(mWin, "CREATE pos=("
620 + mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
621 + mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
622 + "), layer=" + mAnimLayer + " HIDE", null);
623 }
624 Surface.openTransaction();
625 try {
626 try {
627 mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
628 mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
629 mSurface.setPosition(mSurfaceX, mSurfaceY);
630 mSurfaceLayer = mAnimLayer;
631 mSurface.setLayer(mAnimLayer);
Craig Mautner7d8df392012-04-06 15:26:23 -0700632 mSurface.setAlpha(0);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700633 mSurfaceShown = false;
634 mSurface.hide();
635 if ((mWin.mAttrs.flags&WindowManager.LayoutParams.FLAG_DITHER) != 0) {
636 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "DITHER", null);
637 mSurface.setFlags(Surface.SURFACE_DITHER, Surface.SURFACE_DITHER);
638 }
639 } catch (RuntimeException e) {
640 Slog.w(TAG, "Error creating surface in " + w, e);
641 mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
642 }
Craig Mautner749a7bb2012-04-02 13:49:53 -0700643 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700644 } finally {
645 Surface.closeTransaction();
646 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
647 "<<< CLOSE TRANSACTION createSurfaceLocked");
648 }
649 if (WindowManagerService.localLOGV) Slog.v(
650 TAG, "Created surface " + this);
651 }
652 return mSurface;
653 }
654
655 void destroySurfaceLocked() {
656 if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
657 mWin.mAppToken.startingDisplayed = false;
658 }
659
Craig Mautner749a7bb2012-04-02 13:49:53 -0700660 mDrawState = NO_SURFACE;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700661 if (mSurface != null) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700662
663 int i = mWin.mChildWindows.size();
664 while (i > 0) {
665 i--;
666 WindowState c = mWin.mChildWindows.get(i);
667 c.mAttachedHidden = true;
668 }
669
670 if (mReportDestroySurface) {
671 mReportDestroySurface = false;
672 mSurfacePendingDestroy = true;
673 try {
674 mWin.mClient.dispatchGetNewSurface();
675 // We'll really destroy on the next time around.
676 return;
677 } catch (RemoteException e) {
678 }
679 }
680
681 try {
682 if (DEBUG_VISIBILITY) {
683 RuntimeException e = null;
684 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
685 e = new RuntimeException();
686 e.fillInStackTrace();
687 }
688 Slog.w(TAG, "Window " + this + " destroying surface "
689 + mSurface + ", session " + mSession, e);
690 }
691 if (mSurfaceDestroyDeferred) {
692 if (mSurface != null && mPendingDestroySurface != mSurface) {
693 if (mPendingDestroySurface != null) {
694 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
695 RuntimeException e = null;
696 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
697 e = new RuntimeException();
698 e.fillInStackTrace();
699 }
700 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
701 }
702 mPendingDestroySurface.destroy();
703 }
704 mPendingDestroySurface = mSurface;
705 }
706 } else {
707 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
708 RuntimeException e = null;
709 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
710 e = new RuntimeException();
711 e.fillInStackTrace();
712 }
713 WindowManagerService.logSurface(mWin, "DESTROY", e);
714 }
715 mSurface.destroy();
716 }
717 } catch (RuntimeException e) {
718 Slog.w(TAG, "Exception thrown when destroying Window " + this
719 + " surface " + mSurface + " session " + mSession
720 + ": " + e.toString());
721 }
722
723 mSurfaceShown = false;
724 mSurface = null;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700725 mWin.mHasSurface =false;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700726 }
727 }
728
729 void destroyDeferredSurfaceLocked() {
730 try {
731 if (mPendingDestroySurface != null) {
732 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
733 RuntimeException e = null;
734 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
735 e = new RuntimeException();
736 e.fillInStackTrace();
737 }
738 WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
739 }
740 mPendingDestroySurface.destroy();
741 }
742 } catch (RuntimeException e) {
Craig Mautnerd87946b2012-03-29 18:00:19 -0700743 Slog.w(TAG, "Exception thrown when destroying Window "
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700744 + this + " surface " + mPendingDestroySurface
745 + " session " + mSession + ": " + e.toString());
746 }
747 mSurfaceDestroyDeferred = false;
748 mPendingDestroySurface = null;
749 }
750
751 void computeShownFrameLocked() {
752 final boolean selfTransformation = mHasLocalTransformation;
753 Transformation attachedTransformation =
754 (mAttachedWindow != null && mAttachedWindow.mWinAnimator.mHasLocalTransformation)
755 ? mAttachedWindow.mWinAnimator.mTransformation : null;
Craig Mautner59431632012-04-04 11:56:44 -0700756 final AppWindowAnimator appAnimator =
757 mWin.mAppToken == null ? null : mWin.mAppToken.mAppAnimator;
758 Transformation appTransformation = (appAnimator != null && appAnimator.hasTransformation)
759 ? appAnimator.transformation : null;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700760
761 // Wallpapers are animated based on the "real" window they
762 // are currently targeting.
763 if (mWin.mAttrs.type == TYPE_WALLPAPER && mService.mLowerWallpaperTarget == null
764 && mService.mWallpaperTarget != null) {
765 if (mService.mWallpaperTarget.mWinAnimator.mHasLocalTransformation &&
766 mService.mWallpaperTarget.mWinAnimator.mAnimation != null &&
767 !mService.mWallpaperTarget.mWinAnimator.mAnimation.getDetachWallpaper()) {
768 attachedTransformation = mService.mWallpaperTarget.mWinAnimator.mTransformation;
769 if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
770 Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
771 }
772 }
Craig Mautner59431632012-04-04 11:56:44 -0700773 final AppWindowAnimator wpAppAnimator = mService.mWallpaperTarget.mAppToken == null
774 ? null : mService.mWallpaperTarget.mAppToken.mAppAnimator;
775 if (wpAppAnimator != null &&
776 wpAppAnimator.hasTransformation &&
777 wpAppAnimator.animation != null &&
778 !wpAppAnimator.animation.getDetachWallpaper()) {
779 appTransformation = wpAppAnimator.transformation;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700780 if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
781 Slog.v(TAG, "WP target app xform: " + appTransformation);
782 }
783 }
784 }
785
786 final boolean screenAnimation = mService.mAnimator.mScreenRotationAnimation != null
787 && mService.mAnimator.mScreenRotationAnimation.isAnimating();
788 if (selfTransformation || attachedTransformation != null
789 || appTransformation != null || screenAnimation) {
790 // cache often used attributes locally
791 final Rect frame = mWin.mFrame;
792 final float tmpFloats[] = mService.mTmpFloats;
793 final Matrix tmpMatrix = mWin.mTmpMatrix;
794
795 // Compute the desired transformation.
796 if (screenAnimation) {
797 // If we are doing a screen animation, the global rotation
798 // applied to windows can result in windows that are carefully
799 // aligned with each other to slightly separate, allowing you
800 // to see what is behind them. An unsightly mess. This...
801 // thing... magically makes it call good: scale each window
802 // slightly (two pixels larger in each dimension, from the
803 // window's center).
804 final float w = frame.width();
805 final float h = frame.height();
806 if (w>=1 && h>=1) {
807 tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
808 } else {
809 tmpMatrix.reset();
810 }
811 } else {
812 tmpMatrix.reset();
813 }
814 tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
815 if (selfTransformation) {
816 tmpMatrix.postConcat(mTransformation.getMatrix());
817 }
818 tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
819 if (attachedTransformation != null) {
820 tmpMatrix.postConcat(attachedTransformation.getMatrix());
821 }
822 if (appTransformation != null) {
823 tmpMatrix.postConcat(appTransformation.getMatrix());
824 }
825 if (screenAnimation) {
826 tmpMatrix.postConcat(
827 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getMatrix());
828 }
829
830 // "convert" it into SurfaceFlinger's format
831 // (a 2x2 matrix + an offset)
832 // Here we must not transform the position of the surface
833 // since it is already included in the transformation.
834 //Slog.i(TAG, "Transform: " + matrix);
835
836 mHaveMatrix = true;
837 tmpMatrix.getValues(tmpFloats);
838 mDsDx = tmpFloats[Matrix.MSCALE_X];
839 mDtDx = tmpFloats[Matrix.MSKEW_Y];
840 mDsDy = tmpFloats[Matrix.MSKEW_X];
841 mDtDy = tmpFloats[Matrix.MSCALE_Y];
842 float x = tmpFloats[Matrix.MTRANS_X];
843 float y = tmpFloats[Matrix.MTRANS_Y];
844 int w = frame.width();
845 int h = frame.height();
846 mWin.mShownFrame.set(x, y, x+w, y+h);
847
848 // Now set the alpha... but because our current hardware
849 // can't do alpha transformation on a non-opaque surface,
850 // turn it off if we are running an animation that is also
851 // transforming since it is more important to have that
852 // animation be smooth.
853 mShownAlpha = mAlpha;
854 if (!mService.mLimitedAlphaCompositing
855 || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
856 || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
857 && x == frame.left && y == frame.top))) {
858 //Slog.i(TAG, "Applying alpha transform");
859 if (selfTransformation) {
860 mShownAlpha *= mTransformation.getAlpha();
861 }
862 if (attachedTransformation != null) {
863 mShownAlpha *= attachedTransformation.getAlpha();
864 }
865 if (appTransformation != null) {
866 mShownAlpha *= appTransformation.getAlpha();
867 }
868 if (screenAnimation) {
869 mShownAlpha *=
870 mService.mAnimator.mScreenRotationAnimation.getEnterTransformation().getAlpha();
871 }
872 } else {
873 //Slog.i(TAG, "Not applying alpha transform");
874 }
875
876 if (WindowManagerService.localLOGV) Slog.v(
877 TAG, "computeShownFrameLocked: Animating " + this +
878 ": " + mWin.mShownFrame +
879 ", alpha=" + mTransformation.getAlpha() + ", mShownAlpha=" + mShownAlpha);
880 return;
881 }
882
883 if (WindowManagerService.localLOGV) Slog.v(
884 TAG, "computeShownFrameLocked: " + this +
885 " not attached, mAlpha=" + mAlpha);
886 mWin.mShownFrame.set(mWin.mFrame);
887 if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
888 mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
889 }
890 mShownAlpha = mAlpha;
891 mHaveMatrix = false;
892 mDsDx = mWin.mGlobalScale;
893 mDtDx = 0;
894 mDsDy = 0;
895 mDtDy = mWin.mGlobalScale;
896 }
Craig Mautnerd87946b2012-03-29 18:00:19 -0700897
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700898 void setSurfaceBoundaries(final boolean recoveringMemory) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700899 final WindowState w = mWin;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700900 int width, height;
901 if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
902 // for a scaled surface, we just want to use
903 // the requested size.
904 width = w.mRequestedWidth;
905 height = w.mRequestedHeight;
906 } else {
907 width = w.mCompatFrame.width();
908 height = w.mCompatFrame.height();
909 }
910
911 if (width < 1) {
912 width = 1;
913 }
914 if (height < 1) {
915 height = 1;
916 }
917 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
918 if (surfaceResized) {
919 mSurfaceW = width;
920 mSurfaceH = height;
921 }
922
923 if (mSurfaceX != w.mShownFrame.left
924 || mSurfaceY != w.mShownFrame.top) {
925 try {
926 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
927 "POS " + w.mShownFrame.left
928 + ", " + w.mShownFrame.top, null);
929 mSurfaceX = w.mShownFrame.left;
930 mSurfaceY = w.mShownFrame.top;
931 mSurface.setPosition(w.mShownFrame.left, w.mShownFrame.top);
932 } catch (RuntimeException e) {
933 Slog.w(TAG, "Error positioning surface of " + w
934 + " pos=(" + w.mShownFrame.left
935 + "," + w.mShownFrame.top + ")", e);
936 if (!recoveringMemory) {
937 mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
938 }
939 }
940 }
941
942 if (surfaceResized) {
943 try {
944 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
945 "SIZE " + width + "x" + height, null);
946 mSurfaceResized = true;
947 mSurface.setSize(width, height);
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700948 mAnimator.mPendingLayoutChanges |=
949 WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700950 } catch (RuntimeException e) {
951 // If something goes wrong with the surface (such
952 // as running out of memory), don't take down the
953 // entire system.
954 Slog.e(TAG, "Error resizing surface of " + w
955 + " size=(" + width + "x" + height + ")", e);
956 if (!recoveringMemory) {
957 mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
958 }
959 }
960 }
Craig Mautneracaf9cc2012-04-17 11:45:25 -0700961 }
962
963 public void prepareSurfaceLocked(final boolean recoveringMemory) {
964 final WindowState w = mWin;
965 if (mSurface == null) {
966 if (w.mOrientationChanging) {
967 if (DEBUG_ORIENTATION) {
968 Slog.v(TAG, "Orientation change skips hidden " + w);
969 }
970 w.mOrientationChanging = false;
971 }
972 return;
973 }
974
975 boolean displayed = false;
976
977 computeShownFrameLocked();
978
979 setSurfaceBoundaries(recoveringMemory);
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700980
981 if (w.mAttachedHidden || !w.isReadyForDisplay()) {
Craig Mautner749a7bb2012-04-02 13:49:53 -0700982 if (!mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700983 //dump();
Craig Mautner749a7bb2012-04-02 13:49:53 -0700984 mLastHidden = true;
Craig Mautnerc2f9be02012-03-27 17:32:29 -0700985 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
986 "HIDE (performLayout)", null);
987 if (mSurface != null) {
988 mSurfaceShown = false;
989 try {
990 mSurface.hide();
991 } catch (RuntimeException e) {
992 Slog.w(TAG, "Exception hiding surface in " + w);
993 }
994 }
995 }
996 // If we are waiting for this window to handle an
997 // orientation change, well, it is hidden, so
998 // doesn't really matter. Note that this does
999 // introduce a potential glitch if the window
1000 // becomes unhidden before it has drawn for the
1001 // new orientation.
1002 if (w.mOrientationChanging) {
1003 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001004 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001005 "Orientation change skips hidden " + w);
1006 }
1007 } else if (mLastLayer != mAnimLayer
1008 || mLastAlpha != mShownAlpha
1009 || mLastDsDx != mDsDx
1010 || mLastDtDx != mDtDx
1011 || mLastDsDy != mDsDy
1012 || mLastDtDy != mDtDy
1013 || w.mLastHScale != w.mHScale
1014 || w.mLastVScale != w.mVScale
Craig Mautner749a7bb2012-04-02 13:49:53 -07001015 || mLastHidden) {
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001016 displayed = true;
1017 mLastAlpha = mShownAlpha;
1018 mLastLayer = mAnimLayer;
1019 mLastDsDx = mDsDx;
1020 mLastDtDx = mDtDx;
1021 mLastDsDy = mDsDy;
1022 mLastDtDy = mDtDy;
1023 w.mLastHScale = w.mHScale;
1024 w.mLastVScale = w.mVScale;
1025 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1026 "alpha=" + mShownAlpha + " layer=" + mAnimLayer
1027 + " matrix=[" + (mDsDx*w.mHScale)
1028 + "," + (mDtDx*w.mVScale)
1029 + "][" + (mDsDy*w.mHScale)
1030 + "," + (mDtDy*w.mVScale) + "]", null);
1031 if (mSurface != null) {
1032 try {
1033 mSurfaceAlpha = mShownAlpha;
1034 mSurface.setAlpha(mShownAlpha);
1035 mSurfaceLayer = w.mWinAnimator.mAnimLayer;
1036 mSurface.setLayer(w.mWinAnimator.mAnimLayer);
1037 mSurface.setMatrix(
1038 mDsDx*w.mHScale, mDtDx*w.mVScale,
1039 mDsDy*w.mHScale, mDtDy*w.mVScale);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001040
1041 if (mLastHidden && mDrawState == HAS_DRAWN) {
1042 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1043 "SHOW (performLayout)", null);
1044 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
1045 + " during relayout");
1046 if (showSurfaceRobustlyLocked()) {
1047 mLastHidden = false;
1048 } else {
1049 w.mOrientationChanging = false;
1050 }
1051 }
1052 if (mSurface != null) {
1053 w.mToken.hasVisible = true;
1054 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001055 } catch (RuntimeException e) {
1056 Slog.w(TAG, "Error updating surface in " + w, e);
1057 if (!recoveringMemory) {
1058 mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
1059 }
1060 }
1061 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001062 } else {
1063 displayed = true;
1064 }
1065
1066 if (displayed) {
1067 if (w.mOrientationChanging) {
1068 if (!w.isDrawnLw()) {
Craig Mautnerd09cc4b2012-04-04 10:23:31 -07001069 mAnimator.mBulkUpdateParams |= CLEAR_ORIENTATION_CHANGE_COMPLETE;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001070 if (DEBUG_ORIENTATION) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001071 "Orientation continue waiting for draw in " + w);
1072 } else {
1073 w.mOrientationChanging = false;
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001074 if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001075 }
1076 }
1077 w.mToken.hasVisible = true;
1078 }
1079 }
1080
Craig Mautner48ba1e72012-04-02 13:18:16 -07001081 void setTransparentRegionHint(final Region region) {
Craig Mautner1f4e0cc2012-04-10 14:24:38 -07001082 if (mSurface == null) {
1083 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1084 return;
1085 }
Craig Mautner48ba1e72012-04-02 13:18:16 -07001086 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1087 ">>> OPEN TRANSACTION setTransparentRegion");
1088 Surface.openTransaction();
1089 try {
1090 if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1091 "transparentRegionHint=" + region, null);
1092 mSurface.setTransparentRegionHint(region);
1093 } finally {
1094 Surface.closeTransaction();
1095 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1096 "<<< CLOSE TRANSACTION setTransparentRegion");
1097 }
1098 }
1099
1100 void setWallpaperOffset(int left, int top) {
1101 Surface.openTransaction();
1102 try {
1103 mSurfaceX = left;
1104 mSurfaceY = top;
1105 mSurface.setPosition(left, top);
1106 } catch (RuntimeException e) {
1107 Slog.w(TAG, "Error positioning surface of " + mWin
1108 + " pos=(" + left + "," + top + ")", e);
1109 }
1110 Surface.closeTransaction();
1111 }
1112
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001113 // This must be called while inside a transaction.
1114 boolean performShowLocked() {
1115 if (DEBUG_VISIBILITY) {
1116 RuntimeException e = null;
1117 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1118 e = new RuntimeException();
1119 e.fillInStackTrace();
1120 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001121 Slog.v(TAG, "performShow on " + this
Craig Mautner749a7bb2012-04-02 13:49:53 -07001122 + ": mDrawState=" + mDrawState + " readyForDisplay="
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001123 + mWin.isReadyForDisplay()
1124 + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING), e);
1125 }
Craig Mautner749a7bb2012-04-02 13:49:53 -07001126 if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplay()) {
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001127 if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001128 WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
Craig Mautnerd87946b2012-03-29 18:00:19 -07001129 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001130 + " during animation: policyVis=" + mWin.mPolicyVisibility
1131 + " attHidden=" + mWin.mAttachedHidden
1132 + " tok.hiddenRequested="
1133 + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1134 + " tok.hidden="
1135 + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1136 + " animating=" + mAnimating
1137 + " tok animating="
Craig Mautner59431632012-04-04 11:56:44 -07001138 + (mWin.mAppToken != null ? mWin.mAppToken.mAppAnimator.animating : false));
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001139 if (!showSurfaceRobustlyLocked()) {
1140 return false;
1141 }
1142
1143 mService.enableScreenIfNeededLocked();
1144
1145 applyEnterAnimationLocked();
1146
1147 mLastAlpha = -1;
Craig Mautner749a7bb2012-04-02 13:49:53 -07001148 mLastHidden = false;
1149 mDrawState = HAS_DRAWN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001150
1151 int i = mWin.mChildWindows.size();
1152 while (i > 0) {
1153 i--;
1154 WindowState c = mWin.mChildWindows.get(i);
1155 if (c.mAttachedHidden) {
1156 c.mAttachedHidden = false;
1157 if (c.mWinAnimator.mSurface != null) {
1158 c.mWinAnimator.performShowLocked();
1159 // It hadn't been shown, which means layout not
1160 // performed on it, so now we want to make sure to
1161 // do a layout. If called from within the transaction
1162 // loop, this will cause it to restart with a new
1163 // layout.
1164 mService.mLayoutNeeded = true;
1165 }
1166 }
1167 }
1168
1169 if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1170 && mWin.mAppToken != null) {
1171 mWin.mAppToken.firstWindowDrawn = true;
1172
1173 if (mWin.mAppToken.startingData != null) {
1174 if (WindowManagerService.DEBUG_STARTING_WINDOW ||
Craig Mautnerd87946b2012-03-29 18:00:19 -07001175 WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001176 "Finish starting " + mWin.mToken
1177 + ": first real window is shown, no animation");
1178 // If this initial window is animating, stop it -- we
1179 // will do an animation to reveal it from behind the
1180 // starting window, so there is no need for it to also
1181 // be doing its own stuff.
1182 if (mAnimation != null) {
1183 mAnimation.cancel();
1184 mAnimation = null;
1185 // Make sure we clean up the animation.
1186 mAnimating = true;
1187 }
1188 mService.mFinishedStarting.add(mWin.mAppToken);
1189 mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1190 }
1191 mWin.mAppToken.updateReportedVisibilityLocked();
1192 }
1193
1194 return true;
1195 }
1196
1197 return false;
1198 }
1199
1200 /**
1201 * Have the surface flinger show a surface, robustly dealing with
1202 * error conditions. In particular, if there is not enough memory
1203 * to show the surface, then we will try to get rid of other surfaces
1204 * in order to succeed.
1205 *
1206 * @return Returns true if the surface was successfully shown.
1207 */
1208 boolean showSurfaceRobustlyLocked() {
1209 try {
1210 if (mSurface != null) {
1211 mSurfaceShown = true;
1212 mSurface.show();
1213 if (mWin.mTurnOnScreen) {
1214 if (DEBUG_VISIBILITY) Slog.v(TAG,
1215 "Show surface turning screen on: " + mWin);
1216 mWin.mTurnOnScreen = false;
Craig Mautner7d8df392012-04-06 15:26:23 -07001217 mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001218 }
1219 }
1220 return true;
1221 } catch (RuntimeException e) {
1222 Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
1223 }
1224
1225 mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1226
1227 return false;
1228 }
1229
1230 void applyEnterAnimationLocked() {
1231 final int transit;
1232 if (mEnterAnimationPending) {
1233 mEnterAnimationPending = false;
1234 transit = WindowManagerPolicy.TRANSIT_ENTER;
1235 } else {
1236 transit = WindowManagerPolicy.TRANSIT_SHOW;
1237 }
1238
1239 applyAnimationLocked(transit, true);
1240 }
1241
Craig Mautner48ba1e72012-04-02 13:18:16 -07001242 // TODO(cmautner): Move back to WindowState?
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001243 /**
1244 * Choose the correct animation and set it to the passed WindowState.
1245 * @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
1246 * then the animation will be app_starting_exit. Any other value loads the animation from
1247 * the switch statement below.
1248 * @param isEntrance The animation type the last time this was called. Used to keep from
1249 * loading the same animation twice.
1250 * @return true if an animation has been loaded.
1251 */
1252 boolean applyAnimationLocked(int transit, boolean isEntrance) {
1253 if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1254 // If we are trying to apply an animation, but already running
1255 // an animation of the same type, then just leave that one alone.
1256 return true;
1257 }
1258
1259 // Only apply an animation if the display isn't frozen. If it is
1260 // frozen, there is no reason to animate and it can cause strange
1261 // artifacts when we unfreeze the display if some different animation
1262 // is running.
1263 if (mService.okToDisplay()) {
1264 int anim = mPolicy.selectAnimationLw(mWin, transit);
1265 int attr = -1;
1266 Animation a = null;
1267 if (anim != 0) {
1268 a = AnimationUtils.loadAnimation(mContext, anim);
1269 } else {
1270 switch (transit) {
1271 case WindowManagerPolicy.TRANSIT_ENTER:
1272 attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1273 break;
1274 case WindowManagerPolicy.TRANSIT_EXIT:
1275 attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1276 break;
1277 case WindowManagerPolicy.TRANSIT_SHOW:
1278 attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1279 break;
1280 case WindowManagerPolicy.TRANSIT_HIDE:
1281 attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1282 break;
1283 }
1284 if (attr >= 0) {
1285 a = mService.loadAnimation(mWin.mAttrs, attr);
1286 }
1287 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001288 if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001289 "applyAnimation: win=" + this
1290 + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1291 + " mAnimation=" + mAnimation
1292 + " isEntrance=" + isEntrance);
1293 if (a != null) {
1294 if (WindowManagerService.DEBUG_ANIM) {
1295 RuntimeException e = null;
1296 if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1297 e = new RuntimeException();
1298 e.fillInStackTrace();
1299 }
Craig Mautnerd87946b2012-03-29 18:00:19 -07001300 Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001301 }
1302 setAnimation(a);
1303 mAnimationIsEntrance = isEntrance;
1304 }
1305 } else {
1306 clearAnimation();
1307 }
1308
1309 return mAnimation != null;
1310 }
1311
Craig Mautnera2c77052012-03-26 12:14:43 -07001312 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1313 if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1314 || mAnimation != null) {
1315 pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1316 pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1317 pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1318 pw.print(" mAnimation="); pw.println(mAnimation);
1319 }
1320 if (mHasTransformation || mHasLocalTransformation) {
1321 pw.print(prefix); pw.print("XForm: has=");
1322 pw.print(mHasTransformation);
1323 pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1324 pw.print(" "); mTransformation.printShortString(pw);
1325 pw.println();
1326 }
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001327 if (mSurface != null) {
1328 if (dumpAll) {
1329 pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
Craig Mautner749a7bb2012-04-02 13:49:53 -07001330 pw.print(prefix); pw.print("mDrawState="); pw.print(mDrawState);
1331 pw.print(" mLastHidden="); pw.println(mLastHidden);
Craig Mautnerc2f9be02012-03-27 17:32:29 -07001332 }
1333 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1334 pw.print(" layer="); pw.print(mSurfaceLayer);
1335 pw.print(" alpha="); pw.print(mSurfaceAlpha);
1336 pw.print(" rect=("); pw.print(mSurfaceX);
1337 pw.print(","); pw.print(mSurfaceY);
1338 pw.print(") "); pw.print(mSurfaceW);
1339 pw.print(" x "); pw.println(mSurfaceH);
1340 }
1341 if (mPendingDestroySurface != null) {
1342 pw.print(prefix); pw.print("mPendingDestroySurface=");
1343 pw.println(mPendingDestroySurface);
1344 }
1345 if (mSurfaceResized || mSurfaceDestroyDeferred) {
1346 pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1347 pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1348 }
1349 if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1350 pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1351 pw.print(" mAlpha="); pw.print(mAlpha);
1352 pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1353 }
1354 if (mHaveMatrix || mWin.mGlobalScale != 1) {
1355 pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1356 pw.print(" mDsDx="); pw.print(mDsDx);
1357 pw.print(" mDtDx="); pw.print(mDtDx);
1358 pw.print(" mDsDy="); pw.print(mDsDy);
1359 pw.print(" mDtDy="); pw.println(mDtDy);
1360 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001361 }
1362
Craig Mautnerc8bc97e2012-04-02 12:54:54 -07001363 @Override
1364 public String toString() {
1365 StringBuffer sb = new StringBuffer("WindowStateAnimator (");
1366 sb.append(mWin.mLastTitle + "): ");
1367 sb.append("mSurface " + mSurface);
1368 sb.append(", mAnimation " + mAnimation);
1369 return sb.toString();
1370 }
Craig Mautnera2c77052012-03-26 12:14:43 -07001371}