diff mbox series

[v3,2/4] backlight: Expose brightness curve type through sysfs

Message ID 20190709190007.91260-3-mka@chromium.org (mailing list archive)
State New, archived
Headers show
Series backlight: Expose brightness curve type through sysfs | expand

Commit Message

Matthias Kaehlcke July 9, 2019, 7 p.m. UTC
Backlight brightness curves can have different shapes. The two main
types are linear and non-linear curves. The human eye doesn't
perceive linearly increasing/decreasing brightness as linear (see
also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
linearly to human eye"), hence many backlights use non-linear (often
logarithmic) brightness curves. The type of curve currently is opaque
to userspace, so userspace often uses more or less reliable heuristics
(like the number of brightness levels) to decide whether to treat a
backlight device as linear or non-linear.

Export the type of the brightness curve via the new sysfs attribute
'scale'. The value of the attribute can be 'linear', 'non-linear' or
'unknown'. For devices that don't provide information about the scale
of their brightness curve the value of the 'scale' attribute is 'unknown'.

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
Feel free to suggest improvements in the documentation :)

Changes in v3:
- removed composite strings, only keep 'linear', 'non-linear' and
  'unknown'
- updated sysfs attribute documentation
- updated commit message

Changes in v2:
- changed order of brightness scale enums, explicitly make 'unknown' zero
- minor update of commit message
- deleted excess blank line after 'backlight_scale_types'
- s/curves/curve/ in sysfs doc
---
 .../ABI/testing/sysfs-class-backlight         | 26 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 drivers/video/backlight/backlight.c           | 19 ++++++++++++++
 include/linux/backlight.h                     |  8 ++++++
 4 files changed, 54 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-backlight

Comments

Matthias Kaehlcke Aug. 7, 2019, 8:15 p.m. UTC | #1
On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> Backlight brightness curves can have different shapes. The two main
> types are linear and non-linear curves. The human eye doesn't
> perceive linearly increasing/decreasing brightness as linear (see
> also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> linearly to human eye"), hence many backlights use non-linear (often
> logarithmic) brightness curves. The type of curve currently is opaque
> to userspace, so userspace often uses more or less reliable heuristics
> (like the number of brightness levels) to decide whether to treat a
> backlight device as linear or non-linear.
> 
> Export the type of the brightness curve via the new sysfs attribute
> 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> 'unknown'. For devices that don't provide information about the scale
> of their brightness curve the value of the 'scale' attribute is 'unknown'.
> 
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>

Daniel (et al): do you have any more comments on this patch/series or
is it ready to land?

Thanks

Matthias
Daniel Thompson Aug. 16, 2019, 3:54 p.m. UTC | #2
On 07/08/2019 21:15, Matthias Kaehlcke wrote:
> On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
>> Backlight brightness curves can have different shapes. The two main
>> types are linear and non-linear curves. The human eye doesn't
>> perceive linearly increasing/decreasing brightness as linear (see
>> also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
>> linearly to human eye"), hence many backlights use non-linear (often
>> logarithmic) brightness curves. The type of curve currently is opaque
>> to userspace, so userspace often uses more or less reliable heuristics
>> (like the number of brightness levels) to decide whether to treat a
>> backlight device as linear or non-linear.
>>
>> Export the type of the brightness curve via the new sysfs attribute
>> 'scale'. The value of the attribute can be 'linear', 'non-linear' or
>> 'unknown'. For devices that don't provide information about the scale
>> of their brightness curve the value of the 'scale' attribute is 'unknown'.
>>
>> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> 
> Daniel (et al): do you have any more comments on this patch/series or
> is it ready to land?

I decided to leave it for a long while for others to review since I'm 
still a tiny bit uneasy about the linear/non-linear terminology.

However that's my only concern, its fairly minor and I've dragged by 
feet for more then long enough, so:
Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>


Daniel.
Uwe Kleine-König Aug. 16, 2019, 4:51 p.m. UTC | #3
On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> Backlight brightness curves can have different shapes. The two main
> types are linear and non-linear curves. The human eye doesn't
> perceive linearly increasing/decreasing brightness as linear (see
> also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> linearly to human eye"), hence many backlights use non-linear (often
> logarithmic) brightness curves. The type of curve currently is opaque
> to userspace, so userspace often uses more or less reliable heuristics
> (like the number of brightness levels) to decide whether to treat a
> backlight device as linear or non-linear.
> 
> Export the type of the brightness curve via the new sysfs attribute
> 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> 'unknown'. For devices that don't provide information about the scale
> of their brightness curve the value of the 'scale' attribute is 'unknown'.
> 
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>

I wonder what kind of problem you are solving here. Can you describe
that in a few words?

Best regards
Uwe
Matthias Kaehlcke Aug. 16, 2019, 5:51 p.m. UTC | #4
Hi Uwe,

On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > Backlight brightness curves can have different shapes. The two main
> > types are linear and non-linear curves. The human eye doesn't
> > perceive linearly increasing/decreasing brightness as linear (see
> > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > linearly to human eye"), hence many backlights use non-linear (often
> > logarithmic) brightness curves. The type of curve currently is opaque
> > to userspace, so userspace often uses more or less reliable heuristics
> > (like the number of brightness levels) to decide whether to treat a
> > backlight device as linear or non-linear.
> > 
> > Export the type of the brightness curve via the new sysfs attribute
> > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > 'unknown'. For devices that don't provide information about the scale
> > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > 
> > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> 
> I wonder what kind of problem you are solving here. Can you describe
> that in a few words?

The human eye perceives brightness in a logarithmic manner. For
backlights with a linear brightness curve brightness controls like
sliders need to use a mapping to achieve a behavior that is perceived
as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)

As of now userspace doesn't have information about the type of the
brightness curve, and often uses heuristics to make a guess, which may
be right most of the time, but not always. The new attribute eliminates
the need to guess.
Matthias Kaehlcke Aug. 16, 2019, 5:53 p.m. UTC | #5
On Fri, Aug 16, 2019 at 04:54:18PM +0100, Daniel Thompson wrote:
> On 07/08/2019 21:15, Matthias Kaehlcke wrote:
> > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > Backlight brightness curves can have different shapes. The two main
> > > types are linear and non-linear curves. The human eye doesn't
> > > perceive linearly increasing/decreasing brightness as linear (see
> > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > linearly to human eye"), hence many backlights use non-linear (often
> > > logarithmic) brightness curves. The type of curve currently is opaque
> > > to userspace, so userspace often uses more or less reliable heuristics
> > > (like the number of brightness levels) to decide whether to treat a
> > > backlight device as linear or non-linear.
> > > 
> > > Export the type of the brightness curve via the new sysfs attribute
> > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > 'unknown'. For devices that don't provide information about the scale
> > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > 
> > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > 
> > Daniel (et al): do you have any more comments on this patch/series or
> > is it ready to land?
> 
> I decided to leave it for a long while for others to review since I'm still
> a tiny bit uneasy about the linear/non-linear terminology.
> 
> However that's my only concern, its fairly minor and I've dragged by feet
> for more then long enough, so:
> Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>

Thanks!

