diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2010-04-30 13:00:05 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2010-04-30 13:00:05 +0100 |
commit | 4ee5119a3467ea086efccef2907b0c576d600b79 (patch) | |
tree | b78b50520933e4c8e24ea549a2c81b459e1bde77 /test/pthread-show-text.c | |
parent | c6dc8ad7dc46d03899cd37fff40bd7f3a60339e2 (diff) |
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.
Diffstat (limited to 'test/pthread-show-text.c')
-rw-r--r-- | test/pthread-show-text.c | 118 |
1 files changed, 74 insertions, 44 deletions
diff --git a/test/pthread-show-text.c b/test/pthread-show-text.c index d95ce20c..2d220762 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) |