[gnome-flashback] monitor-manager-xrandr: use monitor to calculate scaling



commit 653cb2c66479a7eeafd1df6904f8df70654ae588
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sun Sep 12 23:00:47 2021 +0300

    monitor-manager-xrandr: use monitor to calculate scaling
    
    Based on mutter commit:
    https://gitlab.gnome.org/GNOME/mutter/-/commit/1ac63fcd77a2

 backends/gf-monitor-manager-xrandr.c | 126 +++--------------------------------
 backends/gf-monitor.c                |  41 ++++++++----
 2 files changed, 36 insertions(+), 131 deletions(-)
---
diff --git a/backends/gf-monitor-manager-xrandr.c b/backends/gf-monitor-manager-xrandr.c
index f4abc5b..846aa05 100644
--- a/backends/gf-monitor-manager-xrandr.c
+++ b/backends/gf-monitor-manager-xrandr.c
@@ -64,9 +64,6 @@ struct _GfMonitorManagerXrandr
   GHashTable       *tiled_monitor_atoms;
 
   Time              last_xrandr_set_timestamp;
-
-  gfloat           *supported_scales;
-  gint              n_supported_scales;
 };
 
 typedef struct
@@ -76,97 +73,6 @@ typedef struct
 
 G_DEFINE_TYPE (GfMonitorManagerXrandr, gf_monitor_manager_xrandr, GF_TYPE_MONITOR_MANAGER)
 
-static void
-add_supported_scale (GArray *supported_scales,
-                     gfloat  scale)
-{
-  guint i;
-
-  for (i = 0; i < supported_scales->len; i++)
-    {
-      gfloat supported_scale;
-
-      supported_scale = g_array_index (supported_scales, gfloat, i);
-
-      if (scale == supported_scale)
-        return;
-    }
-
-  g_array_append_val (supported_scales, scale);
-}
-
-static gint
-compare_scales (gconstpointer a,
-                gconstpointer b)
-{
-  gfloat f = *(gfloat *) a - *(gfloat *) b;
-
-  if (f < 0)
-    return -1;
-
-  if (f > 0)
-    return 1;
-
-  return 0;
-}
-
-static void
-ensure_supported_monitor_scales (GfMonitorManager *manager)
-{
-  GfMonitorManagerXrandr *xrandr;
-  GfMonitorScalesConstraint constraints;
-  GArray *supported_scales;
-  GList *l;
-
-  xrandr = GF_MONITOR_MANAGER_XRANDR (manager);
-
-  if (xrandr->supported_scales)
-    return;
-
-  constraints = GF_MONITOR_SCALES_CONSTRAINT_NO_FRAC;
-  supported_scales = g_array_new (FALSE, FALSE, sizeof (gfloat));
-
-  for (l = manager->monitors; l; l = l->next)
-    {
-      GfMonitor *monitor;
-      GfMonitorMode *monitor_mode;
-      gfloat *monitor_scales;
-      gint n_monitor_scales;
-      gint i;
-
-      monitor = l->data;
-      monitor_mode = gf_monitor_get_preferred_mode (monitor);
-      monitor_scales = gf_monitor_calculate_supported_scales (monitor,
-                                                              monitor_mode,
-                                                              constraints,
-                                                              &n_monitor_scales);
-
-      for (i = 0; i < n_monitor_scales; i++)
-        {
-          add_supported_scale (supported_scales, monitor_scales[i]);
-        }
-
-      g_array_sort (supported_scales, compare_scales);
-      g_free (monitor_scales);
-    }
-
-  xrandr->supported_scales = (gfloat *) supported_scales->data;
-  xrandr->n_supported_scales = supported_scales->len;
-  g_array_free (supported_scales, FALSE);
-}
-
-static void
-gf_monitor_manager_xrandr_rebuild_derived (GfMonitorManager *manager,
-                                           GfMonitorsConfig *config)
-{
-  GfMonitorManagerXrandr *xrandr;
-
-  xrandr = GF_MONITOR_MANAGER_XRANDR (manager);
-
-  g_clear_pointer (&xrandr->supported_scales, g_free);
-  gf_monitor_manager_rebuild_derived (manager, config);
-}
-
 static gboolean
 xrandr_set_crtc_config (GfMonitorManagerXrandr *xrandr,
                         GfCrtc                 *crtc,
@@ -728,18 +634,6 @@ gf_monitor_manager_xrandr_dispose (GObject *object)
   G_OBJECT_CLASS (gf_monitor_manager_xrandr_parent_class)->dispose (object);
 }
 
-static void
-gf_monitor_manager_xrandr_finalize (GObject *object)
-{
-  GfMonitorManagerXrandr *xrandr;
-
-  xrandr = GF_MONITOR_MANAGER_XRANDR (object);
-
-  g_clear_pointer (&xrandr->supported_scales, g_free);
-
-  G_OBJECT_CLASS (gf_monitor_manager_xrandr_parent_class)->finalize (object);
-}
-
 static GBytes *
 gf_monitor_manager_xrandr_read_edid (GfMonitorManager *manager,
                                      GfOutput         *output)
