Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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.app.WindowConfiguration.ROTATION_UNDEFINED;
20 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
21 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
22 import static android.util.RotationUtils.deltaRotation;
23 import static android.util.RotationUtils.rotateBounds;
24 import static android.view.Surface.ROTATION_270;
25 import static android.view.Surface.ROTATION_90;
26 import static android.view.WindowManager.TRANSIT_CHANGE;
27 import static android.view.WindowManager.TRANSIT_OPEN;
28 import static android.view.WindowManager.TRANSIT_PIP;
29 import static android.view.WindowManager.transitTypeToString;
30 import static android.window.TransitionInfo.FLAG_IS_DISPLAY;
31
32 import static com.android.wm.shell.pip.PipAnimationController.ANIM_TYPE_ALPHA;
33 import static com.android.wm.shell.pip.PipAnimationController.ANIM_TYPE_BOUNDS;
34 import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_LEAVE_PIP;
35 import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_SAME;
36 import static com.android.wm.shell.pip.PipAnimationController.TRANSITION_DIRECTION_TO_PIP;
37 import static com.android.wm.shell.pip.PipAnimationController.isInPipDirection;
38 import static com.android.wm.shell.pip.PipAnimationController.isOutPipDirection;
39 import static com.android.wm.shell.pip.PipTransitionState.ENTERED_PIP;
40 import static com.android.wm.shell.transition.Transitions.TRANSIT_EXIT_PIP;
41 import static com.android.wm.shell.transition.Transitions.TRANSIT_EXIT_PIP_TO_SPLIT;
42 import static com.android.wm.shell.transition.Transitions.TRANSIT_REMOVE_PIP;
43 import static com.android.wm.shell.transition.Transitions.isOpeningType;
44
45 import android.animation.Animator;
46 import android.app.ActivityManager;
47 import android.app.TaskInfo;
48 import android.content.Context;
49 import android.graphics.Matrix;
50 import android.graphics.Point;
51 import android.graphics.Rect;
52 import android.os.IBinder;
53 import android.view.Surface;
54 import android.view.SurfaceControl;
55 import android.view.WindowManager;
56 import android.window.TransitionInfo;
57 import android.window.TransitionRequestInfo;
58 import android.window.WindowContainerToken;
59 import android.window.WindowContainerTransaction;
60
61 import androidx.annotation.NonNull;
62 import androidx.annotation.Nullable;
63
64 import com.android.internal.protolog.common.ProtoLog;
65 import com.android.wm.shell.R;
66 import com.android.wm.shell.ShellTaskOrganizer;
67 import com.android.wm.shell.protolog.ShellProtoLogGroup;
68 import com.android.wm.shell.splitscreen.SplitScreenController;
69 import com.android.wm.shell.sysui.ShellInit;
70 import com.android.wm.shell.transition.CounterRotatorHelper;
71 import com.android.wm.shell.transition.Transitions;
72
73 import java.util.Optional;
74
75 /**
76 * Implementation of transitions for PiP on phone. Responsible for enter (alpha, bounds) and
77 * exit animation.
78 */
79 public class PipTransition extends PipTransitionController {
80
81 private static final String TAG = PipTransition.class.getSimpleName();
82
83 private final Context mContext;
84 private final PipTransitionState mPipTransitionState;
85 private final int mEnterExitAnimationDuration;
86 private final PipSurfaceTransactionHelper mSurfaceTransactionHelper;
87 private final Optional<SplitScreenController> mSplitScreenOptional;
88 private @PipAnimationController.AnimationType int mOneShotAnimationType = ANIM_TYPE_BOUNDS;
89 private Transitions.TransitionFinishCallback mFinishCallback;
90 private SurfaceControl.Transaction mFinishTransaction;
91 private final Rect mExitDestinationBounds = new Rect();
92 @Nullable
93 private IBinder mExitTransition;
94 private IBinder mRequestedEnterTransition;
95 private WindowContainerToken mRequestedEnterTask;
96 /** The Task window that is currently in PIP windowing mode. */
97 @Nullable
98 private WindowContainerToken mCurrentPipTaskToken;
99 /** Whether display is in fixed rotation. */
100 private boolean mInFixedRotation;
101 /**
102 * The rotation that the display will apply after expanding PiP to fullscreen. This is only
103 * meaningful if {@link #mInFixedRotation} is true.
104 */
105 @Surface.Rotation
106 private int mEndFixedRotation;
107 /** Whether the PIP window has fade out for fixed rotation. */
108 private boolean mHasFadeOut;
109
110 public PipTransition(Context context,
111 @NonNull ShellInit shellInit,
112 @NonNull ShellTaskOrganizer shellTaskOrganizer,
113 @NonNull Transitions transitions,
114 PipBoundsState pipBoundsState,
115 PipTransitionState pipTransitionState,
116 PipMenuController pipMenuController,
117 PipBoundsAlgorithm pipBoundsAlgorithm,
118 PipAnimationController pipAnimationController,
119 PipSurfaceTransactionHelper pipSurfaceTransactionHelper,
120 Optional<SplitScreenController> splitScreenOptional) {
121 super(shellInit, shellTaskOrganizer, transitions, pipBoundsState, pipMenuController,
122 pipBoundsAlgorithm, pipAnimationController);
123 mContext = context;
124 mPipTransitionState = pipTransitionState;
125 mEnterExitAnimationDuration = context.getResources()
126 .getInteger(R.integer.config_pipResizeAnimationDuration);
127 mSurfaceTransactionHelper = pipSurfaceTransactionHelper;
128 mSplitScreenOptional = splitScreenOptional;
129 }
130
131 @Override
132 public void setIsFullAnimation(boolean isFullAnimation) {
133 setOneShotAnimationType(isFullAnimation ? ANIM_TYPE_BOUNDS : ANIM_TYPE_ALPHA);
134 }
135
136 /**
137 * Sets the preferred animation type for one time.
138 * This is typically used to set the animation type to
139 * {@link PipAnimationController#ANIM_TYPE_ALPHA}.
140 */
141 private void setOneShotAnimationType(@PipAnimationController.AnimationType int animationType) {
142 mOneShotAnimationType = animationType;
143 }
144
145 @Override
146 public void startExitTransition(int type, WindowContainerTransaction out,
147 @Nullable Rect destinationBounds) {
148 if (destinationBounds != null) {
149 mExitDestinationBounds.set(destinationBounds);
150 }
151 final PipAnimationController.PipTransitionAnimator animator =
152 mPipAnimationController.getCurrentAnimator();
153 if (animator != null && animator.isRunning()) {
154 animator.cancel();
155 }
156 mExitTransition = mTransitions.startTransition(type, out, this);
157 }
158
159 @Override
160 public boolean startAnimation(@NonNull IBinder transition,
161 @NonNull TransitionInfo info,
162 @NonNull SurfaceControl.Transaction startTransaction,
163 @NonNull SurfaceControl.Transaction finishTransaction,
164 @NonNull Transitions.TransitionFinishCallback finishCallback) {
165 final TransitionInfo.Change currentPipTaskChange = findCurrentPipTaskChange(info);
166 final TransitionInfo.Change fixedRotationChange = findFixedRotationChange(info);
167 mInFixedRotation = fixedRotationChange != null;
168 mEndFixedRotation = mInFixedRotation
169 ? fixedRotationChange.getEndFixedRotation()
170 : ROTATION_UNDEFINED;
171
172 // Exiting PIP.
173 final int type = info.getType();
174 if (transition.equals(mExitTransition)) {
175 mExitDestinationBounds.setEmpty();
176 mExitTransition = null;
177 mHasFadeOut = false;
178 if (mFinishCallback != null) {
179 callFinishCallback(null /* wct */);
180 mFinishTransaction = null;
181 throw new RuntimeException("Previous callback not called, aborting exit PIP.");
182 }
183
184 // PipTaskChange can be null if the PIP task has been detached, for example, when the
185 // task contains multiple activities, the PIP will be moved to a new PIP task when
186 // entering, and be moved back when exiting. In that case, the PIP task will be removed
187 // immediately.
188 final TaskInfo pipTaskInfo = currentPipTaskChange != null
189 ? currentPipTaskChange.getTaskInfo()
190 : mPipOrganizer.getTaskInfo();
191 if (pipTaskInfo == null) {
192 throw new RuntimeException("Cannot find the pip task for exit-pip transition.");
193 }
194
195 switch (type) {
196 case TRANSIT_EXIT_PIP:
197 startExitAnimation(info, startTransaction, finishTransaction, finishCallback,
198 pipTaskInfo, currentPipTaskChange);
199 break;
200 case TRANSIT_EXIT_PIP_TO_SPLIT:
201 startExitToSplitAnimation(info, startTransaction, finishTransaction,
202 finishCallback, pipTaskInfo);
203 break;
204 case TRANSIT_REMOVE_PIP:
205 removePipImmediately(info, startTransaction, finishTransaction, finishCallback,
206 pipTaskInfo);
207 break;
208 default:
209 throw new IllegalStateException("mExitTransition with unexpected transit type="
210 + transitTypeToString(type));
211 }
212 mCurrentPipTaskToken = null;
213 return true;
214 } else if (transition == mRequestedEnterTransition) {
215 mRequestedEnterTransition = null;
216 mRequestedEnterTask = null;
217 }
218
219 // The previous PIP Task is no longer in PIP, but this is not an exit transition (This can
220 // happen when a new activity requests enter PIP). In this case, we just show this Task in
221 // its end state, and play other animation as normal.
222 if (currentPipTaskChange != null
223 && currentPipTaskChange.getTaskInfo().getWindowingMode() != WINDOWING_MODE_PINNED) {
224 resetPrevPip(currentPipTaskChange, startTransaction);
225 }
226
227 // Entering PIP.
228 if (isEnteringPip(info)) {
229 startEnterAnimation(info, startTransaction, finishTransaction, finishCallback);
230 return true;
231 }
232
233 // For transition that we don't animate, but contains the PIP leash, we need to update the
234 // PIP surface, otherwise it will be reset after the transition.
235 if (currentPipTaskChange != null) {
236 // Set the "end" bounds of pip. The default setup uses the start bounds. Since this is
237 // changing the *finish*Transaction, we need to use the end bounds. This will also
238 // make sure that the fade-in animation (below) uses the end bounds as well.
239 if (!currentPipTaskChange.getEndAbsBounds().isEmpty()) {
240 mPipBoundsState.setBounds(currentPipTaskChange.getEndAbsBounds());
241 }
242 updatePipForUnhandledTransition(currentPipTaskChange, startTransaction,
243 finishTransaction);
244 }
245
246 // Fade in the fadeout PIP when the fixed rotation is finished.
247 if (mPipTransitionState.isInPip() && !mInFixedRotation && mHasFadeOut) {
248 fadeExistingPip(true /* show */);
249 }
250
251 return false;
252 }
253
254 @Override
255 public void mergeAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
256 @NonNull SurfaceControl.Transaction t, @NonNull IBinder mergeTarget,
257 @NonNull Transitions.TransitionFinishCallback finishCallback) {
258 end();
259 }
260
261 /** Helper to identify whether this handler is currently the one playing an animation */
262 private boolean isAnimatingLocally() {
263 return mFinishTransaction != null;
264 }
265
266 @Nullable
267 @Override
268 public WindowContainerTransaction handleRequest(@NonNull IBinder transition,
269 @NonNull TransitionRequestInfo request) {
270 if (requestHasPipEnter(request)) {
271 WindowContainerTransaction wct = new WindowContainerTransaction();
272 augmentRequest(transition, request, wct);
273 return wct;
274 } else {
275 return null;
276 }
277 }
278
279 @Override
280 public void augmentRequest(@NonNull IBinder transition,
281 @NonNull TransitionRequestInfo request, @NonNull WindowContainerTransaction outWCT) {
282 if (!requestHasPipEnter(request)) {
283 throw new IllegalStateException("Called PiP augmentRequest when request has no PiP");
284 }
285 if (mOneShotAnimationType == ANIM_TYPE_ALPHA) {
286 mRequestedEnterTransition = transition;
287 mRequestedEnterTask = request.getTriggerTask().token;
288 outWCT.setActivityWindowingMode(request.getTriggerTask().token,
289 WINDOWING_MODE_UNDEFINED);
290 final Rect destinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds();
291 outWCT.setBounds(request.getTriggerTask().token, destinationBounds);
292 }
293 }
294
295 @Override
296 public void end() {
297 Animator animator = mPipAnimationController.getCurrentAnimator();
298 if (animator == null) return;
299 animator.end();
300 }
301
302 @Override
303 public boolean handleRotateDisplay(int startRotation, int endRotation,
304 WindowContainerTransaction wct) {
305 if (mRequestedEnterTransition != null && mOneShotAnimationType == ANIM_TYPE_ALPHA) {
306 // A fade-in was requested but not-yet started. In this case, just recalculate the
307 // initial state under the new rotation.
308 int rotationDelta = deltaRotation(startRotation, endRotation);
309 if (rotationDelta != Surface.ROTATION_0) {
310 mPipBoundsState.getDisplayLayout().rotateTo(mContext.getResources(), endRotation);
311 final Rect destinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds();
312 wct.setBounds(mRequestedEnterTask, destinationBounds);
313 return true;
314 }
315 }
316 return false;
317 }
318
319 @Override
320 public void onTransitionConsumed(@NonNull IBinder transition, boolean aborted,
321 @Nullable SurfaceControl.Transaction finishT) {
322 if (transition != mExitTransition) {
323 return;
324 }
325 // This means an expand happened before enter-pip finished and we are now "merging" a
326 // no-op transition that happens to match our exit-pip.
327 boolean cancelled = false;
328 if (mPipAnimationController.getCurrentAnimator() != null) {
329 mPipAnimationController.getCurrentAnimator().cancel();
330 cancelled = true;
331 }
332 // Unset exitTransition AFTER cancel so that finishResize knows we are merging.
333 mExitTransition = null;
334 if (!cancelled || aborted) return;
335 final ActivityManager.RunningTaskInfo taskInfo = mPipOrganizer.getTaskInfo();
336 if (taskInfo != null) {
337 startExpandAnimation(taskInfo, mPipOrganizer.getSurfaceControl(),
338 mPipBoundsState.getBounds(), mPipBoundsState.getBounds(),
339 new Rect(mExitDestinationBounds), Surface.ROTATION_0);
340 }
341 mExitDestinationBounds.setEmpty();
342 mCurrentPipTaskToken = null;
343 }
344
345 @Override
346 public void onFinishResize(TaskInfo taskInfo, Rect destinationBounds,
347 @PipAnimationController.TransitionDirection int direction,
348 @Nullable SurfaceControl.Transaction tx) {
349 final boolean enteringPip = isInPipDirection(direction);
350 if (enteringPip) {
351 mPipTransitionState.setTransitionState(ENTERED_PIP);
352 }
353 // If we have an exit transition, but aren't playing a transition locally, it
354 // means we're expecting the exit transition will be "merged" into another transition
355 // (likely a remote like launcher), so don't fire the finish-callback here -- wait until
356 // the exit transition is merged.
357 if ((mExitTransition == null || isAnimatingLocally()) && mFinishCallback != null) {
358 WindowContainerTransaction wct = null;
359 if (isOutPipDirection(direction)) {
360 // Only need to reset surface properties. The server-side operations were already
361 // done at the start. But if it is running fixed rotation, there will be a seamless
362 // display transition later. So the last rotation transform needs to be kept to
363 // avoid flickering, and then the display transition will reset the transform.
364 if (tx != null && !mInFixedRotation) {
365 mFinishTransaction.merge(tx);
366 }
367 } else {
368 wct = new WindowContainerTransaction();
369 if (isInPipDirection(direction)) {
370 // If we are animating from fullscreen using a bounds animation, then reset the
371 // activity windowing mode, and set the task bounds to the final bounds
372 wct.setActivityWindowingMode(taskInfo.token, WINDOWING_MODE_UNDEFINED);
373 wct.scheduleFinishEnterPip(taskInfo.token, destinationBounds);
374 wct.setBounds(taskInfo.token, destinationBounds);
375 } else {
376 wct.setBounds(taskInfo.token, null /* bounds */);
377 }
378 if (tx != null) {
379 wct.setBoundsChangeTransaction(taskInfo.token, tx);
380 }
381 }
382 final SurfaceControl leash = mPipOrganizer.getSurfaceControl();
383 final int displayRotation = taskInfo.getConfiguration().windowConfiguration
384 .getDisplayRotation();
385 if (enteringPip && mInFixedRotation && mEndFixedRotation != displayRotation
386 && leash != null && leash.isValid()) {
387 // Launcher may update the Shelf height during the animation, which will update the
388 // destination bounds. Because this is in fixed rotation, We need to make sure the
389 // finishTransaction is using the updated bounds in the display rotation.
390 final Rect displayBounds = mPipBoundsState.getDisplayBounds();
391 final Rect finishBounds = new Rect(destinationBounds);
392 rotateBounds(finishBounds, displayBounds, mEndFixedRotation, displayRotation);
393 mSurfaceTransactionHelper.crop(mFinishTransaction, leash, finishBounds);
394 }
395 mFinishTransaction = null;
396 callFinishCallback(wct);
397 }
398 finishResizeForMenu(destinationBounds);
399 }
400
401 private void callFinishCallback(WindowContainerTransaction wct) {
402 // Need to unset mFinishCallback first because onTransitionFinished can re-enter this
403 // handler if there is a pending PiP animation.
404 final Transitions.TransitionFinishCallback finishCallback = mFinishCallback;
405 mFinishCallback = null;
406 finishCallback.onTransitionFinished(wct, null /* callback */);
407 }
408
409 @Override
410 public void forceFinishTransition() {
411 if (mFinishCallback == null) return;
412 mFinishCallback.onTransitionFinished(null /* wct */, null /* callback */);
413 mFinishCallback = null;
414 mFinishTransaction = null;
415 }
416
417 @Override
418 public void onFixedRotationStarted() {
419 // The transition with this fixed rotation may be handled by other handler before reaching
420 // PipTransition, so we cannot do this in #startAnimation.
421 if (mPipTransitionState.getTransitionState() == ENTERED_PIP && !mHasFadeOut) {
422 // Fade out the existing PiP to avoid jump cut during seamless rotation.
423 fadeExistingPip(false /* show */);
424 }
425 }
426
427 @Nullable
428 private TransitionInfo.Change findCurrentPipTaskChange(@NonNull TransitionInfo info) {
429 if (mCurrentPipTaskToken == null) {
430 return null;
431 }
432 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
433 final TransitionInfo.Change change = info.getChanges().get(i);
434 if (mCurrentPipTaskToken.equals(change.getContainer())) {
435 return change;
436 }
437 }
438 return null;
439 }
440
441 @Nullable
442 private TransitionInfo.Change findFixedRotationChange(@NonNull TransitionInfo info) {
443 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
444 final TransitionInfo.Change change = info.getChanges().get(i);
445 if (change.getEndFixedRotation() != ROTATION_UNDEFINED) {
446 return change;
447 }
448 }
449 return null;
450 }
451
452 private void startExitAnimation(@NonNull TransitionInfo info,
453 @NonNull SurfaceControl.Transaction startTransaction,
454 @NonNull SurfaceControl.Transaction finishTransaction,
455 @NonNull Transitions.TransitionFinishCallback finishCallback,
456 @NonNull TaskInfo taskInfo, @Nullable TransitionInfo.Change pipTaskChange) {
457 TransitionInfo.Change pipChange = pipTaskChange;
458 if (mCurrentPipTaskToken == null) {
459 ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
460 "%s: There is no existing PiP Task for TRANSIT_EXIT_PIP", TAG);
461 } else if (pipChange == null) {
462 // The pipTaskChange is null, this can happen if we are reparenting the PIP activity
463 // back to its original Task. In that case, we should animate the activity leash
464 // instead, which should be the change whose last parent is the recorded PiP Task.
465 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
466 final TransitionInfo.Change change = info.getChanges().get(i);
467 if (mCurrentPipTaskToken.equals(change.getLastParent())) {
468 // Find the activity that is exiting PiP.
469 pipChange = change;
470 break;
471 }
472 }
473 }
474 if (pipChange == null) {
475 ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
476 "%s: No window of exiting PIP is found. Can't play expand animation", TAG);
477 removePipImmediately(info, startTransaction, finishTransaction, finishCallback,
478 taskInfo);
479 return;
480 }
481
482 // When exiting PiP, the PiP leash may be an Activity of a multi-windowing Task, for which
483 // case it may not be in the screen coordinate.
484 // Reparent the pip leash to the root with max layer so that we can animate it outside of
485 // parent crop, and make sure it is not covered by other windows.
486 final SurfaceControl pipLeash = pipChange.getLeash();
487 startTransaction.reparent(pipLeash, info.getRootLeash());
488 startTransaction.setLayer(pipLeash, Integer.MAX_VALUE);
489 // Note: because of this, the bounds to animate should be translated to the root coordinate.
490 final Point offset = info.getRootOffset();
491 final Rect currentBounds = mPipBoundsState.getBounds();
492 currentBounds.offset(-offset.x, -offset.y);
493 startTransaction.setPosition(pipLeash, currentBounds.left, currentBounds.top);
494
495 mFinishCallback = (wct, wctCB) -> {
496 mPipOrganizer.onExitPipFinished(taskInfo);
497 finishCallback.onTransitionFinished(wct, wctCB);
498 };
499 mFinishTransaction = finishTransaction;
500
501 // Check if it is Shell rotation.
502 if (Transitions.SHELL_TRANSITIONS_ROTATION) {
503 TransitionInfo.Change displayRotationChange = null;
504 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
505 final TransitionInfo.Change change = info.getChanges().get(i);
506 if (change.getMode() == TRANSIT_CHANGE
507 && (change.getFlags() & FLAG_IS_DISPLAY) != 0
508 && change.getStartRotation() != change.getEndRotation()) {
509 displayRotationChange = change;
510 break;
511 }
512 }
513 if (displayRotationChange != null) {
514 // Exiting PIP to fullscreen with orientation change.
515 startExpandAndRotationAnimation(info, startTransaction, finishTransaction,
516 displayRotationChange, taskInfo, pipChange, offset);
517 return;
518 }
519 }
520
521 // Set the initial frame as scaling the end to the start.
522 final Rect destinationBounds = new Rect(pipChange.getEndAbsBounds());
523 destinationBounds.offset(-offset.x, -offset.y);
524 startTransaction.setWindowCrop(pipLeash, destinationBounds);
525 mSurfaceTransactionHelper.scale(startTransaction, pipLeash, destinationBounds,
526 currentBounds);
527 startTransaction.apply();
528
529 // Check if it is fixed rotation.
530 final int rotationDelta;
531 if (mInFixedRotation) {
532 final int startRotation = pipChange.getStartRotation();
533 final int endRotation = mEndFixedRotation;
534 rotationDelta = deltaRotation(startRotation, endRotation);
535 final Rect endBounds = new Rect(destinationBounds);
536
537 // Set the end frame since the display won't rotate until fixed rotation is finished
538 // in the next display change transition.
539 rotateBounds(endBounds, destinationBounds, rotationDelta);
540 final int degree, x, y;
541 if (rotationDelta == ROTATION_90) {
542 degree = 90;
543 x = destinationBounds.right;
544 y = destinationBounds.top;
545 } else {
546 degree = -90;
547 x = destinationBounds.left;
548 y = destinationBounds.bottom;
549 }
550 mSurfaceTransactionHelper.rotateAndScaleWithCrop(finishTransaction,
551 pipLeash, endBounds, endBounds, new Rect(), degree, x, y,
552 true /* isExpanding */, rotationDelta == ROTATION_270 /* clockwise */);
553 } else {
554 rotationDelta = Surface.ROTATION_0;
555 }
556 startExpandAnimation(taskInfo, pipLeash, currentBounds, currentBounds, destinationBounds,
557 rotationDelta);
558 }
559
560 private void startExpandAndRotationAnimation(@NonNull TransitionInfo info,
561 @NonNull SurfaceControl.Transaction startTransaction,
562 @NonNull SurfaceControl.Transaction finishTransaction,
563 @NonNull TransitionInfo.Change displayRotationChange,
564 @NonNull TaskInfo taskInfo, @NonNull TransitionInfo.Change pipChange,
565 @NonNull Point offset) {
566 final int rotateDelta = deltaRotation(displayRotationChange.getStartRotation(),
567 displayRotationChange.getEndRotation());
568
569 // Counter-rotate all "going-away" things since they are still in the old orientation.
570 final CounterRotatorHelper rotator = new CounterRotatorHelper();
571 rotator.handleClosingChanges(info, startTransaction, displayRotationChange);
572
573 // Get the start bounds in new orientation.
574 final Rect startBounds = new Rect(pipChange.getStartAbsBounds());
575 rotateBounds(startBounds, displayRotationChange.getStartAbsBounds(), rotateDelta);
576 final Rect endBounds = new Rect(pipChange.getEndAbsBounds());
577 startBounds.offset(-offset.x, -offset.y);
578 endBounds.offset(-offset.x, -offset.y);
579
580 // Reverse the rotation direction for expansion.
581 final int pipRotateDelta = deltaRotation(rotateDelta, 0);
582
583 // Set the start frame.
584 final int degree, x, y;
585 if (pipRotateDelta == ROTATION_90) {
586 degree = 90;
587 x = startBounds.right;
588 y = startBounds.top;
589 } else {
590 degree = -90;
591 x = startBounds.left;
592 y = startBounds.bottom;
593 }
594 mSurfaceTransactionHelper.rotateAndScaleWithCrop(startTransaction, pipChange.getLeash(),
595 endBounds, startBounds, new Rect(), degree, x, y, true /* isExpanding */,
596 pipRotateDelta == ROTATION_270 /* clockwise */);
597 startTransaction.apply();
598 rotator.cleanUp(finishTransaction);
599
600 // Expand and rotate the pip window to fullscreen.
601 final PipAnimationController.PipTransitionAnimator animator =
602 mPipAnimationController.getAnimator(taskInfo, pipChange.getLeash(),
603 startBounds, startBounds, endBounds, null, TRANSITION_DIRECTION_LEAVE_PIP,
604 0 /* startingAngle */, pipRotateDelta);
605 animator.setTransitionDirection(TRANSITION_DIRECTION_LEAVE_PIP)
606 .setPipAnimationCallback(mPipAnimationCallback)
607 .setDuration(mEnterExitAnimationDuration)
608 .start();
609 }
610
611 private void startExpandAnimation(final TaskInfo taskInfo, final SurfaceControl leash,
612 final Rect baseBounds, final Rect startBounds, final Rect endBounds,
613 final int rotationDelta) {
614 final PipAnimationController.PipTransitionAnimator animator =
615 mPipAnimationController.getAnimator(taskInfo, leash, baseBounds, startBounds,
616 endBounds, null /* sourceHintRect */, TRANSITION_DIRECTION_LEAVE_PIP,
617 0 /* startingAngle */, rotationDelta);
618 animator.setTransitionDirection(TRANSITION_DIRECTION_LEAVE_PIP)
619 .setPipAnimationCallback(mPipAnimationCallback)
620 .setDuration(mEnterExitAnimationDuration)
621 .start();
622 }
623
624 /** For {@link Transitions#TRANSIT_REMOVE_PIP}, we just immediately remove the PIP Task. */
625 private void removePipImmediately(@NonNull TransitionInfo info,
626 @NonNull SurfaceControl.Transaction startTransaction,
627 @NonNull SurfaceControl.Transaction finishTransaction,
628 @NonNull Transitions.TransitionFinishCallback finishCallback,
629 @NonNull TaskInfo taskInfo) {
630 startTransaction.apply();
631 finishTransaction.setWindowCrop(info.getChanges().get(0).getLeash(),
632 mPipBoundsState.getDisplayBounds());
633 mPipOrganizer.onExitPipFinished(taskInfo);
634 finishCallback.onTransitionFinished(null, null);
635 }
636
637 /** Whether we should handle the given {@link TransitionInfo} animation as entering PIP. */
638 private boolean isEnteringPip(@NonNull TransitionInfo info) {
639 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
640 final TransitionInfo.Change change = info.getChanges().get(i);
641 if (isEnteringPip(change, info.getType())) return true;
642 }
643 return false;
644 }
645
646 /** Whether a particular change is a window that is entering pip. */
647 @Override
648 public boolean isEnteringPip(@NonNull TransitionInfo.Change change,
649 @WindowManager.TransitionType int transitType) {
650 if (change.getTaskInfo() != null
651 && change.getTaskInfo().getWindowingMode() == WINDOWING_MODE_PINNED
652 && !change.getContainer().equals(mCurrentPipTaskToken)) {
653 // We support TRANSIT_PIP type (from RootWindowContainer) or TRANSIT_OPEN (from apps
654 // that enter PiP instantly on opening, mostly from CTS/Flicker tests)
655 if (transitType == TRANSIT_PIP || transitType == TRANSIT_OPEN) {
656 return true;
657 }
658 // This can happen if the request to enter PIP happens when we are collecting for
659 // another transition, such as TRANSIT_CHANGE (display rotation).
660 if (transitType == TRANSIT_CHANGE) {
661 return true;
662 }
663
664 // Please file a bug to handle the unexpected transition type.
665 throw new IllegalStateException("Entering PIP with unexpected transition type="
666 + transitTypeToString(transitType));
667 }
668 return false;
669 }
670
671 private void startEnterAnimation(@NonNull TransitionInfo info,
672 @NonNull SurfaceControl.Transaction startTransaction,
673 @NonNull SurfaceControl.Transaction finishTransaction,
674 @NonNull Transitions.TransitionFinishCallback finishCallback) {
675 // Search for an Enter PiP transition
676 TransitionInfo.Change enterPip = null;
677 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
678 final TransitionInfo.Change change = info.getChanges().get(i);
679 if (change.getTaskInfo() != null
680 && change.getTaskInfo().getWindowingMode() == WINDOWING_MODE_PINNED) {
681 enterPip = change;
682 }
683 }
684 if (enterPip == null) {
685 throw new IllegalStateException("Trying to start PiP animation without a pip"
686 + "participant");
687 }
688
689 // Make sure other open changes are visible as entering PIP. Some may be hidden in
690 // Transitions#setupStartState because the transition type is OPEN (such as auto-enter).
691 for (int i = info.getChanges().size() - 1; i >= 0; --i) {
692 final TransitionInfo.Change change = info.getChanges().get(i);
693 if (change == enterPip) continue;
694 if (isOpeningType(change.getMode())) {
695 final SurfaceControl leash = change.getLeash();
696 startTransaction.show(leash).setAlpha(leash, 1.f);
697 }
698 }
699
700 startEnterAnimation(enterPip, startTransaction, finishTransaction, finishCallback);
701 }
702
703 @Override
704 public void startEnterAnimation(@NonNull final TransitionInfo.Change pipChange,
705 @NonNull final SurfaceControl.Transaction startTransaction,
706 @NonNull final SurfaceControl.Transaction finishTransaction,
707 @NonNull final Transitions.TransitionFinishCallback finishCallback) {
708 if (mFinishCallback != null) {
709 callFinishCallback(null /* wct */);
710 mFinishTransaction = null;
711 throw new RuntimeException("Previous callback not called, aborting entering PIP.");
712 }
713
714 // Keep track of the PIP task and animation.
715 mCurrentPipTaskToken = pipChange.getContainer();
716 mHasFadeOut = false;
717 mPipTransitionState.setTransitionState(PipTransitionState.ENTERING_PIP);
718 mFinishCallback = finishCallback;
719 mFinishTransaction = finishTransaction;
720
721 final ActivityManager.RunningTaskInfo taskInfo = pipChange.getTaskInfo();
722 final SurfaceControl leash = pipChange.getLeash();
723 final int startRotation = pipChange.getStartRotation();
724 final int endRotation = mInFixedRotation ? mEndFixedRotation : pipChange.getEndRotation();
725
726 setBoundsStateForEntry(taskInfo.topActivity, taskInfo.pictureInPictureParams,
727 taskInfo.topActivityInfo);
728 final Rect destinationBounds = mPipBoundsAlgorithm.getEntryDestinationBounds();
729 final Rect currentBounds = taskInfo.configuration.windowConfiguration.getBounds();
730 int rotationDelta = deltaRotation(startRotation, endRotation);
731 Rect sourceHintRect = PipBoundsAlgorithm.getValidSourceHintRect(
732 taskInfo.pictureInPictureParams, currentBounds);
733 if (rotationDelta != Surface.ROTATION_0 && mInFixedRotation) {
734 // Need to get the bounds of new rotation in old rotation for fixed rotation,
735 computeEnterPipRotatedBounds(rotationDelta, startRotation, endRotation, taskInfo,
736 destinationBounds, sourceHintRect);
737 }
738 // Set corner radius for entering pip.
739 mSurfaceTransactionHelper
740 .crop(finishTransaction, leash, destinationBounds)
741 .round(finishTransaction, leash, true /* applyCornerRadius */);
742 mTransitions.getMainExecutor().executeDelayed(() -> mPipMenuController.attach(leash), 0);
743
744 if (taskInfo.pictureInPictureParams != null
745 && taskInfo.pictureInPictureParams.isAutoEnterEnabled()
746 && mPipTransitionState.getInSwipePipToHomeTransition()) {
747 mOneShotAnimationType = ANIM_TYPE_BOUNDS;
748 final SurfaceControl swipePipToHomeOverlay = mPipOrganizer.mSwipePipToHomeOverlay;
749 startTransaction.setMatrix(leash, Matrix.IDENTITY_MATRIX, new float[9])
750 .setPosition(leash, destinationBounds.left, destinationBounds.top)
751 .setWindowCrop(leash, destinationBounds.width(), destinationBounds.height());
752 if (swipePipToHomeOverlay != null) {
753 // Launcher fade in the overlay on top of the fullscreen Task. It is possible we
754 // reparent the PIP activity to a new PIP task (in case there are other activities
755 // in the original Task), so we should also reparent the overlay to the PIP task.
756 startTransaction.reparent(swipePipToHomeOverlay, leash)
757 .setLayer(swipePipToHomeOverlay, Integer.MAX_VALUE);
758 mPipOrganizer.mSwipePipToHomeOverlay = null;
759 }
760 startTransaction.apply();
761 if (rotationDelta != Surface.ROTATION_0 && mInFixedRotation) {
762 // For fixed rotation, set the destination bounds to the new rotation coordinates
763 // at the end.
764 destinationBounds.set(mPipBoundsAlgorithm.getEntryDestinationBounds());
765 }
766 mPipBoundsState.setBounds(destinationBounds);
767 onFinishResize(taskInfo, destinationBounds, TRANSITION_DIRECTION_TO_PIP, null /* tx */);
768 sendOnPipTransitionFinished(TRANSITION_DIRECTION_TO_PIP);
769 if (swipePipToHomeOverlay != null) {
770 mPipOrganizer.fadeOutAndRemoveOverlay(swipePipToHomeOverlay,
771 null /* callback */, false /* withStartDelay */);
772 }
773 mPipTransitionState.setInSwipePipToHomeTransition(false);
774 return;
775 }
776
777 if (rotationDelta != Surface.ROTATION_0) {
778 Matrix tmpTransform = new Matrix();
779 tmpTransform.postRotate(rotationDelta);
780 startTransaction.setMatrix(leash, tmpTransform, new float[9]);
781 }
782 if (mOneShotAnimationType == ANIM_TYPE_ALPHA) {
783 startTransaction.setAlpha(leash, 0f);
784 }
785 startTransaction.apply();
786
787 PipAnimationController.PipTransitionAnimator animator;
788 if (mOneShotAnimationType == ANIM_TYPE_BOUNDS) {
789 animator = mPipAnimationController.getAnimator(taskInfo, leash, currentBounds,
790 currentBounds, destinationBounds, sourceHintRect, TRANSITION_DIRECTION_TO_PIP,
791 0 /* startingAngle */, rotationDelta);
792 if (sourceHintRect == null) {
793 // We use content overlay when there is no source rect hint to enter PiP use bounds
794 // animation.
795 animator.setColorContentOverlay(mContext);
796 }
797 } else if (mOneShotAnimationType == ANIM_TYPE_ALPHA) {
798 animator = mPipAnimationController.getAnimator(taskInfo, leash, destinationBounds,
799 0f, 1f);
800 mOneShotAnimationType = ANIM_TYPE_BOUNDS;
801 } else {
802 throw new RuntimeException("Unrecognized animation type: "
803 + mOneShotAnimationType);
804 }
805 animator.setTransitionDirection(TRANSITION_DIRECTION_TO_PIP)
806 .setPipAnimationCallback(mPipAnimationCallback)
807 .setDuration(mEnterExitAnimationDuration);
808 if (rotationDelta != Surface.ROTATION_0 && mInFixedRotation) {
809 // For fixed rotation, the animation destination bounds is in old rotation coordinates.
810 // Set the destination bounds to new coordinates after the animation is finished.
811 // ComputeRotatedBounds has changed the DisplayLayout without affecting the animation.
812 animator.setDestinationBounds(mPipBoundsAlgorithm.getEntryDestinationBounds());
813 }
814 animator.start();
815 }
816
817 /** Computes destination bounds in old rotation and updates source hint rect if available. */
818 private void computeEnterPipRotatedBounds(int rotationDelta, int startRotation, int endRotation,
819 TaskInfo taskInfo, Rect outDestinationBounds, @Nullable Rect outSourceHintRect) {
820 mPipBoundsState.getDisplayLayout().rotateTo(mContext.getResources(), endRotation);
821 final Rect displayBounds = mPipBoundsState.getDisplayBounds();
822 outDestinationBounds.set(mPipBoundsAlgorithm.getEntryDestinationBounds());
823 // Transform the destination bounds to current display coordinates.
824 rotateBounds(outDestinationBounds, displayBounds, endRotation, startRotation);
825 // When entering PiP (from button navigation mode), adjust the source rect hint by
826 // display cutout if applicable.
827 if (outSourceHintRect != null && taskInfo.displayCutoutInsets != null) {
828 if (rotationDelta == Surface.ROTATION_270) {
829 outSourceHintRect.offset(taskInfo.displayCutoutInsets.left,
830 taskInfo.displayCutoutInsets.top);
831 }
832 }
833 }
834
835 private void startExitToSplitAnimation(@NonNull TransitionInfo info,
836 @NonNull SurfaceControl.Transaction startTransaction,
837 @NonNull SurfaceControl.Transaction finishTransaction,
838 @NonNull Transitions.TransitionFinishCallback finishCallback,
839 @NonNull TaskInfo taskInfo) {
840 final int changeSize = info.getChanges().size();
841 if (changeSize < 4) {
842 throw new RuntimeException(
843 "Got an exit-pip-to-split transition with unexpected change-list");
844 }
845 for (int i = changeSize - 1; i >= 0; i--) {
846 final TransitionInfo.Change change = info.getChanges().get(i);
847 final int mode = change.getMode();
848
849 if (mode == TRANSIT_CHANGE && change.getParent() != null) {
850 // TODO: perform resize/expand animation for reparented child task.
851 continue;
852 }
853
854 if (isOpeningType(mode) && change.getParent() == null) {
855 final SurfaceControl leash = change.getLeash();
856 final Rect endBounds = change.getEndAbsBounds();
857 startTransaction
858 .show(leash)
859 .setAlpha(leash, 1f)
860 .setPosition(leash, endBounds.left, endBounds.top)
861 .setWindowCrop(leash, endBounds.width(), endBounds.height());
862 }
863 }
864 mSplitScreenOptional.get().finishEnterSplitScreen(startTransaction);
865 startTransaction.apply();
866
867 mPipOrganizer.onExitPipFinished(taskInfo);
868 finishCallback.onTransitionFinished(null, null);
869 }
870
871 private void resetPrevPip(@NonNull TransitionInfo.Change prevPipTaskChange,
872 @NonNull SurfaceControl.Transaction startTransaction) {
873 final SurfaceControl leash = prevPipTaskChange.getLeash();
874 final Rect bounds = prevPipTaskChange.getEndAbsBounds();
875 final Point offset = prevPipTaskChange.getEndRelOffset();
876 bounds.offset(-offset.x, -offset.y);
877
878 startTransaction.setWindowCrop(leash, null);
879 startTransaction.setMatrix(leash, 1, 0, 0, 1);
880 startTransaction.setCornerRadius(leash, 0);
881 startTransaction.setPosition(leash, bounds.left, bounds.top);
882
883 if (mHasFadeOut && prevPipTaskChange.getTaskInfo().isVisible()) {
884 if (mPipAnimationController.getCurrentAnimator() != null) {
885 mPipAnimationController.getCurrentAnimator().cancel();
886 }
887 startTransaction.setAlpha(leash, 1);
888 }
889 mHasFadeOut = false;
890 mCurrentPipTaskToken = null;
891 mPipOrganizer.onExitPipFinished(prevPipTaskChange.getTaskInfo());
892 }
893
894 private void updatePipForUnhandledTransition(@NonNull TransitionInfo.Change pipChange,
895 @NonNull SurfaceControl.Transaction startTransaction,
896 @NonNull SurfaceControl.Transaction finishTransaction) {
897 // When the PIP window is visible and being a part of the transition, such as display
898 // rotation, we need to update its bounds and rounded corner.
899 final SurfaceControl leash = pipChange.getLeash();
900 final Rect destBounds = mPipBoundsState.getBounds();
901 final boolean isInPip = mPipTransitionState.isInPip();
902 mSurfaceTransactionHelper
903 .crop(startTransaction, leash, destBounds)
904 .round(startTransaction, leash, isInPip);
905 mSurfaceTransactionHelper
906 .crop(finishTransaction, leash, destBounds)
907 .round(finishTransaction, leash, isInPip);
908 }
909
910 /** Hides and shows the existing PIP during fixed rotation transition of other activities. */
911 private void fadeExistingPip(boolean show) {
912 final SurfaceControl leash = mPipOrganizer.getSurfaceControl();
913 final TaskInfo taskInfo = mPipOrganizer.getTaskInfo();
914 if (leash == null || !leash.isValid() || taskInfo == null) {
915 ProtoLog.w(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE,
916 "%s: Invalid leash on fadeExistingPip: %s", TAG, leash);
917 return;
918 }
919 final float alphaStart = show ? 0 : 1;
920 final float alphaEnd = show ? 1 : 0;
921 mPipAnimationController
922 .getAnimator(taskInfo, leash, mPipBoundsState.getBounds(), alphaStart, alphaEnd)
923 .setTransitionDirection(TRANSITION_DIRECTION_SAME)
924 .setPipAnimationCallback(mPipAnimationCallback)
925 .setDuration(mEnterExitAnimationDuration)
926 .start();
927 mHasFadeOut = !show;
928 }
929
930 private void finishResizeForMenu(Rect destinationBounds) {
931 mPipMenuController.movePipMenu(null, null, destinationBounds);
932 mPipMenuController.updateMenuBounds(destinationBounds);
933 }
934 }