FAQ - Help and questions

What is the difference between logical and physical pixels, and what is the scale factor?

#high-dpi#scale-factor#winit#guipublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Physical pixels are the real dots on the screen. Logical pixels are the units your layout uses, equal to physical pixels divided by the scale factor. On a screen set to 150 percent, an 800 by 600 logical window is 1200 by 900 physical pixels. winit's dpi types do the conversion, and the program shows real results.

Checked with rustc 1.93.0, Windows 11, winit 0.30.13 (dpi types only, no window); compiled and run. The definitions are quoted from winit's documentation.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Modern displays pack many more pixels into the same space. To keep text and buttons readable, the operating system lets the user choose a scale factor (for example 125 percent, 150 percent or 200 percent). winit's documentation defines the three words you need:

  • Physical pixels "correspond with the actual pixels on the device."
  • Logical pixels "correspond to the physical pixels divided by the scale factor."
  • Scale factor is "the factor UI elements should be scaled by to be consistent with the rest of the user's system."

So a layout written in logical units looks the same size at any scale factor, and only the number of real pixels that draw it changes. This program uses winit's dpi types to convert, without opening a window:

src/main.rsrust
use winit::dpi::{LogicalSize, PhysicalSize};

fn main() {
    for scale in [1.0_f64, 1.25, 1.5, 2.0] {
        let logical = LogicalSize::new(800.0, 600.0);
        let physical: PhysicalSize<u32> = logical.to_physical(scale);
        println!("scale {scale:<4}: 800x600 logical points = {}x{} physical pixels", physical.width, physical.height);
    }
}
text
scale 1   : 800x600 logical points = 800x600 physical pixels
scale 1.25: 800x600 logical points = 1000x750 physical pixels
scale 1.5 : 800x600 logical points = 1200x900 physical pixels
scale 2   : 800x600 logical points = 1600x1200 physical pixels

What to do with it:

  1. Lay out in logical units. Toolkits such as egui, iced and Slint already work this way: they take the scale factor from the window and draw at the right size.
  2. Use physical pixels when you talk to the graphics layer, for example the size of a texture or a framebuffer that must match the window's real pixels. winit reports window sizes in physical pixels (WindowEvent::Resized).
  3. Expect the scale factor to change while the program runs. Moving the window to another monitor, or changing the setting, changes it. winit sends WindowEvent::ScaleFactorChanged. See the winit thread.
  4. Do not hard-code "1 point = 1 pixel". It is only true at 100 percent.

Common mistakes#

  • Mixing the two units in one calculation, such as adding a logical margin to a physical width.
  • Caching the scale factor at startup. It can change.
  • Rounding too early. 800 times 1.25 is exact, but 101 times 1.25 is not; convert once at the end.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet