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