Merge "[Re-land] Deprecate getSfInstance usage in PiP component" into tm-qpr-dev

This commit is contained in:
Hongwei Wang
2022-08-11 14:59:33 +00:00
committed by Android (Google) Code Review
6 changed files with 34 additions and 82 deletions

View File

@@ -22,7 +22,6 @@ import android.view.View
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
import androidx.dynamicanimation.animation.FrameCallbackScheduler
import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce import androidx.dynamicanimation.animation.SpringForce
@@ -124,12 +123,6 @@ 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
/**
* FrameCallbackScheduler to use if it need custom FrameCallbackScheduler, if this is null,
* it will use the default FrameCallbackScheduler in the DynamicAnimation.
*/
private var customScheduler: FrameCallbackScheduler? = 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
@@ -454,14 +447,6 @@ class PhysicsAnimator<T> private constructor (target: T) {
this.defaultFling = defaultFling this.defaultFling = defaultFling
} }
/**
* Set the custom FrameCallbackScheduler for all aniatmion in this animator. Set this with null for
* restoring to default FrameCallbackScheduler.
*/
fun setCustomScheduler(scheduler: FrameCallbackScheduler) {
this.customScheduler = scheduler
}
/** Starts the animations! */ /** Starts the animations! */
fun start() { fun start() {
startAction() startAction()
@@ -511,12 +496,9 @@ 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 scheduler if it not null
val flingAnim = getFlingAnimation(animatedProperty, target)
flingAnim.scheduler = customScheduler ?: flingAnim.scheduler
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
flingAnim.also { flingConfig.applyToAnimation(it) }.start() getFlingAnimation(animatedProperty, target)
.also { flingConfig.applyToAnimation(it) }.start()
} }
} }
@@ -529,18 +511,6 @@ class PhysicsAnimator<T> private constructor (target: T) {
// 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 customScheduler is exist and has not been set to the animation,
// it should set here.
if (customScheduler != null &&
springAnim.scheduler != customScheduler) {
// Cancel the animation before set animation handler
if (springAnim.isRunning) {
cancel(animatedProperty)
}
// Apply the custom scheduler handler if it not null
springAnim.scheduler = customScheduler ?: springAnim.scheduler
}
// Apply the configuration and start the animation. // Apply the configuration and start the animation.
springConfig.applyToAnimation(springAnim) springConfig.applyToAnimation(springAnim)
animationStartActions.add(springAnim::start) animationStartActions.add(springAnim::start)
@@ -596,12 +566,9 @@ class PhysicsAnimator<T> private constructor (target: T) {
} }
} }
// Apply the custom animation scheduler if it not null
val springAnim = getSpringAnimation(animatedProperty, target)
springAnim.scheduler = customScheduler ?: springAnim.scheduler
// Apply the configuration and start the spring animation. // Apply the configuration and start the spring animation.
springAnim.also { springConfig.applyToAnimation(it) }.start() getSpringAnimation(animatedProperty, target)
.also { springConfig.applyToAnimation(it) }.start()
} }
} }
}) })

View File

@@ -29,7 +29,6 @@ import android.annotation.NonNull;
import android.app.TaskInfo; import android.app.TaskInfo;
import android.content.Context; import android.content.Context;
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.window.TaskSnapshot; import android.window.TaskSnapshot;
@@ -279,14 +278,15 @@ public class PipAnimationController {
mEndValue = endValue; mEndValue = endValue;
addListener(this); addListener(this);
addUpdateListener(this); addUpdateListener(this);
mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new; mSurfaceControlTransactionFactory =
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, newSurfaceControlTransaction()); onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction());
if (mPipAnimationCallback != null) { if (mPipAnimationCallback != null) {
mPipAnimationCallback.onPipAnimationStart(mTaskInfo, this); mPipAnimationCallback.onPipAnimationStart(mTaskInfo, this);
} }
@@ -294,14 +294,16 @@ public class PipAnimationController {
@Override @Override
public void onAnimationUpdate(ValueAnimator animation) { public void onAnimationUpdate(ValueAnimator animation) {
applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(), applySurfaceControlTransaction(mLeash,
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 = newSurfaceControlTransaction(); final SurfaceControl.Transaction tx =
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);
@@ -348,7 +350,8 @@ public class PipAnimationController {
} }
void setColorContentOverlay(Context context) { void setColorContentOverlay(Context context) {
final SurfaceControl.Transaction tx = newSurfaceControlTransaction(); final SurfaceControl.Transaction tx =
mSurfaceControlTransactionFactory.getTransaction();
if (mContentOverlay != null) { if (mContentOverlay != null) {
mContentOverlay.detach(tx); mContentOverlay.detach(tx);
} }
@@ -357,7 +360,8 @@ public class PipAnimationController {
} }
void setSnapshotContentOverlay(TaskSnapshot snapshot, Rect sourceRectHint) { void setSnapshotContentOverlay(TaskSnapshot snapshot, Rect sourceRectHint) {
final SurfaceControl.Transaction tx = newSurfaceControlTransaction(); final SurfaceControl.Transaction tx =
mSurfaceControlTransactionFactory.getTransaction();
if (mContentOverlay != null) { if (mContentOverlay != null) {
mContentOverlay.detach(tx); mContentOverlay.detach(tx);
} }
@@ -406,7 +410,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, newSurfaceControlTransaction()); onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction());
} }
} }
@@ -441,16 +445,6 @@ 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,6 +20,7 @@ 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;
@@ -234,4 +235,18 @@ 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

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

View File

@@ -33,10 +33,6 @@ 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.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;
@@ -89,25 +85,6 @@ 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<FrameCallbackScheduler> mSfSchedulerThreadLocal =
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;
}
};
return scheduler;
});
/** /**
* PhysicsAnimator instance for animating {@link PipBoundsState#getMotionBoundsState()} * PhysicsAnimator instance for animating {@link PipBoundsState#getMotionBoundsState()}
* using physics animations. * using physics animations.
@@ -210,10 +187,8 @@ 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.setCustomScheduler(mSfSchedulerThreadLocal.get());
} }
@NonNull @NonNull

View File

@@ -170,7 +170,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.getSfInstance().getVsyncId()); tx.setFrameTimelineVsync(Choreographer.getInstance().getVsyncId());
return tx; return tx;
} }
} }