1
/*
2
 * Copyright © 2007 Red Hat, Inc.
3
 * Copyright © 2011 Intel Corporation
4
 *
5
 * Permission is hereby granted, free of charge, to any person
6
 * obtaining a copy of this software and associated documentation
7
 * files (the "Software"), to deal in the Software without
8
 * restriction, including without limitation the rights to use, copy,
9
 * modify, merge, publish, distribute, sublicense, and/or sell copies
10
 * of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 *
25
 * Authors:
26
 *	Behdad Esfahbod <behdad@behdad.org>
27
 *	Chris Wilson <chris@chris-wilson.co.uk>
28
 */
29

            
30
#include "cairo-test.h"
31
#include <math.h>
32
#include <stdio.h>
33

            
34
#define SIZE 90
35

            
36
/* This is written using clip+paint to exercise a bug that once was in the
37
 * recording surface.
38
 */
39

            
40
static cairo_surface_t *
41
24
source (cairo_surface_t *surface)
42
{
43
    cairo_t *cr;
44

            
45
    /* Create a 4-pixel image surface with my favorite four colors in each
46
     * quadrant. */
47
24
    cr = cairo_create (surface);
48
24
    cairo_surface_destroy (surface);
49

            
50
    /* upper-left = white */
51
24
    cairo_set_source_rgb (cr, 1, 1, 1);
52
24
    cairo_rectangle (cr, 0, 0, 1, 1);
53
24
    cairo_fill (cr);
54

            
55
    /* upper-right = red */
56
24
    cairo_set_source_rgb (cr, 1, 0, 0);
57
24
    cairo_rectangle (cr, 1, 0, 1, 1);
58
24
    cairo_fill (cr);
59

            
60
    /* lower-left = green */
61
24
    cairo_set_source_rgb (cr, 0, 1, 0);
62
24
    cairo_rectangle (cr, 0, 1, 1, 1);
63
24
    cairo_fill (cr);
64

            
65
    /* lower-right = blue */
66
24
    cairo_set_source_rgb (cr, 0, 0, 1);
67
24
    cairo_rectangle (cr, 1, 1, 1, 1);
68
24
    cairo_fill (cr);
69

            
70
24
    surface = cairo_surface_reference (cairo_get_target (cr));
71
24
    cairo_destroy (cr);
72

            
73
24
    return surface;
74
}
75

            
76
static cairo_surface_t *
77
12
image (cairo_t *cr)
78
{
79
12
    return source (cairo_image_surface_create (CAIRO_FORMAT_RGB24, 2, 2));
80
}
81

            
82
static cairo_surface_t *
83
12
similar (cairo_t *cr)
84
{
85
12
    return source (cairo_surface_create_similar (cairo_get_target (cr),
86
						 CAIRO_CONTENT_COLOR, 2, 2));
87
}
88

            
89
static cairo_t *
90
24
extend (cairo_t *cr, cairo_surface_t *(*surface)(cairo_t *), cairo_extend_t mode)
91
{
92
    cairo_surface_t *s;
93

            
94
24
    cairo_set_source_rgb (cr, 0, 1, 1);
95
24
    cairo_paint (cr);
96

            
97
    /* Now use extend modes to cover most of the surface with those 4 colors */
98
24
    s = surface (cr);
99
24
    cairo_set_source_surface (cr, s, SIZE/2 - 1, SIZE/2 - 1);
100
24
    cairo_surface_destroy (s);
101

            
102
24
    cairo_pattern_set_extend (cairo_get_source (cr), mode);
103

            
104
24
    cairo_rectangle (cr, 10, 10, SIZE-20, SIZE-20);
105
24
    cairo_clip (cr);
106
24
    cairo_paint (cr);
107

            
108
24
    return cr;
109
}
110

            
111
static cairo_t *
112
6
extend_none (cairo_t *cr,
113
	     cairo_surface_t *(*pattern)(cairo_t *))
114
{
115
6
    return extend (cr, pattern, CAIRO_EXTEND_NONE);
116
}
117

            
118
static cairo_t *
119
6
extend_pad (cairo_t *cr,
120
	    cairo_surface_t *(*pattern)(cairo_t *))
121
{
122
6
    return extend (cr, pattern, CAIRO_EXTEND_PAD);
123
}
124

            
125
static cairo_t *
126
6
extend_repeat (cairo_t *cr,
127
	       cairo_surface_t *(*pattern)(cairo_t *))
128
{
129
6
    return extend (cr, pattern, CAIRO_EXTEND_REPEAT);
130
}
131

            
132
static cairo_t *
133
6
extend_reflect (cairo_t *cr,
134
	       cairo_surface_t *(*pattern)(cairo_t *))
135
{
136
6
    return extend (cr, pattern, CAIRO_EXTEND_REFLECT);
137
}
138

            
139
static cairo_t *
140
24
record_create (cairo_t *target)
141
{
142
    cairo_surface_t *surface;
143
    cairo_t *cr;
144

            
145
24
    surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
146
24
    cr = cairo_create (surface);
147
24
    cairo_surface_destroy (surface);
148

            
149
24
    return cr;
150
}
151

            
152
static cairo_surface_t *
153
24
record_get (cairo_t *target)
154
{
155
    cairo_surface_t *surface;
156

            
157
24
    surface = cairo_surface_reference (cairo_get_target (target));
158
24
    cairo_destroy (target);
159

            
160
24
    return surface;
161
}
162

            
163
static cairo_test_status_t
164
24
record_replay (cairo_t *cr,
165
	       cairo_t *(*func)(cairo_t *,
166
				cairo_surface_t *(*pattern)(cairo_t *)),
167
	       cairo_surface_t *(*pattern)(cairo_t *),
168
	       int width, int height)
