blob: a41eba8205f7a52ebfdd3d5c438bab41b68e88a8 [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 Jaggif75d1612018-02-27 15:05:21 +010022import static com.android.server.wm.proto.AnimationSpecProto.WINDOW;
23import static com.android.server.wm.proto.WindowAnimationSpecProto.ANIMATION;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020024
Jorim Jaggi21c39a72017-10-20 15:47:51 +020025import android.graphics.Point;
chaviw23012112017-12-20 15:29:04 -080026import android.graphics.Rect;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020027import android.os.SystemClock;
Jorim Jaggif75d1612018-02-27 15:05:21 +010028import android.util.proto.ProtoOutputStream;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020029import android.view.SurfaceControl;
30import android.view.SurfaceControl.Transaction;
31import android.view.animation.Animation;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020032import android.view.animation.AnimationSet;
33import android.view.animation.Interpolator;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020034import android.view.animation.Transformation;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020035import android.view.animation.TranslateAnimation;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020036
37import com.android.server.wm.LocalAnimationAdapter.AnimationSpec;
38
Jorim Jaggif75d1612018-02-27 15:05:21 +010039import java.io.PrintWriter;
40
Jorim Jaggi21c39a72017-10-20 15:47:51 +020041/**
42 * Animation spec for regular window animations.
43 */
44public class WindowAnimationSpec implements AnimationSpec {
45
46 private Animation mAnimation;
47 private final Point mPosition = new Point();
Jorim Jaggia5e10572017-11-15 14:36:26 +010048 private final ThreadLocal<TmpValues> mThreadLocalTmps = ThreadLocal.withInitial(TmpValues::new);
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +010049 private final boolean mCanSkipFirstFrame;
chaviw23012112017-12-20 15:29:04 -080050 private final Rect mStackBounds = new Rect();
51 private int mStackClipMode;
52 private final Rect mTmpRect = new Rect();
Jorim Jaggi21c39a72017-10-20 15:47:51 +020053
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +010054 public WindowAnimationSpec(Animation animation, Point position, boolean canSkipFirstFrame) {
chaviw23012112017-12-20 15:29:04 -080055 this(animation, position, null /* stackBounds */, canSkipFirstFrame, STACK_CLIP_NONE);
56 }
57
58 public WindowAnimationSpec(Animation animation, Point position, Rect stackBounds,
59 boolean canSkipFirstFrame, int stackClipMode) {
Jorim Jaggi21c39a72017-10-20 15:47:51 +020060 mAnimation = animation;
chaviw23012112017-12-20 15:29:04 -080061 if (position != null) {
62 mPosition.set(position.x, position.y);
63 }
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +010064 mCanSkipFirstFrame = canSkipFirstFrame;
chaviw23012112017-12-20 15:29:04 -080065 mStackClipMode = stackClipMode;
66 if (stackBounds != null) {
67 mStackBounds.set(stackBounds);
68 }
Jorim Jaggi21c39a72017-10-20 15:47:51 +020069 }
70
71 @Override
72 public boolean getDetachWallpaper() {
73 return mAnimation.getDetachWallpaper();
74 }
75
76 @Override
Jorim Jaggi82c17862018-02-21 17:50:18 +010077 public boolean getShowWallpaper() {
78 return mAnimation.getShowWallpaper();
79 }
80
81 @Override
Jorim Jaggi21c39a72017-10-20 15:47:51 +020082 public int getBackgroundColor() {
83 return mAnimation.getBackgroundColor();
84 }
85
86 @Override
87 public long getDuration() {
88 return mAnimation.computeDurationHint();
89 }
90
91 @Override
92 public void apply(Transaction t, SurfaceControl leash, long currentPlayTime) {
Jorim Jaggia5e10572017-11-15 14:36:26 +010093 final TmpValues tmp = mThreadLocalTmps.get();
Jorim Jaggi21c39a72017-10-20 15:47:51 +020094 tmp.transformation.clear();
95 mAnimation.getTransformation(currentPlayTime, tmp.transformation);
96 tmp.transformation.getMatrix().postTranslate(mPosition.x, mPosition.y);
97 t.setMatrix(leash, tmp.transformation.getMatrix(), tmp.floats);
98 t.setAlpha(leash, tmp.transformation.getAlpha());
chaviw23012112017-12-20 15:29:04 -080099 if (mStackClipMode == STACK_CLIP_NONE) {
100 t.setWindowCrop(leash, tmp.transformation.getClipRect());
101 } else if (mStackClipMode == STACK_CLIP_AFTER_ANIM) {
chaviwf6637d62018-01-08 13:36:23 -0800102 mTmpRect.set(mStackBounds);
103 // Offset stack bounds to stack position so the final crop is in screen space.
104 mTmpRect.offsetTo(mPosition.x, mPosition.y);
105 t.setFinalCrop(leash, mTmpRect);
chaviw23012112017-12-20 15:29:04 -0800106 t.setWindowCrop(leash, tmp.transformation.getClipRect());
107 } else {
chaviw977482a2017-12-28 11:35:53 -0800108 mTmpRect.set(mStackBounds);
109 mTmpRect.intersect(tmp.transformation.getClipRect());
chaviw23012112017-12-20 15:29:04 -0800110 t.setWindowCrop(leash, mTmpRect);
111 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200112 }
113
114 @Override
115 public long calculateStatusBarTransitionStartTime() {
116 TranslateAnimation openTranslateAnimation = findTranslateAnimation(mAnimation);
117 if (openTranslateAnimation != null) {
118
119 // Some interpolators are extremely quickly mostly finished, but not completely. For
120 // our purposes, we need to find the fraction for which ther interpolator is mostly
121 // there, and use that value for the calculation.
122 float t = findAlmostThereFraction(openTranslateAnimation.getInterpolator());
123 return SystemClock.uptimeMillis()
124 + openTranslateAnimation.getStartOffset()
125 + (long)(openTranslateAnimation.getDuration() * t)
126 - STATUS_BAR_TRANSITION_DURATION;
127 } else {
128 return SystemClock.uptimeMillis();
129 }
130 }
131
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100132 @Override
133 public boolean canSkipFirstFrame() {
134 return mCanSkipFirstFrame;
135 }
136
Jorim Jaggif75d1612018-02-27 15:05:21 +0100137 @Override
138 public void dump(PrintWriter pw, String prefix) {
139 pw.print(prefix); pw.println(mAnimation);
140 }
141
142 @Override
143 public void writeToProtoInner(ProtoOutputStream proto) {
144 final long token = proto.start(WINDOW);
145 proto.write(ANIMATION, mAnimation.toString());
146 proto.end(token);
147 }
148
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200149 /**
150 * Tries to find a {@link TranslateAnimation} inside the {@code animation}.
151 *
152 * @return the found animation, {@code null} otherwise
153 */
154 private static TranslateAnimation findTranslateAnimation(Animation animation) {
155 if (animation instanceof TranslateAnimation) {
156 return (TranslateAnimation) animation;
157 } else if (animation instanceof AnimationSet) {
158 AnimationSet set = (AnimationSet) animation;
159 for (int i = 0; i < set.getAnimations().size(); i++) {
160 Animation a = set.getAnimations().get(i);
161 if (a instanceof TranslateAnimation) {
162 return (TranslateAnimation) a;
163 }
164 }
165 }
166 return null;
167 }
168
169 /**
170 * Binary searches for a {@code t} such that there exists a {@code -0.01 < eps < 0.01} for which
171 * {@code interpolator(t + eps) > 0.99}.
172 */
173 private static float findAlmostThereFraction(Interpolator interpolator) {
174 float val = 0.5f;
175 float adj = 0.25f;
176 while (adj >= 0.01f) {
177 if (interpolator.getInterpolation(val) < 0.99f) {
178 val += adj;
179 } else {
180 val -= adj;
181 }
182 adj /= 2;
183 }
184 return val;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200185 }
186
Jorim Jaggia5e10572017-11-15 14:36:26 +0100187 private static class TmpValues {
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200188 final Transformation transformation = new Transformation();
189 final float[] floats = new float[9];
190 }
191}