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 | |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 19 | import static com.android.server.wm.SurfaceAnimatorProto.ANIMATION_ADAPTER; |
| 20 | import static com.android.server.wm.SurfaceAnimatorProto.ANIMATION_START_DELAYED; |
| 21 | import static com.android.server.wm.SurfaceAnimatorProto.LEASH; |
Vishnu Nair | d454442d | 2018-11-13 13:51:01 -0800 | [diff] [blame] | 22 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 25 | |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 26 | import android.annotation.IntDef; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 27 | import android.annotation.NonNull; |
| 28 | import android.annotation.Nullable; |
| 29 | import android.util.Slog; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 30 | import android.util.proto.ProtoOutputStream; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 31 | import android.view.SurfaceControl; |
| 32 | import android.view.SurfaceControl.Transaction; |
| 33 | |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 34 | import com.android.internal.annotations.VisibleForTesting; |
| 35 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 36 | import java.io.PrintWriter; |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 37 | import java.lang.annotation.Retention; |
| 38 | import java.lang.annotation.RetentionPolicy; |
Vishnu Nair | 5e3e2ea | 2020-03-16 11:30:34 -0700 | [diff] [blame] | 39 | import java.util.function.Supplier; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * A class that can run animations on objects that have a set of child surfaces. We do this by |
| 43 | * reparenting all child surfaces of an object onto a new surface, called the "Leash". The Leash |
| 44 | * gets attached in the surface hierarchy where the the children were attached to. We then hand off |
| 45 | * the Leash to the component handling the animation, which is specified by the |
| 46 | * {@link AnimationAdapter}. When the animation is done animating, our callback to finish the |
| 47 | * animation will be invoked, at which we reparent the children back to the original parent. |
| 48 | */ |
| 49 | class SurfaceAnimator { |
| 50 | |
| 51 | private static final String TAG = TAG_WITH_CLASS_NAME ? "SurfaceAnimator" : TAG_WM; |
Winson Chung | 7e895da | 2020-02-03 13:28:44 -0800 | [diff] [blame] | 52 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 53 | private final WindowManagerService mService; |
| 54 | private AnimationAdapter mAnimation; |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 55 | private @AnimationType int mAnimationType; |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 56 | |
| 57 | @VisibleForTesting |
| 58 | SurfaceControl mLeash; |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 59 | @VisibleForTesting |
| 60 | final Animatable mAnimatable; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 61 | private final OnAnimationFinishedCallback mInnerAnimationFinishedCallback; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 62 | @VisibleForTesting |
Vadim Caen | 9745f97 | 2019-09-17 19:16:14 +0200 | [diff] [blame] | 63 | @Nullable |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 64 | final OnAnimationFinishedCallback mStaticAnimationFinishedCallback; |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 65 | @Nullable |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 66 | private OnAnimationFinishedCallback mAnimationFinishedCallback; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 67 | private boolean mAnimationStartDelayed; |
| 68 | |
| 69 | /** |
| 70 | * @param animatable The object to animate. |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 71 | * @param staticAnimationFinishedCallback Callback to invoke when an animation has finished |
| 72 | * running. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 73 | */ |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 74 | SurfaceAnimator(Animatable animatable, |
| 75 | @Nullable OnAnimationFinishedCallback staticAnimationFinishedCallback, |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 76 | WindowManagerService service) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 77 | mAnimatable = animatable; |
| 78 | mService = service; |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 79 | mStaticAnimationFinishedCallback = staticAnimationFinishedCallback; |
| 80 | mInnerAnimationFinishedCallback = getFinishedCallback(staticAnimationFinishedCallback); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 83 | private OnAnimationFinishedCallback getFinishedCallback( |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 84 | @Nullable OnAnimationFinishedCallback staticAnimationFinishedCallback) { |
| 85 | return (type, anim) -> { |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 86 | synchronized (mService.mGlobalLock) { |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 87 | final SurfaceAnimator target = mService.mAnimationTransferMap.remove(anim); |
| 88 | if (target != null) { |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 89 | target.mInnerAnimationFinishedCallback.onAnimationFinished(type, anim); |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 90 | return; |
| 91 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 92 | |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 93 | if (anim != mAnimation) { |
| 94 | return; |
| 95 | } |
Jorim Jaggi | 6de6101 | 2018-03-19 14:53:23 +0100 | [diff] [blame] | 96 | final Runnable resetAndInvokeFinish = () -> { |
Riddle Hsu | 1d5856c | 2019-03-26 22:56:53 +0800 | [diff] [blame] | 97 | // We need to check again if the animation has been replaced with a new |
| 98 | // animation because the animatable may defer to finish. |
| 99 | if (anim != mAnimation) { |
| 100 | return; |
| 101 | } |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 102 | final OnAnimationFinishedCallback animationFinishCallback = |
| 103 | mAnimationFinishedCallback; |
Jorim Jaggi | 6de6101 | 2018-03-19 14:53:23 +0100 | [diff] [blame] | 104 | reset(mAnimatable.getPendingTransaction(), true /* destroyLeash */); |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 105 | if (staticAnimationFinishedCallback != null) { |
| 106 | staticAnimationFinishedCallback.onAnimationFinished(type, anim); |
Jorim Jaggi | 6de6101 | 2018-03-19 14:53:23 +0100 | [diff] [blame] | 107 | } |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 108 | if (animationFinishCallback != null) { |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 109 | animationFinishCallback.onAnimationFinished(type, anim); |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 110 | } |
Jorim Jaggi | 6de6101 | 2018-03-19 14:53:23 +0100 | [diff] [blame] | 111 | }; |
| 112 | if (!mAnimatable.shouldDeferAnimationFinish(resetAndInvokeFinish)) { |
| 113 | resetAndInvokeFinish.run(); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 114 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Starts an animation. |
| 121 | * |
| 122 | * @param anim The object that bridges the controller, {@link SurfaceAnimator}, with the |
| 123 | * component responsible for running the animation. It runs the animation with |
| 124 | * {@link AnimationAdapter#startAnimation} once the hierarchy with |
| 125 | * the Leash has been set up. |
| 126 | * @param hidden Whether the container holding the child surfaces is currently visible or not. |
| 127 | * This is important as it will start with the leash hidden or visible before |
| 128 | * handing it to the component that is responsible to run the animation. |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 129 | * @param animationFinishedCallback The callback being triggered when the animation finishes. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 130 | */ |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 131 | void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden, |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 132 | @AnimationType int type, |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 133 | @Nullable OnAnimationFinishedCallback animationFinishedCallback, |
| 134 | @Nullable SurfaceFreezer freezer) { |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 135 | cancelAnimation(t, true /* restarting */, true /* forwardCancel */); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 136 | mAnimation = anim; |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 137 | mAnimationType = type; |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 138 | mAnimationFinishedCallback = animationFinishedCallback; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 139 | final SurfaceControl surface = mAnimatable.getSurfaceControl(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 140 | if (surface == null) { |
| 141 | Slog.w(TAG, "Unable to start animation, surface is null or no children."); |
| 142 | cancelAnimation(); |
| 143 | return; |
| 144 | } |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 145 | mLeash = freezer != null ? freezer.takeLeashForAnimation() : null; |
| 146 | if (mLeash == null) { |
Winson Chung | 7e895da | 2020-02-03 13:28:44 -0800 | [diff] [blame] | 147 | mLeash = createAnimationLeash(mAnimatable, surface, t, type, |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 148 | mAnimatable.getSurfaceWidth(), mAnimatable.getSurfaceHeight(), 0 /* x */, |
Vishnu Nair | 5e3e2ea | 2020-03-16 11:30:34 -0700 | [diff] [blame] | 149 | 0 /* y */, hidden, mService.mTransactionFactory); |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 150 | mAnimatable.onAnimationLeashCreated(t, mLeash); |
| 151 | } |
| 152 | mAnimatable.onLeashAnimationStarting(t, mLeash); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 153 | if (mAnimationStartDelayed) { |
| 154 | if (DEBUG_ANIM) Slog.i(TAG, "Animation start delayed"); |
| 155 | return; |
| 156 | } |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 157 | mAnimation.startAnimation(mLeash, t, type, mInnerAnimationFinishedCallback); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 160 | void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden, |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 161 | @AnimationType int type, |
| 162 | @Nullable OnAnimationFinishedCallback animationFinishedCallback) { |
| 163 | startAnimation(t, anim, hidden, type, animationFinishedCallback, null /* freezer */); |
| 164 | } |
| 165 | |
| 166 | void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden, |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 167 | @AnimationType int type) { |
| 168 | startAnimation(t, anim, hidden, type, null /* animationFinishedCallback */); |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 171 | /** |
| 172 | * Begins with delaying all animations to start. Any subsequent call to {@link #startAnimation} |
| 173 | * will not start the animation until {@link #endDelayingAnimationStart} is called. When an |
| 174 | * animation start is being delayed, the animator is considered animating already. |
| 175 | */ |
| 176 | void startDelayingAnimationStart() { |
| 177 | |
| 178 | // We only allow delaying animation start we are not currently animating |
| 179 | if (!isAnimating()) { |
| 180 | mAnimationStartDelayed = true; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * See {@link #startDelayingAnimationStart}. |
| 186 | */ |
| 187 | void endDelayingAnimationStart() { |
| 188 | final boolean delayed = mAnimationStartDelayed; |
| 189 | mAnimationStartDelayed = false; |
| 190 | if (delayed && mAnimation != null) { |
| 191 | mAnimation.startAnimation(mLeash, mAnimatable.getPendingTransaction(), |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 192 | mAnimationType, mInnerAnimationFinishedCallback); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 193 | mAnimatable.commitPendingTransaction(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @return Whether we are currently running an animation, or we have a pending animation that |
| 199 | * is waiting to be started with {@link #endDelayingAnimationStart} |
| 200 | */ |
| 201 | boolean isAnimating() { |
| 202 | return mAnimation != null; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @return The current animation spec if we are running an animation, or {@code null} otherwise. |
| 207 | */ |
| 208 | AnimationAdapter getAnimation() { |
| 209 | return mAnimation; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Cancels any currently running animation. |
| 214 | */ |
| 215 | void cancelAnimation() { |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 216 | cancelAnimation(mAnimatable.getPendingTransaction(), false /* restarting */, |
| 217 | true /* forwardCancel */); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 218 | mAnimatable.commitPendingTransaction(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the layer of the surface. |
| 223 | * <p> |
| 224 | * When the layer of the surface needs to be adjusted, we need to set it on the leash if the |
| 225 | * surface is reparented to the leash. This method takes care of that. |
| 226 | */ |
| 227 | void setLayer(Transaction t, int layer) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 228 | t.setLayer(mLeash != null ? mLeash : mAnimatable.getSurfaceControl(), layer); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Sets the surface to be relatively layered. |
| 233 | * |
| 234 | * @see #setLayer |
| 235 | */ |
| 236 | void setRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) { |
| 237 | t.setRelativeLayer(mLeash != null ? mLeash : mAnimatable.getSurfaceControl(), relativeTo, layer); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Reparents the surface. |
| 242 | * |
| 243 | * @see #setLayer |
| 244 | */ |
| 245 | void reparent(Transaction t, SurfaceControl newParent) { |
Robert Carr | 10584fa | 2019-01-14 15:55:19 -0800 | [diff] [blame] | 246 | t.reparent(mLeash != null ? mLeash : mAnimatable.getSurfaceControl(), newParent); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @return True if the surface is attached to the leash; false otherwise. |
| 251 | */ |
| 252 | boolean hasLeash() { |
| 253 | return mLeash != null; |
| 254 | } |
| 255 | |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 256 | void transferAnimation(SurfaceAnimator from) { |
| 257 | if (from.mLeash == null) { |
| 258 | return; |
| 259 | } |
| 260 | final SurfaceControl surface = mAnimatable.getSurfaceControl(); |
Jorim Jaggi | 596a199 | 2017-12-29 14:48:02 +0100 | [diff] [blame] | 261 | final SurfaceControl parent = mAnimatable.getAnimationLeashParent(); |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 262 | if (surface == null || parent == null) { |
| 263 | Slog.w(TAG, "Unable to transfer animation, surface or parent is null"); |
| 264 | cancelAnimation(); |
| 265 | return; |
| 266 | } |
| 267 | endDelayingAnimationStart(); |
| 268 | final Transaction t = mAnimatable.getPendingTransaction(); |
| 269 | cancelAnimation(t, true /* restarting */, true /* forwardCancel */); |
| 270 | mLeash = from.mLeash; |
| 271 | mAnimation = from.mAnimation; |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 272 | mAnimationType = from.mAnimationType; |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 273 | mAnimationFinishedCallback = from.mAnimationFinishedCallback; |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 274 | |
| 275 | // Cancel source animation, but don't let animation runner cancel the animation. |
| 276 | from.cancelAnimation(t, false /* restarting */, false /* forwardCancel */); |
Robert Carr | 10584fa | 2019-01-14 15:55:19 -0800 | [diff] [blame] | 277 | t.reparent(surface, mLeash); |
| 278 | t.reparent(mLeash, parent); |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 279 | mAnimatable.onAnimationLeashCreated(t, mLeash); |
| 280 | mService.mAnimationTransferMap.put(mAnimation, this); |
| 281 | } |
| 282 | |
Jorim Jaggi | c4d29f2 | 2018-03-22 16:30:56 +0100 | [diff] [blame] | 283 | boolean isAnimationStartDelayed() { |
| 284 | return mAnimationStartDelayed; |
| 285 | } |
| 286 | |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 287 | /** |
| 288 | * Cancels the animation, and resets the leash. |
| 289 | * |
| 290 | * @param t The transaction to use for all cancelling surface operations. |
| 291 | * @param restarting Whether we are restarting the animation. |
| 292 | * @param forwardCancel Whether to forward the cancel signal to the adapter executing the |
| 293 | * animation. This will be set to false when just transferring an animation |
| 294 | * to another animator. |
| 295 | */ |
| 296 | private void cancelAnimation(Transaction t, boolean restarting, boolean forwardCancel) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 297 | if (DEBUG_ANIM) Slog.i(TAG, "Cancelling animation restarting=" + restarting); |
| 298 | final SurfaceControl leash = mLeash; |
| 299 | final AnimationAdapter animation = mAnimation; |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 300 | final @AnimationType int animationType = mAnimationType; |
| 301 | final OnAnimationFinishedCallback animationFinishedCallback = mAnimationFinishedCallback; |
Robert Carr | 4a2d374 | 2019-02-11 12:15:38 -0800 | [diff] [blame] | 302 | reset(t, false); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 303 | if (animation != null) { |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 304 | if (!mAnimationStartDelayed && forwardCancel) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 305 | animation.onAnimationCancelled(leash); |
| 306 | } |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 307 | if (!restarting) { |
| 308 | if (mStaticAnimationFinishedCallback != null) { |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 309 | mStaticAnimationFinishedCallback.onAnimationFinished(animationType, animation); |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 310 | } |
| 311 | if (animationFinishedCallback != null) { |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 312 | animationFinishedCallback.onAnimationFinished(animationType, animation); |
Issei Suzuki | 2f54184 | 2020-01-09 20:18:29 +0100 | [diff] [blame] | 313 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
Robert Carr | 4a2d374 | 2019-02-11 12:15:38 -0800 | [diff] [blame] | 316 | |
| 317 | if (forwardCancel && leash != null) { |
| 318 | t.remove(leash); |
| 319 | mService.scheduleAnimationLocked(); |
| 320 | } |
| 321 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 322 | if (!restarting) { |
| 323 | mAnimationStartDelayed = false; |
| 324 | } |
| 325 | } |
| 326 | |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 327 | private void reset(Transaction t, boolean destroyLeash) { |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 328 | mService.mAnimationTransferMap.remove(mAnimation); |
| 329 | mAnimation = null; |
| 330 | mAnimationFinishedCallback = null; |
| 331 | mAnimationType = ANIMATION_TYPE_NONE; |
| 332 | if (mLeash == null) { |
| 333 | return; |
| 334 | } |
| 335 | SurfaceControl leash = mLeash; |
| 336 | mLeash = null; |
| 337 | final boolean scheduleAnim = removeLeash(t, mAnimatable, leash, destroyLeash); |
| 338 | if (scheduleAnim) { |
| 339 | mService.scheduleAnimationLocked(); |
| 340 | } |
| 341 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 342 | |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 343 | static boolean removeLeash(Transaction t, Animatable animatable, @NonNull SurfaceControl leash, |
| 344 | boolean destroy) { |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 345 | boolean scheduleAnim = false; |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 346 | final SurfaceControl surface = animatable.getSurfaceControl(); |
| 347 | final SurfaceControl parent = animatable.getParentSurfaceControl(); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 348 | |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 349 | // If the surface was destroyed or the leash is invalid, we don't care to reparent it back. |
| 350 | // Note that we also set this variable to true even if the parent isn't valid anymore, in |
| 351 | // order to ensure onAnimationLeashLost still gets called in this case. |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 352 | final boolean reparent = surface != null; |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 353 | if (reparent) { |
| 354 | if (DEBUG_ANIM) Slog.i(TAG, "Reparenting to original parent: " + parent); |
Robert Carr | 412f356 | 2019-04-11 13:01:31 -0700 | [diff] [blame] | 355 | // We shouldn't really need these isValid checks but we do |
| 356 | // b/130364451 |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 357 | if (surface.isValid() && parent != null && parent.isValid()) { |
Robert Carr | 412f356 | 2019-04-11 13:01:31 -0700 | [diff] [blame] | 358 | t.reparent(surface, parent); |
| 359 | scheduleAnim = true; |
| 360 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 361 | } |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 362 | if (destroy) { |
| 363 | t.remove(leash); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 364 | scheduleAnim = true; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 365 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 366 | |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 367 | if (reparent) { |
| 368 | // Make sure to inform the animatable after the surface was reparented (or reparent |
| 369 | // wasn't possible, but we still need to invoke the callback) |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 370 | animatable.onAnimationLeashLost(t); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 371 | scheduleAnim = true; |
| 372 | } |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 373 | return scheduleAnim; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 376 | static SurfaceControl createAnimationLeash(Animatable animatable, SurfaceControl surface, |
Winson Chung | 7e895da | 2020-02-03 13:28:44 -0800 | [diff] [blame] | 377 | Transaction t, @AnimationType int type, int width, int height, int x, int y, |
Vishnu Nair | 5e3e2ea | 2020-03-16 11:30:34 -0700 | [diff] [blame] | 378 | boolean hidden, Supplier<Transaction> transactionFactory) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 379 | if (DEBUG_ANIM) Slog.i(TAG, "Reparenting to leash"); |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 380 | final SurfaceControl.Builder builder = animatable.makeAnimationLeash() |
| 381 | .setParent(animatable.getAnimationLeashParent()) |
Vishnu Nair | 10a09f5e | 2020-03-05 14:40:27 -0800 | [diff] [blame] | 382 | .setName(surface + " - animation-leash"); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 383 | final SurfaceControl leash = builder.build(); |
Vishnu Nair | 5e3e2ea | 2020-03-16 11:30:34 -0700 | [diff] [blame] | 384 | if (!hidden) { |
| 385 | // TODO(b/151665759) Defer reparent calls |
| 386 | // We want the leash to be visible immediately but we want to set the effects on |
| 387 | // the layer. Since the transaction used in this function may be deferred, we apply |
| 388 | // another transaction immediately with the correct visibility and effects. |
| 389 | // If this doesn't work, you will can see the 2/3 button nav bar flicker during |
| 390 | // seamless rotation. |
| 391 | transactionFactory.get().unsetColor(leash).show(leash).apply(); |
| 392 | } |
Louis Chang | b92aa02 | 2018-11-16 15:46:50 +0800 | [diff] [blame] | 393 | t.setWindowCrop(leash, width, height); |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 394 | t.setPosition(leash, x, y); |
chaviw | 516ef79 | 2019-07-26 16:11:55 -0700 | [diff] [blame] | 395 | t.show(leash); |
chaviw | 516ef79 | 2019-07-26 16:11:55 -0700 | [diff] [blame] | 396 | t.setAlpha(leash, hidden ? 0 : 1); |
Tiger Huang | 969c608 | 2019-12-24 20:08:57 +0800 | [diff] [blame] | 397 | |
Robert Carr | 10584fa | 2019-01-14 15:55:19 -0800 | [diff] [blame] | 398 | t.reparent(surface, leash); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 399 | return leash; |
| 400 | } |
| 401 | |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 402 | /** |
| 403 | * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 404 | * com.android.server.wm.SurfaceAnimatorProto}. |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 405 | * |
| 406 | * @param proto Stream to write the SurfaceAnimator object to. |
| 407 | * @param fieldId Field Id of the SurfaceAnimator as defined in the parent message. |
| 408 | * @hide |
| 409 | */ |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 410 | void dumpDebug(ProtoOutputStream proto, long fieldId) { |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 411 | final long token = proto.start(fieldId); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 412 | if (mAnimation != null) { |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 413 | mAnimation.dumpDebug(proto, ANIMATION_ADAPTER); |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 414 | } |
Nataniel Borges | 023ecb5 | 2019-01-16 14:15:43 -0800 | [diff] [blame] | 415 | if (mLeash != null) { |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 416 | mLeash.dumpDebug(proto, LEASH); |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 417 | } |
| 418 | proto.write(ANIMATION_START_DELAYED, mAnimationStartDelayed); |
| 419 | proto.end(token); |
| 420 | } |
| 421 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 422 | void dump(PrintWriter pw, String prefix) { |
Jorim Jaggi | f75d161 | 2018-02-27 15:05:21 +0100 | [diff] [blame] | 423 | pw.print(prefix); pw.print("mLeash="); pw.print(mLeash); |
| 424 | if (mAnimationStartDelayed) { |
| 425 | pw.print(" mAnimationStartDelayed="); pw.println(mAnimationStartDelayed); |
| 426 | } else { |
| 427 | pw.println(); |
| 428 | } |
| 429 | pw.print(prefix); pw.println("Animation:"); |
| 430 | if (mAnimation != null) { |
| 431 | mAnimation.dump(pw, prefix + " "); |
| 432 | } else { |
| 433 | pw.print(prefix); pw.println("null"); |
| 434 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 437 | |
| 438 | /** |
| 439 | * No animation is specified. |
| 440 | * @hide |
| 441 | */ |
| 442 | static final int ANIMATION_TYPE_NONE = 0; |
| 443 | |
| 444 | /** |
| 445 | * Animation for an app transition. |
| 446 | * @hide |
| 447 | */ |
| 448 | static final int ANIMATION_TYPE_APP_TRANSITION = 1; |
| 449 | |
| 450 | /** |
| 451 | * Animation for screen rotation. |
| 452 | * @hide |
| 453 | */ |
| 454 | static final int ANIMATION_TYPE_SCREEN_ROTATION = 2; |
| 455 | |
| 456 | /** |
| 457 | * Animation for dimming. |
| 458 | * @hide |
| 459 | */ |
| 460 | static final int ANIMATION_TYPE_DIMMER = 3; |
| 461 | |
| 462 | /** |
| 463 | * Animation for recent apps. |
| 464 | * @hide |
| 465 | */ |
| 466 | static final int ANIMATION_TYPE_RECENTS = 4; |
| 467 | |
| 468 | /** |
| 469 | * Animation for a {@link WindowState} without animating the activity. |
| 470 | * @hide |
| 471 | */ |
| 472 | static final int ANIMATION_TYPE_WINDOW_ANIMATION = 5; |
| 473 | |
| 474 | /** |
| 475 | * Animation to control insets. This is actually not an animation, but is used to give the |
| 476 | * client a leash over the system window causing insets. |
| 477 | * @hide |
| 478 | */ |
| 479 | static final int ANIMATION_TYPE_INSETS_CONTROL = 6; |
| 480 | |
| 481 | /** |
| 482 | * The type of the animation. |
| 483 | * @hide |
| 484 | */ |
| 485 | @IntDef(flag = true, prefix = { "ANIMATION_TYPE_" }, value = { |
| 486 | ANIMATION_TYPE_NONE, |
| 487 | ANIMATION_TYPE_APP_TRANSITION, |
| 488 | ANIMATION_TYPE_SCREEN_ROTATION, |
| 489 | ANIMATION_TYPE_DIMMER, |
| 490 | ANIMATION_TYPE_RECENTS, |
| 491 | ANIMATION_TYPE_WINDOW_ANIMATION, |
| 492 | ANIMATION_TYPE_INSETS_CONTROL |
| 493 | }) |
| 494 | @Retention(RetentionPolicy.SOURCE) |
| 495 | @interface AnimationType {} |
| 496 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 497 | /** |
| 498 | * Callback to be passed into {@link AnimationAdapter#startAnimation} to be invoked by the |
| 499 | * component that is running the animation when the animation is finished. |
| 500 | */ |
| 501 | interface OnAnimationFinishedCallback { |
Issei Suzuki | 8b995df | 2020-01-08 12:23:04 +0100 | [diff] [blame] | 502 | void onAnimationFinished(@AnimationType int type, AnimationAdapter anim); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Interface to be animated by {@link SurfaceAnimator}. |
| 507 | */ |
| 508 | interface Animatable { |
| 509 | |
| 510 | /** |
| 511 | * @return The pending transaction that will be committed in the next frame. |
| 512 | */ |
| 513 | @NonNull Transaction getPendingTransaction(); |
| 514 | |
| 515 | /** |
| 516 | * Schedules a commit of the pending transaction. |
| 517 | */ |
| 518 | void commitPendingTransaction(); |
| 519 | |
| 520 | /** |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 521 | * Called when the animation leash is created. Note that this is also called by |
| 522 | * {@link SurfaceFreezer}, so this doesn't mean we're about to start animating. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 523 | * |
| 524 | * @param t The transaction to use to apply any necessary changes. |
| 525 | * @param leash The leash that was created. |
| 526 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 527 | void onAnimationLeashCreated(Transaction t, SurfaceControl leash); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 528 | |
| 529 | /** |
Evan Rosky | 55bddd8 | 2020-01-29 13:07:18 -0800 | [diff] [blame] | 530 | * Called when the animator is about to start animating the leash. |
| 531 | * |
| 532 | * @param t The transaction to use to apply any necessary changes. |
| 533 | * @param leash The leash that was created. |
| 534 | */ |
| 535 | default void onLeashAnimationStarting(Transaction t, SurfaceControl leash) { } |
| 536 | |
| 537 | /** |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 538 | * Called when the leash is being destroyed, or when the leash is being transferred to |
| 539 | * another SurfaceAnimator. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 540 | * |
| 541 | * @param t The transaction to use to apply any necessary changes. |
| 542 | */ |
lumark | f6f3494 | 2019-04-29 16:56:50 +0800 | [diff] [blame] | 543 | void onAnimationLeashLost(Transaction t); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 544 | |
| 545 | /** |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 546 | * @return A new surface to be used for the animation leash, inserted at the correct |
| 547 | * position in the hierarchy. |
| 548 | */ |
| 549 | SurfaceControl.Builder makeAnimationLeash(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 550 | |
| 551 | /** |
Jorim Jaggi | 596a199 | 2017-12-29 14:48:02 +0100 | [diff] [blame] | 552 | * @return The parent that should be used for the animation leash. |
| 553 | */ |
| 554 | @Nullable SurfaceControl getAnimationLeashParent(); |
| 555 | |
| 556 | /** |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 557 | * @return The surface of the object to be animated. |
Robert Carr | dea7bf4 | 2019-04-04 12:02:51 -0700 | [diff] [blame] | 558 | * This SurfaceControl must be valid if non-null. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 559 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 560 | @Nullable SurfaceControl getSurfaceControl(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 561 | |
| 562 | /** |
| 563 | * @return The parent of the surface object to be animated. |
Robert Carr | dea7bf4 | 2019-04-04 12:02:51 -0700 | [diff] [blame] | 564 | * This SurfaceControl must be valid if non-null. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 565 | */ |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 566 | @Nullable SurfaceControl getParentSurfaceControl(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * @return The width of the surface to be animated. |
| 570 | */ |
| 571 | int getSurfaceWidth(); |
| 572 | |
| 573 | /** |
| 574 | * @return The height of the surface to be animated. |
| 575 | */ |
| 576 | int getSurfaceHeight(); |
Jorim Jaggi | 6de6101 | 2018-03-19 14:53:23 +0100 | [diff] [blame] | 577 | |
| 578 | /** |
| 579 | * Gets called when the animation is about to finish and gives the client the opportunity to |
| 580 | * defer finishing the animation, i.e. it keeps the leash around until the client calls |
| 581 | * {@link #cancelAnimation}. |
| 582 | * |
| 583 | * @param endDeferFinishCallback The callback to call when defer finishing should be ended. |
| 584 | * @return Whether the client would like to defer the animation finish. |
| 585 | */ |
| 586 | default boolean shouldDeferAnimationFinish(Runnable endDeferFinishCallback) { |
| 587 | return false; |
| 588 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 589 | } |
| 590 | } |