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 | 2d74faf | 2017-12-18 19:21:27 +0100 | [diff] [blame] | 19 | import static android.util.TimeUtils.NANOS_PER_MS; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 20 | import static android.view.Choreographer.CALLBACK_TRAVERSAL; |
| 21 | import static android.view.Choreographer.getSfInstance; |
| 22 | |
| 23 | import android.animation.AnimationHandler; |
| 24 | import android.animation.AnimationHandler.AnimationFrameCallbackProvider; |
| 25 | import android.animation.Animator; |
| 26 | import android.animation.AnimatorListenerAdapter; |
| 27 | import android.animation.ValueAnimator; |
| 28 | import android.annotation.Nullable; |
| 29 | import android.util.ArrayMap; |
| 30 | import android.view.Choreographer; |
| 31 | import android.view.SurfaceControl; |
| 32 | import android.view.SurfaceControl.Transaction; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.annotations.GuardedBy; |
| 35 | import com.android.internal.annotations.VisibleForTesting; |
| 36 | import com.android.internal.graphics.SfVsyncFrameCallbackProvider; |
| 37 | import com.android.server.AnimationThread; |
| 38 | import com.android.server.wm.LocalAnimationAdapter.AnimationSpec; |
| 39 | |
| 40 | /** |
| 41 | * Class to run animations without holding the window manager lock. |
| 42 | */ |
| 43 | class SurfaceAnimationRunner { |
| 44 | |
| 45 | private final Object mLock = new Object(); |
| 46 | |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 47 | /** |
| 48 | * Lock for cancelling animations. Must be acquired on it's own, or after acquiring |
| 49 | * {@link #mLock} |
| 50 | */ |
| 51 | private final Object mCancelLock = new Object(); |
| 52 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 53 | @VisibleForTesting |
| 54 | Choreographer mChoreographer; |
| 55 | |
| 56 | private final Runnable mApplyTransactionRunnable = this::applyTransaction; |
| 57 | private final AnimationHandler mAnimationHandler; |
| 58 | private final Transaction mFrameTransaction; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 59 | private final AnimatorFactory mAnimatorFactory; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 60 | private boolean mApplyScheduled; |
| 61 | |
| 62 | @GuardedBy("mLock") |
| 63 | @VisibleForTesting |
| 64 | final ArrayMap<SurfaceControl, RunningAnimation> mPendingAnimations = new ArrayMap<>(); |
| 65 | |
| 66 | @GuardedBy("mLock") |
| 67 | @VisibleForTesting |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 68 | final ArrayMap<SurfaceControl, RunningAnimation> mRunningAnimations = new ArrayMap<>(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 69 | |
Jorim Jaggi | d6d9716 | 2018-01-05 18:28:36 +0100 | [diff] [blame] | 70 | @GuardedBy("mLock") |
| 71 | private boolean mAnimationStartDeferred; |
| 72 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 73 | SurfaceAnimationRunner() { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 74 | this(null /* callbackProvider */, null /* animatorFactory */, new Transaction()); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @VisibleForTesting |
| 78 | SurfaceAnimationRunner(@Nullable AnimationFrameCallbackProvider callbackProvider, |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 79 | AnimatorFactory animatorFactory, Transaction frameTransaction) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 80 | SurfaceAnimationThread.getHandler().runWithScissors(() -> mChoreographer = getSfInstance(), |
| 81 | 0 /* timeout */); |
| 82 | mFrameTransaction = frameTransaction; |
| 83 | mAnimationHandler = new AnimationHandler(); |
| 84 | mAnimationHandler.setProvider(callbackProvider != null |
| 85 | ? callbackProvider |
| 86 | : new SfVsyncFrameCallbackProvider(mChoreographer)); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 87 | mAnimatorFactory = animatorFactory != null |
| 88 | ? animatorFactory |
| 89 | : SfValueAnimator::new; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Jorim Jaggi | d6d9716 | 2018-01-05 18:28:36 +0100 | [diff] [blame] | 92 | /** |
| 93 | * Defers starting of animations until {@link #continueStartingAnimations} is called. This |
| 94 | * method is NOT nestable. |
| 95 | * |
| 96 | * @see #continueStartingAnimations |
| 97 | */ |
| 98 | void deferStartingAnimations() { |
| 99 | synchronized (mLock) { |
| 100 | mAnimationStartDeferred = true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Continues starting of animations. |
| 106 | * |
| 107 | * @see #deferStartingAnimations |
| 108 | */ |
| 109 | void continueStartingAnimations() { |
| 110 | synchronized (mLock) { |
| 111 | mAnimationStartDeferred = false; |
| 112 | if (!mPendingAnimations.isEmpty()) { |
| 113 | mChoreographer.postFrameCallback(this::startAnimations); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 118 | void startAnimation(AnimationSpec a, SurfaceControl animationLeash, Transaction t, |
| 119 | Runnable finishCallback) { |
| 120 | synchronized (mLock) { |
| 121 | final RunningAnimation runningAnim = new RunningAnimation(a, animationLeash, |
| 122 | finishCallback); |
| 123 | mPendingAnimations.put(animationLeash, runningAnim); |
Jorim Jaggi | d6d9716 | 2018-01-05 18:28:36 +0100 | [diff] [blame] | 124 | if (!mAnimationStartDeferred) { |
| 125 | mChoreographer.postFrameCallback(this::startAnimations); |
| 126 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 127 | |
| 128 | // Some animations (e.g. move animations) require the initial transform to be applied |
| 129 | // immediately. |
| 130 | applyTransformation(runningAnim, t, 0 /* currentPlayTime */); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void onAnimationCancelled(SurfaceControl leash) { |
| 135 | synchronized (mLock) { |
| 136 | if (mPendingAnimations.containsKey(leash)) { |
| 137 | mPendingAnimations.remove(leash); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 138 | return; |
| 139 | } |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 140 | final RunningAnimation anim = mRunningAnimations.get(leash); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 141 | if (anim != null) { |
| 142 | mRunningAnimations.remove(leash); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 143 | synchronized (mCancelLock) { |
| 144 | anim.mCancelled = true; |
| 145 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 146 | SurfaceAnimationThread.getHandler().post(() -> { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 147 | anim.mAnim.cancel(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 148 | applyTransaction(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 149 | }); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private void startPendingAnimationsLocked() { |
| 155 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
| 156 | startAnimationLocked(mPendingAnimations.valueAt(i)); |
| 157 | } |
| 158 | mPendingAnimations.clear(); |
| 159 | } |
| 160 | |
| 161 | private void startAnimationLocked(RunningAnimation a) { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 162 | final ValueAnimator anim = mAnimatorFactory.makeAnimator(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 163 | |
| 164 | // Animation length is already expected to be scaled. |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 165 | anim.overrideDurationScale(1.0f); |
| 166 | anim.setDuration(a.mAnimSpec.getDuration()); |
| 167 | anim.addUpdateListener(animation -> { |
| 168 | synchronized (mCancelLock) { |
| 169 | if (!a.mCancelled) { |
| 170 | applyTransformation(a, mFrameTransaction, anim.getCurrentPlayTime()); |
| 171 | } |
| 172 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 173 | |
| 174 | // Transaction will be applied in the commit phase. |
| 175 | scheduleApplyTransaction(); |
| 176 | }); |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 177 | |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 178 | anim.addListener(new AnimatorListenerAdapter() { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 179 | @Override |
| 180 | public void onAnimationStart(Animator animation) { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 181 | synchronized (mCancelLock) { |
| 182 | if (!a.mCancelled) { |
| 183 | mFrameTransaction.show(a.mLeash); |
| 184 | } |
| 185 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void onAnimationEnd(Animator animation) { |
| 190 | synchronized (mLock) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 191 | mRunningAnimations.remove(a.mLeash); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 192 | synchronized (mCancelLock) { |
| 193 | if (!a.mCancelled) { |
Jorim Jaggi | 980c9de | 2017-11-17 01:41:37 +0100 | [diff] [blame] | 194 | |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 195 | // Post on other thread that we can push final state without jank. |
| 196 | AnimationThread.getHandler().post(a.mFinishCallback); |
| 197 | } |
| 198 | } |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | }); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 202 | anim.start(); |
Jorim Jaggi | 2d74faf | 2017-12-18 19:21:27 +0100 | [diff] [blame] | 203 | if (a.mAnimSpec.canSkipFirstFrame()) { |
| 204 | // If we can skip the first frame, we start one frame later. |
| 205 | anim.setCurrentPlayTime(mChoreographer.getFrameIntervalNanos() / NANOS_PER_MS); |
| 206 | } |
| 207 | |
| 208 | // Immediately start the animation by manually applying an animation frame. Otherwise, the |
| 209 | // start time would only be set in the next frame, leading to a delay. |
| 210 | anim.doAnimationFrame(mChoreographer.getFrameTime()); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 211 | a.mAnim = anim; |
| 212 | mRunningAnimations.put(a.mLeash, a); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private void applyTransformation(RunningAnimation a, Transaction t, long currentPlayTime) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 216 | a.mAnimSpec.apply(t, a.mLeash, currentPlayTime); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Jorim Jaggi | d6d9716 | 2018-01-05 18:28:36 +0100 | [diff] [blame] | 219 | private void startAnimations(long frameTimeNanos) { |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 220 | synchronized (mLock) { |
| 221 | startPendingAnimationsLocked(); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private void scheduleApplyTransaction() { |
| 226 | if (!mApplyScheduled) { |
| 227 | mChoreographer.postCallback(CALLBACK_TRAVERSAL, mApplyTransactionRunnable, |
| 228 | null /* token */); |
| 229 | mApplyScheduled = true; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private void applyTransaction() { |
Jorim Jaggi | 32fd84a | 2017-11-20 19:59:42 +0100 | [diff] [blame] | 234 | mFrameTransaction.setAnimationTransaction(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 235 | mFrameTransaction.apply(); |
| 236 | mApplyScheduled = false; |
| 237 | } |
| 238 | |
| 239 | private static final class RunningAnimation { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 240 | final AnimationSpec mAnimSpec; |
| 241 | final SurfaceControl mLeash; |
| 242 | final Runnable mFinishCallback; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 243 | ValueAnimator mAnim; |
| 244 | |
| 245 | @GuardedBy("mCancelLock") |
| 246 | private boolean mCancelled; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 247 | |
| 248 | RunningAnimation(AnimationSpec animSpec, SurfaceControl leash, Runnable finishCallback) { |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 249 | mAnimSpec = animSpec; |
| 250 | mLeash = leash; |
| 251 | mFinishCallback = finishCallback; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 255 | @VisibleForTesting |
| 256 | interface AnimatorFactory { |
| 257 | ValueAnimator makeAnimator(); |
| 258 | } |
| 259 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 260 | /** |
| 261 | * Value animator that uses sf-vsync signal to tick. |
| 262 | */ |
| 263 | private class SfValueAnimator extends ValueAnimator { |
| 264 | |
| 265 | SfValueAnimator() { |
| 266 | setFloatValues(0f, 1f); |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public AnimationHandler getAnimationHandler() { |
| 271 | return mAnimationHandler; |
| 272 | } |
| 273 | } |
| 274 | } |