Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] backlight: convert linear to gamma float brightness #6

Open
wants to merge 1 commit into
base: aosp-12.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <android-base/logging.h>
#include <dirent.h>
#include <fstream>
#include <math.h>

static const std::string BACKLIGHT_DIR = "/sys/class/backlight";
static const std::string LEDS_DIR = "/sys/class/leds";
Expand Down Expand Up @@ -59,11 +60,26 @@ static int32_t rgbToBrightness(int32_t color)
return (77 * r + 150 * g + 29 * b) >> 8;
}

static float convertLinearToGammaFloat(float val)
{
const float r = 0.5;
const float a = 0.17883277;
const float b = 0.28466892;
const float c = 0.55991073;
// HLG normalizes to the range [0, 12] rather than [0, 1]
float normalizedVal = val * 12;

if (normalizedVal <= 1.0)
return sqrt(normalizedVal) * r;
else
return a * log(normalizedVal - b) + c;
}

ndk::ScopedAStatus Backlight::setLightState(const HwLightState &state) const
{
auto brightness = rgbToBrightness(state.color);
// Adding half of the max (255/2=127) provides proper rounding while staying in integer mode:
brightness = (brightness * maxBrightness + 127) / 255;
brightness = convertLinearToGammaFloat((float)brightness / 255) * maxBrightness;

if (state.brightnessMode == BrightnessMode::LOW_PERSISTENCE)
LOG(ERROR) << "TODO: Implement Low Persistence brightness mode";
LOG(DEBUG) << "Changing backlight to level " << brightness << "/" << maxBrightness;
Expand Down