diff --git a/services/core/java/com/android/server/display/AutomaticBrightnessController.java b/services/core/java/com/android/server/display/AutomaticBrightnessController.java index f4d7f9ac5a5e79df6241b7b9eb95426b0e0d14a3..36d69c93c1cbfe49ed893b64da22272a1f285dde 100644 --- a/services/core/java/com/android/server/display/AutomaticBrightnessController.java +++ b/services/core/java/com/android/server/display/AutomaticBrightnessController.java @@ -89,10 +89,8 @@ class AutomaticBrightnessController { private final BrightnessMappingStrategy mBrightnessMapper; // The minimum and maximum screen brightnesses. - private final int mScreenBrightnessRangeMinimum; - private final int mScreenBrightnessRangeMaximum; - private final float mScreenBrightnessRangeMinimumFloat; - private final float mScreenBrightnessRangeMaximumFloat; + private final float mScreenBrightnessRangeMinimum; + private final float mScreenBrightnessRangeMaximum; // How much to scale doze brightness by (should be (0, 1.0]). private final float mDozeScaleFactor; @@ -156,7 +154,6 @@ class AutomaticBrightnessController { // The screen brightness threshold at which to brighten or darken the screen. private float mScreenBrighteningThreshold; private float mScreenDarkeningThreshold; - // The most recent light sample. private float mLastObservedLux; @@ -177,8 +174,9 @@ class AutomaticBrightnessController { // We preserve this value even when we stop using the light sensor so // that we can quickly revert to the previous auto-brightness level // while the light sensor warms up. - // Use -1 if there is no current auto-brightness value available. - private int mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID; + // Use PowerManager.BRIGHTNESS_INVALID_FLOAT if there is no current auto-brightness value + // available. + private float mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT; // The current display policy. This is useful, for example, for knowing when we're dozing, // where the light sensor may not be available. @@ -188,7 +186,7 @@ class AutomaticBrightnessController { // for the initial state of the sample. private boolean mBrightnessAdjustmentSamplePending; 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 // user's adjustment) immediately, but wait for a drastic enough change in the ambient light. @@ -243,13 +241,8 @@ class AutomaticBrightnessController { mCallbacks = callbacks; mSensorManager = sensorManager; mBrightnessMapper = mapper; - mScreenBrightnessRangeMinimum = - BrightnessSynchronizer.brightnessFloatToInt(mContext, brightnessMin); - mScreenBrightnessRangeMaximum = - com.android.internal.BrightnessSynchronizer.brightnessFloatToInt( - mContext, brightnessMax); - mScreenBrightnessRangeMinimumFloat = brightnessMin; - mScreenBrightnessRangeMaximumFloat = brightnessMax; + mScreenBrightnessRangeMinimum = brightnessMin; + mScreenBrightnessRangeMaximum = brightnessMax; mLightSensorWarmUpTimeConfig = lightSensorWarmUpTime; mDozeScaleFactor = dozeScaleFactor; mNormalLightSensorRate = lightSensorRate; @@ -299,12 +292,12 @@ class AutomaticBrightnessController { return true; } - public int getAutomaticScreenBrightness() { + public float getAutomaticScreenBrightness() { if (!mAmbientLuxValid) { - return -1; + return PowerManager.BRIGHTNESS_INVALID_FLOAT; } if (mDisplayPolicy == DisplayPowerRequest.POLICY_DOZE) { - return Math.round(mScreenAutoBrightness * mDozeScaleFactor); + return mScreenAutoBrightness * mDozeScaleFactor; } return mScreenAutoBrightness; } @@ -385,7 +378,7 @@ class AutomaticBrightnessController { private boolean setScreenBrightnessByUser(float brightness) { 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. return false; } @@ -486,7 +479,7 @@ class AutomaticBrightnessController { } else if (mLightSensorEnabled) { mLightSensorEnabled = false; mAmbientLuxValid = !mResetAmbientLuxAfterWarmUpConfig; - mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID; + mScreenAutoBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT; mRecentLightSamples = 0; mAmbientLightRingBuffer.clear(); mCurrentLightSensorRate = -1; @@ -735,29 +728,28 @@ class AutomaticBrightnessController { float value = mBrightnessMapper.getBrightness(mAmbientLux, mForegroundAppPackageName, mForegroundAppCategory); - int newScreenAutoBrightness = BrightnessSynchronizer.brightnessFloatToInt( - mContext, clampScreenBrightnessFloat(value)); + float newScreenAutoBrightness = clampScreenBrightness(value); // 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 // previous one. - if (mScreenAutoBrightness != -1 + if (!Float.isNaN(mScreenAutoBrightness) && !isManuallySet && newScreenAutoBrightness > mScreenDarkeningThreshold && newScreenAutoBrightness < mScreenBrighteningThreshold) { if (mLoggingEnabled) { - Slog.d(TAG, "ignoring newScreenAutoBrightness: " + mScreenDarkeningThreshold - + " < " + newScreenAutoBrightness + " < " + mScreenBrighteningThreshold); + Slog.d(TAG, "ignoring newScreenAutoBrightness: " + + mScreenDarkeningThreshold + " < " + newScreenAutoBrightness + + " < " + mScreenBrighteningThreshold); } return; } - - if (mScreenAutoBrightness != newScreenAutoBrightness) { + if (!BrightnessSynchronizer.floatEquals(mScreenAutoBrightness, + newScreenAutoBrightness)) { if (mLoggingEnabled) { - Slog.d(TAG, "updateAutoBrightness: " + - "mScreenAutoBrightness=" + mScreenAutoBrightness + ", " + - "newScreenAutoBrightness=" + newScreenAutoBrightness); + Slog.d(TAG, "updateAutoBrightness: " + + "mScreenAutoBrightness=" + mScreenAutoBrightness + ", " + + "newScreenAutoBrightness=" + newScreenAutoBrightness); } - mScreenAutoBrightness = newScreenAutoBrightness; mScreenBrighteningThreshold = clampScreenBrightness( mScreenBrightnessThresholds.getBrighteningThreshold(newScreenAutoBrightness)); @@ -770,19 +762,12 @@ class AutomaticBrightnessController { } } - // Clamps values with float range [1.0-255.0] - // TODO(brightnessfloat): convert everything that uses this to float system + // Clamps values with float range [0.0-1.0] private float clampScreenBrightness(float value) { return MathUtils.constrain(value, 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() { if (!mBrightnessAdjustmentSamplePending) { mBrightnessAdjustmentSamplePending = true; @@ -806,12 +791,13 @@ class AutomaticBrightnessController { private void collectBrightnessAdjustmentSample() { if (mBrightnessAdjustmentSamplePending) { mBrightnessAdjustmentSamplePending = false; - if (mAmbientLuxValid && mScreenAutoBrightness >= 0) { + if (mAmbientLuxValid && (mScreenAutoBrightness >= PowerManager.BRIGHTNESS_MIN + || mScreenAutoBrightness == PowerManager.BRIGHTNESS_OFF_FLOAT)) { if (mLoggingEnabled) { - Slog.d(TAG, "Auto-brightness adjustment changed by user: " + - "lux=" + mAmbientLux + ", " + - "brightness=" + mScreenAutoBrightness + ", " + - "ring=" + mAmbientLightRingBuffer); + Slog.d(TAG, "Auto-brightness adjustment changed by user: " + + "lux=" + mAmbientLux + ", " + + "brightness=" + mScreenAutoBrightness + ", " + + "ring=" + mAmbientLightRingBuffer); } EventLog.writeEvent(EventLogTags.AUTO_BRIGHTNESS_ADJ, diff --git a/services/core/java/com/android/server/display/DisplayPowerController.java b/services/core/java/com/android/server/display/DisplayPowerController.java index 48e30bf42c2d19f6d7dfa0c1ed21a5d945f7ef69..bafeb77c55e6c2118374d891d199ee1d982177b5 100644 --- a/services/core/java/com/android/server/display/DisplayPowerController.java +++ b/services/core/java/com/android/server/display/DisplayPowerController.java @@ -948,8 +948,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call if (Float.isNaN(brightnessState)) { float newAutoBrightnessAdjustment = autoBrightnessAdjustment; if (autoBrightnessEnabled) { - brightnessState = BrightnessSynchronizer.brightnessIntToFloat( - mContext, mAutomaticBrightnessController.getAutomaticScreenBrightness()); + brightnessState = mAutomaticBrightnessController.getAutomaticScreenBrightness(); newAutoBrightnessAdjustment = mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment(); } diff --git a/services/core/java/com/android/server/display/HysteresisLevels.java b/services/core/java/com/android/server/display/HysteresisLevels.java index f0a505d4d8183b8c4091d833d6f32712bd2b9b0f..2b565698ff8cec388ad34ff92f8768563a038d59 100644 --- a/services/core/java/com/android/server/display/HysteresisLevels.java +++ b/services/core/java/com/android/server/display/HysteresisLevels.java @@ -64,8 +64,8 @@ public class HysteresisLevels { * Return the brightening hysteresis threshold for the given value level. */ public float getBrighteningThreshold(float value) { - float brightConstant = getReferenceLevel(value, mBrighteningThresholds); - float brightThreshold = value * (1.0f + brightConstant); + final float brightConstant = getReferenceLevel(value, mBrighteningThresholds); + final float brightThreshold = value * (1.0f + brightConstant); if (DEBUG) { Slog.d(TAG, "bright hysteresis constant=" + brightConstant + ", threshold=" + brightThreshold + ", value=" + value); @@ -77,8 +77,8 @@ public class HysteresisLevels { * Return the darkening hysteresis threshold for the given value level. */ public float getDarkeningThreshold(float value) { - float darkConstant = getReferenceLevel(value, mDarkeningThresholds); - float darkThreshold = value * (1.0f - darkConstant); + final float darkConstant = getReferenceLevel(value, mDarkeningThresholds); + final float darkThreshold = value * (1.0f - darkConstant); if (DEBUG) { Slog.d(TAG, "dark hysteresis constant=: " + darkConstant + ", threshold=" + darkThreshold + ", value=" + value); diff --git a/services/tests/servicestests/src/com/android/server/display/AutomaticBrightnessControllerTest.java b/services/tests/servicestests/src/com/android/server/display/AutomaticBrightnessControllerTest.java index 7b3417a67857645012fb05ada2598051c61c8067..b69cc47ba7385e3a80b1a277a88562326595df8c 100644 --- a/services/tests/servicestests/src/com/android/server/display/AutomaticBrightnessControllerTest.java +++ b/services/tests/servicestests/src/com/android/server/display/AutomaticBrightnessControllerTest.java @@ -107,9 +107,10 @@ public class AutomaticBrightnessControllerTest { eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class)); 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 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)) .thenReturn(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 // brightening threshold is beyond the maximum brightness value...so that we can test that // our threshold clamping works. - when(mScreenBrightnessThresholds.getBrighteningThreshold(5)).thenReturn(1.0f); + when(mScreenBrightnessThresholds.getBrighteningThreshold(normalizedBrightness1)) + .thenReturn(1.0f); // Send new sensor value and verify listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux1)); - assertEquals(5, controller.getAutomaticScreenBrightness()); + assertEquals(normalizedBrightness1, controller.getAutomaticScreenBrightness(), 0.001f); - - // Set up system to return 255 as a brightness value + // Set up system to return 0.0f (minimum possible brightness) as a brightness value float lux2 = 10.0f; float normalizedBrightness2 = 0.0f; when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2)) @@ -139,7 +140,7 @@ public class AutomaticBrightnessControllerTest { // Send new sensor value and verify listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2)); - assertEquals(1, controller.getAutomaticScreenBrightness()); + assertEquals(normalizedBrightness2, controller.getAutomaticScreenBrightness(), 0.001f); } @Test @@ -153,9 +154,9 @@ public class AutomaticBrightnessControllerTest { eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class)); 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 normalizedBrightness1 = 0.981f; //float equivalent of 250 out of 255 + float normalizedBrightness1 = 0.98f; when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux1)) .thenReturn(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 // brightening threshold is beyond the maximum brightness value...so that we can test that // our threshold clamping works. - when(mScreenBrightnessThresholds.getBrighteningThreshold(250)).thenReturn(260.0f); + when(mScreenBrightnessThresholds.getBrighteningThreshold(normalizedBrightness1)) + .thenReturn(1.1f); // Send new sensor value and verify 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 normalizedBrightness2 = 1.0f; when(mAmbientBrightnessThresholds.getBrighteningThreshold(lux2)) @@ -185,7 +187,7 @@ public class AutomaticBrightnessControllerTest { // Send new sensor value and verify listener.onSensorChanged(TestUtils.createSensorEvent(lightSensor, (int) lux2)); - assertEquals(255, controller.getAutomaticScreenBrightness()); + assertEquals(normalizedBrightness2, controller.getAutomaticScreenBrightness(), 0.001f); } @Test @@ -204,10 +206,10 @@ public class AutomaticBrightnessControllerTest { // User sets brightness to 100 controller.configure(true /* enable */, null /* configuration */, - 100 /* brightness */, true /* userChangedBrightness */, 0 /* adjustment */, + 0.5f /* brightness */, true /* userChangedBrightness */, 0 /* adjustment */, false /* userChanged */, DisplayPowerRequest.POLICY_BRIGHT); // There should be a user data point added to the mapper. - verify(mBrightnessMappingStrategy).addUserDataPoint(1000f, 100); + verify(mBrightnessMappingStrategy).addUserDataPoint(1000f, 0.5f); } }