Merge "Revert "Deprecate getSfInstance usage in PiP component"" into tm-dev

This commit is contained in:
Hongwei Wang
2022-04-20 19:31:59 +00:00
committed by Android (Google) Code Review
8 changed files with 94 additions and 37 deletions

View File

@@ -19,6 +19,7 @@ package com.android.wm.shell.animation
import android.util.ArrayMap import android.util.ArrayMap
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import androidx.dynamicanimation.animation.AnimationHandler
import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.FlingAnimation import androidx.dynamicanimation.animation.FlingAnimation
import androidx.dynamicanimation.animation.FloatPropertyCompat import androidx.dynamicanimation.animation.FloatPropertyCompat
@@ -122,6 +123,12 @@ class PhysicsAnimator<T> private constructor (target: T) {
/** FlingConfig to use by default for properties whose fling configs were not provided. */ /** FlingConfig to use by default for properties whose fling configs were not provided. */
private var defaultFling: FlingConfig = globalDefaultFling private var defaultFling: FlingConfig = globalDefaultFling
/**
* AnimationHandler to use if it need custom AnimationHandler, if this is null, it will use
* the default AnimationHandler in the DynamicAnimation.
*/
private var customAnimationHandler: AnimationHandler? = null
/** /**
* Internal listeners that respond to DynamicAnimations updating and ending, and dispatch to * Internal listeners that respond to DynamicAnimations updating and ending, and dispatch to
* the listeners provided via [addUpdateListener] and [addEndListener]. This allows us to add * the listeners provided via [addUpdateListener] and [addEndListener]. This allows us to add
@@ -446,6 +453,14 @@ class PhysicsAnimator<T> private constructor (target: T) {
this.defaultFling = defaultFling this.defaultFling = defaultFling
} }
/**
* Set the custom AnimationHandler for all aniatmion in this animator. Set this with null for
* restoring to default AnimationHandler.
*/
fun setCustomAnimationHandler(handler: AnimationHandler) {
this.customAnimationHandler = handler
}
/** Starts the animations! */ /** Starts the animations! */
fun start() { fun start() {
startAction() startAction()
@@ -495,10 +510,13 @@ class PhysicsAnimator<T> private constructor (target: T) {
// springs) on this property before flinging. // springs) on this property before flinging.
cancel(animatedProperty) cancel(animatedProperty)
// Apply the custom animation handler if it not null
val flingAnim = getFlingAnimation(animatedProperty, target)
flingAnim.animationHandler =
customAnimationHandler ?: flingAnim.animationHandler
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
getFlingAnimation(animatedProperty, target) flingAnim.also { flingConfig.applyToAnimation(it) }.start()
.also { flingConfig.applyToAnimation(it) }
.start()
} }
} }
@@ -510,6 +528,21 @@ class PhysicsAnimator<T> private constructor (target: T) {
if (flingConfig == null) { if (flingConfig == null) {
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
val springAnim = getSpringAnimation(animatedProperty, target) val springAnim = getSpringAnimation(animatedProperty, target)
// If customAnimationHander is exist and has not been set to the animation,
// it should set here.
if (customAnimationHandler != null &&
springAnim.animationHandler != customAnimationHandler) {
// Cancel the animation before set animation handler
if (springAnim.isRunning) {
cancel(animatedProperty)
}
// Apply the custom animation handler if it not null
springAnim.animationHandler =
customAnimationHandler ?: springAnim.animationHandler
}
// Apply the configuration and start the animation.
springConfig.applyToAnimation(springAnim) springConfig.applyToAnimation(springAnim)
animationStartActions.add(springAnim::start) animationStartActions.add(springAnim::start)
} else { } else {
@@ -564,10 +597,13 @@ class PhysicsAnimator<T> private constructor (target: T) {
} }
} }
// Apply the custom animation handler if it not null
val springAnim = getSpringAnimation(animatedProperty, target)
springAnim.animationHandler =
customAnimationHandler ?: springAnim.animationHandler
// Apply the configuration and start the spring animation. // Apply the configuration and start the spring animation.
getSpringAnimation(animatedProperty, target) springAnim.also { springConfig.applyToAnimation(it) }.start()
.also { springConfig.applyToAnimation(it) }
.start()
} }
} }
}) })

View File

