Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c673f679 authored by petsjonkin's avatar petsjonkin Committed by Oleg Petšjonkin
Browse files

Add possibility to ignore animationLimits in RampAnimator

Bug: b/283447291
Test: atest DisplayPowerControllerTest atest DisplayPowerController2Test
Change-Id: I586de60cc653397c63796b035764e584af503256
parent 49f668d4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2425,7 +2425,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            Slog.d(mTag, "Animating brightness: target=" + target + ", sdrTarget=" + sdrTarget
                    + ", rate=" + rate);
        }
        if (mScreenBrightnessRampAnimator.animateTo(target, sdrTarget, rate)) {
        if (mScreenBrightnessRampAnimator.animateTo(target, sdrTarget, rate, false)) {
            Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", (int) target);

            String propertyKey = "debug.tracing.screen_brightness";
+8 −2
Original line number Diff line number Diff line
@@ -1547,7 +1547,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                            SCREEN_ANIMATION_RATE_MINIMUM);
                } else if (customTransitionRate > 0) {
                    animateScreenBrightness(animateValue, sdrAnimateValue,
                            customTransitionRate);
                            customTransitionRate, /* ignoreAnimationLimits = */true);
                } else {
                    boolean isIncreasing = animateValue > currentBrightness;
                    final float rampSpeed;
@@ -2017,11 +2017,17 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    }

    private void animateScreenBrightness(float target, float sdrTarget, float rate) {
        animateScreenBrightness(target, sdrTarget, rate, /* ignoreAnimationLimits = */false);
    }

    private void animateScreenBrightness(float target, float sdrTarget, float rate,
            boolean ignoreAnimationLimits) {
        if (DEBUG) {
            Slog.d(mTag, "Animating brightness: target=" + target + ", sdrTarget=" + sdrTarget
                    + ", rate=" + rate);
        }
        if (mScreenBrightnessRampAnimator.animateTo(target, sdrTarget, rate)) {
        if (mScreenBrightnessRampAnimator.animateTo(target, sdrTarget, rate,
                ignoreAnimationLimits)) {
            Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", (int) target);

            String propertyKey = "debug.tracing.screen_brightness";
+41 −13
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ class RampAnimator<T> {
    private final T mObject;
    private final FloatProperty<T> mProperty;

    private final Clock mClock;

    private float mCurrentValue;

    // target in HLG space
@@ -47,10 +49,14 @@ class RampAnimator<T> {

    private boolean mFirstTime = true;


    RampAnimator(T object, FloatProperty<T> property) {
        this(object, property, System::nanoTime);
    }

    RampAnimator(T object, FloatProperty<T> property, Clock clock) {
        mObject = object;
        mProperty = property;
        mClock = clock;
    }

    /**
@@ -66,15 +72,24 @@ class RampAnimator<T> {

    /**
     * Sets the animation target and the rate of this ramp animator.
     *
     * Animation rate will be set ignoring maxTime animation limits
     * If this is the first time the property is being set or if the rate is 0,
     * the value jumps directly to the target.
     *
     * @param targetLinear The target value.
     * @param rate The convergence rate in units per second, or 0 to set the value immediately.
     * @param ignoreAnimationLimits if mAnimationIncreaseMaxTimeSecs and
     *                              mAnimationDecreaseMaxTimeSecs should be respected when adjusting
     *                              animation speed
     * @return True if the target differs from the previous target.
     */
    boolean setAnimationTarget(float targetLinear, float rate) {
    boolean setAnimationTarget(float targetLinear, float rate, boolean ignoreAnimationLimits) {
        float maxIncreaseTimeSecs = ignoreAnimationLimits ? 0 : mAnimationIncreaseMaxTimeSecs;
        float maxDecreaseTimeSecs = ignoreAnimationLimits ? 0 : mAnimationDecreaseMaxTimeSecs;
        return setAnimationTarget(targetLinear, rate, maxIncreaseTimeSecs, maxDecreaseTimeSecs);
    }
    private boolean setAnimationTarget(float targetLinear, float rate,
            float maxIncreaseTimeSecs, float maxDecreaseTimeSecs) {
        // Convert the target from the linear into the HLG space.
        final float target = BrightnessUtils.convertLinearToGamma(targetLinear);

@@ -94,12 +109,12 @@ class RampAnimator<T> {
        }

        // Adjust the rate so that we do not exceed our maximum animation time.
        if (target > mCurrentValue && mAnimationIncreaseMaxTimeSecs > 0.0f
                && ((target - mCurrentValue) / rate) > mAnimationIncreaseMaxTimeSecs) {
            rate = (target - mCurrentValue) / mAnimationIncreaseMaxTimeSecs;
        } else if (target < mCurrentValue && mAnimationDecreaseMaxTimeSecs > 0.0f
                && ((mCurrentValue - target) / rate) > mAnimationDecreaseMaxTimeSecs) {
            rate = (mCurrentValue - target) / mAnimationDecreaseMaxTimeSecs;
        if (target > mCurrentValue && maxIncreaseTimeSecs > 0.0f
                && ((target - mCurrentValue) / rate) > maxIncreaseTimeSecs) {
            rate = (target - mCurrentValue) / maxIncreaseTimeSecs;
        } else if (target < mCurrentValue && maxDecreaseTimeSecs > 0.0f
                && ((mCurrentValue - target) / rate) > maxDecreaseTimeSecs) {
            rate = (mCurrentValue - target) / maxDecreaseTimeSecs;
        }

        // Adjust the rate based on the closest target.
@@ -124,7 +139,7 @@ class RampAnimator<T> {
        if (!mAnimating && target != mCurrentValue) {
            mAnimating = true;
            mAnimatedValue = mCurrentValue;
            mLastFrameTimeNanos = System.nanoTime();
            mLastFrameTimeNanos = mClock.nanoTime();
        }

        return changed;
@@ -184,6 +199,13 @@ class RampAnimator<T> {
        void onAnimationEnd();
    }

    interface Clock {
        /**
         * Returns current system time in nanoseconds.
         */
        long nanoTime();
    }

    static class DualRampAnimator<T> {
        private final Choreographer mChoreographer;
        private final RampAnimator<T> mFirst;
@@ -219,11 +241,17 @@ class RampAnimator<T> {
         * @param linearFirstTarget The first target value in linear space.
         * @param linearSecondTarget The second target value in linear space.
         * @param rate The convergence rate in units per second, or 0 to set the value immediately.
         * @param ignoreAnimationLimits if mAnimationIncreaseMaxTimeSecs and
         *                              mAnimationDecreaseMaxTimeSecs should be respected
         *                              when adjusting animation speed
         * @return True if either target differs from the previous target.
         */
        public boolean animateTo(float linearFirstTarget, float linearSecondTarget, float rate) {
            boolean animationTargetChanged = mFirst.setAnimationTarget(linearFirstTarget, rate);
            animationTargetChanged |= mSecond.setAnimationTarget(linearSecondTarget, rate);
        public boolean animateTo(float linearFirstTarget, float linearSecondTarget, float rate,
                boolean ignoreAnimationLimits) {
            boolean animationTargetChanged = mFirst.setAnimationTarget(linearFirstTarget, rate,
                    ignoreAnimationLimits);
            animationTargetChanged |= mSecond.setAnimationTarget(linearSecondTarget, rate,
                    ignoreAnimationLimits);
            boolean shouldBeAnimating = isAnimating();

            if (shouldBeAnimating != mAwaitingCallback) {
+42 −40
Original line number Diff line number Diff line
@@ -335,9 +335,9 @@ public final class DisplayPowerController2Test {
        when(mHolder.brightnessSetting.getBrightness()).thenReturn(leadBrightness);
        listener.onBrightnessChanged(leadBrightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(leadBrightness), anyFloat(), anyFloat());
        verify(mHolder.animator).animateTo(eq(leadBrightness), anyFloat(), anyFloat(), eq(false));
        verify(followerDpc.animator).animateTo(eq(followerBrightness), anyFloat(),
                anyFloat());
                anyFloat(), eq(false));

        clearInvocations(mHolder.animator, followerDpc.animator);

@@ -351,9 +351,9 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
    }

    @Test
@@ -383,9 +383,9 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
    }

    @Test
@@ -413,9 +413,9 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
    }

    @Test
@@ -445,9 +445,9 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
    }

    @Test
@@ -484,11 +484,11 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator).animateTo(eq(leadBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        // One triggered by handleBrightnessModeChange, another triggered by setBrightnessToFollow
        verify(followerDpc.hbmController, times(2)).onAmbientLuxChange(ambientLux);
        verify(followerDpc.animator, times(2)).animateTo(eq(followerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(mHolder.displayPowerState.getScreenBrightness()).thenReturn(leadBrightness);
        when(followerDpc.displayPowerState.getScreenBrightness()).thenReturn(followerBrightness);
@@ -515,10 +515,10 @@ public final class DisplayPowerController2Test {

        // The second time, the animation rate should be slow
        verify(mHolder.animator).animateTo(eq(leadBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_SLOW_DECREASE));
                eq(BRIGHTNESS_RAMP_RATE_SLOW_DECREASE), eq(false));
        verify(followerDpc.hbmController).onAmbientLuxChange(ambientLux);
        verify(followerDpc.animator).animateTo(eq(followerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_SLOW_DECREASE));
                eq(BRIGHTNESS_RAMP_RATE_SLOW_DECREASE), eq(false));
    }

    @Test
@@ -552,7 +552,7 @@ public final class DisplayPowerController2Test {
        followerListener.onBrightnessChanged(initialFollowerBrightness);
        advanceTime(1);
        verify(followerDpc.animator).animateTo(eq(initialFollowerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(followerDpc.displayPowerState.getScreenBrightness())
                .thenReturn(initialFollowerBrightness);
@@ -573,11 +573,11 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(secondFollowerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(mHolder.displayPowerState.getScreenBrightness()).thenReturn(brightness);
        when(followerDpc.displayPowerState.getScreenBrightness()).thenReturn(brightness);
@@ -588,7 +588,7 @@ public final class DisplayPowerController2Test {
        mHolder.dpc.removeDisplayBrightnessFollower(followerDpc.dpc);
        advanceTime(1);
        verify(followerDpc.animator).animateTo(eq(initialFollowerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_DECREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_DECREASE), eq(false));

        when(followerDpc.displayPowerState.getScreenBrightness())
                .thenReturn(initialFollowerBrightness);
@@ -606,10 +606,11 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
        verify(followerDpc.animator, never()).animateTo(anyFloat(), anyFloat(), anyFloat());
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerDpc.animator, never()).animateTo(anyFloat(), anyFloat(), anyFloat(),
                anyBoolean());
        verify(secondFollowerDpc.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
    }

    @Test
@@ -652,9 +653,9 @@ public final class DisplayPowerController2Test {
        secondFollowerListener.onBrightnessChanged(initialFollowerBrightness);
        advanceTime(1);
        verify(followerHolder.animator).animateTo(eq(initialFollowerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(secondFollowerHolder.animator).animateTo(eq(initialFollowerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(followerHolder.displayPowerState.getScreenBrightness())
                .thenReturn(initialFollowerBrightness);
@@ -677,11 +678,11 @@ public final class DisplayPowerController2Test {
        listener.onBrightnessChanged(brightness);
        advanceTime(1); // Send messages, run updatePowerState
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(followerHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));
        verify(secondFollowerHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(mHolder.displayPowerState.getScreenBrightness()).thenReturn(brightness);
        when(followerHolder.displayPowerState.getScreenBrightness()).thenReturn(brightness);
@@ -692,9 +693,9 @@ public final class DisplayPowerController2Test {
        mHolder.dpc.stop();
        advanceTime(1);
        verify(followerHolder.animator).animateTo(eq(initialFollowerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_DECREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_DECREASE), eq(false));
        verify(secondFollowerHolder.animator).animateTo(eq(initialFollowerBrightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_DECREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_DECREASE), eq(false));
        clearInvocations(followerHolder.animator, secondFollowerHolder.animator);
    }

@@ -716,7 +717,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator).animateTo(eq(sdrBrightness), eq(sdrBrightness),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(mHolder.displayPowerState.getScreenBrightness()).thenReturn(sdrBrightness);
        when(mHolder.displayPowerState.getSdrScreenBrightness()).thenReturn(sdrBrightness);
@@ -730,7 +731,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator).animateTo(eq(hdrBrightness), eq(sdrBrightness),
                eq(BRIGHTNESS_RAMP_RATE_MINIMUM));
                eq(BRIGHTNESS_RAMP_RATE_MINIMUM), eq(false));
    }

    @Test
@@ -756,7 +757,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator).animateTo(eq(hdrBrightness), eq(sdrBrightness),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(mHolder.displayPowerState.getScreenBrightness()).thenReturn(hdrBrightness);
        when(mHolder.displayPowerState.getSdrScreenBrightness()).thenReturn(sdrBrightness);
@@ -769,7 +770,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator).animateTo(eq(sdrBrightness), eq(sdrBrightness),
                eq(BRIGHTNESS_RAMP_RATE_MINIMUM));
                eq(BRIGHTNESS_RAMP_RATE_MINIMUM), eq(false));
    }

    @Test
@@ -823,7 +824,7 @@ public final class DisplayPowerController2Test {

        verify(mHolder.screenOffBrightnessSensorController, atLeastOnce())
                .getAutomaticScreenBrightness();
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(), anyFloat());
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(), anyFloat(), eq(false));
    }

    @Test
@@ -861,7 +862,7 @@ public final class DisplayPowerController2Test {

        verify(mHolder.screenOffBrightnessSensorController, atLeastOnce())
                .getAutomaticScreenBrightness();
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(), anyFloat());
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(), anyFloat(), eq(false));
    }

    @Test
@@ -1104,7 +1105,8 @@ public final class DisplayPowerController2Test {
        mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false);
        advanceTime(1); // Run updatePowerState
        // One triggered by handleBrightnessModeChange, another triggered by onDisplayChanged
        verify(mHolder.animator, times(2)).animateTo(eq(newBrightness), anyFloat(), anyFloat());
        verify(mHolder.animator, times(2)).animateTo(eq(newBrightness), anyFloat(), anyFloat(),
                eq(false));
    }

    @Test
@@ -1213,7 +1215,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE));
                eq(BRIGHTNESS_RAMP_RATE_FAST_INCREASE), eq(false));

        when(mHolder.displayPowerState.getScreenBrightness()).thenReturn(brightness);
        brightness = 0.05f;
@@ -1225,7 +1227,7 @@ public final class DisplayPowerController2Test {

        // The second time, the animation rate should be slow
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_SLOW_DECREASE_IDLE));
                eq(BRIGHTNESS_RAMP_RATE_SLOW_DECREASE_IDLE), eq(false));

        brightness = 0.9f;
        when(mHolder.automaticBrightnessController.getAutomaticScreenBrightness(
@@ -1235,7 +1237,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState
        // The third time, the animation rate should be slow
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(),
                eq(BRIGHTNESS_RAMP_RATE_SLOW_INCREASE_IDLE));
                eq(BRIGHTNESS_RAMP_RATE_SLOW_INCREASE_IDLE), eq(false));
    }

    @Test
@@ -1272,7 +1274,7 @@ public final class DisplayPowerController2Test {
        advanceTime(1); // Run updatePowerState

        verify(mHolder.animator, atLeastOnce()).animateTo(eq(clampedBrightness), anyFloat(),
                eq(transitionRate));
                eq(transitionRate), eq(true));
    }

    /**
+41 −39

File changed.

Preview size limit exceeded, changes collapsed.

Loading