blob: 43fa3d56914e1e43c27c307bac1e49204342b187 [file] [log] [blame]
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001/*
2 * Copyright (C) 2017 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
Jorim Jaggif5f9e122017-10-24 18:21:09 +020019import static com.android.server.wm.AnimationAdapter.STATUS_BAR_TRANSITION_DURATION;
chaviw23012112017-12-20 15:29:04 -080020import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_AFTER_ANIM;
21import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020022
Jorim Jaggi21c39a72017-10-20 15:47:51 +020023import android.graphics.Point;
chaviw23012112017-12-20 15:29:04 -080024import android.graphics.Rect;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020025import android.os.SystemClock;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020026import android.view.SurfaceControl;
27import android.view.SurfaceControl.Transaction;
28import android.view.animation.Animation;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020029import android.view.animation.AnimationSet;
30import android.view.animation.Interpolator;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020031import android.view.animation.Transformation;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020032import android.view.animation.TranslateAnimation;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020033
34import com.android.server.wm.LocalAnimationAdapter.AnimationSpec;
35
36/**
37 * Animation spec for regular window animations.
38 */
39public class WindowAnimationSpec implements AnimationSpec {
40
41 private Animation mAnimation;
42 private final Point mPosition = new Point();
Jorim Jaggia5e10572017-11-15 14:36:26 +010043 private final ThreadLocal<TmpValues> mThreadLocalTmps = ThreadLocal.withInitial(TmpValues::new);
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +010044 private final boolean mCanSkipFirstFrame;
chaviw23012112017-12-20 15:29:04 -080045 private final Rect mStackBounds = new Rect();
46 private int mStackClipMode;
47 private final Rect mTmpRect = new Rect();
Jorim Jaggi21c39a72017-10-20 15:47:51 +020048
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +010049 public WindowAnimationSpec(Animation animation, Point position, boolean canSkipFirstFrame) {
chaviw23012112017-12-20 15:29:04 -080050 this(animation, position, null /* stackBounds */, canSkipFirstFrame, STACK_CLIP_NONE);
51 }
52
53 public WindowAnimationSpec(Animation animation, Point position, Rect stackBounds,
54 boolean canSkipFirstFrame, int stackClipMode) {
Jorim Jaggi21c39a72017-10-20 15:47:51 +020055 mAnimation = animation;
chaviw23012112017-12-20 15:29:04 -080056 if (position != null) {
57 mPosition.set(position.x, position.y);
58 }
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +010059 mCanSkipFirstFrame = canSkipFirstFrame;
chaviw23012112017-12-20 15:29:04 -080060 mStackClipMode = stackClipMode;
61 if (stackBounds != null) {
62 mStackBounds.set(stackBounds);
63 }
Jorim Jaggi21c39a72017-10-20 15:47:51 +020064 }
65
66 @Override
67 public boolean getDetachWallpaper() {
68 return mAnimation.getDetachWallpaper();
69 }
70
71 @Override
Jorim Jaggi82c17862018-02-21 17:50:18 +010072 public boolean getShowWallpaper() {
73 return mAnimation.getShowWallpaper();
74 }
75
76 @Override
Jorim Jaggi21c39a72017-10-20 15:47:51 +020077 public int getBackgroundColor() {
78 return mAnimation.getBackgroundColor();
79 }
80
81 @Override
82 public long getDuration() {
83 return mAnimation.computeDurationHint();
84 }
85
86 @Override
87 public void apply(Transaction t, SurfaceControl leash, long currentPlayTime) {
Jorim Jaggia5e10572017-11-15 14:36:26 +010088 final TmpValues tmp = mThreadLocalTmps.get();
Jorim Jaggi21c39a72017-10-20 15:47:51 +020089 tmp.transformation.clear();
90 mAnimation.getTransformation(currentPlayTime, tmp.transformation);
91 tmp.transformation.getMatrix().postTranslate(mPosition.x, mPosition.y);
92 t.setMatrix(leash, tmp.transformation.getMatrix(), tmp.floats);
93 t.setAlpha(leash, tmp.transformation.getAlpha());
chaviw23012112017-12-20 15:29:04 -080094 if (mStackClipMode == STACK_CLIP_NONE) {
95 t.setWindowCrop(leash, tmp.transformation.getClipRect());
96 } else if (mStackClipMode == STACK_CLIP_AFTER_ANIM) {
chaviwf6637d62018-01-08 13:36:23 -080097 mTmpRect.set(mStackBounds);
98 // Offset stack bounds to stack position so the final crop is in screen space.
99 mTmpRect.offsetTo(mPosition.x, mPosition.y);
100 t.setFinalCrop(leash, mTmpRect);
chaviw23012112017-12-20 15:29:04 -0800101 t.setWindowCrop(leash, tmp.transformation.getClipRect());
102 } else {
chaviw977482a2017-12-28 11:35:53 -0800103 mTmpRect.set(mStackBounds);
104 mTmpRect.intersect(tmp.transformation.getClipRect());
chaviw23012112017-12-20 15:29:04 -0800105 t.setWindowCrop(leash, mTmpRect);
106 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200107 }
108
109 @Override
110 public long calculateStatusBarTransitionStartTime() {
111 TranslateAnimation openTranslateAnimation = findTranslateAnimation(mAnimation);
112 if (openTranslateAnimation != null) {
113
114 // Some interpolators are extremely quickly mostly finished, but not completely. For
115 // our purposes, we need to find the fraction for which ther interpolator is mostly
116 // there, and use that value for the calculation.
117 float t = findAlmostThereFraction(openTranslateAnimation.getInterpolator());
118 return SystemClock.uptimeMillis()
119 + openTranslateAnimation.getStartOffset()
120 + (long)(openTranslateAnimation.getDuration() * t)
121 - STATUS_BAR_TRANSITION_DURATION;
122 } else {
123 return SystemClock.uptimeMillis();
124 }
125 }
126
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100127 @Override
128 public boolean canSkipFirstFrame() {
129 return mCanSkipFirstFrame;
130 }
131
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200132 /**
133 * Tries to find a {@link TranslateAnimation} inside the {@code animation}.
134 *
135 * @return the found animation, {@code null} otherwise
136 */
137 private static TranslateAnimation findTranslateAnimation(Animation animation) {
138 if (animation instanceof TranslateAnimation) {
139 return (TranslateAnimation) animation;
140 } else if (animation instanceof AnimationSet) {
141 AnimationSet set = (AnimationSet) animation;
142 for (int i = 0; i < set.getAnimations().size(); i++) {
143 Animation a = set.getAnimations().get(i);
144 if (a instanceof TranslateAnimation) {
145 return (TranslateAnimation) a;
146 }
147 }
148 }
149 return null;
150 }
151
152 /**
153 * Binary searches for a {@code t} such that there exists a {@code -0.01 < eps < 0.01} for which
154 * {@code interpolator(t + eps) > 0.99}.
155 */
156 private static float findAlmostThereFraction(Interpolator interpolator) {
157 float val = 0.5f;
158 float adj = 0.25f;
159 while (adj >= 0.01f) {
160 if (interpolator.getInterpolation(val) < 0.99f) {
161 val += adj;
162 } else {
163 val -= adj;
164 }
165 adj /= 2;
166 }
167 return val;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200168 }
169
Jorim Jaggia5e10572017-11-15 14:36:26 +0100170 private static class TmpValues {
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200171 final Transformation transformation = new Transformation();
172 final float[] floats = new float[9];
173 }
174}