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