🐛 Tell the guest a Windows or X11 screen's content scale

On Windows and X11, SDL's window sizes are pixels, so a window's pixels over
its size are 1 whatever the monitor's scale, and the reims-graphics guest was
told 120 (1x) on a Windows screen at 150% (a user's report): the kext marked
no mode HiDPI and its EDID gave 110 pixels per inch, so an x86-64 guest ran at
1x on a 2560 x 1600 screen, and Displays offered no HiDPI mode.

What the guest is told changes on those hosts: the screen's scale, the
window's density where it is above 1 (Wayland, as before), else SDL's display
scale of the window, which SDL documents as the density times the content
scale of the window's screen: 1.5 on a Windows screen at 150%, told as 180 in
120ths, so such a guest starts HiDPI from 2048 x 1536 pixels as on macOS; on
X11 the desktop's content scale. SDL tells a change of it, which lays the
window out again. Nothing changes on Wayland, where the density decides, nor
on macOS, whose windows tell their backing scale.

The told scale is a field of the layout of its own (Layout::screen_scale): the
drawable, the image, the pointer and Linux's in-window question keep the
drawable's pixels a unit of SDL's sizes (Layout::scale). features.md's
On Linux section gets the entry, with its tests.
This commit is contained in:
Anees Iqbal committed 2026-11-25 19:17:48 -08:00
1 parent abe9cc84da
commit 4420e9cc46
10 files changed
+135 -16

No files matched your search

+15
View File
@@ -500,6 +500,21 @@ runtime's own device. The switches above apply, and `resolution=` too.
- Sharp at any scale, fractional ones included: one drawable pixel per
screen pixel, the swapchain made again whenever the window's pixels
change, across screens and scale changes. `manual`
- The reims-graphics guest is told the scale of the screen the window is
on, as on macOS (The picture): on Wayland the window's pixels a point; on
X11 and on a Windows host, where SDL's sizes are pixels and a window has
one pixel to each of them at any scale, the screen's content scale, SDL's
display scale of the window (on X11 the desktop's `Xft.dpi` over 96, else
GDK's scale; on Windows the monitor's scale setting, 150% telling the
guest 180 in 120ths). The drawable, the image and the pointer stay in the
window's pixels. So on a Windows screen at 150% an x86-64 guest starts
HiDPI at every window size from 2048 × 1536 pixels, as on macOS (a
2560 × 1600 screen's borderless window at 1280 × 800 points). A change of
the screen's scale, or a move to a screen of another, tells the guest the
new one.
`test: sdl::units::a_window_s_scale_is_its_density_where_sizes_are_points_else_its_screen_s`,
`test: geometry::the_layout_carries_the_screen_s_scale`, `manual` (a
Windows host at 150%, X11 at a content scale above 1)
- The window resizes freely, keeping the guest's ratio between black bars,
and follows the guest's mode until the person resizes it (a compositor's
rounding by a point is not the person's).
+50 -4
View File
@@ -65,6 +65,24 @@ pub struct Layout {
/// The drawable's pixels a point: the view's backing scale, 1 where it is not a positive
/// number.
pub scale: f64,
/// The pixels a host point of the screen the view is on, which the reims-graphics display
/// tells its guest: [`Layout::scale`] where the view's points are the host's (AppKit's, and
/// SDL's on Wayland), and the screen's own scale where they are the screen's pixels (SDL's on
/// X11 and Windows, whose drawable is a pixel a point at any scale:
/// [`Layout::on_screen_of`]).
pub screen_scale: f64,
}
impl Layout {
/// This layout on a screen of `scale` pixels a host point ([`Layout::screen_scale`]), its
/// drawable, image and pixels unchanged; a `scale` that is not a positive number keeps the
/// layout's own.
pub fn on_screen_of(self, scale: f64) -> Layout {
Layout {
screen_scale: valid_scale_or(scale, self.screen_scale),
..self
}
}
}
/// `value` where it is finite and not below 0, else 0.
@@ -78,10 +96,15 @@ fn non_negative(value: f64) -> f64 {
/// `scale` where it is finite and above 0, else 1.
fn valid_scale(scale: f64) -> f64 {
valid_scale_or(scale, 1.0)
}
/// `scale` where it is finite and above 0, else `otherwise`.
fn valid_scale_or(scale: f64, otherwise: f64) -> f64 {
if scale.is_finite() && scale > 0.0 {
scale
} else {
1.0
otherwise
}
}
@@ -145,6 +168,7 @@ pub fn layout(bounds: (f64, f64), scale: f64, insets: Insets, guest: Option<(u16
},
pixels,
scale,
screen_scale: scale,
}
}
@@ -468,16 +492,38 @@ mod tests {
}
/// The layout carries the scale of the screen the window is on, which the reims-graphics
/// display tells its guest, so the guest's density follows the screen at any window size
/// display tells its guest, so the guest's density follows the screen at any window size:
/// the view's backing scale, else, where the view's points are the screen's pixels (SDL's on
/// X11 and Windows), the screen's own scale, the drawable and the image left as they are
#[test]
fn the_layout_carries_the_screen_s_scale() {
for scale in [1.0, 1.25, 2.0, 2.1] {
for bounds in [(800.0, 600.0), (1371.0, 800.0), (1829.0, 1000.0)] {
assert_eq!(layout(bounds, scale, NO_INSETS, None).scale, scale);
let l = layout(bounds, scale, NO_INSETS, None);
assert_eq!((l.scale, l.screen_scale), (scale, scale));
}
}
assert_eq!(layout((800.0, 600.0), f64::NAN, NO_INSETS, None).scale, 1.0);
assert_eq!(layout((800.0, 600.0), 0.0, NO_INSETS, None).scale, 1.0);
assert_eq!(
layout((800.0, 600.0), 0.0, NO_INSETS, None).screen_scale,
1.0
);
// A Windows screen of 2560 × 1600 at 150%: SDL's sizes are its pixels, a pixel a point to
// the drawable, and the guest is told 1.5.
let pixels = layout((2560.0, 1600.0), 1.0, NO_INSETS, Some((2560, 1600)));
let windows = pixels.on_screen_of(1.5);
assert_eq!(windows.screen_scale, 1.5);
assert_eq!(
Layout {
screen_scale: 1.0,
..windows
},
pixels
);
assert_eq!(windows.drawable, (2560, 1600));
// A scale that is not a positive number keeps the layout's.
assert_eq!(windows.on_screen_of(f64::NAN).screen_scale, 1.5);
assert_eq!(pixels.on_screen_of(0.0).screen_scale, 1.0);
}
/// The mode offered for a drawable is its width rounded down to a multiple of 16 pixels and
@@ -296,7 +296,7 @@ fn run(window: &Window) {
}
if presenting.draw(
work.layout.drawable,
work.layout.scale,
work.layout.screen_scale,
work.layout.pixels,
cursor.as_ref(),
None,
@@ -258,6 +258,12 @@ pub(crate) fn metrics(shell: &Shell) -> Option<((f64, f64), f64)> {
.then_some(((size.width, size.height), scale))
}
/// The pixels a host point of the screen the window is on, which its guest is told: its backing
/// scale, `density` ([`metrics`]), as AppKit's sizes are points.
pub(crate) fn screen_scale(_shell: &Shell, density: f64) -> f64 {
density
}
/// The room the window keeps clear around the image: a borderless window's screen's safe area.
pub(crate) fn insets(shell: &Shell) -> Insets {
if !shell.os.borderless {
@@ -289,7 +289,7 @@ fn run(window: &Window) {
let overlay = surface.overlay();
if presenting.draw(
work.layout.drawable,
work.layout.scale,
work.layout.screen_scale,
work.layout.pixels,
cursor.as_ref(),
overlay.as_ref(),
@@ -14,6 +14,11 @@
//! and its smallest are asked for in SDL's sizes (`crate::sdl::units`): points on Wayland, and on
//! X11 and Windows pixels, the desktop's content scale of them to the point.
//!
//! **The scale the guest is told** is the screen's ([`screen_scale`]): on Wayland the window's
//! pixels a point, and on X11 and Windows, where its drawable is a pixel a unit of SDL's sizes at
//! any scale, the content scale of the screen SDL has it on. The drawable, the image and the
//! pointer stay in SDL's sizes.
//!
//! **Borderless**: through the compositor's own full screen on its screen, over its panels.
//!
//! **The surface.** The window is made for Vulkan: its render thread makes the window's Vulkan
@@ -241,6 +246,19 @@ pub(crate) fn metrics(shell: &Shell) -> Option<((f64, f64), f64)> {
Some(((f64::from(pw) / density, f64::from(ph) / density), density))
}
/// The pixels a host point of the screen the window is on, which its guest is told
/// (`crate::geometry::Layout::screen_scale`), for a window of `density` pixels a unit of SDL's
/// sizes ([`metrics`]): the density on Wayland, whose sizes are points; on X11 and Windows, whose
/// sizes are pixels and whose windows have a density of 1 at any scale, SDL's display scale of
/// the window, its screen's content scale (`crate::sdl::units`): 1.5 on a Windows screen at 150%.
/// SDL tells a change of it (`SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED`), which lays the window out
/// again (`crate::sdl::shell`).
pub(crate) fn screen_scale(shell: &Shell, density: f64) -> f64 {
// SAFETY: the window is SDL's, on this thread.
let display_scale = unsafe { SDL_GetWindowDisplayScale(shell.sdl) };
units::pixels_per_point(density, display_scale)
}
/// The room the window keeps clear around the image: none.
pub(crate) fn insets(_shell: &Shell) -> Insets {
Insets::default()
@@ -316,7 +316,7 @@ fn host_screen(id: SDL_DisplayID) -> Option<HostScreen> {
// SAFETY: as above.
let content = unsafe { SDL_GetDisplayContentScale(id) };
let density = f64::from(mode.pixel_density);
let scale = crate::sdl::units::pixels_per_point(mode.pixel_density, content);
let scale = crate::sdl::units::pixels_per_point(density, content);
let pixels = (mode_pixels(mode.w, density), mode_pixels(mode.h, density));
let mut fullscreen = Vec::new();
let mut count = 0;
@@ -14,7 +14,10 @@
//! - **DPI.** SDL's video start declares the process per-monitor DPI aware (v2), unless the
//! process has declared its awareness already (read from SDL 3.4.18's code,
//! `WIN_InitDPIAwareness`): SDL's sizes are then pixels, its modes have a pixel density of 1,
//! and a screen's content scale is its DPI over 96, as on X11 (`crate::sdl::units`).
//! and a screen's content scale is its DPI over 96, as on X11 (`crate::sdl::units`). A window's
//! pixels over its size are 1 at any scale, so the scale its guest is told is SDL's display
//! scale of the window, its monitor's content scale (`super::sdl_thread::sdl::screen_scale`):
//! 1.5 at 150%.
//! - **The keyboard grab** while captured (`SDL_SetWindowKeyboardGrab`) is a low-level keyboard
//! hook SDL installs on the UI thread while the window has the keyboard, which takes the
//! system's shortcuts (the Windows key, Alt+Tab, Alt+Esc) before Windows does (read from SDL
+6 -4
View File
@@ -18,9 +18,10 @@
//!
//! **The layout.** Whenever the window's size, its scale or the guest's mode changes, the image is
//! laid out again ([`geometry::layout`]) over the window's points at its pixels per point
//! ([`os::metrics`]), inside the room the OS keeps clear ([`os::insets`]), and the render thread
//! draws a changed one: the drawable is the window's size in pixels, one pixel per screen pixel at
//! any scale.
//! ([`os::metrics`]), inside the room the OS keeps clear ([`os::insets`]), on a screen of the scale
//! the guest is told ([`os::screen_scale`]: the window's pixels a point where SDL's sizes are
//! points, else its screen's content scale), and the render thread draws a changed one: the
//! drawable is the window's size in pixels, one pixel per screen pixel at any scale.
//!
//! **Events.** Closing the window (its close button, or the keyboard's or the window system's
//! close) lets its input go and asks the person, in the OS's own dialog over the window
@@ -459,7 +460,8 @@ impl Shell {
return;
};
let insets = os::insets(self);
let layout = geometry::layout(bounds, density, insets, self.window.mode());
let layout = geometry::layout(bounds, density, insets, self.window.mode())
.on_screen_of(os::screen_scale(self, density));
if layout != self.layout || density.to_bits() != self.density.to_bits() {
log::debug!(
target: "reims_vgpu_window",
+33 -4
View File
@@ -9,6 +9,14 @@
//! for in pixels, its display bounds and usable bounds (the monitor's work area) are pixels, its
//! modes have a pixel density of 1, and the content scale is the monitor's effective DPI over 96.
//!
//! So a window's pixels over its size in SDL's sizes are its screen's scale on Wayland alone: on
//! X11 and Windows they are 1 at any scale, and the screen's scale is SDL's display scale of the
//! window, which SDL documents as the pixel density times the content scale of the window's screen
//! (`SDL_GetWindowDisplayScale`, docs/README-highdpi.md, read in SDL 3.4.18's code: X11's and
//! Windows' drivers have no window content scale of their own, and Wayland's is the window's
//! buffer scale). The scale a window's guest is told is chosen as a screen's is
//! ([`pixels_per_point`]), from the window's density and that display scale.
//!
//! Linux and Windows size their windows here (`native/sdl_thread/sdl.rs`); macOS's SDL windows
//! open in the frames AppKit's window takes, in points.
@@ -22,9 +30,11 @@ use crate::geometry;
/// A screen's pixels per host point, from its current mode's `pixel_density` and SDL's
/// `content_scale` of it: the density where it is above 1 (Wayland at a scale above 1), else the
/// content scale (X11, where the desktop's scale is the content scale alone), else 1.
pub(crate) fn pixels_per_point(pixel_density: f32, content_scale: f32) -> f64 {
let density = f64::from(pixel_density);
/// content scale (X11 and Windows, where the desktop's scale is the content scale alone), else 1.
/// A window's the same way, from its pixels over its size in SDL's sizes and SDL's display scale
/// of it (the module doc): so on Wayland the density, as the window's pixels come (3841 over
/// 1829 points at 2.1), and on X11 and Windows its screen's content scale.
pub(crate) fn pixels_per_point(density: f64, content_scale: f32) -> f64 {
if density > 1.0 {
density
} else if content_scale.is_finite() && content_scale > 0.0 {
@@ -52,7 +62,7 @@ impl Units {
pub(crate) fn new(pixel_density: f32, content_scale: f32) -> Units {
let in_120ths = |scale: f64| (scale * 120.0).round() / 120.0;
let pixels = f64::from(pixel_density).max(1.0);
let scale = pixels_per_point(pixel_density, content_scale);
let scale = pixels_per_point(f64::from(pixel_density), content_scale);
if pixels > 1.0 {
Units {
pixels: in_120ths(pixels),
@@ -152,6 +162,25 @@ mod tests {
assert_eq!(pixels_per_point(0.0, 0.0), 1.0);
}
/// A window's guest is told its pixels over its points where SDL's sizes are points (Wayland:
/// the ratio as its pixels come, not SDL's float of the scale), and where they are pixels, a
/// density of 1 at any scale (X11, Windows), SDL's display scale of it, its screen's content
/// scale: a Windows screen at 150% is 1.5, not 1
#[test]
fn a_window_s_scale_is_its_density_where_sizes_are_points_else_its_screen_s() {
// Wayland at 2×, at 2.1 and at 1×: the density, whatever SDL's display scale says.
assert_eq!(pixels_per_point(2.0, 2.0), 2.0);
assert_eq!(pixels_per_point(3841.0 / 1829.0, 2.1), 3841.0 / 1829.0);
assert_eq!(pixels_per_point(1.0, 1.0), 1.0);
// Windows at 150% and 125%, X11 at 200%: the content scale.
assert_eq!(pixels_per_point(1.0, 1.5), 1.5);
assert_eq!(pixels_per_point(1.0, 1.25), 1.25);
assert_eq!(pixels_per_point(1.0, 2.0), 2.0);
// No display scale known: 1.
assert_eq!(pixels_per_point(1.0, 0.0), 1.0);
assert_eq!(pixels_per_point(1.0, f32::INFINITY), 1.0);
}
/// A window opens at a guest point per host point in SDL's sizes: points on Wayland, and on
/// X11 pixels, the content scale of them to the point, so a window at a content scale of 2
/// is not opened at half its size; on whole pixels, shrunk to fit, and never below the