FORUMS
Remove All Ads from XDA

[MOD][Kernel] K-Lapse : A kernel-level livedisplay module [GPLv2]

1,809 posts
Thanks Meter: 2,185
 
Post Reply Email Thread
Announcement from tanish2k09: Franco Kernel Manager now supports KLapse!

Klapse-logo
K-Lapse : A kernel level livedisplay module


Intro - What is K-Lapse?

Kernel-based Lapse ("K-Lapse") is a linear RGB scaling module that 'shifts' RGB based on time (of the day/selected by the user), or (since v2.0) brightness. This concept is inspired by LineageOS (formerly known as 'CyanogenMod') ROM's feature "Livedisplay" which also changes the display settings (RGB, hue, temperature, etc) based on time. This is very very similar to f.lux for desktop too.

Why did I decide to make this? (A short story)

I (personally) am a big fan of the Livedisplay feature found on LineageOS. I used it every single day, since Android Lollipop. Starting from Android Nougat, a native night mode solution was added to AOSP and it felt like Livedisplay was still way superior, thanks to its various options (you could say it spoiled me, sure). I also maintained a kernel (Venom kernel) for the device I was using at that time. It was all good until the OEM dropped support for the device at Android M, and XDA being XDA, was already working on N ROMs. The issue was, these ROMs weren't LineageOS or based on it, so Livedisplay was... gone. I decided I'll try to bring that feature to every other ROM. How would I do that? Of course! The kernel! It worked on every single ROM, it was the key! I started to work on it ASAP and here it is, up on GitHub, licensed under GPL (check klapse.c), open to everyone

How does it work?

Think of it like a fancy night mode, but not really. Klapse is dependent on an RGB interface (like Gamma on MTK and KCAL on SD chipsets). In mode 1, it fetches time from the kernel, converts it to local time, and selects and RGB set based on the time. The result is really smooth shifting of RGB over time. Mode 2 uses the current brightness level to scale RGB, with the concept behind it being that lower brightness usually implies a dark environment, so a slight color temperature shift should help with eye strain.

There's also an option for a "brightness factor" that can reduce your brightness down to 80% below the minimum brightness that your phone allows. The catch is, it doesn't actually reduce the brightness, but rather uses a clever trick to fade away the RGB of the screen by the same amount so it "appears" to be lower brightness.

How does it really work (dev)?

Klapse mode 1 (time-based scaling) uses a method void klapse_pulse(unsigned long data) that should ideally be called every minute. This is done using a kernel timer, that is asynchronous so it should be handled with care, which I did. The pulse function fetches the current time and makes calculations based on the current hour and the values of the tunables listed down below.

Klapse mode 2 (brightness-based scaling) uses a method void set_rgb_slider(<type> bl_lvl) where type is the data type of the brightness level used in your kernel source. (OnePlus 6 uses u32 data type for bl_lvl) set_rgb_slider needs to be called/injected inside a function that sets brightness for your device. (OnePlus 6 uses dsi_panel.c for that, check out the diff for that file in op6 branch)

