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

Commit 37027b38 authored by Santos Cordon's avatar Santos Cordon
Browse files

Removing AutomaticBrightnessStrategy refs in DPC2

This change starts the ABS/ABC move out of DPC2 by moving the
construction of ABS to BrightnessStrategySelector. Subsequent changes
will move ABC to ABS and convert the ABS calls in DPC2 into return
values of DisplayBrightnessController.updateBrightness.

This is a first step in that direction.

The call to ABS.shouldUseAutoBrightness() was changed to a value on
DisplayBrightnessState (return val of DBC.updateBrighntess()) in
order to prove out the plan of converting method calls to
return values instead. This required some additional scaffolding in
DBC which will be removed once ABS works like a standard strategy
and can fill DisplayBrightnessState during its updateBrightness phase.

Bug: 253226419
Test: atest com.android.server.display
Test: Manual, verify ScreenOffStrategy and Auto-brightness work.
Change-Id: If3251cdc76ef47effe3b8bb762d10b7311385790
parent 5b35c961
Loading
Loading
Loading
Loading
+58 −21
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.display;

import android.text.TextUtils;

import com.android.server.display.brightness.BrightnessReason;

import java.util.Objects;
@@ -29,12 +31,14 @@ public final class DisplayBrightnessState {
    private final float mSdrBrightness;
    private final BrightnessReason mBrightnessReason;
    private final String mDisplayBrightnessStrategyName;
    private final boolean mShouldUseAutoBrightness;

    private DisplayBrightnessState(Builder builder) {
        this.mBrightness = builder.getBrightness();
        this.mSdrBrightness = builder.getSdrBrightness();
        this.mBrightnessReason = builder.getBrightnessReason();
        this.mDisplayBrightnessStrategyName = builder.getDisplayBrightnessStrategyName();
        mBrightness = builder.getBrightness();
        mSdrBrightness = builder.getSdrBrightness();
        mBrightnessReason = builder.getBrightnessReason();
        mDisplayBrightnessStrategyName = builder.getDisplayBrightnessStrategyName();
        mShouldUseAutoBrightness = builder.getShouldUseAutoBrightness();
    }

    /**
@@ -66,6 +70,13 @@ public final class DisplayBrightnessState {
        return mDisplayBrightnessStrategyName;
    }

    /**
     * @return {@code true} if the device is set up to run auto-brightness.
     */
    public boolean getShouldUseAutoBrightness() {
        return mShouldUseAutoBrightness;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("DisplayBrightnessState:");
@@ -75,6 +86,8 @@ public final class DisplayBrightnessState {
        stringBuilder.append(getSdrBrightness());
        stringBuilder.append("\n    brightnessReason:");
        stringBuilder.append(getBrightnessReason());
        stringBuilder.append("\n    shouldUseAutoBrightness:");
        stringBuilder.append(getShouldUseAutoBrightness());
        return stringBuilder.toString();
    }

@@ -91,28 +104,20 @@ public final class DisplayBrightnessState {
            return false;
        }

        DisplayBrightnessState
                displayBrightnessState = (DisplayBrightnessState) other;
        DisplayBrightnessState otherState = (DisplayBrightnessState) other;

        if (mBrightness != displayBrightnessState.getBrightness()) {
            return false;
        }
        if (mSdrBrightness != displayBrightnessState.getSdrBrightness()) {
            return false;
        }
        if (!mBrightnessReason.equals(displayBrightnessState.getBrightnessReason())) {
            return false;
        }
        if (!mDisplayBrightnessStrategyName.equals(
                displayBrightnessState.getDisplayBrightnessStrategyName())) {
            return false;
        }
        return true;
        return mBrightness == otherState.getBrightness()
                && mSdrBrightness == otherState.getSdrBrightness()
                && mBrightnessReason.equals(otherState.getBrightnessReason())
                && TextUtils.equals(mDisplayBrightnessStrategyName,
                        otherState.getDisplayBrightnessStrategyName())
                && mShouldUseAutoBrightness == otherState.getShouldUseAutoBrightness();
    }

    @Override
    public int hashCode() {
        return Objects.hash(mBrightness, mSdrBrightness, mBrightnessReason);
        return Objects.hash(
                mBrightness, mSdrBrightness, mBrightnessReason, mShouldUseAutoBrightness);
    }

    /**
@@ -123,6 +128,23 @@ public final class DisplayBrightnessState {
        private float mSdrBrightness;
        private BrightnessReason mBrightnessReason = new BrightnessReason();
        private String mDisplayBrightnessStrategyName;
        private boolean mShouldUseAutoBrightness;

        /**
         * Create a builder starting with the values from the specified {@link
         * DisplayBrightnessState}.
         *
         * @param state The state from which to initialize.
         */
        public static Builder from(DisplayBrightnessState state) {
            Builder builder = new Builder();
            builder.setBrightness(state.getBrightness());
            builder.setSdrBrightness(state.getSdrBrightness());
            builder.setBrightnessReason(state.getBrightnessReason());
            builder.setDisplayBrightnessStrategyName(state.getDisplayBrightnessStrategyName());
            builder.setShouldUseAutoBrightness(state.getShouldUseAutoBrightness());
            return builder;
        }

        /**
         * Gets the brightness
@@ -199,6 +221,21 @@ public final class DisplayBrightnessState {
            return this;
        }

        /**
         * See {@link DisplayBrightnessState#getShouldUseAutoBrightness}.
         */
        public Builder setShouldUseAutoBrightness(boolean shouldUseAutoBrightness) {
            this.mShouldUseAutoBrightness = shouldUseAutoBrightness;
            return this;
        }

        /**
         * See {@link DisplayBrightnessState#getShouldUseAutoBrightness}.
         */
        public boolean getShouldUseAutoBrightness() {
            return mShouldUseAutoBrightness;
        }

        /**
         * This is used to construct an immutable DisplayBrightnessState object from its builder
         */
+23 −15
Original line number Diff line number Diff line
@@ -492,7 +492,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                mWakelockController, mDisplayDeviceConfig, mHandler.getLooper(),
                () -> updatePowerState(), mDisplayId, mSensorManager);
        mDisplayStateController = new DisplayStateController(mDisplayPowerProximityStateController);
        mAutomaticBrightnessStrategy = new AutomaticBrightnessStrategy(context, mDisplayId);
        mTag = "DisplayPowerController2[" + mDisplayId + "]";
        mThermalBrightnessThrottlingDataId =
                logicalDisplay.getDisplayInfoLocked().thermalBrightnessThrottlingDataId;
