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

Commit f972a08b authored by Fiona Campbell's avatar Fiona Campbell
Browse files

Convert AutomaticBrightnessController to float

Change the Autobrightness system to use a float value for brightness.

Test: AutomaticBrightnessControllerTest

Bug: 150671605
Change-Id: Id0bc3e4f6fd14c7cab692f2cb6b40a2c5a8b622d
parent e6bd8505
Loading
Loading
Loading
Loading
+30 −44
Original line number Original line Diff line number Diff line
@@ -89,10 +89,8 @@ class AutomaticBrightnessController {
    private final BrightnessMappingStrategy mBrightnessMapper;
    private final BrightnessMappingStrategy mBrightnessMapper;


    // The minimum and maximum screen brightnesses.
    // The minimum and maximum screen brightnesses.
    private final int mScreenBrightnessRangeMinimum;
    private final float mScreenBrightnessRangeMinimum;
    private final int mScreenBrightnessRangeMaximum;
    private final float mScreenBrightnessRangeMaximum;
    private final float mScreenBrightnessRangeMinimumFloat;
    private final float mScreenBrightnessRangeMaximumFloat;


    // How much to scale doze brightness by (should be (0, 1.0]).
    // How much to scale doze brightness by (should be (0, 1.0]).
    private final float mDozeScaleFactor;
    private final float mDozeScaleFactor;
@@ -156,7 +154,6 @@ class AutomaticBrightnessController {
    // The screen brightness threshold at which to brighten or darken the screen.
    // The screen brightness threshold at which to brighten or darken the screen.
    private float mScreenBrighteningThreshold;
    private float mScreenBrighteningThreshold;
    private float mScreenDarkeningThreshold;
    private float mScreenDarkeningThreshold;

    // The most recent light sample.
    // The most recent light sample.
    private float mLastObservedLux;
    private float mLastObservedLux;


@@ -177,8 +174,9 @@ class AutomaticBrightnessController {
    // We preserve this value even when we stop using the light sensor so
    // We preserve this value even when we stop using the light sensor so
    // that we can quickly revert to the previous auto-brightness level
    // that we can quickly revert to the previous auto-brightness level
    // while the light sensor warms up.
    // while the light sensor warms up.
    // Use -1 if there is no current auto-brightness value available.
    // Use PowerManager.BRIGHTNESS_INVALID_FLOAT if there is no current auto-brightness value
    private int mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID;
    // available.
    private float mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;


    // The current display policy. This is useful, for example,  for knowing when we're dozing,
    // The current display policy. This is useful, for example,  for knowing when we're dozing,
    // where the light sensor may not be available.
    // where the light sensor may not be available.
@@ -188,7 +186,7 @@ class AutomaticBrightnessController {
    // for the initial state of the sample.
    // for the initial state of the sample.
    private boolean mBrightnessAdjustmentSamplePending;
    private boolean mBrightnessAdjustmentSamplePending;
    private float mBrightnessAdjustmentSampleOldLux;
    private float mBrightnessAdjustmentSampleOldLux;
    private int mBrightnessAdjustmentSampleOldBrightness;
    private float mBrightnessAdjustmentSampleOldBrightness;


    // When the short term model is invalidated, we don't necessarily reset it (i.e. clear the
    // When the short term model is invalidated, we don't necessarily reset it (i.e. clear the
    // user's adjustment) immediately, but wait for a drastic enough change in the ambient light.
    // user's adjustment) immediately, but wait for a drastic enough change in the ambient light.
@@ -243,13 +241,8 @@ class AutomaticBrightnessController {
        mCallbacks = callbacks;
        mCallbacks = callbacks;
        mSensorManager = sensorManager;
        mSensorManager = sensorManager;
        mBrightnessMapper = mapper;
        mBrightnessMapper = mapper;
        mScreenBrightnessRangeMinimum =
        mScreenBrightnessRangeMinimum = brightnessMin;
                BrightnessSynchronizer.brightnessFloatToInt(mContext, brightnessMin);
        mScreenBrightnessRangeMaximum = brightnessMax;
        mScreenBrightnessRangeMaximum =
                com.android.internal.BrightnessSynchronizer.brightnessFloatToInt(
                        mContext, brightnessMax);
        mScreenBrightnessRangeMinimumFloat = brightnessMin;
        mScreenBrightnessRangeMaximumFloat = brightnessMax;
        mLightSensorWarmUpTimeConfig = lightSensorWarmUpTime;
        mLightSensorWarmUpTimeConfig = lightSensorWarmUpTime;
        mDozeScaleFactor = dozeScaleFactor;
        mDozeScaleFactor = dozeScaleFactor;
        mNormalLightSensorRate = lightSensorRate;
        mNormalLightSensorRate = lightSensorRate;
@@ -299,12 +292,12 @@ class AutomaticBrightnessController {
        return true;
        return true;
    }
    }


    public int getAutomaticScreenBrightness() {
    public float getAutomaticScreenBrightness() {
        if (!mAmbientLuxValid) {
        if (!mAmbientLuxValid) {
            return -1;
            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
        }
        }
        if (mDisplayPolicy == DisplayPowerRequest.POLICY_DOZE) {
        if (mDisplayPolicy == DisplayPowerRequest.POLICY_DOZE) {
            return Math.round(mScreenAutoBrightness * mDozeScaleFactor);
            return mScreenAutoBrightness * mDozeScaleFactor;
        }
        }
        return mScreenAutoBrightness;
        return mScreenAutoBrightness;
    }
    }
@@ -385,7 +378,7 @@ class AutomaticBrightnessController {


    private boolean setScreenBrightnessByUser(float brightness) {
    private boolean setScreenBrightnessByUser(float brightness) {
        if (!mAmbientLuxValid) {
        if (!mAmbientLuxValid) {
            // If we don't have a valid ambient lux then we don't have a valid brightness anyways,
            // If we don't have a valid ambient lux then we don't have a valid brightness anyway,
            // and we can't use this data to add a new control point to the short-term model.
            // and we can't use this data to add a new control point to the short-term model.
            return false;
            return false;
        }
        }
@@ -486,7 +479,7 @@ class AutomaticBrightnessController {
        } else if (mLightSensorEnabled) {
        } else if (mLightSensorEnabled) {
            mLightSensorEnabled = false;
            mLightSensorEnabled = false;
            mAmbientLuxValid = !mResetAmbientLuxAfterWarmUpConfig;
            mAmbientLuxValid = !mResetAmbientLuxAfterWarmUpConfig;
            mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID;
            mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
            mRecentLightSamples = 0;
            mRecentLightSamples = 0;
            mAmbientLightRingBuffer.clear();
            mAmbientLightRingBuffer.clear();
            mCurrentLightSensorRate = -1;
            mCurrentLightSensorRate = -1;
@@ -735,29 +728,28 @@ class AutomaticBrightnessController {


        float value = mBrightnessMapper.getBrightness(mAmbientLux, mForegroundAppPackageName,
        float value = mBrightnessMapper.getBrightness(mAmbientLux, mForegroundAppPackageName,
                mForegroundAppCategory);
                mForegroundAppCategory);
        int newScreenAutoBrightness = BrightnessSynchronizer.brightnessFloatToInt(
        float newScreenAutoBrightness = clampScreenBrightness(value);
                mContext, clampScreenBrightnessFloat(value));
        // If screenAutoBrightness is set, we should have screen{Brightening,Darkening}Threshold,
        // If screenAutoBrightness is set, we should have screen{Brightening,Darkening}Threshold,
        // in which case we ignore the new screen brightness if it doesn't differ enough from the
        // in which case we ignore the new screen brightness if it doesn't differ enough from the
        // previous one.
        // previous one.
        if (mScreenAutoBrightness != -1
        if (!Float.isNaN(mScreenAutoBrightness)
                && !isManuallySet
                && !isManuallySet
                && newScreenAutoBrightness > mScreenDarkeningThreshold
                && newScreenAutoBrightness > mScreenDarkeningThreshold
                && newScreenAutoBrightness < mScreenBrighteningThreshold) {
                && newScreenAutoBrightness < mScreenBrighteningThreshold) {
            if (mLoggingEnabled) {
            if (mLoggingEnabled) {
                Slog.d(TAG, "ignoring newScreenAutoBrightness: " + mScreenDarkeningThreshold
                Slog.d(TAG, "ignoring newScreenAutoBrightness: "
                        + " < " + newScreenAutoBrightness + " < " + mScreenBrighteningThreshold);
                        + mScreenDarkeningThreshold + " < " + newScreenAutoBrightness
                        + " < " + mScreenBrighteningThreshold);
            }
            }
            return;
            return;
        }
        }

        if (!BrightnessSynchronizer.floatEquals(mScreenAutoBrightness,
        if (mScreenAutoBrightness != newScreenAutoBrightness) {
                newScreenAutoBrightness)) {
            if (mLoggingEnabled) {
            if (mLoggingEnabled) {
                Slog.d(TAG, "updateAutoBrightness: " +
                Slog.d(TAG, "updateAutoBrightness: "
                        "mScreenAutoBrightness=" + mScreenAutoBrightness + ", " +
                        + "mScreenAutoBrightness=" + mScreenAutoBrightness + ", "
                        "newScreenAutoBrightness=" + newScreenAutoBrightness);
                        + "newScreenAutoBrightness=" + newScreenAutoBrightness);
            }
            }

            mScreenAutoBrightness = newScreenAutoBrightness;
            mScreenAutoBrightness = newScreenAutoBrightness;
            mScreenBrighteningThreshold = clampScreenBrightness(
            mScreenBrighteningThreshold = clampScreenBrightness(
                    mScreenBrightnessThresholds.getBrighteningThreshold(newScreenAutoBrightness));
                    mScreenBrightnessThresholds.getBrighteningThreshold(newScreenAutoBrightness));
@@ -770,19 +762,12 @@ class AutomaticBrightnessController {
        }
        }
    }
    }


    // Clamps values with float range [1.0-255.0]
    // Clamps values with float range [0.0-1.0]
    // TODO(brightnessfloat): convert everything that uses this to float system
    private float clampScreenBrightness(float value) {
    private float clampScreenBrightness(float value) {
        return MathUtils.constrain(value,
        return MathUtils.constrain(value,
                mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum);
                mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum);
    }
    }


    // Clamps values with float range [0.0-1.0]
    private float clampScreenBrightnessFloat(float value) {
        return MathUtils.constrain(value,
                mScreenBrightnessRangeMinimumFloat, mScreenBrightnessRangeMaximumFloat);
    }

    private void prepareBrightnessAdjustmentSample() {
    private void prepareBrightnessAdjustmentSample() {
        if (!mBrightnessAdjustmentSamplePending) {
        if (!mBrightnessAdjustmentSamplePending) {
            mBrightnessAdjustmentSamplePending = true;
            mBrightnessAdjustmentSamplePending = true;
@@ -806,12 +791,13 @@ class AutomaticBrightnessController {
    private void collectBrightnessAdjustmentSample() {
    private void collectBrightnessAdjustmentSample() {
        if (mBrightnessAdjustmentSamplePending) {
        if (mBrightnessAdjustmentSamplePending) {
            mBrightnessAdjustmentSamplePending = false;
            mBrightnessAdjustmentSamplePending = false;
            if (mAmbientLuxValid && mScreenAutoBrightness >= 0) {
            if (mAmbientLuxValid && (mScreenAutoBrightness >= PowerManager.BRIGHTNESS_MIN
                    || mScreenAutoBrightness == PowerManager.BRIGHTNESS_OFF_FLOAT)) {
                if (mLoggingEnabled) {
                if (mLoggingEnabled) {
                    Slog.d(TAG, "Auto-brightness adjustment changed by user: " +
                    Slog.d(TAG, "Auto-brightness adjustment changed by user: "
                            "lux=" + mAmbientLux + ", " +
                            + "lux=" + mAmbientLux + ", "
                            "brightness=" + mScreenAutoBrightness + ", " +
                            + "brightness=" + mScreenAutoBrightness + ", "
                            "ring=" + mAmbientLightRingBuffer);
                            + "ring=" + mAmbientLightRingBuffer);
                }
                }


                EventLog.writeEvent(EventLogTags.AUTO_BRIGHTNESS_ADJ,
                EventLog.writeEvent(EventLogTags.AUTO_BRIGHTNESS_ADJ,
+1 −2
Original line number Original line Diff line number Diff line
@@ -942,8 +942,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
        if (Float.isNaN(brightnessState)) {
        if (Float.isNaN(brightnessState)) {
            float newAutoBrightnessAdjustment = autoBrightnessAdjustment;
            float newAutoBrightnessAdjustment = autoBrightnessAdjustment;
            if (autoBrightnessEnabled) {
            if (autoBrightnessEnabled) {
                brightnessState = BrightnessSynchronizer.brightnessIntToFloat(
                brightnessState = mAutomaticBrightnessController.getAutomaticScreenBrightness();
                mContext, mAutomaticBrightnessController.getAutomaticScreenBrightness());
                newAutoBrightnessAdjustment =
                newAutoBrightnessAdjustment =
                        mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment();
                        mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment();
            }
            }
+4 −4
Original line number Original line Diff line number Diff line
@@ -64,8 +64,8 @@ public class HysteresisLevels {
     * Return the brightening hysteresis threshold for the given value level.
     * Return the brightening hysteresis threshold for the given value level.
     */
     */
    public float getBrighteningThreshold(float value) {
    public float getBrighteningThreshold(float value) {
        float brightConstant = getReferenceLevel(value, mBrighteningThresholds);
        final float brightConstant = getReferenceLevel(value, mBrighteningThresholds);
        float brightThreshold = value * (1.0f + brightConstant);
        final float brightThreshold = value * (1.0f + brightConstant);
        if (DEBUG) {
        if (DEBUG) {
            Slog.d(TAG, "bright hysteresis constant=" + brightConstant + ", threshold="
            Slog.d(TAG, "bright hysteresis constant=" + brightConstant + ", threshold="
                    + brightThreshold + ", value=" + value);
                    + brightThreshold + ", value=" + value);
@@ -77,8 +77,8 @@ public class HysteresisLevels {
     * Return the darkening hysteresis threshold for the given value level.
     * Return the darkening hysteresis threshold for the given value level.
     */
     */
    public float getDarkeningThreshold(float value) {
    public float getDarkeningThreshold(float value) {
        float darkConstant = getReferenceLevel(value, mDarkeningThresholds);
        final float darkConstant = getReferenceLevel(value, mDarkeningThresholds);
        float darkThreshold = value * (1.0f - darkConstant);
        final float darkThreshold = value * (1.0f - darkConstant);
        if (DEBUG) {
        if (DEBUG) {
            Slog.d(TAG, "dark hysteresis constant=: " + darkConstant + ", threshold="
            Slog.d(TAG, "dark hysteresis constant=: " + darkConstant + ", threshold="
                    + darkThreshold + ", value=" + value);
                    + darkThreshold + ", value=" + value);
+17 −15
Original line number Original line Diff line number Diff line
@@ -107,9 +107,10 @@ public class AutomaticBrightnessControllerTest {
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


        // Set up system to return 5 as a brightness value
        // Set up system to return 0.02f as a brightness value
        float lux1 = 100.0f;
        float lux1 = 100.0f;
        float normalizedBrightness1 = 0.0158f; //float equivalent of 5 out of 255
        // Brightness as float (from 0.0f to 1.0f)
        float normalizedBrightness1 = 0.02f;
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1))
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1))
                .thenReturn(lux1);
                .thenReturn(lux1);
        when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux1))
        when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux1))
@@ -120,14 +121,14 @@ public class AutomaticBrightnessControllerTest {
        // This is the important bit: When the new brightness is set, make sure the new
        // This is the important bit: When the new brightness is set, make sure the new
        // brightening threshold is beyond the maximum brightness value...so that we can test that
        // brightening threshold is beyond the maximum brightness value...so that we can test that
        // our threshold clamping works.
        // our threshold clamping works.
        when(mScreenBrightnessThresholds.getBrighteningThreshold(5)).thenReturn(1.0f);
        when(mScreenBrightnessThresholds.getBrighteningThreshold(normalizedBrightness1))
                .thenReturn(1.0f);


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
        assertEquals(5, controller.getAutomaticScreenBrightness());
        assertEquals(normalizedBrightness1, controller.getAutomaticScreenBrightness(), 0.001f);



        // Set up system to return 0.0f (minimum possible brightness) as a brightness value
        // Set up system to return 255 as a brightness value
        float lux2 = 10.0f;
        float lux2 = 10.0f;
        float normalizedBrightness2 = 0.0f;
        float normalizedBrightness2 = 0.0f;
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2))
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2))
@@ -139,7 +140,7 @@ public class AutomaticBrightnessControllerTest {


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
        assertEquals(1, controller.getAutomaticScreenBrightness());
        assertEquals(normalizedBrightness2, controller.getAutomaticScreenBrightness(), 0.001f);
    }
    }


    @Test
    @Test
@@ -153,9 +154,9 @@ public class AutomaticBrightnessControllerTest {
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();
        SensorEventListener listener = listenerCaptor.getValue();


        // Set up system to return 250 as a brightness value
        // Set up system to return 0.98f as a brightness value
        float lux1 = 100.0f;
        float lux1 = 100.0f;
        float normalizedBrightness1 = 0.981f; //float equivalent of 250 out of 255
        float normalizedBrightness1 = 0.98f;
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1))
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1))
                .thenReturn(lux1);
                .thenReturn(lux1);
        when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux1))
        when(mAmbientBrightnessThresholds.getDarkeningThreshold(lux1))
@@ -166,14 +167,15 @@ public class AutomaticBrightnessControllerTest {
        // This is the important bit: When the new brightness is set, make sure the new
        // This is the important bit: When the new brightness is set, make sure the new
        // brightening threshold is beyond the maximum brightness value...so that we can test that
        // brightening threshold is beyond the maximum brightness value...so that we can test that
        // our threshold clamping works.
        // our threshold clamping works.
        when(mScreenBrightnessThresholds.getBrighteningThreshold(250)).thenReturn(260.0f);
        when(mScreenBrightnessThresholds.getBrighteningThreshold(normalizedBrightness1))
                .thenReturn(1.1f);


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1));
        assertEquals(250, controller.getAutomaticScreenBrightness());
        assertEquals(normalizedBrightness1, controller.getAutomaticScreenBrightness(), 0.001f);




        // Set up system to return 255 as a brightness value
        // Set up system to return 1.0f as a brightness value (brightness_max)
        float lux2 = 110.0f;
        float lux2 = 110.0f;
        float normalizedBrightness2 = 1.0f;
        float normalizedBrightness2 = 1.0f;
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2))
        when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2))
@@ -185,7 +187,7 @@ public class AutomaticBrightnessControllerTest {


        // Send new sensor value and verify
        // Send new sensor value and verify
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
        listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2));
        assertEquals(255, controller.getAutomaticScreenBrightness());
        assertEquals(normalizedBrightness2, controller.getAutomaticScreenBrightness(), 0.001f);
    }
    }


    @Test
    @Test
@@ -204,10 +206,10 @@ public class AutomaticBrightnessControllerTest {


        // User sets brightness to 100
        // User sets brightness to 100
        controller.configure(true /* enable */, null /* configuration */,
        controller.configure(true /* enable */, null /* configuration */,
                100 /* brightness */, true /* userChangedBrightness */, 0 /* adjustment */,
                0.5f /* brightness */, true /* userChangedBrightness */, 0 /* adjustment */,
                false /* userChanged */, DisplayPowerRequest.POLICY_BRIGHT);
                false /* userChanged */, DisplayPowerRequest.POLICY_BRIGHT);


        // There should be a user data point added to the mapper.
        // There should be a user data point added to the mapper.
        verify(mBrightnessMappingStrategy).addUserDataPoint(1000f, 100);
        verify(mBrightnessMappingStrategy).addUserDataPoint(1000f, 0.5f);
    }
    }
}
}