Skip to content

Fix-SAMA5D2-LCDC-framebuffer-artefacts-(LVGL) - #12733

Closed
TimJTi wants to merge 1 commit into
apache:masterfrom
TimJTi:Fix-SAMA5D2-LCDC-framebuffer-artefacts-(LVGL)
Closed

TimJTi wants to merge 1 commit into
apache:masterfrom
TimJTi:Fix-SAMA5D2-LCDC-framebuffer-artefacts-(LVGL)

Conversation

@TimJTi

@TimJTi TimJTi commented Jul 18, 2024

Copy link
Copy Markdown
Contributor

The SAMA5D2 LCDC driver has memory mapped framebuffers that LVGL (for example) can write to directly causing issues. LVGL - and presumably other applications - need a buffer that they can render to and only when done should that be copied to the "real" framebuffers of the LCDC.

I have added a function CONFIG_FB_UPDATE that can be called via the relevant IOCTL that does the copy and it has fixed the issue I was seeing in relation to screen artefacts I noted as an issue in the nuttx-apps repo here.

I am not 100% sure what the correct way to address this is, so for now I have added a new Kconfig entry to allow an "EXT" memory buffer to be declared, and that is what is reported as the pinfo->fbmem memory location but it doesn't really feel right - and it seems there are no other architectures that address this so it is perhaps unique to the SAMA5D2?

There must be a better/neater/proper way to be doing this?

Impact

It allows LVGL V9 to behave as expected, on this processor. I have tried to ensure that the driver reverts to the previous methodology if the new Kconfig entry is not present.

Testing

Custom board with SAMA5D27-D1G and 800x480 LCDC. Manually deleted new Kconfig entry to check the driver reverts to as before and, similarly, deleted the newly enforced CONFIG FB_UPDATE entry to check.

@TimJTi
TimJTi force-pushed the Fix-SAMA5D2-LCDC-framebuffer-artefacts-(LVGL) branch from acd3dd2 to 3c26aeb Compare July 18, 2024 15:46
@acassis

acassis commented Jul 18, 2024 •

Copy link
Copy Markdown
Contributor