If you or someone else has another suggestion for the terminology that
we can all agree on I'm happy to change it.
Uwe Kleine-König Aug. 16, 2019, 7:47 p.m. UTC | #6
On Fri, Aug 16, 2019 at 10:51:57AM -0700, Matthias Kaehlcke wrote:
> Hi Uwe,
> 
> On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > Backlight brightness curves can have different shapes. The two main
> > > types are linear and non-linear curves. The human eye doesn't
> > > perceive linearly increasing/decreasing brightness as linear (see
> > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > linearly to human eye"), hence many backlights use non-linear (often
> > > logarithmic) brightness curves. The type of curve currently is opaque
> > > to userspace, so userspace often uses more or less reliable heuristics
> > > (like the number of brightness levels) to decide whether to treat a
> > > backlight device as linear or non-linear.
> > > 
> > > Export the type of the brightness curve via the new sysfs attribute
> > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > 'unknown'. For devices that don't provide information about the scale
> > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > 
> > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > 
> > I wonder what kind of problem you are solving here. Can you describe
> > that in a few words?
> 
> The human eye perceives brightness in a logarithmic manner. For
> backlights with a linear brightness curve brightness controls like
> sliders need to use a mapping to achieve a behavior that is perceived
> as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)
> 
> As of now userspace doesn't have information about the type of the
> brightness curve, and often uses heuristics to make a guess, which may
> be right most of the time, but not always. The new attribute eliminates
> the need to guess.

This is about backlights right? So the kernel provides to userspace an
interval [0, x] for some x and depending on the physics of the the
backlight configuring x/2 (probably?) either means 50% measured light or
50% perceived light, right?

I wonder if it would be possible instead of giving different backlight
implementations the freedom to use either linear or logarithmic (or
quadratic?) scaling and tell userspace which of the options were picked
require the drivers to provide a (say) linear scaling and then userspace
wouldn't need to care about the exact physics.

Best regards
Uwe
Matthias Kaehlcke Aug. 16, 2019, 9:10 p.m. UTC | #7
On Fri, Aug 16, 2019 at 09:47:54PM +0200, Uwe Kleine-König wrote:
> On Fri, Aug 16, 2019 at 10:51:57AM -0700, Matthias Kaehlcke wrote:
> > Hi Uwe,
> > 
> > On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > Backlight brightness curves can have different shapes. The two main
> > > > types are linear and non-linear curves. The human eye doesn't
> > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > (like the number of brightness levels) to decide whether to treat a
> > > > backlight device as linear or non-linear.
> > > > 
> > > > Export the type of the brightness curve via the new sysfs attribute
> > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > 'unknown'. For devices that don't provide information about the scale
> > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > 
> > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > 
> > > I wonder what kind of problem you are solving here. Can you describe
> > > that in a few words?
> > 
> > The human eye perceives brightness in a logarithmic manner. For
> > backlights with a linear brightness curve brightness controls like
> > sliders need to use a mapping to achieve a behavior that is perceived
> > as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)
> > 
> > As of now userspace doesn't have information about the type of the
> > brightness curve, and often uses heuristics to make a guess, which may
> > be right most of the time, but not always. The new attribute eliminates
> > the need to guess.
> 
> This is about backlights right? So the kernel provides to userspace an
> interval [0, x] for some x and depending on the physics of the the
> backlight configuring x/2 (probably?) either means 50% measured light or
> 50% perceived light, right?

correct

> I wonder if it would be possible instead of giving different backlight
> implementations the freedom to use either linear or logarithmic (or
> quadratic?) scaling and tell userspace which of the options were picked
> require the drivers to provide a (say) linear scaling and then userspace
> wouldn't need to care about the exact physics.

In an ideal world the backlight interface would be consistent as you
suggest, however there are plenty of existing devices which use the
'other' scaling (regardless of which is chosen as the 'correct'
one). Userspace still has to deal with these. And changing previously
'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
when it keeps using its 'old' scaling, which now isn't correct anymore.
Uwe Kleine-König Aug. 19, 2019, 5:46 a.m. UTC | #8
Hello Matthias,

On Fri, Aug 16, 2019 at 02:10:51PM -0700, Matthias Kaehlcke wrote:
> On Fri, Aug 16, 2019 at 09:47:54PM +0200, Uwe Kleine-König wrote:
> > On Fri, Aug 16, 2019 at 10:51:57AM -0700, Matthias Kaehlcke wrote:
> > > Hi Uwe,
> > > 
> > > On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> > > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > > Backlight brightness curves can have different shapes. The two main
> > > > > types are linear and non-linear curves. The human eye doesn't
> > > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > > (like the number of brightness levels) to decide whether to treat a
> > > > > backlight device as linear or non-linear.
> > > > > 
> > > > > Export the type of the brightness curve via the new sysfs attribute
> > > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > > 'unknown'. For devices that don't provide information about the scale
> > > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > > 
> > > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > > 
> > > > I wonder what kind of problem you are solving here. Can you describe
> > > > that in a few words?
> > > 
> > > The human eye perceives brightness in a logarithmic manner. For
> > > backlights with a linear brightness curve brightness controls like
> > > sliders need to use a mapping to achieve a behavior that is perceived
> > > as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)
> > > 
> > > As of now userspace doesn't have information about the type of the
> > > brightness curve, and often uses heuristics to make a guess, which may
> > > be right most of the time, but not always. The new attribute eliminates
> > > the need to guess.
> > 
> > This is about backlights right? So the kernel provides to userspace an
> > interval [0, x] for some x and depending on the physics of the the
> > backlight configuring x/2 (probably?) either means 50% measured light or
> > 50% perceived light, right?
> 
> correct
> 
> > I wonder if it would be possible instead of giving different backlight
> > implementations the freedom to use either linear or logarithmic (or
> > quadratic?) scaling and tell userspace which of the options were picked
> > require the drivers to provide a (say) linear scaling and then userspace
> > wouldn't need to care about the exact physics.
> 
> In an ideal world the backlight interface would be consistent as you
> suggest, however there are plenty of existing devices which use the
> 'other' scaling (regardless of which is chosen as the 'correct'
> one). Userspace still has to deal with these. And changing previously
> 'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
> when it keeps using its 'old' scaling, which now isn't correct anymore.

It might be subjective, or maybe I'm just too optimistic, but I think if
there was no policy before about the meaning of

	echo 17 > brightness

other than "brighter than lower values and darker than higher ones"
introducing (say) the scale is intended to represent a linear brightness
curve is ok.

Unless userspace jumps through hoops and tries to identify the actual
device it is running on it is wrong on some machines anyhow and we're
only shifting the set of affected machines with a tighter policy (until
that userspace application is fixed).

And the big upside is that in the end (i.e. when all kernel drivers and
userspace applications are adapted to provide/consume the "correct"
curve) the result is simpler.

