AppWindowAnimator.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 android.graphics.Matrix;
20import android.os.RemoteException;
21import android.util.Slog;
22import android.util.TimeUtils;
23import android.view.Display;
24import android.view.SurfaceControl;
25import android.view.WindowManagerPolicy;
26import android.view.animation.Animation;
27import android.view.animation.Transformation;
28
29import java.io.PrintWriter;
30import java.util.ArrayList;
31
32public class AppWindowAnimator {
33    static final String TAG = "AppWindowAnimator";
34
35    final AppWindowToken mAppToken;
36    final WindowManagerService mService;
37    final WindowAnimator mAnimator;
38
39    boolean animating;
40    Animation animation;
41    boolean hasTransformation;
42    final Transformation transformation = new Transformation();
43
44    // Have we been asked to have this token keep the screen frozen?
45    // Protect with mAnimator.
46    boolean freezingScreen;
47
48    /**
49     * How long we last kept the screen frozen.
50     */
51    int lastFreezeDuration;
52
53    // Offset to the window of all layers in the token, for use by
54    // AppWindowToken animations.
55    int animLayerAdjustment;
56
57    // Propagated from AppWindowToken.allDrawn, to determine when
58    // the state changes.
59    boolean allDrawn;
60
61    // Special surface for thumbnail animation.
62    SurfaceControl thumbnail;
63    int thumbnailTransactionSeq;
64    int thumbnailX;
65    int thumbnailY;
66    int thumbnailLayer;
67    int thumbnailForceAboveLayer;
68    Animation thumbnailAnimation;
69    final Transformation thumbnailTransformation = new Transformation();
70    // This flag indicates that the destruction of the thumbnail surface is synchronized with
71    // another animation, so do not pre-emptively destroy the thumbnail surface when the animation
72    // completes
73    boolean deferThumbnailDestruction;
74    // This is the thumbnail surface that has been bestowed upon this animator, and when the
75    // surface for this animator's animation is complete, we will destroy the thumbnail surface
76    // as well.  Do not animate or do anything with this surface.
77    SurfaceControl deferredThumbnail;
78
79    /** WindowStateAnimator from mAppAnimator.allAppWindows as of last performLayout */
80    ArrayList<WindowStateAnimator> mAllAppWinAnimators = new ArrayList<WindowStateAnimator>();
81
82    static final Animation sDummyAnimation = new DummyAnimation();
83
84    public AppWindowAnimator(final AppWindowToken atoken) {
85        mAppToken = atoken;
86        mService = atoken.service;
87        mAnimator = atoken.mAnimator;
88    }
89
90    public void setAnimation(Animation anim, int width, int height) {
91        if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting animation in " + mAppToken
92                + ": " + anim + " wxh=" + width + "x" + height
93                + " isVisible=" + mAppToken.isVisible());
94        animation = anim;
95        animating = false;
96        if (!anim.isInitialized()) {
97            anim.initialize(width, height, width, height);
98        }
99        anim.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
100        anim.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
101        int zorder = anim.getZAdjustment();
102        int adj = 0;
103        if (zorder == Animation.ZORDER_TOP) {
104            adj = WindowManagerService.TYPE_LAYER_OFFSET;
105        } else if (zorder == Animation.ZORDER_BOTTOM) {
106            adj = -WindowManagerService.TYPE_LAYER_OFFSET;
107        }
108
109        if (animLayerAdjustment != adj) {
110            animLayerAdjustment = adj;
111            updateLayers();
112        }
113        // Start out animation gone if window is gone, or visible if window is visible.
114        transformation.clear();
115        transformation.setAlpha(mAppToken.isVisible() ? 1 : 0);
116        hasTransformation = true;
117    }
118
119    public void setDummyAnimation() {
120        if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting dummy animation in " + mAppToken
121                + " isVisible=" + mAppToken.isVisible());
122        animation = sDummyAnimation;
123        hasTransformation = true;
124        transformation.clear();
125        transformation.setAlpha(mAppToken.isVisible() ? 1 : 0);
126    }
127
128    public void clearAnimation() {
129        if (animation != null) {
130            animation = null;
131            animating = true;
132        }
133        if (!deferThumbnailDestruction) {
134            clearThumbnail();
135        }
136        if (mAppToken.deferClearAllDrawn) {
137            mAppToken.allDrawn = false;
138            mAppToken.deferClearAllDrawn = false;
139        }
140    }
141
142    public void clearThumbnail() {
143        if (thumbnail != null) {
144            thumbnail.destroy();
145            thumbnail = null;
146        }
147    }
148
149    public void clearDeferredThumbnail() {
150        if (deferredThumbnail != null) {
151            deferredThumbnail.destroy();
152            deferredThumbnail = null;
153        }
154    }
155
156    void updateLayers() {
157        final int N = mAppToken.allAppWindows.size();
158        final int adj = animLayerAdjustment;
159        thumbnailLayer = -1;
160        for (int i=0; i<N; i++) {
161            final WindowState w = mAppToken.allAppWindows.get(i);
162            final WindowStateAnimator winAnimator = w.mWinAnimator;
163            winAnimator.mAnimLayer = w.mLayer + adj;
164            if (winAnimator.mAnimLayer > thumbnailLayer) {
165                thumbnailLayer = winAnimator.mAnimLayer;
166            }
167            if (WindowManagerService.DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
168                    + winAnimator.mAnimLayer);
169            if (w == mService.mInputMethodTarget && !mService.mInputMethodTargetWaitingAnim) {
170                mService.setInputMethodAnimLayerAdjustment(adj);
171            }
172            if (w == mService.mWallpaperTarget && mService.mLowerWallpaperTarget == null) {
173                mService.setWallpaperAnimLayerAdjustmentLocked(adj);
174            }
175        }
176    }
177
178    private void stepThumbnailAnimation(long currentTime) {
179        thumbnailTransformation.clear();
180        thumbnailAnimation.getTransformation(currentTime, thumbnailTransformation);
181        thumbnailTransformation.getMatrix().preTranslate(thumbnailX, thumbnailY);
182
183        ScreenRotationAnimation screenRotationAnimation =
184                mAnimator.getScreenRotationAnimationLocked(Display.DEFAULT_DISPLAY);
185        final boolean screenAnimation = screenRotationAnimation != null
186                && screenRotationAnimation.isAnimating();
187        if (screenAnimation) {
188            thumbnailTransformation.postCompose(screenRotationAnimation.getEnterTransformation());
189        }
190        // cache often used attributes locally
191        final float tmpFloats[] = mService.mTmpFloats;
192        thumbnailTransformation.getMatrix().getValues(tmpFloats);
193        if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail,
194                "thumbnail", "POS " + tmpFloats[Matrix.MTRANS_X]
195                + ", " + tmpFloats[Matrix.MTRANS_Y], null);
196        thumbnail.setPosition(tmpFloats[Matrix.MTRANS_X], tmpFloats[Matrix.MTRANS_Y]);
197        if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail,
198                "thumbnail", "alpha=" + thumbnailTransformation.getAlpha()
199                + " layer=" + thumbnailLayer
200                + " matrix=[" + tmpFloats[Matrix.MSCALE_X]
201                + "," + tmpFloats[Matrix.MSKEW_Y]
202                + "][" + tmpFloats[Matrix.MSKEW_X]
203                + "," + tmpFloats[Matrix.MSCALE_Y] + "]", null);
204        thumbnail.setAlpha(thumbnailTransformation.getAlpha());
205        if (thumbnailForceAboveLayer > 0) {
206            thumbnail.setLayer(thumbnailForceAboveLayer + 1);
207        } else {
208            // The thumbnail is layered below the window immediately above this
209            // token's anim layer.
210            thumbnail.setLayer(thumbnailLayer + WindowManagerService.WINDOW_LAYER_MULTIPLIER
211                    - WindowManagerService.LAYER_OFFSET_THUMBNAIL);
212        }
213        thumbnail.setMatrix(tmpFloats[Matrix.MSCALE_X], tmpFloats[Matrix.MSKEW_Y],
214                tmpFloats[Matrix.MSKEW_X], tmpFloats[Matrix.MSCALE_Y]);
215    }
216
217    private boolean stepAnimation(long currentTime) {
218        if (animation == null) {
219            return false;
220        }
221        transformation.clear();
222        final boolean more = animation.getTransformation(currentTime, transformation);
223        if (false && WindowManagerService.DEBUG_ANIM) Slog.v(
224            TAG, "Stepped animation in " + mAppToken + ": more=" + more + ", xform=" + transformation);
225        if (!more) {
226            animation = null;
227            if (!deferThumbnailDestruction) {
228                clearThumbnail();
229            }
230            if (WindowManagerService.DEBUG_ANIM) Slog.v(
231                TAG, "Finished animation in " + mAppToken + " @ " + currentTime);
232        }
233        hasTransformation = more;
234        return more;
235    }
236
237    // This must be called while inside a transaction.
238    boolean stepAnimationLocked(long currentTime) {
239        if (mService.okToDisplay()) {
240            // We will run animations as long as the display isn't frozen.
241
242            if (animation == sDummyAnimation) {
243                // This guy is going to animate, but not yet.  For now count
244                // it as not animating for purposes of scheduling transactions;
245                // when it is really time to animate, this will be set to
246                // a real animation and the next call will execute normally.
247                return false;
248            }
249
250            if ((mAppToken.allDrawn || animating || mAppToken.startingDisplayed)
251                    && animation != null) {
252                if (!animating) {
253                    if (WindowManagerService.DEBUG_ANIM) Slog.v(
254                        TAG, "Starting animation in " + mAppToken +
255                        " @ " + currentTime + " scale="
256                        + mService.getTransitionAnimationScaleLocked()
257                        + " allDrawn=" + mAppToken.allDrawn + " animating=" + animating);
258                    animation.setStartTime(currentTime);
259                    animating = true;
260                    if (thumbnail != null) {
261                        thumbnail.show();
262                        thumbnailAnimation.setStartTime(currentTime);
263                    }
264                }
265                if (stepAnimation(currentTime)) {
266                    // animation isn't over, step any thumbnail and that's
267                    // it for now.
268                    if (thumbnail != null) {
269                        stepThumbnailAnimation(currentTime);
270                    }
271                    return true;
272                }
273            }
274        } else if (animation != null) {
275            // If the display is frozen, and there is a pending animation,
276            // clear it and make sure we run the cleanup code.
277            animating = true;
278            animation = null;
279        }
280
281        hasTransformation = false;
282
283        if (!animating && animation == null) {
284            return false;
285        }
286
287        mAnimator.setAppLayoutChanges(this, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM,
288                "AppWindowToken");
289
290        clearAnimation();
291        animating = false;
292        if (animLayerAdjustment != 0) {
293            animLayerAdjustment = 0;
294            updateLayers();
295        }
296        if (mService.mInputMethodTarget != null
297                && mService.mInputMethodTarget.mAppToken == mAppToken) {
298            mService.moveInputMethodWindowsIfNeededLocked(true);
299        }
300
301        if (WindowManagerService.DEBUG_ANIM) Slog.v(
302                TAG, "Animation done in " + mAppToken
303                + ": reportedVisible=" + mAppToken.reportedVisible);
304
305        transformation.clear();
306
307        final int N = mAllAppWinAnimators.size();
308        for (int i=0; i<N; i++) {
309            final WindowStateAnimator winAnim = mAllAppWinAnimators.get(i);
310            if (mAppToken.mLaunchTaskBehind) {
311                winAnim.mWin.mExiting = true;
312            }
313            winAnim.finishExit();
314        }
315        if (mAppToken.mLaunchTaskBehind) {
316            try {
317                mService.mActivityManager.notifyLaunchTaskBehindComplete(mAppToken.token);
318            } catch (RemoteException e) {
319            }
320            mAppToken.mLaunchTaskBehind = false;
321        } else {
322            mAppToken.updateReportedVisibilityLocked();
323            if (mAppToken.mEnteringAnimation) {
324                mAppToken.mEnteringAnimation = false;
325                try {
326                    mService.mActivityManager.notifyEnterAnimationComplete(mAppToken.token);
327                } catch (RemoteException e) {
328                }
329            }
330        }
331
332        return false;
333    }
334
335    boolean showAllWindowsLocked() {
336        boolean isAnimating = false;
337        final int NW = mAllAppWinAnimators.size();
338        for (int i=0; i<NW; i++) {
339            WindowStateAnimator winAnimator = mAllAppWinAnimators.get(i);
340            if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
341                    "performing show on: " + winAnimator);
342            winAnimator.performShowLocked();
343            isAnimating |= winAnimator.isAnimating();
344        }
345        return isAnimating;
346    }
347
348    void dump(PrintWriter pw, String prefix, boolean dumpAll) {
349        pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
350        pw.print(prefix); pw.print("mAnimator="); pw.println(mAnimator);
351        pw.print(prefix); pw.print("freezingScreen="); pw.print(freezingScreen);
352                pw.print(" allDrawn="); pw.print(allDrawn);
353                pw.print(" animLayerAdjustment="); pw.println(animLayerAdjustment);
354        if (lastFreezeDuration != 0) {
355            pw.print(prefix); pw.print("lastFreezeDuration=");
356                    TimeUtils.formatDuration(lastFreezeDuration, pw); pw.println();
357        }
358        if (animating || animation != null) {
359            pw.print(prefix); pw.print("animating="); pw.println(animating);
360            pw.print(prefix); pw.print("animation="); pw.println(animation);
361        }
362        if (hasTransformation) {
363            pw.print(prefix); pw.print("XForm: ");
364                    transformation.printShortString(pw);
365                    pw.println();
366        }
367        if (thumbnail != null) {
368            pw.print(prefix); pw.print("thumbnail="); pw.print(thumbnail);
369                    pw.print(" x="); pw.print(thumbnailX);
370                    pw.print(" y="); pw.print(thumbnailY);
371                    pw.print(" layer="); pw.println(thumbnailLayer);
372            pw.print(prefix); pw.print("thumbnailAnimation="); pw.println(thumbnailAnimation);
373            pw.print(prefix); pw.print("thumbnailTransformation=");
374                    pw.println(thumbnailTransformation.toShortString());
375        }
376        for (int i=0; i<mAllAppWinAnimators.size(); i++) {
377            WindowStateAnimator wanim = mAllAppWinAnimators.get(i);
378            pw.print(prefix); pw.print("App Win Anim #"); pw.print(i);
379                    pw.print(": "); pw.println(wanim);
380        }
381    }
382
383    // This is an animation that does nothing: it just immediately finishes
384    // itself every time it is called.  It is used as a stub animation in cases
385    // where we want to synchronize multiple things that may be animating.
386    static final class DummyAnimation extends Animation {
387        @Override
388        public boolean getTransformation(long currentTime, Transformation outTransformation) {
389            return false;
390        }
391    }
392
393}
394