blitters-test.c revision 9ad247e8
1/*
2 * Test program, which stresses the use of different color formats and
3 * compositing operations.
4 *
5 * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in
6 * the case of test failure.
7 */
8#include <stdlib.h>
9#include <stdio.h>
10#include "utils.h"
11
12static pixman_indexed_t rgb_palette[9];
13static pixman_indexed_t y_palette[9];
14
15/* The first eight format in the list are by far the most widely
16 * used formats, so we test those more than the others
17 */
18#define N_MOST_LIKELY_FORMATS 8
19
20/* Create random image for testing purposes */
21static pixman_image_t *
22create_random_image (pixman_format_code_t *allowed_formats,
23		     int                   max_width,
24		     int                   max_height,
25		     int                   max_extra_stride,
26		     pixman_format_code_t *used_fmt)
27{
28    int n = 0, i, width, height, stride;
29    pixman_format_code_t fmt;
30    uint32_t *buf;
31    pixman_image_t *img;
32
33    while (allowed_formats[n] != PIXMAN_null)
34	n++;
35
36    if (n > N_MOST_LIKELY_FORMATS && lcg_rand_n (4) != 0)
37	n = N_MOST_LIKELY_FORMATS;
38    fmt = allowed_formats[lcg_rand_n (n)];
39
40    width = lcg_rand_n (max_width) + 1;
41    height = lcg_rand_n (max_height) + 1;
42    stride = (width * PIXMAN_FORMAT_BPP (fmt) + 7) / 8 +
43	lcg_rand_n (max_extra_stride + 1);
44    stride = (stride + 3) & ~3;
45
46    /* do the allocation */
47    buf = aligned_malloc (64, stride * height);
48
49    /* initialize image with random data */
50    for (i = 0; i < stride * height; i++)
51    {
52	/* generation is biased to having more 0 or 255 bytes as
53	 * they are more likely to be special-cased in code
54	 */
55	*((uint8_t *)buf + i) = lcg_rand_n (4) ? lcg_rand_n (256) :
56	    (lcg_rand_n (2) ? 0 : 255);
57    }
58
59    img = pixman_image_create_bits (fmt, width, height, buf, stride);
60
61    if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
62    {
63	pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)]));
64    }
65    else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
66    {
67	pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)]));
68    }
69
70    if (lcg_rand_n (16) == 0)
71	pixman_image_set_filter (img, PIXMAN_FILTER_BILINEAR, NULL, 0);
72
73    image_endian_swap (img);
74
75    if (used_fmt) *used_fmt = fmt;
76    return img;
77}
78
79/* Free random image, and optionally update crc32 based on its data */
80static uint32_t
81free_random_image (uint32_t initcrc,
82		   pixman_image_t *img,
83		   pixman_format_code_t fmt)
84{
85    uint32_t crc32 = 0;
86    uint32_t *data = pixman_image_get_data (img);
87
88    if (fmt != PIXMAN_null)
89	crc32 = compute_crc32_for_image (initcrc, img);
90
91    pixman_image_unref (img);
92    free (data);
93
94    return crc32;
95}
96
97static pixman_op_t op_list[] = {
98    PIXMAN_OP_SRC,
99    PIXMAN_OP_OVER,
100    PIXMAN_OP_ADD,
101    PIXMAN_OP_CLEAR,
102    PIXMAN_OP_SRC,
103    PIXMAN_OP_DST,
104    PIXMAN_OP_OVER,
105    PIXMAN_OP_OVER_REVERSE,
106    PIXMAN_OP_IN,
107    PIXMAN_OP_IN_REVERSE,
108    PIXMAN_OP_OUT,
109    PIXMAN_OP_OUT_REVERSE,
110    PIXMAN_OP_ATOP,
111    PIXMAN_OP_ATOP_REVERSE,
112    PIXMAN_OP_XOR,
113    PIXMAN_OP_ADD,
114    PIXMAN_OP_SATURATE,
115    PIXMAN_OP_DISJOINT_CLEAR,
116    PIXMAN_OP_DISJOINT_SRC,
117    PIXMAN_OP_DISJOINT_DST,
118    PIXMAN_OP_DISJOINT_OVER,
119    PIXMAN_OP_DISJOINT_OVER_REVERSE,
120    PIXMAN_OP_DISJOINT_IN,
121    PIXMAN_OP_DISJOINT_IN_REVERSE,
122    PIXMAN_OP_DISJOINT_OUT,
123    PIXMAN_OP_DISJOINT_OUT_REVERSE,
124    PIXMAN_OP_DISJOINT_ATOP,
125    PIXMAN_OP_DISJOINT_ATOP_REVERSE,
126    PIXMAN_OP_DISJOINT_XOR,
127    PIXMAN_OP_CONJOINT_CLEAR,
128    PIXMAN_OP_CONJOINT_SRC,
129    PIXMAN_OP_CONJOINT_DST,
130    PIXMAN_OP_CONJOINT_OVER,
131    PIXMAN_OP_CONJOINT_OVER_REVERSE,
132    PIXMAN_OP_CONJOINT_IN,
133    PIXMAN_OP_CONJOINT_IN_REVERSE,
134    PIXMAN_OP_CONJOINT_OUT,
135    PIXMAN_OP_CONJOINT_OUT_REVERSE,
136    PIXMAN_OP_CONJOINT_ATOP,
137    PIXMAN_OP_CONJOINT_ATOP_REVERSE,
138    PIXMAN_OP_CONJOINT_XOR,
139    PIXMAN_OP_MULTIPLY,
140    PIXMAN_OP_SCREEN,
141    PIXMAN_OP_OVERLAY,
142    PIXMAN_OP_DARKEN,
143    PIXMAN_OP_LIGHTEN,
144    PIXMAN_OP_COLOR_DODGE,
145    PIXMAN_OP_COLOR_BURN,
146    PIXMAN_OP_HARD_LIGHT,
147    PIXMAN_OP_DIFFERENCE,
148    PIXMAN_OP_EXCLUSION,
149#if 0 /* these use floating point math and are not always bitexact on different platforms */
150    PIXMAN_OP_SOFT_LIGHT,
151    PIXMAN_OP_HSL_HUE,
152    PIXMAN_OP_HSL_SATURATION,
153    PIXMAN_OP_HSL_COLOR,
154    PIXMAN_OP_HSL_LUMINOSITY,
155#endif
156};
157
158static pixman_format_code_t img_fmt_list[] = {
159    PIXMAN_a8r8g8b8,
160    PIXMAN_a8b8g8r8,
161    PIXMAN_x8r8g8b8,
162    PIXMAN_x8b8g8r8,
163    PIXMAN_r5g6b5,
164    PIXMAN_b5g6r5,
165    PIXMAN_a8,
166    PIXMAN_a1,
167    PIXMAN_r3g3b2,
168    PIXMAN_b8g8r8a8,
169    PIXMAN_b8g8r8x8,
170    PIXMAN_r8g8b8a8,
171    PIXMAN_r8g8b8x8,
172    PIXMAN_x14r6g6b6,
173    PIXMAN_r8g8b8,
174    PIXMAN_b8g8r8,
175#if 0 /* These are going to use floating point in the near future */
176    PIXMAN_x2r10g10b10,
177    PIXMAN_a2r10g10b10,
178    PIXMAN_x2b10g10r10,
179    PIXMAN_a2b10g10r10,
180#endif
181    PIXMAN_a1r5g5b5,
182    PIXMAN_x1r5g5b5,
183    PIXMAN_a1b5g5r5,
184    PIXMAN_x1b5g5r5,
185    PIXMAN_a4r4g4b4,
186    PIXMAN_x4r4g4b4,
187    PIXMAN_a4b4g4r4,
188    PIXMAN_x4b4g4r4,
189    PIXMAN_r3g3b2,
190    PIXMAN_b2g3r3,
191    PIXMAN_a2r2g2b2,
192    PIXMAN_a2b2g2r2,
193    PIXMAN_c8,
194    PIXMAN_g8,
195    PIXMAN_x4c4,
196    PIXMAN_x4g4,
197    PIXMAN_c4,
198    PIXMAN_g4,
199    PIXMAN_g1,
200    PIXMAN_x4a4,
201    PIXMAN_a4,
202    PIXMAN_r1g2b1,
203    PIXMAN_b1g2r1,
204    PIXMAN_a1r1g1b1,
205    PIXMAN_a1b1g1r1,
206    PIXMAN_null
207};
208
209static pixman_format_code_t mask_fmt_list[] = {
210    PIXMAN_a8r8g8b8,
211    PIXMAN_a8,
212    PIXMAN_a4,
213    PIXMAN_a1,
214    PIXMAN_null
215};
216
217
218/*
219 * Composite operation with pseudorandom images
220 */
221uint32_t
222test_composite (int testnum, int verbose)
223{
224    int i;
225    pixman_image_t *src_img = NULL;
226    pixman_image_t *dst_img = NULL;
227    pixman_image_t *mask_img = NULL;
228    int src_width, src_height;
229    int dst_width, dst_height;
230    int src_stride, dst_stride;
231    int src_x, src_y;
232    int dst_x, dst_y;
233    int mask_x, mask_y;
234    int w, h;
235    pixman_op_t op;
236    pixman_format_code_t src_fmt, dst_fmt, mask_fmt;
237    uint32_t *dstbuf, *srcbuf, *maskbuf;
238    uint32_t crc32;
239    int max_width, max_height, max_extra_stride;
240    FLOAT_REGS_CORRUPTION_DETECTOR_START ();
241
242    max_width = max_height = 24 + testnum / 10000;
243    max_extra_stride = 4 + testnum / 1000000;
244
245    if (max_width > 256)
246	max_width = 256;
247
248    if (max_height > 16)
249	max_height = 16;
250
251    if (max_extra_stride > 8)
252	max_extra_stride = 8;
253
254    lcg_srand (testnum);
255
256    op = op_list[lcg_rand_n (ARRAY_LENGTH (op_list))];
257
258    if (lcg_rand_n (8))
259    {
260	/* normal image */
261	src_img = create_random_image (img_fmt_list, max_width, max_height,
262				       max_extra_stride, &src_fmt);
263    }
264    else
265    {
266	/* solid case */
267	src_img = create_random_image (img_fmt_list, 1, 1,
268				       max_extra_stride, &src_fmt);
269
270	pixman_image_set_repeat (src_img, PIXMAN_REPEAT_NORMAL);
271    }
272
273    dst_img = create_random_image (img_fmt_list, max_width, max_height,
274				   max_extra_stride, &dst_fmt);
275
276    src_width = pixman_image_get_width (src_img);
277    src_height = pixman_image_get_height (src_img);
278    src_stride = pixman_image_get_stride (src_img);
279
280    dst_width = pixman_image_get_width (dst_img);
281    dst_height = pixman_image_get_height (dst_img);
282    dst_stride = pixman_image_get_stride (dst_img);
283
284    dstbuf = pixman_image_get_data (dst_img);
285    srcbuf = pixman_image_get_data (src_img);
286
287    src_x = lcg_rand_n (src_width);
288    src_y = lcg_rand_n (src_height);
289    dst_x = lcg_rand_n (dst_width);
290    dst_y = lcg_rand_n (dst_height);
291
292    mask_img = NULL;
293    mask_fmt = PIXMAN_null;
294    mask_x = 0;
295    mask_y = 0;
296    maskbuf = NULL;
297
298    if ((src_fmt == PIXMAN_x8r8g8b8 || src_fmt == PIXMAN_x8b8g8r8) &&
299	(lcg_rand_n (4) == 0))
300    {
301	/* PIXBUF */
302	mask_fmt = lcg_rand_n (2) ? PIXMAN_a8r8g8b8 : PIXMAN_a8b8g8r8;
303	mask_img = pixman_image_create_bits (mask_fmt,
304	                                     src_width,
305	                                     src_height,
306	                                     srcbuf,
307	                                     src_stride);
308	mask_x = src_x;
309	mask_y = src_y;
310	maskbuf = srcbuf;
311    }
312    else if (lcg_rand_n (2))
313    {
314	if (lcg_rand_n (2))
315	{
316	    mask_img = create_random_image (mask_fmt_list, max_width, max_height,
317					   max_extra_stride, &mask_fmt);
318	}
319	else
320	{
321	    /* solid case */
322	    mask_img = create_random_image (mask_fmt_list, 1, 1,
323					   max_extra_stride, &mask_fmt);
324	    pixman_image_set_repeat (mask_img, PIXMAN_REPEAT_NORMAL);
325	}
326
327	if (lcg_rand_n (2))
328	    pixman_image_set_component_alpha (mask_img, 1);
329
330	mask_x = lcg_rand_n (pixman_image_get_width (mask_img));
331	mask_y = lcg_rand_n (pixman_image_get_height (mask_img));
332    }
333
334
335    w = lcg_rand_n (dst_width - dst_x + 1);
336    h = lcg_rand_n (dst_height - dst_y + 1);
337
338    if (verbose)
339    {
340	printf ("op=%d, src_fmt=%08X, dst_fmt=%08X, mask_fmt=%08X\n",
341	    op, src_fmt, dst_fmt, mask_fmt);
342	printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
343	    src_width, src_height, dst_width, dst_height);
344	printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
345	    src_x, src_y, dst_x, dst_y);
346	printf ("src_stride=%d, dst_stride=%d\n",
347	    src_stride, dst_stride);
348	printf ("w=%d, h=%d\n", w, h);
349    }
350
351    pixman_image_composite (op, src_img, mask_img, dst_img,
352			    src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h);
353
354    if (verbose)
355    {
356	int j;
357
358	printf ("---\n");
359	for (i = 0; i < dst_height; i++)
360	{
361	    for (j = 0; j < dst_stride; j++)
362	    {
363		if (j == (dst_width * PIXMAN_FORMAT_BPP (dst_fmt) + 7) / 8)
364		    printf ("| ");
365
366		printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
367	    }
368	    printf ("\n");
369	}
370	printf ("---\n");
371    }
372
373    free_random_image (0, src_img, PIXMAN_null);
374    crc32 = free_random_image (0, dst_img, dst_fmt);
375
376    if (mask_img)
377    {
378	if (srcbuf == maskbuf)
379	    pixman_image_unref(mask_img);
380	else
381	    free_random_image (0, mask_img, PIXMAN_null);
382    }
383
384    FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
385    return crc32;
386}
387
388int
389main (int argc, const char *argv[])
390{
391    int i;
392
393    for (i = 1; i <= 8; i++)
394    {
395	initialize_palette (&(rgb_palette[i]), i, TRUE);
396	initialize_palette (&(y_palette[i]), i, FALSE);
397    }
398
399    return fuzzer_test_main("blitters", 2000000,
400			    0x46136E0A,
401			    test_composite, argc, argv);
402}
403