Just my 0.02€
Uwe
Daniel Thompson Aug. 19, 2019, 9:50 a.m. UTC | #9
On Mon, Aug 19, 2019 at 07:46:28AM +0200, Uwe Kleine-König wrote:
> Hello Matthias,
> 
> On Fri, Aug 16, 2019 at 02:10:51PM -0700, Matthias Kaehlcke wrote:
> > On Fri, Aug 16, 2019 at 09:47:54PM +0200, Uwe Kleine-König wrote:
> > > On Fri, Aug 16, 2019 at 10:51:57AM -0700, Matthias Kaehlcke wrote:
> > > > Hi Uwe,
> > > > 
> > > > On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> > > > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > > > Backlight brightness curves can have different shapes. The two main
> > > > > > types are linear and non-linear curves. The human eye doesn't
> > > > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > > > (like the number of brightness levels) to decide whether to treat a
> > > > > > backlight device as linear or non-linear.
> > > > > > 
> > > > > > Export the type of the brightness curve via the new sysfs attribute
> > > > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > > > 'unknown'. For devices that don't provide information about the scale
> > > > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > > > 
> > > > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > > > 
> > > > > I wonder what kind of problem you are solving here. Can you describe
> > > > > that in a few words?
> > > > 
> > > > The human eye perceives brightness in a logarithmic manner. For
> > > > backlights with a linear brightness curve brightness controls like
> > > > sliders need to use a mapping to achieve a behavior that is perceived
> > > > as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)
> > > > 
> > > > As of now userspace doesn't have information about the type of the
> > > > brightness curve, and often uses heuristics to make a guess, which may
> > > > be right most of the time, but not always. The new attribute eliminates
> > > > the need to guess.
> > > 
> > > This is about backlights right? So the kernel provides to userspace an
> > > interval [0, x] for some x and depending on the physics of the the
> > > backlight configuring x/2 (probably?) either means 50% measured light or
> > > 50% perceived light, right?
> > 
> > correct
> > 
> > > I wonder if it would be possible instead of giving different backlight
> > > implementations the freedom to use either linear or logarithmic (or
> > > quadratic?) scaling and tell userspace which of the options were picked
> > > require the drivers to provide a (say) linear scaling and then userspace
> > > wouldn't need to care about the exact physics.
> > 
> > In an ideal world the backlight interface would be consistent as you
> > suggest, however there are plenty of existing devices which use the
> > 'other' scaling (regardless of which is chosen as the 'correct'
> > one). Userspace still has to deal with these. And changing previously
> > 'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
> > when it keeps using its 'old' scaling, which now isn't correct anymore.
> 
> It might be subjective, or maybe I'm just too optimistic, but I think if
> there was no policy before about the meaning of
> 
> 	echo 17 > brightness
> 
> other than "brighter than lower values and darker than higher ones"
> introducing (say) the scale is intended to represent a linear brightness
> curve is ok.
> 
> Unless userspace jumps through hoops and tries to identify the actual
> device it is running on it is wrong on some machines anyhow and we're
> only shifting the set of affected machines with a tighter policy (until
> that userspace application is fixed).

I believe that there are two common approaches by userspace at present:

1. Assume the scale is perceptual and we can directly map a slider
   to the backlight value. This is common simply because most ACPI
   backlights are perceptual and therefore when tested in a laptop
   it works OK.

2. Assume that is max brightness is small (e.g. ACPI) then the
   scale is perceptual and if the max brightness is large (e.g.
   a PWM) then the scale is linear and apply a correction
   function between the slider and the control.

That historic baggage makes is diffcult to "just define a standardized
scale"... especially given that if we selected a standardized scale we
would probably want a perceptual scale with lots of steps (e.g. break
the heuristic).


> And the big upside is that in the end (i.e. when all kernel drivers and
> userspace applications are adapted to provide/consume the "correct"
> curve) the result is simpler.

My view is that this convergence will eventually be achieved but it will
happen through the obsolescence of the backlight sysfs interface. The
sysfs interface has other flaws, in particular no integration with the
DRM connector API.

Thus I would expect an alternative interface to emerge, most likely as
part of the DRM connector API. I'd expect such a new API to a
perceptual scale and to have a fixed max brightness with enough
steps to support animated backlight effects (IIRC 0..100 has been
proposed in the past) 

In the mean time getting the existing collection of backlight drivers
marked up as linear/logarithmic/etc will ease the introduction of that
API because, within the kernel, we might have gathered enough knowledge
to have some hope of correctly mapping each backlight onto a
standardized scale.


Daniel.
Daniel Thompson Aug. 19, 2019, 10:02 a.m. UTC | #10
On Fri, Aug 16, 2019 at 10:53:17AM -0700, Matthias Kaehlcke wrote:
> On Fri, Aug 16, 2019 at 04:54:18PM +0100, Daniel Thompson wrote:
> > On 07/08/2019 21:15, Matthias Kaehlcke wrote:
> > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > Backlight brightness curves can have different shapes. The two main
> > > > types are linear and non-linear curves. The human eye doesn't
> > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > (like the number of brightness levels) to decide whether to treat a
> > > > backlight device as linear or non-linear.
> > > > 
> > > > Export the type of the brightness curve via the new sysfs attribute
> > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > 'unknown'. For devices that don't provide information about the scale
> > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > 
> > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > 
> > > Daniel (et al): do you have any more comments on this patch/series or
> > > is it ready to land?
> > 
> > I decided to leave it for a long while for others to review since I'm still
> > a tiny bit uneasy about the linear/non-linear terminology.
> > 
> > However that's my only concern, its fairly minor and I've dragged by feet
> > for more then long enough, so:
> > Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
> 
> Thanks!
> 
> If you or someone else has another suggestion for the terminology that
> we can all agree on I'm happy to change it.

As you will see in my reply to Uwe. The term I tend to adopt when I want
to be precise about userspace behaviour is "perceptual" (e.g. that a
backlight can be mapped directly to a slider and it will feel right).

However that raises its own concerns: mostly about what is perceptual
enough.

Clear the automatic brightness curve support in the PWM driver is
perceptual.

To be honest I suspect that in most cases a true logarithmic curve (given a
sane exponent) would be perceptual enough. In other words it will feel
comfortable with a direct mapped slider and using it for animation
won't be too bad.

However when we get right down to it *that* is the information that is
actually most useful to userspace: explicit confirmation that the scale
can be mapped directly to a slider. I think it also aligned better with
Uwe's feedback (e.g. to start working towards having a preferred scale).


Daniel.
Uwe Kleine-König Aug. 19, 2019, 10:21 a.m. UTC | #11
Hello Daniel,

