Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / pthread-same-source.c
1 /*
2  * Copyright 2009 Benjamin Otte
3  *
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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
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
22  * SOFTWARE.
23  *
24  * Author: Benjamin Otte <otte@gnome.org>
25  */
26
27 #include "cairo-test.h"
28 #include <pthread.h>
29
30 #define N_THREADS 8
31
32 #define WIDTH 64
33 #define HEIGHT 8
34
35 typedef struct {
36   cairo_surface_t *target;
37   cairo_surface_t *source;
38   int id;
39 } thread_data_t;
40
41 static void *
42 draw_thread (void *arg)
43 {
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 };
48     cairo_t *cr;
49     int x, y;
50
51     cr = cairo_create (thread_data->target);
52     cairo_surface_destroy (thread_data->target);
53
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);
59
60     for (y = 0; y < HEIGHT; y++) {
61         for (x = 0; x < WIDTH; x++) {
62             cairo_save (cr);
63             cairo_translate (cr, 4 * x + 1, 4 * y + 1);
64             cairo_rectangle (cr, 0, 0, 2, 2);
65             cairo_set_source (cr, pattern);
66             cairo_fill (cr);
67             cairo_restore (cr);
68         }
69     }
70     cairo_pattern_destroy (pattern);
71
72     surface = cairo_surface_reference (cairo_get_target (cr));
73     cairo_destroy (cr);
74
75     return surface;
76 }
77
78 static cairo_surface_t *
79 create_source (cairo_surface_t *similar)
80 {
81     cairo_surface_t *source;
82     cairo_t *cr;
83     double colors[4][3] = {
84       { 0.75, 0,    0    },
85       { 0,    0.75, 0    },
86       { 0,    0,    0.75 },
87       { 0.75, 0.75, 0    }
88     };
89     int i;
90
91     source = cairo_surface_create_similar (similar,
92                                            CAIRO_CONTENT_COLOR_ALPHA,
93                                            2, 2);
94
95     cr = cairo_create (source);
96     cairo_surface_destroy (source);
97
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);
101       cairo_fill (cr);
102     }
103
104     source = cairo_surface_reference (cairo_get_target (cr));
105     cairo_destroy (cr);
106
107     return source;
108 }
109
110 static cairo_test_status_t
111 draw (cairo_t *cr, int width, int height)
112 {
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;
118     int i;
119
120     source = create_source (cairo_get_target (cr));
121     status = cairo_surface_status (source);
122     if (status) {
123         cairo_surface_destroy (source);
124         return cairo_test_status_from_status (cairo_test_get_context (cr),
125                                               status);
126     }
127
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;
139             break;
140         }
141     }
142
143     cairo_surface_destroy (source);
144
145     cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
146     cairo_paint (cr);
147
148     for (i = 0; i < N_THREADS; i++) {
149         void *surface;
150
151         if (pthread_equal (threads[i], pthread_self ()))
152             break;
153
154         if (pthread_join (threads[i], &surface) == 0) {
155             cairo_set_source_surface (cr, surface, 0, 0);
156             cairo_surface_destroy (surface);
157             cairo_paint (cr);
158
159             cairo_translate (cr, 0, 4 * HEIGHT);
160         } else {
161             test_status = CAIRO_TEST_FAILURE;
162         }
163     }
164
165     return test_status;
166 }
167
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,
173             NULL, draw)