🐎 Offer the guest modes a multiple of 16 pixels wide

The owner's ruling of 2026-11-07: remove the cause of the desktop's copy, so
there is no copy at all. On Intel's Vulkan driver (ANV) a render pass writes
back whole 64-byte lines, so a guest surface over the guest's RAM is drawn in
place only where each row starts and ends on a line. The guest's desktop
surfaces are its mode's width in BGRA8: the 3662-pixel desktop of a window
3662 pixels wide ends each 14648-byte row 56 bytes into a line, and stayed a
copy of the guest's pages, 2.54 ms of GPU time a plain frame and 3.94 ms a
frame that reads its own pixels on homelab-ms01's Iris Xe (measured by
metal2vulkan's lane). Padding the row's bytes alone cannot help: its last line
still holds bytes past its last pixel.

What the guest sees: the one mode the reims-graphics display offers, the
window's size in pixels, now has its width rounded down to a multiple of 16
(64 bytes) and its height to even, where it was rounded to even each way
(geometry::offered_mode); HiDPI modes still halve exactly. The device, the
option ROM and the kext take their modes from it, so they agree.

What the host sees (features.md, changed on purpose):
- a window of other pixels shows the mode unscaled at its top left beside up
  to 15 columns and a row of black, where it was at most one of each;
- a new window opens on whole pixels a multiple of 16 wide and even high
  (AppKit's sizing and SDL's first size), so it shows no black;
- AppKit's windowed window resizes in steps of 16 by 2 pixels (its content
  resize increments at the scale of the screen it opened on). SDL takes no
  increments, so on Linux a resized window can show the black columns.

The tests that held the even rounding change with it:
geometry::a_drawable_offers_a_mode_a_multiple_of_16_wide_drawn_unscaled,
sdl::units::a_window_opens_a_multiple_of_16_pixels_wide_at_fractional_scales,
and the macOS shell's sizing tests, with a new guard for the increments.
This commit is contained in:
Anees Iqbal committed 2026-11-08 00:03:13 -08:00
1 parent 61b1aac9ec
commit 7638d2ea0e
10 files changed
+251 -97

No files matched your search

