🐛 Open the window through a window system its GPU presents to

A laptop's NVIDIA GPU beside the Intel GPU KWin composites on presented to
X11 and not to Wayland (a user's report: its Wayland surface was not
presented to, its XCB and Xlib surfaces were). SDL opened the window through
Wayland, the swapchain was refused, and the window never appeared: Wayland
maps a window only once a frame is presented to it, so its task bar entry
alone showed.

Before SDL's video starts, the GPU of the first window's sink is now asked,
with no window, whether it presents to the session's Wayland compositor,
and where it does not, to its X server: Vulkan's per-platform
presentation-support questions, over connections the window crate makes and
closes. The windows open through Wayland where it presents to the
compositor, else through X11 (XWayland under a Wayland compositor) where it
presents to the X server, and where it presents to neither, no window opens
and the guest runs headless. A named Wayland also holds where the
compositor has no fifo-v1 and SDL would take XWayland on its own.

Each window also tells QEMU's stderr once whether it shows: "reims-vgpu:
window shown: display N" at its first frame presented, or "reims-vgpu: no
window: <why>" where it stays empty, or where no window opens because the
GPU presents to neither window system. reims-ctl reads them to say whether
a VM's window is open.

The sink is attached before the host's screens are asked for, so the first
window's attach starts the UI thread with its GPU.
This commit is contained in:
Anees Iqbal committed 2026-12-02 02:33:01 -08:00
1 parent 700bd8bc36
commit 09f166332e
19 files changed
+883 -51

No files matched your search

