[cairo-commit] test/Makefile.am test/pthread-show-text.c test/pthread-show-text.image16.ref.png test/pthread-show-text.ps.ref.png test/pthread-show-text.ref.png

Chris Wilson ickle at kemper.freedesktop.org
Fri Apr 30 05:01:43 PDT 2010


 test/Makefile.am                       |    3 
 test/pthread-show-text.c               |  118 ++++++++++++++++++++-------------
 test/pthread-show-text.image16.ref.png |binary
 test/pthread-show-text.ps.ref.png      |binary
 test/pthread-show-text.ref.png         |binary
 5 files changed, 77 insertions(+), 44 deletions(-)

New commits:
commit 4ee5119a3467ea086efccef2907b0c576d600b79
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Fri Apr 30 13:00:05 2010 +0100

    test: Expand pthread-show-text to cover all surfaces.
    
    In a similar fashion to pthread-same-source and pthread-similar, check
    that the texting handling is thread-safe for all surface and font
    backends.

diff --git a/test/Makefile.am b/test/Makefile.am
index 0d2283c..8607a54 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -877,6 +877,9 @@ REFERENCE_IMAGES = \
 	pthread-same-source.image16.ref.png \
 	pthread-same-source.ref.png \
 	pthread-similar.ref.png \
+	pthread-show-text.ref.png \
+	pthread-show-text.image16.ref.png \
+	pthread-show-text.ps.ref.png \
 	ps-surface-source.image16.ref.png \
 	ps-surface-source.rgb24.ref.png \
 	ps-surface-source.argb32.ref.png \
diff --git a/test/pthread-show-text.c b/test/pthread-show-text.c
index d95ce20..2d22076 100644
--- a/test/pthread-show-text.c
+++ b/test/pthread-show-text.c
@@ -35,79 +35,109 @@
 #include <stdlib.h>
 #include <pthread.h>
 
-#define NUM_THREADS_DEFAULT 50
-#define NUM_ITERATIONS 50
+#define N_THREADS 8
+#define NUM_ITERATIONS 40
+
+#define WIDTH 400
+#define HEIGHT 42
+
+typedef struct {
+  cairo_surface_t *target;
+  int id;
+} thread_data_t;
 
 static void *
