2 * Copyright 2009 Benjamin Otte
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Author: Benjamin Otte <otte@gnome.org>
27 #include "cairo-test.h"
36 cairo_surface_t *target;
37 cairo_surface_t *source;
42 draw_thread (void *arg)
44 thread_data_t *thread_data = arg;
45 cairo_surface_t *surface;
46 cairo_pattern_t *pattern;
47 cairo_matrix_t pattern_matrix = { 2, 0, 0, 2, 0, 0 };
51 cr = cairo_create (thread_data->target);
52 cairo_surface_destroy (thread_data->target);
54 pattern = cairo_pattern_create_for_surface (thread_data->source);
55 cairo_surface_destroy (thread_data->source);
56 cairo_pattern_set_extend (pattern, thread_data->id % 4);
57 cairo_pattern_set_filter (pattern, thread_data->id >= 4 ? CAIRO_FILTER_BILINEAR : CAIRO_FILTER_NEAREST);
58 cairo_pattern_set_matrix (pattern, &pattern_matrix);
60 for (y = 0; y < HEIGHT; y++) {
61 for (x = 0; x < WIDTH; x++) {
63 cairo_translate (cr, 4 * x + 1, 4 * y + 1);
64 cairo_rectangle (cr, 0, 0, 2, 2);
65 cairo_set_source (cr, pattern);
70 cairo_pattern_destroy (pattern);
72 surface = cairo_surface_reference (cairo_get_target (cr));
78 static cairo_surface_t *
79 create_source (cairo_surface_t *similar)
81 cairo_surface_t *source;
83 double colors[4][3] = {
91 source = cairo_surface_create_similar (similar,
92 CAIRO_CONTENT_COLOR_ALPHA,
95 cr = cairo_create (source);
96 cairo_surface_destroy (source);
98 for (i = 0; i < 4; i++) {
99 cairo_set_source_rgb (cr, colors[i][0], colors[i][1], colors[i][2]);
100 cairo_rectangle (cr, i % 2, i / 2, 1, 1);
104 source = cairo_surface_reference (cairo_get_target (cr));
110 static cairo_test_status_t
111 draw (cairo_t *cr, int width, int height)
113 pthread_t threads[N_THREADS];
114 thread_data_t thread_data[N_THREADS];
115 cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;
116 cairo_surface_t *source;
117 cairo_status_t status;
120 source = create_source (cairo_get_target (cr));
121 status = cairo_surface_status (source);
123 cairo_surface_destroy (source);
124 return cairo_test_status_from_status (cairo_test_get_context (cr),
128 for (i = 0; i < N_THREADS; i++) {
129 thread_data[i].target = cairo_surface_create_similar (cairo_get_target (cr),
130 CAIRO_CONTENT_COLOR_ALPHA,
131 4 * WIDTH, 4 * HEIGHT);
132 thread_data[i].source = cairo_surface_reference (source);
133 thread_data[i].id = i;
134 if (pthread_create (&threads[i], NULL, draw_thread, &thread_data[i]) != 0) {
135 threads[i] = pthread_self (); /* to indicate error */
136 cairo_surface_destroy (thread_data[i].target);
137 cairo_surface_destroy (thread_data[i].source);
138 test_status = CAIRO_TEST_FAILURE;
143 cairo_surface_destroy (source);
145 cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
148 for (i = 0; i < N_THREADS; i++) {
151 if (pthread_equal (threads[i], pthread_self ()))
154 if (pthread_join (threads[i], &surface) == 0) {
155 cairo_set_source_surface (cr, surface, 0, 0);
156 cairo_surface_destroy (surface);
159 cairo_translate (cr, 0, 4 * HEIGHT);
161 test_status = CAIRO_TEST_FAILURE;
168 CAIRO_TEST (pthread_same_source,
169 "Use the same source for drawing in different threads",
170 "threads", /* keywords */
171 NULL, /* requirements */
172 4 * WIDTH, 4 * HEIGHT * N_THREADS,