Repository navigation
Description
Thanks to previous discussions #105 and some awesome work by @francesca64 in #548 winit has now a quite fleshed out dpi-handling story. However there is still some friction (see #796 notably), so I suggest putting everything on the table to figure out the details.
HiDPI handling on different platforms
It tried to gather information about how HiDPI is handled on different platforms, please correct me if I'm wrong.
Here, "physical pixels" represents the coordinate space of the actual pixels on the screen. "Logical pixels" represents the abstract coordinate space obtained from scaling the physical coordinates by the hidpi factor. Logical pixels generally give a better feeling of "how big the surface is on the screen".
Windows
On Windows an app needs to declare itself as dpi aware. If it does not, the OS will rescale everything to make it believe the screen just has a DPI factor of 1.
The user can configure DPI factors for their screens by increments of 0.25 (so 1, 1.25, 1.5, 1.75, 2 ...)
If the app declares itself as DPI aware, it then handles everything in physical pixels, and is responsible for drawing correctly and scaling its input.
macOS
On macOS, the app can request to be assigned an OpenGL surface of the best size on retina (HiDPI) displays. If it does not, its contents are scaled up when necessary.
DPI factor is generally either 2 for retina displays or 1 for regular displays.
Input handling is done in logical pixels.
Linux X11
The Xorg server doesn't really have any concept of "logical pixels". Everything is done directly in physical pixels, and app need to handle everything themselves.
The app can handle HiDPI handling by querying the DPI value for the screen it is displayed one and computing the HiDPI factor by dividing it by 96, which is considered the reference DPI for a regular non-HiDPI screen. Meaning the DPI factor can basically have any float value.
There are several potential sources for obtaining the DPI of the screen:
- retrieving it from Xrandr, which computes it from the physical dimensions of the monitor meaning:
- it'll almost never be a round number
- it can be unreliable if the X server couldn't get the actual physical dimensions
- retrieving it from a user configuration like
xft.dpior the gnome service configuration, meaning:- it is a globally set value, not per-monitor, so it won't work well on mixed-dpi setups
- if several config sources are available, which is to be used?
Linux Wayland
Similarly to macOS, most of the handling is done only on logical pixels.
The app can check the HiDPI factor for each monitor (which is always an integer), and decide to draw its content with any (integer) dpi factor it chooses. If the chosen dpi factor does not match the one of the monitor displaying the window, the display server will upscale or downscale the contents accordingly.
Mobile platforms
I don't know about Android or iOS, but there should not be any difficulties coming from them : a device only has a single screen, and generally apps take up the whole screen.
Web platform
I don't know about it.
Current HiDPI model of winit
Currently winit follows an hidpi model similar to Wayland or macOS : almost everything is handled in logical pixels (marked by a specific type for handling it), and hidpi support is mandatory in the sense that the drawing surface always match the physical dimensions of the app.
The LogicalSize and LogicalPosition types provide helper methods to be converted to their physical counterpart given an hidpi factor.
The app is supposed to track HiDPIFactoChanged(..) events, and whenever this event is emitted, winit has resized the drawing surface to match its new correct physical size, so that the logic size does not change.
Reported friction
As can be expected from the previous description, most of the reported friction with winit's model comes from the X11 platform, for which hidpi awareness is very barebones.
I believe the main source of friction however comes from the fact that linux developpers are very used to the x11 model and their expectations do not map to the model winit has chosen: winit forces its user to mostly work using logical coordinates to handle hidpi, and only use physical coordinates when actually drawing. This causes a lot of impedance mismatch, which is a large source frustration for everyone.
Solutions
There doesn't appear to be some silver-bullet that can solve everything, mostly as the X11 model for hidpi handling is so different from the other platforms that unifying them in a satisfactory way seems really difficult.
So while this section is really the open question of this issue, there are some simple "solutions" that I can think of, in no particular order:
- Winit could just keep its current model and work around X11 subtleties as best as it can, even if the dpi factor is
1.0866666666, and we can close this issue asWONTFIXorWORKS AS INTENDED - Winit could officially declare that X11 is a second-class citizen wrt to hidpi handling. As such, on X11 winit will always report an hidpi factor of 1 and have physical an logical coordinates always be the same. Winit would then expose some platform-specific methods like
get_xrandr_dpi_for_monitorandget_xft_dpi_config, and let the downstream crate deal themselves with X11 hidpi - Winit could shift its dpi model to be closer to X11: do everything in physical pixels and only provide the dpi factor as an information and let the downstream users handle everything themselves
- Someone suddenly has a "Eureka!" moment and figures out a new revolutionary API that cleanly unifies everything
Now tagging the persons that may be interested in this discussion: @Osspial @zegentzy @icefoxen @mitchmindtree @fschutt @chrisduerr
Activity
icefoxen commentedon Apr 9, 2019
Thank you, this is a nice overview of the complexity of things! It definitely makes clearer that alot of the weird edge cases are due to X11 being half-assed about it. We'll be rid of it someday, honest.... I'm also not sure we can dismiss mobile platforms as easy though; mobile devices often have much higher DPIs than desktops, 300+ rather than ~100. Making stuff look nice on both is a non-trivial problem, and I sure don't know how to solve it.
For completeness, the definition of "hidpi factor" is
physical pixels = logical pixels * hidpi factor. Also, apple uses the term "points" for logical pixels, so one may stumble across that term in apple-related docs.Not to restate what I've already stated at too much length (:-P) but as a user, I am attracted to the Windows style. By default you get Something More Or Less Sane, and if you tell the system you know what you're doing it gets out of your way and lets you handle it. My use case is basically only gamedev though, which is fairly narrow and specialized.
Osspial commentedon Apr 9, 2019
Android, at least, provides fairly detailed docs on how it handles DPI scaling. That's available here.
Regarding X11 DPI scaling: For winit, my general rule of thumb in solutions like this is to do what everyone else does. GDK, Qt, Firefox, and Chrome all have faced this problem before; what solutions did they come up with? If they collectively decided to just not handle DPI scaling, that's the solution we should use, but if there are cleaner solutions buried in their source code it would be silly not find them out and implement them here.
I've got thoughts on reducing the reported friction outside of X11 and solutions for it, but those thoughts aren't fully baked so I'll wait until I figure out what exactly I'm trying to say there before I post them here.
elinorbgr commentedon Apr 9, 2019
From the point of view of winit, I was rather thinking in terms of API constraints: a mobile platform will very likely have a constant dpi factor during the whole run of the app, and it also imposes the dimensions of the window (most often the whole screen). This makes questions like "How to handle resizing?" or "How to specify the initial window size?" basically nonexistent.
Though if there were no question about X11, isn't that pretty similar to what winit currently does (and wayland and macOS), except everything is handled in "logical pixels" / "points" rather than physical pixels?
The main advantage I see is that it allows most of the app code to be invariant under DPI change: only the drawing logic needs to be DPI-aware if all input / UI processing is done in points.
I don't think these are actually what we want to compare winit with. Winit's scope is more similar to projects like FreeGlut of GLFW on this point.
Though, for the record, from my experience of having an hidpi laptop, both Qt and GTK (and to some extend Firefox) "solve" it mostly by being configured using env variables, defaulting to not doing any any hidpi handling on X11, see https://wiki.archlinux.org/index.php/HiDPI#GUI_toolkits for example.
Osspial commentedon Apr 9, 2019
I was mainly talking about looking at how they calculate DPI scaling values. If they don't even bother, then I think we should just make X11 DPI support second-class - yes, it breaks multi-monitor DPI scaling, but trying to solve that has caused enough problems that I'm tempted to just not even bother with it any more.
Anyhow, my thoughts on reducing API friction:
TL;DR: I think we should drop
LogicalSizeandLogicalPositionentirely, since Winit is too low-level to expose them in a way that's useful for application developers.Why do I think that's the case?
There are two broad classes of applications Winit aims to support: 3D/2D rendering for games, and 2D rendering for GUIs. Broadly speaking, logical pixels are useless for the 3D rendering, so I won't spend any time talking about them in that context.
It's less obvious why Winit's logical pixels are useless for GUI rendering: it's useful for GUI applications to express units in terms of logical pixels, right?
Well, yes. However, if a GUI application wants to render its assets without blurring, it still has to choose the proper assets for the pixel density, and manually align those assets to the physical pixel grid. To do that, the GUI's layout engine has to distribute the fractional part of the logical pixels throughout the output image as described here (admittedly for text justification, but the same principle can be applied to widget layout). It can only do that effectively in particular, safe, areas (mainly whitespace areas); if it doesn't take care to do it in those safe areas, it'll end up stretching out parts of the image that shouldn't stretched, such as text, with the following result:
Winit's logical pixel model doesn't account for those subtleties, and using it as suggested just distributes fractional pixel values evenly through the rendered image, resulting in artifacts like shown above.
To reiterate, the proper way for an application to account for fractional DPI scaling values is by:
Winit is, by design, so low in the application rendering stack that it cannot assist with either of those things. As such, we shouldn't even pretend to do it, and just provide physical pixel values w/ a scaling factor to downstream applications. Getting rid of
LogicalSizealso allows us to expose pixel values exclusively as integers instead of as floats which, as a downstream user, makes a whole lot more sense.EDIT: To be clear: we shouldn't drop
LogicalSizeandLogicalPositionwithout adding in some documentation to fill in the gap - DPI is a hard problem, and if we don't make it a prominent problem, users will forget about it. We should also makeget_hidpi_factora more prominent function by moving it so it's close to the top ofWindow's documentation, so that it's one of the first functions people see; as of now, it's easy to miss since it's buried in the middle of a whole lot of other, less important functions.elinorbgr commentedon Apr 9, 2019
I see your point @Osspial. I'm not yet sure I fully agree, but it makes sense.
However, if we go that route, I believe hidpi awareness should be opt-in, maybe with a
.with_hidpi_awareness()method on the WindowBuilder. Because handling HiDPI from solely physical coordinate space will likely require more care than from logical coordinates, and so I believe winit users should actively request it.icefoxen commentedon Apr 11, 2019
No because winit lacks the ability to tell it to ignore the hidpi scaling factor.
Usually when one clicks on a window one is clicking... well, on something. So your application would need to convert logical to physical pixels or vice versa at some point anyway.
...Or by the application itself, in Qt's case by calling
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);as demonstrated here.With regards to
get_hidpi_factor, I'd just like to remind us all of the sort of wacky edge cases we're dealing with: one of the exciting things that's just Hard to deal with in general is you can't always know what hidpi factor a window will actually have until it's created. You can't always control which monitor a window is created on, and if you have multiple monitors with different DPI factors, you don't know what DPI factor to use until the window is already opened, at which point it's too late. I THINK it currently immediately sends a resize event orHidpiChangeevent or whatever it's called when this happens, but that's really not a good solution, just the least bad one possible.That said, I'm fine with either opting in or opting out, since unlike converting all logical pixels to physical ones, that's an easy choice to make that only happens in one place.
chrisduerr commentedon Apr 11, 2019
This is often a big problem with Linux WMs too, since it can lead to resizing twice immediately at startup which many don't really like very much.
On X11, I think Qt attempts most to provide proper Hidpi support. It supports both the Xrandr method of scaling and fixed scaling values for all screens. GTK is surprisingly lacking here by only reading the Xft.dpi for fonts (which is fine and works for most users), but only allowing a fixed DPI UI scale using an environment variable.
I think if winit would provide physical pixels with a way to get the Xft.dpi using the get_hidpi_factor and a way to override this to use the dynamic xrandr approach instead (maybe WindowExt on linux can provide something like `with_xrandr_dpi(true)?), it would likely give developers all the tools necessary to implement DPI however they want (and however a user might want it) on X11.
In general I think the biggest difference from the change to physical pixels to logical pixels was the automatic window resizing. For Alacritty this is nice since we don't have to do it manually anymore, however I think it would be trivial to implement ourselves but very hard to disable. So if this is not done, it seems like the user is mostly in control of handling DPI themselves which I'd be in favor of generally.
All I'd want personally would be a way to get the hpdip factor, handling everything else myself seems straight-forward. Note that this is with a pretty simple application that fully tries to comply with DPI, however we still convert everything to physical pixels anyways.
elinorbgr commentedon Apr 11, 2019
I'm no going to answer to individual points in a quote-reply manner, because I feel it'd would only exacerbate misunderstanding about details, rather than actually advance the discussion.
The thing I understand is that uses such as gaming really only deal in physical pixels, and as @Osspial explained advanced UI layout really needs to account for the physical pixels rather than only work in logical pixels.
However, I feel most of the discussion is done taking the X11/Windows model as a basis. Please take into account that these intuitions don't map well to macOS/Wayland. Especially the question of automatic resizing.
On Wayland, if the dpi factor changes (by moving the window to a monitor), the physical surface is resized, so that the logical size does not change, period. On Wayland, it does not make any sense to request a specific physical size. (But on wayland, the hidpid factor is always an integer, so this simplifies stuff).
The initial sequence of the creation of a wayland window is something like that:
This all works together because when submitting its contents, the client actually tells the server "I'm drawing on this window using a dpi factor of XX".
In this context, if winit is to always expose physical pixels to the user, I see a few possibilities. Assuming winit has some switch to enable or disable DPI-awareness:
HiDPIFactorChangedandResizedevents to the appwinit::Window::set_inner_size()I personally think the first option is the most correct, to ensure the best visual outcome for the user & avoid wasting resources rendering in hidpi when displayed on a lowdpi screen.
xStrom commentedon Apr 12, 2019
This is certainly true for the per-monitor DPI scaling mode that was introduced in Windows 8.1. However it might be worth mentioning that the older system wide DPI scaling mode, introduced in Vista, lets you choose an integer % between 100 and 500. Users can still opt in to this mode today, as seen from the screenshot I just took on Windows 10 1809.
What's more, this value doesn't actually get directly used in Windows. Whatever you insert gets multiplied by 96/100 and saved as an integer in the
HKCU\Control Panel\Desktop\LogPixelsregistry key. This leads to configuration UI quirks that a value like 112% can't actually be set. 112 * (96/100) = 107.52 so LogPixels gets set to 108. When the configuration UI reads this value, it shows 113% instead, which is 108.48.tangmi commentedon Apr 13, 2019
Oof, I'm familiar with that field. It's the enum,
ResolutionScale, that gets casted to an int. The "named" settings have some interesting values that might help informwinit's design: 120, 140, and 160.That being said, would it be reasonable to just round the DPI scale factor on X11 to the nearest integer (or 0.25 increment)? I believe DPI scaling exists to make UIs appear (about) the same size on different resolution density monitors and that scaling is standardized in the system's compositor to make the UIs of different apps on the same system appear the same size. If X11 doesn't have a standard "blessed" way of getting a nice DPI scale factor, I think we can only make our best guess and hope that's what most other apps are doing.
In support of @Osspial's proposal to drop the logical units from
winit, I'd like to make the observation that macOS/iOS and Windows (and likely others I'm not familiar with) use logical units in their windowing APIs since those APIs are tied to OS's UI libraries. While all UI libraries need to be in some logical coordinate system (which could just be equal to the physical units),winitdoesn't provide a UI library and can expose just physical units. It should, however, provide tools (like exposing thehi_dpi_factorbefore creating a window) for callers to create and manage their own UI coordinate system (e.g. they would simply divide by thehi_dpi_factoron all input).winitcould even still provideLogicalSizeandLogicalPositionas helpers that must be created from ahi_dpi_factorand aPhysicalSizeorPhysicalPositionto help encourage and document DPI handling.21 remaining items
elinorbgr commentedon May 6, 2019
IMO it'd be better to roll it in the same release as EL2.0. I'd tend to cluster all breaking changes together rather than do several largely breaking releases in a short timespan.
tangmi commentedon May 7, 2019
I believe that it's possible for input devices to have a higher resolution input space than the screen they're displaying to (e.g. pen digitizers), so
PhysicalPositionmight want to use floats.An example is the
POINTER_INFOstruct on Windows, which can return values in HIMETRIC for higher precision with pen input.Ralith commentedon May 7, 2019
This is actually pretty common even for conventional input devices, in my experience. There's really no reason for any analog input device's precision to be limited to the display resolution, of all things.
tangmi commentedon May 7, 2019
Yeah, as another example, it looks like Wacom wintab's
PACKETstruct has fields,pkX,pkY, andpkZ, which (although the docs aren't clear on this) seem to be in either the tablets "native" or "output" space, which maps to some region of the screen, but are not equatable to the screen.Additionally UIKit's
UITouch.locationreturns a point that's in a given view's coordinate system. It's float precision presumably because views don't necessarily need to be integer multiple scale factors from each other, but this design has the side effect of allowing fractional input, even on the root view.Osspial commentedon May 30, 2019
I'm gonna take a stab at implementing this.
Osspial commentedon May 30, 2019
@vberger Actually, thinking about it, does it maybe make more sense to have the window creation API take either a
PhysicalSizeor aLogicalSize, like this?IMO this would be easier to document/implement than having an optional target DPI parameter, but I'm also the person that came up with it so I may not be the best judge of that.
elinorbgr commentedon Jun 1, 2019
@Osspial I guess it makes sense yes. And in that case, such API could also be used for
Window::set_inner_sizeI guess?Osspial commentedon Jun 1, 2019
@vberger Yep. I'll go ahead on that design, then.
PSteinhaus commentedon Jun 4, 2021
This helped us very much, thank you for providing it. (Sorry that this comment doesn't really provide any constructive feedback, I hope it's ok anyway.)
dhardy commentedon Jun 6, 2021
I believe this is all implemented now and can be closed?
At least, the biggest issue I have with Wayland scaling is the lack of support for non-integer scale factors, but that's not really something we can solve here.
kchibisov commentedon Jun 9, 2023
I'll close this issue given that semantics outlined here established in the winit.
chris-clark-serverless commentedon Feb 18, 2025
I'm a little confused. Physical size is with DPI scaling included? The reason I ask this is an early comment
physical pixels = logical pixels * hidpi factorOr is it Logical? Seems weird to me that a Physical size can incorporate a software based dpi value?
chrisduerr commentedon Feb 18, 2025
The logical pixels already take that scale factor into account. So the hidpi factor is necessary to get back to the "real" physical dimensions.