2 * Test program, which stresses the use of different color formats and
3 * compositing operations.
5 * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in
6 * the case of test failure.
14 static pixman_indexed_t palette;
17 aligned_malloc (size_t align, size_t size)
21 #ifdef HAVE_POSIX_MEMALIGN
22 if (posix_memalign (&result, align, size) != 0)
25 result = malloc (size);
31 /* Create random image for testing purposes */
32 static pixman_image_t *
33 create_random_image (pixman_format_code_t *allowed_formats,
37 pixman_format_code_t *used_fmt)
39 int n = 0, i, width, height, stride;
40 pixman_format_code_t fmt;
44 while (allowed_formats[n] != -1)
46 fmt = allowed_formats[lcg_rand_n (n)];
48 width = lcg_rand_n (max_width) + 1;
49 height = lcg_rand_n (max_height) + 1;
50 stride = (width * PIXMAN_FORMAT_BPP (fmt) + 7) / 8 +
51 lcg_rand_n (max_extra_stride + 1);
52 stride = (stride + 3) & ~3;
54 /* do the allocation */
55 buf = aligned_malloc (64, stride * height);
57 /* initialize image with random data */
58 for (i = 0; i < stride * height; i++)
60 /* generation is biased to having more 0 or 255 bytes as
61 * they are more likely to be special-cased in code
63 *((uint8_t *)buf + i) = lcg_rand_n (4) ? lcg_rand_n (256) :
64 (lcg_rand_n (2) ? 0 : 255);
67 img = pixman_image_create_bits (fmt, width, height, buf, stride);
69 if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR ||
70 PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
72 pixman_image_set_indexed (img, &palette);
75 image_endian_swap (img, PIXMAN_FORMAT_BPP (fmt));
77 if (used_fmt) *used_fmt = fmt;
81 /* Free random image, and optionally update crc32 based on its data */
83 free_random_image (uint32_t initcrc,
85 pixman_format_code_t fmt)
88 int stride = pixman_image_get_stride (img);
89 uint32_t *data = pixman_image_get_data (img);
90 int height = pixman_image_get_height (img);
94 /* mask unused 'x' part */
95 if (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt) &&
96 PIXMAN_FORMAT_DEPTH (fmt) != 0)
99 uint32_t *data = pixman_image_get_data (img);
100 uint32_t mask = (1 << PIXMAN_FORMAT_DEPTH (fmt)) - 1;
102 if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_BGRA)
103 mask <<= (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt));
105 for (i = 0; i < 32; i++)
106 mask |= mask << (i * PIXMAN_FORMAT_BPP (fmt));
108 for (i = 0; i < stride * height / 4; i++)
112 /* swap endiannes in order to provide identical results on both big
113 * and litte endian systems
115 image_endian_swap (img, PIXMAN_FORMAT_BPP (fmt));
116 crc32 = compute_crc32 (initcrc, data, stride * height);
119 pixman_image_unref (img);
125 static pixman_op_t op_list[] = {
133 PIXMAN_OP_OVER_REVERSE,
135 PIXMAN_OP_IN_REVERSE,
137 PIXMAN_OP_OUT_REVERSE,
139 PIXMAN_OP_ATOP_REVERSE,
143 PIXMAN_OP_DISJOINT_CLEAR,
144 PIXMAN_OP_DISJOINT_SRC,
145 PIXMAN_OP_DISJOINT_DST,
146 PIXMAN_OP_DISJOINT_OVER,
147 PIXMAN_OP_DISJOINT_OVER_REVERSE,
148 PIXMAN_OP_DISJOINT_IN,
149 PIXMAN_OP_DISJOINT_IN_REVERSE,
150 PIXMAN_OP_DISJOINT_OUT,
151 PIXMAN_OP_DISJOINT_OUT_REVERSE,
152 PIXMAN_OP_DISJOINT_ATOP,
153 PIXMAN_OP_DISJOINT_ATOP_REVERSE,
154 PIXMAN_OP_DISJOINT_XOR,
155 PIXMAN_OP_CONJOINT_CLEAR,
156 PIXMAN_OP_CONJOINT_SRC,
157 PIXMAN_OP_CONJOINT_DST,
158 PIXMAN_OP_CONJOINT_OVER,
159 PIXMAN_OP_CONJOINT_OVER_REVERSE,
160 PIXMAN_OP_CONJOINT_IN,
161 PIXMAN_OP_CONJOINT_IN_REVERSE,
162 PIXMAN_OP_CONJOINT_OUT,
163 PIXMAN_OP_CONJOINT_OUT_REVERSE,
164 PIXMAN_OP_CONJOINT_ATOP,
165 PIXMAN_OP_CONJOINT_ATOP_REVERSE,
166 PIXMAN_OP_CONJOINT_XOR,
172 PIXMAN_OP_COLOR_DODGE,
173 PIXMAN_OP_COLOR_BURN,
174 PIXMAN_OP_HARD_LIGHT,
175 PIXMAN_OP_DIFFERENCE,
177 #if 0 /* these use floating point math and are not always bitexact on different platforms */
178 PIXMAN_OP_SOFT_LIGHT,
180 PIXMAN_OP_HSL_SATURATION,
182 PIXMAN_OP_HSL_LUMINOSITY,
186 static pixman_format_code_t img_fmt_list[] = {
234 static pixman_format_code_t mask_fmt_list[] = {
244 * Composite operation with pseudorandom images
247 test_composite (int testnum, int verbose)
250 pixman_image_t *src_img = NULL;
251 pixman_image_t *dst_img = NULL;
252 pixman_image_t *mask_img = NULL;
253 int src_width, src_height;
254 int dst_width, dst_height;
255 int src_stride, dst_stride;
261 pixman_format_code_t src_fmt, dst_fmt, mask_fmt;
262 uint32_t *dstbuf, *srcbuf, *maskbuf;
264 int max_width, max_height, max_extra_stride;
266 max_width = max_height = 24 + testnum / 10000;
267 max_extra_stride = 4 + testnum / 1000000;
275 if (max_extra_stride > 8)
276 max_extra_stride = 8;
280 op = op_list[lcg_rand_n (sizeof (op_list) / sizeof (op_list[0]))];
285 src_img = create_random_image (img_fmt_list, max_width, max_height,
286 max_extra_stride, &src_fmt);
291 src_img = create_random_image (img_fmt_list, 1, 1,
292 max_extra_stride, &src_fmt);
294 pixman_image_set_repeat (src_img, PIXMAN_REPEAT_NORMAL);
297 dst_img = create_random_image (img_fmt_list, max_width, max_height,
298 max_extra_stride, &dst_fmt);
300 src_width = pixman_image_get_width (src_img);
301 src_height = pixman_image_get_height (src_img);
302 src_stride = pixman_image_get_stride (src_img);
304 dst_width = pixman_image_get_width (dst_img);
305 dst_height = pixman_image_get_height (dst_img);
306 dst_stride = pixman_image_get_stride (dst_img);
308 dstbuf = pixman_image_get_data (dst_img);
309 srcbuf = pixman_image_get_data (src_img);
311 src_x = lcg_rand_n (src_width);
312 src_y = lcg_rand_n (src_height);
313 dst_x = lcg_rand_n (dst_width);
314 dst_y = lcg_rand_n (dst_height);
322 if ((src_fmt == PIXMAN_x8r8g8b8 || src_fmt == PIXMAN_x8b8g8r8) &&
323 (lcg_rand_n (4) == 0))
326 mask_fmt = lcg_rand_n (2) ? PIXMAN_a8r8g8b8 : PIXMAN_a8b8g8r8;
327 mask_img = pixman_image_create_bits (mask_fmt,
336 else if (lcg_rand_n (2))
340 mask_img = create_random_image (mask_fmt_list, max_width, max_height,
341 max_extra_stride, &mask_fmt);
346 mask_img = create_random_image (mask_fmt_list, 1, 1,
347 max_extra_stride, &mask_fmt);
348 pixman_image_set_repeat (mask_img, PIXMAN_REPEAT_NORMAL);
352 pixman_image_set_component_alpha (mask_img, 1);
354 mask_x = lcg_rand_n (pixman_image_get_width (mask_img));
355 mask_y = lcg_rand_n (pixman_image_get_height (mask_img));
359 w = lcg_rand_n (dst_width - dst_x + 1);
360 h = lcg_rand_n (dst_height - dst_y + 1);
364 printf ("op=%d, src_fmt=%08X, dst_fmt=%08X, mask_fmt=%08X\n",
365 op, src_fmt, dst_fmt, mask_fmt);
366 printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
367 src_width, src_height, dst_width, dst_height);
368 printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
369 src_x, src_y, dst_x, dst_y);
370 printf ("src_stride=%d, dst_stride=%d\n",
371 src_stride, dst_stride);
372 printf ("w=%d, h=%d\n", w, h);
375 pixman_image_composite (op, src_img, mask_img, dst_img,
376 src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h);
383 for (i = 0; i < dst_height; i++)
385 for (j = 0; j < dst_stride; j++)
387 if (j == (dst_width * PIXMAN_FORMAT_BPP (dst_fmt) + 7) / 8)
390 printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
397 free_random_image (0, src_img, -1);
398 crc32 = free_random_image (0, dst_img, dst_fmt);
402 if (srcbuf == maskbuf)
403 pixman_image_unref(mask_img);
405 free_random_image (0, mask_img, -1);
413 initialize_palette (void)
417 for (i = 0; i < PIXMAN_MAX_INDEXED; ++i)
418 palette.rgba[i] = lcg_rand ();
420 for (i = 0; i < 32768; ++i)
421 palette.ent[i] = lcg_rand() & 0xff;
425 main (int argc, const char *argv[])
427 initialize_palette();
429 return fuzzer_test_main("blitters", 2000000, 0xF3A16994,
430 test_composite, argc, argv);