AppWindowAnimator.java revision 1a4dfe593aafda057ac9cb3086b84588d88cd09f
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 com.android.server.wm.WindowManagerService.DEBUG_ANIM;
20import static com.android.server.wm.WindowManagerService.DEBUG_LAYERS;
21import static com.android.server.wm.WindowManagerService.SHOW_TRANSACTIONS;
22import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
23
24import android.graphics.Matrix;
25import android.util.Slog;
26import android.util.TimeUtils;
27import android.view.Choreographer;
28import android.view.Display;
29import android.view.SurfaceControl;
30import android.view.WindowManagerPolicy;
31import android.view.animation.Animation;
32import android.view.animation.Transformation;
33
34import java.io.PrintWriter;
35import java.util.ArrayList;
36
37public class AppWindowAnimator {
38    static final String TAG = "AppWindowAnimator";
39
40    private static final int PROLONG_ANIMATION_DISABLED = 0;
41    static final int PROLONG_ANIMATION_AT_END = 1;
42    static final int PROLONG_ANIMATION_AT_START = 2;
43
44    final AppWindowToken mAppToken;
45    final WindowManagerService mService;
46    final WindowAnimator mAnimator;
47
48    boolean animating;
49    boolean wasAnimating;
50    Animation animation;
51    boolean hasTransformation;
52    final Transformation transformation = new Transformation();
53
54    // Have we been asked to have this token keep the screen frozen?
55    // Protect with mAnimator.
56    boolean freezingScreen;
57
58    /**
59     * How long we last kept the screen frozen.
60     */
61    int lastFreezeDuration;
62
63    // Offset to the window of all layers in the token, for use by
64    // AppWindowToken animations.
65    int animLayerAdjustment;
66
67    // Propagated from AppWindowToken.allDrawn, to determine when
68    // the state changes.
69    boolean allDrawn;
70
71    // Special surface for thumbnail animation.  If deferThumbnailDestruction is enabled, then we
72    // will make sure that the thumbnail is destroyed after the other surface is completed.  This
73    // requires that the duration of the two animations are the same.
74    SurfaceControl thumbnail;
75    int thumbnailTransactionSeq;
76    int thumbnailX;
77    int thumbnailY;
78    int thumbnailLayer;
79    int thumbnailForceAboveLayer;
80    Animation thumbnailAnimation;
81    final Transformation thumbnailTransformation = new Transformation();
82    // This flag indicates that the destruction of the thumbnail surface is synchronized with
83    // another animation, so defer the destruction of this thumbnail surface for a single frame
84    // after the secondary animation completes.
85    boolean deferThumbnailDestruction;
86    // This flag is set if the animator has deferThumbnailDestruction set and has reached the final
87    // frame of animation.  It will extend the animation by one frame and then clean up afterwards.
88    boolean deferFinalFrameCleanup;
89    // If true when the animation hits the last frame, it will keep running on that last frame.
90    // This is used to synchronize animation with Recents and we wait for Recents to tell us to
91    // finish or for a new animation be set as fail-safe mechanism.
92    private int mProlongAnimation;
93    // Whether the prolong animation can be removed when animation is set. The purpose of this is
94    // that if recents doesn't tell us to remove the prolonged animation, we will get rid of it
95    // when new animation is set.
96    private boolean mClearProlongedAnimation;
97
98    /** WindowStateAnimator from mAppAnimator.allAppWindows as of last performLayout */
99    ArrayList<WindowStateAnimator> mAllAppWinAnimators = new ArrayList<>();
100
101    /** True if the current animation was transferred from another AppWindowAnimator.
102     *  See {@link #transferCurrentAnimation}*/
103    boolean usingTransferredAnimation = false;
104
105    private boolean mSkipFirstFrame = false;
106
107    static final Animation sDummyAnimation = new DummyAnimation();
108
109    public AppWindowAnimator(final AppWindowToken atoken) {
110        mAppToken = atoken;
111        mService = atoken.service;
112        mAnimator = atoken.mAnimator;
113    }
114
115    public void setAnimation(Animation anim, int width, int height, boolean skipFirstFrame) {
116        if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting animation in " + mAppToken
117                + ": " + anim + " wxh=" + width + "x" + height
118                + " isVisible=" + mAppToken.isVisible());
119        animation = anim;
120        animating = false;
121        if (!anim.isInitialized()) {
122            anim.initialize(width, height, width, height);
123        }
124        anim.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
125        anim.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
126        int zorder = anim.getZAdjustment();
127        int adj = 0;
128        if (zorder == Animation.ZORDER_TOP) {
129            adj = TYPE_LAYER_OFFSET;
130        } else if (zorder == Animation.ZORDER_BOTTOM) {
131            adj = -TYPE_LAYER_OFFSET;
132        }
133
134        if (animLayerAdjustment != adj) {
135            animLayerAdjustment = adj;
136            updateLayers();
137        }
138        // Start out animation gone if window is gone, or visible if window is visible.
139        transformation.clear();
140        transformation.setAlpha(mAppToken.isVisible() ? 1 : 0);
141        hasTransformation = true;
142
143        this.mSkipFirstFrame = skipFirstFrame;
144
145        if (!mAppToken.appFullscreen) {
146            anim.setBackgroundColor(0);
147        }
148        if (mClearProlongedAnimation) {
149            mProlongAnimation = PROLONG_ANIMATION_DISABLED;
150        } else {
151            mClearProlongedAnimation = true;
152        }
153    }
154
155    public void setDummyAnimation() {
156        if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting dummy animation in " + mAppToken
157                + " isVisible=" + mAppToken.isVisible());
158        animation = sDummyAnimation;
159        hasTransformation = true;
160        transformation.clear();
161        transformation.setAlpha(mAppToken.isVisible() ? 1 : 0);
162    }
163
164    public void clearAnimation() {
165        if (animation != null) {
166            animation = null;
167            animating = true;
168        }
169        clearThumbnail();
170        if (mAppToken.deferClearAllDrawn) {
171            mAppToken.allDrawn = false;
172            mAppToken.deferClearAllDrawn = false;
173        }
174        usingTransferredAnimation = false;
175    }
176
177    public boolean isAnimating() {
178        return animation != null || mAppToken.inPendingTransaction;
179    }
180
181    public void clearThumbnail() {
182        if (thumbnail != null) {
183            thumbnail.destroy();
184            thumbnail = null;
185        }
186        deferThumbnailDestruction = false;
187    }
188
189    void transferCurrentAnimation(
190            AppWindowAnimator toAppAnimator, WindowStateAnimator transferWinAnimator) {
191
192        if (animation != null) {
193            toAppAnimator.animation = animation;
194            animation = null;
195            toAppAnimator.animating = animating;
196            toAppAnimator.animLayerAdjustment = animLayerAdjustment;
197            animLayerAdjustment = 0;
198            toAppAnimator.updateLayers();
199            updateLayers();
200            toAppAnimator.usingTransferredAnimation = true;
201        }
202        if (transferWinAnimator != null) {
203            mAllAppWinAnimators.remove(transferWinAnimator);
204            toAppAnimator.mAllAppWinAnimators.add(transferWinAnimator);
205            transferWinAnimator.mAppAnimator = toAppAnimator;
206        }
207    }
208
209    void updateLayers() {
210        final int windowCount = mAppToken.allAppWindows.size();
211        final int adj = animLayerAdjustment;
212        thumbnailLayer = -1;
213        final WallpaperController wallpaperController = mService.mWallpaperControllerLocked;
214        for (int i = 0; i < windowCount; i++) {
215            final WindowState w = mAppToken.allAppWindows.get(i);
216            final WindowStateAnimator winAnimator = w.mWinAnimator;
217            winAnimator.mAnimLayer = w.mLayer + adj;
218            if (winAnimator.mAnimLayer > thumbnailLayer) {
219                thumbnailLayer = winAnimator.mAnimLayer;
220            }
221            if (DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": " + winAnimator.mAnimLayer);
222            if (w == mService.mInputMethodTarget && !mService.mInputMethodTargetWaitingAnim) {
223                mService.setInputMethodAnimLayerAdjustment(adj);
224            }
225            wallpaperController.setAnimLayerAdjustment(w, adj);
226        }
227    }
228
229    private void stepThumbnailAnimation(long currentTime) {
230        thumbnailTransformation.clear();
231        thumbnailAnimation.getTransformation(currentTime, thumbnailTransformation);
232        thumbnailTransformation.getMatrix().preTranslate(thumbnailX, thumbnailY);
233
234        ScreenRotationAnimation screenRotationAnimation =
235                mAnimator.getScreenRotationAnimationLocked(Display.DEFAULT_DISPLAY);
236        final boolean screenAnimation = screenRotationAnimation != null
237                && screenRotationAnimation.isAnimating();
238        if (screenAnimation) {
239            thumbnailTransformation.postCompose(screenRotationAnimation.getEnterTransformation());
240        }
241        // cache often used attributes locally
242        final float tmpFloats[] = mService.mTmpFloats;
243        thumbnailTransformation.getMatrix().getValues(tmpFloats);
244        if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail,
245                "thumbnail", "POS " + tmpFloats[Matrix.MTRANS_X]
246                + ", " + tmpFloats[Matrix.MTRANS_Y], null);
247        thumbnail.setPosition(tmpFloats[Matrix.MTRANS_X], tmpFloats[Matrix.MTRANS_Y]);
248        if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail,
249                "thumbnail", "alpha=" + thumbnailTransformation.getAlpha()
250                + " layer=" + thumbnailLayer
251                + " matrix=[" + tmpFloats[Matrix.MSCALE_X]
252                + "," + tmpFloats[Matrix.MSKEW_Y]
253                + "][" + tmpFloats[Matrix.MSKEW_X]
254                + "," + tmpFloats[Matrix.MSCALE_Y] + "]", null);
255        thumbnail.setAlpha(thumbnailTransformation.getAlpha());
256        if (thumbnailForceAboveLayer > 0) {
257            thumbnail.setLayer(thumbnailForceAboveLayer + 1);
258        } else {
259            // The thumbnail is layered below the window immediately above this
260            // token's anim layer.
261            thumbnail.setLayer(thumbnailLayer + WindowManagerService.WINDOW_LAYER_MULTIPLIER
262                    - WindowManagerService.LAYER_OFFSET_THUMBNAIL);
263        }
264        thumbnail.setMatrix(tmpFloats[Matrix.MSCALE_X], tmpFloats[Matrix.MSKEW_Y],
265                tmpFloats[Matrix.MSKEW_X], tmpFloats[Matrix.MSCALE_Y]);
266    }
267
268    private boolean stepAnimation(long currentTime) {
269        if (animation == null) {
270            return false;
271        }
272        transformation.clear();
273        if (mProlongAnimation == PROLONG_ANIMATION_AT_START) {
274            animation.setStartTime(currentTime);
275            currentTime += 1;
276        }
277        boolean hasMoreFrames = animation.getTransformation(currentTime, transformation);
278        if (!hasMoreFrames) {
279            if (deferThumbnailDestruction && !deferFinalFrameCleanup) {
280                // We are deferring the thumbnail destruction, so extend the animation for one more
281                // (dummy) frame before we clean up
282                deferFinalFrameCleanup = true;
283                hasMoreFrames = true;
284            } else {
285                if (false && DEBUG_ANIM) Slog.v(TAG,
286                        "Stepped animation in " + mAppToken + ": more=" + hasMoreFrames +
287                        ", xform=" + transformation + ", mProlongAnimation=" + mProlongAnimation);
288                deferFinalFrameCleanup = false;
289                if (mProlongAnimation == PROLONG_ANIMATION_AT_END) {
290                    hasMoreFrames = true;
291                } else {
292                    animation = null;
293                }
294                clearThumbnail();
295                if (DEBUG_ANIM) Slog.v(TAG,
296                        "Finished animation in " + mAppToken + " @ " + currentTime);
297            }
298        }
299        hasTransformation = hasMoreFrames;
300        return hasMoreFrames;
301    }
302
303    private long getStartTimeCorrection() {
304        if (mSkipFirstFrame) {
305
306            // If the transition is an animation in which the first frame doesn't change the screen
307            // contents at all, we can just skip it and start at the second frame. So we shift the
308            // start time of the animation forward by minus the frame duration.
309            return -Choreographer.getInstance().getFrameIntervalNanos() / TimeUtils.NANOS_PER_MS;
310        } else {
311            return 0;
312        }
313    }
314
315    // This must be called while inside a transaction.
316    boolean stepAnimationLocked(long currentTime, final int displayId) {
317        if (mService.okToDisplay()) {
318            // We will run animations as long as the display isn't frozen.
319
320            if (animation == sDummyAnimation) {
321                // This guy is going to animate, but not yet.  For now count
322                // it as not animating for purposes of scheduling transactions;
323                // when it is really time to animate, this will be set to
324                // a real animation and the next call will execute normally.
325                return false;
326            }
327
328            if ((mAppToken.allDrawn || mAppToken.mAnimatingWithSavedSurface
329                    || animating || mAppToken.startingDisplayed)
330                    && animation != null) {
331                if (!animating) {
332                    if (DEBUG_ANIM) Slog.v(TAG,
333                        "Starting animation in " + mAppToken +
334                        " @ " + currentTime + " scale="
335                        + mService.getTransitionAnimationScaleLocked()
336                        + " allDrawn=" + mAppToken.allDrawn + " animating=" + animating);
337                    long correction = getStartTimeCorrection();
338                    animation.setStartTime(currentTime + correction);
339                    animating = true;
340                    if (thumbnail != null) {
341                        thumbnail.show();
342                        thumbnailAnimation.setStartTime(currentTime + correction);
343                    }
344                    mSkipFirstFrame = false;
345                }
346                if (stepAnimation(currentTime)) {
347                    // animation isn't over, step any thumbnail and that's
348                    // it for now.
349                    if (thumbnail != null) {
350                        stepThumbnailAnimation(currentTime);
351                    }
352                    return true;
353                }
354            }
355        } else if (animation != null) {
356            // If the display is frozen, and there is a pending animation,
357            // clear it and make sure we run the cleanup code.
358            animating = true;
359            animation = null;
360        }
361
362        hasTransformation = false;
363
364        if (!animating && animation == null) {
365            return false;
366        }
367
368        mAnimator.setAppLayoutChanges(this, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM,
369                "AppWindowToken", displayId);
370
371        clearAnimation();
372        animating = false;
373        if (animLayerAdjustment != 0) {
374            animLayerAdjustment = 0;
375            updateLayers();
376        }
377        if (mService.mInputMethodTarget != null
378                && mService.mInputMethodTarget.mAppToken == mAppToken) {
379            mService.moveInputMethodWindowsIfNeededLocked(true);
380        }
381
382        if (DEBUG_ANIM) Slog.v(TAG,
383                "Animation done in " + mAppToken
384                + ": reportedVisible=" + mAppToken.reportedVisible);
385
386        transformation.clear();
387
388        final int numAllAppWinAnimators = mAllAppWinAnimators.size();
389        for (int i = 0; i < numAllAppWinAnimators; i++) {
390            mAllAppWinAnimators.get(i).finishExit();
391        }
392        mService.mAppTransition.notifyAppTransitionFinishedLocked(mAppToken.token);
393        return false;
394    }
395
396    // This must be called while inside a transaction.
397    boolean showAllWindowsLocked() {
398        boolean isAnimating = false;
399        final int NW = mAllAppWinAnimators.size();
400        for (int i=0; i<NW; i++) {
401            WindowStateAnimator winAnimator = mAllAppWinAnimators.get(i);
402            if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
403                    "performing show on: " + winAnimator);
404            winAnimator.performShowLocked();
405            isAnimating |= winAnimator.isAnimating();
406        }
407        return isAnimating;
408    }
409
410    void dump(PrintWriter pw, String prefix, boolean dumpAll) {
411        pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
412        pw.print(prefix); pw.print("mAnimator="); pw.println(mAnimator);
413        pw.print(prefix); pw.print("freezingScreen="); pw.print(freezingScreen);
414                pw.print(" allDrawn="); pw.print(allDrawn);
415                pw.print(" animLayerAdjustment="); pw.println(animLayerAdjustment);
416        if (lastFreezeDuration != 0) {
417            pw.print(prefix); pw.print("lastFreezeDuration=");
418                    TimeUtils.formatDuration(lastFreezeDuration, pw); pw.println();
419        }
420        if (animating || animation != null) {
421            pw.print(prefix); pw.print("animating="); pw.println(animating);
422            pw.print(prefix); pw.print("animation="); pw.println(animation);
423        }
424        if (hasTransformation) {
425            pw.print(prefix); pw.print("XForm: ");
426                    transformation.printShortString(pw);
427                    pw.println();
428        }
429        if (thumbnail != null) {
430            pw.print(prefix); pw.print("thumbnail="); pw.print(thumbnail);
431                    pw.print(" x="); pw.print(thumbnailX);
432                    pw.print(" y="); pw.print(thumbnailY);
433                    pw.print(" layer="); pw.println(thumbnailLayer);
434            pw.print(prefix); pw.print("thumbnailAnimation="); pw.println(thumbnailAnimation);
435            pw.print(prefix); pw.print("thumbnailTransformation=");
436                    pw.println(thumbnailTransformation.toShortString());
437        }
438        for (int i=0; i<mAllAppWinAnimators.size(); i++) {
439            WindowStateAnimator wanim = mAllAppWinAnimators.get(i);
440            pw.print(prefix); pw.print("App Win Anim #"); pw.print(i);
441                    pw.print(": "); pw.println(wanim);
442        }
443    }
444
445    void startProlongAnimation(int prolongType) {
446        mProlongAnimation = prolongType;
447        mClearProlongedAnimation = false;
448    }
449
450    void endProlongedAnimation() {
451        mProlongAnimation = PROLONG_ANIMATION_DISABLED;
452    }
453
454    // This is an animation that does nothing: it just immediately finishes
455    // itself every time it is called.  It is used as a stub animation in cases
456    // where we want to synchronize multiple things that may be animating.
457    static final class DummyAnimation extends Animation {
458        @Override
459        public boolean getTransformation(long currentTime, Transformation outTransformation) {
460            return false;
461        }
462    }
463
464}
465