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

Commit 8a6e0bfb authored by Yifei Zhang's avatar Yifei Zhang Committed by Rupesh Bansal
Browse files

Doze brightness for non-offloaded watchfaces

Enable auto-brightness in doze so that we can support top-of-minute updates.

Disable auto-brightness in doze temporarily if the offload session doesn't allow it - we don't want to use auto-brightness while the offload session is in control of the display.

Update the auto-brightness value in AutomaticBrightnessController when switching modes. This can be done in the same thread and there's no need to notify the listeners if the mode is switched from updatePowerState().

Use the display state and not the policy when setting auto-brightness state - this is reverting to the code we had before and in ag/26495348 we decided to rely on the state consistently.

Don't use the initial doze brightness based on last observed lux because of b/328714026.

Don't use the default doze brightness when auto-brightness enabled in doze - sometimes AutomaticBrightnessController might not provide a brightness value (when the lux isn't ready yet or the sensor is temporarily disabled). We don't want to cause unexpected brightness changes in those situations with the default doze brightness.

Test: adb logcat | grep -i "brightnessEvent"
Test: atest AutomaticBrightnessControllerTest
Test: atest AutomaticBrightnessStrategyTest
Test: atest BrightnessReasonTest
Test: atest DisplayPowerControllerTest
Test: atest DisplayOffloadSessionImplTest
Test: atest LocalDisplayAdapterTest
Bug: 327392714
Bug: 328714026
Change-Id: Ie3ae27e69d3edad41d27cc1f871295e8a73c09cb
parent 3d7c06c1
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -739,6 +739,9 @@ public abstract class DisplayManagerInternal {
         *                  on is done.
         */
        void onBlockingScreenOn(Runnable unblocker);

        /** Whether auto brightness update in doze is allowed */
        boolean allowAutoBrightnessInDoze();
    }

    /** A session token that associates a internal display with a {@link DisplayOffloader}. */
