Skip to content

linux-capture: XComposite tracks window state from X events instead of a round trip per frame (#1356) - #1377

Merged
zackees merged 4 commits into
masterfrom
fix/1356-xcomposite-exists-from-events
Oct 18, 2026
Merged

zackees merged 4 commits into
masterfrom
fix/1356-xcomposite-exists-from-events

Conversation

@zackees

@zackees zackees commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Refs #1356 (parent #1273; builds on #1366).

Cause

xcompcap_video_tick called xcomp_window_exists() (a blocking xcb_get_window_attributes request + reply) once or twice per frame, under obs_enter_graphics() and the source lock. A busy or remote X server stalled the render path every frame.

Design

  • New pure helper plugins/linux-capture/xcomposite-presence.h (same approach as linux-capture: XComposite backs off the window search while the window is unmapped (#1357) #1366's xcomposite-search.h): cached known/viewable state, fed by events, with a safety-net recheck at most once per second (XCOMP_PRESENCE_RECHECK).
  • The watcher already selects StructureNotifyMask | ExposureMask | VisibilityChangeMask on the target window, so no mask change is needed. watcher_process now maps MapNotify → viewable, UnmapNotify (newly handled; does not set window_changed) → not viewable, DestroyNotify → not viewable; Configure/Expose/Visibility still set window_changed as before.
  • The tick reads events with the non-blocking xcb_poll_for_event (the per-frame round trips used to be what pulled events into the queue for xcb_poll_for_queued_event), frees them (they were leaked), and does the real round trip only when the cache is unknown (new settings, window_changed) or ≥1 s old. That check runs under the source lock but before obs_enter_graphics().
  • The unhooked signal and window_lost read the cached state. After a search, the result's viewable refreshes the cache. linux-capture: XComposite backs off the window search while the window is unmapped (#1357) #1366's backoff (xcomp_search_*) is unchanged.

Test

New self-contained cmocka test test/cmocka/test_xcomposite_presence.c (tests.d/test_xcomposite_presence.cmake, links only cmocka): simulated 60 fps ticks with a fake server and event feed, counting existence round trips.

  • stable mapped window, 10 s: ≤ 11 round trips (was 600)
  • Unmap/Map/Destroy events flip the state immediately with no round trip
  • a missed unmap event is caught within 61 frames (~1 s)
  • invalidate (window_changed / new settings) asks the server on the next tick

RED (74d91fc82, helpers stubbed to today's per-frame check): rust/tools/linux-validate/run.sh --repeat 1 --tests test_xcomposite_presence

[  ERROR   ] --- 600 is not within the range [1, 11]
[  FAILED  ] test_stable_window_rarely_asks_server
[  ERROR   ] --- s.st.viewable is not false
[  FAILED  ] test_events_flip_state_without_round_trip
[  FAILED  ] test_invalidate_asks_next_tick
99% tests passed, 1 tests failed out of 296

GREEN (c4789fcb4): 100% tests passed, 0 tests failed out of 296; repeat run test_xcomposite_presence ... Passed, test_xcomposite_search_backoff ... Passed.

End to end under Xvfb (requested by the lead)

test/cmocka/test_xcomposite_window_events.c (38a292ac5, tests.d/test_xcomposite_window_events.cmake): compiles the linux-capture unity file into the test, calls xcomposite_load(), runs obs_startup + obs_reset_video (libobs-opengl by path) under the harness's xvfb-run display (Composite present). The test publishes a minimal EWMH setup (_NET_SUPPORTING_WM_CHECK, _NET_CLIENT_LIST) in place of a window manager, creates a plain xcb window and an xcomposite_input source on it, and lets libobs tick it:

  1. mapped → source reports hooked (get_hooked)
  2. unmap → unhooked within 400 ms, three rounds (the safety net is 1 s, so this comes from the UnmapNotify read by xcb_poll_for_event)
  3. map again → hooked again within 2 s
  4. destroy → unhooked

Limitation: Xvfb's Mesa llvmpipe has no DRI3, so gs_texture_create_from_pixmap logs Cannot create EGLImage: One or more argument values are invalid and the source has no texture (0x0). Width/height are therefore not asserted; the rounds run on the no-texture path. Passed under run.sh --repeat 1 --tests test_xcomposite (full run 298/298, repeat 3/3).

Tiers: Tier 2 (new C tests). Tier 1/3/4 do not apply (C plugin fix, no Rust port, no protocol). Not run against a real desktop X server with GPU pixmap import; the compile check is PR CI's Fast Gate for linux-capture.
Ledger: docs/features.md, Fixed upstream bugs → Video capture.

@zackees
zackees force-pushed the fix/1356-xcomposite-exists-from-events branch from 9c6cf2d to 4bff34e Compare October 18, 2026 02:04
@zackees
zackees marked this pull request as ready for review October 18, 2026 02:05
@zackees
zackees enabled auto-merge October 18, 2026 02:05
@zackees
zackees disabled auto-merge October 18, 2026 02:15
…ndow exists every frame (#1356)

Pure presence helpers stubbed to today's behaviour (a round trip per
tick, events ignored); the new cmocka test counts round trips.

Refs #1356
…f a round trip per frame (#1356)

xcompcap_video_tick called xcomp_window_exists (xcb_get_window_attributes
request + reply) once or twice per frame under obs_enter_graphics and the
source lock. The watcher now feeds Map/Unmap/DestroyNotify into a cached
presence state; the tick reads the cache, reads events with the
non-blocking xcb_poll_for_event (and frees them), and asks the server only
after window_changed/new settings and at most once a second as a safety
net, before obs_enter_graphics. #1366's search backoff is unchanged.

Refs #1356
… to end under Xvfb (#1356)

Compiles the linux-capture unity file into a cmocka test, publishes a
minimal EWMH setup in place of a window manager, and drives a plain xcb
window through map, three unmap/map rounds and destroy while libobs ticks
the real xcomposite source. Unmap must unhook within 400 ms (well under
the 1 s safety net, so it comes from the UnmapNotify read by
xcb_poll_for_event), map must re-hook within 2 s, destroy must unhook.
Xvfb's llvmpipe cannot import the window pixmap as an EGLImage, so the
capture's texture is not checked; the hooked state is.

Refs #1356
@zackees
zackees force-pushed the fix/1356-xcomposite-exists-from-events branch from 4bff34e to 38a292a Compare October 18, 2026 02:58
@zackees
zackees enabled auto-merge October 18, 2026 02:58
@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into master with commit 3352823 Oct 18, 2026
23 checks passed
@zackees
zackees deleted the fix/1356-xcomposite-exists-from-events branch October 26, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant