Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipAnimationController.java

raw symbols SCIP
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.wm.shell.pip;
18
19 import static android.util.RotationUtils.rotateBounds;
20 import static android.view.Surface.ROTATION_270;
21 import static android.view.Surface.ROTATION_90;
22
23 import android.animation.AnimationHandler;
24 import android.animation.Animator;
25 import android.animation.RectEvaluator;
26 import android.animation.ValueAnimator;
27 import android.annotation.IntDef;
28 import android.annotation.NonNull;
29 import android.app.TaskInfo;
30 import android.content.Context;
31 import android.graphics.Rect;
32 import android.view.Surface;
33 import android.view.SurfaceControl;
34 import android.window.TaskSnapshot;
35
36 import com.android.internal.annotations.VisibleForTesting;
37 import com.android.internal.graphics.SfVsyncFrameCallbackProvider;
38 import com.android.wm.shell.animation.Interpolators;
39 import com.android.wm.shell.transition.Transitions;
40
41 import java.lang.annotation.Retention;
42 import java.lang.annotation.RetentionPolicy;
43 import java.util.Objects;
44
45 /**
46 * Controller class of PiP animations (both from and to PiP mode).
47 */
48 public class PipAnimationController {
49 static final float FRACTION_START = 0f;
50 private static final float FRACTION_END = 1f;
51
52 public static final int ANIM_TYPE_BOUNDS = 0;
53 public static final int ANIM_TYPE_ALPHA = 1;
54
55 @IntDef(prefix = { "ANIM_TYPE_" }, value = {
56 ANIM_TYPE_BOUNDS,
57 ANIM_TYPE_ALPHA
58 })
59 @Retention(RetentionPolicy.SOURCE)
60 public @interface AnimationType {}
61
62 public static final int TRANSITION_DIRECTION_NONE = 0;
63 public static final int TRANSITION_DIRECTION_SAME = 1;
64 public static final int TRANSITION_DIRECTION_TO_PIP = 2;
65 public static final int TRANSITION_DIRECTION_LEAVE_PIP = 3;
66 public static final int TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN = 4;
67 public static final int TRANSITION_DIRECTION_REMOVE_STACK = 5;
68 public static final int TRANSITION_DIRECTION_SNAP_AFTER_RESIZE = 6;
69 public static final int TRANSITION_DIRECTION_USER_RESIZE = 7;
70 public static final int TRANSITION_DIRECTION_EXPAND_OR_UNEXPAND = 8;
71
72 @IntDef(prefix = { "TRANSITION_DIRECTION_" }, value = {
73 TRANSITION_DIRECTION_NONE,
74 TRANSITION_DIRECTION_SAME,
75 TRANSITION_DIRECTION_TO_PIP,
76 TRANSITION_DIRECTION_LEAVE_PIP,
77 TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN,
78 TRANSITION_DIRECTION_REMOVE_STACK,
79 TRANSITION_DIRECTION_SNAP_AFTER_RESIZE,
80 TRANSITION_DIRECTION_USER_RESIZE,
81 TRANSITION_DIRECTION_EXPAND_OR_UNEXPAND
82 })
83 @Retention(RetentionPolicy.SOURCE)
84 public @interface TransitionDirection {}
85
86 public static boolean isInPipDirection(@TransitionDirection int direction) {
87 return direction == TRANSITION_DIRECTION_TO_PIP;
88 }
89
90 public static boolean isOutPipDirection(@TransitionDirection int direction) {
91 return direction == TRANSITION_DIRECTION_LEAVE_PIP
92 || direction == TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN;
93 }
94
95 /** Whether the given direction represents removing PIP. */
96 public static boolean isRemovePipDirection(@TransitionDirection int direction) {
97 return direction == TRANSITION_DIRECTION_REMOVE_STACK;
98 }
99
100 private final PipSurfaceTransactionHelper mSurfaceTransactionHelper;
101
102 private final ThreadLocal<AnimationHandler> mSfAnimationHandlerThreadLocal =
103 ThreadLocal.withInitial(() -> {
104 AnimationHandler handler = new AnimationHandler();
105 handler.setProvider(new SfVsyncFrameCallbackProvider());
106 return handler;
107 });
108
109 private PipTransitionAnimator mCurrentAnimator;
110
111 public PipAnimationController(PipSurfaceTransactionHelper helper) {
112 mSurfaceTransactionHelper = helper;
113 }
114
115 @SuppressWarnings("unchecked")
116 @VisibleForTesting
117 public PipTransitionAnimator getAnimator(TaskInfo taskInfo, SurfaceControl leash,
118 Rect destinationBounds, float alphaStart, float alphaEnd) {
119 if (mCurrentAnimator == null) {
120 mCurrentAnimator = setupPipTransitionAnimator(
121 PipTransitionAnimator.ofAlpha(taskInfo, leash, destinationBounds, alphaStart,
122 alphaEnd));
123 } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA
124 && Objects.equals(destinationBounds, mCurrentAnimator.getDestinationBounds())
125 && mCurrentAnimator.isRunning()) {
126 mCurrentAnimator.updateEndValue(alphaEnd);
127 } else {
128 mCurrentAnimator.cancel();
129 mCurrentAnimator = setupPipTransitionAnimator(
130 PipTransitionAnimator.ofAlpha(taskInfo, leash, destinationBounds, alphaStart,
131 alphaEnd));
132 }
133 return mCurrentAnimator;
134 }
135
136 @SuppressWarnings("unchecked")
137 /**
138 * Construct and return an animator that animates from the {@param startBounds} to the
139 * {@param endBounds} with the given {@param direction}. If {@param direction} is type
140 * {@link ANIM_TYPE_BOUNDS}, then {@param sourceHintRect} will be used to animate
141 * in a better, more smooth manner. If the original bound was rotated and a reset needs to
142 * happen, pass in {@param startingAngle}.
143 *
144 * In the case where one wants to start animation during an intermediate animation (for example,
145 * if the user is currently doing a pinch-resize, and upon letting go now PiP needs to animate
146 * to the correct snap fraction region), then provide the base bounds, which is current PiP
147 * leash bounds before transformation/any animation. This is so when we try to construct
148 * the different transformation matrices for the animation, we are constructing this based off
149 * the PiP original bounds, rather than the {@param startBounds}, which is post-transformed.
150 *
151 * If non-zero {@param rotationDelta} is given, it means that the display will be rotated by
152 * leaving PiP to fullscreen, and the {@param endBounds} is the fullscreen bounds before the
153 * rotation change.
154 */
155 @VisibleForTesting
156 public PipTransitionAnimator getAnimator(TaskInfo taskInfo, SurfaceControl leash,
157 Rect baseBounds, Rect startBounds, Rect endBounds, Rect sourceHintRect,
158 @PipAnimationController.TransitionDirection int direction, float startingAngle,
159 @Surface.Rotation int rotationDelta) {
160 if (mCurrentAnimator == null) {
161 mCurrentAnimator = setupPipTransitionAnimator(
162 PipTransitionAnimator.ofBounds(taskInfo, leash, startBounds, startBounds,
163 endBounds, sourceHintRect, direction, 0 /* startingAngle */,
164 rotationDelta));
165 } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA
166 && mCurrentAnimator.isRunning()) {
167 // If we are still animating the fade into pip, then just move the surface and ensure
168 // we update with the new destination bounds, but don't interrupt the existing animation
169 // with a new bounds
170 mCurrentAnimator.setDestinationBounds(endBounds);
171 } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_BOUNDS
172 && mCurrentAnimator.isRunning()) {
173 mCurrentAnimator.setDestinationBounds(endBounds);
174 // construct new Rect instances in case they are recycled
175 mCurrentAnimator.updateEndValue(new Rect(endBounds));
176 } else {
177 mCurrentAnimator.cancel();
178 mCurrentAnimator = setupPipTransitionAnimator(
179 PipTransitionAnimator.ofBounds(taskInfo, leash, baseBounds, startBounds,
180 endBounds, sourceHintRect, direction, startingAngle, rotationDelta));
181 }
182 return mCurrentAnimator;
183 }
184
185 public PipTransitionAnimator getCurrentAnimator() {
186 return mCurrentAnimator;
187 }
188
189 private PipTransitionAnimator setupPipTransitionAnimator(PipTransitionAnimator animator) {
190 animator.setSurfaceTransactionHelper(mSurfaceTransactionHelper);
191 animator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
192 animator.setFloatValues(FRACTION_START, FRACTION_END);
193 animator.setAnimationHandler(mSfAnimationHandlerThreadLocal.get());
194 return animator;
195 }
196
197 /**
198 * Returns true if the PiP window is currently being animated.
199 */
200 public boolean isAnimating() {
201 PipAnimationController.PipTransitionAnimator animator = getCurrentAnimator();
202 if (animator != null && animator.isRunning()) {
203 return true;
204 }
205 return false;
206 }
207
208 /**
209 * Quietly cancel the animator by removing the listeners first.
210 */
211 static void quietCancel(@NonNull ValueAnimator animator) {
212 animator.removeAllUpdateListeners();
213 animator.removeAllListeners();
214 animator.cancel();
215 }
216
217 /**
218 * Additional callback interface for PiP animation
219 */
220 public static class PipAnimationCallback {
221 /**
222 * Called when PiP animation is started.
223 */
224 public void onPipAnimationStart(TaskInfo taskInfo, PipTransitionAnimator animator) {}
225
226 /**
227 * Called when PiP animation is ended.
228 */
229 public void onPipAnimationEnd(TaskInfo taskInfo, SurfaceControl.Transaction tx,
230 PipTransitionAnimator animator) {}
231
232 /**
233 * Called when PiP animation is cancelled.
234 */
235 public void onPipAnimationCancel(TaskInfo taskInfo, PipTransitionAnimator animator) {}
236 }
237
238 /**
239 * A handler class that could register itself to apply the transaction instead of the
240 * animation controller doing it. For example, the menu controller can be one such handler.
241 */
242 public static class PipTransactionHandler {
243
244 /**
245 * Called when the animation controller is about to apply a transaction. Allow a registered
246 * handler to apply the transaction instead.
247 *
248 * @return true if handled by the handler, false otherwise.
249 */
250 public boolean handlePipTransaction(SurfaceControl leash, SurfaceControl.Transaction tx,
251 Rect destinationBounds) {
252 return false;
253 }
254 }
255
256 /**
257 * Animator for PiP transition animation which supports both alpha and bounds animation.
258 * @param <T> Type of property to animate, either alpha (float) or bounds (Rect)
259 */
260 public abstract static class PipTransitionAnimator<T> extends ValueAnimator implements
261 ValueAnimator.AnimatorUpdateListener,
262 ValueAnimator.AnimatorListener {
263 private final TaskInfo mTaskInfo;
264 private final SurfaceControl mLeash;
265 private final @AnimationType int mAnimationType;
266 private final Rect mDestinationBounds = new Rect();
267
268 private T mBaseValue;
269 protected T mCurrentValue;
270 protected T mStartValue;
271 private T mEndValue;
272 private PipAnimationCallback mPipAnimationCallback;
273 private PipTransactionHandler mPipTransactionHandler;
274 private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory
275 mSurfaceControlTransactionFactory;
276 private PipSurfaceTransactionHelper mSurfaceTransactionHelper;
277 private @TransitionDirection int mTransitionDirection;
278 protected PipContentOverlay mContentOverlay;
279
280 private PipTransitionAnimator(TaskInfo taskInfo, SurfaceControl leash,
281 @AnimationType int animationType,
282 Rect destinationBounds, T baseValue, T startValue, T endValue) {
283 mTaskInfo = taskInfo;
284 mLeash = leash;
285 mAnimationType = animationType;
286 mDestinationBounds.set(destinationBounds);
287 mBaseValue = baseValue;
288 mStartValue = startValue;
289 mEndValue = endValue;
290 addListener(this);
291 addUpdateListener(this);
292 mSurfaceControlTransactionFactory =
293 new PipSurfaceTransactionHelper.VsyncSurfaceControlTransactionFactory();
294 mTransitionDirection = TRANSITION_DIRECTION_NONE;
295 }
296
297 @Override
298 public void onAnimationStart(Animator animation) {
299 mCurrentValue = mStartValue;
300 onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction());
301 if (mPipAnimationCallback != null) {
302 mPipAnimationCallback.onPipAnimationStart(mTaskInfo, this);
303 }
304 }
305
306 @Override
307 public void onAnimationUpdate(ValueAnimator animation) {
308 applySurfaceControlTransaction(mLeash,
309 mSurfaceControlTransactionFactory.getTransaction(),
310 animation.getAnimatedFraction());
311 }
312
313 @Override
314 public void onAnimationEnd(Animator animation) {
315 mCurrentValue = mEndValue;
316 final SurfaceControl.Transaction tx =
317 mSurfaceControlTransactionFactory.getTransaction();
318 onEndTransaction(mLeash, tx, mTransitionDirection);
319 if (mPipAnimationCallback != null) {
320 mPipAnimationCallback.onPipAnimationEnd(mTaskInfo, tx, this);
321 }
322 mTransitionDirection = TRANSITION_DIRECTION_NONE;
323 }
324
325 @Override
326 public void onAnimationCancel(Animator animation) {
327 if (mPipAnimationCallback != null) {
328 mPipAnimationCallback.onPipAnimationCancel(mTaskInfo, this);
329 }
330 mTransitionDirection = TRANSITION_DIRECTION_NONE;
331 }
332
333 @Override public void onAnimationRepeat(Animator animation) {}
334
335 @VisibleForTesting
336 @AnimationType public int getAnimationType() {
337 return mAnimationType;
338 }
339
340 @VisibleForTesting
341 public PipTransitionAnimator<T> setPipAnimationCallback(PipAnimationCallback callback) {
342 mPipAnimationCallback = callback;
343 return this;
344 }
345
346 PipTransitionAnimator<T> setPipTransactionHandler(PipTransactionHandler handler) {
347 mPipTransactionHandler = handler;
348 return this;
349 }
350
351 boolean handlePipTransaction(SurfaceControl leash, SurfaceControl.Transaction tx,
352 Rect destinationBounds) {
353 if (mPipTransactionHandler != null) {
354 return mPipTransactionHandler.handlePipTransaction(leash, tx, destinationBounds);
355 }
356 return false;
357 }
358
359 SurfaceControl getContentOverlayLeash() {
360 return mContentOverlay == null ? null : mContentOverlay.mLeash;
361 }
362
363 void setColorContentOverlay(Context context) {
364 final SurfaceControl.Transaction tx =
365 mSurfaceControlTransactionFactory.getTransaction();
366 if (mContentOverlay != null) {
367 mContentOverlay.detach(tx);
368 }
369 mContentOverlay = new PipContentOverlay.PipColorOverlay(context);
370 mContentOverlay.attach(tx, mLeash);
371 }
372
373 void setSnapshotContentOverlay(TaskSnapshot snapshot, Rect sourceRectHint) {
374 final SurfaceControl.Transaction tx =
375 mSurfaceControlTransactionFactory.getTransaction();
376 if (mContentOverlay != null) {
377 mContentOverlay.detach(tx);
378 }
379 mContentOverlay = new PipContentOverlay.PipSnapshotOverlay(snapshot, sourceRectHint);
380 mContentOverlay.attach(tx, mLeash);
381 }
382
383 /**
384 * Clears the {@link #mContentOverlay}, this should be done after the content overlay is
385 * faded out, such as in {@link PipTaskOrganizer#fadeOutAndRemoveOverlay}
386 */
387 void clearContentOverlay() {
388 mContentOverlay = null;
389 }
390
391 @VisibleForTesting
392 @TransitionDirection public int getTransitionDirection() {
393 return mTransitionDirection;
394 }
395
396 @VisibleForTesting
397 public PipTransitionAnimator<T> setTransitionDirection(@TransitionDirection int direction) {
398 if (direction != TRANSITION_DIRECTION_SAME) {
399 mTransitionDirection = direction;
400 }
401 return this;
402 }
403
404 T getStartValue() {
405 return mStartValue;
406 }
407
408 T getBaseValue() {
409 return mBaseValue;
410 }
411
412 @VisibleForTesting
413 public T getEndValue() {
414 return mEndValue;
415 }
416
417 Rect getDestinationBounds() {
418 return mDestinationBounds;
419 }
420
421 void setDestinationBounds(Rect destinationBounds) {
422 mDestinationBounds.set(destinationBounds);
423 if (mAnimationType == ANIM_TYPE_ALPHA) {
424 onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction());
425 }
426 }
427
428 void setCurrentValue(T value) {
429 mCurrentValue = value;
430 }
431
432 boolean shouldApplyCornerRadius() {
433 return !isOutPipDirection(mTransitionDirection);
434 }
435
436 boolean shouldApplyShadowRadius() {
437 return !isOutPipDirection(mTransitionDirection)
438 && !isRemovePipDirection(mTransitionDirection);
439 }
440
441 boolean inScaleTransition() {
442 if (mAnimationType != ANIM_TYPE_BOUNDS) return false;
443 final int direction = getTransitionDirection();
444 return !isInPipDirection(direction) && !isOutPipDirection(direction);
445 }
446
447 /**
448 * Updates the {@link #mEndValue}.
449 *
450 * NOTE: Do not forget to call {@link #setDestinationBounds(Rect)} for bounds animation.
451 * This is typically used when we receive a shelf height adjustment during the bounds
452 * animation. In which case we can update the end bounds and keep the existing animation
453 * running instead of cancelling it.
454 */
455 public void updateEndValue(T endValue) {
456 mEndValue = endValue;
457 }
458
459 @VisibleForTesting
460 public void setSurfaceControlTransactionFactory(
461 PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) {
462 mSurfaceControlTransactionFactory = factory;
463 }
464
465 PipSurfaceTransactionHelper getSurfaceTransactionHelper() {
466 return mSurfaceTransactionHelper;
467 }
468
469 void setSurfaceTransactionHelper(PipSurfaceTransactionHelper helper) {
470 mSurfaceTransactionHelper = helper;
471 }
472
473 void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {}
474
475 void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx,
476 @TransitionDirection int transitionDirection) {}
477
478 abstract void applySurfaceControlTransaction(SurfaceControl leash,
479 SurfaceControl.Transaction tx, float fraction);
480
481 static PipTransitionAnimator<Float> ofAlpha(TaskInfo taskInfo, SurfaceControl leash,
482 Rect destinationBounds, float startValue, float endValue) {
483 return new PipTransitionAnimator<Float>(taskInfo, leash, ANIM_TYPE_ALPHA,
484 destinationBounds, startValue, startValue, endValue) {
485 @Override
486 void applySurfaceControlTransaction(SurfaceControl leash,
487 SurfaceControl.Transaction tx, float fraction) {
488 final float alpha = getStartValue() * (1 - fraction) + getEndValue() * fraction;
489 setCurrentValue(alpha);
490 getSurfaceTransactionHelper().alpha(tx, leash, alpha)
491 .round(tx, leash, shouldApplyCornerRadius())
492 .shadow(tx, leash, shouldApplyShadowRadius());
493 tx.apply();
494 }
495
496 @Override
497 void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {
498 if (getTransitionDirection() == TRANSITION_DIRECTION_REMOVE_STACK) {
499 // while removing the pip stack, no extra work needs to be done here.
500 return;
501 }
502 getSurfaceTransactionHelper()
503 .resetScale(tx, leash, getDestinationBounds())
504 .crop(tx, leash, getDestinationBounds())
505 .round(tx, leash, shouldApplyCornerRadius())
506 .shadow(tx, leash, shouldApplyShadowRadius());
507 tx.show(leash);
508 tx.apply();
509 }
510
511 @Override
512 public void updateEndValue(Float endValue) {
513 super.updateEndValue(endValue);
514 mStartValue = mCurrentValue;
515 }
516 };
517 }
518
519 static PipTransitionAnimator<Rect> ofBounds(TaskInfo taskInfo, SurfaceControl leash,
520 Rect baseValue, Rect startValue, Rect endValue, Rect sourceHintRect,
521 @PipAnimationController.TransitionDirection int direction, float startingAngle,
522 @Surface.Rotation int rotationDelta) {
523 final boolean isOutPipDirection = isOutPipDirection(direction);
524 final boolean isInPipDirection = isInPipDirection(direction);
525 // Just for simplicity we'll interpolate between the source rect hint insets and empty
526 // insets to calculate the window crop
527 final Rect initialSourceValue;
528 if (isOutPipDirection) {
529 initialSourceValue = new Rect(endValue);
530 } else {
531 initialSourceValue = new Rect(baseValue);
532 }
533
534 final Rect rotatedEndRect;
535 final Rect lastEndRect;
536 final Rect initialContainerRect;
537 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
538 lastEndRect = new Rect(endValue);
539 rotatedEndRect = new Rect(endValue);
540 // Rotate the end bounds according to the rotation delta because the display will
541 // be rotated to the same orientation.
542 rotateBounds(rotatedEndRect, initialSourceValue, rotationDelta);
543 // Use the rect that has the same orientation as the hint rect.
544 initialContainerRect = isOutPipDirection ? rotatedEndRect : initialSourceValue;
545 } else {
546 rotatedEndRect = lastEndRect = null;
547 initialContainerRect = initialSourceValue;
548 }
549
550 final Rect sourceHintRectInsets;
551 if (sourceHintRect == null) {
552 sourceHintRectInsets = null;
553 } else {
554 sourceHintRectInsets = new Rect(sourceHintRect.left - initialContainerRect.left,
555 sourceHintRect.top - initialContainerRect.top,
556 initialContainerRect.right - sourceHintRect.right,
557 initialContainerRect.bottom - sourceHintRect.bottom);
558 }
559 final Rect zeroInsets = new Rect(0, 0, 0, 0);
560
561 // construct new Rect instances in case they are recycled
562 return new PipTransitionAnimator<Rect>(taskInfo, leash, ANIM_TYPE_BOUNDS,
563 endValue, new Rect(baseValue), new Rect(startValue), new Rect(endValue)) {
564 private final RectEvaluator mRectEvaluator = new RectEvaluator(new Rect());
565 private final RectEvaluator mInsetsEvaluator = new RectEvaluator(new Rect());
566
567 @Override
568 void applySurfaceControlTransaction(SurfaceControl leash,
569 SurfaceControl.Transaction tx, float fraction) {
570 final Rect base = getBaseValue();
571 final Rect start = getStartValue();
572 final Rect end = getEndValue();
573 if (mContentOverlay != null) {
574 mContentOverlay.onAnimationUpdate(tx, fraction);
575 }
576 if (rotatedEndRect != null) {
577 // Animate the bounds in a different orientation. It only happens when
578 // switching between PiP and fullscreen.
579 applyRotation(tx, leash, fraction, start, end);
580 return;
581 }
582 Rect bounds = mRectEvaluator.evaluate(fraction, start, end);
583 float angle = (1.0f - fraction) * startingAngle;
584 setCurrentValue(bounds);
585 if (inScaleTransition() || sourceHintRect == null) {
586 if (isOutPipDirection) {
587 getSurfaceTransactionHelper().crop(tx, leash, end)
588 .scale(tx, leash, end, bounds);
589 } else {
590 getSurfaceTransactionHelper().crop(tx, leash, base)
591 .scale(tx, leash, base, bounds, angle)
592 .round(tx, leash, base, bounds)
593 .shadow(tx, leash, shouldApplyShadowRadius());
594 }
595 } else {
596 final Rect insets = computeInsets(fraction);
597 getSurfaceTransactionHelper().scaleAndCrop(tx, leash,
598 sourceHintRect, initialSourceValue, bounds, insets,
599 isInPipDirection, fraction);
600 if (shouldApplyCornerRadius()) {
601 final Rect sourceBounds = new Rect(initialContainerRect);
602 sourceBounds.inset(insets);
603 getSurfaceTransactionHelper()
604 .round(tx, leash, sourceBounds, bounds)
605 .shadow(tx, leash, shouldApplyShadowRadius());
606 }
607 }
608 if (!handlePipTransaction(leash, tx, bounds)) {
609 tx.apply();
610 }
611 }
612
613 private void applyRotation(SurfaceControl.Transaction tx, SurfaceControl leash,
614 float fraction, Rect start, Rect end) {
615 if (!end.equals(lastEndRect)) {
616 // If the end bounds are changed during animating (e.g. shelf height), the
617 // rotated end bounds also need to be updated.
618 rotatedEndRect.set(endValue);
619 rotateBounds(rotatedEndRect, initialSourceValue, rotationDelta);
620 lastEndRect.set(end);
621 }
622 final Rect bounds = mRectEvaluator.evaluate(fraction, start, rotatedEndRect);
623 setCurrentValue(bounds);
624 final Rect insets = computeInsets(fraction);
625 final float degree, x, y;
626 if (Transitions.SHELL_TRANSITIONS_ROTATION) {
627 if (rotationDelta == ROTATION_90) {
628 degree = 90 * (1 - fraction);
629 x = fraction * (end.left - start.left)
630 + start.left + start.width() * (1 - fraction);
631 y = fraction * (end.top - start.top) + start.top;
632 } else {
633 degree = -90 * (1 - fraction);
634 x = fraction * (end.left - start.left) + start.left;
635 y = fraction * (end.top - start.top)
636 + start.top + start.height() * (1 - fraction);
637 }
638 } else {
639 if (rotationDelta == ROTATION_90) {
640 degree = 90 * fraction;
641 x = fraction * (end.right - start.left) + start.left;
642 y = fraction * (end.top - start.top) + start.top;
643 } else {
644 degree = -90 * fraction;
645 x = fraction * (end.left - start.left) + start.left;
646 y = fraction * (end.bottom - start.top) + start.top;
647 }
648 }
649 final Rect sourceBounds = new Rect(initialContainerRect);
650 sourceBounds.inset(insets);
651 getSurfaceTransactionHelper()
652 .rotateAndScaleWithCrop(tx, leash, initialContainerRect, bounds,
653 insets, degree, x, y, isOutPipDirection,
654 rotationDelta == ROTATION_270 /* clockwise */);
655 if (shouldApplyCornerRadius()) {
656 getSurfaceTransactionHelper()
657 .round(tx, leash, sourceBounds, bounds)
658 .shadow(tx, leash, shouldApplyShadowRadius());
659 }
660 tx.apply();
661 }
662
663 private Rect computeInsets(float fraction) {
664 if (sourceHintRectInsets == null) {
665 return zeroInsets;
666 }
667 final Rect startRect = isOutPipDirection ? sourceHintRectInsets : zeroInsets;
668 final Rect endRect = isOutPipDirection ? zeroInsets : sourceHintRectInsets;
669 return mInsetsEvaluator.evaluate(fraction, startRect, endRect);
670 }
671
672 @Override
673 void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {
674 getSurfaceTransactionHelper()
675 .alpha(tx, leash, 1f)
676 .round(tx, leash, shouldApplyCornerRadius())
677 .shadow(tx, leash, shouldApplyShadowRadius());
678 // TODO(b/178632364): this is a work around for the black background when
679 // entering PiP in button navigation mode.
680 if (isInPipDirection(direction)) {
681 tx.setWindowCrop(leash, getStartValue());
682 }
683 tx.show(leash);
684 tx.apply();
685 }
686
687 @Override
688 void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx,
689 int transitionDirection) {
690 // NOTE: intentionally does not apply the transaction here.
691 // this end transaction should get executed synchronously with the final
692 // WindowContainerTransaction in task organizer
693 final Rect destBounds = getDestinationBounds();
694 getSurfaceTransactionHelper().resetScale(tx, leash, destBounds);
695 if (isOutPipDirection(transitionDirection)) {
696 // Exit pip, clear scale, position and crop.
697 tx.setMatrix(leash, 1, 0, 0, 1);
698 tx.setPosition(leash, 0, 0);
699 tx.setWindowCrop(leash, 0, 0);
700 } else {
701 getSurfaceTransactionHelper().crop(tx, leash, destBounds);
702 }
703 if (mContentOverlay != null) {
704 mContentOverlay.onAnimationEnd(tx, destBounds);
705 }
706 }
707
708 @Override
709 public void updateEndValue(Rect endValue) {
710 super.updateEndValue(endValue);
711 if (mStartValue != null && mCurrentValue != null) {
712 mStartValue.set(mCurrentValue);
713 }
714 }
715 };
716 }
717 }
718 }