Skip to content

fix(linux): retain PipeWire DMA-BUFs through CUDA conversion - #1

Closed
happyme531 wants to merge 1 commit into
masterfrom
codex/fix-pipewire-dmabuf-lifetime
Closed

fix(linux): retain PipeWire DMA-BUFs through CUDA conversion#1
happyme531 wants to merge 1 commit into
masterfrom
codex/fix-pipewire-dmabuf-lifetime

Conversation

@happyme531

Copy link
Copy Markdown
Owner

Summary

Retain producer-owned PipeWire DMA-BUFs until Sunshine's GL/CUDA conversion has finished reading them.

This fixes intermittent remote-only flashes of coherent content from other windows when using KWin ScreenCast, PipeWire DMA-BUF capture, and NVENC on NVIDIA. The physical display and OBS output remain correct while the streamed frame briefly shows an older or background window.

Root cause

fill_img_dmabuf() duplicates each DMA-BUF file descriptor, but the corresponding pw_buffer was still left in stream_data.current_buffer. When the next frame arrived, on_process() immediately queued that previous pw_buffer back to PipeWire.

Duplicating a DMA-BUF fd keeps the allocation alive; it does not prevent the producer from reusing and overwriting the buffer. Because GL/CUDA conversion consumes the imported image later on another thread, KWin could begin rendering new content into the same allocation before Sunshine had completed the read. That produces coherent flashes of unrelated window content rather than codec corruption.

Changes

  • Transfer the current PipeWire buffer to the captured image for CUDA DMA-BUF capture and clear stream_data.current_buffer, preventing the next process callback from returning it early.
  • Add a one-shot completion callback to the EGL image descriptor. reset() invokes it as a fallback when conversion exits early.
  • Return retained buffers only after CUDA maps/unmaps the GL conversion targets, which synchronizes the preceding GL draw that consumed the source DMA-BUF.
  • Serialize releases through the PipeWire thread loop and use shared/weak release state so late image destruction cannot access a torn-down stream.
  • Keep the behavior limited to the validated CUDA DMA-BUF path; VAAPI, Vulkan, memory-buffer capture, and KMS are unchanged.
  • Add unit coverage for one-shot release and reset fallback behavior.

Validation

  • The functionally equivalent prototype built successfully in Release mode with GCC 15 and CUDA 13.2 on Sunshine commit 25c06d7.
  • The current diff passes git diff --check after rebasing onto 3cba9ba.
  • Real session: KDE/KWin 6.7.4, PipeWire 1.6.8, NVIDIA 610.57.04, KWin ScreenCast + DMA-BUF + HEVC NVENC, 2944x1840 at 90 FPS and about 16 Mbps.
  • Before the change, random background-window flashes occurred even while idle. No recurrence was observed during a full day of normal remote-desktop use with the patched binary.
  • The newly added unit tests have not been run locally yet; this PR remains a draft while CI validation is pending.

Scope

This does not change the encoder and does not attempt to fix KMS capture. LizardByte#3235 describes a similar visual symptom, but that report used KMS and is referenced only for context.

@happyme531

Copy link
Copy Markdown
Owner Author

Superseded by the upstream pull request: LizardByte#5498

@happyme531 happyme531 closed this Aug 11, 2026
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