On Mon, Aug 19, 2019 at 10:50:37AM +0100, Daniel Thompson wrote:
> On Mon, Aug 19, 2019 at 07:46:28AM +0200, Uwe Kleine-König wrote:
> > Hello Matthias,
> > 
> > On Fri, Aug 16, 2019 at 02:10:51PM -0700, Matthias Kaehlcke wrote:
> > > On Fri, Aug 16, 2019 at 09:47:54PM +0200, Uwe Kleine-König wrote:
> > > > On Fri, Aug 16, 2019 at 10:51:57AM -0700, Matthias Kaehlcke wrote:
> > > > > Hi Uwe,
> > > > > 
> > > > > On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> > > > > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > > > > Backlight brightness curves can have different shapes. The two main
> > > > > > > types are linear and non-linear curves. The human eye doesn't
> > > > > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > > > > (like the number of brightness levels) to decide whether to treat a
> > > > > > > backlight device as linear or non-linear.
> > > > > > > 
> > > > > > > Export the type of the brightness curve via the new sysfs attribute
> > > > > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > > > > 'unknown'. For devices that don't provide information about the scale
> > > > > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > > > > 
> > > > > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > > > > 
> > > > > > I wonder what kind of problem you are solving here. Can you describe
> > > > > > that in a few words?
> > > > > 
> > > > > The human eye perceives brightness in a logarithmic manner. For
> > > > > backlights with a linear brightness curve brightness controls like
> > > > > sliders need to use a mapping to achieve a behavior that is perceived
> > > > > as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)
> > > > > 
> > > > > As of now userspace doesn't have information about the type of the
> > > > > brightness curve, and often uses heuristics to make a guess, which may
> > > > > be right most of the time, but not always. The new attribute eliminates
> > > > > the need to guess.
> > > > 
> > > > This is about backlights right? So the kernel provides to userspace an
> > > > interval [0, x] for some x and depending on the physics of the the
> > > > backlight configuring x/2 (probably?) either means 50% measured light or
> > > > 50% perceived light, right?
> > > 
> > > correct
> > > 
> > > > I wonder if it would be possible instead of giving different backlight
> > > > implementations the freedom to use either linear or logarithmic (or
> > > > quadratic?) scaling and tell userspace which of the options were picked
> > > > require the drivers to provide a (say) linear scaling and then userspace
> > > > wouldn't need to care about the exact physics.
> > > 
> > > In an ideal world the backlight interface would be consistent as you
> > > suggest, however there are plenty of existing devices which use the
> > > 'other' scaling (regardless of which is chosen as the 'correct'
> > > one). Userspace still has to deal with these. And changing previously
> > > 'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
> > > when it keeps using its 'old' scaling, which now isn't correct anymore.
> > 
> > It might be subjective, or maybe I'm just too optimistic, but I think if
> > there was no policy before about the meaning of
> > 
> > 	echo 17 > brightness
> > 
> > other than "brighter than lower values and darker than higher ones"
> > introducing (say) the scale is intended to represent a linear brightness
> > curve is ok.
> > 
> > Unless userspace jumps through hoops and tries to identify the actual
> > device it is running on it is wrong on some machines anyhow and we're
> > only shifting the set of affected machines with a tighter policy (until
> > that userspace application is fixed).
> 
> I believe that there are two common approaches by userspace at present:
> 
> 1. Assume the scale is perceptual and we can directly map a slider
>    to the backlight value. This is common simply because most ACPI
>    backlights are perceptual and therefore when tested in a laptop
>    it works OK.
> 
> 2. Assume that is max brightness is small (e.g. ACPI) then the
>    scale is perceptual and if the max brightness is large (e.g.
>    a PWM) then the scale is linear and apply a correction
>    function between the slider and the control.
> 
> That historic baggage makes is diffcult to "just define a standardized
> scale"... especially given that if we selected a standardized scale we
> would probably want a perceptual scale with lots of steps (e.g. break
> the heuristic).

With "perceptual" you mean that logarithmic stuff, right?

I would tend to go for linear because this is easily measureable and
also is straight forward to implement in the usual cases (attention: I
assume that "usual" means something like PWM and I don't know much about
the physics of backlights but just assume that a PWM will create a
linear mapping).

> > And the big upside is that in the end (i.e. when all kernel drivers and
> > userspace applications are adapted to provide/consume the "correct"
> > curve) the result is simpler.
> 
> My view is that this convergence will eventually be achieved but it will
> happen through the obsolescence of the backlight sysfs interface. The
> sysfs interface has other flaws, in particular no integration with the
> DRM connector API.
> 
> Thus I would expect an alternative interface to emerge, most likely as
> part of the DRM connector API. I'd expect such a new API to a
> perceptual scale and to have a fixed max brightness with enough
> steps to support animated backlight effects (IIRC 0..100 has been
> proposed in the past) 

Then work on the new stuff instead of making the old stuff (that is
intended to die) harder to use correctly?

> In the mean time getting the existing collection of backlight drivers
> marked up as linear/logarithmic/etc will ease the introduction of that
> API because, within the kernel, we might have gathered enough knowledge
> to have some hope of correctly mapping each backlight onto a
> standardized scale.

It would be enough to do this in a code comment then. That would come
without the need to adapt the old userspace API. Also when the old
solution works at 95% instead of 90% before, it will resist harder to
dying.

Best regards
Uwe
Daniel Thompson Aug. 19, 2019, 11:16 a.m. UTC | #12
On Mon, Aug 19, 2019 at 12:21:27PM +0200, Uwe Kleine-König wrote:
> > > > In an ideal world the backlight interface would be consistent as you
> > > > suggest, however there are plenty of existing devices which use the
> > > > 'other' scaling (regardless of which is chosen as the 'correct'
> > > > one). Userspace still has to deal with these. And changing previously
> > > > 'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
> > > > when it keeps using its 'old' scaling, which now isn't correct anymore.
> > > 
> > > It might be subjective, or maybe I'm just too optimistic, but I think if
> > > there was no policy before about the meaning of
> > > 
> > > 	echo 17 > brightness
> > > 
> > > other than "brighter than lower values and darker than higher ones"
> > > introducing (say) the scale is intended to represent a linear brightness
> > > curve is ok.
> > > 
> > > Unless userspace jumps through hoops and tries to identify the actual
> > > device it is running on it is wrong on some machines anyhow and we're
> > > only shifting the set of affected machines with a tighter policy (until
> > > that userspace application is fixed).
> > 
> > I believe that there are two common approaches by userspace at present:
> > 
> > 1. Assume the scale is perceptual and we can directly map a slider
> >    to the backlight value. This is common simply because most ACPI
> >    backlights are perceptual and therefore when tested in a laptop
> >    it works OK.
> > 
> > 2. Assume that is max brightness is small (e.g. ACPI) then the
> >    scale is perceptual and if the max brightness is large (e.g.
> >    a PWM) then the scale is linear and apply a correction
> >    function between the slider and the control.
> > 
> > That historic baggage makes is diffcult to "just define a standardized
> > scale"... especially given that if we selected a standardized scale we
> > would probably want a perceptual scale with lots of steps (e.g. break
> > the heuristic).
> 
> With "perceptual" you mean that logarithmic stuff, right?

Human perception is fairly complex so it depends how strict you want to
get. At the end of the day what it means is you can map a slider UI
component directly to the backlight range and it will feel right. Thus
a userspace that maps directly to a slider *is* assuming the scale
is perceptual.

Logarithmic scales are often mentioned in this kind of conversation
because they are hoped to be "perceptual enough". They are not strictly
matched to human perception but with a sane exponent shouldn't feel
as broken as a linear scale does.

Finally note that for people with an audio background then logarithmic
and perceptual are used interchangably (based on the assumption that
decibels are a preceptual scale).


> I would tend to go for linear because this is easily measureable and
> also is straight forward to implement in the usual cases (attention: I
> assume that "usual" means something like PWM and I don't know much about
> the physics of backlights but just assume that a PWM will create a
> linear mapping).

Mandating a linear scale makes things difficult for ACPI systems (which
are amoung the most common cases) since we would have to un-perceptualize
it. Additionally it is not a particularly helpful scale to offer the
userspace.

All proposals I have seen in this space assume we will map linear
hardware to a perceptual scale rather than trying to unmap devices that
are already perceptual.

