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
#define GENERATE_REFERENCE 0
28

            
29
#include "cairo-test.h"
30
#if !GENERATE_REFERENCE
31
#include <pthread.h>
32
#endif
33

            
34
#define N_THREADS 8
35

            
36
#define WIDTH 64
37
#define HEIGHT 8
38

            
39
typedef struct {
40
  cairo_surface_t *target;
41
  cairo_surface_t *source;
42
  int id;
43
} thread_data_t;
44

            
45
static void *
46
24
draw_thread (void *arg)
47
{
48
24
    thread_data_t *thread_data = arg;
49
    cairo_surface_t *surface;
50
    cairo_pattern_t *pattern;
51
24
    cairo_matrix_t pattern_matrix = { 2, 0, 0, 2, 0, 0 };
52
    cairo_t *cr;
53
    int x, y;
54

            
55
24
    cr = cairo_create (thread_data->target);
56
24
    cairo_surface_destroy (thread_data->target);
57

            
58
24
    pattern = cairo_pattern_create_for_surface (thread_data->source);
59
24
    cairo_surface_destroy (thread_data->source);
60
24
    cairo_pattern_set_extend (pattern, thread_data->id % 4);
61
24
    cairo_pattern_set_filter (pattern, thread_data->id >= 4 ? CAIRO_FILTER_BILINEAR : CAIRO_FILTER_NEAREST);
62
24
    cairo_pattern_set_matrix (pattern, &pattern_matrix);
63

            
64
216
    for (y = 0; y < HEIGHT; y++) {
65
12480
        for (x = 0; x < WIDTH; x++) {
66
12288
            cairo_save (cr);
67
12288
            cairo_translate (cr, 4 * x + 1, 4 * y + 1);
68
12288
            cairo_rectangle (cr, 0, 0, 2, 2);
69
12288
            cairo_set_source (cr, pattern);
70
12288
            cairo_fill (cr);
71
12288
            cairo_restore (cr);
72
        }
73
    }
74
24
    cairo_pattern_destroy (pattern);
75

            
76
24
    surface = cairo_surface_reference (cairo_get_target (cr));
77
24
    cairo_destroy (cr);
78

            
79
24
    return surface;
80
}
81

            
82
static cairo_surface_t *
83
3
create_source (cairo_surface_t *similar)
84
{
85
    cairo_surface_t *source;
86
    cairo_t *cr;
87
3
    double colors[4][3] = {
88
      { 0.75, 0,    0    },
89
      { 0,    0.75, 0    },
90
      { 0,    0,    0.75 },
91
      { 0.75, 0.75, 0    }
92
    };
93
    int i;
94

            
95
3
    source = cairo_surface_create_similar (similar,
96
                                           CAIRO_CONTENT_COLOR_ALPHA,
97
                                           2, 2);
98

            
99
3
    cr = cairo_create (source);
100
3
    cairo_surface_destroy (source);
101

            
102
15
    for (i = 0; i < 4; i++) {
103
12
      cairo_set_source_rgb (cr, colors[i][0], colors[i][1], colors[i][2]);
104
12
      cairo_rectangle (cr, i % 2, i / 2, 1, 1);
105
12
      cairo_fill (cr);
106
    }
107

            
108
3
    source = cairo_surface_reference (cairo_get_target (cr));
109
3
    cairo_destroy (cr);
110

            
111
3
    return source;
112
}
113

            
114
static cairo_test_status_t
115
3
draw (cairo_t *cr, int width, int height)
116
{
117
#if !GENERATE_REFERENCE
118
    pthread_t threads[N_THREADS];
119
#endif
120
    thread_data_t thread_data[N_THREADS];
121
3
    cairo_test_status_t test_status = CAIRO_TEST_SUCCESS;
122
    cairo_surface_t *source;
123
    cairo_status_t status;
124
    int i;
125

            
126
3
    source = create_source (cairo_get_target (cr));
127
3
    status = cairo_surface_status (source);
128
3
    if (status) {
129
	cairo_surface_destroy (source);
130
	return cairo_test_status_from_status (cairo_test_get_context (cr),
131
					      status);
132
    }
133

            
134
3
    cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
135
3
    cairo_paint (cr);
136

            
137
27
    for (i = 0; i < N_THREADS; i++) {
138
24
        thread_data[i].target = cairo_surface_create_similar (cairo_get_target (cr),
139
                                                              CAIRO_CONTENT_COLOR_ALPHA,
140
                                                              4 * WIDTH, 4 * HEIGHT);
141
24
        thread_data[i].source = cairo_surface_reference (source);
142
24
        thread_data[i].id = i;
143
#if !GENERATE_REFERENCE
144
24
        if (pthread_create (&threads[i], NULL, draw_thread, &thread_data[i]) != 0) {
145
	    threads[i] = pthread_self (); /* to indicate error */
146
            cairo_surface_destroy (thread_data[i].target);
147
            cairo_surface_destroy (thread_data[i].source);
148
            test_status = CAIRO_TEST_FAILURE;
149
	    break;
150
        }
151
#else
152
	{
153
	    cairo_surface_t *surface = draw_thread(&thread_data[i]);
154
	    cairo_set_source_surface (cr, surface, 0, 0);
155
	    cairo_surface_destroy (surface);
156
	    cairo_paint (cr);
157

            
158
	    cairo_translate (cr, 0, 4 * HEIGHT);
159
	}
160
#endif
161
    }
162

            
163
3
    cairo_surface_destroy (source);
164

            
165
#if !GENERATE_REFERENCE
166
27
    for (i = 0; i < N_THREADS; i++) {
167
	void *surface;
168

            
169
24
        if (pthread_equal (threads[i], pthread_self ()))
170
            break;
171

            
172
24
        if (pthread_join (threads[i], &surface) == 0) {
173
24
	    cairo_set_source_surface (cr, surface, 0, 0);
174
24
	    cairo_surface_destroy (surface);
175
24
	    cairo_paint (cr);
176

            
177
24
	    cairo_translate (cr, 0, 4 * HEIGHT);
178
	} else {
179
            test_status = CAIRO_TEST_FAILURE;
180
	}
181
    }
182
#endif
183

            
184
3
    return test_status;
185
}
186

            
187
1
CAIRO_TEST (pthread_same_source,
188
	    "Use the same source for drawing in different threads",
189
	    "threads", /* keywords */
190
	    NULL, /* requirements */
191
	    4 * WIDTH, 4 * HEIGHT * N_THREADS,
192
	    NULL, draw)