Skip to content

[rcore][drm] Replace DRM swap buffer implementation with asynchronous page-flipping and triple framebuffer caching #4988

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rob-bits
Copy link

@rob-bits rob-bits commented Jun 5, 2025

The original implementation created/destroyed framebuffers (FBs) per-frame, leading to kernel overhead and screen tearing. This commit replaces it with a different approach using:

  • Asynchronous drmModePageFlip() with vblank sync
  • Framebuffer caching to reduce repeated FB creation/removal operations
  • Proper resource management through BO callbacks and buffer release synchronization
  • Added error handling for busy displays, cache overflows, and flip failures
  • Event-driven cleanup via page_flip_handler to prevent GPU/scanout conflicts

Would you please review this code?
In my case it works better, has less framedrop or tearing.
Tested it on a Raspberry Pi Zero 2W.

Maybe this implementation should be handled as a compiler option for the user. To select if he wants the minimal DRM or the page-flipping, cached DRM.

Summary: Key Differences

Aspect Function (SwapScreenBuffer - Original) Function (SwapScreenBuffer new)
Framebuffer Creation Creates/removes an FB per frame (expensive). Caches Fbs for reuse; minimizes kernel operations.
Flip Synchronization No vblank sync: likely causes tearing. Uses drmModePageFlip() with vblank sync to prevent tearing.
Error Handling Minimal recovery (logs errors, continues). Graceful error handling and tracking via counters/callbacks.
Resource Management Frees buffers immediately; risks conflicts. Properly releases BOs/Fbs only after flips complete.
Complexity/Overhead Simple code but performs poorly at high frame rates. More complex but optimizes for performance

…ge-flipping and framebuffer caching

The original implementation created/destroyed framebuffers (FBs) per-frame, leading to kernel overhead and screen tearing. This commit replaces it with a different approach using:
- Asynchronous `drmModePageFlip()` with vblank sync
- Framebuffer caching to reduce repeated FB creation/removal operations
- Proper resource management through BO callbacks and buffer release synchronization
- Added error handling for busy displays, cache overflows, and flip failures
- Event-driven cleanup via page_flip_handler to prevent GPU/scanout conflicts

Co-authored-by: rob-bits
@rob-bits rob-bits changed the title Replace DRM swap buffer implementation with asynchronous page-flipping and triple framebuffer caching [rcore][drm]Replace DRM swap buffer implementation with asynchronous page-flipping and triple framebuffer caching Jun 5, 2025
@rob-bits rob-bits changed the title [rcore][drm]Replace DRM swap buffer implementation with asynchronous page-flipping and triple framebuffer caching [rcore][drm] Replace DRM swap buffer implementation with asynchronous page-flipping and triple framebuffer caching Jun 5, 2025
@@ -910,7 +1099,8 @@ int InitPlatform(void)
EGL_BLUE_SIZE, 8, // BLUE color bit depth (alternative: 5)
EGL_ALPHA_SIZE, 8, // ALPHA bit depth (required for transparent framebuffer)
//EGL_TRANSPARENT_TYPE, EGL_NONE, // Request transparent framebuffer (EGL_TRANSPARENT_RGB does not work on RPI)
EGL_DEPTH_SIZE, 24, // Depth buffer size (Required to use Depth testing!)
//ToDo: verify this. In 5.5 it is 16, in master it was 24
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct, it was increased to address some z-fighting isssues. Afaik, most devies already support 24bit depth buffers.

@raysan5
Copy link
Owner

raysan5 commented Jun 6, 2025

@rob-bits Thanks for the improvement, it would be nie to keep both methods to be selected with a compilation flag.

Please, could you first review the code to follow raylib coding conventions?

@rob-bits
Copy link
Author

rob-bits commented Jun 6, 2025

@rob-bits Thanks for the improvement, it would be nie to keep both methods to be selected with a compilation flag.

Please, could you first review the code to follow raylib coding conventions?

Yeah, will review it and revisit the code if it brokes any convention. And will add a compiler define to have control which one should be used.

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.

2 participants