Within the backlight space we are also moving (slowly) towards
perceptual scales to align with this. For example pwm_bl.c will
automatically generate a perceptual table unless the DT tells it
not to.


> > > And the big upside is that in the end (i.e. when all kernel drivers and
> > > userspace applications are adapted to provide/consume the "correct"
> > > curve) the result is simpler.
> > 
> > My view is that this convergence will eventually be achieved but it will
> > happen through the obsolescence of the backlight sysfs interface. The
> > sysfs interface has other flaws, in particular no integration with the
> > DRM connector API.
> > 
> > Thus I would expect an alternative interface to emerge, most likely as
> > part of the DRM connector API. I'd expect such a new API to a
> > perceptual scale and to have a fixed max brightness with enough
> > steps to support animated backlight effects (IIRC 0..100 has been
> > proposed in the past) 
> 
> Then work on the new stuff instead of making the old stuff (that is
> intended to die) harder to use correctly?

I do not accept this this change makes it harder to use the backlight
correctly.

We know the current userspace heuristics are fragile and that as
backlight animation becomes more popular will become unmaintainable
(backlight animation encourages backlights with a perceptual scale to
introduce more steps... resulting in them being mishandled by
userspace).

Anyhow given we know sophisticated userspaces already have two code
paths controls by heuristic and that this will break then we are
simply providing a clear indication that allows their maintainers a
non-heuristic solution when bug reports come in. It also means that
userspaces that are currently naive (e.g. assume perceptual scale) need
not introduce a heuristic if they get a bug report.


> > In the mean time getting the existing collection of backlight drivers
> > marked up as linear/logarithmic/etc will ease the introduction of that
> > API because, within the kernel, we might have gathered enough knowledge
> > to have some hope of correctly mapping each backlight onto a
> > standardized scale.
> 
> It would be enough to do this in a code comment then. That would come
> without the need to adapt the old userspace API. Also when the old
> solution works at 95% instead of 90% before, it will resist harder to
> dying.

To be honest I think the traction for the sysfs interface comes from
the fact that, with or without the proposed change, it is good enough
for the ACPI systems that dominate the landscape.


Daniel.
Uwe Kleine-König Aug. 19, 2019, 12:29 p.m. UTC | #13
On Mon, Aug 19, 2019 at 12:16:13PM +0100, Daniel Thompson wrote:
> On Mon, Aug 19, 2019 at 12:21:27PM +0200, Uwe Kleine-König wrote:
> > > > > In an ideal world the backlight interface would be consistent as you
> > > > > suggest, however there are plenty of existing devices which use the
> > > > > 'other' scaling (regardless of which is chosen as the 'correct'
> > > > > one). Userspace still has to deal with these. And changing previously
> > > > > 'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
> > > > > when it keeps using its 'old' scaling, which now isn't correct anymore.
> > > > 
> > > > It might be subjective, or maybe I'm just too optimistic, but I think if
> > > > there was no policy before about the meaning of
> > > > 
> > > > 	echo 17 > brightness
> > > > 
> > > > other than "brighter than lower values and darker than higher ones"
> > > > introducing (say) the scale is intended to represent a linear brightness
> > > > curve is ok.
> > > > 
> > > > Unless userspace jumps through hoops and tries to identify the actual
> > > > device it is running on it is wrong on some machines anyhow and we're
> > > > only shifting the set of affected machines with a tighter policy (until
> > > > that userspace application is fixed).
> > > 
> > > I believe that there are two common approaches by userspace at present:
> > > 
> > > 1. Assume the scale is perceptual and we can directly map a slider
> > >    to the backlight value. This is common simply because most ACPI
> > >    backlights are perceptual and therefore when tested in a laptop
> > >    it works OK.
> > > 
> > > 2. Assume that is max brightness is small (e.g. ACPI) then the
> > >    scale is perceptual and if the max brightness is large (e.g.
> > >    a PWM) then the scale is linear and apply a correction
> > >    function between the slider and the control.
> > > 
> > > That historic baggage makes is diffcult to "just define a standardized
> > > scale"... especially given that if we selected a standardized scale we
> > > would probably want a perceptual scale with lots of steps (e.g. break
> > > the heuristic).
> > 
> > With "perceptual" you mean that logarithmic stuff, right?
> 
> Human perception is fairly complex so it depends how strict you want to
> get. At the end of the day what it means is you can map a slider UI
> component directly to the backlight range and it will feel right. Thus
> a userspace that maps directly to a slider *is* assuming the scale
> is perceptual.

I have problems to declare something as "the right thing to do" that
depends on feeling of users. I much prefer to make a technical device
authoritative here (in this case a device that measures emitted light).

Other than that I don't have enough experience with backlights to judge
the decisions that have to be done and so will stop my participation in
this thread now.

Best regards
Uwe
Matthias Kaehlcke Aug. 19, 2019, 6:50 p.m. UTC | #14
Hi Daniel,

On Mon, Aug 19, 2019 at 11:02:41AM +0100, Daniel Thompson wrote:
> On Fri, Aug 16, 2019 at 10:53:17AM -0700, Matthias Kaehlcke wrote:
> > On Fri, Aug 16, 2019 at 04:54:18PM +0100, Daniel Thompson wrote:
> > > On 07/08/2019 21:15, Matthias Kaehlcke wrote:
> > > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > > Backlight brightness curves can have different shapes. The two main
> > > > > types are linear and non-linear curves. The human eye doesn't
> > > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > > (like the number of brightness levels) to decide whether to treat a
> > > > > backlight device as linear or non-linear.
> > > > > 
> > > > > Export the type of the brightness curve via the new sysfs attribute
> > > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > > 'unknown'. For devices that don't provide information about the scale
> > > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > > 
> > > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > > 
> > > > Daniel (et al): do you have any more comments on this patch/series or
> > > > is it ready to land?
> > > 
> > > I decided to leave it for a long while for others to review since I'm still
> > > a tiny bit uneasy about the linear/non-linear terminology.
> > > 
> > > However that's my only concern, its fairly minor and I've dragged by feet
> > > for more then long enough, so:
> > > Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
> > 
> > Thanks!
> > 
> > If you or someone else has another suggestion for the terminology that
> > we can all agree on I'm happy to change it.
> 
> As you will see in my reply to Uwe. The term I tend to adopt when I want
> to be precise about userspace behaviour is "perceptual" (e.g. that a
> backlight can be mapped directly to a slider and it will feel right).
> 
> However that raises its own concerns: mostly about what is perceptual
> enough.
> 
> Clear the automatic brightness curve support in the PWM driver is
> perceptual.
> 
> To be honest I suspect that in most cases a true logarithmic curve (given a
> sane exponent) would be perceptual enough. In other words it will feel
> comfortable with a direct mapped slider and using it for animation
> won't be too bad.
> 
> However when we get right down to it *that* is the information that is
> actually most useful to userspace: explicit confirmation that the scale
> can be mapped directly to a slider. I think it also aligned better with
> Uwe's feedback (e.g. to start working towards having a preferred scale).

