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