[gtk/matthiasc/css-values: 10/23] cssshadowvalue: Treat one shadow like multiple shadows



commit 6535af84247cd0701d4a187dc5a2798281b20e5c
Author: Timm Bäder <mail baedert org>
Date:   Thu Jan 9 13:03:40 2020 +0100

    cssshadowvalue: Treat one shadow like multiple shadows
    
    If we create a GtkCssShadowsValue for just one shadow, just return that
    show value instead and allow calls on GtkCssShadowsValue API on one
    shadow. That saves us around 480 GtkCssShadowsValue instances in the
    widget factory.
    
    css value stats before:
    
    GtkCssColorValue: 1452
    GtkCssFilterValue: 3
    GtkCssRgbaValue: 1092
    GtkCssShadowValue: 708
    GtkCssEaseValue: 33
    GtkCssBorderValue: 2
    GtkCssTransformValue: 11
    GtkCssDimensionValue: 882
    GtkCssShadowsValue: 584
    GtkCssBgSizeValue: 23
    GtkCssIdentValue: 25
    GtkCssPositionValue: 81
    GtkCssArrayValue: 143
    GtkCssStringValue: 33
    GtkCssPaletteValue: 29
    GtkCssImageValue: 2765
    SUM: 7872
    
    and after:
    
    GtkCssShadowsValue: 107
    GtkCssBgSizeValue: 23
    GtkCssIdentValue: 25
    GtkCssPositionValue: 81
    GtkCssArrayValue: 143
    GtkCssStringValue: 33
    GtkCssPaletteValue: 29
    GtkCssImageValue: 2764
    GtkCssColorValue: 1452
    GtkCssFilterValue: 3
    GtkCssRgbaValue: 1092
    GtkCssShadowValue: 708
    GtkCssEaseValue: 33
    GtkCssBorderValue: 2
    GtkCssTransformValue: 11
    GtkCssDimensionValue: 883
    SUM: 7395
    
    7872 to 7395 is a 477 reduction (6.0%)

 gtk/gtkcssshadowsvalue.c       | 61 +++++++++++++++++++++++++++++++++++++++---
 gtk/gtkcssshadowvalue.c        |  5 ++++
 gtk/gtkcssshadowvalueprivate.h |  2 ++
 3 files changed, 64 insertions(+), 4 deletions(-)
---
diff --git a/gtk/gtkcssshadowsvalue.c b/gtk/gtkcssshadowsvalue.c
index 60e3a548ec..2e09dae683 100644
--- a/gtk/gtkcssshadowsvalue.c
+++ b/gtk/gtkcssshadowsvalue.c
@@ -223,14 +223,17 @@ gtk_css_shadows_value_new (GtkCssValue **values,
                            guint         len)
 {
   GtkCssValue *result;
-           
+
   g_return_val_if_fail (values != NULL, NULL);
   g_return_val_if_fail (len > 0, NULL);
-         
+
+  if (len == 1)
+    return values[0];
+
   result = _gtk_css_value_alloc (&GTK_CSS_VALUE_SHADOWS, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * 
(len - 1));
   result->len = len;
   memcpy (&result->values[0], values, sizeof (GtkCssValue *) * len);
-            
+
   return result;
 }
 
@@ -267,6 +270,9 @@ _gtk_css_shadows_value_parse (GtkCssParser *parser,
 gboolean
 _gtk_css_shadows_value_is_none (const GtkCssValue *shadows)
 {
+  if (gtk_css_shadow_value_is_shadow (shadows))
+    return FALSE;
+
   g_return_val_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS, TRUE);
 
   return shadows->len == 0;
@@ -275,6 +281,9 @@ _gtk_css_shadows_value_is_none (const GtkCssValue *shadows)
 gsize
 gtk_css_shadows_value_get_n_shadows (const GtkCssValue *shadows)
 {
+  if (gtk_css_shadow_value_is_shadow (shadows))
+    return 1;
+
   return shadows->len;
 }
 
@@ -284,6 +293,12 @@ gtk_css_shadows_value_get_shadows (const GtkCssValue  *shadows,
 {
   guint i;
 
+  if (gtk_css_shadow_value_is_shadow (shadows))
+    {
+      gtk_css_shadow_value_get_shadow (shadows, &out_shadows[0]);
+      return;
+    }
+
   for (i = 0; i < shadows->len; i++)
     gtk_css_shadow_value_get_shadow (shadows->values[i], &out_shadows[i]);
 }
@@ -295,6 +310,14 @@ gtk_css_shadows_value_snapshot_outset (const GtkCssValue   *shadows,
 {
   guint i;
 
+  if (gtk_css_shadow_value_is_shadow (shadows))
+    {
+      if (!_gtk_css_shadow_value_get_inset (shadows))
+        gtk_css_shadow_value_snapshot_outset (shadows, snapshot, border_box);
+
+      return;
+    }
+
   g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);
 
   for (i = 0; i < shadows->len; i++)
@@ -313,6 +336,14 @@ gtk_css_shadows_value_snapshot_inset (const GtkCssValue   *shadows,
 {
   guint i;
 
+  if (gtk_css_shadow_value_is_shadow (shadows))
+    {
+      if (_gtk_css_shadow_value_get_inset (shadows))
+        gtk_css_shadow_value_snapshot_outset (shadows, snapshot, padding_box);
+
+      return;
+    }
+
   g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);
 
   for (i = 0; i < shadows->len; i++)
@@ -332,6 +363,15 @@ _gtk_css_shadows_value_get_extents (const GtkCssValue *shadows,
   GtkBorder b = { 0 }, sb;
   const GtkCssValue *shadow;
 
+  if (gtk_css_shadow_value_is_shadow (shadows))
+    {
+      if (!_gtk_css_shadow_value_get_inset (shadows))
+        gtk_css_shadow_value_get_extents (shadows, &b);
+
+      *border = b;
+      return;
+    }
+
   g_return_if_fail (shadows->class == &GTK_CSS_VALUE_SHADOWS);
 
   for (i = 0; i < shadows->len; i++)
@@ -356,9 +396,22 @@ gboolean
 gtk_css_shadows_value_push_snapshot (const GtkCssValue *value,
                                      GtkSnapshot       *snapshot)
 {
-  gboolean need_shadow = FALSE;
+  gboolean need_shadow;
   int i;
 
+  if (gtk_css_shadow_value_is_shadow (value))
+    {
+      GskShadow shadow;
+
+      if (!gtk_css_shadow_value_is_clear (value))
+        return FALSE;
+
+      gtk_css_shadow_value_get_shadow (value, &shadow);
+      gtk_snapshot_push_shadow (snapshot, &shadow, 1);
+      return TRUE;
+    }
+
+  need_shadow = FALSE;
   for (i = 0; i < value->len; i++)
     {
       if (!gtk_css_shadow_value_is_clear (value->values[i]))
diff --git a/gtk/gtkcssshadowvalue.c b/gtk/gtkcssshadowvalue.c
index 79a2950b8e..9f44d5f0b1 100644
--- a/gtk/gtkcssshadowvalue.c
+++ b/gtk/gtkcssshadowvalue.c
@@ -473,3 +473,8 @@ gtk_css_shadow_value_is_clear (const GtkCssValue *shadow)
   return gdk_rgba_is_clear (_gtk_css_rgba_value_get_rgba (shadow->color));
 }
 
+gboolean
+gtk_css_shadow_value_is_shadow (const GtkCssValue *maybe_shadow)
+{
+  return maybe_shadow->class == &GTK_CSS_VALUE_SHADOW;
+}
diff --git a/gtk/gtkcssshadowvalueprivate.h b/gtk/gtkcssshadowvalueprivate.h
index c2f66f0d72..b817dc61b1 100644
--- a/gtk/gtkcssshadowvalueprivate.h
+++ b/gtk/gtkcssshadowvalueprivate.h
@@ -52,6 +52,8 @@ void            gtk_css_shadow_value_snapshot_inset   (const GtkCssValue
 
 gboolean        gtk_css_shadow_value_is_clear         (const GtkCssValue        *shadow);
 
+gboolean        gtk_css_shadow_value_is_shadow        (const GtkCssValue        *maybe_shadow);
+
 G_END_DECLS
 
 #endif /* __GTK_SHADOW_H__ */


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