Backlight dimming ramps do not use a constant duration
I am wondering about a peculiar "perceived" (and now proven) imbalance about when dimming occurs in UT. It bugged me for a while, but now I took this apart.
So the code basically calculates:
Slow ramp: With a step size of 0.01% per step, and a total duration of 100000 microseconds, how many steps at which duration do I need to do for getting the job done in one second? Fast ramp: With a step size of 0.01% per step, and a fixed step duration of 1000 microseconds, just calculate how many steps are needed - time will vary. Here is an example:
Dimming from 73% to 0%:
2022-03-01T21:41:14.325910+01:00 ubuntu-phablet repowerd[1553]: BacklightBrightnessControl: Transitioning brightness 0.73 => 0.00 in 73.00 steps 1369.86us each
2022-03-01T21:41:15.541909+01:00 ubuntu-phablet repowerd[1553]: BacklightBrightnessControl: Transitioning brightness 0.73 => 0.00 done
The difference of the timestamps is 1.216 seconds. Okay. What is that error with the seconds though. We wanted 73 x 1369.86us time which indeed is 100000us BUT we get more. Why is that? Because calling into the Android lights API takes time, and the code does not account for that. Instead of a sleep_for() it would need to sleep_until() after the value has been set. So, first fix needed here.
Dimming from 34% to 0%:
2022-03-01T21:50:03.303807+01:00 ubuntu-phablet repowerd[1553]: BacklightBrightnessControl: Transitioning brightness 0.34 => 0.00 in 35.00 steps 2857.14us each
2022-03-01T21:50:03.880754+01:00 ubuntu-phablet repowerd[1553]: BacklightBrightnessControl: Transitioning brightness 0.34 => 0.00 done
This time the difference is only 0.57695 seconds! Amazing. Again, 35 x 2857,14us is 100000us which is 1 second. But, alas, we finish much earlier. This needs analysis, maybe conversion to nanoseconds is wrong or idk yet.
Bonus question: What happens when we manually move the brightness slider?
2022-03-01T21:49:53.248079+01:00 ubuntu-phablet repowerd[1553]: BacklightBrightnessControl: Transitioning brightness 0.73 => 0.34 in 39.00 steps 1000.00us each
2022-03-01T21:49:53.887392+01:00 ubuntu-phablet repowerd[1553]: BacklightBrightnessControl: Transitioning brightness 0.73 => 0.34 done
Well this is also strange: The code has in this case a constant for a step duration: Means the higher the difference between 2 brightness values is the longer it will take to accomodate the change. One can observe this when going crazy on the brightness slider. The ramping cannot keep up with your fingers and then it wildly jumps around
How to fix it:
- For better clarity, set for both cases (dim to/from 0% or between 2 brightness values) a constant time: 0.25 second for the first case, 0.001 seconds for the latter.
- Do not sleep between steps in the brightness curve: Instead, use step size adaption to pack as many steps as possible into the alloted time slice.