169
{
170
    cairo_surface_t *surface;
171
    int x, y;
172

            
173
24
    surface = record_get (func (record_create (cr), pattern));
174

            
175
24
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
176
24
    cairo_set_source_surface (cr, surface, 0, 0);
177
24
    cairo_surface_destroy (surface);
178
24
    cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
179

            
180
1104
    for (y = 0; y < height; y += 2) {
181
49680
	for (x = 0; x < width; x += 2) {
182
48600
	    cairo_rectangle (cr, x, y, 2, 2);
183
48600
	    cairo_clip (cr);
184
48600
	    cairo_paint (cr);
185
48600
	    cairo_reset_clip (cr);
186
	}
187
    }
188

            
189
24
    return CAIRO_TEST_SUCCESS;
190
}
191

            
192
static cairo_test_status_t
193
3
record_extend_none (cairo_t *cr, int width, int height)
194
{
195
3
    return record_replay (cr, extend_none, image, width, height);
196
}
197

            
198
static cairo_test_status_t
199
3
record_extend_pad (cairo_t *cr, int width, int height)
200
{
201
3
    return record_replay (cr, extend_pad, image, width, height);
202
}
203

            
204
static cairo_test_status_t
205
3
record_extend_repeat (cairo_t *cr, int width, int height)
206
{
207
3
    return record_replay (cr, extend_repeat, image, width, height);
208
}
209

            
210
static cairo_test_status_t
211
3
record_extend_reflect (cairo_t *cr, int width, int height)
212
{
213
3
    return record_replay (cr, extend_reflect, image, width, height);
214
}
215

            
216
static cairo_test_status_t
217
3
record_extend_none_similar (cairo_t *cr, int width, int height)
218
{
219
3
    return record_replay (cr, extend_none, similar, width, height);
220
}
221

            
222
static cairo_test_status_t
223
3
record_extend_pad_similar (cairo_t *cr, int width, int height)
224
{
225
3
    return record_replay (cr, extend_pad, similar, width, height);
226
}
227

            
228
static cairo_test_status_t
229
3
record_extend_repeat_similar (cairo_t *cr, int width, int height)
230
{
231
3
    return record_replay (cr, extend_repeat, similar, width, height);
232
}
233

            
234
static cairo_test_status_t
235
3
record_extend_reflect_similar (cairo_t *cr, int width, int height)
236
{
237
3
    return record_replay (cr, extend_reflect, similar, width, height);
238
}
239

            
240
1
CAIRO_TEST (record_extend_none,
241
	    "Test CAIRO_EXTEND_NONE for recorded surface patterns",
242
	    "record, extend", /* keywords */
243
	    NULL, /* requirements */
244
	    SIZE, SIZE,
245
	    NULL, record_extend_none)
246
1
CAIRO_TEST (record_extend_pad,
247
	    "Test CAIRO_EXTEND_PAD for recorded surface patterns",
248
	    "record, extend", /* keywords */
249
	    NULL, /* requirements */
250
	    SIZE, SIZE,
251
	    NULL, record_extend_pad)
252
1
CAIRO_TEST (record_extend_repeat,
253
	    "Test CAIRO_EXTEND_REPEAT for recorded surface patterns",
254
	    "record, extend", /* keywords */
255
	    NULL, /* requirements */
256
	    SIZE, SIZE,
257
	    NULL, record_extend_repeat)
258
1
CAIRO_TEST (record_extend_reflect,
259
	    "Test CAIRO_EXTEND_REFLECT for recorded surface patterns",
260
	    "record, extend", /* keywords */
261
	    NULL, /* requirements */
262
	    SIZE, SIZE,
263
	    NULL, record_extend_reflect)
264

            
265
1
CAIRO_TEST (record_extend_none_similar,
266
	    "Test CAIRO_EXTEND_NONE for recorded surface patterns",
267
	    "record, extend", /* keywords */
268
	    NULL, /* requirements */
269
	    SIZE, SIZE,
270
	    NULL, record_extend_none_similar)
271
1
CAIRO_TEST (record_extend_pad_similar,
272
	    "Test CAIRO_EXTEND_PAD for recorded surface patterns",
273
	    "record, extend", /* keywords */
274
	    NULL, /* requirements */
275
	    SIZE, SIZE,
276
	    NULL, record_extend_pad_similar)
277
1
CAIRO_TEST (record_extend_repeat_similar,
278
	    "Test CAIRO_EXTEND_REPEAT for recorded surface patterns",
279
	    "record, extend", /* keywords */
280
	    NULL, /* requirements */
281
	    SIZE, SIZE,
282
	    NULL, record_extend_repeat_similar)
283
1
CAIRO_TEST (record_extend_reflect_similar,
284
	    "Test CAIRO_EXTEND_REFLECT for recorded surface patterns",
285
	    "record, extend", /* keywords */
286
	    NULL, /* requirements */
287
	    SIZE, SIZE,
288
	    NULL, record_extend_reflect_similar)