2 * Copyright © 2007 Red Hat, Inc.
3 * Copyright © 2011 Intel Corporation
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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
26 * Behdad Esfahbod <behdad@behdad.org>
27 * Chris Wilson <chris@chris-wilson.co.uk>
31 #include "cairo-test.h"
36 /* This is written using clip+paint to exercise a bug that once was in the
40 static cairo_surface_t *
41 source (cairo_surface_t *surface)
45 /* Create a 4-pixel image surface with my favorite four colors in each
47 cr = cairo_create (surface);
48 cairo_surface_destroy (surface);
50 /* upper-left = white */
51 cairo_set_source_rgb (cr, 1, 1, 1);
52 cairo_rectangle (cr, 0, 0, 1, 1);
55 /* upper-right = red */
56 cairo_set_source_rgb (cr, 1, 0, 0);
57 cairo_rectangle (cr, 1, 0, 1, 1);
60 /* lower-left = green */
61 cairo_set_source_rgb (cr, 0, 1, 0);
62 cairo_rectangle (cr, 0, 1, 1, 1);
65 /* lower-right = blue */
66 cairo_set_source_rgb (cr, 0, 0, 1);
67 cairo_rectangle (cr, 1, 1, 1, 1);
70 surface = cairo_surface_reference (cairo_get_target (cr));
76 static cairo_surface_t *
79 return source (cairo_image_surface_create (CAIRO_FORMAT_RGB24, 2, 2));
82 static cairo_surface_t *
85 return source (cairo_surface_create_similar (cairo_get_target (cr),
86 CAIRO_CONTENT_COLOR, 2, 2));
90 extend (cairo_t *cr, cairo_surface_t *(*surface)(cairo_t *), cairo_extend_t mode)
94 cairo_set_source_rgb (cr, 0, 1, 1);
97 /* Now use extend modes to cover most of the surface with those 4 colors */
99 cairo_set_source_surface (cr, s, SIZE/2 - 1, SIZE/2 - 1);
100 cairo_surface_destroy (s);
102 cairo_pattern_set_extend (cairo_get_source (cr), mode);
104 cairo_rectangle (cr, 10, 10, SIZE-20, SIZE-20);
112 extend_none (cairo_t *cr,
113 cairo_surface_t *(*pattern)(cairo_t *))
115 return extend (cr, pattern, CAIRO_EXTEND_NONE);
119 extend_pad (cairo_t *cr,
120 cairo_surface_t *(*pattern)(cairo_t *))
122 return extend (cr, pattern, CAIRO_EXTEND_PAD);
126 extend_repeat (cairo_t *cr,
127 cairo_surface_t *(*pattern)(cairo_t *))
129 return extend (cr, pattern, CAIRO_EXTEND_REPEAT);
133 extend_reflect (cairo_t *cr,
134 cairo_surface_t *(*pattern)(cairo_t *))
136 return extend (cr, pattern, CAIRO_EXTEND_REFLECT);
140 record_create (cairo_t *target)
142 cairo_surface_t *surface;
145 surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
146 cr = cairo_create (surface);
147 cairo_surface_destroy (surface);
152 static cairo_surface_t *
153 record_get (cairo_t *target)
155 cairo_surface_t *surface;
157 surface = cairo_surface_reference (cairo_get_target (target));
158 cairo_destroy (target);
163 static cairo_test_status_t
164 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)
170 cairo_surface_t *surface;
173 surface = record_get (func (record_create (cr), pattern));
175 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
176 cairo_set_source_surface (cr, surface, 0, 0);
177 cairo_surface_destroy (surface);
178 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
180 for (y = 0; y < height; y += 2) {
181 for (x = 0; x < width; x += 2) {
182 cairo_rectangle (cr, x, y, 2, 2);
185 cairo_reset_clip (cr);
189 return CAIRO_TEST_SUCCESS;
192 static cairo_test_status_t
193 record_extend_none (cairo_t *cr, int width, int height)
195 return record_replay (cr, extend_none, image, width, height);
198 static cairo_test_status_t
199 record_extend_pad (cairo_t *cr, int width, int height)
201 return record_replay (cr, extend_pad, image, width, height);
204 static cairo_test_status_t
205 record_extend_repeat (cairo_t *cr, int width, int height)
207 return record_replay (cr, extend_repeat, image, width, height);
210 static cairo_test_status_t
211 record_extend_reflect (cairo_t *cr, int width, int height)
213 return record_replay (cr, extend_reflect, image, width, height);
216 static cairo_test_status_t
217 record_extend_none_similar (cairo_t *cr, int width, int height)
219 return record_replay (cr, extend_none, similar, width, height);
222 static cairo_test_status_t
223 record_extend_pad_similar (cairo_t *cr, int width, int height)
225 return record_replay (cr, extend_pad, similar, width, height);
228 static cairo_test_status_t
229 record_extend_repeat_similar (cairo_t *cr, int width, int height)
231 return record_replay (cr, extend_repeat, similar, width, height);
234 static cairo_test_status_t
235 record_extend_reflect_similar (cairo_t *cr, int width, int height)
237 return record_replay (cr, extend_reflect, similar, width, height);
240 CAIRO_TEST (record_extend_none,
241 "Test CAIRO_EXTEND_NONE for recorded surface patterns",
242 "record, extend", /* keywords */
243 NULL, /* requirements */
245 NULL, record_extend_none)
246 CAIRO_TEST (record_extend_pad,
247 "Test CAIRO_EXTEND_PAD for recorded surface patterns",
248 "record, extend", /* keywords */
249 NULL, /* requirements */
251 NULL, record_extend_pad)
252 CAIRO_TEST (record_extend_repeat,
253 "Test CAIRO_EXTEND_REPEAT for recorded surface patterns",
254 "record, extend", /* keywords */
255 NULL, /* requirements */
257 NULL, record_extend_repeat)
258 CAIRO_TEST (record_extend_reflect,
259 "Test CAIRO_EXTEND_REFLECT for recorded surface patterns",
260 "record, extend", /* keywords */
261 NULL, /* requirements */
263 NULL, record_extend_reflect)
265 CAIRO_TEST (record_extend_none_similar,
266 "Test CAIRO_EXTEND_NONE for recorded surface patterns",
267 "record, extend", /* keywords */
268 NULL, /* requirements */
270 NULL, record_extend_none_similar)
271 CAIRO_TEST (record_extend_pad_similar,
272 "Test CAIRO_EXTEND_PAD for recorded surface patterns",
273 "record, extend", /* keywords */
274 NULL, /* requirements */
276 NULL, record_extend_pad_similar)
277 CAIRO_TEST (record_extend_repeat_similar,
278 "Test CAIRO_EXTEND_REPEAT for recorded surface patterns",
279 "record, extend", /* keywords */
280 NULL, /* requirements */
282 NULL, record_extend_repeat_similar)
283 CAIRO_TEST (record_extend_reflect_similar,
284 "Test CAIRO_EXTEND_REFLECT for recorded surface patterns",
285 "record, extend", /* keywords */
286 NULL, /* requirements */
288 NULL, record_extend_reflect_similar)