-start (void *closure)
+draw_thread (void *arg)
 {
+    const char *text = "Hello world. ";
+    thread_data_t *thread_data = arg;
     cairo_surface_t *surface;
+    cairo_font_extents_t extents;
     cairo_t *cr;
     int i;
 
-    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
-    cr = cairo_create (surface);
+    cr = cairo_create (thread_data->target);
+    cairo_surface_destroy (thread_data->target);
 
-    cairo_save (cr);
-    {
-	cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
-	cairo_paint (cr);
-    }
-    cairo_restore (cr);
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    cairo_paint (cr);
+    cairo_set_source_rgb (cr, 0, 0, 0);
+
+    cairo_select_font_face (cr, "serif",
+			    CAIRO_FONT_SLANT_NORMAL,
+			    CAIRO_FONT_WEIGHT_NORMAL);
+    cairo_set_font_size (cr, NUM_ITERATIONS);
+    cairo_font_extents (cr, &extents);
 
-    cairo_move_to (cr, 1, 1);
+    cairo_move_to (cr, 1, HEIGHT - extents.descent - 1);
+
+    for (i = 0; i < NUM_ITERATIONS; i++) {
+	char buf[2];
 
-    for (i=0; i < NUM_ITERATIONS; i++) {
         cairo_select_font_face (cr, "serif",
 				CAIRO_FONT_SLANT_NORMAL,
 				CAIRO_FONT_WEIGHT_NORMAL);
-	cairo_set_font_size (cr, 8 + i);
-	cairo_show_text (cr, "Hello world.\n");
+	cairo_set_font_size (cr, i);
+
+	buf[0] = text[i%strlen(text)];
+	buf[1] = '\0';
+	cairo_show_text (cr, buf);
     }
 
+    surface = cairo_surface_reference (cairo_get_target (cr));
     cairo_destroy (cr);
-    cairo_surface_destroy (surface);
 
-    return NULL;
+    return surface;
 }
 
 static cairo_test_status_t
-preamble (cairo_test_context_t *ctx)
+draw (cairo_t *cr, int width, int height)
 {
-    pthread_t *pthread;
-    int i, num_threads;
-    cairo_test_status_t status = CAIRO_TEST_SUCCESS;
-
-    num_threads = 0;
-    if (getenv ("CAIRO_TEST_NUM_THREADS"))
-	num_threads = atoi (getenv ("CAIRO_TEST_NUM_THREADS"));
-    if (num_threads == 0)
-	num_threads = NUM_THREADS_DEFAULT;
-    cairo_test_log (ctx, "Running with %d threads.\n", num_threads);
-
-    pthread = xmalloc (num_threads * sizeof (pthread_t));
-
-    for (i = 0; i < num_threads; i++) {
-	int err = pthread_create (&pthread[i], NULL, start, NULL);
-	if (err) {
-	    cairo_test_log (ctx, "pthread_create failed: %s\n", strerror(err));
-	    num_threads = i;
-	    status = CAIRO_TEST_FAILURE;
+    pthread_t threads[N_THREADS];
+    thread_data_t thread_data[N_THREADS];
+    cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;
+    int i;
+
+    for (i = 0; i < N_THREADS; i++) {
+        thread_data[i].target = cairo_surface_create_similar (cairo_get_target (cr),
+							      cairo_surface_get_content (cairo_get_target (cr)),
+							      WIDTH, HEIGHT);
+        thread_data[i].id = i;
+        if (pthread_create (&threads[i], NULL, draw_thread, &thread_data[i]) != 0) {
+	    threads[i] = pthread_self (); /* to indicate error */
+            cairo_surface_destroy (thread_data[i].target);
+            test_status = CAIRO_TEST_FAILURE;
 	    break;
-	}
+        }
     }
 
-    for (i = 0; i < num_threads; i++)
-	pthread_join (pthread[i], NULL);
+    cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
+    cairo_paint (cr);
+
+    for (i = 0; i < N_THREADS; i++) {
+	void *surface;
+
+        if (pthread_equal (threads[i], pthread_self ()))
+            break;
+
+        if (pthread_join (threads[i], &surface) != 0) {
+            test_status = CAIRO_TEST_FAILURE;
+	    break;
+	}
 
-    free (pthread);
+        cairo_set_source_surface (cr, surface, 0, 0);
+        cairo_surface_destroy (surface);
+        cairo_paint (cr);
+
+        cairo_translate (cr, 0, HEIGHT);
+    }
 
-    return status;
+    return test_status;
 }
 
 CAIRO_TEST (pthread_show_text,
 	    "Concurrent stress test of the cairo_show_text().",
 	    "thread, text", /* keywords */
 	    NULL, /* requirements */
-	    0, 0,
-	    preamble, NULL)
+	    WIDTH, HEIGHT * N_THREADS,
+	    NULL, draw)
diff --git a/test/pthread-show-text.image16.ref.png b/test/pthread-show-text.image16.ref.png
new file mode 100644
index 0000000..a1d1af5
Binary files /dev/null and b/test/pthread-show-text.image16.ref.png differ
diff --git a/test/pthread-show-text.ps.ref.png b/test/pthread-show-text.ps.ref.png
new file mode 100644
index 0000000..807b73f
Binary files /dev/null and b/test/pthread-show-text.ps.ref.png differ
diff --git a/test/pthread-show-text.ref.png b/test/pthread-show-text.ref.png
new file mode 100644
index 0000000..96b5971
Binary files /dev/null and b/test/pthread-show-text.ref.png differ


More information about the cairo-commit mailing list