[cairo] Cache solid patterns

Chris Wilson chris at chris-wilson.co.uk
Tue Mar 13 16:29:02 PDT 2007


This is the old solid colour surface cache moved up a level to cache
the allocated patterns. This is important for toolkits such as GTK+,
which tend to use a small colour palette across many widgets and trigger
frequent reallocation of the same solid patterns.
-------------- next part --------------
>From d03b6613880eb0ae0d7b4431b23ad48d8a146ae1 Mon Sep 17 00:00:00 2001
From: Chris Wilson <chris at chris-wilson.co.uk>
Date: Tue, 13 Mar 2007 22:51:12 +0000
Subject: [PATCH] Cache solid patterns

We use a small cache of size 16 for patterns created from solid colors,
e.g. cairo_set_source_rgb(). This helps with toolkits that draw many
widgets using the same colour scheme.

The cache uses a static index variable, which itself acts like a cache
of size 1, remembering the most recently used colour. So repeated
lookups for the same colour hit immediately. If that fails, the cache
is searched linearly, and if that fails too, a new pattern is created
and a random member of the cache is evicted.
---
 src/cairo-color.c   |    7 +++++
 src/cairo-debug.c   |    2 +
 src/cairo-pattern.c |   65 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/cairoint.h      |    7 +++++
 4 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/src/cairo-color.c b/src/cairo-color.c
index a348839..ec3799c 100644
--- a/src/cairo-color.c
+++ b/src/cairo-color.c
@@ -159,3 +159,10 @@ _cairo_color_get_rgba_premultiplied (cairo_color_t *color,
     *blue  = color->blue  * color->alpha;
     *alpha = color->alpha;
 }
+
+cairo_bool_t
+_cairo_color_equal (const cairo_color_t *color_a,
+	            const cairo_color_t *color_b)
+{
+    return memcmp (color_a, color_b, sizeof (cairo_color_t)) == 0;
+}
diff --git a/src/cairo-debug.c b/src/cairo-debug.c
index 7c29932..dd57dd7 100644
--- a/src/cairo-debug.c
+++ b/src/cairo-debug.c
@@ -68,4 +68,6 @@ cairo_debug_reset_static_data (void)
 #if CAIRO_HAS_FT_FONT
     _cairo_ft_font_reset_static_data ();
 #endif
+
+    _cairo_pattern_reset_static_data ();
 }
diff --git a/src/cairo-pattern.c b/src/cairo-pattern.c
index efa0c5a..6287378 100644
--- a/src/cairo-pattern.c
+++ b/src/cairo-pattern.c
@@ -252,18 +252,79 @@ _cairo_pattern_init_radial (cairo_radial_pattern_t *pattern,
     pattern->gradient.c2.radius = _cairo_fixed_from_double (fabs (radius1));
 }
 
+/* We use a small cache here, because we don't want to constantly
+ * reallocate simple colors. */
+#define MAX_PATTERN_CACHE_SIZE 16
+static struct {
+    struct {
+	cairo_color_t          color;
+	cairo_solid_pattern_t *pattern;
+    } cache[MAX_PATTERN_CACHE_SIZE];
+    int size;
+} solid_pattern_cache;
+
+CAIRO_MUTEX_DECLARE (solid_pattern_cache_lock);
+
 cairo_pattern_t *
 _cairo_pattern_create_solid (const cairo_color_t *color)
 {
-    cairo_solid_pattern_t *pattern;
+    static int cache_index;
+    void *pattern;
+
+    CAIRO_MUTEX_LOCK (solid_pattern_cache_lock);
+
+    /* Check cache first. */
+    if (cache_index < solid_pattern_cache.size &&
+	    _cairo_color_equal (
+		&solid_pattern_cache.cache[cache_index].color, color))
+	goto DONE;
 
+    for (cache_index = 0; cache_index < solid_pattern_cache.size; cache_index++)
+	if (_cairo_color_equal (
+		    &solid_pattern_cache.cache[cache_index].color, color))
+	    goto DONE;
+
+    /* Not cached, need to create a new pattern. */
     pattern = malloc (sizeof (cairo_solid_pattern_t));
     if (pattern == NULL)
 	return (cairo_pattern_t *) &cairo_pattern_nil.base;
 
     _cairo_pattern_init_solid (pattern, color);
 
-    return &pattern->base;
+    /* And insert it into the cache. */
+    if (solid_pattern_cache.size < MAX_PATTERN_CACHE_SIZE) {
+	solid_pattern_cache.size ++;
+	/* cache_index == solid_pattern_cache.size */
+    } else { 
+	/* Evict an old pattern. */
+	cache_index = rand () % MAX_PATTERN_CACHE_SIZE;
+	cairo_pattern_destroy (
+		&solid_pattern_cache.cache[cache_index].pattern->base);
+    }
+
+    solid_pattern_cache.cache[cache_index].color = *color;
+    solid_pattern_cache.cache[cache_index].pattern = pattern;
+
+DONE:
+    pattern = cairo_pattern_reference (
+	    &solid_pattern_cache.cache[cache_index].pattern->base);
+    CAIRO_MUTEX_UNLOCK (solid_pattern_cache_lock);
+
+    return pattern;
+}
+
+void
+_cairo_pattern_reset_static_data (void)
+{
+    int i;
+
+    CAIRO_MUTEX_LOCK (solid_pattern_cache_lock);
+
+    for (i = 0; i < solid_pattern_cache.size; i++)
+	cairo_pattern_destroy (&solid_pattern_cache.cache[i].pattern->base);
+    solid_pattern_cache.size = 0;
+
+    CAIRO_MUTEX_UNLOCK (solid_pattern_cache_lock);
 }
 
 static const cairo_pattern_t *
