[gtk/wip/baedert/css-values: 57/84] cssimagelinear: Don't compute new image if it didn't change



commit eef74c18e8ecd4c00ca31138af3858b9903bd7df
Author: Timm Bäder <mail baedert org>
Date:   Sat Jan 11 09:16:25 2020 +0100

    cssimagelinear: Don't compute new image if it didn't change

 gtk/gtkcssimagelinear.c | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)
---
diff --git a/gtk/gtkcssimagelinear.c b/gtk/gtkcssimagelinear.c
index 52e9a33e2a..8ae38a8288 100644
--- a/gtk/gtkcssimagelinear.c
+++ b/gtk/gtkcssimagelinear.c
@@ -495,24 +495,26 @@ gtk_css_image_linear_compute (GtkCssImage      *image,
                               GtkCssStyle      *parent_style)
 {
   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
+  gboolean changed = FALSE;
+  GtkCssImageLinearColorStop *stops;
+  GtkCssValue *computed_angle = NULL;
   GtkCssImageLinear *copy;
   guint i;
 
-  copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
-  copy->repeating = linear->repeating;
-  copy->side = linear->side;
-
   if (linear->angle)
-    copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, style, parent_style);
+    {
+      computed_angle = _gtk_css_value_compute (linear->angle, property_id, provider, style, parent_style);
+      changed |= (computed_angle != linear->angle);
+    }
 
-  copy->n_stops = linear->n_stops;
-  copy->color_stops = g_malloc (sizeof (GtkCssImageLinearColorStop) * copy->n_stops);
+  stops = g_alloca (sizeof (GtkCssImageLinearColorStop) * linear->n_stops);
   for (i = 0; i < linear->n_stops; i++)
     {
       const GtkCssImageLinearColorStop *stop = &linear->color_stops[i];
-      GtkCssImageLinearColorStop *scopy = &copy->color_stops[i];
+      GtkCssImageLinearColorStop *scopy = &stops[i];
 
       scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, style, parent_style);
+      changed |= (scopy->color != stop->color);
 
       if (stop->offset)
         {
@@ -524,6 +526,30 @@ gtk_css_image_linear_compute (GtkCssImage      *image,
         }
     }
 
+  if (!changed)
+    {
+      if (computed_angle)
+        _gtk_css_value_unref (computed_angle);
+
+      for (i = 0; i < linear->n_stops; i ++)
+        {
+          _gtk_css_value_unref (stops[i].color);
+          if (stops[i].offset)
+            _gtk_css_value_unref (stops[i].offset);
+        }
+
+      return g_object_ref (image);
+    }
+
+  copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
+  copy->repeating = linear->repeating;
+  copy->side = linear->side;
+  copy->angle = computed_angle;
+  copy->n_stops = linear->n_stops;
+  copy->color_stops = g_malloc (sizeof (GtkCssImageLinearColorStop) * copy->n_stops);
+
+  memcpy (copy->color_stops, stops, sizeof (GtkCssImageLinearColorStop ) * linear->n_stops);
+
   return GTK_CSS_IMAGE (copy);
 }
 


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