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

Commit 33a0b252 authored by Rupesh Bansal's avatar Rupesh Bansal Committed by Automerger Merge Worker
Browse files

Merge "Introducing AutomaticBrightnessStrategy" into udc-dev am: 1b35bc3e

parents 03805702 1b35bc3e
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -1634,7 +1634,6 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            mAppliedAutoBrightness = false;
            brightnessAdjustmentFlags = 0;
        }

        // Use default brightness when dozing unless overridden.
        if ((Float.isNaN(brightnessState))
                && Display.isDozeState(state)) {
+67 −156
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ import com.android.internal.display.BrightnessSynchronizer;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.util.FrameworkStatsLog;
import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.RingBuffer;
import com.android.server.LocalServices;
import com.android.server.am.BatteryStatsService;
@@ -72,6 +73,7 @@ import com.android.server.display.brightness.BrightnessEvent;
import com.android.server.display.brightness.BrightnessReason;
import com.android.server.display.brightness.BrightnessUtils;
import com.android.server.display.brightness.DisplayBrightnessController;
import com.android.server.display.brightness.strategy.AutomaticBrightnessStrategy;
import com.android.server.display.color.ColorDisplayService.ColorDisplayServiceInternal;
import com.android.server.display.color.ColorDisplayService.ReduceBrightColorsListener;
import com.android.server.display.layout.Layout;
@@ -209,9 +211,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    // True if auto-brightness should be used.
    private boolean mUseSoftwareAutoBrightnessConfig;

    // True if the brightness config has changed and the short-term model needs to be reset
    private boolean mShouldResetShortTermModel;

    // Whether or not the color fade on screen on / off is enabled.
    private final boolean mColorFadeEnabled;

@@ -296,12 +295,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    // If the last recorded screen state was dozing or not.
    private boolean mDozing;

    // Remembers whether certain kinds of brightness adjustments
    // were recently applied so that we can decide how to transition.
    private boolean mAppliedAutoBrightness;
    private boolean mAppliedDimming;
    private boolean mAppliedLowPower;
    private boolean mAppliedTemporaryAutoBrightnessAdjustment;
    private boolean mAppliedThrottling;

    // Reason for which the brightness was last changed. See {@link BrightnessReason} for more
@@ -359,6 +354,11 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    // Tracks and manages the display state of the associated display.
    private final DisplayStateController mDisplayStateController;


    // Responsible for evaluating and tracking the automatic brightness relevant states.
    // Todo: This is a temporary workaround. Ideally DPC2 should never talk to the strategies
    private final AutomaticBrightnessStrategy mAutomaticBrightnessStrategy;

    // A record of state for skipping brightness ramps.
    private int mSkipRampState = RAMP_STATE_SKIP_NONE;

@@ -385,24 +385,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    @Nullable
    private BrightnessMappingStrategy mIdleModeBrightnessMapper;

    // The current brightness configuration.
    @Nullable
    private BrightnessConfiguration mBrightnessConfiguration;

    // The last auto brightness adjustment that was set by the user and not temporary. Set to
    // Float.NaN when an auto-brightness adjustment hasn't been recorded yet.
    private float mAutoBrightnessAdjustment;

    // The pending auto brightness adjustment that will take effect on the next power state update.
    private float mPendingAutoBrightnessAdjustment;

    // The temporary auto brightness adjustment. Typically set when a user is interacting with the
    // adjustment slider but hasn't settled on a choice yet. Set to
    // PowerManager.BRIGHTNESS_INVALID_FLOAT when there's no temporary adjustment set.
    private float mTemporaryAutoBrightnessAdjustment;

    private boolean mUseAutoBrightness;

    private boolean mIsRbcActive;

    // Animators.
@@ -454,6 +436,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                () -> updatePowerState(), mDisplayId, mSensorManager);
        mHighBrightnessModeMetadata = hbmMetadata;
        mDisplayStateController = new DisplayStateController(mDisplayPowerProximityStateController);
        mAutomaticBrightnessStrategy = new AutomaticBrightnessStrategy(context, mDisplayId);
        mTag = "DisplayPowerController2[" + mDisplayId + "]";
        mBrightnessThrottlingDataId = logicalDisplay.getBrightnessThrottlingDataIdLocked();