diff --git a/src/cairoint.h b/src/cairoint.h
index 3251950..6cb31b6 100755
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -1619,6 +1619,10 @@ _cairo_color_get_rgba_premultiplied (cairo_color_t *color,
 				     double	   *blue,
 				     double	   *alpha);
 
+cairo_private cairo_bool_t
+_cairo_color_equal (const cairo_color_t *color_a,
+                    const cairo_color_t *color_b);
+
 /* cairo-font.c */
 
 cairo_private void
@@ -2477,6 +2481,9 @@ cairo_private cairo_status_t
 _cairo_pattern_get_extents (cairo_pattern_t	    *pattern,
 			    cairo_rectangle_int16_t *extents);
 
+cairo_private void
+_cairo_pattern_reset_static_data (void);
+
 cairo_private cairo_status_t
 _cairo_gstate_set_antialias (cairo_gstate_t *gstate,
 			     cairo_antialias_t antialias);
-- 
1.4.4.2

-------------- next part --------------
>From 0b27d6dd2b76e3209265f2fe00d67bb43a510da7 Mon Sep 17 00:00:00 2001
From: Chris Wilson <chris at chris-wilson.co.uk>
Date: Tue, 13 Mar 2007 23:16:20 +0000
Subject: [PATCH] A very simple stress test for the pattern cache.

---
 test/Makefile.am                  |    1 +
 test/solid-pattern-cache-stress.c |   83 +++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/test/Makefile.am b/test/Makefile.am
index ea4787c..74de256 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -85,6 +85,7 @@ self-copy			\
 self-intersecting		\
 set-source			\
 show-text-current-point		\
+solid-pattern-cache-stress	\
 source-clip			\
 source-clip-scale		\
 source-surface-scale-paint	\
diff --git a/test/solid-pattern-cache-stress.c b/test/solid-pattern-cache-stress.c
new file mode 100644
index 0000000..855c8b7
--- /dev/null
+++ b/test/solid-pattern-cache-stress.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright ? 2007 Chris Wilson.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Chris Wilson. not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Chris Wilson makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Chris Wilson <chris at chris-wilson.co.uk>
+ */
+
+#include "cairo-test.h"
+
+static cairo_test_draw_function_t draw;
+
+cairo_test_t test = {
+    "solid-pattern-cache-stress",
+    "Stress the solid pattern cache and ensure it behaves",
+    0, 0,
+    draw
+};
+#include <cairo.h>
+#include <stdlib.h>
+
+#define LOOPS 10
+#define NRAND 100
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    int loop;
+    int i;
+
+    for (loop = 0; loop < LOOPS; loop++) {
+	cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
+	cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
+	cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); /* green */
+	cairo_set_source_rgb (cr, 1.0, 1.0, 0.0); /* yellow */
+	cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); /* blue */
+	cairo_set_source_rgb (cr, 1.0, 0.0, 1.0); /* magenta */
+	cairo_set_source_rgb (cr, 0.0, 1.0, 1.0); /* cyan */
+	cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
+
+	cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0); /* black */
+	cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 1.0); /* red */
+	cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, 1.0); /* green */
+	cairo_set_source_rgba (cr, 1.0, 1.0, 0.0, 1.0); /* yellow */
+	cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 1.0); /* blue */
+	cairo_set_source_rgba (cr, 1.0, 0.0, 1.0, 1.0); /* magenta */
+	cairo_set_source_rgba (cr, 0.0, 1.0, 1.0, 1.0); /* cyan */
+	cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0); /* white */
+
+	for (i = 0; i < NRAND; i++)
+	    cairo_set_source_rgba (cr,
+		                   drand48 (),
+				   drand48 (),
+				   drand48 (),
+				   drand48 ());
+    }
+
+    return CAIRO_TEST_SUCCESS;
+}
+
+int
+main (void)
+{
+    return cairo_test (&test);
+}
+
-- 
1.4.4.2



More information about the cairo mailing list