What all stuff can it do?
  1. Emulate night mode with the proper RGB settings
  2. Smoothly scale from one set of RGB to another set of RGB in integral intervals over time.
  3. Reduce perceived brightness using brightness_factor by reducing the amount of color on screen. Allows lower apparent brightness than system permits.
  4. Scale RGB based on brightness of display (low brightness usually implies a dark environment, where yellowness is probably useful).
  5. Automate the perceived brightness independent of whether klapse is enabled, using its own set of start and stop hours.
  6. Theoretically more efficient, faster by residing inside the kernel instead of having to use the HWC HAL like android's night mode. This is unproven and probably has no practical significance.
  7. (On older devices) Reduce stuttering or frame lags caused by native night mode.
  8. An easier solution against overlay-based apps that run as service in userspace/Android and sometimes block apps asking for permissions.
  9. Give you a Livedisplay alternative if it doesn't work in your ROM.
  10. Impress your crush so you can get a date (Hey, don't forget to credit me if it works).

Alright, so this is a replacement for night mode?
NO! Kinda, but no. Lemme explain. One can say this is an alternative for LineageOS' Livedisplay, but inside a kernel. Night mode is a sub-function of both Livedisplay and KLapse. Most comparisons here were made with night mode because that's what an average user uses, and will relate to the most. There is absolutely no reason for your Android kernel to not have KLapse. Go ahead and add it or ask your kernel maintainer to. It's super-easy!

What can it NOT do (yet)?
  1. Calculate scaling to the level of minutes, like "Start from 5:37pm till 7:19am". --TODO
  2. Make coffee for you.
  3. Fly you to the moon.
  4. Get you a monthly subscription of free food, cereal and milk included.

I want more! Tell me what can I customize!

All these following tunables are found in their respective files in /sys/klapse/
Code:
1. enable_klapse : A switch to enable or disable klapse. Values : 0 = off, 1 = on (since v2.0, 2 = brightness-dependent mode)

2. klapse_start_hour : The hour at which klapse should start scaling the RGB values from daytime to target (see next points). Values : 0-23

3. klapse_stop_hour : The hour by which klapse should scale back the RGB values from target to daytime (see next points). Values : 0-23

4. daytime_r,g,b : The RGB set that must be used for all the time outside of start and stop hour range.

5. target_r,g,b : The RGB set that must be scaled towards for all the time inside of start and stop hour range.

6. klapse_scaling_rate : Controls how soon the RGB reaches from daytime to target inside of start and stop hour range. Once target is reached, it remains constant till fadeback_minutes (#13) before stop hour, where target RGB scales back to daytime RGB. (Pre-v4.2 value was a factor, now it is a minute value)

7. brightness_factor : From the name itself, this value has the ability to bend perception and make your display appear as if it is at a lesser brightness level than it actually is at. It works by reducing the RGB values by the same factor. Values : 2-10, (10 means accurate brightness, 5 means 50% of current brightness, you get it)

8. brightness_factor_auto : A switch that allows you to automatically set the brightness factor in a set time range. Value : 0 = off, 1 = on

9. brightness_factor_auto_start_hour : The hour at which brightness_factor should be applied. Works only if #8 is 1. Values : 0-23

10. brightness_factor_auto_stop_hour : The hour at which brightness_factor should be reverted to 10. Works only if #8 is 1. Values : 0-23

11. backlight_range : The brightness range within which klapse should scale from daytime to target_rgb. Works only if #1 is 2. Values : MIN_BRIGHTNESS-MAX_BRIGHTNESS

12. pulse_freq : The amount of milliseconds after which klapse_pulse is called. A more developer-targeted tunable. Only works when one or both of #1 and #8 are 1. Values : 1000-600000 (Represents 1sec to 10 minutes)

13. fadeback_minutes : The number of minutes before klapse_stop_hour when RGB should start going back to daytime_rgb. Only works when #1 is 1. Values : 0-minutes between #2 and #3
Impact on performance or battery...

Fortunately, as per practical testing there is absolutely no negative effect on performance or battery backup!

"I'm a kernel maintainer. How do I add it to my source?"

Note : I'm currently maintaining klapse for OnePlus6 (enchilada), using the snapshot branch.

The klapse.c file is pretty much generic, but depending on your device you may need to change some of the #define values
The klapse.h file should be edited in order to make the K_RED etc. defines point to the correct RGB interface variable. OnePlus 6 simply uses kcal_red, kcal_green and kcal_blue in sde. Some devices have a struct or pointers instead of a variable. Those devices must edit their kcal files to keep a copy of the address that klapse will access. An example of a source with struct-based kcal with klapse support is this: commit (thanks to @rupanshji for this commit)

The KCONFIG is pretty understandable too, but you may wanna remove the "DEPENDS" line for your device.
The Makefile is just one line, and so is enabling klapse in the defconfig.

Now you must change the file that provides the kcal/gamma (mtk) interface. Thanks to other developers, all I had to do on the OnePlus 6 was to remove the keyword "static" from the variable declaration.


Great work! Can I pay for your next meal?

I'm just a university CS student so sure, any amount is much appreciated! You can donate via PayPal here :
Donate




XDA:DevDB Information
K-Lapse, Kernel for all devices (see above for details)

Contributors
tanish2k09
Source Code: https://github.com/tanish2k09/KLapse-Livedisplay

Kernel Special Features: RGB shifting based on a context

Version Information
Status: Stable
Current Stable Version: 4.3
Stable Release Date: 2019-03-02

Created 2019-03-04
Last Updated 2019-03-19
The Following 95 Users Say Thank You to tanish2k09 For This Useful Post: [ View ] Gift tanish2k09 Ad-Free
 
 
4th March 2019, 02:01 AM |#2  
tanish2k09's Avatar
OP Senior Member
Thanks Meter: 2,185
 
Donate to Me
More
TODO :
  1. Add custom-minute support
  2. Add full-smooth scaling algorithm that actually scales from node to node to make the shift smoother
  3. Add a third "intelligent" mode that uses both time-based and brightness-based values to magically come up with an RGB set that's perfect for that environment
  4. A grayscale feature, that maybe also would support a yellow tint over it, so it's like a grayscale night mode.

Telegram support?
Yeah yeah, I know. Telegram links are common now. In compliance with the xda rule of "Only one TG link per thread" that I saw on some other sub-forums, maybe a link would be fine but mods can remove it per will.
Here's the official klapse telegram group, with commit notifications too:
t.me/klapse or from within telegram you can just join klapse but with an @ in front of it (I didn't use it to prevent an unintended mention to some person)

Notes :
I'm open to any other contructive feedback or suggestions
Klapse doesn't conflict with night mode of Android. They both can work TOGETHER, the result would be an overlap of both their colors.

Credits :
I pretty much wrote all the code for k-lapse myself, but it would be useless without these awesome people who put it to use -
@pappschlumpf for getting k-lapse working for the first time on any snapdragon device, with Smurf kernel for OnePlus 6
@Eliminater74 for tips along the way, very helpful
@HolyAngel for a collab to add support to HolyDragon, and indirectly SkyDragon, and suggesting some tunable structuring
@rupanshji for the commit used for an example of a structured kcal implementation
@flar2 for debugging and adding k-lapse support to his app EX kernel manager, purchase the app here - EXKM
@franciscofranco for adding k-lapse support to his app Franco Kernel Manager, purchase the app here - FKM
The Following 28 Users Say Thank You to tanish2k09 For This Useful Post: [ View ] Gift tanish2k09 Ad-Free
4th March 2019, 02:01 AM |#3  
tanish2k09's Avatar
OP Senior Member
Thanks Meter: 2,185
 
Donate to Me
More
Screenshots
Screenshots from EXKM v4.04, Mixplorer and FKM v4.0 have been attached.

Note that these screenshots were taken while v4.3 was the active version. May change in future.
Attached Thumbnails
Click image for larger version

Name:	Screenshot_20190303-183333.jpg
Views:	6788
Size:	215.0 KB
ID:	4716218   Click image for larger version

Name:	Screenshot_20190303-183402.jpg
Views:	6774
Size:	140.8 KB
ID:	4716219   Click image for larger version

Name:	franco_klapse.jpg
Views:	3748
Size:	197.8 KB
ID:	4727295  
The Following 13 Users Say Thank You to tanish2k09 For This Useful Post: [ View ] Gift tanish2k09 Ad-Free
4th March 2019, 02:09 AM |#4  
tanish2k09's Avatar
OP Senior Member
Thanks Meter: 2,185
 
Donate to Me
More
It's pretty common now
@apophis9283 Seeing you're one of the mods here, would you please be kind enough to delete the other 3 clones of this thread that got created due to XDA request failure?

Also, in case this thread is in the wrong sub-forum, please feel free to move it to the correct one but notify me via PM or email. Many thanks
The Following 2 Users Say Thank You to tanish2k09 For This Useful Post: [ View ] Gift tanish2k09 Ad-Free
4th March 2019, 02:36 AM |#5  
Ryano89's Avatar
Senior Member
Flag Indiana
Thanks Meter: 387
 
More
Been using this on Smurf kernel for awhile now. Absolutely love it. Great job.
The Following User Says Thank You to Ryano89 For This Useful Post: [ View ] Gift Ryano89 Ad-Free
4th March 2019, 02:41 AM |#6  
david72262's Avatar
Senior Member
Flag Oregon
Thanks Meter: 1,624
 
More
Glad your work is posted on XDA now works good in sky dragon.
The Following User Says Thank You to david72262 For This Useful Post: [ View ] Gift david72262 Ad-Free
4th March 2019, 02:49 AM |#7  
Tomatot-'s Avatar
Senior Member
Thanks Meter: 1,269
 
More
Great alternative to night mode that does way more than that if you're interested in other features.

It's lightweight and won't bloat your system at all.

Any proper kernel should include this as there are only benefits and no drawbacks. 8)

As a last addition/compliment/support: it's been very well maintained and as you can tell just from reading OP, the dev is friendly and will help you if you have issues with it.
The Following 4 Users Say Thank You to Tomatot- For This Useful Post: [ View ] Gift Tomatot- Ad-Free
4th March 2019, 04:01 AM |#8  
akiwiz's Avatar
Senior Member
Thanks Meter: 47
 
More
On Smurf kernel on op6. I have the gray scale option on digital wellbeing in the night and after switching on this mod (light mode) it automatically turns off the gray scale. Is it a bug?
4th March 2019, 04:14 AM |#9  
tanish2k09's Avatar
OP Senior Member
Thanks Meter: 2,185
 
Donate to Me
More
Quote:
Originally Posted by akiwiz

On Smurf kernel on op6. I have the gray scale option on digital wellbeing in the night and after switching on this mod (light mode) it automatically turns off the grayscale. Is it a bug?

It is not a bug, it's actually expected behaviour.
First of all, we both know that the screen can't be both grayscale AND have a yellow tint at the same time, because yellow isn't gray.
Only one of them can stay active. The fact that klapse works from the kernelspace makes klapse more powerful. As soon as klapse refreshes, which happens every 30 seconds by default, the grayscale RGB will be overridden.

It also depends on how digital wellbeing's grayscale option works. I'm assuming it uses the same HWC HAL that android uses, but I have not the slightest clue. In case I'm right, both klapse and grayscale will work together to give you a black-n-white + slight yellow tinted screen.

As an alternative, how about I add grayscale as a feature to klapse itself? I can't add it to klapse right away but I can mark it in my TODO post above. Sounds good? Maybe I can come up with a solution that overlaps the grayscale with the yellow tint to create a grayscale night mode?

I don't know about you but it seems like an amazing idea to me
The Following 3 Users Say Thank You to tanish2k09 For This Useful Post: [ View ] Gift tanish2k09 Ad-Free
4th March 2019, 07:39 AM |#10  
akiwiz's Avatar
Senior Member
Thanks Meter: 47
 
More
Quote:
Originally Posted by tanish2k09

It is not a bug, it's actually expected behaviour.
First of all, we both know that the screen can't be both grayscale AND have a yellow tint at the same time, because yellow isn't gray.
Only one of them can stay active. The fact that klapse works from the kernelspace makes klapse more powerful. As soon as klapse refreshes, which happens every 30 seconds by default, the grayscale RGB will be overridden.

It also depends on how digital wellbeing's grayscale option works. I'm assuming it uses the same HWC HAL that android uses, but I have not the slightest clue. In case I'm right, both klapse and grayscale will work together to give you a black-n-white + slight yellow tinted screen.

As an alternative, how about I add grayscale as a feature to klapse itself? I can't add it to klapse right away but I can mark it in my TODO post above. Sounds good? Maybe I can come up with a solution that overlaps the grayscale with the yellow tint to create a grayscale night mode?

I don't know about you but it seems like an amazing idea to me


Thank you for the explanation 😊 is there a way I can switch off klapse at night when grey scale from digital wellbeing kicks in? Also your alternative would be ideal if it's possible in future! Thanks again
4th March 2019, 07:44 AM |#11  
tanish2k09's Avatar
OP Senior Member
Thanks Meter: 2,185
 
Donate to Me
More
Quote:
Originally Posted by akiwiz

Thank you for the explanation 😊 is there a way I can switch off klapse at night when grey scale from digital wellbeing kicks in? Also your alternative would be ideal if it's possible in future! Thanks again

A tester just told me that klapse's yellow tint worked on gray-scale too.

But to answer your question, the only possible solution is to either disable Klapse or keep the schedule of start and stop outside the range of grayscale's schedule.

Btw I totally recommend you to use Klapse at night because yellow tint in the dark is better than grayscale to look at
The Following User Says Thank You to tanish2k09 For This Useful Post: [ View ] Gift tanish2k09 Ad-Free
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes