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

Commit ed5a8fe9 authored by Santos Cordon's avatar Santos Cordon
Browse files

Adjust float-to-int brightness conversion to be purely in float.

By converting to int and back as part of the final steps in sending the
brightness to the HAL, we undo the whole benefit of having floating
point brightness.  This change alters the conversion to an int-range
without using the int-primitives. This helps maintain the existing
precision.

Bug: 155875691
Test: Manually verify with logs that the float value isn't altered
before going to the HAL (it previously was).
Test: atest CtsDisplayTestCases
Test: atest DisplayManagerServiceTests
Change-Id: Ibf2f8d00c575d440c3fe437b32c0613bcd471c2a

Change-Id: Ie9569efb7b951c15ffcf2306ff9f864f858df8b1
parent a6019674
Loading
Loading
Loading
Loading
+35 −26
Original line number Original line Diff line number Diff line
@@ -84,17 +84,17 @@ public class BrightnessSynchronizer{
     * Converts between the int brightness system and the float brightness system.
     * Converts between the int brightness system and the float brightness system.
     */
     */
    public static float brightnessIntToFloat(Context context, int brightnessInt) {
    public static float brightnessIntToFloat(Context context, int brightnessInt) {
        PowerManager pm = context.getSystemService(PowerManager.class);
        final PowerManager pm = context.getSystemService(PowerManager.class);
        float pmMinBrightness = pm.getBrightnessConstraint(
        final float pmMinBrightness = pm.getBrightnessConstraint(
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM);
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM);
        float pmMaxBrightness = pm.getBrightnessConstraint(
        final float pmMaxBrightness = pm.getBrightnessConstraint(
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM);
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM);
        int minBrightnessInt = brightnessFloatToInt(pmMinBrightness, PowerManager.BRIGHTNESS_MIN,
        final int minBrightnessInt = Math.round(brightnessFloatToIntRange(pmMinBrightness,
                PowerManager.BRIGHTNESS_MAX, PowerManager.BRIGHTNESS_OFF + 1,
                PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX,
                PowerManager.BRIGHTNESS_ON);
                PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON));
        int maxBrightnessInt = brightnessFloatToInt(pmMaxBrightness, PowerManager.BRIGHTNESS_MIN,
        final int maxBrightnessInt = Math.round(brightnessFloatToIntRange(pmMaxBrightness,
                PowerManager.BRIGHTNESS_MAX, PowerManager.BRIGHTNESS_OFF + 1,
                PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX,
                PowerManager.BRIGHTNESS_ON);
                PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON));


        return brightnessIntToFloat(brightnessInt, minBrightnessInt, maxBrightnessInt,
        return brightnessIntToFloat(brightnessInt, minBrightnessInt, maxBrightnessInt,
                pmMinBrightness, pmMaxBrightness);
                pmMinBrightness, pmMaxBrightness);
@@ -119,34 +119,43 @@ public class BrightnessSynchronizer{
     * Converts between the float brightness system and the int brightness system.
     * Converts between the float brightness system and the int brightness system.
     */
     */
    public static int brightnessFloatToInt(Context context, float brightnessFloat) {
    public static int brightnessFloatToInt(Context context, float brightnessFloat) {
        PowerManager pm = context.getSystemService(PowerManager.class);
        return Math.round(brightnessFloatToIntRange(context, brightnessFloat));
        float pmMinBrightness = pm.getBrightnessConstraint(
    }

    /**
     * Converts between the float brightness system and the int brightness system, but returns
     * the converted value as a float within the int-system's range. This method helps with
     * conversions from one system to the other without losing the floating-point precision.
     */
    public static float brightnessFloatToIntRange(Context context, float brightnessFloat) {
        final PowerManager pm = context.getSystemService(PowerManager.class);
        final float minFloat = pm.getBrightnessConstraint(
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM);
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM);
        float pmMaxBrightness = pm.getBrightnessConstraint(
        final float maxFloat = pm.getBrightnessConstraint(
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM);
                PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM);
        int minBrightnessInt = brightnessFloatToInt(pmMinBrightness, PowerManager.BRIGHTNESS_MIN,
        final float minInt = brightnessFloatToIntRange(minFloat,
                PowerManager.BRIGHTNESS_MAX, PowerManager.BRIGHTNESS_OFF + 1,
                PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX,
                PowerManager.BRIGHTNESS_ON);
                PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON);
        int maxBrightnessInt = brightnessFloatToInt(pmMaxBrightness, PowerManager.BRIGHTNESS_MIN,
        final float maxInt = brightnessFloatToIntRange(maxFloat,
                PowerManager.BRIGHTNESS_MAX, PowerManager.BRIGHTNESS_OFF + 1,
                PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX,
                PowerManager.BRIGHTNESS_ON);
                PowerManager.BRIGHTNESS_OFF + 1, PowerManager.BRIGHTNESS_ON);

        return brightnessFloatToIntRange(brightnessFloat, minFloat, maxFloat, minInt, maxInt);
        return brightnessFloatToInt(brightnessFloat, pmMinBrightness, pmMaxBrightness,
                minBrightnessInt, maxBrightnessInt);
    }
    }


    /**
    /**
     * Converts between the float brightness system and the int brightness system.
     * Translates specified value from the float brightness system to the int brightness system,
     * given the min/max of each range.  Accounts for special values such as OFF and invalid values.
     * Value returned as a float privimite (to preserve precision), but is a value within the
     * int-system range.
     */
     */
    public static int brightnessFloatToInt(float brightnessFloat, float minFloat, float maxFloat,
    private static float brightnessFloatToIntRange(float brightnessFloat, float minFloat,
            int minInt, int maxInt) {
            float maxFloat, float minInt, float maxInt) {
        if (floatEquals(brightnessFloat, PowerManager.BRIGHTNESS_OFF_FLOAT)) {
        if (floatEquals(brightnessFloat, PowerManager.BRIGHTNESS_OFF_FLOAT)) {
            return PowerManager.BRIGHTNESS_OFF;
            return PowerManager.BRIGHTNESS_OFF;
        } else if (Float.isNaN(brightnessFloat)) {
        } else if (Float.isNaN(brightnessFloat)) {
            return PowerManager.BRIGHTNESS_INVALID;
            return PowerManager.BRIGHTNESS_INVALID;
        } else {
        } else {
            return Math.round(MathUtils.constrainedMap((float) minInt, (float) maxInt, minFloat,
            return MathUtils.constrainedMap(minInt, maxInt, minFloat, maxFloat, brightnessFloat);
                    maxFloat, brightnessFloat));
        }
        }
    }
    }


+5 −7
Original line number Original line Diff line number Diff line
@@ -201,7 +201,6 @@ final class LocalDisplayAdapter extends DisplayAdapter {
        private SurfaceControl.DisplayConfig[] mDisplayConfigs;
        private SurfaceControl.DisplayConfig[] mDisplayConfigs;
        private Spline mSystemBrightnessToNits;
        private Spline mSystemBrightnessToNits;
        private Spline mNitsToHalBrightness;
        private Spline mNitsToHalBrightness;
        private boolean mHalBrightnessSupport;


        private DisplayDeviceConfig mDisplayDeviceConfig;
        private DisplayDeviceConfig mDisplayDeviceConfig;


@@ -225,7 +224,6 @@ final class LocalDisplayAdapter extends DisplayAdapter {
            }
            }
            mAllmSupported = SurfaceControl.getAutoLowLatencyModeSupport(displayToken);
            mAllmSupported = SurfaceControl.getAutoLowLatencyModeSupport(displayToken);
            mGameContentTypeSupported = SurfaceControl.getGameContentTypeSupport(displayToken);
            mGameContentTypeSupported = SurfaceControl.getGameContentTypeSupport(displayToken);
            mHalBrightnessSupport = SurfaceControl.getDisplayBrightnessSupport(displayToken);
            mDisplayDeviceConfig = null;
            mDisplayDeviceConfig = null;
            // Defer configuration file loading
            // Defer configuration file loading
            BackgroundThread.getHandler().sendMessage(PooledLambda.obtainMessage(
            BackgroundThread.getHandler().sendMessage(PooledLambda.obtainMessage(
@@ -717,11 +715,10 @@ final class LocalDisplayAdapter extends DisplayAdapter {
                        Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayBrightness("
                        Trace.traceBegin(Trace.TRACE_TAG_POWER, "setDisplayBrightness("
                                + "id=" + physicalDisplayId + ", brightness=" + brightness + ")");
                                + "id=" + physicalDisplayId + ", brightness=" + brightness + ")");
                        try {
                        try {
                            // TODO: make it float
                            if (isHalBrightnessRangeSpecified()) {
                            if (isHalBrightnessRangeSpecified()) {
                                brightness = displayBrightnessToHalBrightness(
                                brightness = displayBrightnessToHalBrightness(
                                        BrightnessSynchronizer.brightnessFloatToInt(getContext(),
                                        BrightnessSynchronizer.brightnessFloatToIntRange(
                                                brightness));
                                                getContext(), brightness));
                            }
                            }
                            if (mBacklight != null) {
                            if (mBacklight != null) {
                                mBacklight.setBrightness(brightness);
                                mBacklight.setBrightness(brightness);
@@ -744,12 +741,13 @@ final class LocalDisplayAdapter extends DisplayAdapter {
                     * Hal brightness space if the HAL brightness space has been provided via
                     * Hal brightness space if the HAL brightness space has been provided via
                     * a display device configuration file.
                     * a display device configuration file.
                     */
                     */
                    private float displayBrightnessToHalBrightness(int brightness) {
                    private float displayBrightnessToHalBrightness(float brightness) {
                        if (!isHalBrightnessRangeSpecified()) {
                        if (!isHalBrightnessRangeSpecified()) {
                            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
                            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
                        }
                        }


                        if (brightness == 0) {
                        if (BrightnessSynchronizer.floatEquals(
                                brightness, PowerManager.BRIGHTNESS_OFF)) {
                            return PowerManager.BRIGHTNESS_OFF_FLOAT;
                            return PowerManager.BRIGHTNESS_OFF_FLOAT;
                        }
                        }