Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.systemui.pip; |
| 18 | |
Tony Huang | 4b8795d | 2020-05-29 16:08:31 +0800 | [diff] [blame] | 19 | import android.animation.AnimationHandler; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 20 | import android.animation.Animator; |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 21 | import android.animation.RectEvaluator; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
| 23 | import android.annotation.IntDef; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 25 | import android.view.SurfaceControl; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 26 | |
| 27 | import com.android.internal.annotations.VisibleForTesting; |
Tony Huang | 4b8795d | 2020-05-29 16:08:31 +0800 | [diff] [blame] | 28 | import com.android.internal.graphics.SfVsyncFrameCallbackProvider; |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 29 | import com.android.systemui.Interpolators; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 30 | |
| 31 | import java.lang.annotation.Retention; |
| 32 | import java.lang.annotation.RetentionPolicy; |
| 33 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 34 | import javax.inject.Inject; |
| 35 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Controller class of PiP animations (both from and to PiP mode). |
| 38 | */ |
| 39 | public class PipAnimationController { |
| 40 | private static final float FRACTION_START = 0f; |
| 41 | private static final float FRACTION_END = 1f; |
| 42 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 43 | public static final int ANIM_TYPE_BOUNDS = 0; |
| 44 | public static final int ANIM_TYPE_ALPHA = 1; |
| 45 | |
| 46 | @IntDef(prefix = { "ANIM_TYPE_" }, value = { |
| 47 | ANIM_TYPE_BOUNDS, |
| 48 | ANIM_TYPE_ALPHA |
| 49 | }) |
| 50 | @Retention(RetentionPolicy.SOURCE) |
| 51 | public @interface AnimationType {} |
| 52 | |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 53 | public static final int TRANSITION_DIRECTION_NONE = 0; |
| 54 | public static final int TRANSITION_DIRECTION_SAME = 1; |
| 55 | public static final int TRANSITION_DIRECTION_TO_PIP = 2; |
| 56 | public static final int TRANSITION_DIRECTION_TO_FULLSCREEN = 3; |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 57 | public static final int TRANSITION_DIRECTION_TO_SPLIT_SCREEN = 4; |
Hongwei Wang | 925a102 | 2020-07-14 15:26:32 -0700 | [diff] [blame^] | 58 | public static final int TRANSITION_DIRECTION_REMOVE_STACK = 5; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 59 | |
| 60 | @IntDef(prefix = { "TRANSITION_DIRECTION_" }, value = { |
| 61 | TRANSITION_DIRECTION_NONE, |
| 62 | TRANSITION_DIRECTION_SAME, |
| 63 | TRANSITION_DIRECTION_TO_PIP, |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 64 | TRANSITION_DIRECTION_TO_FULLSCREEN, |
Hongwei Wang | 925a102 | 2020-07-14 15:26:32 -0700 | [diff] [blame^] | 65 | TRANSITION_DIRECTION_TO_SPLIT_SCREEN, |
| 66 | TRANSITION_DIRECTION_REMOVE_STACK |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 67 | }) |
| 68 | @Retention(RetentionPolicy.SOURCE) |
Hongwei Wang | 221fe3d | 2020-03-26 13:13:04 -0700 | [diff] [blame] | 69 | public @interface TransitionDirection {} |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 70 | |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 71 | public static boolean isInPipDirection(@TransitionDirection int direction) { |
| 72 | return direction == TRANSITION_DIRECTION_TO_PIP; |
| 73 | } |
| 74 | |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 75 | public static boolean isOutPipDirection(@TransitionDirection int direction) { |
| 76 | return direction == TRANSITION_DIRECTION_TO_FULLSCREEN |
| 77 | || direction == TRANSITION_DIRECTION_TO_SPLIT_SCREEN; |
| 78 | } |
| 79 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 80 | private final PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 81 | |
| 82 | private PipTransitionAnimator mCurrentAnimator; |
| 83 | |
Tony Huang | 4b8795d | 2020-05-29 16:08:31 +0800 | [diff] [blame] | 84 | private ThreadLocal<AnimationHandler> mSfAnimationHandlerThreadLocal = |
| 85 | ThreadLocal.withInitial(() -> { |
| 86 | AnimationHandler handler = new AnimationHandler(); |
| 87 | handler.setProvider(new SfVsyncFrameCallbackProvider()); |
| 88 | return handler; |
| 89 | }); |
| 90 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 91 | @Inject |
Hongwei Wang | 925a102 | 2020-07-14 15:26:32 -0700 | [diff] [blame^] | 92 | PipAnimationController(PipSurfaceTransactionHelper helper) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 93 | mSurfaceTransactionHelper = helper; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 96 | @SuppressWarnings("unchecked") |
| 97 | PipTransitionAnimator getAnimator(SurfaceControl leash, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 98 | Rect destinationBounds, float alphaStart, float alphaEnd) { |
| 99 | if (mCurrentAnimator == null) { |
| 100 | mCurrentAnimator = setupPipTransitionAnimator( |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 101 | PipTransitionAnimator.ofAlpha(leash, destinationBounds, alphaStart, alphaEnd)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 102 | } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA |
| 103 | && mCurrentAnimator.isRunning()) { |
| 104 | mCurrentAnimator.updateEndValue(alphaEnd); |
| 105 | } else { |
| 106 | mCurrentAnimator.cancel(); |
| 107 | mCurrentAnimator = setupPipTransitionAnimator( |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 108 | PipTransitionAnimator.ofAlpha(leash, destinationBounds, alphaStart, alphaEnd)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 109 | } |
| 110 | return mCurrentAnimator; |
| 111 | } |
| 112 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 113 | @SuppressWarnings("unchecked") |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 114 | PipTransitionAnimator getAnimator(SurfaceControl leash, Rect startBounds, Rect endBounds, |
| 115 | Rect sourceHintRect) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 116 | if (mCurrentAnimator == null) { |
| 117 | mCurrentAnimator = setupPipTransitionAnimator( |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 118 | PipTransitionAnimator.ofBounds(leash, startBounds, endBounds, sourceHintRect)); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 119 | } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA |
| 120 | && mCurrentAnimator.isRunning()) { |
| 121 | // If we are still animating the fade into pip, then just move the surface and ensure |
| 122 | // we update with the new destination bounds, but don't interrupt the existing animation |
| 123 | // with a new bounds |
| 124 | mCurrentAnimator.setDestinationBounds(endBounds); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 125 | } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_BOUNDS |
| 126 | && mCurrentAnimator.isRunning()) { |
| 127 | mCurrentAnimator.setDestinationBounds(endBounds); |
| 128 | // construct new Rect instances in case they are recycled |
| 129 | mCurrentAnimator.updateEndValue(new Rect(endBounds)); |
| 130 | } else { |
| 131 | mCurrentAnimator.cancel(); |
| 132 | mCurrentAnimator = setupPipTransitionAnimator( |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 133 | PipTransitionAnimator.ofBounds(leash, startBounds, endBounds, sourceHintRect)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 134 | } |
| 135 | return mCurrentAnimator; |
| 136 | } |
| 137 | |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 138 | PipTransitionAnimator getCurrentAnimator() { |
| 139 | return mCurrentAnimator; |
| 140 | } |
| 141 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 142 | private PipTransitionAnimator setupPipTransitionAnimator(PipTransitionAnimator animator) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 143 | animator.setSurfaceTransactionHelper(mSurfaceTransactionHelper); |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 144 | animator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 145 | animator.setFloatValues(FRACTION_START, FRACTION_END); |
Tony Huang | 4b8795d | 2020-05-29 16:08:31 +0800 | [diff] [blame] | 146 | animator.setAnimationHandler(mSfAnimationHandlerThreadLocal.get()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 147 | return animator; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Additional callback interface for PiP animation |
| 152 | */ |
| 153 | public static class PipAnimationCallback { |
| 154 | /** |
| 155 | * Called when PiP animation is started. |
| 156 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 157 | public void onPipAnimationStart(PipTransitionAnimator animator) {} |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Called when PiP animation is ended. |
| 161 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 162 | public void onPipAnimationEnd(SurfaceControl.Transaction tx, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 163 | PipTransitionAnimator animator) {} |
| 164 | |
| 165 | /** |
| 166 | * Called when PiP animation is cancelled. |
| 167 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 168 | public void onPipAnimationCancel(PipTransitionAnimator animator) {} |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Animator for PiP transition animation which supports both alpha and bounds animation. |
| 173 | * @param <T> Type of property to animate, either alpha (float) or bounds (Rect) |
| 174 | */ |
| 175 | public abstract static class PipTransitionAnimator<T> extends ValueAnimator implements |
| 176 | ValueAnimator.AnimatorUpdateListener, |
| 177 | ValueAnimator.AnimatorListener { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 178 | private final SurfaceControl mLeash; |
| 179 | private final @AnimationType int mAnimationType; |
| 180 | private final Rect mDestinationBounds = new Rect(); |
| 181 | |
Tony Huang | 5ebe019 | 2020-05-15 19:27:54 +0800 | [diff] [blame] | 182 | protected T mCurrentValue; |
| 183 | protected T mStartValue; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 184 | private T mEndValue; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 185 | private PipAnimationCallback mPipAnimationCallback; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 186 | private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory |
| 187 | mSurfaceControlTransactionFactory; |
| 188 | private PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 189 | private @TransitionDirection int mTransitionDirection; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 190 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 191 | private PipTransitionAnimator(SurfaceControl leash, @AnimationType int animationType, |
| 192 | Rect destinationBounds, T startValue, T endValue) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 193 | mLeash = leash; |
| 194 | mAnimationType = animationType; |
| 195 | mDestinationBounds.set(destinationBounds); |
| 196 | mStartValue = startValue; |
| 197 | mEndValue = endValue; |
| 198 | addListener(this); |
| 199 | addUpdateListener(this); |
| 200 | mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 201 | mTransitionDirection = TRANSITION_DIRECTION_NONE; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void onAnimationStart(Animator animation) { |
| 206 | mCurrentValue = mStartValue; |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 207 | onStartTransaction(mLeash, newSurfaceControlTransaction()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 208 | if (mPipAnimationCallback != null) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 209 | mPipAnimationCallback.onPipAnimationStart(this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onAnimationUpdate(ValueAnimator animation) { |
| 215 | applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(), |
| 216 | animation.getAnimatedFraction()); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onAnimationEnd(Animator animation) { |
| 221 | mCurrentValue = mEndValue; |
| 222 | final SurfaceControl.Transaction tx = newSurfaceControlTransaction(); |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 223 | onEndTransaction(mLeash, tx); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 224 | if (mPipAnimationCallback != null) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 225 | mPipAnimationCallback.onPipAnimationEnd(tx, this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void onAnimationCancel(Animator animation) { |
| 231 | if (mPipAnimationCallback != null) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 232 | mPipAnimationCallback.onPipAnimationCancel(this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | @Override public void onAnimationRepeat(Animator animation) {} |
| 237 | |
| 238 | @AnimationType int getAnimationType() { |
| 239 | return mAnimationType; |
| 240 | } |
| 241 | |
| 242 | PipTransitionAnimator<T> setPipAnimationCallback(PipAnimationCallback callback) { |
| 243 | mPipAnimationCallback = callback; |
| 244 | return this; |
| 245 | } |
| 246 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 247 | @TransitionDirection int getTransitionDirection() { |
| 248 | return mTransitionDirection; |
| 249 | } |
| 250 | |
| 251 | PipTransitionAnimator<T> setTransitionDirection(@TransitionDirection int direction) { |
| 252 | if (direction != TRANSITION_DIRECTION_SAME) { |
| 253 | mTransitionDirection = direction; |
| 254 | } |
| 255 | return this; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | T getStartValue() { |
| 259 | return mStartValue; |
| 260 | } |
| 261 | |
| 262 | T getEndValue() { |
| 263 | return mEndValue; |
| 264 | } |
| 265 | |
| 266 | Rect getDestinationBounds() { |
| 267 | return mDestinationBounds; |
| 268 | } |
| 269 | |
| 270 | void setDestinationBounds(Rect destinationBounds) { |
| 271 | mDestinationBounds.set(destinationBounds); |
Hongwei Wang | 951dc02 | 2020-03-30 16:16:16 -0700 | [diff] [blame] | 272 | if (mAnimationType == ANIM_TYPE_ALPHA) { |
| 273 | onStartTransaction(mLeash, newSurfaceControlTransaction()); |
| 274 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void setCurrentValue(T value) { |
| 278 | mCurrentValue = value; |
| 279 | } |
| 280 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 281 | boolean shouldApplyCornerRadius() { |
Hongwei Wang | 5c52ff8 | 2020-04-20 16:02:30 -0700 | [diff] [blame] | 282 | return !isOutPipDirection(mTransitionDirection); |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 285 | boolean inScaleTransition() { |
| 286 | if (mAnimationType != ANIM_TYPE_BOUNDS) return false; |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 287 | return !isInPipDirection(getTransitionDirection()); |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 290 | /** |
| 291 | * Updates the {@link #mEndValue}. |
| 292 | * |
| 293 | * NOTE: Do not forget to call {@link #setDestinationBounds(Rect)} for bounds animation. |
| 294 | * This is typically used when we receive a shelf height adjustment during the bounds |
| 295 | * animation. In which case we can update the end bounds and keep the existing animation |
| 296 | * running instead of cancelling it. |
| 297 | */ |
| 298 | void updateEndValue(T endValue) { |
| 299 | mEndValue = endValue; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | SurfaceControl.Transaction newSurfaceControlTransaction() { |
| 303 | return mSurfaceControlTransactionFactory.getTransaction(); |
| 304 | } |
| 305 | |
| 306 | @VisibleForTesting |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 307 | void setSurfaceControlTransactionFactory( |
| 308 | PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 309 | mSurfaceControlTransactionFactory = factory; |
| 310 | } |
| 311 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 312 | PipSurfaceTransactionHelper getSurfaceTransactionHelper() { |
| 313 | return mSurfaceTransactionHelper; |
| 314 | } |
| 315 | |
| 316 | void setSurfaceTransactionHelper(PipSurfaceTransactionHelper helper) { |
| 317 | mSurfaceTransactionHelper = helper; |
| 318 | } |
| 319 | |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 320 | void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {} |
| 321 | |
| 322 | void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {} |
| 323 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 324 | abstract void applySurfaceControlTransaction(SurfaceControl leash, |
| 325 | SurfaceControl.Transaction tx, float fraction); |
| 326 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 327 | static PipTransitionAnimator<Float> ofAlpha(SurfaceControl leash, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 328 | Rect destinationBounds, float startValue, float endValue) { |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 329 | return new PipTransitionAnimator<Float>(leash, ANIM_TYPE_ALPHA, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 330 | destinationBounds, startValue, endValue) { |
| 331 | @Override |
| 332 | void applySurfaceControlTransaction(SurfaceControl leash, |
| 333 | SurfaceControl.Transaction tx, float fraction) { |
| 334 | final float alpha = getStartValue() * (1 - fraction) + getEndValue() * fraction; |
| 335 | setCurrentValue(alpha); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame] | 336 | getSurfaceTransactionHelper().alpha(tx, leash, alpha); |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 337 | tx.apply(); |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) { |
Hongwei Wang | 925a102 | 2020-07-14 15:26:32 -0700 | [diff] [blame^] | 342 | if (getTransitionDirection() == TRANSITION_DIRECTION_REMOVE_STACK) { |
| 343 | // while removing the pip stack, no extra work needs to be done here. |
| 344 | return; |
| 345 | } |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 346 | getSurfaceTransactionHelper() |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 347 | .resetScale(tx, leash, getDestinationBounds()) |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 348 | .crop(tx, leash, getDestinationBounds()) |
| 349 | .round(tx, leash, shouldApplyCornerRadius()); |
Winson Chung | 1b5d055 | 2020-04-06 19:28:49 -0700 | [diff] [blame] | 350 | tx.show(leash); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 351 | tx.apply(); |
| 352 | } |
Tony Huang | 5ebe019 | 2020-05-15 19:27:54 +0800 | [diff] [blame] | 353 | |
| 354 | @Override |
| 355 | void updateEndValue(Float endValue) { |
| 356 | super.updateEndValue(endValue); |
| 357 | mStartValue = mCurrentValue; |
| 358 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 359 | }; |
| 360 | } |
| 361 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 362 | static PipTransitionAnimator<Rect> ofBounds(SurfaceControl leash, |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 363 | Rect startValue, Rect endValue, Rect sourceHintRect) { |
| 364 | // Just for simplicity we'll interpolate between the source rect hint insets and empty |
| 365 | // insets to calculate the window crop |
| 366 | final Rect initialStartValue = new Rect(startValue); |
| 367 | final Rect sourceHintRectInsets = sourceHintRect != null |
| 368 | ? new Rect(sourceHintRect.left - startValue.left, |
| 369 | sourceHintRect.top - startValue.top, |
| 370 | startValue.right - sourceHintRect.right, |
| 371 | startValue.bottom - sourceHintRect.bottom) |
| 372 | : null; |
| 373 | final Rect sourceInsets = new Rect(0, 0, 0, 0); |
| 374 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 375 | // construct new Rect instances in case they are recycled |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 376 | return new PipTransitionAnimator<Rect>(leash, ANIM_TYPE_BOUNDS, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 377 | endValue, new Rect(startValue), new Rect(endValue)) { |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 378 | private final RectEvaluator mRectEvaluator = new RectEvaluator(new Rect()); |
| 379 | private final RectEvaluator mInsetsEvaluator = new RectEvaluator(new Rect()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 380 | |
| 381 | @Override |
| 382 | void applySurfaceControlTransaction(SurfaceControl leash, |
| 383 | SurfaceControl.Transaction tx, float fraction) { |
| 384 | final Rect start = getStartValue(); |
| 385 | final Rect end = getEndValue(); |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 386 | Rect bounds = mRectEvaluator.evaluate(fraction, start, end); |
| 387 | setCurrentValue(bounds); |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 388 | if (inScaleTransition()) { |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 389 | if (isOutPipDirection(getTransitionDirection())) { |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 390 | getSurfaceTransactionHelper().scale(tx, leash, end, bounds); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 391 | } else { |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 392 | getSurfaceTransactionHelper().scale(tx, leash, start, bounds); |
Winson Chung | c4d4ee8 | 2020-05-05 12:51:06 -0700 | [diff] [blame] | 393 | } |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 394 | } else { |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 395 | if (sourceHintRectInsets != null) { |
| 396 | Rect insets = mInsetsEvaluator.evaluate(fraction, sourceInsets, |
| 397 | sourceHintRectInsets); |
| 398 | getSurfaceTransactionHelper().scaleAndCrop(tx, leash, initialStartValue, |
| 399 | bounds, insets); |
| 400 | } else { |
| 401 | getSurfaceTransactionHelper().scale(tx, leash, start, bounds); |
| 402 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 403 | } |
| 404 | tx.apply(); |
| 405 | } |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 406 | |
| 407 | @Override |
| 408 | void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) { |
| 409 | getSurfaceTransactionHelper() |
| 410 | .alpha(tx, leash, 1f) |
| 411 | .round(tx, leash, shouldApplyCornerRadius()); |
Winson Chung | 1b5d055 | 2020-04-06 19:28:49 -0700 | [diff] [blame] | 412 | tx.show(leash); |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 413 | tx.apply(); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) { |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 418 | // NOTE: intentionally does not apply the transaction here. |
| 419 | // this end transaction should get executed synchronously with the final |
| 420 | // WindowContainerTransaction in task organizer |
Winson Chung | 3b4d856 | 2020-06-10 04:25:18 +0000 | [diff] [blame] | 421 | getSurfaceTransactionHelper() |
| 422 | .resetScale(tx, leash, getDestinationBounds()) |
Robert Carr | b80a55c | 2020-05-05 15:17:51 -0700 | [diff] [blame] | 423 | .crop(tx, leash, getDestinationBounds()); |
Hongwei Wang | 09c1f04 | 2020-03-18 13:47:45 -0700 | [diff] [blame] | 424 | } |
Tony Huang | 5ebe019 | 2020-05-15 19:27:54 +0800 | [diff] [blame] | 425 | |
| 426 | @Override |
| 427 | void updateEndValue(Rect endValue) { |
| 428 | super.updateEndValue(endValue); |
| 429 | if (mStartValue != null && mCurrentValue != null) { |
| 430 | mStartValue.set(mCurrentValue); |
| 431 | } |
| 432 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 433 | }; |
| 434 | } |
| 435 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 436 | } |