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; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 20 | import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_AFTER_ANIM; |
| 21 | import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 22 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 23 | import android.graphics.Point; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 25 | import android.os.SystemClock; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 26 | import android.view.SurfaceControl; |
| 27 | import android.view.SurfaceControl.Transaction; |
| 28 | import android.view.animation.Animation; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 29 | import android.view.animation.AnimationSet; |
| 30 | import android.view.animation.Interpolator; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 31 | import android.view.animation.Transformation; |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 32 | import android.view.animation.TranslateAnimation; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 33 | |
| 34 | import com.android.server.wm.LocalAnimationAdapter.AnimationSpec; |
| 35 | |
| 36 | /** |
| 37 | * Animation spec for regular window animations. |
| 38 | */ |
| 39 | public class WindowAnimationSpec implements AnimationSpec { |
| 40 | |
| 41 | private Animation mAnimation; |
| 42 | private final Point mPosition = new Point(); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 43 | private final ThreadLocal<TmpValues> mThreadLocalTmps = ThreadLocal.withInitial(TmpValues::new); |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 44 | private final boolean mCanSkipFirstFrame; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 45 | private final Rect mStackBounds = new Rect(); |
| 46 | private int mStackClipMode; |
| 47 | private final Rect mTmpRect = new Rect(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 48 | |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 49 | public WindowAnimationSpec(Animation animation, Point position, boolean canSkipFirstFrame) { |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 50 | 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 Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 55 | mAnimation = animation; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 56 | if (position != null) { |
| 57 | mPosition.set(position.x, position.y); |
| 58 | } |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 59 | mCanSkipFirstFrame = canSkipFirstFrame; |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 60 | mStackClipMode = stackClipMode; |
| 61 | if (stackBounds != null) { |
| 62 | mStackBounds.set(stackBounds); |
| 63 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean getDetachWallpaper() { |
| 68 | return mAnimation.getDetachWallpaper(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int getBackgroundColor() { |
| 73 | return mAnimation.getBackgroundColor(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public long getDuration() { |
| 78 | return mAnimation.computeDurationHint(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void apply(Transaction t, SurfaceControl leash, long currentPlayTime) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 83 | final TmpValues tmp = mThreadLocalTmps.get(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 84 | tmp.transformation.clear(); |
| 85 | mAnimation.getTransformation(currentPlayTime, tmp.transformation); |
| 86 | tmp.transformation.getMatrix().postTranslate(mPosition.x, mPosition.y); |
| 87 | t.setMatrix(leash, tmp.transformation.getMatrix(), tmp.floats); |
| 88 | t.setAlpha(leash, tmp.transformation.getAlpha()); |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 89 | if (mStackClipMode == STACK_CLIP_NONE) { |
| 90 | t.setWindowCrop(leash, tmp.transformation.getClipRect()); |
| 91 | } else if (mStackClipMode == STACK_CLIP_AFTER_ANIM) { |
chaviw | f6637d6 | 2018-01-08 13:36:23 -0800 | [diff] [blame] | 92 | mTmpRect.set(mStackBounds); |
| 93 | // Offset stack bounds to stack position so the final crop is in screen space. |
| 94 | mTmpRect.offsetTo(mPosition.x, mPosition.y); |
| 95 | t.setFinalCrop(leash, mTmpRect); |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 96 | t.setWindowCrop(leash, tmp.transformation.getClipRect()); |
| 97 | } else { |
chaviw | 977482a | 2017-12-28 11:35:53 -0800 | [diff] [blame] | 98 | mTmpRect.set(mStackBounds); |
| 99 | mTmpRect.intersect(tmp.transformation.getClipRect()); |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 100 | t.setWindowCrop(leash, mTmpRect); |
| 101 | } |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public long calculateStatusBarTransitionStartTime() { |
| 106 | TranslateAnimation openTranslateAnimation = findTranslateAnimation(mAnimation); |
| 107 | if (openTranslateAnimation != null) { |
| 108 | |
| 109 | // Some interpolators are extremely quickly mostly finished, but not completely. For |
| 110 | // our purposes, we need to find the fraction for which ther interpolator is mostly |
| 111 | // there, and use that value for the calculation. |
| 112 | float t = findAlmostThereFraction(openTranslateAnimation.getInterpolator()); |
| 113 | return SystemClock.uptimeMillis() |
| 114 | + openTranslateAnimation.getStartOffset() |
| 115 | + (long)(openTranslateAnimation.getDuration() * t) |
| 116 | - STATUS_BAR_TRANSITION_DURATION; |
| 117 | } else { |
| 118 | return SystemClock.uptimeMillis(); |
| 119 | } |
| 120 | } |
| 121 | |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 122 | @Override |
| 123 | public boolean canSkipFirstFrame() { |
| 124 | return mCanSkipFirstFrame; |
| 125 | } |
| 126 | |
Jorim Jaggi | f5f9e12 | 2017-10-24 18:21:09 +0200 | [diff] [blame] | 127 | /** |
| 128 | * Tries to find a {@link TranslateAnimation} inside the {@code animation}. |
| 129 | * |
| 130 | * @return the found animation, {@code null} otherwise |
| 131 | */ |
| 132 | private static TranslateAnimation findTranslateAnimation(Animation animation) { |
| 133 | if (animation instanceof TranslateAnimation) { |
| 134 | return (TranslateAnimation) animation; |
| 135 | } else if (animation instanceof AnimationSet) { |
| 136 | AnimationSet set = (AnimationSet) animation; |
| 137 | for (int i = 0; i < set.getAnimations().size(); i++) { |
| 138 | Animation a = set.getAnimations().get(i); |
| 139 | if (a instanceof TranslateAnimation) { |
| 140 | return (TranslateAnimation) a; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return null; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Binary searches for a {@code t} such that there exists a {@code -0.01 < eps < 0.01} for which |
| 149 | * {@code interpolator(t + eps) > 0.99}. |
| 150 | */ |
| 151 | private static float findAlmostThereFraction(Interpolator interpolator) { |
| 152 | float val = 0.5f; |
| 153 | float adj = 0.25f; |
| 154 | while (adj >= 0.01f) { |
| 155 | if (interpolator.getInterpolation(val) < 0.99f) { |
| 156 | val += adj; |
| 157 | } else { |
| 158 | val -= adj; |
| 159 | } |
| 160 | adj /= 2; |
| 161 | } |
| 162 | return val; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 165 | private static class TmpValues { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 166 | final Transformation transformation = new Transformation(); |
| 167 | final float[] floats = new float[9]; |
| 168 | } |
| 169 | } |