Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 19 | import static com.android.server.wm.AnimationAdapter.STATUS_BAR_TRANSITION_DURATION; |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 20 | import static com.android.server.wm.AnimationSpecProto.WINDOW; |
| 21 | import static com.android.server.wm.WindowAnimationSpecProto.ANIMATION; |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 22 | import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_AFTER_ANIM; |
| 23 | import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 24 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 25 | import android.graphics.Point; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 27 | import android.os.SystemClock; |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 28 | import android.util.proto.ProtoOutputStream; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 29 | import android.view.SurfaceControl; |
| 30 | import android.view.SurfaceControl.Transaction; |
| 31 | import android.view.animation.Animation; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 32 | import android.view.animation.AnimationSet; |
| 33 | import android.view.animation.Interpolator; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 34 | import android.view.animation.Transformation; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 35 | import android.view.animation.TranslateAnimation; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 36 | |
| 37 | import com.android.server.wm.LocalAnimationAdapter.AnimationSpec; |
| 38 | |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 39 | import java.io.PrintWriter; |
| 40 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Animation spec for regular window animations. |
| 43 | */ |
| 44 | public class WindowAnimationSpec implements AnimationSpec { |
| 45 | |
| 46 | private Animation mAnimation; |
| 47 | private final Point mPosition = new Point(); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 48 | private final ThreadLocal<TmpValues> mThreadLocalTmps = ThreadLocal.withInitial(TmpValues::new); |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 49 | private final boolean mCanSkipFirstFrame; |
Jorim Jaggi | aa763cd | 2018-03-22 23:20:36 +0100 | [diff] [blame] | 50 | private final boolean mIsAppAnimation; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 51 | private final Rect mStackBounds = new Rect(); |
| 52 | private int mStackClipMode; |
| 53 | private final Rect mTmpRect = new Rect(); |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 54 | private final float mWindowCornerRadius; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 55 | |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 56 | public WindowAnimationSpec(Animation animation, Point position, boolean canSkipFirstFrame, |
| 57 | float windowCornerRadius) { |
Jorim Jaggi | aa763cd | 2018-03-22 23:20:36 +0100 | [diff] [blame] | 58 | this(animation, position, null /* stackBounds */, canSkipFirstFrame, STACK_CLIP_NONE, |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 59 | false /* isAppAnimation */, windowCornerRadius); |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public WindowAnimationSpec(Animation animation, Point position, Rect stackBounds, |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 63 | boolean canSkipFirstFrame, int stackClipMode, boolean isAppAnimation, |
| 64 | float windowCornerRadius) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 65 | mAnimation = animation; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 66 | if (position != null) { |
| 67 | mPosition.set(position.x, position.y); |
| 68 | } |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 69 | mWindowCornerRadius = windowCornerRadius; |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 70 | mCanSkipFirstFrame = canSkipFirstFrame; |
Jorim Jaggi | aa763cd | 2018-03-22 23:20:36 +0100 | [diff] [blame] | 71 | mIsAppAnimation = isAppAnimation; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 72 | mStackClipMode = stackClipMode; |
| 73 | if (stackBounds != null) { |
| 74 | mStackBounds.set(stackBounds); |
| 75 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Jorim Jaggi | 82c1786 | 2018-02-21 17:50:18 +0100 | [diff] [blame] | 79 | public boolean getShowWallpaper() { |
| 80 | return mAnimation.getShowWallpaper(); |
| 81 | } |
| 82 | |
| 83 | @Override |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 84 | public int getBackgroundColor() { |
| 85 | return mAnimation.getBackgroundColor(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public long getDuration() { |
| 90 | return mAnimation.computeDurationHint(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void apply(Transaction t, SurfaceControl leash, long currentPlayTime) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 95 | final TmpValues tmp = mThreadLocalTmps.get(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 96 | tmp.transformation.clear(); |
| 97 | mAnimation.getTransformation(currentPlayTime, tmp.transformation); |
| 98 | tmp.transformation.getMatrix().postTranslate(mPosition.x, mPosition.y); |
| 99 | t.setMatrix(leash, tmp.transformation.getMatrix(), tmp.floats); |
| 100 | t.setAlpha(leash, tmp.transformation.getAlpha()); |
Vishnu Nair | a297726 | 2018-07-26 13:31:26 -0700 | [diff] [blame] | 101 | if (mStackClipMode == STACK_CLIP_NONE || mStackClipMode == STACK_CLIP_AFTER_ANIM) { |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 102 | t.setWindowCrop(leash, tmp.transformation.getClipRect()); |
| 103 | } else { |
chaviw | 977482a | 2017-12-28 11:35:53 -0800 | [diff] [blame] | 104 | mTmpRect.set(mStackBounds); |
| 105 | mTmpRect.intersect(tmp.transformation.getClipRect()); |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 106 | t.setWindowCrop(leash, mTmpRect); |
| 107 | } |
Lucas Dupin | 3e1dc20 | 2018-12-02 15:14:20 -0800 | [diff] [blame] | 108 | if (mAnimation.hasRoundedCorners() && mWindowCornerRadius > 0) { |
| 109 | t.setCornerRadius(leash, mWindowCornerRadius); |
| 110 | } |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public long calculateStatusBarTransitionStartTime() { |
| 115 | TranslateAnimation openTranslateAnimation = findTranslateAnimation(mAnimation); |
| 116 | if (openTranslateAnimation != null) { |
| 117 | |
| 118 | // Some interpolators are extremely quickly mostly finished, but not completely. For |
| 119 | // our purposes, we need to find the fraction for which ther interpolator is mostly |
| 120 | // there, and use that value for the calculation. |
| 121 | float t = findAlmostThereFraction(openTranslateAnimation.getInterpolator()); |
| 122 | return SystemClock.uptimeMillis() |
| 123 | + openTranslateAnimation.getStartOffset() |
| 124 | + (long)(openTranslateAnimation.getDuration() * t) |
| 125 | - STATUS_BAR_TRANSITION_DURATION; |
| 126 | } else { |
| 127 | return SystemClock.uptimeMillis(); |
| 128 | } |
| 129 | } |
| 130 | |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 131 | @Override |
| 132 | public boolean canSkipFirstFrame() { |
| 133 | return mCanSkipFirstFrame; |
| 134 | } |
| 135 | |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 136 | @Override |
Jorim Jaggi | aa763cd | 2018-03-22 23:20:36 +0100 | [diff] [blame] | 137 | public boolean needsEarlyWakeup() { |
| 138 | return mIsAppAnimation; |
| 139 | } |
| 140 | |
| 141 | @Override |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 142 | public void dump(PrintWriter pw, String prefix) { |
| 143 | pw.print(prefix); pw.println(mAnimation); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void writeToProtoInner(ProtoOutputStream proto) { |
| 148 | final long token = proto.start(WINDOW); |
| 149 | proto.write(ANIMATION, mAnimation.toString()); |
| 150 | proto.end(token); |
| 151 | } |
| 152 | |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 153 | /** |
| 154 | * Tries to find a {@link TranslateAnimation} inside the {@code animation}. |
| 155 | * |
| 156 | * @return the found animation, {@code null} otherwise |
| 157 | */ |
| 158 | private static TranslateAnimation findTranslateAnimation(Animation animation) { |
| 159 | if (animation instanceof TranslateAnimation) { |
| 160 | return (TranslateAnimation) animation; |
| 161 | } else if (animation instanceof AnimationSet) { |
| 162 | AnimationSet set = (AnimationSet) animation; |
| 163 | for (int i = 0; i < set.getAnimations().size(); i++) { |
| 164 | Animation a = set.getAnimations().get(i); |
| 165 | if (a instanceof TranslateAnimation) { |
| 166 | return (TranslateAnimation) a; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Binary searches for a {@code t} such that there exists a {@code -0.01 < eps < 0.01} for which |
| 175 | * {@code interpolator(t + eps) > 0.99}. |
| 176 | */ |
| 177 | private static float findAlmostThereFraction(Interpolator interpolator) { |
| 178 | float val = 0.5f; |
| 179 | float adj = 0.25f; |
| 180 | while (adj >= 0.01f) { |
| 181 | if (interpolator.getInterpolation(val) < 0.99f) { |
| 182 | val += adj; |
| 183 | } else { |
| 184 | val -= adj; |
| 185 | } |
| 186 | adj /= 2; |
| 187 | } |
| 188 | return val; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 191 | private static class TmpValues { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 192 | final Transformation transformation = new Transformation(); |
| 193 | final float[] floats = new float[9]; |
| 194 | } |
| 195 | } |