WindowStateAnimator.java revision d2a1eec400128f39e1b223a720a88dbd395f3e6e
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
744            // Set up surface control with initial size.
745            try {
746                mSurfaceW = width;
747                mSurfaceH = height;
748
749                final boolean isHwAccelerated = (attrs.flags &
750                        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
751                final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
752                if (!PixelFormat.formatHasAlpha(attrs.format)) {
753                    flags |= SurfaceControl.OPAQUE;
754                }
755
756                if (DEBUG_SURFACE_TRACE) {
757                    mSurfaceControl = new SurfaceTrace(
758                            mSession.mSurfaceSession,
759                            attrs.getTitle().toString(),
760                            width, height, format, flags);
761                } else {
762                    mSurfaceControl = new SurfaceControl(
763                        mSession.mSurfaceSession,
764                        attrs.getTitle().toString(),
765                        width, height, format, flags);
766                }
767
768                w.mHasSurface = true;
769
770                if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
771                    Slog.i(TAG, "  CREATE SURFACE "
772                            + mSurfaceControl + " IN SESSION "
773                            + mSession.mSurfaceSession
774                            + ": pid=" + mSession.mPid + " format="
775                            + attrs.format + " flags=0x"
776                            + Integer.toHexString(flags)
777                            + " / " + this);
778                }
779            } catch (OutOfResourcesException e) {
780                w.mHasSurface = false;
781                Slog.w(TAG, "OutOfResourcesException creating surface");
782                mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
783                mDrawState = NO_SURFACE;
784                return null;
785            } catch (Exception e) {
786                w.mHasSurface = false;
787                Slog.e(TAG, "Exception creating surface", e);
788                mDrawState = NO_SURFACE;
789                return null;
790            }
791
792            if (WindowManagerService.localLOGV) {
793                Slog.v(TAG, "Got surface: " + mSurfaceControl
794                        + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top
795                        + ", animLayer=" + mAnimLayer);
796            }
797
798            if (SHOW_LIGHT_TRANSACTIONS) {
799                Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
800                WindowManagerService.logSurface(w, "CREATE pos=("
801                        + w.mFrame.left + "," + w.mFrame.top + ") ("
802                        + w.mCompatFrame.width() + "x" + w.mCompatFrame.height()
803                        + "), layer=" + mAnimLayer + " HIDE", null);
804            }
805
806            // Start a new transaction and apply position & offset.
807            SurfaceControl.openTransaction();
808            try {
809                mSurfaceX = left;
810                mSurfaceY = top;
811
812                try {
813                    mSurfaceControl.setPosition(left, top);
814                    mSurfaceLayer = mAnimLayer;
815                    final DisplayContent displayContent = w.getDisplayContent();
816                    if (displayContent != null) {
817                        mSurfaceControl.setLayerStack(displayContent.getDisplay().getLayerStack());
818                    }
819                    mSurfaceControl.setLayer(mAnimLayer);
820                    mSurfaceControl.setAlpha(0);
821                    mSurfaceShown = false;
822                } catch (RuntimeException e) {
823                    Slog.w(TAG, "Error creating surface in " + w, e);
824                    mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
825                }
826                mLastHidden = true;
827            } finally {
828                SurfaceControl.closeTransaction();
829                if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
830                        "<<< CLOSE TRANSACTION createSurfaceLocked");
831            }
832            if (WindowManagerService.localLOGV) Slog.v(
833                    TAG, "Created surface " + this);
834        }
835        return mSurfaceControl;
836    }
837
838    void destroySurfaceLocked() {
839        if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
840            mWin.mAppToken.startingDisplayed = false;
841        }
842
843        if (mSurfaceControl != null) {
844
845            int i = mWin.mChildWindows.size();
846            while (i > 0) {
847                i--;
848                WindowState c = mWin.mChildWindows.get(i);
849                c.mAttachedHidden = true;
850            }
851
852            try {
853                if (DEBUG_VISIBILITY) {
854                    RuntimeException e = null;
855                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
856                        e = new RuntimeException();
857                        e.fillInStackTrace();
858                    }
859                    Slog.w(TAG, "Window " + this + " destroying surface "
860                            + mSurfaceControl + ", session " + mSession, e);
861                }
862                if (mSurfaceDestroyDeferred) {
863                    if (mSurfaceControl != null && mPendingDestroySurface != mSurfaceControl) {
864                        if (mPendingDestroySurface != null) {
865                            if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
866                                RuntimeException e = null;
867                                if (!WindowManagerService.HIDE_STACK_CRAWLS) {
868                                    e = new RuntimeException();
869                                    e.fillInStackTrace();
870                                }
871                                WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
872                            }
873                            mPendingDestroySurface.destroy();
874                        }
875                        mPendingDestroySurface = mSurfaceControl;
876                    }
877                } else {
878                    if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
879                        RuntimeException e = null;
880                        if (!WindowManagerService.HIDE_STACK_CRAWLS) {
881                            e = new RuntimeException();
882                            e.fillInStackTrace();
883                        }
884                        WindowManagerService.logSurface(mWin, "DESTROY", e);
885                    }
886                    mSurfaceControl.destroy();
887                }
888                mAnimator.hideWallpapersLocked(mWin);
889            } catch (RuntimeException e) {
890                Slog.w(TAG, "Exception thrown when destroying Window " + this
891                    + " surface " + mSurfaceControl + " session " + mSession
892                    + ": " + e.toString());
893            }
894
895            mSurfaceShown = false;
896            mSurfaceControl = null;
897            mWin.mHasSurface = false;
898            mDrawState = NO_SURFACE;
899        }
900    }
901
902    void destroyDeferredSurfaceLocked() {
903        try {
904            if (mPendingDestroySurface != null) {
905                if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
906                    RuntimeException e = null;
907                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
908                        e = new RuntimeException();
909                        e.fillInStackTrace();
910                    }
911                    WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
912                }
913                mPendingDestroySurface.destroy();
914                mAnimator.hideWallpapersLocked(mWin);
915            }
916        } catch (RuntimeException e) {
917            Slog.w(TAG, "Exception thrown when destroying Window "
918                    + this + " surface " + mPendingDestroySurface
919                    + " session " + mSession + ": " + e.toString());
920        }
921        mSurfaceDestroyDeferred = false;
922        mPendingDestroySurface = null;
923    }
924
925    void computeShownFrameLocked() {
926        final boolean selfTransformation = mHasLocalTransformation;
927        Transformation attachedTransformation =
928                (mAttachedWinAnimator != null && mAttachedWinAnimator.mHasLocalTransformation)
929                ? mAttachedWinAnimator.mTransformation : null;
930        Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation)
931                ? mAppAnimator.transformation : null;
932
933        // Wallpapers are animated based on the "real" window they
934        // are currently targeting.
935        final WindowState wallpaperTarget = mService.mWallpaperTarget;
936        if (mIsWallpaper && wallpaperTarget != null && mService.mAnimateWallpaperWithTarget) {
937            final WindowStateAnimator wallpaperAnimator = wallpaperTarget.mWinAnimator;
938            if (wallpaperAnimator.mHasLocalTransformation &&
939                    wallpaperAnimator.mAnimation != null &&
940                    !wallpaperAnimator.mAnimation.getDetachWallpaper()) {
941                attachedTransformation = wallpaperAnimator.mTransformation;
942                if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
943                    Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
944                }
945            }
946            final AppWindowAnimator wpAppAnimator = wallpaperTarget.mAppToken == null ?
947                    null : wallpaperTarget.mAppToken.mAppAnimator;
948                if (wpAppAnimator != null && wpAppAnimator.hasTransformation
949                    && wpAppAnimator.animation != null
950                    && !wpAppAnimator.animation.getDetachWallpaper()) {
951                appTransformation = wpAppAnimator.transformation;
952                if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
953                    Slog.v(TAG, "WP target app xform: " + appTransformation);
954                }
955            }
956        }
957
958        final int displayId = mWin.getDisplayId();
959        final ScreenRotationAnimation screenRotationAnimation =
960                mAnimator.getScreenRotationAnimationLocked(displayId);
961        final boolean screenAnimation =
962                screenRotationAnimation != null && screenRotationAnimation.isAnimating();
963        if (selfTransformation || attachedTransformation != null
964                || appTransformation != null || screenAnimation) {
965            // cache often used attributes locally
966            final Rect frame = mWin.mFrame;
967            final float tmpFloats[] = mService.mTmpFloats;
968            final Matrix tmpMatrix = mWin.mTmpMatrix;
969
970            // Compute the desired transformation.
971            if (screenAnimation && screenRotationAnimation.isRotating()) {
972                // If we are doing a screen animation, the global rotation
973                // applied to windows can result in windows that are carefully
974                // aligned with each other to slightly separate, allowing you
975                // to see what is behind them.  An unsightly mess.  This...
976                // thing...  magically makes it call good: scale each window
977                // slightly (two pixels larger in each dimension, from the
978                // window's center).
979                final float w = frame.width();
980                final float h = frame.height();
981                if (w>=1 && h>=1) {
982                    tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
983                } else {
984                    tmpMatrix.reset();
985                }
986            } else {
987                tmpMatrix.reset();
988            }
989            tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
990            if (selfTransformation) {
991                tmpMatrix.postConcat(mTransformation.getMatrix());
992            }
993            tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
994            if (attachedTransformation != null) {
995                tmpMatrix.postConcat(attachedTransformation.getMatrix());
996            }
997            if (appTransformation != null) {
998                tmpMatrix.postConcat(appTransformation.getMatrix());
999            }
1000            if (mAnimator.mUniverseBackground != null) {
1001                tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
1002            }
1003            if (screenAnimation) {
1004                tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix());
1005            }
1006
1007            //TODO (multidisplay): Magnification is supported only for the default display.
1008            if (mService.mAccessibilityController != null && displayId == Display.DEFAULT_DISPLAY) {
1009                MagnificationSpec spec = mService.mAccessibilityController
1010                        .getMagnificationSpecForWindowLocked(mWin);
1011                if (spec != null && !spec.isNop()) {
1012                    tmpMatrix.postScale(spec.scale, spec.scale);
1013                    tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
1014                }
1015            }
1016
1017            // "convert" it into SurfaceFlinger's format
1018            // (a 2x2 matrix + an offset)
1019            // Here we must not transform the position of the surface
1020            // since it is already included in the transformation.
1021            //Slog.i(TAG, "Transform: " + matrix);
1022
1023            mHaveMatrix = true;
1024            tmpMatrix.getValues(tmpFloats);
1025            mDsDx = tmpFloats[Matrix.MSCALE_X];
1026            mDtDx = tmpFloats[Matrix.MSKEW_Y];
1027            mDsDy = tmpFloats[Matrix.MSKEW_X];
1028            mDtDy = tmpFloats[Matrix.MSCALE_Y];
1029            float x = tmpFloats[Matrix.MTRANS_X];
1030            float y = tmpFloats[Matrix.MTRANS_Y];
1031            int w = frame.width();
1032            int h = frame.height();
1033            mWin.mShownFrame.set(x, y, x+w, y+h);
1034
1035            // Now set the alpha...  but because our current hardware
1036            // can't do alpha transformation on a non-opaque surface,
1037            // turn it off if we are running an animation that is also
1038            // transforming since it is more important to have that
1039            // animation be smooth.
1040            mShownAlpha = mAlpha;
1041            mHasClipRect = false;
1042            if (!mService.mLimitedAlphaCompositing
1043                    || (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
1044                    || (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
1045                            && x == frame.left && y == frame.top))) {
1046                //Slog.i(TAG, "Applying alpha transform");
1047                if (selfTransformation) {
1048                    mShownAlpha *= mTransformation.getAlpha();
1049                }
1050                if (attachedTransformation != null) {
1051                    mShownAlpha *= attachedTransformation.getAlpha();
1052                }
1053                if (appTransformation != null) {
1054                    mShownAlpha *= appTransformation.getAlpha();
1055                    if (appTransformation.hasClipRect()) {
1056                        mClipRect.set(appTransformation.getClipRect());
1057                        mHasClipRect = true;
1058                    }
1059                }
1060                if (mAnimator.mUniverseBackground != null) {
1061                    mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
1062                }
1063                if (screenAnimation) {
1064                    mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
1065                }
1066            } else {
1067                //Slog.i(TAG, "Not applying alpha transform");
1068            }
1069
1070            if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV)
1071                    && (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
1072                    TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
1073                    + " self=" + (selfTransformation ? mTransformation.getAlpha() : "null")
1074                    + " attached=" + (attachedTransformation == null ?
1075                            "null" : attachedTransformation.getAlpha())
1076                    + " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha())
1077                    + " screen=" + (screenAnimation ?
1078                            screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
1079            return;
1080        } else if (mIsWallpaper && mService.mInnerFields.mWallpaperActionPending) {
1081            return;
1082        }
1083
1084        if (WindowManagerService.localLOGV) Slog.v(
1085                TAG, "computeShownFrameLocked: " + this +
1086                " not attached, mAlpha=" + mAlpha);
1087
1088        final boolean applyUniverseTransformation = (mAnimator.mUniverseBackground != null
1089                && mWin.mAttrs.type != WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
1090                && mWin.mBaseLayer < mAnimator.mAboveUniverseLayer);
1091        MagnificationSpec spec = null;
1092        //TODO (multidisplay): Magnification is supported only for the default display.
1093        if (mService.mAccessibilityController != null && displayId == Display.DEFAULT_DISPLAY) {
1094            spec = mService.mAccessibilityController.getMagnificationSpecForWindowLocked(mWin);
1095        }
1096        if (applyUniverseTransformation || spec != null) {
1097            final Rect frame = mWin.mFrame;
1098            final float tmpFloats[] = mService.mTmpFloats;
1099            final Matrix tmpMatrix = mWin.mTmpMatrix;
1100
1101            tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
1102            tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
1103
1104            if (applyUniverseTransformation) {
1105                tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
1106            }
1107
1108            if (spec != null && !spec.isNop()) {
1109                tmpMatrix.postScale(spec.scale, spec.scale);
1110                tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
1111            }
1112
1113            tmpMatrix.getValues(tmpFloats);
1114
1115            mHaveMatrix = true;
1116            mDsDx = tmpFloats[Matrix.MSCALE_X];
1117            mDtDx = tmpFloats[Matrix.MSKEW_Y];
1118            mDsDy = tmpFloats[Matrix.MSKEW_X];
1119            mDtDy = tmpFloats[Matrix.MSCALE_Y];
1120            float x = tmpFloats[Matrix.MTRANS_X];
1121            float y = tmpFloats[Matrix.MTRANS_Y];
1122            int w = frame.width();
1123            int h = frame.height();
1124            mWin.mShownFrame.set(x, y, x + w, y + h);
1125
1126            mShownAlpha = mAlpha;
1127            if (applyUniverseTransformation) {
1128                mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
1129            }
1130        } else {
1131            mWin.mShownFrame.set(mWin.mFrame);
1132            if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
1133                mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
1134            }
1135            mShownAlpha = mAlpha;
1136            mHaveMatrix = false;
1137            mDsDx = mWin.mGlobalScale;
1138            mDtDx = 0;
1139            mDsDy = 0;
1140            mDtDy = mWin.mGlobalScale;
1141        }
1142    }
1143
1144    void applyDecorRect(final Rect decorRect) {
1145        final WindowState w = mWin;
1146        final int width = w.mFrame.width();
1147        final int height = w.mFrame.height();
1148
1149        // Compute the offset of the window in relation to the decor rect.
1150        final int left = w.mXOffset + w.mFrame.left;
1151        final int top = w.mYOffset + w.mFrame.top;
1152
1153        // Initialize the decor rect to the entire frame.
1154        w.mSystemDecorRect.set(0, 0, width, height);
1155
1156        // Intersect with the decor rect, offsetted by window position.
1157        w.mSystemDecorRect.intersect(decorRect.left - left, decorRect.top - top,
1158                decorRect.right - left, decorRect.bottom - top);
1159
1160        // If size compatibility is being applied to the window, the
1161        // surface is scaled relative to the screen.  Also apply this
1162        // scaling to the crop rect.  We aren't using the standard rect
1163        // scale function because we want to round things to make the crop
1164        // always round to a larger rect to ensure we don't crop too
1165        // much and hide part of the window that should be seen.
1166        if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
1167            final float scale = w.mInvGlobalScale;
1168            w.mSystemDecorRect.left = (int) (w.mSystemDecorRect.left * scale - 0.5f);
1169            w.mSystemDecorRect.top = (int) (w.mSystemDecorRect.top * scale - 0.5f);
1170            w.mSystemDecorRect.right = (int) ((w.mSystemDecorRect.right+1) * scale - 0.5f);
1171            w.mSystemDecorRect.bottom = (int) ((w.mSystemDecorRect.bottom+1) * scale - 0.5f);
1172        }
1173    }
1174
1175    void updateSurfaceWindowCrop(final boolean recoveringMemory) {
1176        final WindowState w = mWin;
1177        final DisplayContent displayContent = w.getDisplayContent();
1178        if (displayContent == null) {
1179            return;
1180        }
1181
1182        // Need to recompute a new system decor rect each time.
1183        if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
1184            // Currently can't do this cropping for scaled windows.  We'll
1185            // just keep the crop rect the same as the source surface.
1186            w.mSystemDecorRect.set(0, 0, w.mRequestedWidth, w.mRequestedHeight);
1187        } else if (!w.isDefaultDisplay()) {
1188            // On a different display there is no system decor.  Crop the window
1189            // by the screen boundaries.
1190            final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1191            w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1192            w.mSystemDecorRect.intersect(-w.mCompatFrame.left, -w.mCompatFrame.top,
1193                    displayInfo.logicalWidth - w.mCompatFrame.left,
1194                    displayInfo.logicalHeight - w.mCompatFrame.top);
1195        } else if (w.mLayer >= mService.mSystemDecorLayer) {
1196            // Above the decor layer is easy, just use the entire window.
1197            // Unless we have a universe background...  in which case all the
1198            // windows need to be cropped by the screen, so they don't cover
1199            // the universe background.
1200            if (mAnimator.mUniverseBackground == null) {
1201                w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1202            } else {
1203                applyDecorRect(mService.mScreenRect);
1204            }
1205        } else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
1206                || w.mDecorFrame.isEmpty()) {
1207            // The universe background isn't cropped, nor windows without policy decor.
1208            w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
1209        } else {
1210            // Crop to the system decor specified by policy.
1211            applyDecorRect(w.mDecorFrame);
1212        }
1213
1214        // By default, the clip rect is the system decor.
1215        final Rect clipRect = mTmpClipRect;
1216        clipRect.set(w.mSystemDecorRect);
1217
1218        // Expand the clip rect for surface insets.
1219        final WindowManager.LayoutParams attrs = w.mAttrs;
1220        clipRect.left -= attrs.surfaceInsets.left;
1221        clipRect.top -= attrs.surfaceInsets.top;
1222        clipRect.right += attrs.surfaceInsets.right;
1223        clipRect.bottom += attrs.surfaceInsets.bottom;
1224
1225        // If we have an animated clip rect, intersect it with the clip rect.
1226        if (mHasClipRect) {
1227            // NOTE: We are adding a temporary workaround due to the status bar
1228            // not always reporting the correct system decor rect. In such
1229            // cases, we take into account the specified content insets as well.
1230            if ((w.mSystemUiVisibility & SYSTEM_UI_FLAGS_LAYOUT_STABLE_FULLSCREEN)
1231                    == SYSTEM_UI_FLAGS_LAYOUT_STABLE_FULLSCREEN) {
1232                // Don't apply the workaround to apps explicitly requesting
1233                // fullscreen layout.
1234                clipRect.intersect(mClipRect);
1235            } else {
1236                final int offsetTop = Math.max(clipRect.top, w.mContentInsets.top);
1237                clipRect.offset(0, -offsetTop);
1238                clipRect.intersect(mClipRect);
1239                clipRect.offset(0, offsetTop);
1240            }
1241        }
1242
1243        // The clip rect was generated assuming (0,0) as the window origin,
1244        // so we need to translate to match the actual surface coordinates.
1245        clipRect.offset(attrs.surfaceInsets.left, attrs.surfaceInsets.top);
1246
1247        if (!clipRect.equals(mLastClipRect)) {
1248            mLastClipRect.set(clipRect);
1249            try {
1250                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1251                        "CROP " + clipRect.toShortString(), null);
1252                mSurfaceControl.setWindowCrop(clipRect);
1253            } catch (RuntimeException e) {
1254                Slog.w(TAG, "Error setting crop surface of " + w
1255                        + " crop=" + clipRect.toShortString(), e);
1256                if (!recoveringMemory) {
1257                    mService.reclaimSomeSurfaceMemoryLocked(this, "crop", true);
1258                }
1259            }
1260        }
1261    }
1262
1263    void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
1264        final WindowState w = mWin;
1265
1266        int width;
1267        int height;
1268        if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
1269            // for a scaled surface, we always want the requested
1270            // size.
1271            width  = w.mRequestedWidth;
1272            height = w.mRequestedHeight;
1273        } else {
1274            width = w.mCompatFrame.width();
1275            height = w.mCompatFrame.height();
1276        }
1277
1278        // Something is wrong and SurfaceFlinger will not like this,
1279        // try to revert to sane values
1280        if (width < 1) {
1281            width = 1;
1282        }
1283        if (height < 1) {
1284            height = 1;
1285        }
1286
1287        float left = w.mShownFrame.left;
1288        float top = w.mShownFrame.top;
1289
1290        // Adjust for surface insets.
1291        final LayoutParams attrs = w.getAttrs();
1292        width += attrs.surfaceInsets.left + attrs.surfaceInsets.right;
1293        height += attrs.surfaceInsets.top + attrs.surfaceInsets.bottom;
1294        left -= attrs.surfaceInsets.left;
1295        top -= attrs.surfaceInsets.top;
1296
1297        final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top;
1298        if (surfaceMoved) {
1299            mSurfaceX = left;
1300            mSurfaceY = top;
1301
1302            try {
1303                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1304                        "POS " + left + ", " + top, null);
1305                mSurfaceControl.setPosition(left, top);
1306            } catch (RuntimeException e) {
1307                Slog.w(TAG, "Error positioning surface of " + w
1308                        + " pos=(" + left + "," + top + ")", e);
1309                if (!recoveringMemory) {
1310                    mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
1311                }
1312            }
1313        }
1314
1315        final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
1316        if (surfaceResized) {
1317            mSurfaceW = width;
1318            mSurfaceH = height;
1319            mSurfaceResized = true;
1320
1321            try {
1322                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1323                        "SIZE " + width + "x" + height, null);
1324                mSurfaceControl.setSize(width, height);
1325                mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1326                        WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
1327                if ((w.mAttrs.flags & LayoutParams.FLAG_DIM_BEHIND) != 0) {
1328                    w.getStack().startDimmingIfNeeded(this);
1329                }
1330            } catch (RuntimeException e) {
1331                // If something goes wrong with the surface (such
1332                // as running out of memory), don't take down the
1333                // entire system.
1334                Slog.e(TAG, "Error resizing surface of " + w
1335                        + " size=(" + width + "x" + height + ")", e);
1336                if (!recoveringMemory) {
1337                    mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
1338                }
1339            }
1340        }
1341
1342        updateSurfaceWindowCrop(recoveringMemory);
1343    }
1344
1345    public void prepareSurfaceLocked(final boolean recoveringMemory) {
1346        final WindowState w = mWin;
1347        if (mSurfaceControl == null) {
1348            if (w.mOrientationChanging) {
1349                if (DEBUG_ORIENTATION) {
1350                    Slog.v(TAG, "Orientation change skips hidden " + w);
1351                }
1352                w.mOrientationChanging = false;
1353            }
1354            return;
1355        }
1356
1357        boolean displayed = false;
1358
1359        computeShownFrameLocked();
1360
1361        setSurfaceBoundariesLocked(recoveringMemory);
1362
1363        if (mIsWallpaper && !mWin.mWallpaperVisible) {
1364            // Wallpaper is no longer visible and there is no wp target => hide it.
1365            hide();
1366        } else if (w.mAttachedHidden || !w.isOnScreen()) {
1367            hide();
1368            mAnimator.hideWallpapersLocked(w);
1369
1370            // If we are waiting for this window to handle an
1371            // orientation change, well, it is hidden, so
1372            // doesn't really matter.  Note that this does
1373            // introduce a potential glitch if the window
1374            // becomes unhidden before it has drawn for the
1375            // new orientation.
1376            if (w.mOrientationChanging) {
1377                w.mOrientationChanging = false;
1378                if (DEBUG_ORIENTATION) Slog.v(TAG,
1379                        "Orientation change skips hidden " + w);
1380            }
1381        } else if (mLastLayer != mAnimLayer
1382                || mLastAlpha != mShownAlpha
1383                || mLastDsDx != mDsDx
1384                || mLastDtDx != mDtDx
1385                || mLastDsDy != mDsDy
1386                || mLastDtDy != mDtDy
1387                || w.mLastHScale != w.mHScale
1388                || w.mLastVScale != w.mVScale
1389                || mLastHidden) {
1390            displayed = true;
1391            mLastAlpha = mShownAlpha;
1392            mLastLayer = mAnimLayer;
1393            mLastDsDx = mDsDx;
1394            mLastDtDx = mDtDx;
1395            mLastDsDy = mDsDy;
1396            mLastDtDy = mDtDy;
1397            w.mLastHScale = w.mHScale;
1398            w.mLastVScale = w.mVScale;
1399            if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1400                    "alpha=" + mShownAlpha + " layer=" + mAnimLayer
1401                    + " matrix=[" + (mDsDx*w.mHScale)
1402                    + "," + (mDtDx*w.mVScale)
1403                    + "][" + (mDsDy*w.mHScale)
1404                    + "," + (mDtDy*w.mVScale) + "]", null);
1405            if (mSurfaceControl != null) {
1406                try {
1407                    mSurfaceAlpha = mShownAlpha;
1408                    mSurfaceControl.setAlpha(mShownAlpha);
1409                    mSurfaceLayer = mAnimLayer;
1410                    mSurfaceControl.setLayer(mAnimLayer);
1411                    mSurfaceControl.setMatrix(
1412                            mDsDx * w.mHScale, mDtDx * w.mVScale,
1413                            mDsDy * w.mHScale, mDtDy * w.mVScale);
1414
1415                    if (mLastHidden && mDrawState == HAS_DRAWN) {
1416                        if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
1417                                "SHOW (performLayout)", null);
1418                        if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
1419                                + " during relayout");
1420                        if (showSurfaceRobustlyLocked()) {
1421                            mLastHidden = false;
1422                            if (mIsWallpaper) {
1423                                mService.dispatchWallpaperVisibility(w, true);
1424                            }
1425                            // This draw means the difference between unique content and mirroring.
1426                            // Run another pass through performLayout to set mHasContent in the
1427                            // LogicalDisplay.
1428                            mAnimator.setPendingLayoutChanges(w.getDisplayId(),
1429                                    WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
1430                        } else {
1431                            w.mOrientationChanging = false;
1432                        }
1433                    }
1434                    if (mSurfaceControl != null) {
1435                        w.mToken.hasVisible = true;
1436                    }
1437                } catch (RuntimeException e) {
1438                    Slog.w(TAG, "Error updating surface in " + w, e);
1439                    if (!recoveringMemory) {
1440                        mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
1441                    }
1442                }
1443            }
1444        } else {
1445            if (DEBUG_ANIM && isAnimating()) {
1446                Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
1447            }
1448            displayed = true;
1449        }
1450
1451        if (displayed) {
1452            if (w.mOrientationChanging) {
1453                if (!w.isDrawnLw()) {
1454                    mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
1455                    mAnimator.mLastWindowFreezeSource = w;
1456                    if (DEBUG_ORIENTATION) Slog.v(TAG,
1457                            "Orientation continue waiting for draw in " + w);
1458                } else {
1459                    w.mOrientationChanging = false;
1460                    if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
1461                }
1462            }
1463            w.mToken.hasVisible = true;
1464        }
1465    }
1466
1467    void setTransparentRegionHintLocked(final Region region) {
1468        if (mSurfaceControl == null) {
1469            Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
1470            return;
1471        }
1472        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion");
1473        SurfaceControl.openTransaction();
1474        try {
1475            if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1476                    "transparentRegionHint=" + region, null);
1477            mSurfaceControl.setTransparentRegionHint(region);
1478        } finally {
1479            SurfaceControl.closeTransaction();
1480            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1481                    "<<< CLOSE TRANSACTION setTransparentRegion");
1482        }
1483    }
1484
1485    void setWallpaperOffset(RectF shownFrame) {
1486        final int left = (int) shownFrame.left;
1487        final int top = (int) shownFrame.top;
1488        if (mSurfaceX != left || mSurfaceY != top) {
1489            mSurfaceX = left;
1490            mSurfaceY = top;
1491            if (mAnimating) {
1492                // If this window (or its app token) is animating, then the position
1493                // of the surface will be re-computed on the next animation frame.
1494                // We can't poke it directly here because it depends on whatever
1495                // transformation is being applied by the animation.
1496                return;
1497            }
1498            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setWallpaperOffset");
1499            SurfaceControl.openTransaction();
1500            try {
1501                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
1502                        "POS " + left + ", " + top, null);
1503                mSurfaceControl.setPosition(mWin.mFrame.left + left, mWin.mFrame.top + top);
1504                updateSurfaceWindowCrop(false);
1505            } catch (RuntimeException e) {
1506                Slog.w(TAG, "Error positioning surface of " + mWin
1507                        + " pos=(" + left + "," + top + ")", e);
1508            } finally {
1509                SurfaceControl.closeTransaction();
1510                if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
1511                        "<<< CLOSE TRANSACTION setWallpaperOffset");
1512            }
1513        }
1514    }
1515
1516    void setOpaque(boolean isOpaque) {
1517        if (mSurfaceControl == null) {
1518            return;
1519        }
1520        if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaque");
1521        SurfaceControl.openTransaction();
1522        try {
1523            if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin, "isOpaque=" + isOpaque,
1524                    null);
1525            mSurfaceControl.setOpaque(isOpaque);
1526        } finally {
1527            SurfaceControl.closeTransaction();
1528            if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaque");
1529        }
1530    }
1531
1532    // This must be called while inside a transaction.
1533    boolean performShowLocked() {
1534        if (mWin.isHiddenFromUserLocked()) {
1535            Slog.w(TAG, "current user violation " + mService.mCurrentUserId + " trying to display "
1536                    + this + ", type " + mWin.mAttrs.type + ", belonging to " + mWin.mOwnerUid);
1537            return false;
1538        }
1539        if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
1540                mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
1541            RuntimeException e = null;
1542            if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1543                e = new RuntimeException();
1544                e.fillInStackTrace();
1545            }
1546            Slog.v(TAG, "performShow on " + this
1547                    + ": mDrawState=" + mDrawState + " readyForDisplay="
1548                    + mWin.isReadyForDisplayIgnoringKeyguard()
1549                    + " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING)
1550                    + " during animation: policyVis=" + mWin.mPolicyVisibility
1551                    + " attHidden=" + mWin.mAttachedHidden
1552                    + " tok.hiddenRequested="
1553                    + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1554                    + " tok.hidden="
1555                    + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1556                    + " animating=" + mAnimating
1557                    + " tok animating="
1558                    + (mAppAnimator != null ? mAppAnimator.animating : false), e);
1559        }
1560        if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplayIgnoringKeyguard()) {
1561            if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
1562                WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
1563            if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
1564                    mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
1565                Slog.v(TAG, "Showing " + this
1566                        + " during animation: policyVis=" + mWin.mPolicyVisibility
1567                        + " attHidden=" + mWin.mAttachedHidden
1568                        + " tok.hiddenRequested="
1569                        + (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
1570                        + " tok.hidden="
1571                        + (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
1572                        + " animating=" + mAnimating
1573                        + " tok animating="
1574                        + (mAppAnimator != null ? mAppAnimator.animating : false));
1575            }
1576
1577            mService.enableScreenIfNeededLocked();
1578
1579            applyEnterAnimationLocked();
1580
1581            // Force the show in the next prepareSurfaceLocked() call.
1582            mLastAlpha = -1;
1583            if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
1584                Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + this);
1585            mDrawState = HAS_DRAWN;
1586            mService.scheduleAnimationLocked();
1587
1588            int i = mWin.mChildWindows.size();
1589            while (i > 0) {
1590                i--;
1591                WindowState c = mWin.mChildWindows.get(i);
1592                if (c.mAttachedHidden) {
1593                    c.mAttachedHidden = false;
1594                    if (c.mWinAnimator.mSurfaceControl != null) {
1595                        c.mWinAnimator.performShowLocked();
1596                        // It hadn't been shown, which means layout not
1597                        // performed on it, so now we want to make sure to
1598                        // do a layout.  If called from within the transaction
1599                        // loop, this will cause it to restart with a new
1600                        // layout.
1601                        final DisplayContent displayContent = c.getDisplayContent();
1602                        if (displayContent != null) {
1603                            displayContent.layoutNeeded = true;
1604                        }
1605                    }
1606                }
1607            }
1608
1609            if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
1610                    && mWin.mAppToken != null) {
1611                mWin.mAppToken.firstWindowDrawn = true;
1612
1613                if (mWin.mAppToken.startingData != null) {
1614                    if (WindowManagerService.DEBUG_STARTING_WINDOW ||
1615                            WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
1616                            "Finish starting " + mWin.mToken
1617                            + ": first real window is shown, no animation");
1618                    // If this initial window is animating, stop it -- we
1619                    // will do an animation to reveal it from behind the
1620                    // starting window, so there is no need for it to also
1621                    // be doing its own stuff.
1622                    clearAnimation();
1623                    mService.mFinishedStarting.add(mWin.mAppToken);
1624                    mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
1625                }
1626                mWin.mAppToken.updateReportedVisibilityLocked();
1627            }
1628
1629            return true;
1630        }
1631
1632        return false;
1633    }
1634
1635    /**
1636     * Have the surface flinger show a surface, robustly dealing with
1637     * error conditions.  In particular, if there is not enough memory
1638     * to show the surface, then we will try to get rid of other surfaces
1639     * in order to succeed.
1640     *
1641     * @return Returns true if the surface was successfully shown.
1642     */
1643    boolean showSurfaceRobustlyLocked() {
1644        try {
1645            if (mSurfaceControl != null) {
1646                mSurfaceShown = true;
1647                mSurfaceControl.show();
1648                if (mWin.mTurnOnScreen) {
1649                    if (DEBUG_VISIBILITY) Slog.v(TAG,
1650                            "Show surface turning screen on: " + mWin);
1651                    mWin.mTurnOnScreen = false;
1652                    mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
1653                }
1654            }
1655            return true;
1656        } catch (RuntimeException e) {
1657            Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + mWin, e);
1658        }
1659
1660        mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
1661
1662        return false;
1663    }
1664
1665    void applyEnterAnimationLocked() {
1666        final int transit;
1667        if (mEnterAnimationPending) {
1668            mEnterAnimationPending = false;
1669            transit = WindowManagerPolicy.TRANSIT_ENTER;
1670        } else {
1671            transit = WindowManagerPolicy.TRANSIT_SHOW;
1672        }
1673        applyAnimationLocked(transit, true);
1674        //TODO (multidisplay): Magnification is supported only for the default display.
1675        if (mService.mAccessibilityController != null
1676                && mWin.getDisplayId() == Display.DEFAULT_DISPLAY) {
1677            mService.mAccessibilityController.onWindowTransitionLocked(mWin, transit);
1678        }
1679    }
1680
1681    /**
1682     * Choose the correct animation and set it to the passed WindowState.
1683     * @param transit If AppTransition.TRANSIT_PREVIEW_DONE and the app window has been drawn
1684     *      then the animation will be app_starting_exit. Any other value loads the animation from
1685     *      the switch statement below.
1686     * @param isEntrance The animation type the last time this was called. Used to keep from
1687     *      loading the same animation twice.
1688     * @return true if an animation has been loaded.
1689     */
1690    boolean applyAnimationLocked(int transit, boolean isEntrance) {
1691        if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
1692            // If we are trying to apply an animation, but already running
1693            // an animation of the same type, then just leave that one alone.
1694            return true;
1695        }
1696
1697        // Only apply an animation if the display isn't frozen.  If it is
1698        // frozen, there is no reason to animate and it can cause strange
1699        // artifacts when we unfreeze the display if some different animation
1700        // is running.
1701        if (mService.okToDisplay()) {
1702            int anim = mPolicy.selectAnimationLw(mWin, transit);
1703            int attr = -1;
1704            Animation a = null;
1705            if (anim != 0) {
1706                a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null;
1707            } else {
1708                switch (transit) {
1709                    case WindowManagerPolicy.TRANSIT_ENTER:
1710                        attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
1711                        break;
1712                    case WindowManagerPolicy.TRANSIT_EXIT:
1713                        attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
1714                        break;
1715                    case WindowManagerPolicy.TRANSIT_SHOW:
1716                        attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
1717                        break;
1718                    case WindowManagerPolicy.TRANSIT_HIDE:
1719                        attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
1720                        break;
1721                }
1722                if (attr >= 0) {
1723                    a = mService.mAppTransition.loadAnimationAttr(mWin.mAttrs, attr);
1724                }
1725            }
1726            if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
1727                    "applyAnimation: win=" + this
1728                    + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
1729                    + " a=" + a
1730                    + " transit=" + transit
1731                    + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
1732            if (a != null) {
1733                if (WindowManagerService.DEBUG_ANIM) {
1734                    RuntimeException e = null;
1735                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
1736                        e = new RuntimeException();
1737                        e.fillInStackTrace();
1738                    }
1739                    Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
1740                }
1741                setAnimation(a);
1742                mAnimationIsEntrance = isEntrance;
1743            }
1744        } else {
1745            clearAnimation();
1746        }
1747
1748        return mAnimation != null;
1749    }
1750
1751    public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1752        if (mAnimating || mLocalAnimating || mAnimationIsEntrance
1753                || mAnimation != null) {
1754            pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
1755                    pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
1756                    pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
1757                    pw.print(" mAnimation="); pw.println(mAnimation);
1758        }
1759        if (mHasTransformation || mHasLocalTransformation) {
1760            pw.print(prefix); pw.print("XForm: has=");
1761                    pw.print(mHasTransformation);
1762                    pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
1763                    pw.print(" "); mTransformation.printShortString(pw);
1764                    pw.println();
1765        }
1766        if (mSurfaceControl != null) {
1767            if (dumpAll) {
1768                pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl);
1769                pw.print(prefix); pw.print("mDrawState=");
1770                pw.print(drawStateToString(mDrawState));
1771                pw.print(" mLastHidden="); pw.println(mLastHidden);
1772            }
1773            pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
1774                    pw.print(" layer="); pw.print(mSurfaceLayer);
1775                    pw.print(" alpha="); pw.print(mSurfaceAlpha);
1776                    pw.print(" rect=("); pw.print(mSurfaceX);
1777                    pw.print(","); pw.print(mSurfaceY);
1778                    pw.print(") "); pw.print(mSurfaceW);
1779                    pw.print(" x "); pw.println(mSurfaceH);
1780        }
1781        if (mPendingDestroySurface != null) {
1782            pw.print(prefix); pw.print("mPendingDestroySurface=");
1783                    pw.println(mPendingDestroySurface);
1784        }
1785        if (mSurfaceResized || mSurfaceDestroyDeferred) {
1786            pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
1787                    pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
1788        }
1789        if (mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND) {
1790            pw.print(prefix); pw.print("mUniverseTransform=");
1791                    mUniverseTransform.printShortString(pw);
1792                    pw.println();
1793        }
1794        if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
1795            pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
1796                    pw.print(" mAlpha="); pw.print(mAlpha);
1797                    pw.print(" mLastAlpha="); pw.println(mLastAlpha);
1798        }
1799        if (mHaveMatrix || mWin.mGlobalScale != 1) {
1800            pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
1801                    pw.print(" mDsDx="); pw.print(mDsDx);
1802                    pw.print(" mDtDx="); pw.print(mDtDx);
1803                    pw.print(" mDsDy="); pw.print(mDsDy);
1804                    pw.print(" mDtDy="); pw.println(mDtDy);
1805        }
1806    }
1807
1808    @Override
1809    public String toString() {
1810        StringBuffer sb = new StringBuffer("WindowStateAnimator{");
1811        sb.append(Integer.toHexString(System.identityHashCode(this)));
1812        sb.append(' ');
1813        sb.append(mWin.mAttrs.getTitle());
1814        sb.append('}');
1815        return sb.toString();
1816    }
1817}
1818