forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf2ce84bYuya Nishihara2 years ago1130 commits

scale.rs104 lines, 3.6 KiBHistoryRawDownload
1//! Default monitor scale calculation.
2//!
3//! This module follows logic and tests from Mutter:
4//! https://gitlab.gnome.org/GNOME/mutter/-/blob/gnome-46/src/backends/meta-monitor.c
5
6use smithay::utils::{Physical, Raw, Size};
7
8const MIN_SCALE: i32 = 1;
9const MAX_SCALE: i32 = 4;
10const MIN_LOGICAL_AREA: i32 = 800 * 480;
11
12const MOBILE_TARGET_DPI: f64 = 135.;
13const LARGE_TARGET_DPI: f64 = 110.;
14const LARGE_MIN_SIZE_INCHES: f64 = 20.;
15
16/// Calculates the ideal scale for a monitor.
17pub fn guess_monitor_scale(size_mm: Size<i32, Raw>, resolution: Size<i32, Physical>) -> f64 {
18 if size_mm.w == 0 || size_mm.h == 0 {
19 return 1.;
20 }
21
22 let diag_inches = f64::from(size_mm.w * size_mm.w + size_mm.h * size_mm.h).sqrt() / 25.4;
23
24 let target_dpi = if diag_inches < LARGE_MIN_SIZE_INCHES {
25 MOBILE_TARGET_DPI
26 } else {
27 LARGE_TARGET_DPI
28 };
29
30 let physical_dpi =
31 f64::from(resolution.w * resolution.w + resolution.h * resolution.h).sqrt() / diag_inches;
32 let perfect_scale = physical_dpi / target_dpi;
33
34 // For integer scaling factors (we currently only do integer), bias the perfect scale down.
35 let perfect_scale = perfect_scale - 0.15;
36
37 supported_scales(resolution)
38 .map(|scale| (scale, (scale - perfect_scale).abs()))
39 .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
40 .map_or(1., |(scale, _)| scale)
41}
42
43fn supported_scales(resolution: Size<i32, Physical>) -> impl Iterator<Item = f64> {
44 (MIN_SCALE..=MAX_SCALE)
45 .filter(move |scale| is_valid_for_resolution(resolution, *scale))
46 .map(f64::from)
47}
48
49fn is_valid_for_resolution(resolution: Size<i32, Physical>, scale: i32) -> bool {
50 let logical = resolution.to_logical(scale);
51 logical.w * logical.h >= MIN_LOGICAL_AREA
52}
53
54#[cfg(test)]
55mod tests {
56 use k9::snapshot;
57
58 use super::*;
59
60 fn check(size_mm: (i32, i32), resolution: (i32, i32)) -> f64 {
61 guess_monitor_scale(Size::from(size_mm), Size::from(resolution))
62 }
63
64 #[test]
65 fn test_guess_monitor_scale() {
66 // Librem 5; not enough logical area when scaled
67 snapshot!(check((65, 129), (720, 1440)), "1.0");
68 // OnePlus 6
69 snapshot!(check((68, 144), (1080, 2280)), "2.0");
70 // Google Pixel 6a
71 snapshot!(check((64, 142), (1080, 2400)), "2.0");
72 // 13" MacBook Retina
73 snapshot!(check((286, 179), (2560, 1600)), "2.0");
74 // Surface Laptop Studio
75 snapshot!(check((303, 202), (2400, 1600)), "1.0");
76 // Dell XPS 9320
77 snapshot!(check((290, 180), (3840, 2400)), "2.0");
78 // Lenovo ThinkPad X1 Yoga Gen 6
79 snapshot!(check((300, 190), (3840, 2400)), "2.0");
80 // Generic 23" 1080p
81 snapshot!(check((509, 286), (1920, 1080)), "1.0");
82 // Generic 23" 4K
83 snapshot!(check((509, 286), (3840, 2160)), "2.0");
84 // Generic 27" 4K
85 snapshot!(check((598, 336), (3840, 2160)), "1.0");
86 // Generic 32" 4K
87 snapshot!(check((708, 398), (3840, 2160)), "1.0");
88 // Generic 25" 4K; ideal scale is 1.60, should round to 1.5 and 1.0
89 snapshot!(check((554, 312), (3840, 2160)), "1.0");
90 // Generic 23.5" 4K; ideal scale is 1.70, should round to 1.75 and 2.0
91 snapshot!(check((522, 294), (3840, 2160)), "2.0");
92 // Lenovo Legion 7 Gen 7 AMD 16"
93 snapshot!(check((340, 210), (2560, 1600)), "1.0");
94 // Acer Nitro XV320QU LV 31.5"
95 snapshot!(check((700, 390), (2560, 1440)), "1.0");
96 // Surface Pro 6
97 snapshot!(check((260, 170), (2736, 1824)), "2.0");
98 }
99
100 #[test]
101 fn guess_monitor_scale_unknown_size() {
102 assert_eq!(check((0, 0), (1920, 1080)), 1.);
103 }
104}