diff --git a/docs/13-single-layer-windowing.md b/docs/13-single-layer-windowing.md index b8dd8a3..2300a1a 100644 --- a/docs/13-single-layer-windowing.md +++ b/docs/13-single-layer-windowing.md @@ -59,12 +59,41 @@ Step by step, with the override's exact insertion point. helper Dishonored nests under) — swaps it for the root child gamescope actually paints, matched by `_NET_WM_PID`/`WM_CLASS`, falling back to the X input focus, then `_NET_ACTIVE_WINDOW`, then the largest candidate. -4. **The override is created.** The shim opens **its own wayland connection** to - gamescope's wayland socket (`wl_setup`), creates **its own `wl_surface`** - (`wlproto::compositor_create_surface`), wraps it in a `gamescope_swapchain` - (`swapchain_factory_create_swapchain`), and binds that surface to the X - window: `swapchain_override_window_content(scobj, server=0, bound_window)` — - `gamescope_swapchain.override_window_content`. +4. **The override is created, and bound at the first commit.** The shim opens + **its own wayland connection** to gamescope's wayland socket (`wl_setup`), + creates **its own `wl_surface`** (`wlproto::compositor_create_surface`), + wraps it in a `gamescope_swapchain` + (`swapchain_factory_create_swapchain`). The binding to the X window waits + for a swapchain's first commit (`bind_window_at_first_commit`): + `swapchain_override_window_content(scobj, server=0, bound_window)` — + `gamescope_swapchain.override_window_content`, sent in the same flush and + right before that commit. gamescope keeps one surface per window and a new + binding retires the old one, so a swapchain that never presents (the Epic + overlay creates one on a child of the game's window only to find `Present`) + never takes the window from the game. + + Two decisions live at different instants here, deliberately: + + | decision | when | why not earlier | + |---|---|---| + | which XID (`resolve_override_target`) | surface creation | the swapchain's extent is read from the window (`surface_extent`), so a surface needs an answer to hand out caps | + | taking the window | first commit | a surface proves it is the game's content by painting | + + The first of the two is a reading of the X tree, and on the fallback path it + is a guess: `resolve_override_target` returns `provisional = true` when the + named window's root-child ancestor is degenerate and the target came from + pid/class/focus instead. That guess is re-asked at the first commit, when the + tree has the guest's own window in it; the answer replaces `GnSurface.window` + so later caps and later swapchains agree. Under `wine explorer + /desktop=shell` the ancestor is the full-size desktop window, so the + legitimate branch is taken and nothing is re-asked. + + `vkDestroySurfaceKHR` sends `gamescope_swapchain.destroy` and then + `wl_surface.destroy`. The order is load-bearing: the first nulls + `wl_surface_info->x11_surface`, so the second skips + `wlserver_x11_surface_info_finish()` and the window keeps its Xwayland main + surface. Reversed, the destroy finishes the window's surface info — + `main_surface = nullptr`, `wl_id = 0` — for the rest of the session. 5. **gamescope accepts it.** `gamescope_xwayland_server_t::handle_override_window_content` (`src/wlserver.cpp:836`) resolves the named XID the same way — `x11_window = x11_find_toplevel_for_xid( this->ctx->dpy, x11_window )` — diff --git a/icd_shim/src/proxy.rs b/icd_shim/src/proxy.rs index d17abd7..4b73241 100644 --- a/icd_shim/src/proxy.rs +++ b/icd_shim/src/proxy.rs @@ -764,6 +764,7 @@ unsafe fn commit_generated(sc: *mut GnSwapchain, slot_index: usize) { let parent = unsafe { (*(*sc).surf).parent }; let _sg = SEND_LOCK.lock().unwrap(); unsafe { + bind_window_at_first_commit(sc); wlproto::surface_attach(parent, (*s).buf, 0, 0); wlproto::surface_damage_buffer( parent, @@ -2004,6 +2005,47 @@ fn worker_main(sc: *mut GnSwapchain) { } } +/// Binds the swapchain's override surface to its X window, once per swapchain, +/// right before its first commit. gamescope keeps one surface per window, so a +/// swapchain that never presents (the Epic overlay's Present probe on a child +/// of the game's window) must not take the window from the one that does. +/// Called with `SEND_LOCK` held. +unsafe fn bind_window_at_first_commit(sc: *mut GnSwapchain) { + unsafe { + let surf = (*sc).surf; + if (*sc).window_bound || (*surf).swapchain_obj.is_null() { + return; + } + (*sc).window_bound = true; + // The resolution at surface creation had to read a tree the guest had + // not drawn into yet. On the fallback path that answer is a guess about + // which window is the game's, and this is the first instant there is + // evidence: the swapchain it is on is committing a frame. `wine + // explorer /desktop=shell` never takes the fallback (its root child is + // the full-size desktop window), so this costs one X round trip only on + // the guests that needed the fallback in the first place. + if (*surf).window_provisional { + let resolved = crate::x11::resolve_override_target((*surf).named_window); + if resolved.window != (*surf).window { + crate::logf!(gndrm_obf::obf!( + "override: XID {:#x} re-resolved at first commit: {:#x} -> {:#x}", + (*surf).named_window, + (*surf).window, + resolved.window + )); + } + (*surf).window = resolved.window; + (*surf).window_provisional = resolved.provisional; + } + // Single xwayland server (--xwayland-count 1) is always id 0. + wlproto::swapchain_override_window_content((*surf).swapchain_obj, 0, (*surf).window); + crate::logf!(gndrm_obf::obf!( + "override: swapchain {sc:?} binds surface {surf:?} to XID {:#x} at its first commit", + (*surf).window + )); + } +} + /// Commit a guest frame: into its X11 window on that route, otherwise attached /// to the surface and committed. The slot's own fence has already been waited. unsafe fn commit_real(sc: *mut GnSwapchain, idx: i32, fence_ms: u128) { @@ -2139,6 +2181,7 @@ unsafe fn commit_real(sc: *mut GnSwapchain, idx: i32, fence_ms: u128) { gamescope_present_mode(declared_present_mode(sc)), ); } + bind_window_at_first_commit(sc); wlproto::surface_attach(parent, (*s).buf, 0, 0); wlproto::surface_damage_buffer( parent, @@ -2364,6 +2407,7 @@ unsafe extern "C" fn gn_vkCreateSwapchainKHR( cv_put: Condvar::new(), cv_free: Condvar::new(), x11_trace: 0, + window_bound: false, next: ptr::null_mut(), }); let scp: *mut GnSwapchain = &mut *sc; @@ -3055,6 +3099,7 @@ unsafe fn present_sync_commit(sc: *mut GnSwapchain, slot: *mut GnSlot, idx: i32) gamescope_present_mode(declared_present_mode(sc)), ); } + bind_window_at_first_commit(sc); wlproto::surface_attach(parent, (*slot).buf, 0, 0); wlproto::surface_damage_buffer( parent, @@ -7632,6 +7677,15 @@ pub struct GnSurface { // The guest's X window this surface presents for, queried for its live // size by `surface_extent`; 0 on the wayland-surface route (no X window). pub window: u32, + // The XID winex11 named, kept for the re-ask in + // `bind_window_at_first_commit`: the resolution rule reads the tree, not the + // id, so asking again needs the window the guest pointed at rather than the + // answer it produced. + pub named_window: u32, + // `window` came out of the resolution rule's fallback, which reads the tree + // as it is at surface creation. A legitimate root-child ancestor is not a + // guess (`false`), and is never re-resolved. + pub window_provisional: bool, pub next: *mut GnSurface, } @@ -7724,6 +7778,8 @@ pub struct GnSwapchain { pub cv_put: Condvar, pub cv_free: Condvar, pub x11_trace: u32, + /// Set by `bind_window_at_first_commit`, under `SEND_LOCK`. + pub window_bound: bool, pub next: *mut GnSwapchain, /// Set once a destination could not be made: the set stops growing. pub gen_no_grow: bool, @@ -9598,6 +9654,8 @@ unsafe extern "C" fn gn_vkCreateWaylandSurfaceKHR( swapchain_obj: ptr::null_mut(), x11: ptr::null_mut(), window: 0, + named_window: 0, + window_provisional: false, next: ptr::null_mut(), }); let s = Box::into_raw(s); @@ -9667,10 +9725,10 @@ fn use_guest_surface() -> bool { /// The override/Wayland present route — the DEFAULT (`GN_VKSHIM_GUEST_SURFACE` /// unset, or any value other than exactly `1`; see `use_guest_surface`). -/// Creates the shim's OWN wl_surface, wraps it in a -/// gamescope_swapchain, and binds it to the guest's X window via -/// override_window_content; the compositor then routes this surface's commits -/// as that window's content. Presenting therefore shows the guest's frames +/// Creates the shim's OWN wl_surface and wraps it in a +/// gamescope_swapchain; a swapchain's first commit binds it to the guest's X +/// window via override_window_content (`bind_window_at_first_commit`), and the +/// compositor then routes this surface's commits as that window's content. Presenting therefore shows the guest's frames /// directly — wine's own GDI/2D window content is NOT composited (the known /// and accepted "chooses, never combines" consequence of this route). That /// GDI/2D gap is exactly what the guest-surface route (the opt-in) buys. @@ -9690,9 +9748,11 @@ unsafe fn xlib_override_surface( // winex11 named lands a 1x1 override-redirect wine helper for Dishonored; // `resolve_override_target` swaps a degenerate ancestor for the real, // painted window and leaves a legitimate one untouched (see x11.rs). - let bound_window = unsafe { crate::x11::resolve_override_target(window) }; + let target = unsafe { crate::x11::resolve_override_target(window) }; crate::logf!(gndrm_obf::obf!( - "override: XID {window:#x} -> gamescope override surface on {bound_window:#x} (default route; guest GDI/2D not composited)" + "override: XID {window:#x} -> gamescope override surface on {:#x}{} (default route; guest GDI/2D not composited)", + target.window, + if target.provisional { ", provisional" } else { "" } )); if !wl_setup(ptr::null_mut()) { crate::logf!(gndrm_obf::obf!( @@ -9720,9 +9780,6 @@ unsafe fn xlib_override_surface( return VK_ERROR_INITIALIZATION_FAILED; } let scobj = wlproto::swapchain_factory_create_swapchain(factory, surf); - // Single xwayland server (--xwayland-count 1) is always id 0. Bind to - // the window gamescope paints, not the one wine happened to name. - wlproto::swapchain_override_window_content(scobj, 0, bound_window); wlproto::wl_display_flush(dpy); drop(_sg); let s = Box::new(GnSurface { @@ -9734,7 +9791,9 @@ unsafe fn xlib_override_surface( x11: ptr::null_mut(), // The size gamescope must see this layer at is the window's, not // the guest's requested swapchain extent (see the note in caps). - window: bound_window, + window: target.window, + named_window: window, + window_provisional: target.provisional, next: ptr::null_mut(), }); let s = Box::into_raw(s); @@ -9745,7 +9804,7 @@ unsafe fn xlib_override_surface( } *out = s as usize as VkSurfaceKHR; crate::logf!(gndrm_obf::obf!( - "override: proxy surface {:?} (own wl_surface {:?} bound to XID {:#x}; Vulkan-only, guest GDI/2D not composited)", + "override: proxy surface {:?} (own wl_surface {:?} for XID {:#x}, bound at its first present; Vulkan-only, guest GDI/2D not composited)", s, surf, window @@ -9795,6 +9854,8 @@ unsafe fn x11_surface(gi: *mut GnInstance, window: u32, out: *mut VkSurfaceKHR) swapchain_obj: ptr::null_mut(), x11, window, + named_window: window, + window_provisional: false, next: ptr::null_mut(), }); let s = Box::into_raw(s); @@ -9903,10 +9964,10 @@ unsafe extern "C" fn gn_vkDestroySurfaceKHR( if !(*s).swapchain_obj.is_null() || !(*s).own_surface.is_null() { let _sg = SEND_LOCK.lock().unwrap(); if !(*s).swapchain_obj.is_null() { - wlproto::wl_proxy_destroy((*s).swapchain_obj); + wlproto::swapchain_destroy((*s).swapchain_obj); } if !(*s).own_surface.is_null() { - wlproto::wl_proxy_destroy((*s).own_surface); + wlproto::surface_destroy((*s).own_surface); } wlproto::wl_display_flush((*s).dpy); drop(_sg); diff --git a/icd_shim/src/wlproto.rs b/icd_shim/src/wlproto.rs index c480e4c..11fe1b7 100644 --- a/icd_shim/src/wlproto.rs +++ b/icd_shim/src/wlproto.rs @@ -315,6 +315,18 @@ pub unsafe fn swapchain_factory_create_swapchain( ) } +/// gamescope_swapchain.destroy (opcode 0), then the proxy. +pub unsafe fn swapchain_destroy(sc: *mut wl_proxy) { + wl_proxy_marshal_array(sc, 0, std::ptr::null_mut()); + wl_proxy_destroy(sc); +} + +/// wl_surface.destroy (opcode 0), then the proxy. +pub unsafe fn surface_destroy(surface: *mut wl_proxy) { + wl_proxy_marshal_array(surface, 0, std::ptr::null_mut()); + wl_proxy_destroy(surface); +} + /// gamescope_swapchain.override_window_content (opcode 1): server id, XID. pub unsafe fn swapchain_override_window_content(sc: *mut wl_proxy, server: u32, xid: u32) { let mut args = [wl_argument { u: server }, wl_argument { u: xid }]; diff --git a/icd_shim/src/x11.rs b/icd_shim/src/x11.rs index 21aeacf..5ff4f33 100644 --- a/icd_shim/src/x11.rs +++ b/icd_shim/src/x11.rs @@ -600,18 +600,33 @@ unsafe fn named_identity( (pid, cls, cls_len) } +/// The window the content override must bind to, and how the answer was found. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct OverrideTarget { + /// The root-child window gamescope will paint. + pub window: u32, + /// True when the named window's ancestor was degenerate, so the answer came + /// from the fallback rule (pid, class, input focus, `_NET_ACTIVE_WINDOW`). + /// That rule reads the tree as it is at this instant, so its answer is a + /// guess about the window the guest will render into — worth re-asking once + /// the guest has committed a frame. A legitimate ancestor is not a guess and + /// is never re-resolved. + pub provisional: bool, +} + /// The window the content override must bind to (see the section comment). /// /// Opens its own X connection; returns `named` unchanged when it cannot /// connect, so a failed resolve degrades to the previous behaviour rather than /// binding nothing. -pub unsafe fn resolve_override_target(named: u32) -> u32 { +pub unsafe fn resolve_override_target(named: u32) -> OverrideTarget { + let unresolved = OverrideTarget { window: named, provisional: false }; let conn = unsafe { xcb_connect(ptr::null(), ptr::null_mut()) }; if conn.is_null() || unsafe { xcb_connection_has_error(conn) } != 0 { if !conn.is_null() { unsafe { xcb_disconnect(conn) }; } - return named; + return unresolved; } let target = unsafe { resolve_override_target_on(conn, named) }; unsafe { xcb_disconnect(conn) }; @@ -656,9 +671,9 @@ pub unsafe fn window_extent(window: u32) -> Option<(u32, u32)> { /// focus / `_NET_ACTIVE_WINDOW` are accepted as that window when no /// pid/class match is found; otherwise the largest match wins. /// 4. No candidate -> return the ancestor (the caller logs the empty set). -unsafe fn resolve_override_target_on(conn: *mut xcb_connection_t, named: u32) -> u32 { +unsafe fn resolve_override_target_on(conn: *mut xcb_connection_t, named: u32) -> OverrideTarget { let Some(resolved) = (unsafe { resolve_target(conn, named) }) else { - return named; + return OverrideTarget { window: named, provisional: false }; }; let ancestor = resolved.window; let (aw, ah) = unsafe { window_size(conn, ancestor) }; @@ -666,7 +681,7 @@ unsafe fn resolve_override_target_on(conn: *mut xcb_connection_t, named: u32) -> crate::logf!(gndrm_obf::obf!( "override target: named=0x{named:x} ancestor=0x{ancestor:x} {aw}x{ah} -> 0x{ancestor:x} (legitimate; unchanged)" )); - return ancestor; + return OverrideTarget { window: ancestor, provisional: false }; } let pid_atom = unsafe { intern_atom(conn, b"_NET_WM_PID") }; @@ -677,14 +692,14 @@ unsafe fn resolve_override_target_on(conn: *mut xcb_connection_t, named: u32) -> let root = unsafe { window_root(conn, ancestor) }; if root == 0 { - return ancestor; + return OverrideTarget { window: ancestor, provisional: true }; } let active = unsafe { window_u32_prop(conn, root, active_atom) }; let focus = unsafe { input_focus(conn) }; let tree = unsafe { xcb_query_tree_reply(conn, xcb_query_tree(conn, root), ptr::null_mut()) }; if tree.is_null() { - return ancestor; + return OverrideTarget { window: ancestor, provisional: true }; } let n = unsafe { (*tree).children_len as usize }; let kids = unsafe { (tree as *const u8).add(32).cast::() }; @@ -758,7 +773,10 @@ unsafe fn resolve_override_target_on(conn: *mut xcb_connection_t, named: u32) -> "override target: named=0x{named:x} ancestor=0x{ancestor:x} {aw}x{ah} -> 0x{chosen:x} ({why}; self_pid={self_pid} named_pid={named_pid} named_class=\"{}\"; root_children:{report})", String::from_utf8_lossy(&named_cls[..named_cls_len]) )); - chosen + // Every answer from this half of the rule came out of the fallback, so the + // target is a reading of the tree as it is now: the guest has not committed + // a frame yet, and the window it will render into may not even be mapped. + OverrideTarget { window: chosen, provisional: true } } // ---------------------------------------------------------------- the route @@ -1516,9 +1534,13 @@ mod tests { let chosen = unsafe { resolve_override_target_on(conn, named) }; assert_eq!( - chosen, game, + chosen.window, game, "a degenerate 1x1 ancestor is swapped for the painted 640x480 window" ); + assert!( + chosen.provisional, + "a fallback answer is a reading of the tree, and is re-asked at the first commit" + ); // A legitimate (full-size) ancestor is returned unchanged. let legit = unsafe { xcb_generate_id(conn) }; @@ -1528,8 +1550,8 @@ mod tests { unsafe { xcb_flush(conn) }; assert_eq!( unsafe { resolve_override_target_on(conn, lclient) }, - legit, - "a non-degenerate ancestor is returned unchanged" + OverrideTarget { window: legit, provisional: false }, + "a non-degenerate ancestor is returned unchanged, and is not a guess" ); unsafe {