rust-windowing / winit

Window handling library in pure Rust
https://docs.rs/winit/
Apache License 2.0
4.65k stars 878 forks source link

Incorrect DPI scaling on Windows #3040

Open valaphee opened 12 months ago

valaphee commented 12 months ago

While testing DPI scaling with two monitors on Windows 11, one running at 4k (150% scaling) and the other at 2k (100% scaling) and both being 32", I noticed that the window size is 50% larger on the 4k one, which should be physically the same size.

And the conversion is also "lossy" because when moving the window between both monitors it's getting larger by one pixel

// new_dpi_x, new_scale_factor
144 1 1.5
PhysicalSize { width: 1208, height: 920 } PhysicalSize { width: 1812, height: 1380 }
96 1.5 1
PhysicalSize { width: 1201, height: 901 } PhysicalSize { width: 801, height: 601 }
144 1 1.5
PhysicalSize { width: 1210, height: 921 } PhysicalSize { width: 1815, height: 1382 }
96 1.5 1
PhysicalSize { width: 1203, height: 903 } PhysicalSize { width: 802, height: 602 }
144 1 1.5
PhysicalSize { width: 1211, height: 923 } PhysicalSize { width: 1817, height: 1385 }
96 1.5 1

It seems that both monitors are using 4k logical resolution, but I assume that's Windows 11?

stefnotch commented 11 months ago

I don't exactly understand what the issue is, so please bear with me:

The monitors are 4k and 2k (physically). The 4k monitor has a 150% DPI scaling, so that everything there remains reasonably sized. To turn a physical size into a logical one, one divides by the DPI scale. An alternative way of putting this is that a logical pixel on the 4k monitor is 150% larger than usual.

So the window always has the same logical size, but a different physical size. (Physical size as in the number of physical pixels.)

What exactly is going wrong?

The conversion being lossy is unfortunate. I'm guessing that Windows itself is to blame for that, but I'd have to check.

valaphee commented 11 months ago

Exactly, but Windows seems to also use the logical size, which results in the window being 150% larger (300% in total) as Winit also scales the window.

stefnotch commented 11 months ago

@valaphee Could you post a screenshot of that? From the output, it's not entirely obvious.

valaphee commented 11 months ago

Sure, the calculator is for reference, its the same window just moved and both screenshots combined.

Screenshot 2023-08-27 193445

valaphee commented 10 months ago

Did some experimenting, the size is automatically scaled by the OS, but don't know if this is Windows 11 specific, I assume that at some point the scaling had to work, or its a regression.

P.S. is Windows 11 related as the size is now automatically scaled (but its also an inconsistent behavior as for changing the dpi without moving the window the size must be scaled manually)