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

Commit 5ad8b353 authored by Piotr Wilczyński's avatar Piotr Wilczyński
Browse files

Doze auto-brightness mode

We switch to doze auto-brightness mode if we're in a doze screen state and we're not in idle mode.

Change the naming from setting to preset following the suggestion on the design doc - to not get confused with the other brightness setting (the brightness value).

Bug: 306407598
Test: adb shell dumpsys display
Test: atest com.android.server.display
Change-Id: I7b1e49ac5867512510bd75fc09dcde400b728108
parent 9f09a316
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.display;

import static com.android.server.display.config.DisplayBrightnessMappingConfig.autoBrightnessModeToString;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -72,12 +74,14 @@ public class AutomaticBrightnessController {
    @IntDef(prefix = { "AUTO_BRIGHTNESS_MODE_" }, value = {
            AUTO_BRIGHTNESS_MODE_DEFAULT,
            AUTO_BRIGHTNESS_MODE_IDLE,
            AUTO_BRIGHTNESS_MODE_DOZE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface AutomaticBrightnessMode{}

    public static final int AUTO_BRIGHTNESS_MODE_DEFAULT = 0;
    public static final int AUTO_BRIGHTNESS_MODE_IDLE = 1;
    public static final int AUTO_BRIGHTNESS_MODE_DOZE = 2;

    // How long the current sensor reading is assumed to be valid beyond the current time.
    // This provides a bit of prediction, as well as ensures that the weight for the last sample is
@@ -616,12 +620,13 @@ public class AutomaticBrightnessController {
        pw.println("  mPendingForegroundAppPackageName=" + mPendingForegroundAppPackageName);
        pw.println("  mForegroundAppCategory=" + mForegroundAppCategory);
        pw.println("  mPendingForegroundAppCategory=" + mPendingForegroundAppCategory);
        pw.println("  Current mode=" + mCurrentBrightnessMapper.getMode());
        pw.println("  Current mode="
                + autoBrightnessModeToString(mCurrentBrightnessMapper.getMode()));

        pw.println();
        for (int i = 0; i < mBrightnessMappingStrategyMap.size(); i++) {
            pw.println("  Mapper for mode " + modeToString(mBrightnessMappingStrategyMap.keyAt(i))
                    + "=");
            pw.println("  Mapper for mode "
                    + autoBrightnessModeToString(mBrightnessMappingStrategyMap.keyAt(i)) + "=");
            mBrightnessMappingStrategyMap.valueAt(i).dump(pw,
                    mBrightnessRangeController.getNormalBrightnessMax());
        }
@@ -1224,14 +1229,6 @@ public class AutomaticBrightnessController {
        }
    }

    private String modeToString(@AutomaticBrightnessMode int mode) {
        return switch (mode) {
            case AUTO_BRIGHTNESS_MODE_DEFAULT -> "default";
            case AUTO_BRIGHTNESS_MODE_IDLE -> "idle";
            default -> Integer.toString(mode);
        };
    }

    private class ShortTermModel {
        // When the short term model is invalidated, we don't necessarily reset it (i.e. clear the
        // user's adjustment) immediately, but wait for a drastic enough change in the ambient
+7 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.display;
import static android.text.TextUtils.formatSimple;

import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DEFAULT;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DOZE;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_IDLE;

import android.annotation.Nullable;
@@ -98,8 +99,8 @@ public abstract class BrightnessMappingStrategy {
        switch (mode) {
            case AUTO_BRIGHTNESS_MODE_DEFAULT -> {
                brightnessLevelsNits = displayDeviceConfig.getAutoBrightnessBrighteningLevelsNits();
                luxLevels = displayDeviceConfig.getAutoBrightnessBrighteningLevelsLux();
                brightnessLevels = displayDeviceConfig.getAutoBrightnessBrighteningLevels();
                luxLevels = displayDeviceConfig.getAutoBrightnessBrighteningLevelsLux(mode);
                brightnessLevels = displayDeviceConfig.getAutoBrightnessBrighteningLevels(mode);
            }
            case AUTO_BRIGHTNESS_MODE_IDLE -> {
                brightnessLevelsNits = getFloatArray(resources.obtainTypedArray(
@@ -107,6 +108,10 @@ public abstract class BrightnessMappingStrategy {
                luxLevels = getLuxLevels(resources.getIntArray(
                        com.android.internal.R.array.config_autoBrightnessLevelsIdle));
            }
            case AUTO_BRIGHTNESS_MODE_DOZE -> {
                luxLevels = displayDeviceConfig.getAutoBrightnessBrighteningLevelsLux(mode);
                brightnessLevels = displayDeviceConfig.getAutoBrightnessBrighteningLevels(mode);
            }
        }

        // Display independent, mode independent values
+22 −14
Original line number Diff line number Diff line
@@ -1591,25 +1591,29 @@ public class DisplayDeviceConfig {
    }

    /**
     * @return The default auto-brightness brightening ambient lux levels
     * @param mode The auto-brightness mode
     * @return The default auto-brightness brightening ambient lux levels for the specified mode
     * and the normal brightness preset
     */
    public float[] getAutoBrightnessBrighteningLevelsLux() {
    public float[] getAutoBrightnessBrighteningLevelsLux(
            @AutomaticBrightnessController.AutomaticBrightnessMode int mode) {
        if (mDisplayBrightnessMapping == null) {
            return null;
        }
        return mDisplayBrightnessMapping.getLuxArray();
        return mDisplayBrightnessMapping.getLuxArray(mode);
    }

    /**
     * @param mode The auto-brightness mode
     * @param setting The brightness setting
     * @return Auto brightness brightening ambient lux levels for the specified mode and setting
     * @param preset The brightness preset. Presets are used on devices that allow users to choose
     *               from a set of predefined options in display auto-brightness settings.
     * @return Auto brightness brightening ambient lux levels for the specified mode and preset
     */
    public float[] getAutoBrightnessBrighteningLevelsLux(String mode, String setting) {
    public float[] getAutoBrightnessBrighteningLevelsLux(String mode, String preset) {
        if (mDisplayBrightnessMapping == null) {
            return null;
        }
        return mDisplayBrightnessMapping.getLuxArray(mode, setting);
        return mDisplayBrightnessMapping.getLuxArray(mode, preset);
    }

    /**
@@ -1623,25 +1627,29 @@ public class DisplayDeviceConfig {
    }

    /**
     * @return The default auto-brightness brightening levels
     * @param mode The auto-brightness mode
     * @return The default auto-brightness brightening levels for the specified mode and the normal
     * brightness preset
     */
    public float[] getAutoBrightnessBrighteningLevels() {
    public float[] getAutoBrightnessBrighteningLevels(
            @AutomaticBrightnessController.AutomaticBrightnessMode int mode) {
        if (mDisplayBrightnessMapping == null) {
            return null;
        }
        return mDisplayBrightnessMapping.getBrightnessArray();
        return mDisplayBrightnessMapping.getBrightnessArray(mode);
    }

    /**
     * @param mode The auto-brightness mode
     * @param setting The brightness setting
     * @return Auto brightness brightening backlight levels for the specified mode and setting
     * @param preset The brightness preset. Presets are used on devices that allow users to choose
     *               from a set of predefined options in display auto-brightness settings.
     * @return Auto brightness brightening backlight levels for the specified mode and preset
     */
    public float[] getAutoBrightnessBrighteningLevels(String mode, String setting) {
    public float[] getAutoBrightnessBrighteningLevels(String mode, String preset) {
        if (mDisplayBrightnessMapping == null) {
            return null;
        }
        return mDisplayBrightnessMapping.getBrightnessArray(mode, setting);
        return mDisplayBrightnessMapping.getBrightnessArray(mode, preset);
    }

    /**
+15 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server.display;

import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DEFAULT;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DOZE;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_IDLE;

import android.animation.Animator;
@@ -1006,6 +1007,13 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
            }
        }

        BrightnessMappingStrategy dozeModeBrightnessMapper =
                BrightnessMappingStrategy.create(resources, mDisplayDeviceConfig,
                        AUTO_BRIGHTNESS_MODE_DOZE, mDisplayWhiteBalanceController);
        if (mFlags.areAutoBrightnessModesEnabled() && dozeModeBrightnessMapper != null) {
            brightnessMappers.put(AUTO_BRIGHTNESS_MODE_DOZE, dozeModeBrightnessMapper);
        }

        float userLux = BrightnessMappingStrategy.INVALID_LUX;
        float userNits = BrightnessMappingStrategy.INVALID_NITS;
        if (mAutomaticBrightnessController != null) {
@@ -1349,6 +1357,13 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        animateScreenStateChange(state, mDisplayStateController.shouldPerformScreenOffTransition());
        state = mPowerState.getScreenState();

        // Switch to doze auto-brightness mode if needed
        if (mFlags.areAutoBrightnessModesEnabled() && mAutomaticBrightnessController != null
                && !mAutomaticBrightnessController.isInIdleMode()) {
            setAutomaticScreenBrightnessMode(Display.isDozeState(state)
                    ? AUTO_BRIGHTNESS_MODE_DOZE : AUTO_BRIGHTNESS_MODE_DEFAULT);
        }

        final boolean userSetBrightnessChanged = mDisplayBrightnessController
                .updateUserSetScreenBrightness();

+65 −23
Original line number Diff line number Diff line
@@ -16,11 +16,16 @@

package com.android.server.display.config;

import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DEFAULT;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DOZE;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_IDLE;

import android.content.Context;
import android.os.PowerManager;
import android.util.Spline;

import com.android.internal.display.BrightnessSynchronizer;
import com.android.server.display.AutomaticBrightnessController;
import com.android.server.display.DisplayDeviceConfig;
import com.android.server.display.feature.DisplayManagerFlags;

@@ -33,7 +38,9 @@ import java.util.Map;
 */
public class DisplayBrightnessMappingConfig {

    private static final String DEFAULT_BRIGHTNESS_MAPPING_KEY = "default_normal";
    private static final String DEFAULT_BRIGHTNESS_PRESET_NAME = "normal";
    private static final String DEFAULT_BRIGHTNESS_MAPPING_KEY =
            AutoBrightnessModeName._default.getRawName() + "_" + DEFAULT_BRIGHTNESS_PRESET_NAME;

    /**
     * Array of desired screen brightness in nits corresponding to the lux values
@@ -45,19 +52,22 @@ public class DisplayBrightnessMappingConfig {

    /**
     * Map of arrays of desired screen brightness corresponding to the lux values
     * in mBrightnessLevelsLuxMap, indexed by the auto-brightness mode and the brightness setting.
     * in mBrightnessLevelsLuxMap, indexed by the auto-brightness mode and the brightness preset.
     * The brightness values must be non-negative and non-decreasing. They must be between
     * {@link PowerManager.BRIGHTNESS_MIN} and {@link PowerManager.BRIGHTNESS_MAX}.
     *
     * The keys are a concatenation of the auto-brightness mode and the brightness setting
     * separated by an underscore, e.g. default_normal, default_dim, default_bright, doze_normal,
     * doze_dim, doze_bright.
     * The keys are a concatenation of the auto-brightness mode and the brightness preset separated
     * by an underscore, e.g. default_normal, default_dim, default_bright, doze_normal, doze_dim,
     * doze_bright.
     *
     * The presets are used on devices that allow users to choose from a set of predefined options
     * in display auto-brightness settings.
     */
    private final Map<String, float[]> mBrightnessLevelsMap = new HashMap<>();

    /**
     * Map of arrays of light sensor lux values to define our levels for auto-brightness support,
     * indexed by the auto-brightness mode and the brightness setting.
     * indexed by the auto-brightness mode and the brightness preset.
     *
     * The first lux value in every array is always 0.
     *
@@ -69,9 +79,12 @@ public class DisplayBrightnessMappingConfig {
     * Spline interpolation is used to determine the auto-brightness values for lux levels between
     * these control points.
     *
     * The keys are a concatenation of the auto-brightness mode and the brightness setting
     * separated by an underscore, e.g. default_normal, default_dim, default_bright, doze_normal,
     * doze_dim, doze_bright.
     * The keys are a concatenation of the auto-brightness mode and the brightness preset separated
     * by an underscore, e.g. default_normal, default_dim, default_bright, doze_normal, doze_dim,
     * doze_bright.
     *
     * The presets are used on devices that allow users to choose from a set of predefined options
     * in display auto-brightness settings.
     */
    private final Map<String, float[]> mBrightnessLevelsLuxMap = new HashMap<>();

@@ -138,19 +151,23 @@ public class DisplayBrightnessMappingConfig {
    }

    /**
     * @return The default auto-brightness brightening ambient lux levels
     * @param mode The auto-brightness mode
     * @return The default auto-brightness brightening ambient lux levels for the specified mode
     * and the normal brightness preset
     */
    public float[] getLuxArray() {
        return mBrightnessLevelsLuxMap.get(DEFAULT_BRIGHTNESS_MAPPING_KEY);
    public float[] getLuxArray(@AutomaticBrightnessController.AutomaticBrightnessMode int mode) {
        return mBrightnessLevelsLuxMap.get(
                autoBrightnessModeToString(mode) + "_" + DEFAULT_BRIGHTNESS_PRESET_NAME);
    }

    /**
     * @param mode The auto-brightness mode
     * @param setting The brightness setting
     * @return Auto brightness brightening ambient lux levels for the specified mode and setting
     * @param preset The brightness preset. Presets are used on devices that allow users to choose
     *               from a set of predefined options in display auto-brightness settings.
     * @return Auto brightness brightening ambient lux levels for the specified mode and preset
     */
    public float[] getLuxArray(String mode, String setting) {
        return mBrightnessLevelsLuxMap.get(mode + "_" + setting);
    public float[] getLuxArray(String mode, String preset) {
        return mBrightnessLevelsLuxMap.get(mode + "_" + preset);
    }

    /**
@@ -161,19 +178,24 @@ public class DisplayBrightnessMappingConfig {
    }

    /**
     * @return The default auto-brightness brightening levels
     * @param mode The auto-brightness mode
     * @return The default auto-brightness brightening levels for the specified mode and the normal
     * brightness preset
     */
    public float[] getBrightnessArray() {
        return mBrightnessLevelsMap.get(DEFAULT_BRIGHTNESS_MAPPING_KEY);
    public float[] getBrightnessArray(
            @AutomaticBrightnessController.AutomaticBrightnessMode int mode) {
        return mBrightnessLevelsMap.get(
                autoBrightnessModeToString(mode) + "_" + DEFAULT_BRIGHTNESS_PRESET_NAME);
    }

    /**
     * @param mode The auto-brightness mode
     * @param setting The brightness setting
     * @return Auto brightness brightening ambient lux levels for the specified mode and setting
     * @param preset The brightness preset. Presets are used on devices that allow users to choose
     *               from a set of predefined options in display auto-brightness settings.
     * @return Auto brightness brightening ambient lux levels for the specified mode and preset
     */
    public float[] getBrightnessArray(String mode, String setting) {
        return mBrightnessLevelsMap.get(mode + "_" + setting);
    public float[] getBrightnessArray(String mode, String preset) {
        return mBrightnessLevelsMap.get(mode + "_" + preset);
    }

    @Override
@@ -205,6 +227,26 @@ public class DisplayBrightnessMappingConfig {
                + ", mBrightnessLevelsMap= " + brightnessLevelsMapString;
    }

    /**
     * @param mode The auto-brightness mode
     * @return The string representing the mode
     */
    public static String autoBrightnessModeToString(
            @AutomaticBrightnessController.AutomaticBrightnessMode int mode) {
        switch (mode) {
            case AUTO_BRIGHTNESS_MODE_DEFAULT -> {
                return AutoBrightnessModeName._default.getRawName();
            }
            case AUTO_BRIGHTNESS_MODE_IDLE -> {
                return AutoBrightnessModeName.idle.getRawName();
            }
            case AUTO_BRIGHTNESS_MODE_DOZE -> {
                return AutoBrightnessModeName.doze.getRawName();
            }
            default -> throw new IllegalArgumentException("Unknown auto-brightness mode: " + mode);
        }
    }

    private float[] brightnessArrayIntToFloat(int[] brightnessInt,
            Spline backlightToBrightnessSpline) {
        float[] brightnessFloat = new float[brightnessInt.length];
Loading