Merge "Keep the adjusted and unadjusted spline in BrightnessMappingStrategy" into udc-dev

This commit is contained in:
Piotr Wilczyński
2023-04-20 08:44:18 +00:00
committed by Android (Google) Code Review
7 changed files with 147 additions and 31 deletions

View File

@@ -1160,6 +1160,14 @@ public class AutomaticBrightnessController {
update();
}
/**
* 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
* passing the brightness value to follower displays.
*
* @param brightness The float scale value
* @return The nit value or -1f if no conversion is possible.
*/
public float convertToNits(float brightness) {
if (mCurrentBrightnessMapper != null) {
return mCurrentBrightnessMapper.convertToNits(brightness);
@@ -1168,6 +1176,30 @@ public class AutomaticBrightnessController {
}
}
/**
* Convert a brightness float scale value to a nit value. Adjustments, such as RBC are applied.
* This is used when sending the brightness value to
* {@link com.android.server.display.BrightnessTracker}.
*
* @param brightness The float scale value
* @return The nit value or -1f if no conversion is possible.
*/
public float convertToAdjustedNits(float brightness) {
if (mCurrentBrightnessMapper != null) {
return mCurrentBrightnessMapper.convertToAdjustedNits(brightness);
} else {
return -1.0f;
}
}
/**
* Convert a brightness nit value to a float scale value. It is assumed that the nit value
* provided does not 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 convertToFloatScale(float nits) {
if (mCurrentBrightnessMapper != null) {
return mCurrentBrightnessMapper.convertToFloatScale(nits);

View File

@@ -321,6 +321,14 @@ public abstract class BrightnessMappingStrategy {
*/
public abstract float convertToNits(float brightness);
/**
* Converts the provided brightness value to nits if possible. Adjustments, such as RBC are
* applied.
*
* Returns -1.0f if there's no available mapping for the brightness to nits.
*/
public abstract float convertToAdjustedNits(float brightness);
/**
* Converts the provided nit value to a float scale value if possible.
*
@@ -682,6 +690,11 @@ public abstract class BrightnessMappingStrategy {
return -1.0f;
}
@Override
public float convertToAdjustedNits(float brightness) {
return -1.0f;
}
@Override
public float convertToFloatScale(float nits) {
return PowerManager.BRIGHTNESS_INVALID_FLOAT;
@@ -804,6 +817,14 @@ public abstract class BrightnessMappingStrategy {
// a brightness in nits.
private Spline mBrightnessToNitsSpline;
// A spline mapping from nits with adjustments applied to the corresponding brightness
// value, normalized to the range [0, 1.0].
private Spline mAdjustedNitsToBrightnessSpline;
// A spline mapping from the system brightness value, normalized to the range [0, 1.0], to
// a brightness in nits with adjustments applied.
private Spline mBrightnessToAdjustedNitsSpline;
// The default brightness configuration.
private final BrightnessConfiguration mDefaultConfig;
@@ -843,6 +864,8 @@ public abstract class BrightnessMappingStrategy {
mNits = nits;
mBrightness = brightness;
computeNitsBrightnessSplines(mNits);
mAdjustedNitsToBrightnessSpline = mNitsToBrightnessSpline;
mBrightnessToAdjustedNitsSpline = mBrightnessToNitsSpline;
mDefaultConfig = config;
if (mLoggingEnabled) {
@@ -892,7 +915,7 @@ public abstract class BrightnessMappingStrategy {
nits = mDisplayWhiteBalanceController.calculateAdjustedBrightnessNits(nits);
}
float brightness = mNitsToBrightnessSpline.interpolate(nits);
float brightness = mAdjustedNitsToBrightnessSpline.interpolate(nits);
// Correct the brightness according to the current application and its category, but
// only if no user data point is set (as this will override the user setting).
if (mUserLux == -1) {
@@ -929,6 +952,11 @@ public abstract class BrightnessMappingStrategy {
return mBrightnessToNitsSpline.interpolate(brightness);
}
@Override
public float convertToAdjustedNits(float brightness) {
return mBrightnessToAdjustedNitsSpline.interpolate(brightness);
}
@Override
public float convertToFloatScale(float nits) {
return mNitsToBrightnessSpline.interpolate(nits);
@@ -989,7 +1017,13 @@ public abstract class BrightnessMappingStrategy {
@Override
public void recalculateSplines(boolean applyAdjustment, float[] adjustedNits) {
mBrightnessRangeAdjustmentApplied = applyAdjustment;
computeNitsBrightnessSplines(mBrightnessRangeAdjustmentApplied ? adjustedNits : mNits);
if (applyAdjustment) {
mAdjustedNitsToBrightnessSpline = Spline.createSpline(adjustedNits, mBrightness);
mBrightnessToAdjustedNitsSpline = Spline.createSpline(mBrightness, adjustedNits);
} else {
mAdjustedNitsToBrightnessSpline = mNitsToBrightnessSpline;
mBrightnessToAdjustedNitsSpline = mBrightnessToNitsSpline;
}
}
@Override
@@ -999,6 +1033,8 @@ public abstract class BrightnessMappingStrategy {
pw.println(" mBrightnessSpline=" + mBrightnessSpline);
pw.println(" mNitsToBrightnessSpline=" + mNitsToBrightnessSpline);
pw.println(" mBrightnessToNitsSpline=" + mBrightnessToNitsSpline);
pw.println(" mAdjustedNitsToBrightnessSpline=" + mAdjustedNitsToBrightnessSpline);
pw.println(" mAdjustedBrightnessToNitsSpline=" + mBrightnessToAdjustedNitsSpline);
pw.println(" mMaxGamma=" + mMaxGamma);
pw.println(" mAutoBrightnessAdjustment=" + mAutoBrightnessAdjustment);
pw.println(" mUserLux=" + mUserLux);
@@ -1072,7 +1108,7 @@ public abstract class BrightnessMappingStrategy {
float defaultNits = defaultSpline.interpolate(lux);
float longTermNits = currSpline.interpolate(lux);
float shortTermNits = mBrightnessSpline.interpolate(lux);
float brightness = mNitsToBrightnessSpline.interpolate(shortTermNits);
float brightness = mAdjustedNitsToBrightnessSpline.interpolate(shortTermNits);
String luxPrefix = (lux == mUserLux ? "^" : "");
String strLux = luxPrefix + toStrFloatForDump(lux);
@@ -1146,7 +1182,7 @@ public abstract class BrightnessMappingStrategy {
float[] defaultNits = defaultCurve.second;
float[] defaultBrightness = new float[defaultNits.length];
for (int i = 0; i < defaultBrightness.length; i++) {
defaultBrightness[i] = mNitsToBrightnessSpline.interpolate(defaultNits[i]);
defaultBrightness[i] = mAdjustedNitsToBrightnessSpline.interpolate(defaultNits[i]);
}
Pair<float[], float[]> curve = getAdjustedCurve(defaultLux, defaultBrightness, mUserLux,
mUserBrightness, mAutoBrightnessAdjustment, mMaxGamma);
@@ -1154,7 +1190,7 @@ public abstract class BrightnessMappingStrategy {
float[] brightness = curve.second;
float[] nits = new float[brightness.length];
for (int i = 0; i < nits.length; i++) {
nits[i] = mBrightnessToNitsSpline.interpolate(brightness[i]);
nits[i] = mBrightnessToAdjustedNitsSpline.interpolate(brightness[i]);
}
mBrightnessSpline = Spline.createSpline(lux, nits);
}
@@ -1162,7 +1198,7 @@ public abstract class BrightnessMappingStrategy {
private float getUnadjustedBrightness(float lux) {
Pair<float[], float[]> curve = mConfig.getCurve();
Spline spline = Spline.createSpline(curve.first, curve.second);
return mNitsToBrightnessSpline.interpolate(spline.interpolate(lux));
return mAdjustedNitsToBrightnessSpline.interpolate(spline.interpolate(lux));
}
private float correctBrightness(float brightness, String packageName, int category) {

View File

@@ -1039,7 +1039,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
noteScreenBrightness(mPowerState.getScreenBrightness());
// Initialize all of the brightness tracking state
final float brightness = convertToNits(mPowerState.getScreenBrightness());
final float brightness = convertToAdjustedNits(mPowerState.getScreenBrightness());
if (mBrightnessTracker != null && brightness >= PowerManager.BRIGHTNESS_MIN) {
mBrightnessTracker.start(brightness);
}
@@ -2698,7 +2698,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
private void notifyBrightnessTrackerChanged(float brightness, boolean userInitiated,
boolean wasShortTermModelActive) {
final float brightnessInNits = convertToNits(brightness);
final float brightnessInNits = convertToAdjustedNits(brightness);
if (mUseAutoBrightness && brightnessInNits >= 0.0f
&& mAutomaticBrightnessController != null && mBrightnessTracker != null) {
// We only want to track changes on devices that can actually map the display backlight
@@ -2722,6 +2722,13 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
return mAutomaticBrightnessController.convertToNits(brightness);
}
private float convertToAdjustedNits(float brightness) {
if (mAutomaticBrightnessController == null) {
return -1f;
}
return mAutomaticBrightnessController.convertToAdjustedNits(brightness);
}
private float convertToFloatScale(float nits) {
if (mAutomaticBrightnessController == null) {
return PowerManager.BRIGHTNESS_INVALID_FLOAT;
@@ -3075,16 +3082,16 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
int appliedRbcStrength = event.isRbcEnabled() ? event.getRbcStrength() : -1;
float appliedHbmMaxNits =
event.getHbmMode() == BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF
? -1f : convertToNits(event.getHbmMax());
? -1f : convertToAdjustedNits(event.getHbmMax());
// thermalCapNits set to -1 if not currently capping max brightness
float appliedThermalCapNits =
event.getThermalMax() == PowerManager.BRIGHTNESS_MAX
? -1f : convertToNits(event.getThermalMax());
? -1f : convertToAdjustedNits(event.getThermalMax());
if (mIsDisplayInternal) {
FrameworkStatsLog.write(FrameworkStatsLog.DISPLAY_BRIGHTNESS_CHANGED,
convertToNits(event.getInitialBrightness()),
convertToNits(event.getBrightness()),
convertToAdjustedNits(event.getInitialBrightness()),
convertToAdjustedNits(event.getBrightness()),
event.getLux(),
event.getPhysicalDisplayId(),
event.wasShortTermModelActive(),

View File

@@ -854,7 +854,7 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
noteScreenBrightness(mPowerState.getScreenBrightness());
// Initialize all of the brightness tracking state
final float brightness = mDisplayBrightnessController.convertToNits(
final float brightness = mDisplayBrightnessController.convertToAdjustedNits(
mPowerState.getScreenBrightness());
if (mBrightnessTracker != null && brightness >= PowerManager.BRIGHTNESS_MIN) {
mBrightnessTracker.start(brightness);
@@ -2165,7 +2165,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
private void notifyBrightnessTrackerChanged(float brightness, boolean userInitiated,
boolean wasShortTermModelActive) {
final float brightnessInNits = mDisplayBrightnessController.convertToNits(brightness);
final float brightnessInNits =
mDisplayBrightnessController.convertToAdjustedNits(brightness);
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
@@ -2438,15 +2439,16 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
int appliedRbcStrength = event.isRbcEnabled() ? event.getRbcStrength() : -1;
float appliedHbmMaxNits =
event.getHbmMode() == BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF
? -1f : mDisplayBrightnessController.convertToNits(event.getHbmMax());
? -1f : mDisplayBrightnessController.convertToAdjustedNits(event.getHbmMax());
// thermalCapNits set to -1 if not currently capping max brightness
float appliedThermalCapNits =
event.getThermalMax() == PowerManager.BRIGHTNESS_MAX
? -1f : mDisplayBrightnessController.convertToNits(event.getThermalMax());
? -1f : mDisplayBrightnessController.convertToAdjustedNits(event.getThermalMax());
if (mIsDisplayInternal) {
FrameworkStatsLog.write(FrameworkStatsLog.DISPLAY_BRIGHTNESS_CHANGED,
mDisplayBrightnessController.convertToNits(event.getInitialBrightness()),
mDisplayBrightnessController.convertToNits(event.getBrightness()),
mDisplayBrightnessController
.convertToAdjustedNits(event.getInitialBrightness()),
mDisplayBrightnessController.convertToAdjustedNits(event.getBrightness()),
event.getLux(),
event.getPhysicalDisplayId(),
event.wasShortTermModelActive(),

View File

@@ -312,7 +312,10 @@ public final class DisplayBrightnessController {
}
/**
* Convert a brightness float scale value to a nit value.
* 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
* passing the brightness value to follower displays.
*
* @param brightness The float scale value
* @return The nit value or -1f if no conversion is possible.
*/
@@ -324,7 +327,24 @@ public final class DisplayBrightnessController {
}
/**
* Convert a brightness nit value to a float scale value.
* Convert a brightness float scale value to a nit value. Adjustments, such as RBC are applied.
* This is used when sending the brightness value to
* {@link com.android.server.display.BrightnessTracker}.
*
* @param brightness The float scale value
* @return The nit value or -1f if no conversion is possible.
*/
public float convertToAdjustedNits(float brightness) {
if (mAutomaticBrightnessController == null) {
return -1f;
}
return mAutomaticBrightnessController.convertToAdjustedNits(brightness);
}
/**
* Convert a brightness nit value to a float scale value. It is assumed that the nit value
* provided does not 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.

View File

@@ -287,25 +287,37 @@ public class BrightnessMappingStrategyTest {
adjustedNits50p[i] = DISPLAY_RANGE_NITS[i] * 0.5f;
}
// Default is unadjusted
// Default
assertEquals(DISPLAY_RANGE_NITS[0], strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]),
0.0001f /* tolerance */);
TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[1], strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]),
0.0001f /* tolerance */);
TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[0],
strategy.convertToAdjustedNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]), TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[1],
strategy.convertToAdjustedNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]), TOLERANCE);
// When adjustment is turned on, adjustment array is used
// Adjustment is turned on
strategy.recalculateSplines(true, adjustedNits50p);
assertEquals(DISPLAY_RANGE_NITS[0], strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]),
TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[1], strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]),
TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[0] / 2,
strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]), 0.0001f /* tolerance */);
strategy.convertToAdjustedNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]), TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[1] / 2,
strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]), 0.0001f /* tolerance */);
strategy.convertToAdjustedNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]), TOLERANCE);
// When adjustment is turned off, adjustment array is ignored
// Adjustment is turned off
strategy.recalculateSplines(false, adjustedNits50p);
assertEquals(DISPLAY_RANGE_NITS[0], strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]),
0.0001f /* tolerance */);
TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[1], strategy.convertToNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]),
0.0001f /* tolerance */);
TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[0],
strategy.convertToAdjustedNits(BACKLIGHT_RANGE_ZERO_TO_ONE[0]), TOLERANCE);
assertEquals(DISPLAY_RANGE_NITS[1],
strategy.convertToAdjustedNits(BACKLIGHT_RANGE_ZERO_TO_ONE[1]), TOLERANCE);
}
@Test

View File

@@ -279,20 +279,27 @@ public final class DisplayBrightnessControllerTest {
@Test
public void testConvertToNits() {
float brightness = 0.5f;
float nits = 300;
final float brightness = 0.5f;
final float nits = 300;
final float adjustedNits = 200;
// ABC is null
assertEquals(-1f, mDisplayBrightnessController.convertToNits(brightness),
/* delta= */ 0);
assertEquals(-1f, mDisplayBrightnessController.convertToAdjustedNits(brightness),
/* delta= */ 0);
AutomaticBrightnessController automaticBrightnessController =
mock(AutomaticBrightnessController.class);
when(automaticBrightnessController.convertToNits(brightness)).thenReturn(nits);
when(automaticBrightnessController.convertToAdjustedNits(brightness))
.thenReturn(adjustedNits);
mDisplayBrightnessController.setAutomaticBrightnessController(
automaticBrightnessController);
assertEquals(nits, mDisplayBrightnessController.convertToNits(brightness), /* delta= */ 0);
assertEquals(adjustedNits, mDisplayBrightnessController.convertToAdjustedNits(brightness),
/* delta= */ 0);
}
@Test