@@ -827,7 +721,7 @@ gf_monitor_manager_xrandr_apply_monitors_config (GfMonitorManager        *manage
 
   if (!config)
     {
-      gf_monitor_manager_xrandr_rebuild_derived (manager, NULL);
+      gf_monitor_manager_rebuild_derived (manager, NULL);
       return TRUE;
     }
 
@@ -861,7 +755,7 @@ gf_monitor_manager_xrandr_apply_monitors_config (GfMonitorManager        *manage
         }
       else
         {
-          gf_monitor_manager_xrandr_rebuild_derived (manager, config);
+          gf_monitor_manager_rebuild_derived (manager, config);
         }
     }
 
@@ -1067,15 +961,14 @@ gf_monitor_manager_xrandr_calculate_supported_scales (GfMonitorManager
                                                       GfMonitorMode              *monitor_mode,
                                                       gint                       *n_supported_scales)
 {
-  GfMonitorManagerXrandr *xrandr;
-
-  xrandr = GF_MONITOR_MANAGER_XRANDR (manager);
+  GfMonitorScalesConstraint constraints;
 
-  ensure_supported_monitor_scales (manager);
+  constraints = GF_MONITOR_SCALES_CONSTRAINT_NO_FRAC;
 
-  *n_supported_scales = xrandr->n_supported_scales;
-  return g_memdup2 (xrandr->supported_scales,
-                    xrandr->n_supported_scales * sizeof (gfloat));
+  return gf_monitor_calculate_supported_scales (monitor,
+                                                monitor_mode,
+                                                constraints,
+                                                n_supported_scales);
 }
 
 static GfMonitorManagerCapability
@@ -1118,7 +1011,6 @@ gf_monitor_manager_xrandr_class_init (GfMonitorManagerXrandrClass *xrandr_class)
 
   object_class->constructed = gf_monitor_manager_xrandr_constructed;
   object_class->dispose = gf_monitor_manager_xrandr_dispose;
-  object_class->finalize = gf_monitor_manager_xrandr_finalize;
 
   manager_class->read_edid = gf_monitor_manager_xrandr_read_edid;
   manager_class->read_current_state = gf_monitor_manager_xrandr_read_current_state;
@@ -1197,7 +1089,7 @@ gf_monitor_manager_xrandr_handle_xevent (GfMonitorManagerXrandr *xrandr,
           config = gf_monitor_config_manager_get_current (config_manager);
         }
 
-      gf_monitor_manager_xrandr_rebuild_derived (manager, config);
+      gf_monitor_manager_rebuild_derived (manager, config);
     }
 
   return TRUE;
diff --git a/backends/gf-monitor.c b/backends/gf-monitor.c
index a8fcc1d..bd5674c 100644
--- a/backends/gf-monitor.c
+++ b/backends/gf-monitor.c
@@ -306,6 +306,18 @@ is_logical_size_large_enough (gint width,
   return width * height >= MINIMUM_LOGICAL_AREA;
 }
 
+static gboolean
+is_scale_valid_for_size (float width,
+                         float height,
+                         float scale)
+{
+  if (scale < MINIMUM_SCALE_FACTOR || scale > MAXIMUM_SCALE_FACTOR)
+    return FALSE;
+
+  return is_logical_size_large_enough (floorf (width / scale),
+                                       floorf (height / scale));
+}
+
 static gfloat
 get_closest_scale_factor_for_resolution (gfloat width,
                                          gfloat height,
@@ -319,20 +331,16 @@ get_closest_scale_factor_for_resolution (gfloat width,
   gboolean found_one;
 
   best_scale = 0;
-  scaled_w = width / scale;
-  scaled_h = height / scale;
 
-  if (scale < MINIMUM_SCALE_FACTOR ||
-      scale > MAXIMUM_SCALE_FACTOR ||
-      !is_logical_size_large_enough (floorf (scaled_w), floorf (scaled_h)))
+  if (!is_scale_valid_for_size (width, height, scale))
     goto out;
 
-  if (floorf (scaled_w) == scaled_w && floorf (scaled_h) == scaled_h)
+  if (fmodf (width, scale) == 0.0f && fmodf (height, scale) == 0.0f)
     return scale;
 
   i = 0;
   found_one = FALSE;
-  base_scaled_w = floorf (scaled_w);
+  base_scaled_w = floorf (width / scale);
 
   do
     {
@@ -1094,15 +1102,20 @@ gf_monitor_calculate_supported_scales (GfMonitor                 *monitor,
           gfloat scale;
           gfloat scale_value = i + j * SCALE_FACTORS_STEPS;
 
-          if ((constraints & GF_MONITOR_SCALES_CONSTRAINT_NO_FRAC) &&
-              fmodf (scale_value, 1.0) != 0.0f)
+          if (constraints & GF_MONITOR_SCALES_CONSTRAINT_NO_FRAC)
             {
-              continue;
-            }
+              if (fmodf (scale_value, 1.0) != 0.0f ||
+                  !is_scale_valid_for_size (width, height, scale_value))
+                continue;
 
-          scale = get_closest_scale_factor_for_resolution (width,
-                                                           height,
-                                                           scale_value);
+              scale = scale_value;
+            }
+          else
+            {
+              scale = get_closest_scale_factor_for_resolution (width,
+                                                               height,
+                                                               scale_value);
+            }
 
           if (scale > 0.0f)
             g_array_append_val (supported_scales, scale);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]