Skip to content

perf(leds): sample ambient color from RGB565 and throttle LED updates to 15 Hz - #918

Open
pvaibhav wants to merge 7 commits into
LoveRetro:mainfrom
pvaibhav:perf/ambient-led-rgb565-15hz
Open

pvaibhav wants to merge 7 commits into
LoveRetro:mainfrom
pvaibhav:perf/ambient-led-rgb565-15hz

Conversation

@pvaibhav

Copy link
Copy Markdown

This PR reduces the CPU and sysfs overhead of Ambient LED mode during gameplay so users can keep reactive LEDs enabled without forcing extra pixel conversion or writing to hardware 60 times a second.

Ambient LED mode now keeps direct RGB565 texture uploads (eliminating full-frame CPU RGBA conversion), cuts ambient color sampling and LED hardware writes by 4× (from 60 Hz to ~15 Hz), and fixes a cross-thread race between gameplay LED updates and the background battery monitor.

Note: Built on top of #916 (upstream/perf-video-gles) for the retained RGB565 buffer — please merge #916 first, after which this PR can be rebased onto main.

Changes

  • Ambient color sampling format (workspace/all/common/api.c, api.h, workspace/all/minarch/ma_video.c): Because GFX_setAmbientColor() only accepted RGBA buffers, turning on Ambient LEDs forced full-frame CPU RGB565→RGBA conversion (~0.2–0.9 ms/frame) on every frame. Teaching GFX_setAmbientColor() to sample RGB565 directly with its matching pitch lets Ambient mode keep direct RGB565 texture uploads and eliminates that conversion cost.
  • 60 Hz ambient LED updates (workspace/all/common/api.c, GFX_setAmbientColor, LEDS_setProfile): Ambient mode sampled pixels and pushed hardware LED writes on every video frame (up to 15 sysfs writes per frame on Brick Pro, ~1.2 ms/frame). Rate-limiting sampling and hardware updates to once every 66 ms (~15 Hz)—while applying mode changes immediately and still updating all zones per push so H700 UART commits and EAGAIN retries stay intact—cuts LED write overhead 4×.
  • Cross-thread LED races (workspace/all/common/api.c, LEDS_lock/LEDS_unlock): Both the main thread (ambient sampling and profile rules) and the background battery monitor thread read and wrote LED profile state and hardware simultaneously, risking interleaved writes or stale profiles. Guarding lightsAmbient writes, LEDS_setProfile(), and LEDS_updateLeds() with a cancellation-safe mutex serializes LED updates without risking deadlock when PWR_quit() cancels the battery thread.

Results

  • Eliminates forced full-frame CPU RGBA conversion during Ambient LED mode on RGB565 cores.
  • Cuts ambient LED sampling and sysfs/UART hardware writes from 60 updates/sec to ~15 updates/sec (4× reduction).
  • Preserves H700 multi-zone UART commits and EAGAIN retry behavior.

Blast Radius

Testing and Verification

  • Compiled minarch cleanly for both tg5040 and h700 toolchains with zero warnings.
  • Verified live on Trimui Brick Pro (tg5040) on battery.
  • Edge cases caught and handled during code review:
    • H700 multi-zone commit & EAGAIN retries: A per-LED color-delta cache would skip the last zone (which triggers H700's UART commit) on left-only changes and suppress retries after transient EAGAIN; updating all zones at 15 Hz via LEDS_updateLeds() avoids both issues and also preserves effect-only changes.
    • Thread races & pthread_cancel safety: Serialized lightsAmbient writes, LEDS_setProfile(), and direct LEDS_updateLeds() calls (e.g., from ledcontrol) under a mutex wrapped with PTHREAD_CANCEL_DISABLE so PWR_quit()'s pthread_cancel() cannot deadlock while holding the lock.

The source texture already holds the pixels for a duplicate frame. Keep
uploading when the debug HUD or fade-in modifies those pixels.
Only the debug HUD, fade-in and ambient LEDs need the RGBA frame on the
CPU. When none of them is active, upload the core's RGB565 buffer
directly. Draw duplicates from the texture because there is no CPU copy.
Uploading to a texture that the current frame still samples makes the
PowerVR driver allocate storage on every frame. Upload to the texture
that the previous frame did not use.
A duplicate frame ran every user shader pass again on the same source
texture. Reuse the output in the FBO textures and run only the final
draw to the screen.
…, and hash shader cache keys

Keep packed RGB565 frames across duplicate callbacks and context resets, track dual source texture storage per slot with GL allocation error checks, keep SDL presentation safe across source changes, include source FNV-1a hash in shader cache keys, and allow direct RGB565 upload during fast-forward.
…L swap

Cache MVPMatrix, VertexCoord, and TexCoord locations at shader link time, keep one persistent FBO per shader pass instead of re-attaching every frame, and drop per-frame glGetError/glIsTexture/glCheckFramebufferStatus queries (checking completeness on attach). Move SDL_RenderClear() out of PLAT_blitRenderer() into the non-GL PLAT_flip() blit path to avoid per-frame SDL GL context switches, and clear FBO 0 inside PLAT_GL_Swap() before the final pass.
… to 15 Hz

Allow direct RGB565 texture upload during ambient LED mode by sampling average color directly from RGB565, rate-limit ambient sampling and LED hardware writes to ~15 Hz (66 ms), and serialize LED profile/color updates across the main and battery threads with a cancellation-safe mutex.
@pvaibhav

Copy link
Copy Markdown
Author

Note: This PR is stacked on top of #916 (and conflicts with it in ma_video.c if merged out of order), so please merge #916 first — I'll rebase this onto main right after #916 lands.

This branch has not been deployed

No deployments
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.

1 participant