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