@TimJTi I think it is fine. Just let @patacongo say if he thinks this approach is fine (don't know if it is compatible with FLAT, PROTECTED and KERNEL modes).

@TimJTi please also remember to update SAMA5D2 Documentation/ to include this information that they need to enable CONFIG_FB_UPDATE to avoid tearing/flickering as you was facing.

@patacongo

patacongo commented Jul 18, 2024 •

Copy link
Copy Markdown
Contributor

Drivers really should not write directly into hardware frame buffers. For the case of a single graphics user thread, that would work but it will not work in a multithreaded environment. The result could be a disaster.

Mutexes might be used to enforce single user access, but that would also result in terrible performance in multithreaded case: One or most thread would be blocked waiting for display access and trashing real-time performance.

[I am not sure if the existing framebuffer driver has these same issues. It was a pretty low level of effort to create so that wouldn't surprise me.]

AFAIK LVGL itself knows nothing about mulithreaded display usage. At the application level, that is fine. But within the OS we really need to be cognizant and responsible for multithreaded use in all cases.

When the NX windowing system is used, for example, numerous threads may be accessing the display concurrently. Display requests are serialized in a message queue. LVGL will not work in that configuration, but other graphics do. NX will serialize all accesses across multiple threads and all will work well.

This is not a good solution and I ask that you do not commit it. Instead, please consider using the pre-window framebuffer logic. I think this does whay you want to do, but might require that you think a little differently:

``CONFIG_NX_RAMBACKED``
   Enables RAM backed window support. If this option is selected, then
   windows may be optionally created with a RAM framebuffer backing up
   the window content. Rending into the window will result in rending
   into the backup framebuffer, then updating the physical display from
   the framebuffer.

   The advantage of this option is that the application that manages
   window will no longer receive redraw() callbacks. Those calls
   normally occur, for example, when a window "above" moves exposing a
   portion of the window below. If this option is selected, then the
   system will redraw the exposed portion of the window from the backup
   framebuffer without intervention of the window applications. This
   greatly reduces the complexity of the application and performance of
   the window at the expense of increased memory usage.

   An exception is the case when the window is resized to a wider and/or
   taller size. In that case, the redraw callback will till occur. It is
   necessary in that case to provide new graphic content for the
   extended window area.

   Redraw requests in other cases are also suppressed: Changes to window
   position, size, etc.

The RAM-backed, per-window framebuffers were created to support grabbing and dragging windows without tearing or degraded performance. The is a superior, bug-free alternative.

@patacongo

patacongo commented Jul 18, 2024 •

Copy link
Copy Markdown
Contributor

This is not a good solution and I ask that you do not commit it. Instead, please consider using the pre-window framebuffer logic.

In the case of LVGL, you would not use all windows but only the background window (which is not movable or resizeable). You might have to enable the NX subsystem which would provide the multithreaded support. I am not sure how entangled those are. Perhaps other work would be required to do this without NX.

It is always best to reuse and extend the currently architecture rather than redesign some parallel, overlapping, bugger redudant solution.

@acassis

acassis commented Jul 18, 2024

Copy link
Copy Markdown
Contributor

Thank you @patacongo !!! @XuNeo since you are working on LVGL V9 could you please take a look and help Tim on that?

@patacongo

Copy link
Copy Markdown
Contributor

Thank you @patacongo !!! @XuNeo since you are working on LVGL V9 could you please take a look and help Tim on that?

A truly great solution would be to implement a LGVL compatible framebuffer driver for the NX windows. That would allow to solve BOTH the tearing problem to to support multithreaded concurrent windows with LVGL. How cool would that be?

@TimJTi

TimJTi commented Jul 19, 2024

Copy link
Copy Markdown
Contributor Author

Thank you @patacongo !!! @XuNeo since you are working on LVGL V9 could you please take a look and help Tim on that?

Thank you for your comments. Having slept on it I have realised my solution is right...but in completely the wrong place. It belongs in LVCGL not NuttX which is what I had thought until convincing myself otherwise.

I am right that LVGL was "messing" with the framebuffer and homed in on a SAMA5D2 driver issue; but the LVGL NuttX framebuffer is rendering DIRECT to the framebuffer, not to it's own draw buffer. This is contrary to the basic principles of LVGL - set up a buffer or buffers for LVGL to render to; flush it to the display; notify that the flush is complete; repeat.

I will reopen my issue on LVGL as this needs to be addressed there. I will keep this PR open for a short while as it is a useful place for a discussion and NuttX issues get more attention than LVGL ones do, sadly.

But I like the idea of NX and RAM backed windows but I am not sure what "a LGVL compatible framebuffer driver for the NX windows" would be like?

Since LVGL simply renders to a buffer that is setup in user-land, can an app not simply use LVGL to render to an NX RAM-backed window without any change to the NX/NuttX ecosystem?

Very interested to discuss this further - when I asked a question that was 100% related to this issus @gregory-nutt essentially said the same: LVGL on multiple NX windows would be super cool; and my nascent App could easily use multiple NX windows, many of which have independent LVGL graphics rendered to them. Bring it on!!

@patacongo

patacongo commented Jul 19, 2024 •

Copy link
Copy Markdown
Contributor

Since LVGL simply renders to a buffer that is setup in user-land, can an app not simply use LVGL to render to an NX RAM-backed window without any change to the NX/NuttX ecosystem?

The RAM backed windows are an option and it sounds like you don't need the option since the buffering will occur in user space.

Very interested to discuss this further - when I asked a question that was 100% related to this issus @gregory-nutt essentially said the same: LVGL on multiple NX windows would be super cool; and my nascent App could easily use multiple NX windows, many of which have independent LVGL graphics rendered to them. Bring it on!!

An window framebuffer would be a minor extension to the existing full display framebuffer. If the framebuffer is not RAM backed then this driver Window framebuffer driver would just replace the direct writes into the framebuffer with calls to NX APIs to write the image. NX would write to the correct position on the display, clip, and handle intersections.

Documentation: https://cwiki.apache.org/confluence/display/NUTTX/NX+Graphics+Subsystem

Other than the initialization of the window and selection of the driver, I don't think that the change would affect the application.

The driver would also have to handle a few callbacks like redraw (just redraw the window if it a new part of the window is exposed) and other events (like moving the window or keyboard or mouse input). Keyboard / mouse input is another topic. These events go to the window that has focus. I would need to think about that more.

Imagine a normal LVGL display. And error occurs and and error window pops up. The error window gets focus so the error window would then get the mouse input. Clicking OK should dismiss the error window and return focus to the LVGL window.

To integrate, LVGL would have to use this NX framebuffer with the so-called "background" window: https://cwiki.apache.org/confluence/display/NUTTX/NX+Graphics+Subsystem#nxrequestbkgd

I am interested in this topic. I am very retired now, but still willing to help out.

@TimJTi

TimJTi commented Jul 19, 2024

Copy link
Copy Markdown
Contributor Author

I am interested in this topic. I am very retired now, but still willing to help out.
Thanks! I spent the morning trying to fix the LVGL nuttx framebuffer driver but it misbehaves. I added local buffers - as there should be - for the rendering, and a flush function to memcpy the specific "dirty" area of the local render buffer to the nuttX framebuffer but it has odd, and repeatable, side effects. I just hope someone in LVGL-land will advise/comment.

I am on the cusp of being too deep into my current LVGL path to invite NX to the party, but I will if I have to, short term - and will certainly look into it mid-term if there is a miraculous lvgl fix for now. Bear with me.

@XuNeo

XuNeo commented Jul 20, 2024

Copy link
Copy Markdown
Contributor

Hi @TimJTi ,

There are two issues with your current setup. I have explained a bit in lvgl/lvgl#6389.

Issue

From the video, it looks like the arc head is flicking. This happens because LVGL's intermediate drawing result is sent to display by hardware LCD controller directly without waiting.
The second issue is tearing. With single frame buffer, this will happen for sure.

Solution

LVGL has a special refreshing mode called PARTIAL mode. It allows LVGL to draw on a temporal buffer and memcpy to FB when it's really finished(thus no flicking). It can still suffer from screen tearing with single FB.

Current LVGL NuttX porting(lv_nuttx_fbdev.c) does not support this mode. lv_nuttx_lcd.c has support but I have no experience using it.

Instead of hacking into FB driver, we should upgrading lv_nuttx_fbdev.c to add support to PARTIAL mode.

Multiple instance

LVGL V9 introduces support for multiple instances, allowing multiple windows to run simultaneously. The server allocates virtual framebuffers to clients, which can be easily composed using the LVGL Image widget. Clients draw on the virtual FB and send update events to the server.

The NX Graphic system sounds like a good candidate for managing the window framebuffers, and input events. The recent LVGL QNX porting PR has something similar that using QNX Screen window as a display.

@TimJTi

TimJTi commented Jul 20, 2024 •

Copy link
Copy Markdown
Contributor Author

@XuNeo
Hi. If you look at the latest updates to the LVGL issue you linked to, you will see that I am 99% sure LVGL is rendering DIRECTLY to the framebuffer which, if I am right, is WRONG. I have implemented a variant (a link to my fork is in that issue) that uses LVGL Linux-type framebuffer code, with dedicated buffer(s) that lvgl can render to that are only flushed to the real hardware framebuffer via the flush callback - but it still has issues and I included a video of them. This suggests lvgl is calling a flush BEFORE it has completely finished rendering. If I am right with that, then it is also WRONG!!

I have tried partial mode but, to be fair, not before I did the linux-style framebuffer driver. If the declaration of the buffer as PARTIAL rather than DIRECT is a trigger for lvgl to only call the flush after rendering is definitely complete, it would be useful to know, and I will try it when I get a moment.

But, for now, LVGL V9 is broken when it comes to using a framebuffer rather than arch or board-specfic device driver.

@TimJTi

TimJTi commented Jul 20, 2024

Copy link
Copy Markdown
Contributor Author

Perhaps the answer is lv_disp_flush_is_last(&disp_drv) ? That - I think - can be used to not flush until rendering is finished...I just came across this in the porting guide (which until now I hadn't expected to need to read!)

flush_cb a callback function to copy a buffer's content to a specific area of the display. lv_disp_flush_ready(&disp_drv) needs to be called when flushing is ready. LVGL might render the screen in multiple chunks and therefore call flush_cb multiple times. To see if the current one is the last chunk of rendering use lv_disp_flush_is_last(&disp_drv).

@TimJTi

TimJTi commented Jul 20, 2024

Copy link
Copy Markdown
Contributor Author

This is 100% LVGL, so I will close this and work on a fix and PR at the LVGL repo. Once I get time to look at an NX+LVGL combo I will open a new discussion and/or PR.

Thanks for everyone's help here.

@TimJTi TimJTi closed this Jul 20, 2024
@TimJTi
TimJTi deleted the Fix-SAMA5D2-LCDC-framebuffer-artefacts-(LVGL) branch October 12, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants