From d121fd5b7d5f890c55b1b73a89e21fce2cf7bbd5 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 31 May 2024 14:23:38 -0400 Subject: [PATCH] css: Tweak compute for color values Make color values carry their srgb equivalent and don't turn them into literals at compute time. This is necessary so we can use their original color space values in interpolation. This makes color values a bit larger, but they still fit into one cacheline. We don't change handling of named colors and our color expressions. They still get turned into literal colors. --- gtk/gtkcsscolorvalue.c | 50 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/gtk/gtkcsscolorvalue.c b/gtk/gtkcsscolorvalue.c index 0d1ba46517..92fbe25629 100644 --- a/gtk/gtkcsscolorvalue.c +++ b/gtk/gtkcsscolorvalue.c @@ -50,13 +50,13 @@ typedef enum { struct _GtkCssValue { GTK_CSS_VALUE_BASE - guint serialize_as_rgb: 1; - ColorType type; + guint serialize_as_rgb : 1; + guint type : 16; + GdkRGBA rgba; union { char *name; - GdkRGBA rgba; GtkCssColor color; struct @@ -190,6 +190,14 @@ static gboolean gtk_css_value_color_equal (const GtkCssValue *value1, const GtkCssValue *value2) { + if (value1->type == COLOR_TYPE_COLOR && value1->color.missing == 0 && + value2->type == COLOR_TYPE_LITERAL) + return gdk_rgba_equal (&value1->rgba, &value2->rgba); + + if (value2->type == COLOR_TYPE_COLOR && value2->color.missing == 0 && + value1->type == COLOR_TYPE_LITERAL) + return gdk_rgba_equal (&value1->rgba, &value2->rgba); + if (value1->type != value2->type) return FALSE; @@ -456,21 +464,8 @@ gtk_css_color_value_do_resolve (GtkCssValue *color, switch (color->type) { case COLOR_TYPE_LITERAL: - value = gtk_css_value_ref (color); - break; - case COLOR_TYPE_COLOR: - { - GtkCssColor tmp; - GdkRGBA rgba; - - gtk_css_color_convert (&color->color, GTK_CSS_COLOR_SPACE_SRGB, &tmp); - rgba.red = tmp.values[0]; - rgba.green = tmp.values[1]; - rgba.blue = tmp.values[2]; - rgba.alpha = tmp.values[3]; - value = gtk_css_color_value_new_literal (&rgba); - } + value = gtk_css_value_ref (color); break; case COLOR_TYPE_NAME: @@ -620,13 +615,22 @@ gtk_css_color_value_new_color (GtkCssColorSpace color_space, gboolean missing[4]) { GtkCssValue *value; + GtkCssColor tmp; value = gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_COLOR); - value->type = COLOR_TYPE_COLOR; value->color.color_space = color_space; + value->is_computed = TRUE; value->serialize_as_rgb = serialize_as_rgb; + value->type = COLOR_TYPE_COLOR; gtk_css_color_init_with_missing (&value->color, color_space, values, missing); + gtk_css_color_convert (&value->color, GTK_CSS_COLOR_SPACE_SRGB, &tmp); + + value->rgba.red = tmp.values[0]; + value->rgba.green = tmp.values[1]; + value->rgba.blue = tmp.values[2]; + value->rgba.alpha = tmp.values[3]; + return value; } @@ -652,7 +656,7 @@ gtk_css_color_value_new_shade (GtkCssValue *color, gtk_internal_return_val_if_fail (color->class == >K_CSS_VALUE_COLOR, NULL); - if (color->type == COLOR_TYPE_LITERAL) + if (color->type == COLOR_TYPE_LITERAL || color->type == COLOR_TYPE_COLOR) { GdkRGBA c; @@ -677,7 +681,7 @@ gtk_css_color_value_new_alpha (GtkCssValue *color, gtk_internal_return_val_if_fail (color->class == >K_CSS_VALUE_COLOR, NULL); - if (color->type == COLOR_TYPE_LITERAL) + if (color->type == COLOR_TYPE_LITERAL || color->type == COLOR_TYPE_COLOR) { GdkRGBA c; @@ -704,8 +708,8 @@ gtk_css_color_value_new_mix (GtkCssValue *color1, gtk_internal_return_val_if_fail (color1->class == >K_CSS_VALUE_COLOR, NULL); gtk_internal_return_val_if_fail (color2->class == >K_CSS_VALUE_COLOR, NULL); - if (color1->type == COLOR_TYPE_LITERAL && - color2->type == COLOR_TYPE_LITERAL) + if ((color1->type == COLOR_TYPE_LITERAL || color1->type == COLOR_TYPE_COLOR) && + (color2->type == COLOR_TYPE_LITERAL || color2->type == COLOR_TYPE_COLOR)) { GdkRGBA result; @@ -1625,7 +1629,7 @@ const GdkRGBA * gtk_css_color_value_get_rgba (const GtkCssValue *color) { g_assert (color->class == >K_CSS_VALUE_COLOR); - g_assert (color->type == COLOR_TYPE_LITERAL); + g_assert (color->type == COLOR_TYPE_LITERAL || color->type == COLOR_TYPE_COLOR); return &color->rgba; }