@@ -556,6 +555,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                        new HandlerExecutor(mHandler));
        // Seed the cached brightness
        saveBrightnessInfo(getScreenBrightnessSetting());
        mAutomaticBrightnessStrategy =
                mDisplayBrightnessController.getAutomaticBrightnessStrategy();

        DisplayWhiteBalanceSettings displayWhiteBalanceSettings = null;
        DisplayWhiteBalanceController displayWhiteBalanceController = null;
@@ -599,7 +600,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

        setUpAutoBrightness(resources, handler);

        mColorFadeEnabled = !ActivityManager.isLowRamDeviceStatic();
        mColorFadeEnabled = mInjector.isColorFadeEnabled();
        mColorFadeFadesConfig = resources.getBoolean(
                R.bool.config_animateScreenLights);

@@ -1257,14 +1258,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        int state = mDisplayStateController
                .updateDisplayState(mPowerRequest, mIsEnabled, mIsInTransition);

        if (mScreenOffBrightnessSensorController != null) {
            mScreenOffBrightnessSensorController
                    .setLightSensorEnabled(mAutomaticBrightnessStrategy.shouldUseAutoBrightness()
                    && mIsEnabled && (state == Display.STATE_OFF || (state == Display.STATE_DOZE
                    && !mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig()))
                    && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
        }

        // Initialize things the first time the power state is changed.
        if (mustInitialize) {
            initialize(readyToUpdateDisplayState() ? state : Display.STATE_UNKNOWN);
@@ -1290,6 +1283,17 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
            slowChange = mBrightnessToFollowSlowChange;
        }

        // Set up the ScreenOff controller used when coming out of SCREEN_OFF and the ALS sensor
        // doesn't yet have a valid lux value to use with auto-brightness.
        if (mScreenOffBrightnessSensorController != null) {
            mScreenOffBrightnessSensorController
                    .setLightSensorEnabled(displayBrightnessState.getShouldUseAutoBrightness()
                    && mIsEnabled && (state == Display.STATE_OFF
                    || (state == Display.STATE_DOZE
                    && !mDisplayBrightnessController.isAllowAutoBrightnessWhileDozingConfig()))
                    && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
        }

        // Take note if the short term model was already active before applying the current
        // request changes.
        final boolean wasShortTermModelActive =
@@ -1563,7 +1567,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

            notifyBrightnessTrackerChanged(brightnessState, userInitiatedChange,
                    wasShortTermModelActive, mAutomaticBrightnessStrategy.isAutoBrightnessEnabled(),
                    brightnessIsTemporary);
                    brightnessIsTemporary, displayBrightnessState.getShouldUseAutoBrightness());

            // We save the brightness info *after* the brightness setting has been changed and
            // adjustments made so that the brightness info reflects the latest value.
@@ -1607,8 +1611,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        mTempBrightnessEvent.setWasShortTermModelActive(wasShortTermModelActive);
        mTempBrightnessEvent.setDisplayBrightnessStrategyName(displayBrightnessState
                .getDisplayBrightnessStrategyName());
        mTempBrightnessEvent.setAutomaticBrightnessEnabled(mAutomaticBrightnessStrategy
                .shouldUseAutoBrightness());
        mTempBrightnessEvent.setAutomaticBrightnessEnabled(
                displayBrightnessState.getShouldUseAutoBrightness());
        // 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 =
@@ -2219,7 +2223,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

    private void notifyBrightnessTrackerChanged(float brightness, boolean userInitiated,
            boolean wasShortTermModelActive, boolean autobrightnessEnabled,
            boolean brightnessIsTemporary) {
            boolean brightnessIsTemporary, boolean shouldUseAutoBrightness) {

        final float brightnessInNits =
                mDisplayBrightnessController.convertToAdjustedNits(brightness);
@@ -2234,7 +2238,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                || mAutomaticBrightnessController.isInIdleMode()
                || !autobrightnessEnabled
                || mBrightnessTracker == null
                || !mAutomaticBrightnessStrategy.shouldUseAutoBrightness()
                || !shouldUseAutoBrightness
                || brightnessInNits < 0.0f) {
            return;
        }
@@ -2916,6 +2920,10 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
                    displayUniqueId, brightnessMin, brightnessMax, hbmData, hdrBrightnessCfg,
                    hbmChangeCallback, hbmMetadata, context);
        }

        boolean isColorFadeEnabled() {
            return !ActivityManager.isLowRamDeviceStatic();
        }
    }

    static class CachedBrightnessInfo {
+31 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.server.display.AutomaticBrightnessController;
import com.android.server.display.BrightnessSetting;
import com.android.server.display.DisplayBrightnessState;
import com.android.server.display.brightness.strategy.AutomaticBrightnessStrategy;
import com.android.server.display.brightness.strategy.DisplayBrightnessStrategy;

import java.io.PrintWriter;
@@ -134,11 +135,21 @@ public final class DisplayBrightnessController {
    public DisplayBrightnessState updateBrightness(
            DisplayManagerInternal.DisplayPowerRequest displayPowerRequest,
            int targetDisplayState) {

        DisplayBrightnessState state;
        synchronized (mLock) {
            mDisplayBrightnessStrategy = mDisplayBrightnessStrategySelector.selectStrategy(
                    displayPowerRequest, targetDisplayState);
            return mDisplayBrightnessStrategy.updateBrightness(displayPowerRequest);
            state = mDisplayBrightnessStrategy.updateBrightness(displayPowerRequest);
        }

        // This is a temporary measure until AutomaticBrightnessStrategy works as a traditional
        // strategy.
        // TODO: Remove when AutomaticBrightnessStrategy is populating the values directly.
        if (state != null) {
            state = addAutomaticBrightnessState(state);
        }
        return state;
    }

    /**
@@ -321,6 +332,13 @@ public final class DisplayBrightnessController {
        loadNitBasedBrightnessSetting();
    }

    /**
     * TODO(b/253226419): Remove once auto-brightness is a fully-functioning strategy.
     */
    public AutomaticBrightnessStrategy getAutomaticBrightnessStrategy() {
        return mDisplayBrightnessStrategySelector.getAutomaticBrightnessStrategy();
    }

    /**
     * Convert a brightness float scale value to a nit value. Adjustments, such as RBC, are not
     * applied. This is used when storing the brightness in nits for the default display and when
@@ -425,6 +443,18 @@ public final class DisplayBrightnessController {
        }
    }

    /**
     * TODO(b/253226419): Remove once auto-brightness is a fully-functioning strategy.
     */
    private DisplayBrightnessState addAutomaticBrightnessState(DisplayBrightnessState state) {
        AutomaticBrightnessStrategy autoStrat = getAutomaticBrightnessStrategy();

        DisplayBrightnessState.Builder builder = DisplayBrightnessState.Builder.from(state);
        builder.setShouldUseAutoBrightness(
                autoStrat != null && autoStrat.shouldUseAutoBrightness());
        return builder.build();
    }

    @GuardedBy("mLock")
    private void setTemporaryBrightnessLocked(float temporaryBrightness) {
        mDisplayBrightnessStrategySelector.getTemporaryDisplayBrightnessStrategy()
+12 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.view.Display;

import com.android.internal.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.display.brightness.strategy.AutomaticBrightnessStrategy;
import com.android.server.display.brightness.strategy.BoostBrightnessStrategy;
import com.android.server.display.brightness.strategy.DisplayBrightnessStrategy;
import com.android.server.display.brightness.strategy.DozeBrightnessStrategy;
@@ -60,6 +61,8 @@ public class DisplayBrightnessStrategySelector {
    private final FollowerBrightnessStrategy mFollowerBrightnessStrategy;
    // The brightness strategy used to manage the brightness state when the request is invalid.
    private final InvalidBrightnessStrategy mInvalidBrightnessStrategy;
    // Controls brightness when automatic (adaptive) brightness is running.
    private final AutomaticBrightnessStrategy mAutomaticBrightnessStrategy;

    // We take note of the old brightness strategy so that we can know when the strategy changes.
    private String mOldBrightnessStrategyName;
@@ -81,6 +84,7 @@ public class DisplayBrightnessStrategySelector {
        mBoostBrightnessStrategy = injector.getBoostBrightnessStrategy();
        mFollowerBrightnessStrategy = injector.getFollowerBrightnessStrategy(displayId);
        mInvalidBrightnessStrategy = injector.getInvalidBrightnessStrategy();
        mAutomaticBrightnessStrategy = injector.getAutomaticBrightnessStrategy(context, displayId);
        mAllowAutoBrightnessWhileDozingConfig = context.getResources().getBoolean(
                R.bool.config_allowAutoBrightnessWhileDozing);
        mOldBrightnessStrategyName = mInvalidBrightnessStrategy.getName();
@@ -130,6 +134,10 @@ public class DisplayBrightnessStrategySelector {
        return mFollowerBrightnessStrategy;
    }

    public AutomaticBrightnessStrategy getAutomaticBrightnessStrategy() {
        return mAutomaticBrightnessStrategy;
    }

    /**
     * Returns a boolean flag indicating if the light sensor is to be used to decide the screen
     * brightness when dozing
@@ -198,5 +206,9 @@ public class DisplayBrightnessStrategySelector {
        InvalidBrightnessStrategy getInvalidBrightnessStrategy() {
            return new InvalidBrightnessStrategy();
        }

        AutomaticBrightnessStrategy getAutomaticBrightnessStrategy(Context context, int displayId) {
            return new AutomaticBrightnessStrategy(context, displayId);
        }
    }
}
+14 −2
Original line number Diff line number Diff line
@@ -18,17 +18,23 @@ package com.android.server.display.brightness;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.hardware.display.DisplayManagerInternal;
import android.view.Display;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.R;
import com.android.internal.util.test.FakeSettingsProvider;
import com.android.internal.util.test.FakeSettingsProviderRule;
import com.android.server.display.brightness.strategy.BoostBrightnessStrategy;
import com.android.server.display.brightness.strategy.DozeBrightnessStrategy;
import com.android.server.display.brightness.strategy.FollowerBrightnessStrategy;
@@ -38,6 +44,7 @@ import com.android.server.display.brightness.strategy.ScreenOffBrightnessStrateg
import com.android.server.display.brightness.strategy.TemporaryBrightnessStrategy;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -64,15 +71,20 @@ public final class DisplayBrightnessStrategySelectorTest {
    @Mock
    private FollowerBrightnessStrategy mFollowerBrightnessStrategy;
    @Mock
    private Context mContext;
    @Mock
    private Resources mResources;

    private DisplayBrightnessStrategySelector mDisplayBrightnessStrategySelector;
    private Context mContext;

    @Rule
    public FakeSettingsProviderRule mSettingsProviderRule = FakeSettingsProvider.rule();

    @Before
    public void before() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(new ContextWrapper(ApplicationProvider.getApplicationContext()));
        ContentResolver contentResolver = mSettingsProviderRule.mockContentResolver(mContext);
        when(mContext.getContentResolver()).thenReturn(contentResolver);
        when(mContext.getResources()).thenReturn(mResources);
        when(mInvalidBrightnessStrategy.getName()).thenReturn("InvalidBrightnessStrategy");
        DisplayBrightnessStrategySelector.Injector injector =
Loading