Commit f5dcca6b authored by Marco Trevisan's avatar Marco Trevisan 🎺

monitor-manager-xrandr: Use monitor to calculate scaling

In Xrandr we were caching the available scaling modes that were computed just
for the current mode, for each monitor, while we can actually reuse the
default implementation, by just passing the proper scaling constraint.

In monitor we need then to properly filter these values, by only accepting
integer scaling factors that would allow to have a minimal logical monitor
size.

Fixes GNOME/mutter#407
GNOME/mutter!336
parent 4f09b250
......@@ -1570,6 +1570,16 @@ is_logical_size_large_enough (gint width, gint height)
height >= MINIMUM_LOGICAL_HEIGHT;
}
static gboolean
is_scale_valid_for_size (float width,
float height,
float scale)
{
return scale >= MINIMUM_SCALE_FACTOR &&
scale <= MAXIMUM_SCALE_FACTOR &&
is_logical_size_large_enough (floorf (width/scale), floorf (width/scale));
}
gboolean
meta_monitor_mode_should_be_advertised (MetaMonitorMode *monitor_mode)
{
......@@ -1592,21 +1602,16 @@ get_closest_scale_factor_for_resolution (float 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.0 && fmodf (height, scale) == 0.0)
return scale;
i = 0;
found_one = FALSE;
base_scaled_w = floorf (scaled_w);
base_scaled_w = floorf (width / scale);
do
{
for (j = 0; j < 2; j++)
......@@ -1666,16 +1671,18 @@ meta_monitor_calculate_supported_scales (MetaMonitor *monitor,
float scale;
float scale_value = i + j * SCALE_FACTORS_STEPS;
if ((constraints & META_MONITOR_SCALES_CONSTRAINT_NO_FRAC) &&
fmodf (scale_value, 1.0) != 0.0)
if (constraints & META_MONITOR_SCALES_CONSTRAINT_NO_FRAC)
{
continue;
}
scale = get_closest_scale_factor_for_resolution (width,
height,
scale_value);
if (fmodf (scale_value, 1.0) != 0.0 ||
!is_scale_valid_for_size (width, height, scale_value))
continue;
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);
}
......
......@@ -84,8 +84,6 @@ struct _MetaMonitorManagerXrandr
GHashTable *tiled_monitor_atoms;
float *supported_scales;
int n_supported_scales;
};
struct _MetaMonitorManagerXrandrClass
......@@ -562,17 +560,6 @@ meta_monitor_manager_xrandr_ensure_initial_config (MetaMonitorManager *manager)
meta_monitor_manager_update_logical_state_derived (manager, config);
}
static void
meta_monitor_manager_xrandr_rebuild_derived (MetaMonitorManager *manager,
MetaMonitorsConfig *config)
{
MetaMonitorManagerXrandr *manager_xrandr =
META_MONITOR_MANAGER_XRANDR (manager);
g_clear_pointer (&manager_xrandr->supported_scales, g_free);
meta_monitor_manager_rebuild_derived (manager, config);
}
static gboolean
meta_monitor_manager_xrandr_apply_monitors_config (MetaMonitorManager *manager,
MetaMonitorsConfig *config,
......@@ -584,7 +571,7 @@ meta_monitor_manager_xrandr_apply_monitors_config (MetaMonitorManager *mana
if (!config)
{
meta_monitor_manager_xrandr_rebuild_derived (manager, NULL);
meta_monitor_manager_rebuild_derived (manager, NULL);
return TRUE;
}
......@@ -618,7 +605,7 @@ meta_monitor_manager_xrandr_apply_monitors_config (MetaMonitorManager *mana
}
else
{
meta_monitor_manager_xrandr_rebuild_derived (manager, config);
meta_monitor_manager_rebuild_derived (manager, config);
}
}
......@@ -861,77 +848,6 @@ meta_monitor_manager_xrandr_calculate_monitor_mode_scale (MetaMonitorManager
return meta_monitor_calculate_mode_scale (monitor, monitor_mode, constraints);
}
static void
add_supported_scale (GArray *supported_scales,
float scale)
{
unsigned int i;
for (i = 0; i < supported_scales->len; i++)
{
float supported_scale = g_array_index (supported_scales, float, i);
if (scale == supported_scale)
return;
}
g_array_append_val (supported_scales, scale);
}
static int
compare_scales (gconstpointer a,
gconstpointer b)
{
float f = *(float *) a - *(float *) b;
if (f < 0)
return -1;
if (f > 0)
return 1;
return 0;
}
static void
ensure_supported_monitor_scales (MetaMonitorManager *manager)
{
MetaMonitorManagerXrandr *manager_xrandr =
META_MONITOR_MANAGER_XRANDR (manager);
MetaMonitorScalesConstraint constraints;
GList *l;
GArray *supported_scales;
if (manager_xrandr->supported_scales)
return;
constraints = META_MONITOR_SCALES_CONSTRAINT_NO_FRAC;
supported_scales = g_array_new (FALSE, FALSE, sizeof (float));
for (l = manager->monitors; l; l = l->next)
{
MetaMonitor *monitor = l->data;
MetaMonitorMode *monitor_mode;
float *monitor_scales;
int n_monitor_scales;
int i;
monitor_mode = meta_monitor_get_preferred_mode (monitor);
monitor_scales =
meta_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);
}
manager_xrandr->supported_scales = (float *) supported_scales->data;
manager_xrandr->n_supported_scales = supported_scales->len;
g_array_free (supported_scales, FALSE);
}
static float *
meta_monitor_manager_xrandr_calculate_supported_scales (MetaMonitorManager *manager,
MetaLogicalMonitorLayoutMode layout_mode,
......@@ -939,14 +855,12 @@ meta_monitor_manager_xrandr_calculate_supported_scales (MetaMonitorManager
MetaMonitorMode *monitor_mode,
int *n_supported_scales)
{
MetaMonitorManagerXrandr *manager_xrandr =
META_MONITOR_MANAGER_XRANDR (manager);
ensure_supported_monitor_scales (manager);
MetaMonitorScalesConstraint constraints;
*n_supported_scales = manager_xrandr->n_supported_scales;
return g_memdup (manager_xrandr->supported_scales,
manager_xrandr->n_supported_scales * sizeof (float));
constraints = META_MONITOR_SCALES_CONSTRAINT_NO_FRAC;
return meta_monitor_calculate_supported_scales (monitor, monitor_mode,
constraints,
n_supported_scales);
}
static MetaMonitorManagerCapability
......@@ -1030,7 +944,6 @@ meta_monitor_manager_xrandr_finalize (GObject *object)
g_clear_object (&manager_xrandr->gpu);
g_hash_table_destroy (manager_xrandr->tiled_monitor_atoms);
g_free (manager_xrandr->supported_scales);
G_OBJECT_CLASS (meta_monitor_manager_xrandr_parent_class)->finalize (object);
}
......@@ -1112,7 +1025,7 @@ meta_monitor_manager_xrandr_handle_xevent (MetaMonitorManagerXrandr *manager_xra
config = NULL;
}
meta_monitor_manager_xrandr_rebuild_derived (manager, config);
meta_monitor_manager_rebuild_derived (manager, config);
}
return TRUE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment