1 /*
2  * Copyright 2010 Intel Corporation
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: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26 
27 #include "cairo-test.h"
28 
29 /* Test the fidelity of the rasterisation, because Cairo is my favourite
30  * driver test suite.
31  */
32 
33 #define GENERATE_REFERENCE 0
34 
35 #define WIDTH 256
36 #define HEIGHT 40
37 
38 #include "../src/cairo-fixed-type-private.h"
39 #define PRECISION (1 << CAIRO_FIXED_FRAC_BITS)
40 
41 /* XXX beware multithreading! */
42 static uint32_t state;
43 
44 static uint32_t
hars_petruska_f54_1_random(void)45 hars_petruska_f54_1_random (void)
46 {
47 #define rol(x,k) ((x << k) | (x >> (32-k)))
48     return state = (state ^ rol (state, 5) ^ rol (state, 24)) + 0x37798849;
49 #undef rol
50 }
51 
52 static double
random_offset(int range,int precise)53 random_offset (int range, int precise)
54 {
55     double x = hars_petruska_f54_1_random() / (double) UINT32_MAX * range / WIDTH;
56     if (precise)
57 	x = floor (x * PRECISION) / PRECISION;
58     return x;
59 }
60 
61 static cairo_test_status_t
rectangles(cairo_t * cr,int width,int height)62 rectangles (cairo_t *cr, int width, int height)
63 {
64     int x, y, channel;
65 
66     state = 0x12345678;
67 
68     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
69     cairo_paint (cr);
70 
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);
75 	cairo_fill (cr);
76     }
77 #else
78     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
79     for (channel = 0; channel < 3; channel++) {
80 	switch (channel) {
81 	default:
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;
85 	}
86 
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);
92 	    }
93 	}
94 	cairo_fill (cr);
95     }
96 #endif
97 
98     return CAIRO_TEST_SUCCESS;
99 }
100 
101 static cairo_test_status_t
rhombus(cairo_t * cr,int width,int height)102 rhombus (cairo_t *cr, int width, int height)
103 {
104     int x, y;
105 
106     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
107     cairo_paint (cr);
108 
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);
115 	    cairo_fill (cr);
116 	}
117     }
118 #else
119     cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
120     cairo_set_source_rgb (cr, 1, 1, 1);
121 
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;
126 
127 	    cairo_move_to (cr,
128 			   2*x + 1 - xf,
129 			   2*y + 1);
130 	    cairo_line_to (cr,
131 			   2*x + 1,
132 			   2*y + 1 - yf);
133 	    cairo_line_to (cr,
134 			   2*x + 1 + xf,
135 			   2*y + 1);
136 	    cairo_line_to (cr,
137 			   2*x + 1,
138 			   2*y + 1 + yf);
139 	    cairo_close_path (cr);
140 	}
141     }
142 
143     cairo_fill (cr);
144 #endif
145 
146     return CAIRO_TEST_SUCCESS;
147 }
148 
149 static cairo_test_status_t
intersecting_quads(cairo_t * cr,int width,int height)150 intersecting_quads (cairo_t *cr, int width, int height)
151 {
152     int x, y, channel;
153 
154     state = 0x12345678;
155 
156     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
157     cairo_paint (cr);
158 
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);
163 	cairo_fill (cr);
164     }
165 #else
166     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
167     for (channel = 0; channel < 3; channel++) {
168 	switch (channel) {
169 	default:
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;
173 	}
174 
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);
185 	    }
186 	}
187 	cairo_fill (cr);
188     }
189 #endif
190 
191     return CAIRO_TEST_SUCCESS;
192 }
193 
194 static cairo_test_status_t
intersecting_triangles(cairo_t * cr,int width,int height)195 intersecting_triangles (cairo_t *cr, int width, int height)
196 {
197     int x, y, channel;
198 
199     state = 0x12345678;
200 
201     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
202     cairo_paint (cr);
203 
204 #if GENERATE_REFERENCE
205     for (x = 0; x < WIDTH; x++) {
206 	cairo_set_source_rgba (cr, 1, 1, 1, x * x * 0.75 / (WIDTH * WIDTH));
207 	cairo_rectangle (cr, x, 0, 1, HEIGHT);
208 	cairo_fill (cr);
209     }
210 #else
211     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
212     for (channel = 0; channel < 3; channel++) {
213 	switch (channel) {
214 	default:
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;
218 	}
219 
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);
225 
226 		/* left */
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);
231 
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);
237 	    }
238 	}
239 	cairo_fill (cr);
240     }
241 #endif
242 
243     return CAIRO_TEST_SUCCESS;
244 }
245 
246 static cairo_test_status_t
triangles(cairo_t * cr,int width,int height)247 triangles (cairo_t *cr, int width, int height)
248 {
249     int x, y, channel;
250 
251     state = 0x12345678;
252 
253     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
254     cairo_paint (cr);
255 
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);
260 	cairo_fill (cr);
261     }
262 #else
263     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
264     for (channel = 0; channel < 3; channel++) {
265 	switch (channel) {
266 	default:
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;
270 	}
271 
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);
280 	    }
281 	}
282 	cairo_fill (cr);
283     }
284 #endif
285 
286     return CAIRO_TEST_SUCCESS;
287 }
288 
289 static cairo_test_status_t
abutting(cairo_t * cr,int width,int height)290 abutting (cairo_t *cr, int width, int height)
291 {
292     int x, y;
293 
294     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
295     cairo_paint (cr);
296 
297     cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.75);
298 
299 #if GENERATE_REFERENCE
300     cairo_paint (cr);
301 #else
302     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
303 
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;
309 
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);
313 	    cairo_fill (cr);
314 
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);
318 	    cairo_fill (cr);
319 
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);
323 	    cairo_fill (cr);
324 
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);
328 	    cairo_fill (cr);
329 	}
330     }
331 #endif
332 
333     return CAIRO_TEST_SUCCESS;
334 }
335 
336 static cairo_test_status_t
column_triangles(cairo_t * cr,int width,int height)337 column_triangles (cairo_t *cr, int width, int height)
338 {
339     int x, y, i, channel;
340 
341     state = 0x12345678;
342 
343     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
344     cairo_paint (cr);
345 
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);
350 	cairo_fill (cr);
351     }
352 #else
353     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
354     for (channel = 0; channel < 3; channel++) {
355 	switch (channel) {
356 	default:
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;
360 	}
361 
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);
367 
368 		    /*
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
373 		     * neighbours:
374 		     *
375 		     *  s ---  .      ---
376 		     *  t  |   |\      |
377 		     *  e  |   | \     |
378 		     *  p ---  ....    |  2 * step = x / WIDTH
379 		     *          \ |    |
380 		     *           \|    |
381 		     *            .   ---
382 		     *        |---|
383 		     *     1 / PRECISION
384 		     *
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).
388 		     */
389 
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);
395 		}
396 		cairo_fill (cr); /* do these per-pixel due to the extra volume of edges */
397 	    }
398 	}
399     }
400 #endif
401 
402     return CAIRO_TEST_SUCCESS;
403 }
404 
405 static cairo_test_status_t
row_triangles(cairo_t * cr,int width,int height)406 row_triangles (cairo_t *cr, int width, int height)
407 {
408     int x, y, i, channel;
409 
410     state = 0x12345678;
411 
412     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
413     cairo_paint (cr);
414 
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);
419 	cairo_fill (cr);
420     }
421 #else
422     cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
423     for (channel = 0; channel < 3; channel++) {
424 	switch (channel) {
425 	default:
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;
429 	}
430 
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);
436 
437 		    /* See column_triangles() for a transposed description
438 		     * of this geometry.
439 		     */
440 
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);
446 		}
447 		cairo_fill (cr); /* do these per-pixel due to the extra volume of edges */
448 	    }
449 	}
450     }
451 #endif
452 
453     return CAIRO_TEST_SUCCESS;
454 }
455 
456 CAIRO_TEST (coverage_rectangles,
457 	    "Check the fidelity of the rasterisation.",
458 	    NULL, /* keywords */
459 	    "target=raster", /* requirements */
460 	    WIDTH, HEIGHT,
461 	    NULL, rectangles)
462 
463 CAIRO_TEST (coverage_rhombus,
464 	    "Check the fidelity of the rasterisation.",
465 	    NULL, /* keywords */
466 	    "target=raster", /* requirements */
467 	    2*WIDTH, 2*WIDTH,
468 	    NULL, rhombus)
469 
470 CAIRO_TEST (coverage_intersecting_quads,
471 	    "Check the fidelity of the rasterisation.",
472 	    NULL, /* keywords */
473 	    "target=raster", /* requirements */
474 	    WIDTH, HEIGHT,
475 	    NULL, intersecting_quads)
476 
477 CAIRO_TEST (coverage_intersecting_triangles,
478 	    "Check the fidelity of the rasterisation.",
479 	    NULL, /* keywords */
480 	    "target=raster", /* requirements */
481 	    WIDTH, HEIGHT,
482 	    NULL, intersecting_triangles)
483 CAIRO_TEST (coverage_row_triangles,
484 	    "Check the fidelity of the rasterisation.",
485 	    NULL, /* keywords */
486 	    "target=raster", /* requirements */
487 	    WIDTH, HEIGHT,
488 	    NULL, row_triangles)
489 CAIRO_TEST (coverage_column_triangles,
490 	    "Check the fidelity of the rasterisation.",
491 	    NULL, /* keywords */
492 	    "target=raster", /* requirements */
493 	    WIDTH, HEIGHT,
494 	    NULL, column_triangles)
495 CAIRO_TEST (coverage_triangles,
496 	    "Check the fidelity of the rasterisation.",
497 	    NULL, /* keywords */
498 	    "target=raster", /* requirements */
499 	    WIDTH, HEIGHT,
500 	    NULL, triangles)
501 CAIRO_TEST (coverage_abutting,
502 	    "Check the fidelity of the rasterisation.",
503 	    NULL, /* keywords */
504 	    "target=raster", /* requirements */
505 	    16*16, 16*16,
506 	    NULL, abutting)
507