IIUC the conclusion is that there is no need for a string attribute
because we only need to distinguish between 'perceptual' and
'non-perceptual'. If that is correct, do you have any preference for
the attribute name ('perceptual_scale', 'perceptual', ...)?
Daniel Thompson Aug. 20, 2019, 1:56 p.m. UTC | #15
On Mon, Aug 19, 2019 at 11:50:49AM -0700, Matthias Kaehlcke wrote:
> Hi Daniel,
> 
> On Mon, Aug 19, 2019 at 11:02:41AM +0100, Daniel Thompson wrote:
> > On Fri, Aug 16, 2019 at 10:53:17AM -0700, Matthias Kaehlcke wrote:
> > > On Fri, Aug 16, 2019 at 04:54:18PM +0100, Daniel Thompson wrote:
> > > > On 07/08/2019 21:15, Matthias Kaehlcke wrote:
> > > > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > > > Backlight brightness curves can have different shapes. The two main
> > > > > > types are linear and non-linear curves. The human eye doesn't
> > > > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > > > (like the number of brightness levels) to decide whether to treat a
> > > > > > backlight device as linear or non-linear.
> > > > > > 
> > > > > > Export the type of the brightness curve via the new sysfs attribute
> > > > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > > > 'unknown'. For devices that don't provide information about the scale
> > > > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > > > 
> > > > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > > > 
> > > > > Daniel (et al): do you have any more comments on this patch/series or
> > > > > is it ready to land?
> > > > 
> > > > I decided to leave it for a long while for others to review since I'm still
> > > > a tiny bit uneasy about the linear/non-linear terminology.
> > > > 
> > > > However that's my only concern, its fairly minor and I've dragged by feet
> > > > for more then long enough, so:
> > > > Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
> > > 
> > > Thanks!
> > > 
> > > If you or someone else has another suggestion for the terminology that
> > > we can all agree on I'm happy to change it.
> > 
> > As you will see in my reply to Uwe. The term I tend to adopt when I want
> > to be precise about userspace behaviour is "perceptual" (e.g. that a
> > backlight can be mapped directly to a slider and it will feel right).
> > 
> > However that raises its own concerns: mostly about what is perceptual
> > enough.
> > 
> > Clear the automatic brightness curve support in the PWM driver is
> > perceptual.
> > 
> > To be honest I suspect that in most cases a true logarithmic curve (given a
> > sane exponent) would be perceptual enough. In other words it will feel
> > comfortable with a direct mapped slider and using it for animation
> > won't be too bad.
> > 
> > However when we get right down to it *that* is the information that is
> > actually most useful to userspace: explicit confirmation that the scale
> > can be mapped directly to a slider. I think it also aligned better with
> > Uwe's feedback (e.g. to start working towards having a preferred scale).
> 
> IIUC the conclusion is that there is no need for a string attribute
> because we only need to distinguish between 'perceptual' and
> 'non-perceptual'. If that is correct, do you have any preference for
> the attribute name ('perceptual_scale', 'perceptual', ...)?

More a summary than a conclusion! There is a reason I have left a bit or
space for others to comment on this over the last month (and a bit).

To be clear my Reviewed-by: means that I believe that the kernel is better
with "non-linear/linear/unknown" than without it and that I am comfortable
the API isn't likely to be a millstone for us.

Lee, Jingoo: Either of you care to offer $0.02


