You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enable runtime color correction toggling without reinitializing GL context
🐞 Bug fix✨ Enhancement🕐 10-20 Minutes
AI Description
• Make color correction gain/offset updates safe and visible across threads.
• Re-read correction parameters every frame to reflect runtime colortrans changes.
• Avoid stale parameters baked into the first GL initialization.
The following are alternative approaches to this PR:
1. Command queue to processor thread
➕ Avoids atomics and memory-order reasoning by serializing updates on the processor thread
➕ Naturally batches multiple parameter changes
➖ Requires additional plumbing (queue, wakeups) and careful lifecycle handling
➖ More latency/complexity than necessary for two scalar params
2. Mutex-protected shared params
➕ Straightforward correctness model
➕ Easier to extend to more parameters
➖ Adds lock contention on the per-frame hot path unless carefully structured
➖ Still needs ordering discipline for enable/disable semantics
3. Reinitialize GL context on each toggle
➕ Simplifies state management inside the GL stage
➖ Expensive and disruptive; can cause stalls and visible glitches
➖ Does not scale if users tweak parameters frequently
Recommendation: Current approach (atomics + per-frame parameter refresh) is the best trade-off here: it keeps the per-frame cost minimal, avoids expensive GL reinitialization, and ensures the processor thread never observes an enable without the matching parameters (release-store on enable + acquire-load on read). The main review focus should be verifying memory-ordering and ensuring set_params() is only called from the processor thread as documented.
Files changed (3) +27 / -9
Enhancement (1) +5 / -0
frame_colorcorrect.hAdd runtime parameter update API for color correction+5/-0
Add runtime parameter update API for color correction
• Introduces FrameColorCorrect::set_params(gain, offset) to update uniforms for an already-initialized GL context. Documents that the change takes effect on the next frame and must be called from the processor thread.
frame_processor.cppPublish color correction params atomically and apply them every frame+20/-8
Publish color correction params atomically and apply them every frame
• Stores gain/offset in atomics and enables correction using release semantics to prevent reordering. In the processor loop, reads enable with acquire semantics, derives default params when disabled, and calls FrameColorCorrect::set_params() each frame so runtime toggles are reflected without reinit.
FrameProcessor::set_color_correction() writes drm_fd_ (plain int) from the UI/control thread
while process_loop() reads drm_fd_ on the processor thread, which is a C++ data race (undefined
behavior). This can manifest as sporadic wrong need_gl decisions or EGL/GBM init using a
stale/invalid fd during runtime colortrans switching.
+ // Release-store so the processor thread can't observe the enable before+ // the gain/offset it belongs to.+ color_correct_.store(true, std::memory_order_release);+ // Actual EGL/GL init happens lazily on the processor thread (first frame);
Evidence
The code explicitly documents that color correction can be toggled/updated from the UI thread, and
main.cpp calls the setter from a live-control callback. In that setter, drm_fd_ is written without
atomic/mutex protection, while in process_loop() the processor thread reads drm_fd_ to
decide/init the GL path, creating concurrent read/write on a non-atomic variable (UB).
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
## Issue description
`FrameProcessor::set_color_correction()` updates `drm_fd_` without atomic/mutex protection while the processor thread reads it every frame. This is a C++ data race (UB) when `set_color_correction()` is called at runtime (as the header/docs and main.cpp show).
### Issue Context
This PR made gain/offset atomics and added release/acquire on `color_correct_`, but `drm_fd_` remains non-atomic and is still written from the UI thread.
### Fix Focus Areas
- Make `drm_fd_` thread-safe (prefer `std::atomic<int>`), and use `store()`/`load()` consistently where it is written/read.
- Optionally, if `drm_fd_` is intended to be immutable after construction, remove the runtime write path and enforce/document that invariant.
- src/frame_processor.h[67-75]
- src/frame_processor.h[126-136]
- src/frame_processor.cpp[82-91]
- src/frame_processor.cpp[180-199]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix colortrans switching during runtime.