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