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

Commit 831de7fe authored by David van Tonder's avatar David van Tonder Committed by Gerrit Code Review
Browse files

Merge "Improve auto-brightness handling" into cm-10.1

parents 913557b2 eb2eaa5a
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -1739,6 +1739,17 @@ public final class Settings {
         */
        public static final String AUTO_BRIGHTNESS_BACKLIGHT = "auto_brightness_backlight";

        /**
         * Correction factor for auto-brightness adjustment light sensor
         * debounce times.
         * Smaller factors will make the adjustment more responsive, but might
         * cause flicker and/or cause higher CPU usage.
         * Valid range is 0.2 ... 3
         *
         * @hide
         */
        public static final String AUTO_BRIGHTNESS_RESPONSIVENESS = "auto_brightness_responsiveness";

        /**
         * Whether to enable the electron beam animation when turning screen on
         *
+45 −10
Original line number Diff line number Diff line
@@ -157,6 +157,7 @@ final class DisplayPowerController {
    // are used to debounce the light sensor when adapting to brighter or darker environments.
    // This parameter controls how quickly brightness changes occur in response to
    // an observed change in light level that exceeds the hysteresis threshold.
    private static final long BRIGHTENING_LIGHT_FAST_DEBOUNCE = 1000;
    private static final long BRIGHTENING_LIGHT_DEBOUNCE = 4000;
    private static final long DARKENING_LIGHT_DEBOUNCE = 8000;

@@ -166,6 +167,11 @@ final class DisplayPowerController {
    private static final float BRIGHTENING_LIGHT_HYSTERESIS = 0.10f;
    private static final float DARKENING_LIGHT_HYSTERESIS = 0.20f;

    // Threshold (in lux) to select between normal and fast debounce time.
    // If the difference between short and long time average is larger than
    // this value, fast debounce is used.
    private static final float BRIGHTENING_FAST_THRESHOLD = 300f;

    private final Object mLock = new Object();

    // Notifier for sending asynchronous notifications.
@@ -949,8 +955,11 @@ final class DisplayPowerController {
                updateAutoBrightness = true;
                mLightSensorEnabled = true;
                mLightSensorEnableTime = SystemClock.uptimeMillis();

                int updateRateMillis = (int)
                        (mPowerRequest.responsitivityFactor * LIGHT_SENSOR_RATE_MILLIS);
                mSensorManager.registerListener(mLightSensorListener, mLightSensor,
                        LIGHT_SENSOR_RATE_MILLIS * 1000, mHandler);
                        updateRateMillis * 1000, mHandler);
            }
        } else {
            if (mLightSensorEnabled) {
@@ -981,10 +990,20 @@ final class DisplayPowerController {
            mRecentLongTermAverageLux = lux;
        } else {
            final long timeDelta = time - mLastObservedLuxTime;
            final long shortTermConstant = (long)
                    (mPowerRequest.responsitivityFactor * SHORT_TERM_AVERAGE_LIGHT_TIME_CONSTANT);
            final long longTermConstant = (long)
                    (mPowerRequest.responsitivityFactor * LONG_TERM_AVERAGE_LIGHT_TIME_CONSTANT);
            mRecentShortTermAverageLux += (lux - mRecentShortTermAverageLux)
                    * timeDelta / (SHORT_TERM_AVERAGE_LIGHT_TIME_CONSTANT + timeDelta);
                    * timeDelta / (shortTermConstant + timeDelta);
            mRecentLongTermAverageLux += (lux - mRecentLongTermAverageLux)
                    * timeDelta / (LONG_TERM_AVERAGE_LIGHT_TIME_CONSTANT + timeDelta);
                    * timeDelta / (longTermConstant + timeDelta);
        }

        if (DEBUG) {
            Slog.d(TAG, "applyLightSensorMeasurement: lux=" + lux
                    + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux
                    +", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux);
        }

        // Remember this sample value.
@@ -1015,19 +1034,30 @@ final class DisplayPowerController {
        float brighteningLuxThreshold = mAmbientLux * (1.0f + BRIGHTENING_LIGHT_HYSTERESIS);
        if (mRecentShortTermAverageLux > brighteningLuxThreshold
                && mRecentLongTermAverageLux > brighteningLuxThreshold) {
            long debounceDelay;

            if (mRecentShortTermAverageLux - mRecentLongTermAverageLux > BRIGHTENING_FAST_THRESHOLD) {
                debounceDelay = BRIGHTENING_LIGHT_FAST_DEBOUNCE;
            } else {
                debounceDelay = BRIGHTENING_LIGHT_DEBOUNCE;
            }
            debounceDelay = (long) (mPowerRequest.responsitivityFactor * debounceDelay);

            if (mDebounceLuxDirection <= 0) {
                mDebounceLuxDirection = 1;
                mDebounceLuxTime = time;
                if (DEBUG) {
                    Slog.d(TAG, "updateAmbientLux: Possibly brightened, waiting for "
                            + BRIGHTENING_LIGHT_DEBOUNCE + " ms: "
                            + debounceDelay + " ms: "
                            + "brighteningLuxThreshold=" + brighteningLuxThreshold
                            + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux
                            + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux
                            + ", mAmbientLux=" + mAmbientLux);
                }
            }
            long debounceTime = mDebounceLuxTime + BRIGHTENING_LIGHT_DEBOUNCE;

            long debounceTime = mDebounceLuxTime + debounceDelay;

            if (time >= debounceTime) {
                mAmbientLux = mRecentShortTermAverageLux;
                if (DEBUG) {
@@ -1048,19 +1078,21 @@ final class DisplayPowerController {
        float darkeningLuxThreshold = mAmbientLux * (1.0f - DARKENING_LIGHT_HYSTERESIS);
        if (mRecentShortTermAverageLux < darkeningLuxThreshold
                && mRecentLongTermAverageLux < darkeningLuxThreshold) {
            long debounceDelay = (long)
                    (mPowerRequest.responsitivityFactor * DARKENING_LIGHT_DEBOUNCE);
            if (mDebounceLuxDirection >= 0) {
                mDebounceLuxDirection = -1;
                mDebounceLuxTime = time;
                if (DEBUG) {
                    Slog.d(TAG, "updateAmbientLux: Possibly darkened, waiting for "
                            + DARKENING_LIGHT_DEBOUNCE + " ms: "
                            + debounceDelay + " ms: "
                            + "darkeningLuxThreshold=" + darkeningLuxThreshold
                            + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux
                            + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux
                            + ", mAmbientLux=" + mAmbientLux);
                }
            }
            long debounceTime = mDebounceLuxTime + DARKENING_LIGHT_DEBOUNCE;
            long debounceTime = mDebounceLuxTime + debounceDelay;
            if (time >= debounceTime) {
                // Be conservative about reducing the brightness, only reduce it a little bit
                // at a time to avoid having to bump it up again soon.
@@ -1100,15 +1132,18 @@ final class DisplayPowerController {
        // possibly exceed one of the hysteresis thresholds.
        if (mLastObservedLux > brighteningLuxThreshold
                || mLastObservedLux < darkeningLuxThreshold) {
            mHandler.sendEmptyMessageAtTime(MSG_LIGHT_SENSOR_DEBOUNCED,
                    time + SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS);
            long synthesizedDelay = (long)
                    (mPowerRequest.responsitivityFactor * SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS);
            mHandler.sendEmptyMessageAtTime(MSG_LIGHT_SENSOR_DEBOUNCED, time + synthesizedDelay);
        }
    }

    private void debounceLightSensor() {
        if (mLightSensorEnabled) {
            long time = SystemClock.uptimeMillis();
            if (time >= mLastObservedLuxTime + SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS) {
            long synthesizedDelay = (long)
                    (mPowerRequest.responsitivityFactor * SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS);
            if (time >= mLastObservedLuxTime + synthesizedDelay) {
                if (DEBUG) {
                    Slog.d(TAG, "debounceLightSensor: Synthesizing light sensor measurement "
                            + "after " + (time - mLastObservedLuxTime) + " ms.");
+11 −2
Original line number Diff line number Diff line
@@ -62,6 +62,11 @@ final class DisplayPowerRequest {
    // visible to the user.
    public boolean blockScreenOn;

    // Multiplication factor for delays used in auto-brightness computations
    // Lower values mean faster reaction to changing light conditions, but
    // potentially higher CPU usage and flicker.
    public float responsitivityFactor;

    public DisplayPowerRequest() {
        screenState = SCREEN_STATE_BRIGHT;
        useProximitySensor = false;
@@ -69,6 +74,7 @@ final class DisplayPowerRequest {
        screenAutoBrightnessAdjustment = 0.0f;
        useAutoBrightness = false;
        blockScreenOn = false;
        responsitivityFactor = 1.0f;
    }

    public DisplayPowerRequest(DisplayPowerRequest other) {
@@ -82,6 +88,7 @@ final class DisplayPowerRequest {
        screenAutoBrightnessAdjustment = other.screenAutoBrightnessAdjustment;
        useAutoBrightness = other.useAutoBrightness;
        blockScreenOn = other.blockScreenOn;
        responsitivityFactor = other.responsitivityFactor;
    }

    @Override
@@ -97,7 +104,8 @@ final class DisplayPowerRequest {
                && screenBrightness == other.screenBrightness
                && screenAutoBrightnessAdjustment == other.screenAutoBrightnessAdjustment
                && useAutoBrightness == other.useAutoBrightness
                && blockScreenOn == other.blockScreenOn;
                && blockScreenOn == other.blockScreenOn
                && Math.abs(responsitivityFactor - other.responsitivityFactor) < 1E-6;
    }

    @Override
@@ -112,6 +120,7 @@ final class DisplayPowerRequest {
                + ", screenBrightness=" + screenBrightness
                + ", screenAutoBrightnessAdjustment=" + screenAutoBrightnessAdjustment
                + ", useAutoBrightness=" + useAutoBrightness
                + ", blockScreenOn=" + blockScreenOn;
                + ", blockScreenOn=" + blockScreenOn
                + ", responsitivityFactor=" + responsitivityFactor;
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -335,6 +335,9 @@ public final class PowerManagerService extends IPowerManager.Stub
    // Use 0 if there is no adjustment.
    private float mScreenAutoBrightnessAdjustmentSetting;

    // The screen auto-brightness responsitivity factor, from 0.2 to 3.
    private float mAutoBrightnessResponsitivityFactor;

    // The screen brightness mode.
    // One of the Settings.System.SCREEN_BRIGHTNESS_MODE_* constants.
    private int mScreenBrightnessModeSetting;
@@ -509,6 +512,9 @@ public final class PowerManagerService extends IPowerManager.Stub
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.SCREEN_BRIGHTNESS_MODE),
                    false, mSettingsObserver, UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.AUTO_BRIGHTNESS_RESPONSIVENESS),
                    false, mSettingsObserver, UserHandle.USER_ALL);

            // Go.
            readConfigurationLocked();
@@ -580,6 +586,12 @@ public final class PowerManagerService extends IPowerManager.Stub
            mAutoBrightnessHandler.onAutoBrightnessChanged(mScreenBrightnessModeSetting);
        }

        final float newAutoBrightnessResponsitivityFactor = Settings.System.getFloatForUser(resolver,
                Settings.System.AUTO_BRIGHTNESS_RESPONSIVENESS, 1.0f,
                UserHandle.USER_CURRENT);
        mAutoBrightnessResponsitivityFactor =
                Math.min(Math.max(newAutoBrightnessResponsitivityFactor, 0.2f), 3.0f);

        mDirty |= DIRTY_SETTINGS;
    }

@@ -1712,6 +1724,8 @@ public final class PowerManagerService extends IPowerManager.Stub

            mDisplayPowerRequest.blockScreenOn = mScreenOnBlocker.isHeld();

            mDisplayPowerRequest.responsitivityFactor = mAutoBrightnessResponsitivityFactor;

            mDisplayReady = mDisplayPowerController.requestPowerState(mDisplayPowerRequest,
                    mRequestWaitForNegativeProximity);
            mRequestWaitForNegativeProximity = false;