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

Commit 188b35e1 authored by Rupesh Bansal's avatar Rupesh Bansal Committed by Android (Google) Code Review
Browse files

Merge changes from topic "do-block-brightness" into main

* changes:
  Maintaining lux buffers since elapsed time and not uptime
  Doze brightness for non-offloaded watchfaces
parents b9148d32 8281f604
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
+60 −43
Original line number Diff line number Diff line
@@ -56,10 +56,12 @@ import com.android.server.EventLogTags;
import com.android.server.display.brightness.BrightnessEvent;
import com.android.server.display.brightness.clamper.BrightnessClamperController;
import com.android.server.display.config.HysteresisLevels;
import com.android.server.display.feature.DisplayManagerFlags;

import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.TimeUnit;

/**
 * Manages the associated display brightness when in auto-brightness mode. This is also
@@ -206,7 +208,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;
@@ -277,6 +279,8 @@ public class AutomaticBrightnessController {
    private Clock mClock;
    private final Injector mInjector;

    private final DisplayManagerFlags mDisplayManagerFlags;

    AutomaticBrightnessController(Callbacks callbacks, Looper looper,
            SensorManager sensorManager, Sensor lightSensor,
            SparseArray<BrightnessMappingStrategy> brightnessMappingStrategyMap,
@@ -291,7 +295,8 @@ public class AutomaticBrightnessController {
            BrightnessRangeController brightnessModeController,
            BrightnessThrottler brightnessThrottler, int ambientLightHorizonShort,
            int ambientLightHorizonLong, float userLux, float userNits,
            BrightnessClamperController brightnessClamperController) {
            BrightnessClamperController brightnessClamperController,
            DisplayManagerFlags displayManagerFlags) {
        this(new Injector(), callbacks, looper, sensorManager, lightSensor,
                brightnessMappingStrategyMap, lightSensorWarmUpTime, brightnessMin, brightnessMax,
                dozeScaleFactor, lightSensorRate, initialLightSensorRate,
@@ -301,7 +306,7 @@ public class AutomaticBrightnessController {
                screenBrightnessThresholds, ambientBrightnessThresholdsIdle,
                screenBrightnessThresholdsIdle, context, brightnessModeController,
                brightnessThrottler, ambientLightHorizonShort, ambientLightHorizonLong, userLux,
                userNits, brightnessClamperController
                userNits, brightnessClamperController, displayManagerFlags
        );
    }

@@ -320,9 +325,10 @@ public class AutomaticBrightnessController {
            BrightnessRangeController brightnessRangeController,
            BrightnessThrottler brightnessThrottler, int ambientLightHorizonShort,
            int ambientLightHorizonLong, float userLux, float userNits,
            BrightnessClamperController brightnessClamperController) {
            BrightnessClamperController brightnessClamperController,
            DisplayManagerFlags displayManagerFlags) {
        mInjector = injector;
        mClock = injector.createClock();
        mClock = injector.createClock(displayManagerFlags.offloadControlsDozeAutoBrightness());
        mContext = context;
        mCallbacks = callbacks;
        mSensorManager = sensorManager;
@@ -367,6 +373,7 @@ public class AutomaticBrightnessController {
        mBrightnessClamperController = brightnessClamperController;
        mBrightnessThrottler = brightnessThrottler;
        mBrightnessMappingStrategyMap = brightnessMappingStrategyMap;
        mDisplayManagerFlags = displayManagerFlags;

        // Use the given short-term model
        if (userNits != BrightnessMappingStrategy.INVALID_NITS) {
@@ -429,34 +436,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;
    }
@@ -747,7 +726,6 @@ public class AutomaticBrightnessController {
        mRecentLightSamples++;
        mAmbientLightRingBuffer.prune(time - mAmbientLightHorizonLong);
        mAmbientLightRingBuffer.push(time, lux);

        // Remember this sample value.
        mLastObservedLux = lux;
        mLastObservedLuxTime = time;
@@ -891,7 +869,7 @@ public class AutomaticBrightnessController {
    }

    private void updateAmbientLux() {
        long time = mClock.uptimeMillis();
        long time = mClock.getSensorEventScaleTime();
        mAmbientLightRingBuffer.prune(time - mAmbientLightHorizonLong);
        updateAmbientLux(time);
    }
@@ -968,7 +946,16 @@ public class AutomaticBrightnessController {
            Slog.d(TAG, "updateAmbientLux: Scheduling ambient lux update for " +
                    nextTransitionTime + TimeUtils.formatUptime(nextTransitionTime));
        }
        mHandler.sendEmptyMessageAtTime(MSG_UPDATE_AMBIENT_LUX, nextTransitionTime);

        // The nextTransitionTime is computed as elapsedTime(Which also accounts for the time when
        // android was sleeping) as the main reference. However, handlers work on the uptime(Not
        // accounting for the time when android was sleeping)
        mHandler.sendEmptyMessageAtTime(MSG_UPDATE_AMBIENT_LUX,
                convertToUptime(nextTransitionTime));
    }

    private long convertToUptime(long time) {
        return time - mClock.getSensorEventScaleTime() + mClock.uptimeMillis();
    }

    private void updateAutoBrightness(boolean sendUpdate, boolean isManuallySet) {
@@ -1185,15 +1172,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 +1193,11 @@ public class AutomaticBrightnessController {
            resetShortTermModel();
            mCurrentBrightnessMapper = mBrightnessMappingStrategyMap.get(mode);
        }
        if (sendUpdate) {
            update();
        } else {
            updateAutoBrightness(/* sendUpdate= */ false, /* isManuallySet= */ false);
        }
    }

    float getUserLux() {
@@ -1391,7 +1381,9 @@ public class AutomaticBrightnessController {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (mLightSensorEnabled) {
                final long time = mClock.uptimeMillis();
                // The time received from the sensor is in nano seconds, hence changing it to ms
                final long time = (mDisplayManagerFlags.offloadControlsDozeAutoBrightness())
                        ? TimeUnit.NANOSECONDS.toMillis(event.timestamp) : mClock.uptimeMillis();
                final float lux = event.values[0];
                handleLightSensorEvent(time, lux);
            }
@@ -1424,6 +1416,12 @@ public class AutomaticBrightnessController {
         * Returns current time in milliseconds since boot, not counting time spent in deep sleep.
         */
        long uptimeMillis();

        /**
         * Gets the time on either the elapsedTime or the uptime scale, depending on how we
         * processing the events from the sensor
         */
        long getSensorEventScaleTime();
    }

    /**
@@ -1571,7 +1569,8 @@ public class AutomaticBrightnessController {
            StringBuilder buf = new StringBuilder();
            buf.append('[');
            for (int i = 0; i < mCount; i++) {
                final long next = i + 1 < mCount ? getTime(i + 1) : mClock.uptimeMillis();
                final long next = i + 1 < mCount ? getTime(i + 1)
                        : mClock.getSensorEventScaleTime();
                if (i != 0) {
                    buf.append(", ");
                }
@@ -1596,13 +1595,31 @@ public class AutomaticBrightnessController {
        }
    }

    private static class RealClock implements Clock {
        private final boolean mOffloadControlsDozeBrightness;

        RealClock(boolean offloadControlsDozeBrightness) {
            mOffloadControlsDozeBrightness = offloadControlsDozeBrightness;
        }

        @Override
        public long uptimeMillis() {
            return SystemClock.uptimeMillis();
        }

        public long getSensorEventScaleTime() {
            return (mOffloadControlsDozeBrightness)
                    ? SystemClock.elapsedRealtime() : uptimeMillis();
        }
    }

    public static class Injector {
        public Handler getBackgroundThreadHandler() {
            return BackgroundThread.getHandler();
        }

        Clock createClock() {
            return SystemClock::uptimeMillis;
        Clock createClock(boolean offloadControlsDozeBrightness) {
            return new RealClock(offloadControlsDozeBrightness);
        }
    }
}
+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) {
+32 −42
Original line number Diff line number Diff line
@@ -1115,7 +1115,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                    screenBrightnessThresholdsIdle, mContext, mBrightnessRangeController,
                    mBrightnessThrottler, mDisplayDeviceConfig.getAmbientHorizonShort(),
                    mDisplayDeviceConfig.getAmbientHorizonLong(), userLux, userNits,
                    mBrightnessClamperController);
                    mBrightnessClamperController, mFlags);
            mDisplayBrightnessController.setUpAutoBrightness(
                    mAutomaticBrightnessController, mSensorManager, mDisplayDeviceConfig, mHandler,
                    defaultModeBrightnessMapper, mIsEnabled, mLeadDisplayId);
@@ -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);
@@ -3169,7 +3158,8 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                BrightnessRangeController brightnessModeController,
                BrightnessThrottler brightnessThrottler, int ambientLightHorizonShort,
                int ambientLightHorizonLong, float userLux, float userNits,
                BrightnessClamperController brightnessClamperController) {
                BrightnessClamperController brightnessClamperController,
                DisplayManagerFlags displayManagerFlags) {

            return new AutomaticBrightnessController(callbacks, looper, sensorManager, lightSensor,
                    brightnessMappingStrategyMap, lightSensorWarmUpTime, brightnessMin,
@@ -3180,7 +3170,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                    screenBrightnessThresholds, ambientBrightnessThresholdsIdle,
                    screenBrightnessThresholdsIdle, context, brightnessModeController,
                    brightnessThrottler, ambientLightHorizonShort, ambientLightHorizonLong, userLux,
                    userNits, brightnessClamperController);
                    userNits, brightnessClamperController, displayManagerFlags);
        }

        BrightnessMappingStrategy getDefaultModeBrightnessMapper(Context context,
+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