From a8be68c83cb97a8bafc93f5304e177e1b265bca1 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Fri, 5 Jun 2009 12:58:31 +0100 Subject: [PATCH] [cogl-color] Adds a cogl_color_equal() function CoglColors shouldn't be compared using memcmp since they may contain uninitialized padding bytes. The prototype is also suitable for passing to g_hash_table_new as the key_equal_func. _cogl_pango_display_list_add_texture now uses this instead of memcmp. --- clutter/cogl/cogl-color.h | 16 ++++++++++++++++ clutter/cogl/common/cogl-color.c | 15 +++++++++++++++ clutter/pango/cogl-pango-display-list.c | 3 +-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/clutter/cogl/cogl-color.h b/clutter/cogl/cogl-color.h index 73de891de..6ade2594f 100644 --- a/clutter/cogl/cogl-color.h +++ b/clutter/cogl/cogl-color.h @@ -240,6 +240,22 @@ float cogl_color_get_alpha (const CoglColor *color); */ void cogl_color_premultiply (CoglColor *color); +/** + * cogl_color_equal: + * @v1: a #CoglColor + * @v2: a #CoglColor + * + * Compares two #CoglColors and checks if they are the same. + * + * This function can be passed to g_hash_table_new() as the @key_equal_func + * parameter, when using #CoglColors as keys in a #GHashTable. + * + * Return value: %TRUE if the two colors are the same. + * + * Since: 1.0 + */ +gboolean cogl_color_equal (gconstpointer v1, gconstpointer v2); + G_END_DECLS #endif /* __COGL_COLOR_H__ */ diff --git a/clutter/cogl/common/cogl-color.c b/clutter/cogl/common/cogl-color.c index df338fcc4..25a1bdaf2 100644 --- a/clutter/cogl/common/cogl-color.c +++ b/clutter/cogl/common/cogl-color.c @@ -25,6 +25,8 @@ #include "config.h" #endif +#include + #include "cogl.h" #include "cogl-color.h" #include "cogl-fixed.h" @@ -184,3 +186,16 @@ cogl_set_source_color4f (float red, cogl_color_set_from_4f (&c, red, green, blue, alpha); cogl_set_source_color (&c); } + +gboolean +cogl_color_equal (gconstpointer v1, gconstpointer v2) +{ + const guint32 *c1 = v1, *c2 = v2; + + g_return_val_if_fail (v1 != NULL, FALSE); + g_return_val_if_fail (v2 != NULL, FALSE); + + /* XXX: We don't compare the padding */ + return *c1 == *c2 ? TRUE : FALSE; +} + diff --git a/clutter/pango/cogl-pango-display-list.c b/clutter/pango/cogl-pango-display-list.c index 6a3b29c60..6790411bf 100644 --- a/clutter/pango/cogl-pango-display-list.c +++ b/clutter/pango/cogl-pango-display-list.c @@ -138,8 +138,7 @@ _cogl_pango_display_list_add_texture (CoglPangoDisplayList *dl, && (node = dl->last_node->data)->type == COGL_PANGO_DISPLAY_LIST_TEXTURE && node->d.texture.texture == texture && (dl->color_override - ? (node->color_override && !memcmp (&dl->color, &node->color, - sizeof (CoglColor))) + ? (node->color_override && cogl_color_equal (&dl->color, &node->color)) : !node->color_override)) { /* Get rid of the vertex buffer so that it will be recreated */