@@ -555,9 +538,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

        mBrightnessBucketsInDozeConfig = resources.getBoolean(
                R.bool.config_displayBrightnessBucketsInDoze);
        mAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
        mTemporaryAutoBrightnessAdjustment = PowerManager.BRIGHTNESS_INVALID_FLOAT;
        mPendingAutoBrightnessAdjustment = PowerManager.BRIGHTNESS_INVALID_FLOAT;

        mBootCompleted = bootCompleted;
    }
@@ -1038,6 +1018,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
            mDisplayBrightnessController.setAutomaticBrightnessController(
                    mAutomaticBrightnessController);

            mAutomaticBrightnessStrategy
                    .setAutomaticBrightnessController(mAutomaticBrightnessController);
            mBrightnessEventRingBuffer =
                    new RingBuffer<>(BrightnessEvent.class, RINGBUFFER_MAX);

@@ -1168,7 +1150,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        final boolean mustNotify;
        final int previousPolicy;
        boolean mustInitialize = false;
        int brightnessAdjustmentFlags = 0;
        mBrightnessReasonTemp.set(null);
        mTempBrightnessEvent.reset();
        SparseArray<DisplayPowerControllerInterface> displayBrightnessFollowers;
@@ -1208,7 +1189,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                .updateDisplayState(mPowerRequest, mIsEnabled, mIsInTransition);

        if (mScreenOffBrightnessSensorController != null) {
            mScreenOffBrightnessSensorController.setLightSensorEnabled(mUseAutoBrightness
            mScreenOffBrightnessSensorController
                    .setLightSensorEnabled(mAutomaticBrightnessStrategy.shouldUseAutoBrightness()
                    && mIsEnabled && (state == Display.STATE_OFF || (state == Display.STATE_DOZE
                    && !mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig()))
                    && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
@@ -1222,7 +1204,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        // Animate the screen state change unless already animating.
        // The transition may be deferred, so after this point we will use the
        // actual state instead of the desired one.
        final int oldState = mPowerState.getScreenState();
        animateScreenStateChange(state, mDisplayStateController.shouldPerformScreenOffTransition());
        state = mPowerState.getScreenState();

@@ -1231,112 +1212,59 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        float brightnessState = displayBrightnessState.getBrightness();
        float rawBrightnessState = displayBrightnessState.getBrightness();
        mBrightnessReasonTemp.set(displayBrightnessState.getBrightnessReason());

        final boolean autoBrightnessEnabledInDoze =
                mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig()
                        && Display.isDozeState(state);
        final boolean autoBrightnessEnabled = mUseAutoBrightness
                && (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
                && (Float.isNaN(brightnessState)
                        || mBrightnessReasonTemp.getReason() == BrightnessReason.REASON_TEMPORARY
                        || mBrightnessReasonTemp.getReason() == BrightnessReason.REASON_BOOST)
                && mAutomaticBrightnessController != null
                && mBrightnessReasonTemp.getReason() != BrightnessReason.REASON_FOLLOWER;
        final boolean autoBrightnessDisabledDueToDisplayOff = mUseAutoBrightness
                && !(state == Display.STATE_ON || autoBrightnessEnabledInDoze);
        final int autoBrightnessState = autoBrightnessEnabled
                ? AutomaticBrightnessController.AUTO_BRIGHTNESS_ENABLED
                : autoBrightnessDisabledDueToDisplayOff
                        ? AutomaticBrightnessController.AUTO_BRIGHTNESS_OFF_DUE_TO_DISPLAY_STATE
                        : AutomaticBrightnessController.AUTO_BRIGHTNESS_DISABLED;

        final boolean userSetBrightnessChanged = mDisplayBrightnessController
                .updateUserSetScreenBrightness();

        final boolean autoBrightnessAdjustmentChanged = updateAutoBrightnessAdjustment();

        // Use the autobrightness adjustment override if set.
        final float autoBrightnessAdjustment;
        if (!Float.isNaN(mTemporaryAutoBrightnessAdjustment)) {
            autoBrightnessAdjustment = mTemporaryAutoBrightnessAdjustment;
            brightnessAdjustmentFlags = BrightnessReason.ADJUSTMENT_AUTO_TEMP;
            mAppliedTemporaryAutoBrightnessAdjustment = true;
        } else {
            autoBrightnessAdjustment = mAutoBrightnessAdjustment;
            brightnessAdjustmentFlags = BrightnessReason.ADJUSTMENT_AUTO;
            mAppliedTemporaryAutoBrightnessAdjustment = false;
        }
        // Take note if the short term model was already active before applying the current
        // request changes.
        final boolean wasShortTermModelActive =
                mAutomaticBrightnessStrategy.isShortTermModelActive();
        mAutomaticBrightnessStrategy.setAutoBrightnessState(state,
                mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig(),
                brightnessState, mBrightnessReasonTemp.getReason(), mPowerRequest.policy,
                mDisplayBrightnessController.getLastUserSetScreenBrightness(),
                userSetBrightnessChanged);

        // If the brightness is already set then it's been overridden by something other than the
        // user, or is a temporary adjustment.
        boolean userInitiatedChange = (Float.isNaN(brightnessState))
                && (autoBrightnessAdjustmentChanged || userSetBrightnessChanged);
        boolean wasShortTermModelActive = false;
        // Configure auto-brightness.
        if (mAutomaticBrightnessController != null) {
            wasShortTermModelActive = mAutomaticBrightnessController.hasUserDataPoints();
            mAutomaticBrightnessController.configure(autoBrightnessState,
                    mBrightnessConfiguration,
                    mDisplayBrightnessController.getLastUserSetScreenBrightness(),
                    userSetBrightnessChanged, autoBrightnessAdjustment,
                    autoBrightnessAdjustmentChanged, mPowerRequest.policy,
                    mShouldResetShortTermModel);
            mShouldResetShortTermModel = false;
        }
        mHbmController.setAutoBrightnessEnabled(mUseAutoBrightness
                && (mAutomaticBrightnessStrategy.getAutoBrightnessAdjustmentChanged()
                || userSetBrightnessChanged);

        mHbmController.setAutoBrightnessEnabled(mAutomaticBrightnessStrategy
                .shouldUseAutoBrightness()
                ? AutomaticBrightnessController.AUTO_BRIGHTNESS_ENABLED
                : AutomaticBrightnessController.AUTO_BRIGHTNESS_DISABLED);

        if (mBrightnessTracker != null) {
            mBrightnessTracker.setShouldCollectColorSample(mBrightnessConfiguration != null
                    && mBrightnessConfiguration.shouldCollectColorSamples());
        }

        boolean updateScreenBrightnessSetting = false;
        float currentBrightnessSetting = mDisplayBrightnessController.getCurrentBrightness();
        // Apply auto-brightness.
        boolean slowChange = false;
        int brightnessAdjustmentFlags = 0;
        if (Float.isNaN(brightnessState)) {
            float newAutoBrightnessAdjustment = autoBrightnessAdjustment;
            if (autoBrightnessEnabled) {
                rawBrightnessState = mAutomaticBrightnessController
                        .getRawAutomaticScreenBrightness();
                brightnessState = mAutomaticBrightnessController.getAutomaticScreenBrightness(
                        mTempBrightnessEvent);
                newAutoBrightnessAdjustment =
                        mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment();
            }
            if (mAutomaticBrightnessStrategy.isAutoBrightnessEnabled()) {
                brightnessState = mAutomaticBrightnessStrategy.getAutomaticScreenBrightness();
                if (BrightnessUtils.isValidBrightnessValue(brightnessState)
                        || brightnessState == PowerManager.BRIGHTNESS_OFF_FLOAT) {
                // Use current auto-brightness value and slowly adjust to changes.
                    rawBrightnessState = mAutomaticBrightnessController
                            .getRawAutomaticScreenBrightness();
                    brightnessState = clampScreenBrightness(brightnessState);
                if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) {
                    slowChange = true; // slowly adapt to auto-brightness
                }
                    // slowly adapt to auto-brightness
                    slowChange = mAutomaticBrightnessStrategy.hasAppliedAutoBrightness()
                            && !mAutomaticBrightnessStrategy.getAutoBrightnessAdjustmentChanged();
                    brightnessAdjustmentFlags =
                            mAutomaticBrightnessStrategy.getAutoBrightnessAdjustmentReasonsFlags();
                    updateScreenBrightnessSetting = currentBrightnessSetting != brightnessState;
                mAppliedAutoBrightness = true;
                    mBrightnessReasonTemp.setReason(BrightnessReason.REASON_AUTOMATIC);
                    if (mScreenOffBrightnessSensorController != null) {
                        mScreenOffBrightnessSensorController.setLightSensorEnabled(false);
                    }
            } else {
                mAppliedAutoBrightness = false;
                }
            if (autoBrightnessAdjustment != newAutoBrightnessAdjustment) {
                // If the autobrightness controller has decided to change the adjustment value
                // used, make sure that's reflected in settings.
                putAutoBrightnessAdjustmentSetting(newAutoBrightnessAdjustment);
            } else {
                // Adjustment values resulted in no change
                brightnessAdjustmentFlags = 0;
            }
        } else {
            // Any non-auto-brightness values such as override or temporary should still be subject
            // to clamping so that they don't go beyond the current max as specified by HBM
            // Controller.
            brightnessState = clampScreenBrightness(brightnessState);
            mAppliedAutoBrightness = false;
            brightnessAdjustmentFlags = 0;
        }

        // Use default brightness when dozing unless overridden.
@@ -1349,7 +1277,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

        // The ALS is not available yet - use the screen off sensor to determine the initial
        // brightness
        if (Float.isNaN(brightnessState) && autoBrightnessEnabled
        if (Float.isNaN(brightnessState) && mAutomaticBrightnessStrategy.isAutoBrightnessEnabled()
                && mScreenOffBrightnessSensorController != null) {
            rawBrightnessState =
                    mScreenOffBrightnessSensorController.getAutomaticScreenBrightness();
@@ -1466,7 +1394,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        boolean brightnessAdjusted = false;
        final boolean brightnessIsTemporary =
                (mBrightnessReason.getReason() == BrightnessReason.REASON_TEMPORARY)
                        || mAppliedTemporaryAutoBrightnessAdjustment;
                        || mAutomaticBrightnessStrategy
                        .isTemporaryAutoBrightnessAdjustmentApplied();
        if (!mPendingScreenOff) {
            if (mSkipScreenOnBrightnessRamp) {
                if (state == Display.STATE_ON) {
@@ -1524,6 +1453,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

            final float currentBrightness = mPowerState.getScreenBrightness();
            final float currentSdrBrightness = mPowerState.getSdrScreenBrightness();

            if (BrightnessUtils.isValidBrightnessValue(animateValue)
                    && (animateValue != currentBrightness
                    || sdrAnimateValue != currentSdrBrightness)) {
@@ -1605,7 +1535,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        mTempBrightnessEvent.setWasShortTermModelActive(wasShortTermModelActive);
        mTempBrightnessEvent.setDisplayBrightnessStrategyName(displayBrightnessState
                .getDisplayBrightnessStrategyName());
        mTempBrightnessEvent.setAutomaticBrightnessEnabled(mUseAutoBrightness);
        mTempBrightnessEvent.setAutomaticBrightnessEnabled(mAutomaticBrightnessStrategy
                .shouldUseAutoBrightness());
        // Temporary is what we use during slider interactions. We avoid logging those so that
        // we don't spam logcat when the slider is being used.
        boolean tempToTempTransition =
@@ -2151,13 +2082,12 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        mDisplayBrightnessController
                .setPendingScreenBrightness(mDisplayBrightnessController
                        .getScreenBrightnessSetting());
        mPendingAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments(userSwitch);
        if (userSwitch) {
            // Don't treat user switches as user initiated change.
            mDisplayBrightnessController
                    .setAndNotifyCurrentScreenBrightness(mDisplayBrightnessController
                            .getPendingScreenBrightness());
            updateAutoBrightnessAdjustment();
            if (mAutomaticBrightnessController != null) {
                mAutomaticBrightnessController.resetShortTermModel();
            }
@@ -2171,8 +2101,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT);
        mHandler.postAtTime(() -> {
            mUseAutoBrightness = screenBrightnessModeSetting
                    == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
            mAutomaticBrightnessStrategy.setUseAutoBrightness(screenBrightnessModeSetting
                    == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
            updatePowerState();
        }, mClock.uptimeMillis());
    }
@@ -2220,33 +2150,10 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        sendUpdatePowerState();
    }

    private void putAutoBrightnessAdjustmentSetting(float adjustment) {
        if (mDisplayId == Display.DEFAULT_DISPLAY) {
            mAutoBrightnessAdjustment = adjustment;
            Settings.System.putFloatForUser(mContext.getContentResolver(),
                    Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, adjustment,
                    UserHandle.USER_CURRENT);
        }
    }

    private boolean updateAutoBrightnessAdjustment() {
        if (Float.isNaN(mPendingAutoBrightnessAdjustment)) {
            return false;
        }
        if (mAutoBrightnessAdjustment == mPendingAutoBrightnessAdjustment) {
            mPendingAutoBrightnessAdjustment = Float.NaN;
            return false;
        }
        mAutoBrightnessAdjustment = mPendingAutoBrightnessAdjustment;
        mPendingAutoBrightnessAdjustment = Float.NaN;
        mTemporaryAutoBrightnessAdjustment = Float.NaN;
        return true;
    }

    private void notifyBrightnessTrackerChanged(float brightness, boolean userInitiated,
            boolean wasShortTermModelActive) {
        final float brightnessInNits = mDisplayBrightnessController.convertToNits(brightness);
        if (mUseAutoBrightness && brightnessInNits >= 0.0f
        if (mAutomaticBrightnessStrategy.shouldUseAutoBrightness() && brightnessInNits >= 0.0f
                && mAutomaticBrightnessController != null && mBrightnessTracker != null) {
            // We only want to track changes on devices that can actually map the display backlight
            // values into a physical brightness unit since the value provided by the API is in
@@ -2329,16 +2236,10 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        pw.println();
        pw.println("Display Power Controller Thread State:");
        pw.println("  mPowerRequest=" + mPowerRequest);
        pw.println("  mAutoBrightnessAdjustment=" + mAutoBrightnessAdjustment);
        pw.println("  mBrightnessReason=" + mBrightnessReason);
        pw.println("  mTemporaryAutoBrightnessAdjustment=" + mTemporaryAutoBrightnessAdjustment);
        pw.println("  mPendingAutoBrightnessAdjustment=" + mPendingAutoBrightnessAdjustment);
        pw.println("  mAppliedAutoBrightness=" + mAppliedAutoBrightness);
        pw.println("  mAppliedDimming=" + mAppliedDimming);
        pw.println("  mAppliedLowPower=" + mAppliedLowPower);
        pw.println("  mAppliedThrottling=" + mAppliedThrottling);
        pw.println("  mAppliedTemporaryAutoBrightnessAdjustment="
                + mAppliedTemporaryAutoBrightnessAdjustment);
        pw.println("  mDozing=" + mDozing);
        pw.println("  mSkipRampState=" + skipRampStateToString(mSkipRampState));
        pw.println("  mScreenOnBlockStartRealTime=" + mScreenOnBlockStartRealTime);
@@ -2349,6 +2250,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        pw.println("  mReportedToPolicy="
                + reportedToPolicyToString(mReportedScreenStateToPolicy));
        pw.println("  mIsRbcActive=" + mIsRbcActive);
        IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "    ");
        mAutomaticBrightnessStrategy.dump(ipw);

        if (mScreenBrightnessRampAnimator != null) {
            pw.println("  mScreenBrightnessRampAnimator.isAnimating()="
@@ -2580,8 +2483,15 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                    }
                    break;
                case MSG_CONFIGURE_BRIGHTNESS:
                    mBrightnessConfiguration = (BrightnessConfiguration) msg.obj;
                    mShouldResetShortTermModel = msg.arg1 == 1;
                    BrightnessConfiguration brightnessConfiguration =
                            (BrightnessConfiguration) msg.obj;
                    mAutomaticBrightnessStrategy.setBrightnessConfiguration(brightnessConfiguration,
                            msg.arg1 == 1);
                    if (mBrightnessTracker != null) {
                        mBrightnessTracker
                                .setShouldCollectColorSample(brightnessConfiguration != null
                                        && brightnessConfiguration.shouldCollectColorSamples());
                    }
                    updatePowerState();
                    break;

@@ -2593,7 +2503,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                    break;

                case MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT:
                    mTemporaryAutoBrightnessAdjustment = Float.intBitsToFloat(msg.arg1);
                    mAutomaticBrightnessStrategy
                            .setTemporaryAutoBrightnessAdjustment(Float.intBitsToFloat(msg.arg1));
                    updatePowerState();
                    break;