@@ -14,6 +14,11 @@
//! complete, from present fences or Vulkan's rules ([`Presents`], [`Fences`]), and the one lock
//! every call into the window system runs under ([`WSI`]), which the two platforms' windows share.
//!
//! **Which window systems the device presents to** is asked before a window opens, with no
//! surface ([`RtSwapchain::presents_to`]): on Linux the window opens through a window system whose
//! windows the device's queue family presents to (reims-vgpu-window's choice), since a window
//! whose surface it does not present to gets no swapchain and stays empty.
//!
//! **The present mode is logged** at info at the first swapchain made, with the images asked
//! for and the images made; a later make logs it again only where its counts differ from the
//! last line's. Which mode a window presents with depends on what its surface offers, and no
@@ -75,6 +80,8 @@
//! are destroyed before their device, which rt destroys at the host's let-go of it. None waits
//! first: nothing runs on a lost device any more.
use std::ffi::{c_ulong, c_void};
use ash::{khr, vk};
use metal2vulkan_runtime as rt;
@@ -85,8 +92,8 @@ use crate::core::sync::MutexGuard;
use crate::core::trace;
use crate::swapchain::{
Acquired, COLOR_SPACE, END_WAIT_NS, FIFO_IMAGES, FORMAT, Fences, Frame, MAILBOX_IMAGES,
Presents, RETIRED, Refused, Remaking, USAGE, Unmade, WAIT_NS, WSI, extent_for, present_mode,
refused, remake, take_image,
Presents, RETIRED, Refused, Remaking, USAGE, Unmade, WAIT_NS, WSI, WindowSystem, extent_for,
present_mode, refused, remake, take_image,
};
use super::RtObj;
@@ -357,6 +364,76 @@ impl RtSwapchain {
unsafe { RtSwapchain::new(device, surface) }
}
/// Whether `device`'s queue family, the one a swapchain presents on ([`RtSwapchain::new`]),
/// presents to windows of `system`, asked before any window is made and with no surface:
/// Vulkan's per-platform question, `vkGetPhysicalDeviceWaylandPresentationSupportKHR` or
/// `vkGetPhysicalDeviceXlibPresentationSupportKHR`. `false` where rt's instance did not enable
/// the window system's surface extension (the loader lists none), as no window of it can then
/// have a surface on the instance. Asked under [`WSI`], as every call into the window system.
///
/// # Safety
///
/// `system`'s connection is live, and no other thread uses it during the call.
pub unsafe fn presents_to(device: &RtObj<RtDevice>, system: WindowSystem) -> bool {
let (extension, command) = match system {
WindowSystem::Wayland { .. } => (
khr::wayland_surface::NAME,
c"vkGetPhysicalDeviceWaylandPresentationSupportKHR",
),
WindowSystem::Xlib { .. } => (
khr::xlib_surface::NAME,
c"vkGetPhysicalDeviceXlibPresentationSupportKHR",
),
};
if !(device.instance().extensions().iter()).any(|e| e.as_c_str() == extension) {
return false;
}
let (entry, instance) = device.instance().raw();
let raw = device.rt().raw();
let (physical, family) = (raw.physical_device, raw.queue_family_index);
let _wsi = WSI.lock();
// SAFETY: a live instance, which enabled the extension the command is of.
let found = unsafe { entry.get_instance_proc_addr(instance.handle(), command.as_ptr()) };
let Some(found) = found else {
return false;
};
let answer = match system {
WindowSystem::Wayland { display } => {
// SAFETY: the command of that name, of this type (vulkan_wayland.h), called with a
// physical device and a queue family of the instance's, and the caller's live
// `wl_display`.
unsafe {
let ask = std::mem::transmute::<
unsafe extern "system" fn(),
unsafe extern "system" fn(
vk::PhysicalDevice,
u32,
*mut c_void,
) -> vk::Bool32,
>(found);
ask(physical, family, display)
}
}
WindowSystem::Xlib { display, visual } => {
// SAFETY: as above (vulkan_xlib.h, whose `VisualID` is Xlib's, an unsigned long),
// with the caller's live `Display`.
unsafe {
let ask = std::mem::transmute::<
unsafe extern "system" fn(),
unsafe extern "system" fn(
vk::PhysicalDevice,
u32,
*mut c_void,
c_ulong,
) -> vk::Bool32,
>(found);
ask(physical, family, display, visual)
}
}
};
answer == vk::TRUE
}
/// Whether the swapchain presents with MAILBOX (else FIFO).
pub fn is_mailbox(&self) -> bool {
self.mode == vk::PresentModeKHR::MAILBOX
+19
View File
@@ -279,6 +279,25 @@ impl Presents {
}
}
/// A connection to a window system of the host's session, which a question about presenting to its
/// windows names before any window is made (the rt platform's swapchain's `presents_to`): on Linux
/// the Wayland compositor's or the X server's, which a window may open through.
#[derive(Clone, Copy, Debug)]
pub enum WindowSystem {
/// A Wayland compositor: its `wl_display`.
Wayland {
/// The compositor's `wl_display`.
display: *mut std::ffi::c_void,
},
/// An X server, through Xlib: its `Display`, and the visual a window takes there.
Xlib {
/// The server's Xlib `Display`.
display: *mut std::ffi::c_void,
/// The visual's ID, Xlib's `VisualID`.
visual: std::ffi::c_ulong,
},
}
/// Why a swapchain is not made.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refused(pub String);
+26
View File
@@ -45,6 +45,14 @@ Unset or unparsable values give the defaults below.
Input. `manual`
- The title is "Reims vGPU", with " (port N)" for a display on port N above
0. `test: handle::a_window_s_title_is_reims_vgpu_and_its_port`
- Each window tells QEMU's stderr once whether it shows:
`reims-vgpu: window shown: display N` at its first frame presented, or
`reims-vgpu: no window:` and why, where it stays empty, drawing no more
before a frame (no swapchain, swapchains that end again and again, a lost
device). reims-ctl's start says a VM's window is open only after the first
line, and otherwise why there is none, on every host.
`test: present::the_host_is_told_once_whether_the_window_shows`,
`test: told::the_window_s_lines_start_as_the_host_reads_them`
- A new window shows its mode a guest pixel per host point, shrunk to fit
the screen's visible area, on whole pixels, a multiple of 16 wide and even
high (the steps of the guest's modes, below): on a Retina screen the
@@ -500,6 +508,24 @@ runtime's own device. The switches above apply, and `resolution=` too.
Ctrl+C in QEMU's terminal ends QEMU as before. Without a Wayland
compositor or an X server, no window opens, the guest runs headless, and
the reason is logged once. `manual`
- The window system: before the first window opens, its GPU is asked, with
no window, whether it presents to the session's Wayland compositor, and
where it does not, to its X server (Vulkan's per-platform
presentation-support questions). The windows open through Wayland where it
presents to the compositor, also where the compositor has no fifo-v1 and
SDL would take XWayland on its own; else through X11, XWayland's under a
Wayland compositor, where it presents to the X server (a laptop's NVIDIA
GPU beside the Intel GPU KWin composites on, in a user's report). Where it
presents to neither, no window opens, the guest runs headless, and QEMU's
stderr says so in a `reims-vgpu: no window:` line ("this GPU cannot show a
window on this desktop"): a window it does not present to would stay
empty, and on Wayland would never appear, its task bar entry alone
showing. SDL's own
`SDL_VIDEO_DRIVER` still names its driver over the choice. The answers and
the choice are logged.
`test: winsys::the_window_opens_through_a_window_system_its_gpu_presents_to`,
`test: winsys::a_gpu_presenting_to_x11_alone_opens_its_window_through_x11`,
`manual` (a window through X11 on an NVIDIA GPU beside the compositor's)
- The title, the first size (a guest point per host point, shrunk to fit
the screen less 48 points for the title bar and a panel, on whole pixels)
and the screen a display opens on are as above. The size is asked for in
+14
View File
@@ -254,6 +254,8 @@ pub struct Window {
cursor_posted: AtomicBool,
/// How many frames the window was asked to draw ([`Window::new_frame`]).
frames_asked: AtomicU64,
/// Set at the window's first frame presented ([`Window::shown`]).
shown: AtomicBool,
/// What the render thread draws with.
surface: Surface,
/// What it posts to the UI thread.
@@ -298,6 +300,7 @@ impl Window {
open_posted: AtomicBool::new(false),
cursor_posted: AtomicBool::new(false),
frames_asked: AtomicU64::new(0),
shown: AtomicBool::new(false),
surface: Surface::new(),
posts,
})
@@ -352,6 +355,17 @@ impl Window {
self.frames_asked.load(Ordering::Relaxed)
}
/// Whether the window presented its first frame: from then on it shows the display, where a
/// window on Wayland is not even mapped before (the host is told so on stderr, `crate::told`).
pub fn shown(&self) -> bool {
self.shown.load(Ordering::Acquire)
}
/// The render thread presented the window's first frame ([`Window::shown`]).
pub(crate) fn set_shown(&self) {
self.shown.store(true, Ordering::Release);
}
/// The display offers the guest `width` by `height` first: the window opens sized for it
/// until the guest sets a mode. It sizes a window that has opened already for nothing.
pub fn offered_mode(&self, width: u16, height: u16) {
+7 -3
View File
@@ -23,12 +23,14 @@
//! rectangle and the pointer's position in it), [`keymap`] (macOS key codes to USB usages),
//! [`keys`] (modifiers and the keys held down), [`capture`] (who gets the keyboard, and where
//! the pointer may go), [`beyond`] (a captured pointer past the image's edge, onto the guest's
//! other displays), [`scroll`] (wheel and trackpad deltas to notches or smooth scrolling)
//! and [`env`](mod@env) (the `REIMS_VGPU_WINDOW_*` variables);
//! other displays), [`scroll`] (wheel and trackpad deltas to notches or smooth scrolling),
//! [`env`](mod@env) (the `REIMS_VGPU_WINDOW_*` variables) and [`winsys`] (on Linux, the window
//! system the windows open through: one their GPU presents to);
//! - a close the person asks for, which ends the VM, and the question asked before it: [`close`]
//! (when the window asks, its words, and what an answer does), with each OS's question in its
//! `native` side: on Linux one the library draws in the window (`native/sdl_thread/question`);
//! - the host's screens ([`screens`]), as the OS lists them;
//! - what the host is told on stderr of whether a window shows (`told`);
//! - the window itself: [`handle`] (the [`Window`] the display holds), and its OS side
//! (`native`, one directory per OS behind one file). On macOS (`native/macos/`) that is the
//! AppKit side, on the main thread but for the render thread: `main_thread` (work posted to the
@@ -60,6 +62,8 @@ mod native;
mod present;
pub mod screens;
pub mod scroll;
mod told;
pub mod winsys;
// The SDL3 window: Linux's and Windows's (`native/sdl_thread.rs`), and macOS's with the
// `macos-sdl` feature, behind `REIMS_VGPU_WINDOW_IMPL`.
@@ -72,7 +76,7 @@ pub use config::{
WheelFn, WindowCallbacks, WindowConfig,
};
pub use handle::{DrawnCursor, DrawnOverlay, FrameSink, Glyph, PixelRect, Window};
pub use present::{Drawn, Presenter, VulkanTarget};
pub use present::{Drawn, Presenter, VulkanTarget, WindowSystem};
pub use screens::HostScreen;
#[cfg(test)]
+6 -5
View File
@@ -10,7 +10,8 @@
//! library's UI thread (`reims-vgpu.ui`) but for the render thread, which draws into the
//! window's Vulkan swapchain; the window's shell and cursor are SDL's shared ones (`sdl`), with
//! the SDL thread's side in `native/sdl_thread/sdl.rs`, and what each OS adds in its own file
//! (`native/linux.rs`: the GPU a Wayland compositor composites on; `native/windows.rs`).
//! (`native/linux.rs`: the window system the windows open through, and the GPU a Wayland
//! compositor composites on; `native/windows.rs`).
#[cfg(target_os = "linux")]
mod linux;
@@ -21,12 +22,12 @@ mod sdl_thread;
#[cfg(target_os = "windows")]
mod windows;
// What the OS adds under the SDL thread's windows: the GPU its compositor composites on, and its
// names for the release chord's keys in the title.
// What the OS adds under the SDL thread's windows: the window system they open through, the GPU
// its compositor composites on, and its names for the release chord's keys in the title.
#[cfg(target_os = "linux")]
use linux::{HOST_OS, compositor_device};
use linux::{HOST_OS, compositor_device, window_system};
#[cfg(target_os = "windows")]
use windows::{HOST_OS, compositor_device};
use windows::{HOST_OS, compositor_device, window_system};
#[cfg(all(target_os = "macos", feature = "macos-sdl"))]
pub(crate) use macos::sdl as sdl_os;
+7 -3
View File
@@ -1,11 +1,15 @@
//! The window on Linux: SDL3's, on Wayland or X11, on the library's UI thread
//! (`super::sdl_thread`), and what only Linux has: the GPU a Wayland compositor composites on
//! (`feedback`), which a window's render thread asks for (`compositor_device`), and Linux's names
//! for the release chord's keys in the title ([`HOST_OS`]).
//! (`super::sdl_thread`), and what only Linux has: the window system the windows open through,
//! one the GPU presents to (`winsys`), which the UI thread asks for as SDL's video starts
//! (`window_system`), the GPU a Wayland compositor composites on (`feedback`), which a window's
//! render thread asks for (`compositor_device`), and Linux's names for the release chord's keys in
//! the title ([`HOST_OS`]).
mod feedback;
mod winsys;
pub(super) use feedback::main_device_of as compositor_device;
pub(super) use winsys::window_system;
use crate::capture::HostOs;
@@ -0,0 +1,209 @@
//! The window system SDL's windows open through on Linux ([`window_system`], the choice of
//! `crate::winsys`): before SDL's video starts, the GPU of the first window's sink is asked whether
//! it presents to the session's Wayland compositor, and where it does not, to its X server, over
//! connections of this module's own, each made for the question and closed after it
//! ([`answers`]). libwayland-client
//! and libX11 are the libraries SDL opens, opened here by their names and their calls looked up
//! by name, so nothing is linked; a library or a call missing, or a connection refused, is a window
//! system the session does not have. The X server is asked about its default screen's default
//! visual, the one SDL's window takes.
//!
//! Where the session hands its Wayland connection over in `WAYLAND_SOCKET`, which a client can
//! take once, nothing is asked and SDL picks: a connection of this module's would take it from
//! SDL.
use std::ffi::{CStr, c_char, c_int, c_ulong, c_void};
use std::sync::OnceLock;
use reims_vgpu_vulkan::swapchain::WindowSystem;
use crate::present::FrameSink;
use crate::winsys::{Answers, Choice, asks_x11, choose};
/// The window system SDL's windows open through, from what the GPU of `sink`, the first window's,
/// answers ([`answers`], `crate::winsys`), logged at info with the answers; SDL's own pick where
/// there is no sink, its GPU cannot be asked, or the session's Wayland connection is handed over.
pub(crate) fn window_system(sink: Option<&dyn FrameSink>) -> Choice {
if std::env::var_os("WAYLAND_SOCKET").is_some() {
return Choice::Sdls;
}
let Some(answers) = sink.and_then(answers) else {
return Choice::Sdls;
};
let choice = choose(Some(answers));
log::info!(
target: "reims_vgpu_window",
"the windows' GPU presents to the Wayland compositor: {}; to the X server: {}; the windows \
open through {}",
said(answers.wayland, "no compositor"),
if asks_x11(answers.wayland) {
said(answers.x11, "no X server")
} else {
"not asked"
},
match choice {
Choice::Sdls => "SDL's own pick",
Choice::Wayland => "Wayland",
Choice::X11 => "X11",
Choice::Neither => "neither, so no window opens",
}
);
choice
}
/// An answer as the log says it: `yes`, `no`, or `absent` where the session has no such window
/// system.
fn said(answer: Option<bool>, absent: &'static str) -> &'static str {
match answer {
Some(true) => "yes",
Some(false) => "no",
None => absent,
}
}
/// Whether the GPU of `sink` presents to the session's Wayland compositor, and where it does not,
/// to its X server (`crate::winsys`), `None` for one the session does not have; `None` where the
/// sink has no GPU to ask.
fn answers(sink: &dyn FrameSink) -> Option<Answers> {
let mut asked = true;
let mut ask = |system| {
let answer = sink.presents_to(system);
asked &= answer.is_some();
answer.unwrap_or(false)
};
// Each connection is live, and this thread's own, during its question.
let wayland = with_wayland(|display| ask(WindowSystem::Wayland { display }));
let x11 = if asks_x11(wayland) {
with_xlib(|display, visual| ask(WindowSystem::Xlib { display, visual }))
} else {
None
};
asked.then_some(Answers { wayland, x11 })
}
/// Opens `name`, a library SDL opens too, for the process: `None` where it is not installed.
fn open(name: &CStr) -> Option<Library> {
// SAFETY: a library of the window system's, opened by its name; it stays loaded for the
// process, as SDL's own opening of it keeps it.
let lib = unsafe { libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL) };
(!lib.is_null()).then_some(Library(lib))
}
/// An opened library's handle.
struct Library(*mut c_void);
impl Library {
/// The call `name`, read as a `T`, a function pointer of its C type.
///
/// # Safety
///
/// `T` is the C type of the library's symbol `name`, a function pointer.
unsafe fn call<T: Copy>(&self, name: &CStr) -> Option<T> {
// SAFETY: a live handle and a symbol name.
let p = unsafe { libc::dlsym(self.0, name.as_ptr()) };
// SAFETY: the caller's: `T` is the symbol's type, a pointer's size.
(!p.is_null()).then(|| unsafe { std::mem::transmute_copy::<*mut c_void, T>(&p) })
}
}
/// `wl_display_connect`.
type Connect = unsafe extern "C" fn(*const c_char) -> *mut c_void;
/// `wl_display_disconnect`.
type Disconnect = unsafe extern "C" fn(*mut c_void);
/// libwayland-client's calls this module makes.
#[derive(Clone, Copy)]
struct Wayland {
connect: Connect,
disconnect: Disconnect,
}
/// `XOpenDisplay`.
type OpenDisplay = unsafe extern "C" fn(*const c_char) -> *mut c_void;
/// `XDefaultScreen`.
type DefaultScreen = unsafe extern "C" fn(*mut c_void) -> c_int;
/// `XDefaultVisual`.
type DefaultVisual = unsafe extern "C" fn(*mut c_void, c_int) -> *mut c_void;
/// `XVisualIDFromVisual`.
type VisualId = unsafe extern "C" fn(*mut c_void) -> c_ulong;
/// `XCloseDisplay`.
type CloseDisplay = unsafe extern "C" fn(*mut c_void) -> c_int;
/// libX11's calls this module makes.
#[derive(Clone, Copy)]
struct Xlib {
open_display: OpenDisplay,
default_screen: DefaultScreen,
default_visual: DefaultVisual,
visual_id: VisualId,
close_display: CloseDisplay,
}
/// libwayland-client's calls, looked up once: `None` where the library or a call is missing.
fn wayland_calls() -> Option<Wayland> {
static CALLS: OnceLock<Option<Wayland>> = OnceLock::new();
*CALLS.get_or_init(|| {
let lib = open(c"libwayland-client.so.0")?;
// SAFETY: each is libwayland-client's call of that name, of the C type it is read as
// (wayland-client-core.h).
unsafe {
Some(Wayland {
connect: lib.call(c"wl_display_connect")?,
disconnect: lib.call(c"wl_display_disconnect")?,
})
}
})
}
/// libX11's calls, looked up once: `None` where the library or a call is missing.
fn xlib_calls() -> Option<Xlib> {
static CALLS: OnceLock<Option<Xlib>> = OnceLock::new();
*CALLS.get_or_init(|| {
let lib = open(c"libX11.so.6")?;
// SAFETY: each is libX11's call of that name, of the C type it is read as (Xlib.h).
unsafe {
Some(Xlib {
open_display: lib.call(c"XOpenDisplay")?,
default_screen: lib.call(c"XDefaultScreen")?,
default_visual: lib.call(c"XDefaultVisual")?,
visual_id: lib.call(c"XVisualIDFromVisual")?,
close_display: lib.call(c"XCloseDisplay")?,
})
}
})
}
/// `ask` with a connection to the session's Wayland compositor (`WAYLAND_DISPLAY`'s), made for it
/// and closed after: `None` where there is none to connect to.
fn with_wayland(ask: impl FnOnce(*mut c_void) -> bool) -> Option<bool> {
let calls = wayland_calls()?;
// SAFETY: libwayland-client's call; NULL connects to the session's compositor.
let display = unsafe { (calls.connect)(std::ptr::null()) };
if display.is_null() {
return None;
}
let answer = ask(display);
// SAFETY: the connection made above, used by no one else.
unsafe { (calls.disconnect)(display) };
Some(answer)
}
/// `ask` with a connection to the session's X server (`DISPLAY`'s), made for it and closed after,
/// and its default screen's default visual: `None` where there is none to connect to.
fn with_xlib(ask: impl FnOnce(*mut c_void, c_ulong) -> bool) -> Option<bool> {
let calls = xlib_calls()?;
// SAFETY: libX11's call; NULL connects to the session's server.
let display = unsafe { (calls.open_display)(std::ptr::null()) };
if display.is_null() {
return None;
}
// SAFETY: the live connection made above, and the screen and visual it names.
let visual = unsafe {
let screen = (calls.default_screen)(display);
(calls.visual_id)((calls.default_visual)(display, screen))
};
let answer = ask(display, visual);
// SAFETY: the connection made above, used by no one else.
unsafe { (calls.close_display)(display) };
Some(answer)
}
@@ -272,7 +272,7 @@ pub(crate) fn start(window: Arc<Window>) {
fn run(window: &Window) {
let surface = window.surface();
let mut layer: Option<LayerRef> = None;
let mut presenting = Presenting::default();
let mut presenting = Presenting::new(window.port());
let mut retry = None;
while let Some(work) = surface.next(layer.is_none(), retry.take()) {
// A frame is a step of the exit gate's: the process's exit waits for the frame in
@@ -303,6 +303,9 @@ fn run(window: &Window) {
) {
retry = Some(work.layout);
}
if presenting.shown() {
window.set_shown();
}
}
// The swapchain and the surface go before the layer under them.
drop(presenting);
@@ -30,9 +30,11 @@ use crate::screens::HostScreen;
use crate::sdl::shell;
/// Starts `window`'s render thread and posts its opening to the UI thread, starting that where it
/// has not started.
/// has not started, with the window's sink, whose GPU the window system the windows open through
/// is chosen by (`ui::start_presenting`).
pub(crate) fn open(window: &Arc<Window>) {
render::start(Arc::clone(window));
ui::start_presenting(window.sink());
let window = Arc::downgrade(window);
ui::post(move || {
if let Some(window) = window.upgrade() {
@@ -270,7 +270,7 @@ pub(crate) fn start(window: Arc<Window>) {
/// the window's release to the UI thread.
fn run(window: &Window) {
let surface = window.surface();
let mut presenting = Presenting::default();
let mut presenting = Presenting::new(window.port());
let mut retry = None;
while let Some(work) = surface.next(retry.take()) {
// A frame is a step of the exit gate's: the process's exit waits for the frame in
@@ -295,6 +295,9 @@ fn run(window: &Window) {
) {
retry = Some(work);
}
if presenting.shown() {
window.set_shown();
}
#[cfg(target_os = "linux")]
if presenting.is_ended() && surface.tell_unshown() {
let id = window.id();
@@ -19,9 +19,15 @@
//! window system accelerates it, so a captured pointer gone past the image's edge
//! (`crate::beyond`) moves on as fast as it moved over the image.
//!
//! Where SDL's video does not start (no Wayland compositor and no X server to reach on Linux), the
//! reason is logged once, no window opens, the work posted is dropped, and the host's screens are
//! none.
//! **The window system** the windows open through on Linux is chosen as SDL's video starts, by the
//! GPU of the sink of the window whose opening starts the UI thread (`crate::winsys`,
//! `super::super::window_system`): one it presents to, Wayland first, named to SDL as its video
//! driver. Where it presents to none of the session's, SDL's video is not started, so no window
//! opens to stay empty, and the host is told so on stderr (`crate::told`).
//!
//! Where SDL's video does not start (no Wayland compositor and no X server to reach on Linux, or
//! none the GPU presents to), the reason is logged once, no window opens, the work posted is
//! dropped, and the host's screens are none.
//!
//! **The host's screens** ([`screens`]): a snapshot the UI thread takes once SDL's video starts
//! and again when SDL reports a screen added, removed, moved or changed. The main screen (SDL's
@@ -32,6 +38,7 @@
use std::cell::Cell;
use std::collections::VecDeque;
use std::ffi::CStr;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;
@@ -40,8 +47,11 @@ use reims_vgpu_vulkan::core::sync::{Condvar, Mutex};
use reims_vgpu_vulkan::os::OsThreads;
use sdl3_sys::everything::*;
use crate::present::FrameSink;
use crate::screens::HostScreen;
use crate::sdl::sdl_error;
use crate::told;
use crate::winsys::Choice;
/// The UI thread's stack.
const STACK: usize = 1024 * 1024;
@@ -100,6 +110,14 @@ pub(crate) fn on_ui_thread() -> bool {
/// Starts the UI thread, once.
pub(crate) fn start() {
start_presenting(None);
}
/// Starts the UI thread, once, choosing the window system the windows open through by the GPU of
/// `sink`, the sink of the window whose opening starts it (the module doc); SDL picks where the
/// start has none, as a look at the host's screens has. The thread holds the sink only while it
/// chooses.
pub(crate) fn start_presenting(sink: Option<Arc<dyn FrameSink>>) {
{
let mut queue = QUEUE.lock();
if queue.phase != Phase::Idle {
@@ -107,7 +125,13 @@ pub(crate) fn start() {
}
queue.phase = Phase::Starting;
}
spawn_named(&OsThreads::new(), "reims-vgpu.ui", STACK, Box::new(()), run);
spawn_named(
&OsThreads::new(),
"reims-vgpu.ui",
STACK,
Box::new(()),
move || run(sink),
);
}
/// Posts `job` to the UI thread, starting it where it has not started: it runs after the work
@@ -170,9 +194,27 @@ fn set_hints() {
}
}
/// Starts SDL's video: whether it started.
fn start_video() -> bool {
/// What the host is told where the GPU presents to none of the session's window systems.
const PRESENTS_TO_NEITHER: &str = "this GPU cannot show a window on this desktop: it presents to \
neither its Wayland compositor nor its X server";
/// Starts SDL's video, through the window system `sink`'s GPU presents to (the module doc):
/// whether it started.
fn start_video(sink: Option<&dyn FrameSink>) -> bool {
set_hints();
let choice = super::super::window_system(sink);
if choice == Choice::Neither {
log::warn!(
target: "reims_vgpu_window",
"SDL's video does not start: {PRESENTS_TO_NEITHER}, so no window opens"
);
told::none(PRESENTS_TO_NEITHER);
return false;
}
if let Some(driver) = choice.driver() {
// SAFETY: NUL-terminated strings SDL copies, before its video starts.
unsafe { SDL_SetHint(c"SDL_VIDEO_DRIVER".as_ptr(), driver.as_ptr()) };
}
// SAFETY: SDL's video starts on this thread, which becomes its main thread; only the hints,
// which SDL takes on any thread before it starts, came first.
if !unsafe { SDL_Init(SDL_INIT_VIDEO) } {
@@ -198,26 +240,39 @@ fn start_video() -> bool {
log::info!(
target: "reims_vgpu_window",
"windows open through SDL's {driver} driver{}",
driver_note(&driver, std::env::var_os("WAYLAND_DISPLAY").is_some())
driver_note(
&driver,
std::env::var_os("WAYLAND_DISPLAY").is_some(),
choice
)
);
true
}
/// What the log adds to SDL's `driver`: where a Wayland compositor runs (`wayland_session`) and SDL
/// took X11, that the windows are XWayland's, and why (`super::sdl`'s module doc).
fn driver_note(driver: &str, wayland_session: bool) -> &'static str {
if driver == "x11" && wayland_session {
", through XWayland: SDL takes it where the Wayland compositor has no fifo-v1 \
(SDL_VIDEO_DRIVER=wayland asks for Wayland)"
} else {
""
/// took X11, that the windows are XWayland's, and why: the GPU presents to X11 and not to Wayland
/// (`choice`), or SDL took it on its own (`super::sdl`'s module doc).
fn driver_note(driver: &str, wayland_session: bool, choice: Choice) -> &'static str {
match (driver, wayland_session, choice) {
("x11", true, Choice::X11) => {
", through XWayland: the GPU presents to the X server and not to the Wayland \
compositor"
}
("x11", true, _) => {
", through XWayland: SDL takes it where the Wayland compositor has no fifo-v1 \
(SDL_VIDEO_DRIVER=wayland asks for Wayland)"
}
_ => "",
}
}
/// The UI thread's body.
fn run() {
/// The UI thread's body, starting SDL's video through the window system `sink`'s GPU presents to
/// (the module doc), the sink let go of once it is chosen.
fn run(sink: Option<Arc<dyn FrameSink>>) {
ON_UI.with(|on| on.set(true));
if !start_video() {
let started = start_video(sink.as_deref());
drop(sink);
if !started {
QUEUE.lock().phase = Phase::Failed;
drop(std::mem::take(&mut QUEUE.lock().jobs));
*SCREENS.lock() = Some(Vec::new());
@@ -361,13 +416,16 @@ fn mode_pixels(points: i32, density: f64) -> u32 {
mod tests {
use super::*;
/// X11 in a Wayland session is named XWayland, with why SDL took it; otherwise the driver
/// X11 in a Wayland session is named XWayland, with why: the GPU presents to X11 and not to
/// Wayland, or SDL took it; otherwise the driver
/// stands alone
#[test]
fn x11_in_a_wayland_session_says_xwayland() {
assert!(driver_note("x11", true).starts_with(", through XWayland"));
assert_eq!(driver_note("x11", false), "");
assert_eq!(driver_note("wayland", true), "");
assert!(driver_note("x11", true, Choice::Sdls).contains("no fifo-v1"));
assert!(driver_note("x11", true, Choice::X11).contains("not to the Wayland compositor"));
assert_eq!(driver_note("x11", false, Choice::Sdls), "");
assert_eq!(driver_note("x11", false, Choice::X11), "");
assert_eq!(driver_note("wayland", true, Choice::Wayland), "");
}
/// SDL is not told to scale cursors by the screen's content scale: the window gives a
@@ -71,6 +71,12 @@ pub(super) unsafe fn compositor_device(_window: *mut SDL_Window) -> Option<(u32,
None
}
/// The window system the windows open through: Windows has one, Win32's, and SDL picks it
/// (`crate::winsys`).
pub(super) fn window_system(_sink: Option<&dyn crate::FrameSink>) -> crate::winsys::Choice {
crate::winsys::Choice::Sdls
}
/// `ITaskbarList2`'s interface ID, as the Windows SDK's `shobjidl.h` defines it (windows-sys has
/// the taskbar list's class ID, `CLSID_TaskbarList`, but no COM interfaces).
const IID_ITASKBARLIST2: GUID = GUID::from_u128(0x602d4995_b13a_429b_a66e_1935e44f4317);
+105 -3
View File
@@ -17,12 +17,21 @@
//! window's end, unasked, so the window keeps the frame it shows, its swapchain made no Vulkan call
//! on the lost device since, and no presenter is made again: one would make a new surface and
//! swapchain on the lost device.
//!
//! **What the host is told**, once a window, on stderr (`crate::told`): that the window shows, at
//! its first frame presented, or that it stays empty, where it draws no more before one (the sink
//! made no presenter, its presenters ended [`RENEWALS`] times in a row, or its device was lost).
//! A window whose surface the GPU does not present to, on Wayland, never appears at all: the
//! compositor maps a window only once a frame is presented to it. reims-ctl's start says its
//! window is open only on the first line.
use std::time::Duration;
use ash::vk;
pub use reims_vgpu_vulkan::swapchain::WindowSystem;
use crate::handle::{DrawnCursor, DrawnOverlay, PixelRect};
use crate::told;
/// How long the render thread waits before it draws again where no image came in time: about a
/// refresh at 120 Hz. Before it answers [`Drawn::Retry`] for want of an image, the presenter has
@@ -81,6 +90,14 @@ pub trait FrameSink: Send + Sync {
/// sink's instance and a swapchain over it. `None` where there is none (the display has ended,
/// or this host has no swapchain): the window then stays empty.
fn presenter(&self, target: &dyn VulkanTarget) -> Option<Box<dyn Presenter>>;
/// Whether the GPU the sink presents from presents to windows of `system`, a window system of
/// the host's session, asked before any window opens (on Linux, which window system a window
/// opens through: `crate::winsys`). `None` where the sink has no GPU to ask.
fn presents_to(&self, system: WindowSystem) -> Option<bool> {
let _ = system;
None
}
}
/// What makes a window's Vulkan surface, on its render thread: on Linux SDL's window, on macOS
@@ -108,6 +125,8 @@ pub trait VulkanTarget {
/// up to [`RENEWALS`] times in a row (the module doc).
#[derive(Default)]
pub(crate) struct Presenting {
/// The display whose window it presents, which the host is told of (`crate::told`).
display: u64,
/// The presenter, once made.
presenter: Option<Box<dyn Presenter>>,
/// The window stays as it is: the sink made no presenter, the presenters ended too many
@@ -115,9 +134,29 @@ pub(crate) struct Presenting {
ended: bool,
/// The presenters made again since the last frame presented.
renewals: Renewals,
/// What the host was told of the window, once ([`Told`]).
told: Option<Told>,
}
/// What the host is told of a window, once, on stderr (`crate::told`): the first of its first
/// frame presented and its staying empty, drawing no more without one.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Told {
/// It presented its first frame: it shows.
Shown,
/// It stays empty: it draws no more, and presented no frame.
Empty,
}
impl Presenting {
/// The presenting of display `display`'s window, with no presenter yet.
pub(crate) fn new(display: u64) -> Presenting {
Presenting {
display,
..Presenting::default()
}
}
/// Whether the next draw needs a presenter made first ([`Presenting::make`]).
pub(crate) fn needs_presenter(&self) -> bool {
!self.ended && self.presenter.is_none()
@@ -127,7 +166,24 @@ impl Presenting {
/// is.
pub(crate) fn make(&mut self, make: impl FnOnce() -> Option<Box<dyn Presenter>>) {
self.presenter = make();
self.ended |= self.presenter.is_none();
if self.presenter.is_none() {
self.end("the GPU makes no swapchain for it");
}
}
/// Whether the window presented its first frame ([`Told::Shown`]).
pub(crate) fn shown(&self) -> bool {
self.told == Some(Told::Shown)
}
/// The window stays as it is, drawing no more, for `why`: where it presented no frame, the
/// host is told it stays empty.
fn end(&mut self, why: &str) {
self.ended = true;
if self.told.is_none() {
self.told = Some(Told::Empty);
told::empty(self.display, why);
}
}
/// Whether the window stays as it is, drawing no more: the sink made no presenter, the
@@ -161,10 +217,14 @@ impl Presenting {
return false;
};
let drawn = presenter.draw(drawable, scale, region, cursor, overlay);
if drawn == Drawn::Presented && self.told.is_none() {
self.told = Some(Told::Shown);
told::shown(self.display);
}
if drawn == Drawn::Lost {
// Kept to the window's end, asked no more, and no other made (the module doc, "A lost
// device").
self.ended = true;
self.end("its GPU's device is lost");
return false;
}
if drawn == Drawn::Ended {
@@ -175,7 +235,7 @@ impl Presenting {
target: "reims_vgpu_window",
"the window's presenter ended {RENEWALS} times in a row: the window stays as it is"
);
self.ended = true;
self.end("its swapchain ended again and again");
}
drawn == Drawn::Retry
}
@@ -326,6 +386,48 @@ mod tests {
assert!(dropped.load(Ordering::SeqCst), "let go at the window's end");
}
/// The host is told once whether the window shows: at its first frame presented, not before
/// (a draw with no image presents nothing), and never again; or that it stays empty where it
/// draws no more before a frame (no presenter made, presenters that end in a row, a lost
/// device), but not once it has shown
#[test]
fn the_host_is_told_once_whether_the_window_shows() {
let mut p = Presenting::new(1);
p.make(|| scripted(&[Drawn::Retry, Drawn::Nothing, Drawn::Presented]));
p.draw((4, 4), 1.0, REGION, None, None);
p.draw((4, 4), 1.0, REGION, None, None);
assert_eq!(p.told, None, "nothing presented yet");
p.draw((4, 4), 1.0, REGION, None, None);
assert_eq!(p.told, Some(Told::Shown));
assert!(p.shown());
for _ in 0..=RENEWALS {
p.make(|| scripted(&[Drawn::Ended]));
p.draw((4, 4), 1.0, REGION, None, None);
}
assert!(p.ended);
assert_eq!(
p.told,
Some(Told::Shown),
"a window that showed stays told so"
);
let mut p = Presenting::new(0);
p.make(|| None);
assert_eq!(p.told, Some(Told::Empty), "no swapchain");
let mut p = Presenting::new(0);
for _ in 0..=RENEWALS {
p.make(|| scripted(&[Drawn::Ended]));
p.draw((4, 4), 1.0, REGION, None, None);
}
assert_eq!(p.told, Some(Told::Empty), "presenters that end in a row");
let mut p = Presenting::new(0);
p.make(|| scripted(&[Drawn::Lost]));
p.draw((4, 4), 1.0, REGION, None, None);
assert_eq!(p.told, Some(Told::Empty), "a lost device");
}
/// Presenters that end too many times in a row leave the window as it is
#[test]
fn presenters_ending_in_a_row_leave_the_window_as_it_is() {
+76
View File
@@ -0,0 +1,76 @@
//! What the windows tell the host on stderr, each line in one write (docs/api.md, "The window's
//! lines"), so that what starts the VM can say whether a window shows its screen (reims-ctl's
//! start reads them from QEMU's log):
//! - [`SHOWN`], once a window, at its first frame presented (`crate::present`): the window
//! shows. On Wayland it is mapped only from then on, so a window not told so never appeared;
//! - [`NONE`], once a window, where it draws no more and presented no frame: its swapchain was
//! not made, or ended again and again, or its GPU's device was lost: the window stays empty;
//! - [`NONE`], once a process, where no window opens at all because the GPU presents to no
//! window system of the host's session (`crate::winsys`).
//!
//! A line ends with the reason, in words for the person at the host.
use std::io::Write as _;
/// How a window's line that it shows starts: `reims-vgpu: window shown: display <n>`.
pub(crate) const SHOWN: &str = "reims-vgpu: window shown: ";
/// How a line that a window shows nothing starts: `reims-vgpu: no window: <why>`.
pub(crate) const NONE: &str = "reims-vgpu: no window: ";
/// Display `display`'s window presented its first frame.
pub(crate) fn shown(display: u64) {
write(&shown_line(display));
}
/// Display `display`'s window stays empty, for `why`.
pub(crate) fn empty(display: u64, why: &str) {
write(&empty_line(display, why));
}
/// No window opens, for `why`.
#[cfg_attr(
not(any(target_os = "linux", target_os = "windows")),
expect(dead_code, reason = "only SDL's UI thread chooses a window system")
)]
pub(crate) fn none(why: &str) {
write(&none_line(why));
}
/// [`shown`]'s line.
fn shown_line(display: u64) -> String {
format!("{SHOWN}display {display}\n")
}
/// [`empty`]'s line.
fn empty_line(display: u64, why: &str) -> String {
none_line(&format!("display {display}'s window stays empty: {why}"))
}
/// [`none`]'s line.
fn none_line(why: &str) -> String {
format!("{NONE}{why}\n")
}
/// Writes `line` on stderr in one write, so it never interleaves with another thread's.
fn write(line: &str) {
let _ = std::io::stderr().write_all(line.as_bytes());
}
#[cfg(test)]
mod tests {
use super::*;
/// The lines are whole lines, starting as docs/api.md and reims-ctl's start read them, the
/// display and the reason after
#[test]
fn the_window_s_lines_start_as_the_host_reads_them() {
assert_eq!(shown_line(0), "reims-vgpu: window shown: display 0\n");
assert_eq!(
empty_line(1, "the GPU makes no swapchain for it"),
"reims-vgpu: no window: display 1's window stays empty: the GPU makes no swapchain \
for it\n"
);
assert_eq!(none_line("why"), "reims-vgpu: no window: why\n");
}
}
+143
View File
@@ -0,0 +1,143 @@
//! Which window system a window opens through on Linux ([`choose`]): one whose windows the GPU it
//! presents from presents to, Wayland first, X11 second, and none where it presents to neither.
//!
//! A host's session can have two window systems: a Wayland compositor and an X server, which
//! under a Wayland compositor is its XWayland. A GPU's Vulkan driver need not present to both: a
//! laptop's discrete GPU beside the integrated GPU its compositor runs on can present to X11 and
//! not to Wayland (an RTX 4060 Laptop GPU under KWin, NVIDIA 620.70.06, a user's report: its
//! Wayland surface was not presented to, while its XCB and Xlib surfaces were). A window opened
//! through a window system its GPU does not present to gets no swapchain, and on Wayland, which
//! maps a window only once a frame is presented to it, never appears, its entry in the task bar
//! alone showing it. So before SDL's video starts, the GPU of the first window's sink is asked,
//! with no window, whether it presents to the session's window systems
//! ([`FrameSink::presents_to`](crate::FrameSink::presents_to): Vulkan's per-platform
//! presentation-support questions), over a connection of the asker's own to each
//! (`native/linux/winsys.rs`): the Wayland compositor first, and the X server only where it does
//! not present to the compositor, as an X server under a Wayland compositor can be started by the
//! connection (XWayland on demand). SDL is then named the window system to open through:
//!
//! | Wayland | X11 | the windows open through |
//! |---|---|---|
//! | presents | not asked | Wayland |
//! | does not, or no compositor | presents | X11 |
//! | does not | does not, or no X server | none: no window opens ([`Choice::Neither`]) |
//! | no compositor | does not | none |
//! | no compositor | no X server | SDL's own pick ([`Choice::Sdls`]), which finds none and opens no window (logged) |
//!
//! Named, SDL opens through Wayland where the compositor has no fifo-v1 too, where on its own it
//! would take XWayland (`native/sdl_thread/sdl.rs`'s module doc): the GPU may not present to X11.
//! Where the sink has no GPU to ask, SDL picks as it does on its own. SDL's own variable
//! `SDL_VIDEO_DRIVER` still names its driver over the choice, as a hint set by the library yields
//! to the environment. On Windows there is one window system, and the choice is SDL's.
use std::ffi::CStr;
/// What the GPU a window presents from answered for each window system of the host's session:
/// whether its queue family presents to its windows, `None` where the session has none to connect
/// to (no Wayland compositor, no X server).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Answers {
/// The Wayland compositor's answer.
pub wayland: Option<bool>,
/// The X server's answer, through Xlib: `None` too where it was not asked, as the GPU
/// presents to the compositor.
pub x11: Option<bool>,
}
/// The window system the windows open through ([`choose`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Choice {
/// SDL's own pick: the session has no window system to ask about, or the GPU cannot be
/// asked.
Sdls,
/// The Wayland compositor's, named to SDL.
Wayland,
/// The X server's, named to SDL: XWayland's under a Wayland compositor.
X11,
/// None: the GPU presents to no window system the session has, and no window opens.
Neither,
}
impl Choice {
/// The SDL video driver the choice names, as `SDL_HINT_VIDEO_DRIVER` takes it; `None` where
/// SDL picks, or no window opens.
pub fn driver(self) -> Option<&'static CStr> {
match self {
Choice::Wayland => Some(c"wayland"),
Choice::X11 => Some(c"x11"),
Choice::Sdls | Choice::Neither => None,
}
}
}
/// The window system the windows open through, from the GPU's `answers` (the module doc's table);
/// SDL's own pick where there are none.
pub fn choose(answers: Option<Answers>) -> Choice {
let Some(Answers { wayland, x11 }) = answers else {
return Choice::Sdls;
};
match (wayland, x11) {
(Some(true), _) => Choice::Wayland,
(_, Some(true)) => Choice::X11,
(None, None) => Choice::Sdls,
_ => Choice::Neither,
}
}
/// Whether the X server is asked about, after the compositor's `wayland` answer: where the GPU
/// does not present to the compositor, or there is none.
pub fn asks_x11(wayland: Option<bool>) -> bool {
wayland != Some(true)
}
#[cfg(test)]
mod tests {
use super::*;
/// The answers' pairs.
fn answers(wayland: Option<bool>, x11: Option<bool>) -> Option<Answers> {
Some(Answers { wayland, x11 })
}
/// The window opens through a window system its GPU presents to: Wayland first, X11 second,
/// none where it presents to neither. Each of the four answer pairs, then the session's
/// window systems missing; the X server is asked only where the GPU does not present to the
/// compositor
#[test]
fn the_window_opens_through_a_window_system_its_gpu_presents_to() {
let (yes, no) = (Some(true), Some(false));
assert_eq!(choose(answers(yes, yes)), Choice::Wayland);
assert_eq!(choose(answers(yes, no)), Choice::Wayland);
assert_eq!(choose(answers(no, yes)), Choice::X11);
assert_eq!(choose(answers(no, no)), Choice::Neither);
assert_eq!(choose(answers(yes, None)), Choice::Wayland, "no X server");
assert_eq!(choose(answers(no, None)), Choice::Neither, "no X server");
assert_eq!(choose(answers(None, yes)), Choice::X11, "no compositor");
assert_eq!(choose(answers(None, no)), Choice::Neither, "no compositor");
assert_eq!(
choose(answers(None, None)),
Choice::Sdls,
"neither: SDL finds none and opens no window"
);
assert_eq!(choose(None), Choice::Sdls, "no GPU to ask");
assert!(!asks_x11(yes), "the compositor is presented to");
assert!(asks_x11(no) && asks_x11(None));
assert_eq!(Choice::Wayland.driver(), Some(c"wayland"));
assert_eq!(Choice::X11.driver(), Some(c"x11"));
assert_eq!(Choice::Sdls.driver(), None);
assert_eq!(Choice::Neither.driver(), None);
}
/// A user's report: a laptop's NVIDIA GPU beside the Intel GPU KWin composites on, whose
/// driver presented to X11 and not to Wayland, so the window opened through Wayland and never
/// appeared. Its window opens through X11, XWayland's under the compositor
#[test]
fn a_gpu_presenting_to_x11_alone_opens_its_window_through_x11() {
let report = answers(Some(false), Some(true));
assert_eq!(choose(report), Choice::X11);
assert_eq!(choose(report).driver(), Some(c"x11"));
}
}
+13 -10
View File
@@ -5301,23 +5301,26 @@ impl WindowScreen {
let first = config.mode.unwrap_or((1920, 1080));
let window = Window::new(config, port, reims_vgpu_window::handle::title(port));
window.offered_mode(first.0, first.1);
let shown = Arc::new(Shown {
black_undriven: port > 0,
..Shown::default()
});
// The sink is attached before the host's screens are asked for: on Linux the first
// window's attach starts the windows' UI thread, which chooses the window system the
// windows open through by the sink's GPU (`reims_vgpu_window::winsys`).
if let Some(sink) = scanout_sink(platform, region, &shown, presented) {
window.attach(sink);
// Presents wait for the window's draw only where it has a sink to draw them.
let _ = presented.window.set(Arc::clone(&window));
}
// In the crate's tests the display runs at 60 Hz, asking the host for no screen: on Linux
// the ask starts the windows' UI thread.
// the ask starts the windows' UI thread where no attach did.
let refresh_mhz = if cfg!(test) {
FALLBACK_MODE.refresh_mhz
} else {
reims_vgpu_window::screens::screen_for_port(port)
.map_or(FALLBACK_MODE.refresh_mhz, |s| hz_to_mhz(s.refresh_hz))
};
let shown = Arc::new(Shown {
black_undriven: port > 0,
..Shown::default()
});
if let Some(sink) = scanout_sink(platform, region, &shown, presented) {
window.attach(sink);
// Presents wait for the window's draw only where it has a sink to draw them.
let _ = presented.window.set(Arc::clone(&window));
}
let presented = Arc::clone(presented);
let screen = WindowScreen {
window: Arc::clone(&window),
+66 -1
View File
@@ -10,6 +10,12 @@
//! compositor's GPU: rt makes one device for everything. The route is logged once a sink, at its
//! first presenter.
//!
//! **The window system.** Before the first window opens, the window crate asks the sink whether
//! rt's device presents to each window system of the host's session (on Linux the Wayland
//! compositor and the X server, `reims_vgpu_window::winsys`), with no window: rt's swapchain's
//! `presents_to`, Vulkan's per-platform question of the device's queue family. The windows open
//! through one it presents to, so that a window gets a swapchain.
//!
//! **A lost device.** Once rt's device is known lost, which nothing makes again
//! (`reims_vgpu_vulkan::rt_platform::swapchain`'s module doc, "A lost device"), a presenter's draw
//! answers [`Drawn::Lost`] with no Vulkan call made: the window keeps it, and the frame it shows,
@@ -32,7 +38,7 @@ use reims_vgpu_vulkan::rt_platform::swapchain::RtSwapchain;
use reims_vgpu_vulkan::rt_platform::window_times;
use reims_vgpu_vulkan::swapchain::{Acquired, Refused};
use reims_vgpu_window::{
Drawn, DrawnCursor, DrawnOverlay, FrameSink, PixelRect, Presenter, VulkanTarget,
Drawn, DrawnCursor, DrawnOverlay, FrameSink, PixelRect, Presenter, VulkanTarget, WindowSystem,
};
use super::sink::ScanoutDraw;
@@ -129,6 +135,18 @@ impl FrameSink for RtScanoutSink {
}
}
}
/// Whether rt's device presents to windows of `system`, asked with no window (the module
/// doc, "The window system"); `false` once the device is known lost, which presents nothing.
fn presents_to(&self, system: WindowSystem) -> Option<bool> {
if self.draw.device.is_lost() {
return Some(false);
}
// SAFETY: the window crate's connection, live and its own during the call.
Some(entry::run(|_| unsafe {
RtSwapchain::presents_to(&self.draw.device, system)
}))
}
}
/// A swapchain over the window's surface `target` makes on the instance of `draw`'s device.
@@ -264,4 +282,51 @@ mod tests {
assert_eq!(window.0.load(Ordering::SeqCst), 1, "asked for none after");
});
}
/// On the host's desktop, a display's window opens through a window system rt's device
/// presents to (`reims_vgpu_window::winsys`), and shows: it presents its first frame within
/// a few seconds. Ignored, as it opens a window on the desktop: a harness runs it in a job
/// with the session's `WAYLAND_DISPLAY` and `DISPLAY`, holding the device's GPU and the
/// compositor's; with a `WAYLAND_DISPLAY` that names no compositor, the window opens through
/// X11, XWayland's, as on a GPU that presents to X11 alone. `REIMS_VGPU_LOG=info` logs the
/// answers, the choice and SDL's driver
#[test]
#[ignore = "opens a window on the host's desktop"]
fn on_vulkan_a_window_shows_on_the_desktop() {
use std::time::Duration;
use reims_vgpu_window::{Window, WindowConfig};
reims_vgpu_vulkan::core::logging::init();
let platform = crate::rt_test_end::platform();
let region = Arc::new(Region::new(reims_vgpu_graphics::REGION_SIZE as usize).unwrap());
// Black with no scanout, as a display the guest does not drive draws (`sink`'s module
// doc), so the window has a frame to present.
let shown = Arc::new(Shown {
black_undriven: true,
..Shown::default()
});
let presented = Arc::new(Presented::<RtPlatform>::default());
let window = Window::new(
WindowConfig::default(),
0,
reims_vgpu_window::handle::title(0),
);
window.offered_mode(1280, 720);
let made = entry::run(|_| sink(Arc::clone(&platform), &region, &shown, &presented));
window.attach(made.expect("a sink on rt's device"));
// 20 s at most, a new frame each 50 ms.
for _ in 0..400 {
if window.shown() {
break;
}
window.new_frame();
std::thread::sleep(Duration::from_millis(50));
}
let showed = window.shown();
// Up for a few seconds more, for a look at the desktop.
std::thread::sleep(Duration::from_secs(4));
entry::run(|_| window.close());
assert!(showed, "the window presented no frame in 20 s");
}
}
+17
View File
@@ -237,6 +237,23 @@ thread of the library's. QEMU calls it, under the BQL, when its device's `x-writ
is set to true over QMP, which `reims-ctl report` does on a running VM before it zips QEMU's log, so
the zip has the counts as of the report.
### The window's lines
Each display's window writes one line on stderr, in one write, once it is known whether it shows
(`reims-vgpu-window`'s `told`):
- `reims-vgpu: window shown: display <n>` at its first frame presented. On Wayland a window is
mapped only once a frame is presented to it, so a window without this line never appeared;
- `reims-vgpu: no window: display <n>'s window stays empty: <why>` where it draws no more before
a frame: its swapchain was not made, or ended again and again, or the GPU's device was lost.
And on Linux, where the GPU presents to none of the session's window systems (the Wayland
compositor, and the X server where it does not present to the compositor), no window opens, and
the process writes `reims-vgpu: no window: this GPU cannot show a window on this desktop: ...`
once (`reims-vgpu-window`'s `winsys`). The windows otherwise open through a window system the
GPU presents to: Wayland where it presents to the compositor, else X11. These lines are written
whatever `REIMS_VGPU_LOG` says. reims-ctl's start reads them from QEMU's log, and says a VM's
window is open only after the first.
### The guest's Shared pool
A host names the guest physical addresses it can give the guest's Shared pool