2 * Copyright 2010 Intel Corporation
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: Chris Wilson <chris@chris-wilson.co.uk>
27 #include "cairo-test.h"
29 /* Test the fidelity of the rasterisation, because Cairo is my favourite
33 #define GENERATE_REFERENCE 0
38 #include "../src/cairo-fixed-type-private.h"
39 #define PRECISION (1 << CAIRO_FIXED_FRAC_BITS)
41 /* XXX beware multithreading! */
42 static uint32_t state;
45 hars_petruska_f54_1_random (void)
47 #define rol(x,k) ((x << k) | (x >> (32-k)))
48 return state = (state ^ rol (state, 5) ^ rol (state, 24)) + 0x37798849;
53 random_offset (int range, int precise)
55 double x = hars_petruska_f54_1_random() / (double) UINT32_MAX * range / WIDTH;
57 x = floor (x * PRECISION) / PRECISION;
61 static cairo_test_status_t
62 rectangles (cairo_t *cr, int width, int height)
68 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
71 #if GENERATE_REFERENCE
72 for (x = 0; x < WIDTH; x++) {
73 cairo_set_source_rgba (cr, 1, 1, 1, x * x * 1.0 / (WIDTH * WIDTH));
74 cairo_rectangle (cr, x, 0, 1, HEIGHT);
78 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
79 for (channel = 0; channel < 3; channel++) {
82 case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
83 case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
84 case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
87 for (x = 0; x < WIDTH; x++) {
88 for (y = 0; y < HEIGHT; y++) {
89 double dx = random_offset (WIDTH - x, TRUE);
90 double dy = random_offset (WIDTH - x, TRUE);
91 cairo_rectangle (cr, x + dx, y + dy, x / (double) WIDTH, x / (double) WIDTH);
98 return CAIRO_TEST_SUCCESS;
101 static cairo_test_status_t
102 rhombus (cairo_t *cr, int width, int height)
106 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
109 #if GENERATE_REFERENCE
110 for (y = 0; y < WIDTH; y++) {
111 for (x = 0; x < WIDTH; x++) {
112 cairo_set_source_rgba (cr, 1, 1, 1,
113 x * y / (2. * WIDTH * WIDTH));
114 cairo_rectangle (cr, 2*x, 2*y, 2, 2);
119 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
120 cairo_set_source_rgb (cr, 1, 1, 1);
122 for (y = 0; y < WIDTH; y++) {
123 double yf = y / (double) WIDTH;
124 for (x = 0; x < WIDTH; x++) {
125 double xf = x / (double) WIDTH;
139 cairo_close_path (cr);
146 return CAIRO_TEST_SUCCESS;
149 static cairo_test_status_t
150 intersecting_quads (cairo_t *cr, int width, int height)
156 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
159 #if GENERATE_REFERENCE
160 for (x = 0; x < WIDTH; x++) {
161 cairo_set_source_rgba (cr, 1, 1, 1, x * x * 0.5 / (WIDTH * WIDTH));
162 cairo_rectangle (cr, x, 0, 1, HEIGHT);
166 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
167 for (channel = 0; channel < 3; channel++) {
170 case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
171 case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
172 case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
175 for (x = 0; x < WIDTH; x++) {
176 double step = x / (double) WIDTH;
177 for (y = 0; y < HEIGHT; y++) {
178 double dx = random_offset (WIDTH - x, TRUE);
179 double dy = random_offset (WIDTH - x, TRUE);
180 cairo_move_to (cr, x + dx, y + dy);
181 cairo_rel_line_to (cr, step, step);
182 cairo_rel_line_to (cr, 0, -step);
183 cairo_rel_line_to (cr, -step, step);
184 cairo_close_path (cr);
191 return CAIRO_TEST_SUCCESS;
194 static cairo_test_status_t
195 intersecting_triangles (cairo_t *cr, int width, int height)
201 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
204 #if GENERATE_REFERENCE
205 for (x = 0; x < WIDTH; x++) {
206 cairo_set_source_rgba (cr, 1, 1, 1, x * 0.75 / WIDTH);
207 cairo_rectangle (cr, x, 0, 1, HEIGHT);
211 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
212 for (channel = 0; channel < 3; channel++) {
215 case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
216 case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
217 case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
220 for (x = 0; x < WIDTH; x++) {
221 double step = x / (double) WIDTH;
222 for (y = 0; y < HEIGHT; y++) {
223 double dx = random_offset (WIDTH - x, TRUE);
224 double dy = random_offset (WIDTH - x, TRUE);
227 cairo_move_to (cr, x + dx, y + dy);
228 cairo_rel_line_to (cr, 0, step);
229 cairo_rel_line_to (cr, step, 0);
230 cairo_close_path (cr);
232 /* right, mirrored */
233 cairo_move_to (cr, x + dx + step, y + dy + step);
234 cairo_rel_line_to (cr, 0, -step);
235 cairo_rel_line_to (cr, -step, step);
236 cairo_close_path (cr);
243 return CAIRO_TEST_SUCCESS;
246 static cairo_test_status_t
247 triangles (cairo_t *cr, int width, int height)
253 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
256 #if GENERATE_REFERENCE
257 for (x = 0; x < WIDTH; x++) {
258 cairo_set_source_rgba (cr, 1, 1, 1, x * x * 0.5 / (WIDTH * WIDTH));
259 cairo_rectangle (cr, x, 0, 1, HEIGHT);
263 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
264 for (channel = 0; channel < 3; channel++) {
267 case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
268 case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
269 case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
272 for (x = 0; x < WIDTH; x++) {
273 for (y = 0; y < HEIGHT; y++) {
274 double dx = random_offset (WIDTH - x, TRUE);
275 double dy = random_offset (WIDTH - x, TRUE);
276 cairo_move_to (cr, x + dx, y + dy);
277 cairo_rel_line_to (cr, x / (double) WIDTH, 0);
278 cairo_rel_line_to (cr, 0, x / (double) WIDTH);
279 cairo_close_path (cr);
286 return CAIRO_TEST_SUCCESS;
289 static cairo_test_status_t
290 abutting (cairo_t *cr, int width, int height)
294 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
297 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.75);
299 #if GENERATE_REFERENCE
302 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
304 for (y = 0; y < 16; y++) {
305 for (x = 0; x < 16; x++) {
306 double theta = (y * 16 + x) * M_PI / 512;
307 double cx = 16 * cos (theta) + x * 16;
308 double cy = 16 * sin (theta) + y * 16;
310 cairo_move_to (cr, x * 16, y * 16);
311 cairo_line_to (cr, cx, cy);
312 cairo_line_to (cr, (x + 1) * 16, y * 16);
315 cairo_move_to (cr, (x + 1) * 16, y * 16);
316 cairo_line_to (cr, cx, cy);
317 cairo_line_to (cr, (x + 1) * 16, (y + 1) * 16);
320 cairo_move_to (cr, (x + 1) * 16, (y + 1) * 16);
321 cairo_line_to (cr, cx, cy);
322 cairo_line_to (cr, x * 16, (y + 1) * 16);
325 cairo_move_to (cr, x * 16, (y + 1) * 16);
326 cairo_line_to (cr, cx, cy);
327 cairo_line_to (cr, x * 16, y * 16);
333 return CAIRO_TEST_SUCCESS;
336 static cairo_test_status_t
337 column_triangles (cairo_t *cr, int width, int height)
339 int x, y, i, channel;
343 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
346 #if GENERATE_REFERENCE
347 for (x = 0; x < WIDTH; x++) {
348 cairo_set_source_rgba (cr, 1, 1, 1, x * 0.5 / WIDTH);
349 cairo_rectangle (cr, x, 0, 1, HEIGHT);
353 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
354 for (channel = 0; channel < 3; channel++) {
357 case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
358 case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
359 case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
362 for (x = 0; x < WIDTH; x++) {
363 double step = x / (double) (2 * WIDTH);
364 for (y = 0; y < HEIGHT; y++) {
365 for (i = 0; i < PRECISION; i++) {
366 double dy = random_offset (WIDTH - x, FALSE);
369 * We want to test some sharing of edges to further
370 * stress the rasterisers, so instead of using one
371 * tall triangle, it is split into two, with vertical
372 * edges on either side that may co-align with their
378 * p --- .... | 2 * step = x / WIDTH
385 * Each column contains two triangles of width one quantum and
386 * total height of (x / WIDTH), thus the total area covered by all
387 * columns in each pixel is .5 * (x / WIDTH).
390 cairo_move_to (cr, x + i / (double) PRECISION, y + dy);
391 cairo_rel_line_to (cr, 0, step);
392 cairo_rel_line_to (cr, 1 / (double) PRECISION, step);
393 cairo_rel_line_to (cr, 0, -step);
394 cairo_close_path (cr);
396 cairo_fill (cr); /* do these per-pixel due to the extra volume of edges */
402 return CAIRO_TEST_SUCCESS;
405 static cairo_test_status_t
406 row_triangles (cairo_t *cr, int width, int height)
408 int x, y, i, channel;
412 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
415 #if GENERATE_REFERENCE
416 for (x = 0; x < WIDTH; x++) {
417 cairo_set_source_rgba (cr, 1, 1, 1, x * 0.5 / WIDTH);
418 cairo_rectangle (cr, x, 0, 1, HEIGHT);
422 cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
423 for (channel = 0; channel < 3; channel++) {
426 case 0: cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); break;
427 case 1: cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); break;
428 case 2: cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); break;
431 for (x = 0; x < WIDTH; x++) {
432 double step = x / (double) (2 * WIDTH);
433 for (y = 0; y < HEIGHT; y++) {
434 for (i = 0; i < PRECISION; i++) {
435 double dx = random_offset (WIDTH - x, FALSE);
437 /* See column_triangles() for a transposed description
441 cairo_move_to (cr, x + dx, y + i / (double) PRECISION);
442 cairo_rel_line_to (cr, step, 0);
443 cairo_rel_line_to (cr, step, 1 / (double) PRECISION);
444 cairo_rel_line_to (cr, -step, 0);
445 cairo_close_path (cr);
447 cairo_fill (cr); /* do these per-pixel due to the extra volume of edges */
453 return CAIRO_TEST_SUCCESS;
456 CAIRO_TEST (coverage_rectangles,
457 "Check the fidelity of the rasterisation.",
459 "target=raster", /* requirements */
463 CAIRO_TEST (coverage_rhombus,
464 "Check the fidelity of the rasterisation.",
466 "target=raster", /* requirements */
470 CAIRO_TEST (coverage_intersecting_quads,
471 "Check the fidelity of the rasterisation.",
473 "target=raster", /* requirements */
475 NULL, intersecting_quads)
477 CAIRO_TEST (coverage_intersecting_triangles,
478 "Check the fidelity of the rasterisation.",
480 "target=raster", /* requirements */
482 NULL, intersecting_triangles)
483 CAIRO_TEST (coverage_row_triangles,
484 "Check the fidelity of the rasterisation.",
486 "target=raster", /* requirements */
489 CAIRO_TEST (coverage_column_triangles,
490 "Check the fidelity of the rasterisation.",
492 "target=raster", /* requirements */
494 NULL, column_triangles)
495 CAIRO_TEST (coverage_triangles,
496 "Check the fidelity of the rasterisation.",
498 "target=raster", /* requirements */
501 CAIRO_TEST (coverage_abutting,
502 "Check the fidelity of the rasterisation.",
504 "target=raster", /* requirements */