Merge "Revert "SCREEN_BRIGHTNESS - map 255 to current max"" into sc-dev

This commit is contained in:
Daniel Solomon
2021-07-28 17:42:43 +00:00
committed by Android (Google) Code Review
5 changed files with 39 additions and 65 deletions

View File

@@ -16,11 +16,9 @@
package com.android.internal.display; package com.android.internal.display;
import android.annotation.NonNull;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.hardware.display.BrightnessInfo;
import android.hardware.display.DisplayManager; import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener; import android.hardware.display.DisplayManager.DisplayListener;
import android.net.Uri; import android.net.Uri;
@@ -63,10 +61,10 @@ public class BrightnessSynchronizer {
updateBrightnessFloatFromInt(msg.arg1); updateBrightnessFloatFromInt(msg.arg1);
break; break;
case MSG_UPDATE_INT: case MSG_UPDATE_INT:
updateBrightnessIntFromFloat((BrightnessInfo) msg.obj); updateBrightnessIntFromFloat(Float.intBitsToFloat(msg.arg1));
break; break;
case MSG_UPDATE_BOTH: case MSG_UPDATE_BOTH:
updateBoth((BrightnessInfo) msg.obj, Float.intBitsToFloat(msg.arg1)); updateBoth(Float.intBitsToFloat(msg.arg1));
break; break;
default: default:
super.handleMessage(msg); super.handleMessage(msg);
@@ -97,11 +95,11 @@ public class BrightnessSynchronizer {
brightnessSyncObserver = new BrightnessSyncObserver(); brightnessSyncObserver = new BrightnessSyncObserver();
brightnessSyncObserver.startObserving(); brightnessSyncObserver.startObserving();
final BrightnessInfo brightnessInfo = getBrightnessInfo(); final float currentFloatBrightness = getScreenBrightnessFloat();
final int currentIntBrightness = getScreenBrightnessInt(mContext); final int currentIntBrightness = getScreenBrightnessInt(mContext);
if (brightnessInfo != null && !Float.isNaN(brightnessInfo.brightness)) { if (!Float.isNaN(currentFloatBrightness)) {
updateBrightnessIntFromFloat(brightnessInfo); updateBrightnessIntFromFloat(currentFloatBrightness);
} else if (currentIntBrightness != -1) { } else if (currentIntBrightness != -1) {
updateBrightnessFloatFromInt(currentIntBrightness); updateBrightnessFloatFromInt(currentIntBrightness);
} else { } else {
@@ -114,52 +112,45 @@ 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.
*
* @param brightnessInt The int brightness value to convert.
*/ */
public static float brightnessIntToFloat(int brightnessInt) { public static float brightnessIntToFloat(int brightnessInt) {
return brightnessIntToFloat(brightnessInt, null);
}
private static float brightnessIntToFloat(int brightnessInt, BrightnessInfo info) {
if (brightnessInt == PowerManager.BRIGHTNESS_OFF) { if (brightnessInt == PowerManager.BRIGHTNESS_OFF) {
return PowerManager.BRIGHTNESS_OFF_FLOAT; return PowerManager.BRIGHTNESS_OFF_FLOAT;
} else if (brightnessInt == PowerManager.BRIGHTNESS_INVALID) { } else if (brightnessInt == PowerManager.BRIGHTNESS_INVALID) {
return PowerManager.BRIGHTNESS_INVALID_FLOAT; return PowerManager.BRIGHTNESS_INVALID_FLOAT;
} else { } else {
final float minFloat = info != null final float minFloat = PowerManager.BRIGHTNESS_MIN;
? info.brightnessMinimum : PowerManager.BRIGHTNESS_MIN; final float maxFloat = PowerManager.BRIGHTNESS_MAX;
final float maxFloat = info != null
? info.brightnessMaximum : PowerManager.BRIGHTNESS_MAX;
final float minInt = PowerManager.BRIGHTNESS_OFF + 1; final float minInt = PowerManager.BRIGHTNESS_OFF + 1;
final float maxInt = PowerManager.BRIGHTNESS_ON; final float maxInt = PowerManager.BRIGHTNESS_ON;
return MathUtils.constrainedMap(minFloat, maxFloat, minInt, maxInt, brightnessInt); return MathUtils.constrainedMap(minFloat, maxFloat, minInt, maxInt, brightnessInt);
} }
} }
/**
* Converts between the float brightness system and the int brightness system.
*/
public static int brightnessFloatToInt(float brightnessFloat) {
return Math.round(brightnessFloatToIntRange(brightnessFloat));
}
/** /**
* Translates specified value from the float brightness system to 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. * given the min/max of each range. Accounts for special values such as OFF and invalid values.
* Value returned as a float primitive (to preserve precision), but is a value within the * Value returned as a float primitive (to preserve precision), but is a value within the
* int-system range. * int-system range.
*
* @param brightnessFloat The float brightness value to convert.
* @param info Brightness information to use in the conversion.
*/ */
public static int brightnessFloatToInt(float brightnessFloat, BrightnessInfo info) { public static float brightnessFloatToIntRange(float brightnessFloat) {
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 {
final float minFloat = info != null final float minFloat = PowerManager.BRIGHTNESS_MIN;
? info.brightnessMinimum : PowerManager.BRIGHTNESS_MIN; final float maxFloat = PowerManager.BRIGHTNESS_MAX;
final float maxFloat = info != null
? info.brightnessMaximum : PowerManager.BRIGHTNESS_MAX;
final float minInt = PowerManager.BRIGHTNESS_OFF + 1; final float minInt = PowerManager.BRIGHTNESS_OFF + 1;
final float maxInt = PowerManager.BRIGHTNESS_ON; final float maxInt = PowerManager.BRIGHTNESS_ON;
return Math.round(MathUtils.constrainedMap(minInt, maxInt, minFloat, maxFloat, return MathUtils.constrainedMap(minInt, maxInt, minFloat, maxFloat, brightnessFloat);
brightnessFloat));
} }
} }
@@ -194,37 +185,35 @@ public class BrightnessSynchronizer {
* @param value Brightness value as int to store in the float setting. * @param value Brightness value as int to store in the float setting.
*/ */
private void updateBrightnessFloatFromInt(int value) { private void updateBrightnessFloatFromInt(int value) {
final BrightnessInfo info = getBrightnessInfo(); if (brightnessFloatToInt(mPreferredSettingValue) == value) {
if (brightnessFloatToInt(mPreferredSettingValue, info) == value) {
return; return;
} }
mPreferredSettingValue = brightnessIntToFloat(value, info); mPreferredSettingValue = brightnessIntToFloat(value);
final int newBrightnessAsIntBits = Float.floatToIntBits(mPreferredSettingValue); final int newBrightnessAsIntBits = Float.floatToIntBits(mPreferredSettingValue);
mHandler.removeMessages(MSG_UPDATE_BOTH); mHandler.removeMessages(MSG_UPDATE_BOTH);
mHandler.obtainMessage(MSG_UPDATE_BOTH, newBrightnessAsIntBits, 0).sendToTarget(); mHandler.obtainMessage(MSG_UPDATE_BOTH, newBrightnessAsIntBits, 0).sendToTarget();
} }
/** /**
* Updates the settings from the specified {@link BrightnessInfo}. This is called whenever the * Updates the settings based on a passed in float value. This is called whenever the float
* float brightness changed from DisplayManager. mPreferredSettingValue holds the most recently * setting changes. mPreferredSettingValue holds the most recently updated brightness value
* updated brightness value as a float that we would like the display to be set to. * as a float that we would like the display to be set to.
* *
* We then schedule an update to both the int and float settings, but, remove all the other * We then schedule an update to both the int and float settings, but, remove all the other
* messages to update all, to prevent us getting stuck in a loop. * messages to update all, to prevent us getting stuck in a loop.
* *
* @param brightnessInfo Current brightness information * @param value Brightness setting as float to store in int setting.
*/ */
private void updateBrightnessIntFromFloat(@NonNull BrightnessInfo brightnessInfo) { private void updateBrightnessIntFromFloat(float value) {
final float value = brightnessInfo.brightness;
if (floatEquals(mPreferredSettingValue, value)) { if (floatEquals(mPreferredSettingValue, value)) {
return; return;
} }
mPreferredSettingValue = value; mPreferredSettingValue = value;
final int newBrightnessAsIntBits = Float.floatToIntBits(mPreferredSettingValue);
mHandler.removeMessages(MSG_UPDATE_BOTH); mHandler.removeMessages(MSG_UPDATE_BOTH);
mHandler.obtainMessage(MSG_UPDATE_BOTH, Float.floatToIntBits(value), 0, brightnessInfo) mHandler.obtainMessage(MSG_UPDATE_BOTH, newBrightnessAsIntBits, 0).sendToTarget();
.sendToTarget();
} }
@@ -233,24 +222,16 @@ public class BrightnessSynchronizer {
* mDisplayManager.setBrightness automatically checks for changes * mDisplayManager.setBrightness automatically checks for changes
* Settings.System.putIntForUser needs to be checked, to prevent an extra callback to this class * Settings.System.putIntForUser needs to be checked, to prevent an extra callback to this class
* *
* @param brightnessInfo Brightness information, takes precedent over newBrightnessFloat
* @param newBrightnessFloat Brightness setting as float to store in both settings * @param newBrightnessFloat Brightness setting as float to store in both settings
*/ */
private void updateBoth(BrightnessInfo brightnessInfo, float newBrightnessFloat) { private void updateBoth(float newBrightnessFloat) {
int newBrightnessInt = brightnessFloatToInt(newBrightnessFloat, brightnessInfo); int newBrightnessInt = brightnessFloatToInt(newBrightnessFloat);
mDisplayManager.setBrightness(Display.DEFAULT_DISPLAY, newBrightnessFloat); mDisplayManager.setBrightness(Display.DEFAULT_DISPLAY, newBrightnessFloat);
if (getScreenBrightnessInt(mContext) != newBrightnessInt) { if (getScreenBrightnessInt(mContext) != newBrightnessInt) {
Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.putIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, newBrightnessInt, UserHandle.USER_CURRENT); Settings.System.SCREEN_BRIGHTNESS, newBrightnessInt, UserHandle.USER_CURRENT);
} }
}
private BrightnessInfo getBrightnessInfo() {
final Display display = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
if (display != null) {
return display.getBrightnessInfo();
}
return null;
} }
/** /**
@@ -282,15 +263,10 @@ public class BrightnessSynchronizer {
@Override @Override
public void onDisplayChanged(int displayId) { public void onDisplayChanged(int displayId) {
if (displayId != Display.DEFAULT_DISPLAY) { float currentFloat = getScreenBrightnessFloat();
return; int toSend = Float.floatToIntBits(currentFloat);
} mHandler.removeMessages(MSG_UPDATE_INT);
mHandler.obtainMessage(MSG_UPDATE_INT, toSend, 0).sendToTarget();
final BrightnessInfo info = getBrightnessInfo();
if (info != null) {
mHandler.removeMessages(MSG_UPDATE_INT);
mHandler.obtainMessage(MSG_UPDATE_INT, info).sendToTarget();
}
} }
}; };

View File

@@ -354,10 +354,9 @@ public class BrightnessController implements ToggleSlider.Listener {
convertGammaToLinearFloat(value, minBacklight, maxBacklight), convertGammaToLinearFloat(value, minBacklight, maxBacklight),
maxBacklight); maxBacklight);
if (stopTracking) { if (stopTracking) {
// Log brightness as a value between 0-1000 directly correlated to brightnesses 0-1.0 // TODO(brightnessfloat): change to use float value instead.
MetricsLogger.action(mContext, metric, MetricsLogger.action(mContext, metric,
Math.round(MathUtils.constrainedMap(0, 1000, PowerManager.BRIGHTNESS_MIN, BrightnessSynchronizer.brightnessFloatToInt(valFloat));
PowerManager.BRIGHTNESS_MAX, valFloat)));
} }
setBrightness(valFloat); setBrightness(valFloat);

View File

@@ -2342,7 +2342,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
try { try {
// TODO(brightnessfloat): change BatteryStats to use float // TODO(brightnessfloat): change BatteryStats to use float
mBatteryStats.noteScreenBrightness(BrightnessSynchronizer.brightnessFloatToInt( mBatteryStats.noteScreenBrightness(BrightnessSynchronizer.brightnessFloatToInt(
brightness, null)); brightness));
} catch (RemoteException e) { } catch (RemoteException e) {
// same process // same process
} }

View File

@@ -795,12 +795,11 @@ final class LocalDisplayAdapter extends DisplayAdapter {
mBacklightAdapter.setBacklight(sdrBacklight, sdrNits, backlight, nits); mBacklightAdapter.setBacklight(sdrBacklight, sdrNits, backlight, nits);
Trace.traceCounter(Trace.TRACE_TAG_POWER, Trace.traceCounter(Trace.TRACE_TAG_POWER,
"ScreenBrightness", "ScreenBrightness",
BrightnessSynchronizer.brightnessFloatToInt( BrightnessSynchronizer.brightnessFloatToInt(brightnessState));
brightnessState, null));
Trace.traceCounter(Trace.TRACE_TAG_POWER, Trace.traceCounter(Trace.TRACE_TAG_POWER,
"SdrScreenBrightness", "SdrScreenBrightness",
BrightnessSynchronizer.brightnessFloatToInt( BrightnessSynchronizer.brightnessFloatToInt(
sdrBrightnessState, null)); sdrBrightnessState));
} finally { } finally {
Trace.traceEnd(Trace.TRACE_TAG_POWER); Trace.traceEnd(Trace.TRACE_TAG_POWER);
} }

View File

@@ -293,7 +293,7 @@ public class LightsService extends SystemService {
+ ": brightness=" + brightness); + ": brightness=" + brightness);
return; return;
} }
int brightnessInt = BrightnessSynchronizer.brightnessFloatToInt(brightness, null); int brightnessInt = BrightnessSynchronizer.brightnessFloatToInt(brightness);
int color = brightnessInt & 0x000000ff; int color = brightnessInt & 0x000000ff;
color = 0xff000000 | (color << 16) | (color << 8) | color; color = 0xff000000 | (color << 16) | (color << 8) | color;
setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode); setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);