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

Commit 6deb8d37 authored by Piotr Wilczyński's avatar Piotr Wilczyński
Browse files

Brightness in nits adb command

- get/set brightness in different units
- new unit: nits

Bug: 364203835
Flag: EXEMPT adb command
Test: adb shell cmd display, DisplayManagerServiceTest
Change-Id: I31159949fee1a9efce1dccc7ba5c30db70e349ec
parent 21439d00
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -764,11 +764,20 @@ public final class DisplayManager {
    @FlaggedApi(Flags.FLAG_SET_BRIGHTNESS_BY_UNIT)
    public static final int BRIGHTNESS_UNIT_PERCENTAGE = 1;

    /**
     * Brightness value type where the value is in nits. The nits range is defined by
     * screenBrightnessMap in DisplayDeviceConfig. Adjustments such as Reduce Bright Colors might be
     * applied to the nits value.
     * @hide
     */
    public static final int BRIGHTNESS_UNIT_NITS = 2;

    /**
     * @hide
     */
    @IntDef(prefix = { "BRIGHTNESS_UNIT_" }, value = {
            BRIGHTNESS_UNIT_PERCENTAGE
            BRIGHTNESS_UNIT_PERCENTAGE,
            BRIGHTNESS_UNIT_NITS
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BrightnessUnit {}
@@ -777,11 +786,11 @@ public final class DisplayManager {
     * @hide
     */
    public static String brightnessUnitToString(@BrightnessUnit int unit) {
        if (Flags.setBrightnessByUnit() && unit == BRIGHTNESS_UNIT_PERCENTAGE) {
            return "percentage";
        } else {
            throw new IllegalStateException("Unexpected value: " + unit);
        }
        return switch (unit) {
            case BRIGHTNESS_UNIT_PERCENTAGE -> "percentage";
            case BRIGHTNESS_UNIT_NITS -> "nits";
            default -> throw new IllegalStateException("Unexpected value: " + unit);
        };
    }

    /** @hide */
+12 −0
Original line number Diff line number Diff line
@@ -1267,6 +1267,18 @@ public class AutomaticBrightnessController {
        return mCurrentBrightnessMapper.getBrightnessFromNits(nits);
    }

    /**
     * Convert a brightness nit value to a float scale value. It is assumed that the nit value
     * provided might have adjustments, such as RBC, applied.
     *
     * @param nits The nit value
     * @return The float scale value or {@link PowerManager.BRIGHTNESS_INVALID_FLOAT} if no
     * conversion is possible.
     */
    public float getBrightnessFromAdjustedNits(float nits) {
        return mCurrentBrightnessMapper.getBrightnessFromAdjustedNits(nits);
    }

    public void recalculateSplines(boolean applyAdjustment, float[] adjustment) {
        mCurrentBrightnessMapper.recalculateSplines(applyAdjustment, adjustment);

+19 −0
Original line number Diff line number Diff line
@@ -330,6 +330,15 @@ public abstract class BrightnessMappingStrategy {
     */
    public abstract float getBrightnessFromNits(float nits);

    /**
     * Converts the provided nit value to a float scale value if possible. Adjustments, such as RBC
     * might be applied to the nit value.
     *
     * Returns {@link PowerManager.BRIGHTNESS_INVALID_FLOAT} if there's no available mapping for
     * the nits to float scale.
     */
    public abstract float getBrightnessFromAdjustedNits(float nits);

    /**
     * Adds a user interaction data point to the brightness mapping.
     *
@@ -703,6 +712,11 @@ public abstract class BrightnessMappingStrategy {
            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
        }

        @Override
        public float getBrightnessFromAdjustedNits(float nits) {
            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
        }

        @Override
        public void addUserDataPoint(float lux, float brightness) {
            float unadjustedBrightness = getUnadjustedBrightness(lux);
@@ -986,6 +1000,11 @@ public abstract class BrightnessMappingStrategy {
            return mNitsToBrightnessSpline.interpolate(nits);
        }

        @Override
        public float getBrightnessFromAdjustedNits(float nits) {
            return mAdjustedNitsToBrightnessSpline.interpolate(nits);
        }

        @Override
        public void addUserDataPoint(float lux, float brightness) {
            float unadjustedBrightness = getUnadjustedBrightness(lux);
+21 −3
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import static android.Manifest.permission.RESTRICT_DISPLAY_MODES;
import static android.Manifest.permission.WRITE_SETTINGS;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
import static android.hardware.display.DisplayManager.BRIGHTNESS_UNIT_NITS;
import static android.hardware.display.DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_ALWAYS_UNLOCKED;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
@@ -64,6 +65,7 @@ import static android.text.TextUtils.formatSimple;
import static android.view.Display.HdrCapabilities.HDR_TYPE_INVALID;

import static com.android.server.display.PersistentDataStore.DEFAULT_CONNECTION_PREFERENCE;
import static com.android.server.display.brightness.BrightnessUtils.isValidBrightnessValue;
import static com.android.server.display.layout.Layout.Display.POSITION_REAR;

import android.Manifest;
@@ -5340,8 +5342,7 @@ public final class DisplayManagerService extends SystemService {
                    BrightnessInfo info = getBrightnessInfoInternal(displayId);
                    if (info == null) {
                        Slog.w(TAG,
                                "setBrightnessByUnit: no BrightnessInfo for display "
                                        + displayId);
                                "setBrightnessByUnit: no BrightnessInfo for display " + displayId);
                        return;
                    }

@@ -5351,6 +5352,16 @@ public final class DisplayManagerService extends SystemService {
                    // Interpolate to the range [currentlyAllowedMin, currentlyAllowedMax]
                    brightnessFloat = MathUtils.lerp(info.brightnessMinimum, info.brightnessMaximum,
                            linearBrightness);
                } else if (unit == BRIGHTNESS_UNIT_NITS) {
                    DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
                    if (dpc == null) {
                        throw new IllegalArgumentException(
                                "No DisplayPowerController for display " + displayId);
                    }
                    brightnessFloat = dpc.getBrightnessFromAdjustedNits(value);
                    if (!isValidBrightnessValue(brightnessFloat)) {
                        throw new IllegalArgumentException("This device does not support nits");
                    }
                } else {
                    throw new IllegalArgumentException("Invalid brightness unit: " + unit);
                }
@@ -5373,6 +5384,7 @@ public final class DisplayManagerService extends SystemService {
        @Override // Binder call
        public float getBrightnessByUnit(int displayId, @DisplayManager.BrightnessUnit int unit) {
            synchronized (mSyncRoot) {
                float brightnessFloat = getBrightnessInternal(displayId);
                if (unit == BRIGHTNESS_UNIT_PERCENTAGE) {
                    BrightnessInfo info = getBrightnessInfoInternal(displayId);
                    if (info == null) {
@@ -5381,7 +5393,6 @@ public final class DisplayManagerService extends SystemService {
                                        + displayId);
                        return PowerManager.BRIGHTNESS_INVALID;
                    }
                    float brightnessFloat = getBrightnessInternal(displayId);
                    float normalizedBrightness = MathUtils.norm(info.brightnessMinimum,
                            info.brightnessMaximum, brightnessFloat);

@@ -5390,6 +5401,13 @@ public final class DisplayManagerService extends SystemService {
                            normalizedBrightness);

                    return gammaBrightness * 100;
                } else if (unit == BRIGHTNESS_UNIT_NITS) {
                    DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
                    if (dpc == null) {
                        throw new IllegalArgumentException(
                                "No DisplayPowerController for display " + displayId);
                    }
                    return dpc.convertToAdjustedNits(brightnessFloat);
                } else {
                    throw new IllegalArgumentException("Invalid brightness unit: " + unit);
                }
+90 −35
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ class DisplayManagerShellCommand extends ShellCommand {
    private static final String TAG = "DisplayManagerShellCommand";
    private static final String NOTIFICATION_TYPES = "on-hotplug-error, on-link-training-failure, "
            + "on-cable-dp-incapable";
    private static final int INVALID_BRIGHTNESS_UNIT = -1;

    private final DisplayManagerService mService;
    private final DisplayManagerFlags mFlags;
@@ -149,13 +150,24 @@ class DisplayManagerShellCommand extends ShellCommand {
        pw.println("    Show notification for one of the following types: " + NOTIFICATION_TYPES);
        pw.println("  cancel-notifications");
        pw.println("    Cancel notifications.");
        pw.println("  get-brightness DISPLAY_ID UNIT(optional)");
        pw.println("    Gets the current brightness of the specified display. If no unit is "
                + "specified, the returned value is in the float scale [0, 1]. The unit can be '"
                + brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE) + "' which "
                + "will return the value displayed on the brightness slider.");
        pw.println("  set-brightness BRIGHTNESS");
        pw.println("    Sets the current brightness to BRIGHTNESS (a number between 0 and 1).");
        pw.println("  get-brightness [--id DISPLAY_ID] [--unit UNIT]");
        pw.println("    Gets the current brightness. Can specify the display ID, otherwise the "
                + "default display is used. If no unit is specified, the returned value is in the "
                + "float scale [0, 1]. The unit can be one of the following:");
        pw.println("      " + brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE)
                + " - return the value displayed on the brightness slider");
        pw.println("      " + brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_NITS)
                + " - return the brightness in nits (adjustments such as Reduce Bright Colors "
                + "might be included)");
        pw.println("  set-brightness BRIGHTNESS [--id DISPLAY_ID] [--unit UNIT]");
        pw.println("    Sets the current brightness. Can specify the display ID, otherwise the "
                + "default display is used. If no unit is specified, the value should be in the "
                + "float scale [0, 1]. The unit can be one of the following:");
        pw.println("      " + brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE)
                + " - set the value displayed on the brightness slider");
        pw.println("      " + brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_NITS)
                + " - set the brightness in nits (adjustments such as Reduce Bright Colors might "
                + "be included)");
        pw.println("  reset-brightness-configuration");
        pw.println("    Reset the brightness to its default configuration.");
        pw.println("  ab-logging-enable");
@@ -394,38 +406,46 @@ class DisplayManagerShellCommand extends ShellCommand {

    @SuppressLint("AndroidFrameworkRequiresPermission")
    private int getBrightness() {
        String displayIdString = getNextArg();
        if (displayIdString == null) {
            getErrPrintWriter().println("Error: no display id specified");
            return 1;
        }
        int displayId;
        String opt;
        int displayId = Display.DEFAULT_DISPLAY;
        int unit = INVALID_BRIGHTNESS_UNIT;
        while ((opt = getNextOption()) != null) {
            switch (opt) {
                case "--id" -> {
                    String displayIdString = getNextArgRequired();
                    try {
                        displayId = Integer.parseInt(displayIdString);
                    } catch (NumberFormatException e) {
            getErrPrintWriter().println("Error: invalid displayId=" + displayIdString + " not int");
                        getErrPrintWriter().println(
                                "Error: invalid displayId=" + displayIdString + ", not an int");
                        return 1;
                    }

        final Context context = mService.getContext();
        final DisplayManager dm = context.getSystemService(DisplayManager.class);

        String brightnessUnitString = getNextArg();
        float brightness;
        if (brightnessUnitString == null) {
            brightness = dm.getBrightness(displayId);
        } else {
            int unit;
                }
                case "--unit" -> {
                    String brightnessUnitString = getNextArgRequired();
                    if (brightnessUnitString.equals(
                            brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE))) {
                        unit = DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE;
                    } else if (brightnessUnitString.equals(
                            brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_NITS))) {
                        unit = DisplayManager.BRIGHTNESS_UNIT_NITS;
                    } else {
                getErrPrintWriter().println("Unexpected brightness unit: " + brightnessUnitString);
                        getErrPrintWriter().println(
                                "Unexpected brightness unit: " + brightnessUnitString);
                        return 1;
                    }
            brightness = dm.getBrightness(displayId, unit);
                }
            }
        }

        final Context context = mService.getContext();
        final DisplayManager dm = context.getSystemService(DisplayManager.class);
        float brightness;
        if (unit == INVALID_BRIGHTNESS_UNIT) {
            brightness = dm.getBrightness(displayId);
        } else {
            brightness = dm.getBrightness(displayId, unit);
        }
        getOutPrintWriter().println(brightness);
        return 0;
    }
@@ -437,19 +457,54 @@ class DisplayManagerShellCommand extends ShellCommand {
            getErrPrintWriter().println("Error: no brightness specified");
            return 1;
        }
        float brightness = -1.0f;
        float brightness;
        try {
            brightness = Float.parseFloat(brightnessText);
        } catch (NumberFormatException e) {
            getErrPrintWriter().println(
                    "Error: invalid brightness=" + brightnessText + ", not a float");
            return 1;
        }

        String opt;
        int displayId = Display.DEFAULT_DISPLAY;
        int unit = INVALID_BRIGHTNESS_UNIT;
        while ((opt = getNextOption()) != null) {
            switch (opt) {
                case "--id" -> {
                    String displayIdString = getNextArgRequired();
                    try {
                        displayId = Integer.parseInt(displayIdString);
                    } catch (NumberFormatException e) {
                        getErrPrintWriter().println(
                                "Error: invalid displayId=" + displayIdString + ", not an int");
                        return 1;
                    }
                }
        if (brightness < 0 || brightness > 1) {
            getErrPrintWriter().println("Error: brightness should be a number between 0 and 1");
                case "--unit" -> {
                    String brightnessUnitString = getNextArgRequired();
                    if (brightnessUnitString.equals(
                            brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE))) {
                        unit = DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE;
                    } else if (brightnessUnitString.equals(
                            brightnessUnitToString(DisplayManager.BRIGHTNESS_UNIT_NITS))) {
                        unit = DisplayManager.BRIGHTNESS_UNIT_NITS;
                    } else {
                        getErrPrintWriter().println(
                                "Unexpected brightness unit: " + brightnessUnitString);
                        return 1;
                    }
                }
            }
        }

        final Context context = mService.getContext();
        final DisplayManager dm = context.getSystemService(DisplayManager.class);
        dm.setBrightness(Display.DEFAULT_DISPLAY, brightness);
        if (unit == INVALID_BRIGHTNESS_UNIT) {
            dm.setBrightness(displayId, brightness);
        } else {
            dm.setBrightness(displayId, brightness, unit);
        }
        return 0;
    }

Loading