Daniel.
Daniel Vetter Aug. 20, 2019, 2:49 p.m. UTC | #16
On Mon, Aug 19, 2019 at 11:50 AM Daniel Thompson
<daniel.thompson@linaro.org> wrote:
>
> On Mon, Aug 19, 2019 at 07:46:28AM +0200, Uwe Kleine-König wrote:
> > Hello Matthias,
> >
> > On Fri, Aug 16, 2019 at 02:10:51PM -0700, Matthias Kaehlcke wrote:
> > > On Fri, Aug 16, 2019 at 09:47:54PM +0200, Uwe Kleine-König wrote:
> > > > On Fri, Aug 16, 2019 at 10:51:57AM -0700, Matthias Kaehlcke wrote:
> > > > > Hi Uwe,
> > > > >
> > > > > On Fri, Aug 16, 2019 at 06:51:48PM +0200, Uwe Kleine-König wrote:
> > > > > > On Tue, Jul 09, 2019 at 12:00:05PM -0700, Matthias Kaehlcke wrote:
> > > > > > > Backlight brightness curves can have different shapes. The two main
> > > > > > > types are linear and non-linear curves. The human eye doesn't
> > > > > > > perceive linearly increasing/decreasing brightness as linear (see
> > > > > > > also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> > > > > > > linearly to human eye"), hence many backlights use non-linear (often
> > > > > > > logarithmic) brightness curves. The type of curve currently is opaque
> > > > > > > to userspace, so userspace often uses more or less reliable heuristics
> > > > > > > (like the number of brightness levels) to decide whether to treat a
> > > > > > > backlight device as linear or non-linear.
> > > > > > >
> > > > > > > Export the type of the brightness curve via the new sysfs attribute
> > > > > > > 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> > > > > > > 'unknown'. For devices that don't provide information about the scale
> > > > > > > of their brightness curve the value of the 'scale' attribute is 'unknown'.
> > > > > > >
> > > > > > > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > > > > >
> > > > > > I wonder what kind of problem you are solving here. Can you describe
> > > > > > that in a few words?
> > > > >
> > > > > The human eye perceives brightness in a logarithmic manner. For
> > > > > backlights with a linear brightness curve brightness controls like
> > > > > sliders need to use a mapping to achieve a behavior that is perceived
> > > > > as linear-ish (more details: http://www.pathwaylighting.com/products/downloads/brochure/technical_materials_1466797044_Linear+vs+Logarithmic+Dimming+White+Paper.pdf)
> > > > >
> > > > > As of now userspace doesn't have information about the type of the
> > > > > brightness curve, and often uses heuristics to make a guess, which may
> > > > > be right most of the time, but not always. The new attribute eliminates
> > > > > the need to guess.
> > > >
> > > > This is about backlights right? So the kernel provides to userspace an
> > > > interval [0, x] for some x and depending on the physics of the the
> > > > backlight configuring x/2 (probably?) either means 50% measured light or
> > > > 50% perceived light, right?
> > >
> > > correct
> > >
> > > > I wonder if it would be possible instead of giving different backlight
> > > > implementations the freedom to use either linear or logarithmic (or
> > > > quadratic?) scaling and tell userspace which of the options were picked
> > > > require the drivers to provide a (say) linear scaling and then userspace
> > > > wouldn't need to care about the exact physics.
> > >
> > > In an ideal world the backlight interface would be consistent as you
> > > suggest, however there are plenty of existing devices which use the
> > > 'other' scaling (regardless of which is chosen as the 'correct'
> > > one). Userspace still has to deal with these. And changing previously
> > > 'logarithmic' drivers to linear (or viceversa) may 'break' userspace,
> > > when it keeps using its 'old' scaling, which now isn't correct anymore.
> >
> > It might be subjective, or maybe I'm just too optimistic, but I think if
> > there was no policy before about the meaning of
> >
> >       echo 17 > brightness
> >
> > other than "brighter than lower values and darker than higher ones"
> > introducing (say) the scale is intended to represent a linear brightness
> > curve is ok.
> >
> > Unless userspace jumps through hoops and tries to identify the actual
> > device it is running on it is wrong on some machines anyhow and we're
> > only shifting the set of affected machines with a tighter policy (until
> > that userspace application is fixed).
>
> I believe that there are two common approaches by userspace at present:
>
> 1. Assume the scale is perceptual and we can directly map a slider
>    to the backlight value. This is common simply because most ACPI
>    backlights are perceptual and therefore when tested in a laptop
>    it works OK.
>
> 2. Assume that is max brightness is small (e.g. ACPI) then the
>    scale is perceptual and if the max brightness is large (e.g.
>    a PWM) then the scale is linear and apply a correction
>    function between the slider and the control.
>
> That historic baggage makes is diffcult to "just define a standardized
> scale"... especially given that if we selected a standardized scale we
> would probably want a perceptual scale with lots of steps (e.g. break
> the heuristic).
>
>
> > And the big upside is that in the end (i.e. when all kernel drivers and
> > userspace applications are adapted to provide/consume the "correct"
> > curve) the result is simpler.
>
> My view is that this convergence will eventually be achieved but it will
> happen through the obsolescence of the backlight sysfs interface. The
> sysfs interface has other flaws, in particular no integration with the
> DRM connector API.
>
> Thus I would expect an alternative interface to emerge, most likely as
> part of the DRM connector API. I'd expect such a new API to a
> perceptual scale and to have a fixed max brightness with enough
> steps to support animated backlight effects (IIRC 0..100 has been
> proposed in the past)
>
> In the mean time getting the existing collection of backlight drivers
> marked up as linear/logarithmic/etc will ease the introduction of that
> API because, within the kernel, we might have gathered enough knowledge
> to have some hope of correctly mapping each backlight onto a
> standardized scale.

In case people wonder why the drm connector based backlight interface
hasn't happened ages ago, some more context:

- userspace (well libbacklight) selects the right backlight, using
some priority search. Plus blacklists in drivers to make sure they're
not overriding the real backlight driver (e.g. acpi has higher
priority in libbacklight, but on modern system it's not the backlight
driver you want. If we move that into the kernel it's going to be
somewhat a mess, since defacto you never know when loading is complete
and you actually have the right backlight driver.

This isn't a problem on DT platforms, but really just for x86/acpi
platforms. But if we don't fix them, then userspace adoption of these
new interfaces will likely be too low to matter.

- second issue is that right now the kms client is supposed to handle
backlight around modeset, like fbdev does through the fb notifier.
Except for drivers which do handle the backlight across modesets, but
maybe not the right backlight. If we move the backlight interface to
drm connectors then the right thing would be for the drm driver to
handle backlight enable/disable across modesets. But to make that
work, userspace needs to stop touching it (otherwise userspace first
disables, then the kernel and then on restore the two fight and
usually black screen wins), and that's a bit a tricky uapi problem of
not breaking existing userspace.

- finally there's some userspace which assumes the lowest backlight
setting is actually off, and uses that to do fast modesets. This
doesn't work on most ACPI backlights, so I think that problem isn't
widespread.

Anyway from watching from afar, I think this clarification on what the
backlight scale means internally should at least help us somewhat in
the long term. But the long term solution itself needs someone with
way too much time I fear, so lets not hold up anything on that.
-Daniel
Daniel Thompson Aug. 21, 2019, 2:16 p.m. UTC | #17
On Tue, Aug 20, 2019 at 04:49:21PM +0200, Daniel Vetter wrote:
> On Mon, Aug 19, 2019 at 11:50 AM Daniel Thompson
> <daniel.thompson@linaro.org> wrote:
> > On Mon, Aug 19, 2019 at 07:46:28AM +0200, Uwe Kleine-König wrote:
> > > And the big upside is that in the end (i.e. when all kernel drivers and
> > > userspace applications are adapted to provide/consume the "correct"
> > > curve) the result is simpler.
> >
> > My view is that this convergence will eventually be achieved but it will
> > happen through the obsolescence of the backlight sysfs interface. The
> > sysfs interface has other flaws, in particular no integration with the
> > DRM connector API.
> >
> > Thus I would expect an alternative interface to emerge, most likely as
> > part of the DRM connector API. I'd expect such a new API to a
> > perceptual scale and to have a fixed max brightness with enough
> > steps to support animated backlight effects (IIRC 0..100 has been
> > proposed in the past)
> >
> > In the mean time getting the existing collection of backlight drivers
> > marked up as linear/logarithmic/etc will ease the introduction of that
> > API because, within the kernel, we might have gathered enough knowledge
> > to have some hope of correctly mapping each backlight onto a
> > standardized scale.
> 
> In case people wonder why the drm connector based backlight interface
> hasn't happened ages ago, some more context:
> 
> - userspace (well libbacklight) selects the right backlight, using
> some priority search. Plus blacklists in drivers to make sure they're
> not overriding the real backlight driver (e.g. acpi has higher
> priority in libbacklight, but on modern system it's not the backlight
> driver you want. If we move that into the kernel it's going to be
> somewhat a mess, since defacto you never know when loading is complete
> and you actually have the right backlight driver.
> 
> This isn't a problem on DT platforms, but really just for x86/acpi
> platforms. But if we don't fix them, then userspace adoption of these
> new interfaces will likely be too low to matter.
> 
> - second issue is that right now the kms client is supposed to handle
> backlight around modeset, like fbdev does through the fb notifier.
> Except for drivers which do handle the backlight across modesets, but
> maybe not the right backlight. If we move the backlight interface to
> drm connectors then the right thing would be for the drm driver to
> handle backlight enable/disable across modesets. But to make that
> work, userspace needs to stop touching it (otherwise userspace first
> disables, then the kernel and then on restore the two fight and
> usually black screen wins), and that's a bit a tricky uapi problem of
> not breaking existing userspace.
> 
> - finally there's some userspace which assumes the lowest backlight
> setting is actually off, and uses that to do fast modesets. This
> doesn't work on most ACPI backlights, so I think that problem isn't
> widespread.
> 
> Anyway from watching from afar, I think this clarification on what the
> backlight scale means internally should at least help us somewhat in
> the long term. But the long term solution itself needs someone with
> way too much time I fear, so lets not hold up anything on that.

Thanks for sharing your views on this.


Daniel.
Lee Jones Aug. 27, 2019, 9:44 a.m. UTC | #18
[...]

> > IIUC the conclusion is that there is no need for a string attribute
> > because we only need to distinguish between 'perceptual' and
> > 'non-perceptual'. If that is correct, do you have any preference for
> > the attribute name ('perceptual_scale', 'perceptual', ...)?
> 
> More a summary than a conclusion! There is a reason I have left a bit or
> space for others to comment on this over the last month (and a bit).
> 
> To be clear my Reviewed-by: means that I believe that the kernel is better
> with "non-linear/linear/unknown" than without it and that I am comfortable
> the API isn't likely to be a millstone for us.
> 
> Lee, Jingoo: Either of you care to offer $0.02

No, not really.  Happy to leave it to your good judgement.
Daniel Thompson Aug. 29, 2019, 2:09 p.m. UTC | #19
On Tue, Aug 27, 2019 at 10:44:59AM +0100, Lee Jones wrote:
> [...]
> 
> > > IIUC the conclusion is that there is no need for a string attribute
> > > because we only need to distinguish between 'perceptual' and
> > > 'non-perceptual'. If that is correct, do you have any preference for
> > > the attribute name ('perceptual_scale', 'perceptual', ...)?
> > 
> > More a summary than a conclusion! There is a reason I have left a bit or
> > space for others to comment on this over the last month (and a bit).
> > 
> > To be clear my Reviewed-by: means that I believe that the kernel is better
> > with "non-linear/linear/unknown" than without it and that I am comfortable
> > the API isn't likely to be a millstone for us.
> > 
> > Lee, Jingoo: Either of you care to offer $0.02
> 
> No, not really.  Happy to leave it to your good judgement.

In that case... the patch has my reviewed-by and although the concerns
raised in the associated threads have merit I don't think they are
enough to stop the patch.

Please merge.


Daniel.

> 
> -- 
> Lee Jones [李琼斯]
> Linaro Services Technical Lead
> Linaro.org │ Open source software for ARM SoCs
> Follow Linaro: Facebook | Twitter | Blog
Lee Jones Sept. 2, 2019, 9:41 a.m. UTC | #20
On Tue, 09 Jul 2019, Matthias Kaehlcke wrote:

> Backlight brightness curves can have different shapes. The two main
> types are linear and non-linear curves. The human eye doesn't
> perceive linearly increasing/decreasing brightness as linear (see
> also 88ba95bedb79 "backlight: pwm_bl: Compute brightness of LED
> linearly to human eye"), hence many backlights use non-linear (often
> logarithmic) brightness curves. The type of curve currently is opaque
> to userspace, so userspace often uses more or less reliable heuristics
> (like the number of brightness levels) to decide whether to treat a
> backlight device as linear or non-linear.
> 
> Export the type of the brightness curve via the new sysfs attribute
> 'scale'. The value of the attribute can be 'linear', 'non-linear' or
> 'unknown'. For devices that don't provide information about the scale
> of their brightness curve the value of the 'scale' attribute is 'unknown'.
> 
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
> Feel free to suggest improvements in the documentation :)
> 
> Changes in v3:
> - removed composite strings, only keep 'linear', 'non-linear' and
>   'unknown'
> - updated sysfs attribute documentation
> - updated commit message
> 
> Changes in v2:
> - changed order of brightness scale enums, explicitly make 'unknown' zero
> - minor update of commit message
> - deleted excess blank line after 'backlight_scale_types'
> - s/curves/curve/ in sysfs doc
> ---
>  .../ABI/testing/sysfs-class-backlight         | 26 +++++++++++++++++++
>  MAINTAINERS                                   |  1 +
>  drivers/video/backlight/backlight.c           | 19 ++++++++++++++
>  include/linux/backlight.h                     |  8 ++++++
>  4 files changed, 54 insertions(+)
>  create mode 100644 Documentation/ABI/testing/sysfs-class-backlight

Applied, thanks.
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-class-backlight b/Documentation/ABI/testing/sysfs-class-backlight
new file mode 100644
index 000000000000..3ab175a3f5cb
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-backlight
@@ -0,0 +1,26 @@ 
+What:		/sys/class/backlight/<backlight>/scale
+Date:		July 2019
+KernelVersion:	5.4
+Contact:	Daniel Thompson <daniel.thompson@linaro.org>
+Description:
+		Description of the scale of the brightness curve.
+
+		The human eye senses brightness approximately logarithmically,
+		hence linear changes in brightness are perceived as being
+		non-linear. To achieve a linear perception of brightness changes
+		controls like sliders need to apply a logarithmic mapping for
+		backlights with a linear brightness curve.
+
+		Possible values of the attribute are:
+
+		unknown
+		  The scale of the brightness curve is unknown.
+
+		linear
+		  The brightness changes linearly with each step. Brightness
+		  controls should apply a logarithmic mapping for a linear
+		  perception.
+
+		non-linear
+		  The brightness changes non-linearly with each step. Brightness
+		  controls should use a linear mapping for a linear perception.
diff --git a/MAINTAINERS b/MAINTAINERS
index d51e74340870..c46812510ba5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2858,6 +2858,7 @@  F:	include/linux/backlight.h
 F:	include/linux/pwm_backlight.h
 F:	Documentation/devicetree/bindings/leds/backlight
 F:	Documentation/ABI/stable/sysfs-class-backlight
+F:	Documentation/ABI/testing/sysfs-class-backlight
 
 BATMAN ADVANCED
 M:	Marek Lindner <mareklindner@neomailbox.ch>
diff --git a/drivers/video/backlight/backlight.c b/drivers/video/backlight/backlight.c
index 1ef8b6fd62ac..277abc76c83a 100644
--- a/drivers/video/backlight/backlight.c
+++ b/drivers/video/backlight/backlight.c
@@ -32,6 +32,12 @@  static const char *const backlight_types[] = {
 	[BACKLIGHT_FIRMWARE] = "firmware",
 };
 
+static const char *const backlight_scale_types[] = {
+	[BACKLIGHT_SCALE_UNKNOWN]	= "unknown",
+	[BACKLIGHT_SCALE_LINEAR]	= "linear",
+	[BACKLIGHT_SCALE_NON_LINEAR]	= "non-linear",
+};
+
 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
 			   defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
 /* This callback gets called when something important happens inside a
@@ -246,6 +252,18 @@  static ssize_t actual_brightness_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(actual_brightness);
 
+static ssize_t scale_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct backlight_device *bd = to_backlight_device(dev);
+
+	if (WARN_ON(bd->props.scale > BACKLIGHT_SCALE_NON_LINEAR))
+		return sprintf(buf, "unknown\n");
+
+	return sprintf(buf, "%s\n", backlight_scale_types[bd->props.scale]);
+}
+static DEVICE_ATTR_RO(scale);
+
 static struct class *backlight_class;
 
 #ifdef CONFIG_PM_SLEEP
@@ -292,6 +310,7 @@  static struct attribute *bl_device_attrs[] = {
 	&dev_attr_brightness.attr,
 	&dev_attr_actual_brightness.attr,
 	&dev_attr_max_brightness.attr,
+	&dev_attr_scale.attr,
 	&dev_attr_type.attr,
 	NULL,
 };
diff --git a/include/linux/backlight.h b/include/linux/backlight.h
index 0b5897446dca..c7d6b2e8c3b5 100644
--- a/include/linux/backlight.h
+++ b/include/linux/backlight.h
@@ -46,6 +46,12 @@  enum backlight_notification {
 	BACKLIGHT_UNREGISTERED,
 };
 
+enum backlight_scale {
+	BACKLIGHT_SCALE_UNKNOWN = 0,
+	BACKLIGHT_SCALE_LINEAR,
+	BACKLIGHT_SCALE_NON_LINEAR,
+};
+
 struct backlight_device;
 struct fb_info;
 
@@ -80,6 +86,8 @@  struct backlight_properties {
 	enum backlight_type type;
 	/* Flags used to signal drivers of state changes */
 	unsigned int state;
+	/* Type of the brightness scale (linear, non-linear, ...) */
+	enum backlight_scale scale;
 
 #define BL_CORE_SUSPENDED	(1 << 0)	/* backlight is suspended */
 #define BL_CORE_FBBLANK		(1 << 1)	/* backlight is under an fb blank event */