@@ -749,6 +752,9 @@ public abstract class DisplayManagerInternal {
        /** Whether the session is active. */
        boolean isActive();

        /** Whether auto brightness update in doze is allowed */
        boolean allowAutoBrightnessInDoze();

        /**
         * Update the brightness from the offload chip.
         * @param brightness The brightness value between {@link PowerManager.BRIGHTNESS_MIN} and
+7 −32
Original line number Diff line number Diff line
@@ -206,7 +206,7 @@ public class AutomaticBrightnessController {
    private float mScreenBrighteningThreshold;
    private float mScreenDarkeningThreshold;
    // The most recent light sample.
    private float mLastObservedLux = INVALID_LUX;
    private float mLastObservedLux;

    // The time of the most light recent sample.
    private long mLastObservedLuxTime;
@@ -429,34 +429,6 @@ public class AutomaticBrightnessController {
        return mRawScreenAutoBrightness;
    }

    /**
     * Get the automatic screen brightness based on the last observed lux reading. Used e.g. when
     * entering doze - we disable the light sensor, invalidate the lux, but we still need to set
     * the initial brightness in doze mode.
     */
    public float getAutomaticScreenBrightnessBasedOnLastUsedLux(
            BrightnessEvent brightnessEvent) {
        float lastUsedLux = mAmbientLux;
        if (lastUsedLux == INVALID_LUX) {
            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
        }

        float brightness = mCurrentBrightnessMapper.getBrightness(lastUsedLux,
                mForegroundAppPackageName, mForegroundAppCategory);
        if (shouldApplyDozeScaleFactor()) {
            brightness *= mDozeScaleFactor;
        }

        if (brightnessEvent != null) {
            brightnessEvent.setLux(lastUsedLux);
            brightnessEvent.setRecommendedBrightness(brightness);
            brightnessEvent.setFlags(brightnessEvent.getFlags()
                    | (shouldApplyDozeScaleFactor() ? BrightnessEvent.FLAG_DOZE_SCALE : 0));
            brightnessEvent.setAutoBrightnessMode(getMode());
        }
        return brightness;
    }

    public boolean hasValidAmbientLux() {
        return mAmbientLuxValid;
    }
@@ -1185,15 +1157,13 @@ public class AutomaticBrightnessController {
            }
            mPausedShortTermModel.copyFrom(tempShortTermModel);
        }

        update();
    }

    /**
     * Responsible for switching the AutomaticBrightnessMode of the associated display. Also takes
     * care of resetting the short term model wherever required
     */
    public void switchMode(@AutomaticBrightnessMode int mode) {
    public void switchMode(@AutomaticBrightnessMode int mode, boolean sendUpdate) {
        if (!mBrightnessMappingStrategyMap.contains(mode)) {
            return;
        }
@@ -1208,6 +1178,11 @@ public class AutomaticBrightnessController {
            resetShortTermModel();
            mCurrentBrightnessMapper = mBrightnessMappingStrategyMap.get(mode);
        }
        if (sendUpdate) {
            update();
        } else {
            updateAutoBrightness(/* sendUpdate= */ false, /* isManuallySet= */ false);
        }
    }

    float getUserLux() {
+8 −0
Original line number Diff line number Diff line
@@ -51,6 +51,14 @@ public class DisplayOffloadSessionImpl implements DisplayManagerInternal.Display
        return mIsActive;
    }

    @Override
    public boolean allowAutoBrightnessInDoze() {
        if (mDisplayOffloader == null) {
            return false;
        }
        return mDisplayOffloader.allowAutoBrightnessInDoze();
    }

    @Override
    public void updateBrightness(float brightness) {
        if (mIsActive) {
+28 −39
Original line number Diff line number Diff line
@@ -1185,7 +1185,8 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            @AutomaticBrightnessController.AutomaticBrightnessMode int mode) {
        boolean isIdle = mode == AUTO_BRIGHTNESS_MODE_IDLE;
        if (mAutomaticBrightnessController != null) {
            mAutomaticBrightnessController.switchMode(mode);
            // Set sendUpdate to true to make sure that updatePowerState() gets called
            mAutomaticBrightnessController.switchMode(mode, /* sendUpdate= */ true);
            setAnimatorRampSpeeds(isIdle);
        }
        Message msg = mHandler.obtainMessage();
@@ -1334,7 +1335,6 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                mDisplayStateController.shouldPerformScreenOffTransition());
        state = mPowerState.getScreenState();


        DisplayBrightnessState displayBrightnessState = mDisplayBrightnessController
                .updateBrightness(mPowerRequest, state);
        float brightnessState = displayBrightnessState.getBrightness();
@@ -1366,17 +1366,26 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
        // request changes.
        final boolean wasShortTermModelActive =
                mAutomaticBrightnessStrategy.isShortTermModelActive();
        boolean allowAutoBrightnessWhileDozing =
                mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig();
        if (mFlags.offloadControlsDozeAutoBrightness() && mFlags.isDisplayOffloadEnabled()
                && mDisplayOffloadSession != null) {
            allowAutoBrightnessWhileDozing &= mDisplayOffloadSession.allowAutoBrightnessInDoze();
        }
        if (!mFlags.isRefactorDisplayPowerControllerEnabled()) {
            // Switch to doze auto-brightness mode if needed
            if (mFlags.areAutoBrightnessModesEnabled() && mAutomaticBrightnessController != null
                    && !mAutomaticBrightnessController.isInIdleMode()) {
                // Set sendUpdate to false, we're already in updatePowerState() so there's no need
                // to trigger it again
                mAutomaticBrightnessController.switchMode(Display.isDozeState(state)
                        ? AUTO_BRIGHTNESS_MODE_DOZE : AUTO_BRIGHTNESS_MODE_DEFAULT);
                        ? AUTO_BRIGHTNESS_MODE_DOZE : AUTO_BRIGHTNESS_MODE_DEFAULT,
                        /* sendUpdate= */ false);
            }

            mAutomaticBrightnessStrategy.setAutoBrightnessState(state,
                    mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig(),
                    mBrightnessReasonTemp.getReason(), mPowerRequest.policy,
                    allowAutoBrightnessWhileDozing, mBrightnessReasonTemp.getReason(),
                    mPowerRequest.policy,
                    mDisplayBrightnessController.getLastUserSetScreenBrightness(),
                    userSetBrightnessChanged);
        }
@@ -1443,47 +1452,27 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
        }

        if (Display.isDozeState(state)) {
            // If there's an offload session, we need to set the initial doze brightness before
            // the offload session starts controlling the brightness.
            // During the transition DOZE_SUSPEND -> DOZE -> DOZE_SUSPEND, this brightness strategy
            // will be selected again, meaning that no new brightness will be sent to the hardware
            // and the display will stay at the brightness level set by the offload session.
            // TODO(b/329676661): Introduce a config property to choose between this brightness
            //  strategy and DOZE_DEFAULT
            // On some devices, when auto-brightness is disabled and the device is dozing, we use
            // the current brightness setting scaled by the doze scale factor
            if ((Float.isNaN(brightnessState)
                    || displayBrightnessState.getDisplayBrightnessStrategyName()
                    .equals(DisplayBrightnessStrategyConstants.FALLBACK_BRIGHTNESS_STRATEGY_NAME))
                    && mFlags.isDisplayOffloadEnabled()
                    && mDisplayOffloadSession != null) {
                if (mAutomaticBrightnessController != null
                        && mAutomaticBrightnessStrategy.shouldUseAutoBrightness()) {
                    // Use the auto-brightness curve and the last observed lux
                    rawBrightnessState = mAutomaticBrightnessController
                            .getAutomaticScreenBrightnessBasedOnLastUsedLux(
                                    mTempBrightnessEvent);
                } else {
                    && mDisplayOffloadSession != null
                    && (mAutomaticBrightnessController == null
                    || !mAutomaticBrightnessStrategy.shouldUseAutoBrightness())) {
                rawBrightnessState = getDozeBrightnessForOffload();
                    mTempBrightnessEvent.setFlags(mTempBrightnessEvent.getFlags()
                            | BrightnessEvent.FLAG_DOZE_SCALE);
                }

                if (BrightnessUtils.isValidBrightnessValue(rawBrightnessState)) {
                brightnessState = clampScreenBrightness(rawBrightnessState);
                    mBrightnessReasonTemp.setReason(BrightnessReason.REASON_DOZE_INITIAL);

                    if (mAutomaticBrightnessController != null
                            && mAutomaticBrightnessStrategy.shouldUseAutoBrightness()) {
                        // Keep the brightness in the setting so that we can use it after the screen
                        // turns on, until a lux sample becomes available. We don't do this when
                        // auto-brightness is disabled - in that situation we still want to use
                        // the last brightness from when the screen was on.
                        updateScreenBrightnessSetting = currentBrightnessSetting != brightnessState;
                    }
                }
                mBrightnessReasonTemp.setReason(BrightnessReason.REASON_DOZE_MANUAL);
                mTempBrightnessEvent.setFlags(
                        mTempBrightnessEvent.getFlags() | BrightnessEvent.FLAG_DOZE_SCALE);
            }

            // Use default brightness when dozing unless overridden.
            if (Float.isNaN(brightnessState)
                    || displayBrightnessState.getDisplayBrightnessStrategyName()
                    .equals(DisplayBrightnessStrategyConstants.FALLBACK_BRIGHTNESS_STRATEGY_NAME)) {
            if (Float.isNaN(brightnessState) && Display.isDozeState(state)
                    && !mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig()) {
                rawBrightnessState = mScreenBrightnessDozeConfig;
                brightnessState = clampScreenBrightness(rawBrightnessState);
                mBrightnessReasonTemp.setReason(BrightnessReason.REASON_DOZE_DEFAULT);
+4 −4
Original line number Diff line number Diff line
@@ -40,8 +40,8 @@ public final class BrightnessReason {
    public static final int REASON_SCREEN_OFF_BRIGHTNESS_SENSOR = 9;
    public static final int REASON_FOLLOWER = 10;
    public static final int REASON_OFFLOAD = 11;
    public static final int REASON_DOZE_INITIAL = 12;
    public static final int REASON_MAX = REASON_DOZE_INITIAL;
    public static final int REASON_DOZE_MANUAL = 12;
    public static final int REASON_MAX = REASON_DOZE_MANUAL;

    public static final int MODIFIER_DIMMED = 0x1;
    public static final int MODIFIER_LOW_POWER = 0x2;
@@ -208,8 +208,8 @@ public final class BrightnessReason {
                return "follower";
            case REASON_OFFLOAD:
                return "offload";
            case REASON_DOZE_INITIAL:
                return "doze_initial";
            case REASON_DOZE_MANUAL:
                return "doze_manual";
            default:
                return Integer.toString(reason);
        }
Loading