]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
ply-utils: Match mutter's default device scale choice
authorDaniel van Vugt <daniel.van.vugt@canonical.com>
Tue, 27 Feb 2024 06:47:59 +0000 (14:47 +0800)
committerDaniel van Vugt <daniel.van.vugt@canonical.com>
Tue, 27 Feb 2024 06:56:41 +0000 (14:56 +0800)
Until now, laptops with a DPI between 192 and 202 would be given a
default scale of 2 by Plymouth, and 1 by Mutter for the login screen.
That made the visual transition a bit ugly so let's match Mutter's
default scale selection. This means the threshold for laptops is now
1.5 x 135 = 202 DPI instead of 192 DPI. And for desktop monitors it's
now 1.5 x 110 = 165 DPI instead of 192 DPI.

Closes: https://bugs.launchpad.net/bugs/2054769
src/libply/ply-utils.c

index 463f61c927205b5c779c73220323bd71a4258189..ad816f14a831898ad0848fc2499c50ac1bb87285 100644 (file)
@@ -29,6 +29,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <locale.h>
+#include <math.h>
 #include <poll.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -1001,8 +1002,6 @@ ply_set_device_scale (int device_scale)
         ply_trace ("Device scale is set to %d", device_scale);
 }
 
-/* The minimum resolution at which we turn on a device-scale of 2 */
-#define HIDPI_LIMIT 192
 #define HIDPI_MIN_HEIGHT 1200
 #define HIDPI_MIN_WIDTH 2560 /* For heuristic / guessed device-scale */
 
@@ -1019,8 +1018,8 @@ get_device_scale (uint32_t width,
                   uint32_t height_mm,
                   bool     guess)
 {
-        int device_scale;
-        double dpi_x, dpi_y;
+        int device_scale, target_dpi;
+        float diag_inches, diag_pixels, physical_dpi, perfect_scale;
         const char *force_device_scale;
 
         device_scale = 1;
@@ -1031,11 +1030,10 @@ get_device_scale (uint32_t width,
         if (overridden_device_scale != 0)
                 return overridden_device_scale;
 
-        if (height < HIDPI_MIN_HEIGHT)
-                return 1;
-
-        if (guess)
-                return (width >= HIDPI_MIN_WIDTH) ? 2 : 1;
+        if (guess) {
+                return (width >= HIDPI_MIN_WIDTH &&
+                        height >= HIDPI_MIN_HEIGHT) ? 2 : 1;
+        }
 
         /* Somebody encoded the aspect ratio (16/9 or 16/10)
          * instead of the physical size */
@@ -1045,15 +1043,21 @@ get_device_scale (uint32_t width,
             (width_mm == 16 && height_mm == 10))
                 return 1;
 
-        if (width_mm > 0 && height_mm > 0) {
-                dpi_x = (double) width / (width_mm / 25.4);
-                dpi_y = (double) height / (height_mm / 25.4);
-                /* We don't completely trust these values so both
-                 * must be high, and never pick higher ratio than
-                 * 2 automatically */
-                if (dpi_x > HIDPI_LIMIT && dpi_y > HIDPI_LIMIT)
-                        device_scale = 2;
-        }
+        if (width_mm == 0 || height_mm == 0)
+                return 1;
+
+        diag_inches = sqrtf (width_mm * width_mm + height_mm * height_mm) /
+                      25.4f;
+        diag_pixels = sqrtf (width * width + height * height);
+        physical_dpi = diag_pixels / diag_inches;
+
+        /* These constants are copied from Mutter's meta-monitor.c in order
+         * to match the default scale choice of the login screen.
+         */
+        target_dpi = (diag_inches >= 20.f) ? 110 : 135;
+
+        perfect_scale = physical_dpi / target_dpi;
+        device_scale = (perfect_scale > 1.5f) ? 2 : 1;
 
         return device_scale;
 }