@@ -30,6 +30,7 @@ import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Rect; import android.graphics.Rect;
import android.view.Choreographer;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceControl; import android.view.SurfaceControl;
import android.view.SurfaceSession; import android.view.SurfaceSession;
@@ -270,15 +271,14 @@ public class PipAnimationController {
mEndValue = endValue; mEndValue = endValue;
addListener(this); addListener(this);
addUpdateListener(this); addUpdateListener(this);
mSurfaceControlTransactionFactory = mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new;
new PipSurfaceTransactionHelper.VsyncSurfaceControlTransactionFactory();
mTransitionDirection = TRANSITION_DIRECTION_NONE; mTransitionDirection = TRANSITION_DIRECTION_NONE;
} }
@Override @Override
public void onAnimationStart(Animator animation) { public void onAnimationStart(Animator animation) {
mCurrentValue = mStartValue; mCurrentValue = mStartValue;
onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction()); onStartTransaction(mLeash, newSurfaceControlTransaction());
if (mPipAnimationCallback != null) { if (mPipAnimationCallback != null) {
mPipAnimationCallback.onPipAnimationStart(mTaskInfo, this); mPipAnimationCallback.onPipAnimationStart(mTaskInfo, this);
} }
@@ -286,16 +286,14 @@ public class PipAnimationController {
@Override @Override
public void onAnimationUpdate(ValueAnimator animation) { public void onAnimationUpdate(ValueAnimator animation) {
applySurfaceControlTransaction(mLeash, applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(),
mSurfaceControlTransactionFactory.getTransaction(),
animation.getAnimatedFraction()); animation.getAnimatedFraction());
} }
@Override @Override
public void onAnimationEnd(Animator animation) { public void onAnimationEnd(Animator animation) {
mCurrentValue = mEndValue; mCurrentValue = mEndValue;
final SurfaceControl.Transaction tx = final SurfaceControl.Transaction tx = newSurfaceControlTransaction();
mSurfaceControlTransactionFactory.getTransaction();
onEndTransaction(mLeash, tx, mTransitionDirection); onEndTransaction(mLeash, tx, mTransitionDirection);
if (mPipAnimationCallback != null) { if (mPipAnimationCallback != null) {
mPipAnimationCallback.onPipAnimationEnd(mTaskInfo, tx, this); mPipAnimationCallback.onPipAnimationEnd(mTaskInfo, tx, this);
@@ -342,8 +340,7 @@ public class PipAnimationController {
} }
PipTransitionAnimator<T> setUseContentOverlay(Context context) { PipTransitionAnimator<T> setUseContentOverlay(Context context) {
final SurfaceControl.Transaction tx = final SurfaceControl.Transaction tx = newSurfaceControlTransaction();
mSurfaceControlTransactionFactory.getTransaction();
if (mContentOverlay != null) { if (mContentOverlay != null) {
// remove existing content overlay if there is any. // remove existing content overlay if there is any.
tx.remove(mContentOverlay); tx.remove(mContentOverlay);
@@ -418,7 +415,7 @@ public class PipAnimationController {
void setDestinationBounds(Rect destinationBounds) { void setDestinationBounds(Rect destinationBounds) {
mDestinationBounds.set(destinationBounds); mDestinationBounds.set(destinationBounds);
if (mAnimationType == ANIM_TYPE_ALPHA) { if (mAnimationType == ANIM_TYPE_ALPHA) {
onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction()); onStartTransaction(mLeash, newSurfaceControlTransaction());
} }
} }
@@ -453,6 +450,16 @@ public class PipAnimationController {
mEndValue = endValue; mEndValue = endValue;
} }
/**
* @return {@link SurfaceControl.Transaction} instance with vsync-id.
*/
protected SurfaceControl.Transaction newSurfaceControlTransaction() {
final SurfaceControl.Transaction tx =
mSurfaceControlTransactionFactory.getTransaction();
tx.setFrameTimelineVsync(Choreographer.getSfInstance().getVsyncId());
return tx;
}
@VisibleForTesting @VisibleForTesting
public void setSurfaceControlTransactionFactory( public void setSurfaceControlTransactionFactory(
PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) { PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) {

View File

@@ -20,7 +20,6 @@ import android.content.Context;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.view.Choreographer;
import android.view.SurfaceControl; import android.view.SurfaceControl;
import com.android.wm.shell.R; import com.android.wm.shell.R;
@@ -225,18 +224,4 @@ public class PipSurfaceTransactionHelper {
public interface SurfaceControlTransactionFactory { public interface SurfaceControlTransactionFactory {
SurfaceControl.Transaction getTransaction(); SurfaceControl.Transaction getTransaction();
} }
/**
* Implementation of {@link SurfaceControlTransactionFactory} that returns
* {@link SurfaceControl.Transaction} with VsyncId being set.
*/
public static class VsyncSurfaceControlTransactionFactory
implements SurfaceControlTransactionFactory {
@Override
public SurfaceControl.Transaction getTransaction() {
final SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
tx.setFrameTimelineVsync(Choreographer.getInstance().getVsyncId());
return tx;
}
}
} }

View File

@@ -280,8 +280,7 @@ public class PipTaskOrganizer implements ShellTaskOrganizer.TaskListener,
mSurfaceTransactionHelper = surfaceTransactionHelper; mSurfaceTransactionHelper = surfaceTransactionHelper;
mPipAnimationController = pipAnimationController; mPipAnimationController = pipAnimationController;
mPipUiEventLoggerLogger = pipUiEventLogger; mPipUiEventLoggerLogger = pipUiEventLogger;
mSurfaceControlTransactionFactory = mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new;
new PipSurfaceTransactionHelper.VsyncSurfaceControlTransactionFactory();
mSplitScreenOptional = splitScreenOptional; mSplitScreenOptional = splitScreenOptional;
mTaskOrganizer = shellTaskOrganizer; mTaskOrganizer = shellTaskOrganizer;
mMainExecutor = mainExecutor; mMainExecutor = mainExecutor;

View File

@@ -146,10 +146,11 @@ public class PipInputConsumer {
"%s: Failed to create input consumer, %s", TAG, e); "%s: Failed to create input consumer, %s", TAG, e);
} }
mMainExecutor.execute(() -> { mMainExecutor.execute(() -> {
// Choreographer.getInstance() must be called on the thread that the input event // Choreographer.getSfInstance() must be called on the thread that the input event
// receiver should be receiving events // receiver should be receiving events
// TODO(b/222697646): remove getSfInstance usage and use vsyncId for transactions
mInputEventReceiver = new InputEventReceiver(inputChannel, mInputEventReceiver = new InputEventReceiver(inputChannel,
Looper.myLooper(), Choreographer.getInstance()); Looper.myLooper(), Choreographer.getSfInstance());
if (mRegistrationListener != null) { if (mRegistrationListener != null) {
mRegistrationListener.onRegistrationChanged(true /* isRegistered */); mRegistrationListener.onRegistrationChanged(true /* isRegistered */);
} }

View File

@@ -33,6 +33,11 @@ import android.content.Context;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Debug; import android.os.Debug;
import android.os.Looper;
import android.view.Choreographer;
import androidx.dynamicanimation.animation.AnimationHandler;
import androidx.dynamicanimation.animation.AnimationHandler.FrameCallbackScheduler;
import com.android.internal.protolog.common.ProtoLog; import com.android.internal.protolog.common.ProtoLog;
import com.android.wm.shell.R; import com.android.wm.shell.R;
@@ -84,6 +89,26 @@ public class PipMotionHelper implements PipAppOpsListener.Callback,
/** Coordinator instance for resolving conflicts with other floating content. */ /** Coordinator instance for resolving conflicts with other floating content. */
private FloatingContentCoordinator mFloatingContentCoordinator; private FloatingContentCoordinator mFloatingContentCoordinator;
private ThreadLocal<AnimationHandler> mSfAnimationHandlerThreadLocal =
ThreadLocal.withInitial(() -> {
final Looper initialLooper = Looper.myLooper();
final FrameCallbackScheduler scheduler = new FrameCallbackScheduler() {
@Override
public void postFrameCallback(@androidx.annotation.NonNull Runnable runnable) {
// TODO(b/222697646): remove getSfInstance usage and use vsyncId for
// transactions
Choreographer.getSfInstance().postFrameCallback(t -> runnable.run());
}
@Override
public boolean isCurrentThread() {
return Looper.myLooper() == initialLooper;
}
};
AnimationHandler handler = new AnimationHandler(scheduler);
return handler;
});
/** /**
* PhysicsAnimator instance for animating {@link PipBoundsState#getMotionBoundsState()} * PhysicsAnimator instance for animating {@link PipBoundsState#getMotionBoundsState()}
* using physics animations. * using physics animations.
@@ -186,8 +211,11 @@ public class PipMotionHelper implements PipAppOpsListener.Callback,
} }
public void init() { public void init() {
// Note: Needs to get the shell main thread sf vsync animation handler
mTemporaryBoundsPhysicsAnimator = PhysicsAnimator.getInstance( mTemporaryBoundsPhysicsAnimator = PhysicsAnimator.getInstance(
mPipBoundsState.getMotionBoundsState().getBoundsInMotion()); mPipBoundsState.getMotionBoundsState().getBoundsInMotion());
mTemporaryBoundsPhysicsAnimator.setCustomAnimationHandler(
mSfAnimationHandlerThreadLocal.get());
} }
@NonNull @NonNull

View File

@@ -625,7 +625,8 @@ public class PipResizeGestureHandler {
class PipResizeInputEventReceiver extends BatchedInputEventReceiver { class PipResizeInputEventReceiver extends BatchedInputEventReceiver {
PipResizeInputEventReceiver(InputChannel channel, Looper looper) { PipResizeInputEventReceiver(InputChannel channel, Looper looper) {
super(channel, looper, Choreographer.getInstance()); // TODO(b/222697646): remove getSfInstance usage and use vsyncId for transactions
super(channel, looper, Choreographer.getSfInstance());
} }
public void onInputEvent(InputEvent event) { public void onInputEvent(InputEvent event) {

View File

@@ -165,7 +165,7 @@ public class PipSurfaceTransactionHelper {
/** @return {@link SurfaceControl.Transaction} instance with vsync-id */ /** @return {@link SurfaceControl.Transaction} instance with vsync-id */
public static SurfaceControl.Transaction newSurfaceControlTransaction() { public static SurfaceControl.Transaction newSurfaceControlTransaction() {
final SurfaceControl.Transaction tx = new SurfaceControl.Transaction(); final SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
tx.setFrameTimelineVsync(Choreographer.getInstance().getVsyncId()); tx.setFrameTimelineVsync(Choreographer.getSfInstance().getVsyncId());
return tx; return tx;
} }
} }