+2 -2
View File
@@ -195,8 +195,8 @@ impl DiskController {
/// ([`Display::window_size`]) and keeps the size the person gives it, which the guest follows, at
/// the refresh rate of the host screen it is on. The guest starts a HiDPI desktop by itself in a
/// window at least 3840 pixels wide, and offers one in its Displays settings from 2880 (the
/// kext's HiDPI modes, whose pixels halve exactly: the guest is offered the window's pixels
/// rounded down to even). Settings an older reims-ctl saved for apple-gfx's displays (their
/// kext's HiDPI modes, whose pixels halve exactly: the guest is offered the window's pixels, the
/// width rounded down to a multiple of 16 and the height to even). Settings an older reims-ctl saved for apple-gfx's displays (their
/// count, HiDPI and refresh rate) are read past, and left out of the next save.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
+36 -18
View File
@@ -42,7 +42,8 @@ Unset or unparsable values give the defaults below.
- 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`
- A new window shows a guest point per host point, shrunk to fit the
screen's visible area, on an even number of pixels each way: on a Retina
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, a HiDPI guest one guest pixel per screen pixel, a 1× guest (the
default 1920×1080) scaled up on the GPU, not opened at half size. The guest's scale is the one a macOS 13.6 guest
picks from the size it is told: the width in whole millimetres to nearest,
@@ -72,9 +73,15 @@ Unset or unparsable values give the defaults below.
- Each guest frame is drawn as it arrives, up to the screen's refresh rate
(120 Hz on ProMotion). `manual`
- Colours are sRGB, not stretched over a P3 screen's gamut. `manual`
- The window resizes freely while input is not captured (Capture). The
image keeps the guest mode's aspect ratio, centred on whole pixels between
black bars.
- The window resizes while input is not captured (Capture), its content by
16 pixels wide and 2 high at a time (AppKit's content resize increments,
in points at the scale of the screen it opened on): the steps of the
guest's modes, so a window the person resizes shows the guest's whole
mode, with no black beside it. Zoom, full screen and tiling take no
increments, and a window moved to a screen of another scale can be 8
pixels off a step. The image keeps the guest mode's aspect ratio, centred
on whole pixels between black bars.
`test: native::macos::shell::a_windowed_window_resizes_by_a_mode_s_steps`,
`test: geometry::keeps_the_guest_ratio`,
`test: geometry::centres_the_image`,
`test: geometry::the_image_is_on_whole_pixels`
@@ -90,13 +97,22 @@ Unset or unparsable values give the defaults below.
takes the window's size in pixels as its mode) never follows the guest's
mode: it opens sized for the first mode offered, and from then on a guest
mode change re-fits the image, as after the person's resize. `manual`
- That guest is offered the window's pixels rounded down to even, and a
window of odd pixels (a resize, or a borderless window at a fractional
scale) shows that mode unscaled at its top left, beside a column or row of
black: the guest's kext offers a mode HiDPI only where its pixels halve
exactly, and a guest given a 3841 × 2161 mode never brought its display
up, HiDPI or not.
`test: geometry::an_odd_drawable_offers_an_even_mode_drawn_unscaled`
- That guest is offered the window's pixels with the width rounded down to
a multiple of 16 and the height to even, and a window of other pixels (a
resize without steps, a zoom or tiling, or a borderless window at a
fractional scale: 3841 × 2161
pixels are offered 3840 × 2160, 3662 × 2039 are offered 3648 × 2038)
shows that mode unscaled at its top left, beside up to 15 columns and a
row of black. 16 BGRA8 pixels are a 64-byte line of the GPU's memory: on
Intel's Vulkan driver a guest surface over the guest's RAM is drawn in
place only where each row starts and ends on a line, and the guest's
desktop surfaces are the mode's width (on homelab-ms01's Iris Xe the
3662-pixel desktop stayed a copy, 2.54 ms of GPU time a plain frame and
3.94 ms a frame that reads its own pixels; the owner's ruling of
2026-11-07 removes the cause). The guest's kext offers a mode HiDPI only
where its pixels halve exactly, and a guest given a 3841 × 2161 mode
never brought its display up, HiDPI or not.
`test: geometry::a_drawable_offers_a_mode_a_multiple_of_16_wide_drawn_unscaled`
- Sharp on every screen: one drawable pixel per screen pixel, following the
window across screens of different scale. `manual`
- Native full screen (the green button, ⌃⌘F) keeps the ratio. `manual`
@@ -327,13 +343,15 @@ runtime's own device. The switches above apply, and `resolution=` too.
and the screen a display opens on are as above. The size is asked for in
SDL's sizes: points on Wayland, and on X11 pixels, the desktop's content
scale of them to the point, so an X11 desktop at 200% does not open the
window at half its size. At a fractional scale (1.25, 1.5, 1.75, 2.1)
some sizes in points come to an odd number of pixels, so the size is
made smaller (by up to ten points at 2.1) to the largest that comes to
an even number each way, as SDL rounds it: on Wayland the points times
the scale in 120ths, rounded half away from zero; on X11 the pixels
themselves. The compositor may still give the window another size.
`test: sdl::units::a_window_opens_on_an_even_number_of_pixels_at_fractional_scales`,
window at half its size. The size is made smaller to the largest that
comes to a multiple of 16 pixels wide and an even number high, the steps
of the guest's modes, as SDL rounds it: on Wayland the points times the
scale in 120ths, rounded half away from zero (at a fractional scale such
as 2.1, about one size in 16 comes to a multiple of 16 pixels); on X11
the pixels themselves. SDL takes no resize increments, so a window the
person resizes, or the compositor sizes otherwise, shows the guest's mode
beside up to 15 columns and a row of black.
`test: sdl::units::a_window_opens_a_multiple_of_16_pixels_wide_at_fractional_scales`,
`test: handle::a_window_s_title_is_reims_vgpu_and_its_port`,
`test: geometry::a_new_window_shows_a_guest_point_per_host_point`,
`test: sdl::units::a_window_opens_a_guest_point_per_host_point_in_sdl_s_sizes`,
+71 -22
View File
@@ -10,6 +10,17 @@
//! fall on whole pixels, and [`Layout::image`] is that pixel rectangle again in points. The
//! pointer is mapped through the same rectangle, so the guest pixel under the host's pointer is
//! the one drawn there.
//!
//! **The guest's modes are a multiple of 16 pixels wide** ([`MODE_WIDTH_STEP`], [`offered_mode`]).
//! The guest's desktop surfaces are its mode's width in BGRA8 pixels, 4 bytes each, and a
//! surface over the guest's RAM is drawn in place only where each of its rows starts and ends on
//! a line of the GPU's memory: on Intel's Vulkan driver (ANV) a render pass writes back whole
//! 64-byte lines, so the bytes past a row's last pixel on its last line would be written too.
//! 16 pixels are 64 bytes. Measured by metal2vulkan's lane on homelab-ms01's Iris Xe
//! (2026-11-07): the guest's 3662-pixel desktop surface, 14648 bytes a row (ending 56 bytes
//! into a line), stayed a copy of the guest's pages, 2.54 ms of the GPU's time a plain frame and
//! 3.94 ms a frame that reads its own pixels. Padding a row's bytes alone does not help: the
//! row's last line still holds bytes past its last pixel.
use crate::config::{BUTTON_EXTRA, BUTTON_LEFT, BUTTON_MIDDLE, BUTTON_RIGHT, BUTTON_SIDE};
use crate::handle::PixelRect;
@@ -133,26 +144,50 @@ pub fn layout(bounds: (f64, f64), scale: f64, insets: Insets, guest: Option<(u16
}
}
/// The pixels the width of every mode offered the guest is a multiple of: 16 BGRA8 pixels are
/// a 64-byte line of the GPU's memory, so each row of a guest surface of the mode's width starts
/// and ends on a line (the module doc).
pub const MODE_WIDTH_STEP: u64 = 16;
/// The pixels the height of every mode offered the guest is a multiple of: the guest kext
/// offers a mode HiDPI only where its pixels halve exactly (guest/kext's `RgfxMode::hiDpi`).
pub const MODE_HEIGHT_STEP: u64 = 2;
/// The guest mode a window that keeps its size offers the guest, for a drawable of `drawable`
/// pixels: those pixels rounded down to even each way (and 1 where the drawable is 1). The
/// guest kext offers a mode HiDPI only where its pixels halve exactly (guest/kext's
/// `RgfxMode::hiDpi`), and at a fractional scale a window's pixels are often odd: a borderless
/// window on a 3840 × 2160 screen at 2.1 is the screen's 1828.57 × 1028.57 points rounded to
/// 1829 × 1029, which SDL's Wayland backend makes 3841 × 2161 pixels (points times the scale,
/// rounded), and is offered 3840 × 2160, the screen's own pixels.
/// pixels: its width rounded down to a multiple of [`MODE_WIDTH_STEP`] and its height to a
/// multiple of [`MODE_HEIGHT_STEP`] (a side below its step is kept). Both halve exactly, as a
/// HiDPI mode's pixels must. A window's pixels are often neither: a resize of the person's, or a
/// borderless window at a fractional scale, as on a 3840 × 2160 screen at 2.1, the screen's
/// 1828.57 × 1028.57 points rounded to 1829 × 1029, which SDL's Wayland backend makes 3841 × 2161
/// pixels (points times the scale, rounded), offered 3840 × 2160, the screen's own pixels. The
/// layout draws that mode a guest pixel to a pixel at the drawable's top left ([`fit`]).
pub fn offered_mode(drawable: (u64, u64)) -> (u64, u64) {
let even = |pixels: u64| if pixels >= 2 { pixels & !1 } else { pixels };
(even(drawable.0), even(drawable.1))
let down = |pixels: u64, step: u64| {
if pixels >= step {
pixels - pixels % step
} else {
pixels
}
};
(
down(drawable.0, MODE_WIDTH_STEP),
down(drawable.1, MODE_HEIGHT_STEP),
)
}
/// The largest rectangle of `guest`'s ratio in `area`, on whole pixels, centred (to within half
/// a pixel). One side fills the area; the other is the ratio's, rounded, at least 1. A guest
/// mode of the area's pixels, or at most one fewer each way, is drawn a guest pixel to a pixel,
/// at the area's top left: that is the mode a window of odd pixels offers ([`offered_mode`]),
/// which shows whole and unscaled, beside a column or row of black.
/// mode of the area's pixels, or fewer by less than a step each way ([`MODE_WIDTH_STEP`],
/// [`MODE_HEIGHT_STEP`]), is drawn a guest pixel to a pixel, at the area's top left: that is the
/// mode a window offers for its pixels ([`offered_mode`]), which shows whole and unscaled,
/// beside up to 15 columns and a row of black.
fn fit(area: PixelRect, guest: (u16, u16)) -> PixelRect {
let (gw, gh) = (u64::from(guest.0), u64::from(guest.1));
if gw <= area.width && gh <= area.height && area.width - gw <= 1 && area.height - gh <= 1 {
if gw <= area.width
&& gh <= area.height
&& area.width - gw < MODE_WIDTH_STEP
&& area.height - gh < MODE_HEIGHT_STEP
{
return PixelRect {
x: area.x,
y: area.y,
@@ -431,14 +466,25 @@ mod tests {
assert_eq!(l.drawable, (1, 1));
}
/// The mode offered for a drawable is its pixels rounded down to even, and a guest at that
/// mode is drawn a guest pixel to a pixel, at the top left, beside a column or row of black
/// The mode offered for a drawable is its width rounded down to a multiple of 16 pixels and
/// its height to even, and a guest at that mode is drawn a guest pixel to a pixel, at the top
/// left, beside up to 15 columns and a row of black
#[test]
fn an_odd_drawable_offers_an_even_mode_drawn_unscaled() {
fn a_drawable_offers_a_mode_a_multiple_of_16_wide_drawn_unscaled() {
assert_eq!(offered_mode((3841, 2161)), (3840, 2160));
assert_eq!(offered_mode((3662, 2039)), (3662, 2038));
assert_eq!(offered_mode((3662, 2039)), (3648, 2038));
assert_eq!(offered_mode((3663, 2040)), (3648, 2040));
assert_eq!(offered_mode((3840, 2160)), (3840, 2160));
assert_eq!(offered_mode((1366, 768)), (1360, 768));
assert_eq!(offered_mode((15, 9)), (15, 8));
assert_eq!(offered_mode((1, 1)), (1, 1));
// Every mode a wide drawable offers starts each BGRA8 row on a 64-byte line, and halves
// exactly.
for width in 16..4000 {
let (w, h) = offered_mode((width, width / 2 + 1));
assert_eq!((w * 4) % 64, 0, "{width}");
assert!(width - w < MODE_WIDTH_STEP && h % 2 == 0 && (w / 2) % 8 == 0);
}
// Borderless on a 3840 × 2160 screen at 2.1: 1829 × 1029 points, 3841 × 2161 pixels,
// which show the 3840 × 2160 offered whole.
let l = layout(
@@ -455,12 +501,15 @@ mod tests {
height,
};
assert_eq!(l.pixels, whole(3840, 2160));
// A window of 3662 × 2039 pixels shows 3662 × 2038 whole.
let l = layout((3662.0, 2039.0), 1.0, NO_INSETS, Some((3662, 2038)));
assert_eq!(l.pixels, whole(3662, 2038));
// A guest two pixels narrower and lower is fitted as any other: scaled to fill a side.
let l = layout((3842.0, 2162.0), 1.0, NO_INSETS, Some((3840, 2160)));
assert_eq!(l.pixels.width, 3842);
// A window of 3662 × 2039 pixels shows 3648 × 2038 whole, beside 14 columns and a row.
let l = layout((3662.0, 2039.0), 1.0, NO_INSETS, Some((3648, 2038)));
assert_eq!(l.pixels, whole(3648, 2038));
// A guest 16 pixels narrower, or 2 lower, is fitted as any other: centred, filling a side.
let l = layout((3856.0, 2160.0), 1.0, NO_INSETS, Some((3840, 2160)));
assert_eq!(l.pixels.height, 2160);
assert_ne!(l.pixels, whole(3840, 2160));
let l = layout((3850.0, 2162.0), 1.0, NO_INSETS, Some((3840, 2160)));
assert_eq!(l.pixels.height, 2162);
}
/// With no guest mode yet, the image is the whole area inside the insets
@@ -201,6 +201,9 @@ pub(crate) fn open(window: &Arc<Window>, borderless: bool, taken: &[Frame]) -> O
msg::get("setFrame:display:", || {
ns_window.setFrame_display(frame, false);
});
if !borderless {
appkit::step_resizes(&ns_window);
}
let Some(metal_view) = attach_layer(window, sdl) else {
destroy();
return None;
@@ -427,9 +427,10 @@ struct Sizing {
impl Sizing {
/// The content of a frame at most `room`: one guest point per host point, shrunk with its
/// ratio kept where it does not fit ([`geometry::initial_content_size`]), on an even number
/// of whole pixels, so the drawable is the content exactly and the mode the guest is offered
/// for it ([`geometry::offered_mode`]) is the whole drawable.
/// ratio kept where it does not fit ([`geometry::initial_content_size`]), on whole pixels, a
/// multiple of [`geometry::MODE_WIDTH_STEP`] wide and of [`geometry::MODE_HEIGHT_STEP`]
/// high, so the drawable is the content exactly and the mode the guest is offered for it
/// ([`geometry::offered_mode`]) is the whole drawable.
fn content(&self, room: NSSize) -> NSSize {
let (width, height) = geometry::initial_content_size(
self.mode,
@@ -439,16 +440,19 @@ impl Sizing {
room.height - self.chrome.height,
),
);
let whole = |points: f64| {
let pixels = (points * self.scale).floor();
let even = if pixels >= 2.0 {
pixels - pixels % 2.0
let whole = |points: f64, step: u64| {
let (pixels, step) = ((points * self.scale).floor(), step as f64);
let aligned = if pixels >= step {
pixels - pixels % step
} else {
pixels
};
(even / self.scale).max(1.0)
(aligned / self.scale).max(1.0)
};
NSSize::new(whole(width), whole(height))
NSSize::new(
whole(width, geometry::MODE_WIDTH_STEP),
whole(height, geometry::MODE_HEIGHT_STEP),
)
}
/// The frame around `content`.
@@ -610,9 +614,39 @@ fn windowed(
msg::get("setFrame:display:", || {
window.setFrame_display(frame, false)
});
step_resizes(&window);
window
}
/// The content resize increments of a windowed window at backing scale `scale`: a step of the
/// guest's modes each way ([`geometry::MODE_WIDTH_STEP`], [`geometry::MODE_HEIGHT_STEP`]) in
/// points. A window opens on such steps ([`Sizing::content`]), and the person's drag of an edge
/// changes its content by whole steps, so the mode the guest is offered for it
/// ([`geometry::offered_mode`]) stays the whole drawable, with no black beside it. AppKit's zoom,
/// full screen and tiling take no increments, and neither does a move to a screen of another
/// scale: a content of whole steps at 2× can be 8 pixels off a step at 1×, whatever the
/// increments there.
fn resize_increments(scale: f64) -> NSSize {
let scale = if scale.is_finite() && scale > 0.0 {
scale
} else {
1.0
};
NSSize::new(
geometry::MODE_WIDTH_STEP as f64 / scale,
geometry::MODE_HEIGHT_STEP as f64 / scale,
)
}
/// Sets windowed `window`'s content resize increments for its backing scale as it opens
/// ([`resize_increments`]).
pub(crate) fn step_resizes(window: &NSWindow) {
let scale = msg::get("backingScaleFactor", || window.backingScaleFactor());
msg::get("setContentResizeIncrements:", || {
window.setContentResizeIncrements(resize_increments(scale));
});
}
/// `rect`, in AppKit's screen coordinates (y up), in [`geometry`]'s (y down).
fn y_down(rect: NSRect) -> Rect {
Rect {
@@ -1020,7 +1054,7 @@ mod tests {
// The fit: 368x229 of content, centred.
let first = rect((16.0, 0.0), (368.0, 259.0));
let second = cascade(first, &sizing, visible, &[first]);
assert_eq!(second, rect((40.0, 1.0), (328.0, 234.0)));
assert_eq!(second, rect((40.0, 1.0), (320.0, 234.0)));
// A third would have 289x181 of content, below the smallest.
assert_eq!(cascade(first, &sizing, visible, &[first, second]), second);
for frame in [first, second] {
@@ -1032,7 +1066,7 @@ mod tests {
/// A window that fills the visible area does not open over another such window: it opens 24
/// points right and down, made smaller from there, its content keeping the guest's ratio on
/// an even number of whole pixels
/// whole pixels, a multiple of 16 wide and even high
#[test]
fn a_cascaded_window_shrinks_rather_than_overlap() {
let visible = rect((0.0, 100.0), (1728.0, 1110.0));
@@ -1046,10 +1080,34 @@ mod tests {
assert_eq!(content, NSSize::new(1728.0, 1080.0));
let first = NSRect::new(visible.origin, sizing.frame(content));
assert_eq!(first, visible);
// 1689.6x1056 of content, on an even number of whole pixels: 3378x2112.
// 1689.6x1056 of content, on whole pixels a multiple of 16 wide: 3376x2112.
let second = cascade(first, &sizing, visible, &[first]);
assert_eq!(second, rect((24.0, 100.0), (1689.0, 1086.0)));
assert_eq!(second, rect((24.0, 100.0), (1688.0, 1086.0)));
let third = cascade(first, &sizing, visible, &[first, second]);
assert_eq!(third, rect((48.0, 100.0), (1651.0, 1062.0)));
assert_eq!(third, rect((48.0, 100.0), (1648.0, 1062.0)));
}
/// A windowed window's edges move by a step of the guest's modes in pixels: 16 wide and 2
/// high, in points at its scale
#[test]
fn a_windowed_window_resizes_by_a_mode_s_steps() {
assert_eq!(resize_increments(2.0), NSSize::new(8.0, 1.0));
assert_eq!(resize_increments(1.0), NSSize::new(16.0, 2.0));
assert_eq!(resize_increments(f64::NAN), NSSize::new(16.0, 2.0));
// A content on those steps stays on them at any count of steps.
let sizing = Sizing {
mode: (3456, 2160),
guest_scale: 2.0,
scale: 2.0,
chrome: NSSize::new(0.0, 30.0),
};
let content = sizing.content(NSSize::new(1700.0, 1100.0));
let step = resize_increments(2.0);
for n in 0..40 {
let width = (content.width - f64::from(n) * step.width) * 2.0;
let height = (content.height - f64::from(n) * step.height) * 2.0;
let drawable = (width as u64, height as u64);
assert_eq!(geometry::offered_mode(drawable), drawable, "{n}");
}
}
}
+45 -29
View File
@@ -76,15 +76,17 @@ impl Units {
(f64::from(size) * self.pixels + 1e-6).round() as i64
}
/// The largest of SDL's sizes up to `size`, and not below `min`, whose pixels are even: at a
/// fractional scale some sizes in points come to an odd number of pixels (at 2.1, 971 points
/// are 2039 pixels, and the ten sizes from 965 to 974 points all come out odd).
fn even(self, size: i32, min: i32) -> i32 {
let mut even = size;
while even > min && self.pixels_of(even) % 2 != 0 {
even -= 1;
/// The largest of SDL's sizes up to `size`, and not below `min`, whose pixels are a multiple
/// of `step`: at a fractional scale few sizes in points come to such a number of pixels (at
/// 2.1, 971 points are 2039 pixels, and the ten sizes from 965 to 974 points all come out
/// odd; one size in 16 comes to a multiple of 16).
fn aligned(self, size: i32, min: i32, step: u64) -> i32 {
let step = i64::try_from(step).unwrap_or(1).max(1);
let mut aligned = size;
while aligned > min && self.pixels_of(aligned) % step != 0 {
aligned -= 1;
}
even
aligned
}
/// [`MIN_CONTENT`] as a size SDL takes.
@@ -97,10 +99,11 @@ impl Units {
/// The content size, in `units`, of a windowed window for a guest mode `mode` at `guest_scale`,
/// on a screen whose usable bounds are `usable` in those units, where known: a guest point per
/// host point ([`geometry::initial_content_size`]), shrunk to fit the usable bounds less
/// `title_room` points, on whole pixels, made smaller where it takes that to come to an even
/// number of pixels each way, and at least [`MIN_CONTENT`] points. The guest is offered the
/// window's pixels rounded down to even (the window crate's `geometry`), and a HiDPI mode is
/// one whose pixels halve exactly, so a window of even pixels shows the guest's whole mode.
/// `title_room` points, on whole pixels, made smaller where it takes that to come to a multiple
/// of [`geometry::MODE_WIDTH_STEP`] pixels wide and of [`geometry::MODE_HEIGHT_STEP`] high, and
/// at least [`MIN_CONTENT`] points. The guest is offered the window's pixels rounded down to
/// those steps ([`geometry::offered_mode`]), so such a window shows the guest's whole mode, with
/// no black beside it.
pub(crate) fn windowed_size(
mode: (u16, u16),
guest_scale: u8,
@@ -118,8 +121,16 @@ pub(crate) fn windowed_size(
let (width, height) = geometry::initial_content_size(mode, f64::from(guest_scale.max(1)), room);
let min = units.min_content();
(
units.even(units.whole(width).max(min.0), min.0),
units.even(units.whole(height).max(min.1), min.1),
units.aligned(
units.whole(width).max(min.0),
min.0,
geometry::MODE_WIDTH_STEP,
),
units.aligned(
units.whole(height).max(min.1),
min.1,
geometry::MODE_HEIGHT_STEP,
),
)
}
@@ -155,7 +166,7 @@ mod tests {
let wayland_2x = Units::new(2.0, 1.0);
assert_eq!(
windowed_size((3840, 2160), 2, Some((1920, 1080)), ROOM, wayland_2x),
(1835, 1032)
(1832, 1032)
);
// Wayland at 150%: whole pixels, 1280 points of 1.5 pixels.
let wayland_150 = Units::new(1.5, 1.0);
@@ -187,11 +198,11 @@ mod tests {
assert_eq!(windowed_size((400, 300), 2, None, ROOM, x11_2x), (640, 400));
}
/// At the fractional scales desktops offer, a window opens on an even number of pixels each
/// way, as SDL rounds its sizes to pixels: on Wayland, points made smaller until they come to
/// an even number of pixels; on X11, the pixels themselves
/// At the fractional scales desktops offer, a window opens a multiple of 16 pixels wide and an
/// even number high, as SDL rounds its sizes to pixels: on Wayland, points made smaller until
/// they come to such pixels; on X11, the pixels themselves
#[test]
fn a_window_opens_on_an_even_number_of_pixels_at_fractional_scales() {
fn a_window_opens_a_multiple_of_16_pixels_wide_at_fractional_scales() {
const ROOM: f64 = 48.0;
let modes = [
((1920, 1080), 1),
@@ -213,34 +224,39 @@ mod tests {
let size = windowed_size(mode, guest_scale, Some(usable), ROOM, units);
let pixels = (units.pixels_of(size.0), units.pixels_of(size.1));
assert!(
pixels.0 % 2 == 0 && pixels.1 % 2 == 0,
pixels.0 % 16 == 0 && pixels.1 % 2 == 0,
"{mode:?} at {scale}: {size:?}, {pixels:?} pixels"
);
}
}
}
// Wayland at 2.1: 1570 points are 3297 pixels, and 1564 the next size down that is even.
// Wayland at 2.1: 1570 points are 3297 pixels, and 1562 the next size down that comes to
// a multiple of 16.
let wayland_21 = Units::new(2.1, 1.0);
let size = windowed_size((2560, 1600), 1, Some((1829, 1029)), ROOM, wayland_21);
assert_eq!(size, (1564, 981));
assert_eq!(size, (1562, 981));
assert_eq!(
(wayland_21.pixels_of(size.0), wayland_21.pixels_of(size.1)),
(3284, 2060)
(3280, 2060)
);
// At 1.5: 2227 points are 3341 pixels (3340.5 rounded away from zero), 2225 are 3338.
// At 1.5: 2227 points are 3341 pixels (3340.5 rounded away from zero), 2208 are 3312.
let wayland_15 = Units::new(1.5, 1.0);
assert_eq!(
windowed_size((2560, 1600), 1, Some((2560, 1440)), ROOM, wayland_15),
(2225, 1392)
(2208, 1392)
);
// X11 at 2.1: 3660 × 2059 pixels, one fewer high.
assert_eq!(wayland_15.pixels_of(2208), 3312);
// X11 at 2.1: 3660 × 2059 pixels, 3648 wide and one fewer high.
let x11_21 = Units::new(1.0, 2.1);
assert_eq!(
windowed_size((1920, 1080), 1, Some((3840, 2160)), ROOM, x11_21),
(3660, 2058)
(3648, 2058)
);
// The ten sizes from 965 to 974 points are odd at 2.1 (2027 to 2045 pixels).
assert_eq!(wayland_21.even(974, 200), 964);
// The ten sizes from 965 to 974 points are odd at 2.1 (2027 to 2045 pixels); 964 is even,
// and 960 the first below 975 that comes to a multiple of 16.
assert_eq!(wayland_21.aligned(974, 200, 2), 964);
assert_eq!(wayland_21.pixels_of(964), 2024);
assert_eq!(wayland_21.aligned(974, 200, 16), 960);
assert_eq!(wayland_21.pixels_of(960), 2016);
}
}
+7 -4
View File
@@ -10,8 +10,10 @@
//! **The window** ([`WindowScreen`]) is a display window of `reims-vgpu-window` that keeps its size
//! (`WindowConfig::keeps_its_size`): the guest follows it. It opens sized for the config's mode,
//! else 1920 × 1080, and the display then offers the guest one mode, the window's size in pixels as
//! its render thread last drew into it, rounded down to even (a window of odd pixels shows the
//! guest's mode unscaled, beside a column or row of black), at the refresh rate of the host screen
//! its render thread last drew into it, its width rounded down to a multiple of 16 and its height
//! to even, so each row of a guest surface of the mode's width starts and ends on a 64-byte line of
//! the GPU's memory (`geometry::offered_mode`; a window of other pixels shows the guest's mode
//! unscaled, beside up to 15 columns and a row of black), at the refresh rate of the host screen
//! it opened on; when that size changes (the person resized the window) the device tells the guest
//! (`DISPLAY_CHANGED`). The guest's scanout shows in the window at once and then again at each of
//! the display's refreshes, at the screen's rate, while no presented surface shows in its place;
@@ -2587,8 +2589,9 @@ fn hz_to_mhz(hz: f64) -> u32 {
}
impl Screen for WindowScreen {
/// The window's size in pixels as last drawn into, else the mode it opened for, rounded down
/// to even ([`geometry::offered_mode`]), at its host screen's rate.
/// The window's size in pixels as last drawn into, else the mode it opened for, its width
/// rounded down to a multiple of 16 and its height to even ([`geometry::offered_mode`]), at
/// its host screen's rate.
fn mode(&self) -> Mode {
let drawn = *self.shown.drawable.lock();
let (width, height) = geometry::offered_mode(drawn.unwrap_or_else(|| {
+4 -3
View File
@@ -5,9 +5,10 @@
//! the guest's pixels in place, and the CPU copies nothing.
//!
//! **Sizes.** The guest sets a scanout of the size the display offers, the window's size in
//! pixels rounded down to even, so the image is the whole drawable, or, for a drawable of odd
//! pixels, all of it but a column or row, which stays black: the layout draws that mode a guest
//! pixel to a pixel, at the drawable's top left (`geometry::offered_mode`). While the two differ
//! pixels, its width rounded down to a multiple of 16 and its height to even, so the image is the
//! whole drawable, or, for a drawable of other pixels, all of it but up to 15 columns and a row,
//! which stay black: the layout draws that mode a guest pixel to a pixel, at the drawable's top
//! left (`geometry::offered_mode`). While the two differ
//! otherwise (the window resized, the guest not yet following), the blit copies what fits of the
//! scanout at the image's top left, clipped to the drawable, after a clear of the drawable to
//! black: a blit cannot scale, and the guest's next mode makes the sizes match again.
+4 -3
View File
@@ -97,7 +97,7 @@ table is retired and not reused.
| D47 | The library is a plain library: `cargo xtask dist` writes a static library, a shared library, the header and a pkg-config package, `reims-vgpu.pc` (11.3). | A host links it the way it links its other dependencies; QEMU links the archive, so its binary needs no rpath. |
| D49 | The device's cursor callbacks run on one display thread per process, a core `SerialQueue`, in the order the device queued them; every other callback runs synchronously on the thread the device calls it on (10; api.md section 5). | The API has no dispatch queues or blocks, and a host's console takes the cursor in one order. |
| D50 | `core::testkit` (the fake platform, its models and harnesses) is compiled only under `cfg(any(test, feature = "testkit"))`, and only dev-dependencies turn the `testkit` feature on, never a default; `cargo xtask testkit` checks both (13.1). | No product build can name the fake platform or a test harness. |
| D51 | The device's window is the library's: a crate of its own, `reims-vgpu-window`, between the platform and the API, which knows nothing of devices (2.2). A device created with a window config gets an AppKit window on macOS (or SDL3's with `REIMS_VGPU_WINDOW_IMPL=sdl`), into whose `CAMetalLayer` drawable the guest's scanout is drawn on the GPU, and whose input goes to the host's window callbacks; on Linux an SDL3 window, into whose Vulkan swapchain's image the scanout is drawn the same way. **Threads:** the window's AppKit side and callbacks run on the process's main thread, one at a time across every window, and must not block; the library reaches that thread only through blocks it posts to the main run loop, the application's start alone, and never waits for it. On Linux they run on a UI thread of the library's own, `reims-vgpu.ui`, and the main thread is never used (7.3). Each window draws on a render thread of its own, which nothing joins. **Size:** the window keeps the size the person gives it, and the device offers the guest one mode, the window's size in pixels, at the refresh rate of the host screen it opened on. What the window does for the person at the host is `features.md`'s register. | A host may create its device on the main thread before that thread runs its run loop, so a create or release that waited for the main thread would hang (the window crate's `native/macos/main_thread.rs`). |
| D51 | The device's window is the library's: a crate of its own, `reims-vgpu-window`, between the platform and the API, which knows nothing of devices (2.2). A device created with a window config gets an AppKit window on macOS (or SDL3's with `REIMS_VGPU_WINDOW_IMPL=sdl`), into whose `CAMetalLayer` drawable the guest's scanout is drawn on the GPU, and whose input goes to the host's window callbacks; on Linux an SDL3 window, into whose Vulkan swapchain's image the scanout is drawn the same way. **Threads:** the window's AppKit side and callbacks run on the process's main thread, one at a time across every window, and must not block; the library reaches that thread only through blocks it posts to the main run loop, the application's start alone, and never waits for it. On Linux they run on a UI thread of the library's own, `reims-vgpu.ui`, and the main thread is never used (7.3). Each window draws on a render thread of its own, which nothing joins. **Size:** the window keeps the size the person gives it, and the device offers the guest one mode, the window's size in pixels (its width rounded down to a multiple of 16, so a BGRA8 row of the mode's width starts and ends on a 64-byte line, and its height to even), at the refresh rate of the host screen it opened on. What the window does for the person at the host is `features.md`'s register. | A host may create its device on the main thread before that thread runs its run loop, so a create or release that waited for the main thread would hang (the window crate's `native/macos/main_thread.rs`). |
Retired with Apple's paravirtualized device and its protocol: D10 to D13, D15, D20, D21, D23,
D26, D28, D40 and D48.
@@ -594,8 +594,9 @@ The Rust API is owned Rust types over the device, which the C API wraps one to o
thread is marked as the library's (6.5), and a real pool and a holder are open (7.4), so what the
call held or Metal autoreleased on the thread is released before it returns.
- **The window** (`window.rs`, D51). A `WindowConfig` makes the device's `Window` (the window
crate's), which keeps its size: the device offers the guest the window's size in pixels as its
one mode, and tells the guest when the person resizes it. Once the device exists the window gets
crate's), which keeps its size: the device offers the guest the window's size in pixels, its
width rounded down to a multiple of 16 and its height to even, as its one mode, and tells the
guest when the person resizes it. Once the device exists the window gets
its sink. `graphics/vulkan`'s sink makes a
presenter over a swapchain of the window's surface (on macOS one of its `CAMetalLayer`), which
draws into the swapchain's next image, commits, and presents the image on the swapchain's
+7 -2
View File
@@ -1400,8 +1400,13 @@ stage 2.
| 0x102 | `DISPLAY_CHANGED` | display u32; the guest asks `GET_DISPLAYS` again |
| 0x103 | `VBLANK` | display u32, count u64, host time in ns u64 |
**Display.** The modes offered are the host window's size and its screen's
refresh rate (the guest runs at the host screen's rate). The host presents the
**Display.** The modes offered are the host window's size, its width rounded
down to a multiple of 16 pixels and its height to even, and its screen's
refresh rate (the guest runs at the host screen's rate). 16 BGRA8 pixels are a
64-byte line of the GPU's memory: a guest surface of the mode's width over the
guest's RAM, as the desktop's are, starts and ends each row on a line, which
Intel's Vulkan driver needs to draw it in place (the window crate's
`geometry`). The host presents the
scanout buffer to the display's window at each refresh while the window shows
no presented surface, with no dirty log: the scanout shows only at boot and in
recovery, where its full cost is paid for the simpler design (owner,