3232import android .os .Handler ;
3333import android .os .Looper ;
3434import android .os .Message ;
35+ import android .os .PowerManager ;
3536import android .os .SystemClock ;
37+ import android .text .format .DateUtils ;
38+ import android .util .FloatMath ;
3639import android .util .Slog ;
3740import android .util .Spline ;
3841import android .util .TimeUtils ;
@@ -78,6 +81,13 @@ final class DisplayPowerController {
7881 // screen state returns. Playing the animation can also be somewhat slow.
7982 private static final boolean USE_ELECTRON_BEAM_ON_ANIMATION = false ;
8083
84+ // If true, enables the use of the screen auto-brightness adjustment setting.
85+ private static final boolean USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT = false ;
86+
87+ // The maximum range of gamma adjustment possible using the screen
88+ // auto-brightness adjustment setting.
89+ private static final float SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT_MAX_GAMMA = 3.0f ;
90+
8191 private static final int ELECTRON_BEAM_ON_ANIMATION_DURATION_MILLIS = 300 ;
8292 private static final int ELECTRON_BEAM_OFF_ANIMATION_DURATION_MILLIS = 600 ;
8393
@@ -150,8 +160,11 @@ final class DisplayPowerController {
150160 // The dim screen brightness.
151161 private final int mScreenBrightnessDimConfig ;
152162
153- // Auto -brightness.
163+ // True if auto -brightness should be used .
154164 private boolean mUseSoftwareAutoBrightnessConfig ;
165+
166+ // The auto-brightness spline adjustment.
167+ // The brightness values have been scaled to a range of 0..1.
155168 private Spline mScreenAutoBrightnessSpline ;
156169
157170 // Amount of time to delay auto-brightness after screen on while waiting for
@@ -266,6 +279,9 @@ final class DisplayPowerController {
266279 // Use -1 if there is no current auto-brightness value available.
267280 private int mScreenAutoBrightness = -1 ;
268281
282+ // The last screen auto-brightness gamma. (For printing in dump() only.)
283+ private float mLastScreenAutoBrightnessGamma = 1.0f ;
284+
269285 // True if the screen auto-brightness value is actually being used to
270286 // set the display brightness.
271287 private boolean mUsingScreenAutoBrightness ;
@@ -335,10 +351,10 @@ private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) {
335351 final int n = brightness .length ;
336352 float [] x = new float [n ];
337353 float [] y = new float [n ];
338- y [0 ] = brightness [0 ];
354+ y [0 ] = ( float ) brightness [0 ] / PowerManager . BRIGHTNESS_ON ;
339355 for (int i = 1 ; i < n ; i ++) {
340356 x [i ] = lux [i - 1 ];
341- y [i ] = brightness [i ];
357+ y [i ] = ( float ) brightness [i ] / PowerManager . BRIGHTNESS_ON ;
342358 }
343359
344360 Spline spline = Spline .createMonotoneCubicSpline (x , y );
@@ -470,6 +486,8 @@ private void updatePowerState() {
470486 // Update the power state request.
471487 final boolean mustNotify ;
472488 boolean mustInitialize = false ;
489+ boolean updateAutoBrightness = false ;
490+
473491 synchronized (mLock ) {
474492 mPendingUpdatePowerStateLocked = false ;
475493 if (mPendingRequestLocked == null ) {
@@ -483,6 +501,10 @@ private void updatePowerState() {
483501 mPendingRequestChangedLocked = false ;
484502 mustInitialize = true ;
485503 } else if (mPendingRequestChangedLocked ) {
504+ if (mPowerRequest .screenAutoBrightnessAdjustment
505+ != mPendingRequestLocked .screenAutoBrightnessAdjustment ) {
506+ updateAutoBrightness = true ;
507+ }
486508 mPowerRequest .copyFrom (mPendingRequestLocked );
487509 mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked ;
488510 mPendingWaitForNegativeProximityLocked = false ;
@@ -530,19 +552,21 @@ private void updatePowerState() {
530552 // Turn on the light sensor if needed.
531553 if (mLightSensor != null ) {
532554 setLightSensorEnabled (mPowerRequest .useAutoBrightness
533- && wantScreenOn (mPowerRequest .screenState ));
555+ && wantScreenOn (mPowerRequest .screenState ), updateAutoBrightness );
534556 }
535557
536558 // Set the screen brightness.
537559 if (mPowerRequest .screenState == DisplayPowerRequest .SCREEN_STATE_DIM ) {
538560 // Screen is dimmed. Overrides everything else.
539- animateScreenBrightness (mScreenBrightnessDimConfig , BRIGHTNESS_RAMP_RATE_FAST );
561+ animateScreenBrightness (
562+ clampScreenBrightness (mScreenBrightnessDimConfig ),
563+ BRIGHTNESS_RAMP_RATE_FAST );
540564 mUsingScreenAutoBrightness = false ;
541565 } else if (mPowerRequest .screenState == DisplayPowerRequest .SCREEN_STATE_BRIGHT ) {
542566 if (mScreenAutoBrightness >= 0 && mLightSensorEnabled ) {
543567 // Use current auto-brightness value.
544568 animateScreenBrightness (
545- Math . max (mScreenAutoBrightness , mScreenBrightnessDimConfig ),
569+ clampScreenBrightness (mScreenAutoBrightness ),
546570 mUsingScreenAutoBrightness ? BRIGHTNESS_RAMP_RATE_SLOW :
547571 BRIGHTNESS_RAMP_RATE_FAST );
548572 mUsingScreenAutoBrightness = true ;
@@ -552,7 +576,7 @@ private void updatePowerState() {
552576 // provide a nominal default value for the case where auto-brightness
553577 // is not ready yet.
554578 animateScreenBrightness (
555- Math . max (mPowerRequest .screenBrightness , mScreenBrightnessDimConfig ),
579+ clampScreenBrightness (mPowerRequest .screenBrightness ),
556580 BRIGHTNESS_RAMP_RATE_FAST );
557581 mUsingScreenAutoBrightness = false ;
558582 }
@@ -630,6 +654,10 @@ private void setScreenOn(boolean on) {
630654 }
631655 }
632656
657+ private int clampScreenBrightness (int value ) {
658+ return Math .min (Math .max (Math .max (value , mScreenBrightnessDimConfig ), 0 ), 255 );
659+ }
660+
633661 private void animateScreenBrightness (int target , int rate ) {
634662 if (mScreenBrightnessRampAnimator .animateTo (target , rate )) {
635663 mNotifier .onScreenBrightness (target );
@@ -691,9 +719,10 @@ private void debounceProximitySensor() {
691719 }
692720 }
693721
694- private void setLightSensorEnabled (boolean enable ) {
722+ private void setLightSensorEnabled (boolean enable , boolean updateAutoBrightness ) {
695723 if (enable ) {
696724 if (!mLightSensorEnabled ) {
725+ updateAutoBrightness = true ;
697726 mLightSensorEnabled = true ;
698727 mLightSensorEnableTime = SystemClock .uptimeMillis ();
699728 mSensorManager .registerListener (mLightSensorListener , mLightSensor ,
@@ -703,11 +732,13 @@ private void setLightSensorEnabled(boolean enable) {
703732 if (mLightSensorEnabled ) {
704733 mLightSensorEnabled = false ;
705734 mLightMeasurementValid = false ;
706- updateAutoBrightness (false );
707735 mHandler .removeMessages (MSG_LIGHT_SENSOR_DEBOUNCED );
708736 mSensorManager .unregisterListener (mLightSensorListener );
709737 }
710738 }
739+ if (updateAutoBrightness ) {
740+ updateAutoBrightness (false );
741+ }
711742 }
712743
713744 private void handleLightSensorEvent (long time , float lux ) {
@@ -818,26 +849,46 @@ private void updateAutoBrightness(boolean sendUpdate) {
818849 return ;
819850 }
820851
821- final int newScreenAutoBrightness = interpolateBrightness (
822- mScreenAutoBrightnessSpline , mLightMeasurement );
852+ float value = mScreenAutoBrightnessSpline .interpolate (mLightMeasurement );
853+ float gamma = 1.0f ;
854+
855+ if (USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT
856+ && mPowerRequest .screenAutoBrightnessAdjustment != 0.0f ) {
857+ final float adjGamma = FloatMath .pow (SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT_MAX_GAMMA ,
858+ Math .min (1.0f , Math .max (-1.0f ,
859+ -mPowerRequest .screenAutoBrightnessAdjustment )));
860+ gamma *= adjGamma ;
861+ if (DEBUG ) {
862+ Slog .d (TAG , "updateAutoBrightness: adjGamma=" + adjGamma );
863+ }
864+ }
865+
866+ if (gamma != 1.0f ) {
867+ final float in = value ;
868+ value = FloatMath .pow (value , gamma );
869+ if (DEBUG ) {
870+ Slog .d (TAG , "updateAutoBrightness: gamma=" + gamma
871+ + ", in=" + in + ", out=" + value );
872+ }
873+ }
874+
875+ int newScreenAutoBrightness = clampScreenBrightness (
876+ (int )Math .round (value * PowerManager .BRIGHTNESS_ON ));
823877 if (mScreenAutoBrightness != newScreenAutoBrightness ) {
824878 if (DEBUG ) {
825879 Slog .d (TAG , "updateAutoBrightness: mScreenAutoBrightness="
826- + mScreenAutoBrightness + "newScreenAutoBrightness="
880+ + mScreenAutoBrightness + ", newScreenAutoBrightness="
827881 + newScreenAutoBrightness );
828882 }
829883
830884 mScreenAutoBrightness = newScreenAutoBrightness ;
885+ mLastScreenAutoBrightnessGamma = gamma ;
831886 if (sendUpdate ) {
832887 sendUpdatePowerState ();
833888 }
834889 }
835890 }
836891
837- private static int interpolateBrightness (Spline spline , float lux ) {
838- return Math .min (255 , Math .max (0 , (int )Math .round (spline .interpolate (lux ))));
839- }
840-
841892 private void sendOnStateChanged () {
842893 mCallbackHandler .post (mOnStateChangedRunnable );
843894 }
@@ -943,6 +994,7 @@ private void dumpLocal(PrintWriter pw) {
943994 + TimeUtils .formatUptime (mPendingLightSensorDebounceTime ));
944995 pw .println (" mScreenAutoBrightness=" + mScreenAutoBrightness );
945996 pw .println (" mUsingScreenAutoBrightness=" + mUsingScreenAutoBrightness );
997+ pw .println (" mLastScreenAutoBrightnessGamma=" + mLastScreenAutoBrightnessGamma );
946998
947999 if (mElectronBeamOnAnimator != null ) {
9481000 pw .println (" mElectronBeamOnAnimator.isStarted()=" +
0 commit comments