WindowStateAnimator.java revision c2f20b66fd28c10e2ec8654bd74cb501eb7f837b
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
19import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
20import static com.android.server.wm.WindowManagerService.DEBUG_ANIM;
21import static com.android.server.wm.WindowManagerService.DEBUG_LAYERS;
22import static com.android.server.wm.WindowManagerService.DEBUG_ORIENTATION;
23import static com.android.server.wm.WindowManagerService.DEBUG_STARTING_WINDOW;
24import static com.android.server.wm.WindowManagerService.DEBUG_SURFACE_TRACE;
25import static com.android.server.wm.WindowManagerService.SHOW_TRANSACTIONS;
26import static com.android.server.wm.WindowManagerService.DEBUG_VISIBILITY;
27import static com.android.server.wm.WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
28import static com.android.server.wm.WindowManagerService.SHOW_SURFACE_ALLOC;
29import static com.android.server.wm.WindowManagerService.localLOGV;
30import static com.android.server.wm.WindowManagerService.LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE;
31import static com.android.server.wm.WindowManagerService.LayoutFields.SET_TURN_ON_SCREEN;
32
33import android.content.Context;
34import android.graphics.Matrix;
35import android.graphics.PixelFormat;
36import android.graphics.Point;
37import android.graphics.PointF;
38import android.graphics.Rect;
39import android.graphics.RectF;
40import android.graphics.Region;
41import android.os.Debug;
42import android.os.UserHandle;
43import android.util.Slog;
44import android.view.Display;
45import android.view.DisplayInfo;
46import android.view.MagnificationSpec;
47import android.view.Surface.OutOfResourcesException;
48import android.view.SurfaceControl;
49import android.view.SurfaceSession;
50import android.view.View;
51import android.view.WindowManager;
52import android.view.WindowManagerPolicy;
53import android.view.WindowManager.LayoutParams;
54import android.view.animation.Animation;
55import android.view.animation.AnimationUtils;
56import android.view.animation.Transformation;
57
58import com.android.server.wm.WindowManagerService.H;
59
60import java.io.PrintWriter;
61import java.util.ArrayList;
62
63class WinAnimatorList extends ArrayList<WindowStateAnimator> {
64}
65
66/**
67 * Keep track of animations and surface operations for a single WindowState.
68 **/
69class WindowStateAnimator {
70    static final String TAG = "WindowStateAnimator";
71
72    // Unchanging local convenience fields.
73    final WindowManagerService mService;
74    final WindowState mWin;
75    final WindowStateAnimator mAttachedWinAnimator;
76    final WindowAnimator mAnimator;
77    AppWindowAnimator mAppAnimator;
78    final Session mSession;
79    final WindowManagerPolicy mPolicy;
80    final Context mContext;
81    final boolean mIsWallpaper;
82
83    // If this is a universe background window, this is the transformation
84    // it is applying to the rest of the universe.
85    final Transformation mUniverseTransform = new Transformation();
86
87    // Currently running animation.
88    boolean mAnimating;
89    boolean mLocalAnimating;
90    Animation mAnimation;
91    boolean mAnimationIsEntrance;
92    boolean mHasTransformation;
93    boolean mHasLocalTransformation;
94    final Transformation mTransformation = new Transformation();
95    boolean mWasAnimating;      // Were we animating going into the most recent animation step?
96    int mAnimLayer;
97    int mLastLayer;
98
99    SurfaceControl mSurfaceControl;
100    SurfaceControl mPendingDestroySurface;
101
102    /**
103     * Set when we have changed the size of the surface, to know that
104     * we must tell them application to resize (and thus redraw itself).
105     */
106    boolean mSurfaceResized;
107
108    /**
109     * Set if the client has asked that the destroy of its surface be delayed
110     * until it explicitly says it is okay.
111     */
112    boolean mSurfaceDestroyDeferred;
113
114    float mShownAlpha = 0;
115    float mAlpha = 0;
116    float mLastAlpha = 0;
117
118    boolean mHasClipRect;
119    Rect mClipRect = new Rect();
120    Rect mTmpClipRect = new Rect();
121    Rect mLastClipRect = new Rect();
122
123    // Used to save animation distances between the time they are calculated and when they are
124    // used.
125    int mAnimDw;
126    int mAnimDh;
127    float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
128    float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
129
130    boolean mHaveMatrix;
131
132    // For debugging, this is the last information given to the surface flinger.
133    boolean mSurfaceShown;
134    float mSurfaceX, mSurfaceY;
135    float mSurfaceW, mSurfaceH;
136    int mSurfaceLayer;
137    float mSurfaceAlpha;
138
139    // Set to true if, when the window gets displayed, it should perform
140    // an enter animation.
141    boolean mEnterAnimationPending;
142
143    /** This is set when there is no Surface */
144    static final int NO_SURFACE = 0;
145    /** This is set after the Surface has been created but before the window has been drawn. During
146     * this time the surface is hidden. */
147    static final int DRAW_PENDING = 1;
148    /** This is set after the window has finished drawing for the first time but before its surface
149     * is shown.  The surface will be displayed when the next layout is run. */
150    static final int COMMIT_DRAW_PENDING = 2;
151    /** This is set during the time after the window's drawing has been committed, and before its
152     * surface is actually shown.  It is used to delay showing the surface until all windows in a
153     * token are ready to be shown. */
154    static final int READY_TO_SHOW = 3;
155    /** Set when the window has been shown in the screen the first time. */
156    static final int HAS_DRAWN = 4;
157
158    private static final int SYSTEM_UI_FLAGS_LAYOUT_STABLE_FULLSCREEN =
159            View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
160
161    static String drawStateToString(int state) {
162        switch (state) {
163            case NO_SURFACE: return "NO_SURFACE";
164            case DRAW_PENDING: return "DRAW_PENDING";
165            case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING";
166            case READY_TO_SHOW: return "READY_TO_SHOW";
167            case HAS_DRAWN: return "HAS_DRAWN";
168            default: return Integer.toString(state);
169        }
170    }
171    int mDrawState;
172
173    /** Was this window last hidden? */
174    boolean mLastHidden;
175
176    int mAttrType;
177
178    public WindowStateAnimator(final WindowState win) {
179        final WindowManagerService service = win.mService;
180
181        mService = service;
182        mAnimator = service.mAnimator;
183        mPolicy = service.mPolicy;
184        mContext = service.mContext;
185        final DisplayContent displayContent = win.getDisplayContent();
186        if (displayContent != null) {
187            final DisplayInfo displayInfo = displayContent.getDisplayInfo();
188            mAnimDw = displayInfo.appWidth;
189            mAnimDh = displayInfo.appHeight;
190        } else {
191            Slog.w(TAG, "WindowStateAnimator ctor: Display has been removed");
192            // This is checked on return and dealt with.
193        }
194
195        mWin = win;
196        mAttachedWinAnimator = win.mAttachedWindow == null
197                ? null : win.mAttachedWindow.mWinAnimator;
198        mAppAnimator = win.mAppToken == null ? null : win.mAppToken.mAppAnimator;
199        mSession = win.mSession;
200        mAttrType = win.mAttrs.type;
201        mIsWallpaper = win.mIsWallpaper;
202    }
203
204    public void setAnimation(Animation anim) {
205        if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
206        mAnimating = false;
207        mLocalAnimating = false;
208        mAnimation = anim;
209        mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
210        mAnimation.scaleCurrentDuration(mService.getWindowAnimationScaleLocked());
211        // Start out animation gone if window is gone, or visible if window is visible.
212        mTransformation.clear();
213        mTransformation.setAlpha(mLastHidden ? 0 : 1);
214        mHasLocalTransformation = true;
215    }
216
217    public void clearAnimation() {
218        if (mAnimation != null) {
219            mAnimating = true;
220            mLocalAnimating = false;
221            mAnimation.cancel();
222            mAnimation = null;
223        }
224    }
225
226    /** Is the window or its container currently animating? */
227    boolean isAnimating() {
228        return mAnimation != null
229                || (mAttachedWinAnimator != null && mAttachedWinAnimator.mAnimation != null)
230                || (mAppAnimator != null &&
231                        (mAppAnimator.animation != null
232                                || mAppAnimator.mAppToken.inPendingTransaction));
233    }
234
235    /** Is the window animating the DummyAnimation? */
236    boolean isDummyAnimation() {
237        return mAppAnimator != null
238                && mAppAnimator.animation == AppWindowAnimator.sDummyAnimation;
239    }
240
241    /** Is this window currently animating? */
242    boolean isWindowAnimating() {
243        return mAnimation != null;
244    }
245
246    void cancelExitAnimationForNextAnimationLocked() {
247        if (mAnimation != null) {
248            mAnimation.cancel();
249            mAnimation = null;
250            mLocalAnimating = false;
251            destroySurfaceLocked();
252        }
253    }
254
255    private boolean stepAnimation(long currentTime) {
256        if ((mAnimation == null) || !mLocalAnimating) {
257            return false;
258        }
259        mTransformation.clear();
260        final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
261        if (false && DEBUG_ANIM) Slog.v(
262            TAG, "Stepped animation in " + this +
263            ": more=" + more + ", xform=" + mTransformation);
264        return more;
265    }
266
267    // This must be called while inside a transaction.  Returns true if
268    // there is more animation to run.
269    boolean stepAnimationLocked(long currentTime) {
270        // Save the animation state as it was before this step so WindowManagerService can tell if
271        // we just started or just stopped animating by comparing mWasAnimating with isAnimating().
272        mWasAnimating = mAnimating;
273        final DisplayContent displayContent = mWin.getDisplayContent();
274        if (displayContent != null && mService.okToDisplay()) {
275            // We will run animations as long as the display isn't frozen.
276
277            if (mWin.isDrawnLw() && mAnimation != null) {
278                mHasTransformation = true;
279                mHasLocalTransformation = true;
280                if (!mLocalAnimating) {
281                    if (DEBUG_ANIM) Slog.v(
282                        TAG, "Starting animation in " + this +
283                        " @ " + currentTime + ": ww=" + mWin.mFrame.width() +
284                        " wh=" + mWin.mFrame.height() +
285                        " dw=" + mAnimDw + " dh=" + mAnimDh +
286                        " scale=" + mService.getWindowAnimationScaleLocked());
287                    mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
288                            mAnimDw, mAnimDh);
289                    final DisplayInfo displayInfo = displayContent.getDisplayInfo();
290                    mAnimDw = displayInfo.appWidth;
291                    mAnimDh = displayInfo.appHeight;
292                    mAnimation.setStartTime(currentTime);
293                    mLocalAnimating = true;
294                    mAnimating = true;
295                }
296                if ((mAnimation != null) && mLocalAnimating) {
297                    if (stepAnimation(currentTime)) {
298                        return true;
299                    }
300                }
301                if (DEBUG_ANIM) Slog.v(
302                    TAG, "Finished animation in " + this +
303                    " @ " + currentTime);
304                //WindowManagerService.this.dump();
305            }
306            mHasLocalTransformation = false;
307            if ((!mLocalAnimating || mAnimationIsEntrance) && mAppAnimator != null
308                    && mAppAnimator.animation != null) {
309                // When our app token is animating, we kind-of pretend like
310                // we are as well.  Note the mLocalAnimating mAnimationIsEntrance
311                // part of this check means that we will only do this if
312                // our window is not currently exiting, or it is not
313                // locally animating itself.  The idea being that one that
314                // is exiting and doing a local animation should be removed
315                // once that animation is done.
316                mAnimating = true;
317                mHasTransformation = true;
318                mTransformation.clear();
319                return false;
320            } else if (mHasTransformation) {
321                // Little trick to get through the path below to act like
322                // we have finished an animation.
323                mAnimating = true;
324            } else if (isAnimating()) {
325                mAnimating = true;
326            }
327        } else if (mAnimation != null) {
328            // If the display is frozen, and there is a pending animation,
329            // clear it and make sure we run the cleanup code.
330            mAnimating = true;
331        }
332
333        if (!mAnimating && !mLocalAnimating) {
334            return false;
335        }
336
337        // Done animating, clean up.
338        if (DEBUG_ANIM) Slog.v(
339            TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
340            + ", reportedVisible="
341            + (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
342
343        mAnimating = false;
344        mLocalAnimating = false;
345        if (mAnimation != null) {
346            mAnimation.cancel();
347            mAnimation = null;
348        }
349        if (mAnimator.mWindowDetachedWallpaper == mWin) {
350            mAnimator.mWindowDetachedWallpaper = null;
351        }
352        mAnimLayer = mWin.mLayer;
353        if (mWin.mIsImWindow) {
354            mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
355        } else if (mIsWallpaper) {
356            mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
357        }
358        if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
359                + " anim layer: " + mAnimLayer);
360        mHasTransformation = false;
361        mHasLocalTransformation = false;
362        if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
363            if (DEBUG_VISIBILITY) {
364                Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
365                        + mWin.mPolicyVisibilityAfterAnim);
366            }
367            mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
368            if (displayContent != null) {
369                displayContent.layoutNeeded = true;
370            }
371            if (!mWin.mPolicyVisibility) {
372                if (mService.mCurrentFocus == mWin) {
373                    if (WindowManagerService.DEBUG_FOCUS_LIGHT) Slog.i(TAG,
374                            "setAnimationLocked: setting mFocusMayChange true");
375                    mService.mFocusMayChange = true;
376                }
377                // Window is no longer visible -- make sure if we were waiting
378                // for it to be displayed before enabling the display, that
379                // we allow the display to be enabled now.
380                mService.enableScreenIfNeededLocked();
381            }
382        }
383        mTransformation.clear();
384        if (mDrawState == HAS_DRAWN
385                && mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
386                && mWin.mAppToken != null
387                && mWin.mAppToken.firstWindowDrawn
388                && mWin.mAppToken.startingData != null) {
389            if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
390                    + mWin.mToken + ": first real window done animating");
391            mService.mFinishedStarting.add(mWin.mAppToken);
392            mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
393        } else if (mAttrType == LayoutParams.TYPE_STATUS_BAR && mWin.mPolicyVisibility) {
394            // Upon completion of a not-visible to visible status bar animation a relayout is
395            // required.
396            if (displayContent != null) {
397                displayContent.layoutNeeded = true;
398            }
399        }
400
401        finishExit();
402        final int displayId = mWin.getDisplayId();
403        mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
404        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
405                "WindowStateAnimator", mAnimator.getPendingLayoutChanges(displayId));
406
407        if (mWin.mAppToken != null) {
408            mWin.mAppToken.updateReportedVisibilityLocked();
409        }
410
411        return false;
412    }
413
414    void finishExit() {
415        if (WindowManagerService.DEBUG_ANIM) Slog.v(
416                TAG, "finishExit in " + this
417                + ": exiting=" + mWin.mExiting
418                + " remove=" + mWin.mRemoveOnExit
419                + " windowAnimating=" + isWindowAnimating());
420
421        final int N = mWin.mChildWindows.size();
422        for (int i=0; i<N; i++) {
423            mWin.mChildWindows.get(i).mWinAnimator.finishExit();
424        }
425
426        if (!mWin.mExiting) {
427            return;
428        }
429
430        if (isWindowAnimating()) {
431            return;
432        }
433
434        if (WindowManagerService.localLOGV) Slog.v(
435                TAG, "Exit animation finished in " + this
436                + ": remove=" + mWin.mRemoveOnExit);
437        if (mSurfaceControl != null) {
438            mService.mDestroySurface.add(mWin);
439            mWin.mDestroying = true;
440            if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(
441                mWin, "HIDE (finishExit)", null);
442            hide();
443        }
444        mWin.mExiting = false;
445        if (mWin.mRemoveOnExit) {
446            mService.mPendingRemove.add(mWin);
447            mWin.mRemoveOnExit = false;
448        }
449        mAnimator.hideWallpapersLocked(mWin);
450    }
451
452    void hide() {
453        if (!mLastHidden) {
454            //dump();
455            mLastHidden = true;
456            if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
457                    "HIDE (performLayout)", null);
458            if (mSurfaceControl != null) {
459                mSurfaceShown = false;
460                try {
461                    mSurfaceControl.hide();
462                } catch (RuntimeException e) {
463                    Slog.w(TAG, "Exception hiding surface in " + mWin);
464                }
465            }
466        }
467    }
468
469    boolean finishDrawingLocked() {
470        if (DEBUG_STARTING_WINDOW &&
471                mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
472            Slog.v(TAG, "Finishing drawing window " + mWin + ": mDrawState="
473                    + drawStateToString(mDrawState));
474        }
475        if (mDrawState == DRAW_PENDING) {
476            if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
477                Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + this + " in "
478                        + mSurfaceControl);
479            if (DEBUG_STARTING_WINDOW &&
480                    mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
481                Slog.v(TAG, "Draw state now committed in " + mWin);
482            }
483            mDrawState = COMMIT_DRAW_PENDING;
484            return true;
485        }
486        return false;
487    }
488
489    // This must be called while inside a transaction.
490    boolean commitFinishDrawingLocked(long currentTime) {
491        if (DEBUG_STARTING_WINDOW &&
492                mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
493            Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState="
494                    + drawStateToString(mDrawState));
495        }
496        if (mDrawState != COMMIT_DRAW_PENDING) {
497            return false;
498        }
499        if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) {
500            Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurfaceControl);
501        }
502        mDrawState = READY_TO_SHOW;
503        final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
504        final AppWindowToken atoken = mWin.mAppToken;
505        if (atoken == null || atoken.allDrawn || starting) {
506            performShowLocked();
507        }
508        return true;
509    }
510
511    static class SurfaceTrace extends SurfaceControl {
512        private final static String SURFACE_TAG = "SurfaceTrace";
513        final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
514
515        private float mSurfaceTraceAlpha = 0;
516        private int mLayer;
517        private final PointF mPosition = new PointF();
518        private final Point mSize = new Point();
519        private final Rect mWindowCrop = new Rect();
520        private boolean mShown = false;
521        private int mLayerStack;
522        private boolean mIsOpaque;
523        private final String mName;
524
525        public SurfaceTrace(SurfaceSession s,
526                       String name, int w, int h, int format, int flags)
527                   throws OutOfResourcesException {
528            super(s, name, w, h, format, flags);
529            mName = name != null ? name : "Not named";
530            mSize.set(w, h);
531            Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
532                    + Debug.getCallers(3));
533        }
534
535        @Override
536        public void setAlpha(float alpha) {
537            if (mSurfaceTraceAlpha != alpha) {
538                Slog.v(SURFACE_TAG, "setAlpha(" + alpha + "): OLD:" + this + ". Called by "
539                        + Debug.getCallers(3));
540                mSurfaceTraceAlpha = alpha;
541            }
542            super.setAlpha(alpha);
543        }
544
545        @Override
546        public void setLayer(int zorder) {
547            if (zorder != mLayer) {
548                Slog.v(SURFACE_TAG, "setLayer(" + zorder + "): OLD:" + this + ". Called by "
549                        + Debug.getCallers(3));
550                mLayer = zorder;
551            }
552            super.setLayer(zorder);
553
554            sSurfaces.remove(this);
555            int i;
556            for (i = sSurfaces.size() - 1; i >= 0; i--) {
557                SurfaceTrace s = sSurfaces.get(i);
558                if (s.mLayer < zorder) {
559                    break;
560                }
561            }
562            sSurfaces.add(i + 1, this);
563        }
564
565        @Override
566        public void setPosition(float x, float y) {
567            if (x != mPosition.x || y != mPosition.y) {
568                Slog.v(SURFACE_TAG, "setPosition(" + x + "," + y + "): OLD:" + this
569                        + ". Called by " + Debug.getCallers(3));
570                mPosition.set(x, y);
571            }
572            super.setPosition(x, y);
573        }
574
575        @Override
576        public void setSize(int w, int h) {
577            if (w != mSize.x || h != mSize.y) {
578                Slog.v(SURFACE_TAG, "setSize(" + w + "," + h + "): OLD:" + this + ". Called by "
579                        + Debug.getCallers(3));
580                mSize.set(w, h);
581            }
582            super.setSize(w, h);
583        }
584
585        @Override
586        public void setWindowCrop(Rect crop) {
587            if (crop != null) {
588                if (!crop.equals(mWindowCrop)) {
589                    Slog.v(SURFACE_TAG, "setWindowCrop(" + crop.toShortString() + "): OLD:" + this
590                            + ". Called by " + Debug.getCallers(3));
591                    mWindowCrop.set(crop);
592                }
593            }
594            super.setWindowCrop(crop);
595        }
596
597        @Override
598        public void setLayerStack(int layerStack) {
599            if (layerStack != mLayerStack) {
600                Slog.v(SURFACE_TAG, "setLayerStack(" + layerStack + "): OLD:" + this
601                        + ". Called by " + Debug.getCallers(3));
602                mLayerStack = layerStack;
603            }
604            super.setLayerStack(layerStack);
605        }
606
607        @Override
608        public void setOpaque(boolean isOpaque) {
609            if (isOpaque != mIsOpaque) {
610                Slog.v(SURFACE_TAG, "setOpaque(" + isOpaque + "): OLD:" + this
611                        + ". Called by " + Debug.getCallers(3));
612                mIsOpaque = isOpaque;
613            }
614            super.setOpaque(isOpaque);
615        }
616
617        @Override
618        public void hide() {
619            if (mShown) {
620                Slog.v(SURFACE_TAG, "hide: OLD:" + this + ". Called by " + Debug.getCallers(3));
621                mShown = false;
622            }
623            super.hide();
624        }
625
626        @Override
627        public void show() {
628            if (!mShown) {
629                Slog.v(SURFACE_TAG, "show: OLD:" + this + ". Called by " + Debug.getCallers(3));
630                mShown = true;
631            }
632            super.show();
633        }
634
635        @Override
636        public void destroy() {
637            super.destroy();
638            Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by " + Debug.getCallers(3));
639            sSurfaces.remove(this);
640        }
641
642        @Override
643        public void release() {
644            super.release();
645            Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
646                    + Debug.getCallers(3));
647            sSurfaces.remove(this);
648        }
649
650        static void dumpAllSurfaces() {
651            final int N = sSurfaces.size();
652            for (int i = 0; i < N; i++) {
653                Slog.i(TAG, "SurfaceDump: " + sSurfaces.get(i));
654            }
655        }
656
657        @Override
658        public String toString() {
659            return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
660                    + mName + " (" + mLayerStack + "): shown=" + mShown + " layer=" + mLayer
661                    + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
662                    + " " + mSize.x + "x" + mSize.y
663                    + " crop=" + mWindowCrop.toShortString()
664                    + " opaque=" + mIsOpaque;
665        }
666    }
667
668    SurfaceControl createSurfaceLocked() {
669        final WindowState w = mWin;
670        if (mSurfaceControl == null) {
671            if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
672                    "createSurface " + this + ": mDrawState=DRAW_PENDING");
673            mDrawState = DRAW_PENDING;
674            if (w.mAppToken != null) {
675                if (w.mAppToken.mAppAnimator.animation == null) {
676                    w.mAppToken.allDrawn = false;
677                    w.mAppToken.deferClearAllDrawn = false;
678                } else {
679                    // Currently animating, persist current state of allDrawn until animation
680                    // is complete.
681                    w.mAppToken.deferClearAllDrawn = true;
682                }
683            }
684
685            mService.makeWindowFreezingScreenIfNeededLocked(w);
686
687            int flags = SurfaceControl.HIDDEN;
688            final WindowManager.LayoutParams attrs = w.mAttrs;
689
690            if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
691                flags |= SurfaceControl.SECURE;
692            }
693
694            if (mService.isScreenCaptureDisabledLocked(UserHandle.getUserId(mWin.mOwnerUid))) {
695                flags |= SurfaceControl.SECURE;
696            }
697
698            int width;
699            int height;
700            if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
701                // for a scaled surface, we always want the requested
702                // size.
703                width = w.mRequestedWidth;
704                height = w.mRequestedHeight;
705            } else {
706                width = w.mCompatFrame.width();
707                height = w.mCompatFrame.height();
708            }
709
710            // Something is wrong and SurfaceFlinger will not like this,
711            // try to revert to sane values
712            if (width <= 0) {
713                width = 1;
714            }
715            if (height <= 0) {
716                height = 1;
717            }
718
719            float left = w.mFrame.left + w.mXOffset;
720            float top = w.mFrame.top + w.mYOffset;
721
722            // Adjust for surface insets.
723            width += attrs.surfaceInsets.left + attrs.surfaceInsets.right;
724            height += attrs.surfaceInsets.top + attrs.surfaceInsets.bottom;
725            left -= attrs.surfaceInsets.left;
726            top -= attrs.surfaceInsets.top;
727
728            if (DEBUG_VISIBILITY) {
729                Slog.v(TAG, "Creating surface in session "
730                        + mSession.mSurfaceSession + " window " + this
731                        + " w=" + width + " h=" + height
732                        + " x=" + left + " y=" + top
733                        + " format=" + attrs.format + " flags=" + flags);
734            }
735
736            // We may abort, so initialize to defaults.
737            mSurfaceShown = false;
738            mSurfaceLayer = 0;
739            mSurfaceAlpha = 0;
740            mSurfaceX = 0;
741            mSurfaceY = 0;
742            w.mLastSystemDecorRect.set(0, 0, 0, 0);
743            mLastClipRect.set(0, 0, 0, 0);
744
745            // Set up surface control with initial size.
746            try {
747                mSurfaceW = width;
748                mSurfaceH = height;
749
750                final boolean isHwAccelerated = (attrs.flags &
751                        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
752                final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
753                if (!PixelFormat.formatHasAlpha(attrs.format)) {
754                    flags |= SurfaceControl.OPAQUE;
755                }
756
757                if (DEBUG_SURFACE_TRACE) {
758                    mSurfaceControl = new SurfaceTrace(
759                            mSession.mSurfaceSession,
760                            attrs.getTitle().toString(),
761                            width, height, format, flags);
762                } else {
763                    mSurfaceControl = new SurfaceControl(
764                        mSession.mSurfaceSession,
765                        attrs.getTitle().toString(),
766                        width, height, format, flags);
767                }
768
769                w.mHasSurface = true;
770
771                if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
772                    Slog.i(TAG, "  CREATE SURFACE "
773                            + mSurfaceControl + " IN SESSION "
774                            + mSession.mSurfaceSession
775                            + ": pid=" + mSession.mPid + " format="
776                            + attrs.format + " flags=0x"
777                            + Integer.toHexString(flags)
778                            + " / " + this);
779                }
780            } catch (OutOfResourcesException e) {
781                w.mHasSurface = false;
782                Slog.w(TAG, "OutOfResourcesException creating surface");
783                mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
784                mDrawState = NO_SURFACE;
785                return null;
786            } catch (Exception e) {
787                w.mHasSurface = false;
788                Slog.e(TAG, "Exception creating surface", e);
789                mDrawState = NO_SURFACE;
790                return null;
791            }
792
793            if (WindowManagerService.localLOGV) {
794                Slog.v(TAG, "Got surface: " + mSurfaceControl
795                        + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top
796                        + ", animLayer=" + mAnimLayer);
797            }
798
799            if (SHOW_LIGHT_TRANSACTIONS) {
800                Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
801                WindowManagerService.logSurface(w, "CREATE pos=("
802                        + w.mFrame.left + "," + w.mFrame.top + ") ("
803                        + w.mCompatFrame.width() + "x" + w.mCompatFrame.height()
804                        + "), layer=" + mAnimLayer + " HIDE", null);
805            }
806
807            // Start a new transaction and apply position & offset.
808            SurfaceControl.openTransaction();
809            try {
810                mSurfaceX = left;
811                mSurfaceY = top;
812
813                try {
814                    mSurfaceControl.setPosition(left, top);
815                    mSurfaceLayer = mAnimLayer;
816                    final DisplayContent displayContent = w.getDisplayContent();
817                    if (displayContent != null) {
818                        mSurfaceControl.setLayerStack(displayContent.getDisplay().getLayerStack());
819                    }
820                    mSurfaceControl.setLayer(mAnimLayer);
821                    mSurfaceControl.setAlpha(0);
822                    mSurfaceShown = false;
823                } catch (RuntimeException e) {
824                    Slog.w(TAG, "Error creating surface in " + w, e);
825                    mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
826                }
827                mLastHidden = true;
828            } finally {
829                SurfaceControl.closeTransaction();
830                if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
831                        "<<< CLOSE TRANSACTION createSurfaceLocked");
832            }
833            if (WindowManagerService.localLOGV) Slog.v(
834                    TAG, "Created surface " + this);
835        }
836        return mSurfaceControl;
837    }
838
839    void destroySurfaceLocked() {
840        if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
841            mWin.mAppToken.startingDisplayed = false;
842        }
843
844        if (mSurfaceControl != null) {
845
846            int i = mWin.mChildWindows.size();
847            while (i > 0) {
848                i--;
849                WindowState c = mWin.mChildWindows.get(i);
850                c.mAttachedHidden = true;
851            }
852
853            try {
854                if (DEBUG_VISIBILITY) {
855                    RuntimeException e = null;
856                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
857                        e = new RuntimeException();
858                        e.fillInStackTrace();
859                    }
860                    Slog.w(TAG, "Window " + this + " destroying surface "
861                            + mSurfaceControl + ", session " + mSession, e);
862                }
863                if (mSurfaceDestroyDeferred) {
864                    if (mSurfaceControl != null && mPendingDestroySurface != mSurfaceControl) {
865                        if (mPendingDestroySurface != null) {
866                            if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
867                                RuntimeException e = null;
868                                if (!WindowManagerService.HIDE_STACK_CRAWLS) {
869                                    e = new RuntimeException();
870                                    e.fillInStackTrace();
871                                }
872                                WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
873                            }
874                            mPendingDestroySurface.destroy();
875                        }
876                        mPendingDestroySurface = mSurfaceControl;
877                    }
878                } else {
879                    if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
880                        RuntimeException e = null;
881                        if (!WindowManagerService.HIDE_STACK_CRAWLS) {
882                            e = new RuntimeException();
883                            e.fillInStackTrace();
884                        }
885                        WindowManagerService.logSurface(mWin, "DESTROY", e);
886                    }
887                    mSurfaceControl.destroy();
888                }
889                mAnimator.hideWallpapersLocked(mWin);
890            } catch (RuntimeException e) {
891                Slog.w(TAG, "Exception thrown when destroying Window " + this
892                    + " surface " + mSurfaceControl + " session " + mSession
893                    + ": " + e.toString());
894            }
895
896            mSurfaceShown = false;
897            mSurfaceControl = null;
898            mWin.mHasSurface = false;
899            mDrawState = NO_SURFACE;
900        }
901    }
902
903    void destroyDeferredSurfaceLocked() {
904        try {
905            if (mPendingDestroySurface != null) {
906                if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
907                    RuntimeException e = null;
908                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
909                        e = new RuntimeException();
910                        e.fillInStackTrace();
911                    }
912                    WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
913                }
914                mPendingDestroySurface.destroy();
915                mAnimator.hideWallpapersLocked(mWin);
916            }
917        } catch (RuntimeException e) {
918            Slog.w(TAG, "Exception thrown when destroying Window "
919                    + this + " surface " + mPendingDestroySurface
920                    + " session " + mSession + ": " + e.toString());
921        }
922        mSurfaceDestroyDeferred = false;
923        mPendingDestroySurface = null;
924    }
925
926    void computeShownFrameLocked() {
927        final boolean selfTransformation = mHasLocalTransformation;
928        Transformation attachedTransformation =
929                (mAttachedWinAnimator != null && mAttachedWinAnimator.mHasLocalTransformation)
930                ? mAttachedWinAnimator.mTransformation : null;
931        Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation)
932                ? mAppAnimator.transformation : null;
933
934        // Wallpapers are animated based on the "real" window they
935        // are currently targeting.
936        final WindowState wallpaperTarget = mService.mWallpaperTarget;
937        if (mIsWallpaper && wallpaperTarget != null && mService.mAnimateWallpaperWithTarget) {
938            final WindowStateAnimator wallpaperAnimator = wallpaperTarget.mWinAnimator;
939            if (wallpaperAnimator.mHasLocalTransformation &&
940                    wallpaperAnimator.mAnimation != null &&
941                    !wallpaperAnimator.mAnimation.getDetachWallpaper()) {
942                attachedTransformation = wallpaperAnimator.mTransformation;
943                if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
944                    Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
945                }
946            }
947            final AppWindowAnimator wpAppAnimator = wallpaperTarget.mAppToken == null ?
948                    null : wallpaperTarget.mAppToken.mAppAnimator;
949                if (wpAppAnimator != null && wpAppAnimator.hasTransformation
950                    && wpAppAnimator.animation != null
951                    && !wpAppAnimator.animation.getDetachWallpaper()) {
952                appTransformation = wpAppAnimator.transformation;
953                if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
954                    Slog.v(TAG, "WP target app xform: " + appTransformation);
955                }
956            }
957        }
958
959        final int displayId = mWin.getDisplayId();
960        final ScreenRotationAnimation screenRotationAnimation =
961                mAnimator.getScreenRotationAnimationLocked(displayId);
962        final boolean screenAnimation =
963                screenRotationAnimation != null && screenRotationAnimation.isAnimating();
964        if (selfTransformation || attachedTransformation != null
965                || appTransformation != null || screenAnimation) {
966            // cache often used attributes locally
967            final Rect frame = mWin.mFrame;
968            final float tmpFloats[] = mService.mTmpFloats;
969            final Matrix tmpMatrix = mWin.mTmpMatrix;
970
971            // Compute the desired transformation.
972            if (screenAnimation && screenRotationAnimation.isRotating()) {
973                // If we are doing a screen animation, the global rotation
974                // applied to windows can result in windows that are carefully
975                // aligned with each other to slightly separate, allowing you
976                // to see what is behind them.  An unsightly mess.  This...
977                // thing...  magically makes it call good: scale each window
978                // slightly (two pixels larger in each dimension, from the
979                // window's center).
980                final float w = frame.width();
981                final float h = frame.height();
982                if (w>=1 && h>=1) {
983                    tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
984                } else {
985                    tmpMatrix.reset();
986                }
987            } else {
988                tmpMatrix.reset();
989            }
990            tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
991            if (selfTransformation) {
992                tmpMatrix.postConcat(mTransformation.getMatrix());
993            }
994            tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
995            if (attachedTransformation != null) {
996                tmpMatrix.postConcat(attachedTransformation.getMatrix());
997            }
998            if (appTransformation != null) {
999                tmpMatrix.postConcat(appTransformation.getMatrix());
1000            }
1001            if (mAnimator.mUniverseBackground != null) {
1002                tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
1003            }
1004            if (screenAnimation) {
1005                tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix());
1006            }
1007
1008            //TODO (multidisplay): Magnification is supported only for the default display.
1009            if (mService.mAccessibilityController != null && displayId == Display.DEFAULT_DISPLAY) {
1010                MagnificationSpec spec = mService.mAccessibilityController
1011                        .getMagnificationSpecForWindowLocked(mWin);
1012                if (spec != null && !spec.isNop()) {
1013                    tmpMatrix.postScale(spec.scale, spec.scale);
1014                    tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
1015                }
1016            }
1017
1018            // "convert" it into SurfaceFlinger's format
1019            // (a 2x2 matrix + an offset)
1020            // Here we must not transform the position of the surface
1021            // since it is already included in the transformation.
1022            //Slog.i(TAG, "Transform: " + matrix);
1023
1024            mHaveMatrix = true;
1025            tmpMatrix.getValues(tmpFloats);
1026            mDsDx = tmpFloats[Matrix.MSCALE_X];
1027            mDtDx = tmpFloats[Matrix.MSKEW_Y];
1028            mDsDy = tmpFloats[Matrix.MSKEW_X];
1029            mDtDy = tmpFloats[Matrix.MSCALE_Y];
1030            float x = tmpFloats[Matrix.MTRANS_X];
1031            float y = tmpFloats[Matrix.MTRANS_Y];
1032            int w = frame.width();
1033            int h = frame.height();
1034            mWin.mShownFrame.set(x, y, x+w, y+h);
1035
1036            // Now set the alpha...  but because our current hardware
1037            // can't do alpha transformation on a non-opaque surface,
1038            // turn it off if we are running an animation that is also
1039            // transforming since it is more important to have that
1040            // animation be smooth.
1041            mShownAlpha = mAlpha;
1042            mHasClipRect = false;
1043            if (!mService.mLimitedAlphaCompositing
1044                    || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
1045                    || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
1046                            && x == frame.left && y == frame.top))) {
1047                //Slog.i(TAG, "Applying alpha transform");
1048                if (selfTransformation) {
1049                    mShownAlpha *= mTransformation.getAlpha();
1050                }
1051                if (attachedTransformation != null) {
1052                    mShownAlpha *= attachedTransformation.getAlpha();
1053                }
1054                if (appTransformation != null) {
1055                    mShownAlpha *= appTransformation.getAlpha();
1056                    if (appTransformation.hasClipRect()) {
1057                        mClipRect.set(appTransformation.getClipRect());
1058                        mHasClipRect = true;
1059                    }
1060                }
1061                if (mAnimator.mUniverseBackground != null) {
1062                    mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
1063                }
1064                if (screenAnimation) {
1065                    mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
1066                }
1067            } else {
1068                //Slog.i(TAG, "Not applying alpha transform");
1069            }
1070
1071            if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV)
1072                    && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
1073                    TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
1074                    + " self=" + (selfTransformation ? mTransformation.getAlpha() : "null")
1075                    + " attached=" + (attachedTransformation == null ?
1076                            "null" : attachedTransformation.getAlpha())
1077                    + " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha())
1078                    + " screen=" + (screenAnimation ?
1079                            screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
1080            return;
1081        } else if (mIsWallpaper && mService.mInnerFields.mWallpaperActionPending) {
1082            return;
1083        }
1084
1085        if (WindowManagerService.localLOGV) Slog.v(
1086                TAG, "computeShownFrameLocked: " + this +
1087                " not attached, mAlpha=" + mAlpha);
1088
1089        final boolean applyUniverseTransformation = (mAnimator.mUniverseBackground != null
1090                && mWin.mAttrs.type != WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
1091                && mWin.mBaseLayer < mAnimator.mAboveUniverseLayer);
1092        MagnificationSpec spec = null;
1093        //TODO (multidisplay): Magnification is supported only for the default display.
1094        if (mService.mAccessibilityController != null && displayId == Display.DEFAULT_DISPLAY) {
1095            spec = mService.mAccessibilityController.getMagnificationSpecForWindowLocked(mWin);
1096        }
1097        if (applyUniverseTransformation || spec != null) {
1098            final Rect frame = mWin.mFrame;
1099            final float tmpFloats[] = mService.mTmpFloats;
1100            final Matrix tmpMatrix = mWin.mTmpMatrix;
1101
1102            tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
1103            tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
1104
1105            if (applyUniverseTransformation) {
1106                tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
1107            }
1108
1109            if (spec != null && !spec.isNop()) {
1110                tmpMatrix.postScale(spec.scale, spec.scale);
1111                tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
1112            }
1113
1114            tmpMatrix.getValues(tmpFloats);
1115
1116            mHaveMatrix = true;
1117            mDsDx = tmpFloats[Matrix.MSCALE_X];
1118            mDtDx = tmpFloats[Matrix.MSKEW_Y];
1119            mDsDy = tmpFloats[Matrix.MSKEW_X];
1120            mDtDy = tmpFloats[Matrix.MSCALE_Y];
1121            float x = tmpFloats[Matrix.MTRANS_X];
1122            float y = tmpFloats[Matrix.MTRANS_Y];
1123            int w = frame.width();
1124            int h = frame.height();
1125            mWin.mShownFrame.set(x, y, x + w, y + h);
1126
1127            mShownAlpha = mAlpha;
1128            if (applyUniverseTransformation) {
1129                mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
1130            }
1131        } else {
1132            mWin.mShownFrame.set(mWin.mFrame);
1133            if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
1134                mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
1135            }
1136            mShownAlpha = mAlpha;
1137            mHaveMatrix = false;
1138            mDsDx = mWin.mGlobalScale;
1139            mDtDx = 0;
1140            mDsDy = 0;
1141            mDtDy = mWin.mGlobalScale;
1142        }
1143    }
1144
1145    void applyDecorRect(final Rect decorRect) {
1146        final WindowState w = mWin;
1147        final int width = w.mFrame.width();
1148        final int height = w.mFrame.height();
1149
1150        // Compute the offset of the window in relation to the decor rect.
1151        final int left = w.mXOffset + w.mFrame.left;
1152        final int top = w.mYOffset + w.mFrame.top;
1153
1154        // Initialize the decor rect to the entire frame.
1155        w.mSystemDecorRect.set(0, 0, width, height);
1156
1157        // Intersect with the decor rect, offsetted by window position.
1158        w.mSystemDecorRect.intersect(decorRect.left - left, decorRect.top - top,
1159                decorRect.right - left, decorRect.bottom - top);
1160
1161        // If size compatibility is being applied to the window, the
1162        // surface is scaled relative to the screen.  Also apply this
1163        // scaling to the crop rect.  We aren't using the standard rect
1164        // scale function because we want to round things to make the crop
1165        // always round to a larger rect to ensure we don't crop too
1166        // much and hide part of the window that should be seen.
1167        if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
1168            final float scale = w.mInvGlobalScale;
1169            w.mSystemDecorRect.left = (int) (w.mSystemDecorRect.left * scale - 0.5f);
1170            w.mSystemDecorRect.top = (int) (w.mSystemDecorRect.top * scale - 0.5f);
1171            w.mSystemDecorRect.right = (int) ((w.mSystemDecorRect.right+1) * scale - 0.5f);
1172            w.mSystemDecorRect.bottom = (int) ((w.mSystemDecorRect.bottom+1) * scale - 0.5f);
1173        }
1174    }
1175
1176    void updateSurfaceWindowCrop(final boolean recoveringMemory) {
1177        final WindowState w = mWin;
1178        final DisplayContent displayContent = w.getDisplayContent();
1179        if (displayContent == null) {
1180            return;
1181        }
1182
1183        // Need to recompute a new system decor rect each time.
1184        if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
1185            // Currently can't do this cropping for scaled windows.  We'll
1186            // just keep the crop rect the same as the source surface.
1187            w.mSystemDecorRect.set(0, 0, w.mRequestedWidth, w.mRequestedHeight);
1188        } else if (!w.isDefaultDisplay()) {
1189            // On a different display there is no system decor.  Crop the window
1190            // by the screen boundaries.
1191            final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1192            w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1193            w.mSystemDecorRect.intersect(-w.mCompatFrame.left, -w.mCompatFrame.top,
1194                    displayInfo.logicalWidth - w.mCompatFrame.left,
1195                    displayInfo.logicalHeight - w.mCompatFrame.top);
1196        } else if (w.mLayer >= mService.mSystemDecorLayer) {
1197            // Above the decor layer is easy, just use the entire window.
1198            // Unless we have a universe background...  in which case all the
1199            // windows need to be cropped by the screen, so they don't cover
1200            // the universe background.
1201            if (mAnimator.mUniverseBackground == null) {
1202                w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1203            } else {
1204                applyDecorRect(mService.mScreenRect);
1205            }
1206        } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
1207                || w.mDecorFrame.isEmpty()) {
1208            // The universe background isn't cropped, nor windows without policy decor.
1209            w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1210        } else {
1211            // Crop to the system decor specified by policy.
1212            applyDecorRect(w.mDecorFrame);
1213        }
1214
1215        // By default, the clip rect is the system decor.
1216        final Rect clipRect = mTmpClipRect;
1217        clipRect.set(w.mSystemDecorRect);
1218
1219        // Expand the clip rect for surface insets.
1220        final WindowManager.LayoutParams attrs = w.mAttrs;
1221        clipRect.left -= attrs.surfaceInsets.left;
1222        clipRect.top -= attrs.surfaceInsets.top;
1223        clipRect.right += attrs.surfaceInsets.right;
1224        clipRect.bottom += attrs.surfaceInsets.bottom;
1225
1226        // If we have an animated clip rect, intersect it with the clip rect.
1227        if (mHasClipRect) {
1228            // NOTE: We are adding a temporary workaround due to the status bar
1229            // not always reporting the correct system decor rect. In such
1230            // cases, we take into account the specified content insets as well.
1231            if ((w.mSystemUiVisibility & SYSTEM_UI_FLAGS_LAYOUT_STABLE_FULLSCREEN)
1232                    == SYSTEM_UI_FLAGS_LAYOUT_STABLE_FULLSCREEN) {
1233                // Don't apply the workaround to apps explicitly requesting
1234                // fullscreen layout.
1235                clipRect.intersect(mClipRect);
1236            } else {
1237                final int offsetTop = Math.max(clipRect.top, w.mContentInsets.top);
1238                clipRect.offset(0, -offsetTop);
1239                clipRect.intersect(mClipRect);
1240                clipRect.offset(0, offsetTop);
1241            }
1242        }
1243
1244        // The clip rect was generated assuming (0,0) as the window origin,
1245        // so we need to translate to match the actual surface coordinates.
1246        clipRect.offset(attrs.surfaceInsets.left, attrs.surfaceInsets.top);
1247
1248        if (!clipRect.equals(mLastClipRect)) {
1249            mLastClipRect.set(clipRect);
1250            try {
1251                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1252                        "CROP " + clipRect.toShortString(), null);
1253                mSurfaceControl.setWindowCrop(clipRect);
1254            } catch (RuntimeException e) {
1255                Slog.w(TAG, "Error setting crop surface of " + w
1256                        + " crop=" + clipRect.toShortString(), e);
1257                if (!recoveringMemory) {
1258                    mService.reclaimSomeSurfaceMemoryLocked(this, "crop", true);
1259                }
1260            }
1261        }
1262    }
1263
1264    void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
1265        final WindowState w = mWin;
1266
1267        int width;
1268        int height;
1269        if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
1270            // for a scaled surface, we always want the requested
1271            // size.
1272            width  = w.mRequestedWidth;
1273            height = w.mRequestedHeight;
1274        } else {
1275            width = w.mCompatFrame.width();
1276            height = w.mCompatFrame.height();
1277        }
1278
1279        // Something is wrong and SurfaceFlinger will not like this,
1280        // try to revert to sane values
1281        if (width < 1) {
1282            width = 1;
1283        }
1284        if (height < 1) {
1285            height = 1;
1286        }
1287
1288        float left = w.mShownFrame.left;
1289        float top = w.mShownFrame.top;
1290
1291        // Adjust for surface insets.
1292        final LayoutParams attrs = w.getAttrs();
1293        width += attrs.surfaceInsets.left + attrs.surfaceInsets.right;
1294        height += attrs.surfaceInsets.top + attrs.surfaceInsets.bottom;
1295        left -= attrs.surfaceInsets.left;
1296        top -= attrs.surfaceInsets.top;
1297
1298        final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top;
1299        if (surfaceMoved) {
1300            mSurfaceX = left;
1301            mSurfaceY = top;
1302
1303            try {
1304                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1305                        "POS " + left + ", " + top, null);
1306                mSurfaceControl.setPosition(left, top);
1307            } catch (RuntimeException e) {
1308                Slog.w(TAG, "Error positioning surface of " + w
1309                        + " pos=(" + left + "," + top + ")", e);
1310                if (!recoveringMemory) {
1311                    mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
1312                }
1313            }
1314        }
1315
1316        final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
1317        if (surfaceResized) {
1318            mSurfaceW = width;
1319            mSurfaceH = height;
1320            mSurfaceResized = true;
1321
1322            try {
1323                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1324                        "SIZE " + width + "x" + height, null);
1325                mSurfaceControl.setSize(width, height);
1326                mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1327                        WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
1328                if ((w.mAttrs.flags & LayoutParams.FLAG_DIM_BEHIND) != 0) {
1329                    w.getStack().startDimmingIfNeeded(this);
1330                }
1331            } catch (RuntimeException e) {
1332                // If something goes wrong with the surface (such
1333                // as running out of memory), don't take down the
1334                // entire system.
1335                Slog.e(TAG, "Error resizing surface of " + w
1336                        + " size=(" + width + "x" + height + ")", e);
1337                if (!recoveringMemory) {
1338                    mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
1339                }
1340            }
1341        }
1342
1343        updateSurfaceWindowCrop(recoveringMemory);
1344    }
1345
1346    public void prepareSurfaceLocked(final boolean recoveringMemory) {
1347        final WindowState w = mWin;
1348        if (mSurfaceControl == null) {
1349            if (w.mOrientationChanging) {
1350                if (DEBUG_ORIENTATION) {
1351                    Slog.v(TAG, "Orientation change skips hidden " + w);
1352                }
1353                w.mOrientationChanging = false;
1354            }
1355            return;
1356        }
1357
1358        boolean displayed = false;
1359
1360        computeShownFrameLocked();
1361
1362        setSurfaceBoundariesLocked(recoveringMemory);
1363
1364        if (mIsWallpaper && !mWin.mWallpaperVisible) {
1365            // Wallpaper is no longer visible and there is no wp target => hide it.
1366            hide();
1367        } else if (w.mAttachedHidden || !w.isOnScreen()) {
1368            hide();
1369            mAnimator.hideWallpapersLocked(w);
1370
1371            // If we are waiting for this window to handle an
1372            // orientation change, well, it is hidden, so
1373            // doesn't really matter.  Note that this does
1374            // introduce a potential glitch if the window
1375            // becomes unhidden before it has drawn for the
1376            // new orientation.
1377            if (w.mOrientationChanging) {
1378                w.mOrientationChanging = false;
1379                if (DEBUG_ORIENTATION) Slog.v(TAG,
1380                        "Orientation change skips hidden " + w);
1381            }
1382        } else if (mLastLayer != mAnimLayer
1383                || mLastAlpha != mShownAlpha
1384                || mLastDsDx != mDsDx
1385                || mLastDtDx != mDtDx
1386                || mLastDsDy != mDsDy
1387                || mLastDtDy != mDtDy
1388                || w.mLastHScale != w.mHScale
1389                || w.mLastVScale != w.mVScale
1390                || mLastHidden) {
1391            displayed = true;
1392            mLastAlpha = mShownAlpha;
1393            mLastLayer = mAnimLayer;
1394            mLastDsDx = mDsDx;
1395            mLastDtDx = mDtDx;
1396            mLastDsDy = mDsDy;
1397            mLastDtDy = mDtDy;
1398            w.mLastHScale = w.mHScale;
1399            w.mLastVScale = w.mVScale;
1400            if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1401                    "alpha=" + mShownAlpha + " layer=" + mAnimLayer
1402                    + " matrix=[" + (mDsDx*w.mHScale)
1403                    + "," + (mDtDx*w.mVScale)
1404                    + "][" + (mDsDy*w.mHScale)
1405                    + "," + (mDtDy*w.mVScale) + "]", null);
1406            if (mSurfaceControl != null) {
1407                try {
1408                    mSurfaceAlpha = mShownAlpha;
1409                    mSurfaceControl.setAlpha(mShownAlpha);
1410                    mSurfaceLayer = mAnimLayer;
1411                    mSurfaceControl.setLayer(mAnimLayer);
1412                    mSurfaceControl.setMatrix(
1413                            mDsDx * w.mHScale, mDtDx * w.mVScale,
1414                            mDsDy * w.mHScale, mDtDy * w.mVScale);
1415
1416                    if (mLastHidden && mDrawState == HAS_DRAWN) {
1417                        if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1418                                "SHOW (performLayout)", null);
1419                        if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
1420                                + " during relayout");
1421                        if (showSurfaceRobustlyLocked()) {
1422                            mLastHidden = false;
1423                            if (mIsWallpaper) {
1424                                mService.dispatchWallpaperVisibility(w, true);
1425                            }
1426                            // This draw means the difference between unique content and mirroring.
1427                            // Run another pass through performLayout to set mHasContent in the
1428                            // LogicalDisplay.
1429                            mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1430                                    WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
1431                        } else {
1432                            w.mOrientationChanging = false;
1433                        }
1434                    }
1435                    if (mSurfaceControl != null) {
1436                        w.mToken.hasVisible = true;
1437                    }
1438                } catch (RuntimeException e) {
1439                    Slog.w(TAG, "Error updating surface in " + w, e);
1440                    if (!recoveringMemory) {
1441                        mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
1442                    }
1443                }
1444            }
1445        } else {
1446            if (DEBUG_ANIM && isAnimating()) {
1447                Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
1448            }
1449            displayed = true;
1450        }
1451
1452        if (displayed) {
1453            if (w.mOrientationChanging) {
1454                if (!w.isDrawnLw()) {
1455                    mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
1456                    mAnimator.mLastWindowFreezeSource = w;
1457                    if (DEBUG_ORIENTATION) Slog.v(TAG,
1458                            "Orientation continue waiting for draw in " + w);
1459                } else {
1460                    w.mOrientationChanging = false;
1461                    if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
1462                }
1463            }
1464            w.mToken.hasVisible = true;
1465        }
1466    }
1467
1468    void setTransparentRegionHintLocked(final Region region) {
1469        if (mSurfaceControl == null) {
1470            Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1471            return;
1472        }
1473        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion");
1474        SurfaceControl.openTransaction();
1475        try {
1476            if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1477                    "transparentRegionHint=" + region, null);
1478            mSurfaceControl.setTransparentRegionHint(region);
1479        } finally {
1480            SurfaceControl.closeTransaction();
1481            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1482                    "<<< CLOSE TRANSACTION setTransparentRegion");
1483        }
1484    }
1485
1486    void setWallpaperOffset(RectF shownFrame) {
1487        final int left = (int) shownFrame.left;
1488        final int top = (int) shownFrame.top;
1489        if (mSurfaceX != left || mSurfaceY != top) {
1490            mSurfaceX = left;
1491            mSurfaceY = top;
1492            if (mAnimating) {
1493                // If this window (or its app token) is animating, then the position
1494                // of the surface will be re-computed on the next animation frame.
1495                // We can't poke it directly here because it depends on whatever
1496                // transformation is being applied by the animation.
1497                return;
1498            }
1499            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset");
1500            SurfaceControl.openTransaction();
1501            try {
1502                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1503                        "POS " + left + ", " + top, null);
1504                mSurfaceControl.setPosition(mWin.mFrame.left + left, mWin.mFrame.top + top);
1505                updateSurfaceWindowCrop(false);
1506            } catch (RuntimeException e) {
1507                Slog.w(TAG, "Error positioning surface of " + mWin
1508                        + " pos=(" + left + "," + top + ")", e);
1509            } finally {
1510                SurfaceControl.closeTransaction();
1511                if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1512                        "<<< CLOSE TRANSACTION setWallpaperOffset");
1513            }
1514        }
1515    }
1516
1517    void setOpaque(boolean isOpaque) {
1518        if (mSurfaceControl == null) {
1519            return;
1520        }
1521        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaque");
1522        SurfaceControl.openTransaction();
1523        try {
1524            if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "isOpaque=" + isOpaque,
1525                    null);
1526            mSurfaceControl.setOpaque(isOpaque);
1527        } finally {
1528            SurfaceControl.closeTransaction();
1529            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaque");
1530        }
1531    }
1532
1533    // This must be called while inside a transaction.
1534    boolean performShowLocked() {
1535        if (mWin.isHiddenFromUserLocked()) {
1536            return false;
1537        }
1538        if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
1539                mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
1540            RuntimeException e = null;
1541            if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1542                e = new RuntimeException();
1543                e.fillInStackTrace();
1544            }
1545            Slog.v(TAG, "performShow on " + this
1546                    + ": mDrawState=" + mDrawState + " readyForDisplay="
1547                    + mWin.isReadyForDisplayIgnoringKeyguard()
1548                    + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING)
1549                    + " during animation: policyVis=" + mWin.mPolicyVisibility
1550                    + " attHidden=" + mWin.mAttachedHidden
1551                    + " tok.hiddenRequested="
1552                    + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1553                    + " tok.hidden="
1554                    + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1555                    + " animating=" + mAnimating
1556                    + " tok animating="
1557                    + (mAppAnimator != null ? mAppAnimator.animating : false), e);
1558        }
1559        if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplayIgnoringKeyguard()) {
1560            if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
1561                WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
1562            if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
1563                    mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
1564                Slog.v(TAG, "Showing " + this
1565                        + " during animation: policyVis=" + mWin.mPolicyVisibility
1566                        + " attHidden=" + mWin.mAttachedHidden
1567                        + " tok.hiddenRequested="
1568                        + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1569                        + " tok.hidden="
1570                        + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1571                        + " animating=" + mAnimating
1572                        + " tok animating="
1573                        + (mAppAnimator != null ? mAppAnimator.animating : false));
1574            }
1575
1576            mService.enableScreenIfNeededLocked();
1577
1578            applyEnterAnimationLocked();
1579
1580            // Force the show in the next prepareSurfaceLocked() call.
1581            mLastAlpha = -1;
1582            if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
1583                Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + this);
1584            mDrawState = HAS_DRAWN;
1585            mService.scheduleAnimationLocked();
1586
1587            int i = mWin.mChildWindows.size();
1588            while (i > 0) {
1589                i--;
1590                WindowState c = mWin.mChildWindows.get(i);
1591                if (c.mAttachedHidden) {
1592                    c.mAttachedHidden = false;
1593                    if (c.mWinAnimator.mSurfaceControl != null) {
1594                        c.mWinAnimator.performShowLocked();
1595                        // It hadn't been shown, which means layout not
1596                        // performed on it, so now we want to make sure to
1597                        // do a layout.  If called from within the transaction
1598                        // loop, this will cause it to restart with a new
1599                        // layout.
1600                        final DisplayContent displayContent = c.getDisplayContent();
1601                        if (displayContent != null) {
1602                            displayContent.layoutNeeded = true;
1603                        }
1604                    }
1605                }
1606            }
1607
1608            if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1609                    && mWin.mAppToken != null) {
1610                mWin.mAppToken.firstWindowDrawn = true;
1611
1612                if (mWin.mAppToken.startingData != null) {
1613                    if (WindowManagerService.DEBUG_STARTING_WINDOW ||
1614                            WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
1615                            "Finish starting " + mWin.mToken
1616                            + ": first real window is shown, no animation");
1617                    // If this initial window is animating, stop it -- we
1618                    // will do an animation to reveal it from behind the
1619                    // starting window, so there is no need for it to also
1620                    // be doing its own stuff.
1621                    clearAnimation();
1622                    mService.mFinishedStarting.add(mWin.mAppToken);
1623                    mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1624                }
1625                mWin.mAppToken.updateReportedVisibilityLocked();
1626            }
1627
1628            return true;
1629        }
1630
1631        return false;
1632    }
1633
1634    /**
1635     * Have the surface flinger show a surface, robustly dealing with
1636     * error conditions.  In particular, if there is not enough memory
1637     * to show the surface, then we will try to get rid of other surfaces
1638     * in order to succeed.
1639     *
1640     * @return Returns true if the surface was successfully shown.
1641     */
1642    boolean showSurfaceRobustlyLocked() {
1643        try {
1644            if (mSurfaceControl != null) {
1645                mSurfaceShown = true;
1646                mSurfaceControl.show();
1647                if (mWin.mTurnOnScreen) {
1648                    if (DEBUG_VISIBILITY) Slog.v(TAG,
1649                            "Show surface turning screen on: " + mWin);
1650                    mWin.mTurnOnScreen = false;
1651                    mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
1652                }
1653            }
1654            return true;
1655        } catch (RuntimeException e) {
1656            Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + mWin, e);
1657        }
1658
1659        mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1660
1661        return false;
1662    }
1663
1664    void applyEnterAnimationLocked() {
1665        final int transit;
1666        if (mEnterAnimationPending) {
1667            mEnterAnimationPending = false;
1668            transit = WindowManagerPolicy.TRANSIT_ENTER;
1669        } else {
1670            transit = WindowManagerPolicy.TRANSIT_SHOW;
1671        }
1672        applyAnimationLocked(transit, true);
1673        //TODO (multidisplay): Magnification is supported only for the default display.
1674        if (mService.mAccessibilityController != null
1675                && mWin.getDisplayId() == Display.DEFAULT_DISPLAY) {
1676            mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit);
1677        }
1678    }
1679
1680    /**
1681     * Choose the correct animation and set it to the passed WindowState.
1682     * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn
1683     *      then the animation will be app_starting_exit. Any other value loads the animation from
1684     *      the switch statement below.
1685     * @param isEntrance The animation type the last time this was called. Used to keep from
1686     *      loading the same animation twice.
1687     * @return true if an animation has been loaded.
1688     */
1689    boolean applyAnimationLocked(int transit, boolean isEntrance) {
1690        if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1691            // If we are trying to apply an animation, but already running
1692            // an animation of the same type, then just leave that one alone.
1693            return true;
1694        }
1695
1696        // Only apply an animation if the display isn't frozen.  If it is
1697        // frozen, there is no reason to animate and it can cause strange
1698        // artifacts when we unfreeze the display if some different animation
1699        // is running.
1700        if (mService.okToDisplay()) {
1701            int anim = mPolicy.selectAnimationLw(mWin, transit);
1702            int attr = -1;
1703            Animation a = null;
1704            if (anim != 0) {
1705                a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null;
1706            } else {
1707                switch (transit) {
1708                    case WindowManagerPolicy.TRANSIT_ENTER:
1709                        attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1710                        break;
1711                    case WindowManagerPolicy.TRANSIT_EXIT:
1712                        attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1713                        break;
1714                    case WindowManagerPolicy.TRANSIT_SHOW:
1715                        attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1716                        break;
1717                    case WindowManagerPolicy.TRANSIT_HIDE:
1718                        attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1719                        break;
1720                }
1721                if (attr >= 0) {
1722                    a = mService.mAppTransition.loadAnimationAttr(mWin.mAttrs, attr);
1723                }
1724            }
1725            if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
1726                    "applyAnimation: win=" + this
1727                    + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1728                    + " a=" + a
1729                    + " transit=" + transit
1730                    + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
1731            if (a != null) {
1732                if (WindowManagerService.DEBUG_ANIM) {
1733                    RuntimeException e = null;
1734                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1735                        e = new RuntimeException();
1736                        e.fillInStackTrace();
1737                    }
1738                    Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
1739                }
1740                setAnimation(a);
1741                mAnimationIsEntrance = isEntrance;
1742            }
1743        } else {
1744            clearAnimation();
1745        }
1746
1747        return mAnimation != null;
1748    }
1749
1750    public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1751        if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1752                || mAnimation != null) {
1753            pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1754                    pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1755                    pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1756                    pw.print(" mAnimation="); pw.println(mAnimation);
1757        }
1758        if (mHasTransformation || mHasLocalTransformation) {
1759            pw.print(prefix); pw.print("XForm: has=");
1760                    pw.print(mHasTransformation);
1761                    pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1762                    pw.print(" "); mTransformation.printShortString(pw);
1763                    pw.println();
1764        }
1765        if (mSurfaceControl != null) {
1766            if (dumpAll) {
1767                pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl);
1768                pw.print(prefix); pw.print("mDrawState=");
1769                pw.print(drawStateToString(mDrawState));
1770                pw.print(" mLastHidden="); pw.println(mLastHidden);
1771            }
1772            pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1773                    pw.print(" layer="); pw.print(mSurfaceLayer);
1774                    pw.print(" alpha="); pw.print(mSurfaceAlpha);
1775                    pw.print(" rect=("); pw.print(mSurfaceX);
1776                    pw.print(","); pw.print(mSurfaceY);
1777                    pw.print(") "); pw.print(mSurfaceW);
1778                    pw.print(" x "); pw.println(mSurfaceH);
1779        }
1780        if (mPendingDestroySurface != null) {
1781            pw.print(prefix); pw.print("mPendingDestroySurface=");
1782                    pw.println(mPendingDestroySurface);
1783        }
1784        if (mSurfaceResized || mSurfaceDestroyDeferred) {
1785            pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1786                    pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1787        }
1788        if (mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND) {
1789            pw.print(prefix); pw.print("mUniverseTransform=");
1790                    mUniverseTransform.printShortString(pw);
1791                    pw.println();
1792        }
1793        if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1794            pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1795                    pw.print(" mAlpha="); pw.print(mAlpha);
1796                    pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1797        }
1798        if (mHaveMatrix || mWin.mGlobalScale != 1) {
1799            pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1800                    pw.print(" mDsDx="); pw.print(mDsDx);
1801                    pw.print(" mDtDx="); pw.print(mDtDx);
1802                    pw.print(" mDsDy="); pw.print(mDsDy);
1803                    pw.print(" mDtDy="); pw.println(mDtDy);
1804        }
1805    }
1806
1807    @Override
1808    public String toString() {
1809        StringBuffer sb = new StringBuffer("WindowStateAnimator{");
1810        sb.append(Integer.toHexString(System.identityHashCode(this)));
1811        sb.append(' ');
1812        sb.append(mWin.mAttrs.getTitle());
1813        sb.append('}');
1814        return sb.toString();
1815    }
1816}
1817