2 * Test program, which can detect some problems with nearest neighbour
3 * and bilinear scaling in pixman. Testing is done by running lots
4 * of random SRC and OVER compositing operations a8r8g8b8, x8a8r8g8b8
5 * and r5g6b5 color formats.
7 * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in
8 * the case of test failure.
14 #define MAX_SRC_WIDTH 48
15 #define MAX_SRC_HEIGHT 8
16 #define MAX_DST_WIDTH 48
17 #define MAX_DST_HEIGHT 8
21 * Composite operation with pseudorandom images
24 test_composite (int testnum,
28 pixman_image_t * src_img;
29 pixman_image_t * mask_img;
30 pixman_image_t * dst_img;
31 pixman_transform_t transform;
32 pixman_region16_t clip;
33 int src_width, src_height;
34 int mask_width, mask_height;
35 int dst_width, dst_height;
36 int src_stride, mask_stride, dst_stride;
44 pixman_fixed_t scale_x = 65536, scale_y = 65536;
45 pixman_fixed_t translate_x = 0, translate_y = 0;
46 pixman_fixed_t mask_scale_x = 65536, mask_scale_y = 65536;
47 pixman_fixed_t mask_translate_x = 0, mask_translate_y = 0;
49 pixman_repeat_t repeat = PIXMAN_REPEAT_NONE;
50 pixman_repeat_t mask_repeat = PIXMAN_REPEAT_NONE;
51 pixman_format_code_t src_fmt, dst_fmt;
56 FLOAT_REGS_CORRUPTION_DETECTOR_START ();
60 src_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
61 dst_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
62 switch (lcg_rand_n (3))
75 src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
76 src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
80 mask_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
81 mask_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
85 mask_width = mask_height = 1;
88 dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1;
89 dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1;
90 src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp;
91 mask_stride = mask_width * mask_bpp + lcg_rand_n (MAX_STRIDE) * mask_bpp;
92 dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp;
105 src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2);
106 src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2);
107 mask_x = -(mask_width / 4) + lcg_rand_n (mask_width * 3 / 2);
108 mask_y = -(mask_height / 4) + lcg_rand_n (mask_height * 3 / 2);
109 dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2);
110 dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2);
111 w = lcg_rand_n (dst_width * 3 / 2 - dst_x);
112 h = lcg_rand_n (dst_height * 3 / 2 - dst_y);
114 srcbuf = (uint32_t *)malloc (src_stride * src_height);
115 maskbuf = (uint32_t *)malloc (mask_stride * mask_height);
116 dstbuf = (uint32_t *)malloc (dst_stride * dst_height);
118 for (i = 0; i < src_stride * src_height; i++)
119 *((uint8_t *)srcbuf + i) = lcg_rand_n (256);
121 for (i = 0; i < mask_stride * mask_height; i++)
122 *((uint8_t *)maskbuf + i) = lcg_rand_n (256);
124 for (i = 0; i < dst_stride * dst_height; i++)
125 *((uint8_t *)dstbuf + i) = lcg_rand_n (256);
127 src_fmt = src_bpp == 4 ? (lcg_rand_n (2) == 0 ?
128 PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
130 dst_fmt = dst_bpp == 4 ? (lcg_rand_n (2) == 0 ?
131 PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
133 src_img = pixman_image_create_bits (
134 src_fmt, src_width, src_height, srcbuf, src_stride);
136 mask_img = pixman_image_create_bits (
137 PIXMAN_a8, mask_width, mask_height, maskbuf, mask_stride);
139 dst_img = pixman_image_create_bits (
140 dst_fmt, dst_width, dst_height, dstbuf, dst_stride);
142 image_endian_swap (src_img);
143 image_endian_swap (dst_img);
145 if (lcg_rand_n (4) > 0)
147 scale_x = -32768 * 3 + lcg_rand_N (65536 * 5);
148 scale_y = -32768 * 3 + lcg_rand_N (65536 * 5);
149 translate_x = lcg_rand_N (65536);
150 translate_y = lcg_rand_N (65536);
151 pixman_transform_init_scale (&transform, scale_x, scale_y);
152 pixman_transform_translate (&transform, NULL, translate_x, translate_y);
153 pixman_image_set_transform (src_img, &transform);
156 if (lcg_rand_n (2) > 0)
158 mask_scale_x = -32768 * 3 + lcg_rand_N (65536 * 5);
159 mask_scale_y = -32768 * 3 + lcg_rand_N (65536 * 5);
160 mask_translate_x = lcg_rand_N (65536);
161 mask_translate_y = lcg_rand_N (65536);
162 pixman_transform_init_scale (&transform, mask_scale_x, mask_scale_y);
163 pixman_transform_translate (&transform, NULL, mask_translate_x, mask_translate_y);
164 pixman_image_set_transform (mask_img, &transform);
167 switch (lcg_rand_n (4))
170 mask_repeat = PIXMAN_REPEAT_NONE;
174 mask_repeat = PIXMAN_REPEAT_NORMAL;
178 mask_repeat = PIXMAN_REPEAT_PAD;
182 mask_repeat = PIXMAN_REPEAT_REFLECT;
188 pixman_image_set_repeat (mask_img, mask_repeat);
190 switch (lcg_rand_n (4))
193 repeat = PIXMAN_REPEAT_NONE;
197 repeat = PIXMAN_REPEAT_NORMAL;
201 repeat = PIXMAN_REPEAT_PAD;
205 repeat = PIXMAN_REPEAT_REFLECT;
211 pixman_image_set_repeat (src_img, repeat);
214 pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
216 pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
219 pixman_image_set_filter (mask_img, PIXMAN_FILTER_NEAREST, NULL, 0);
221 pixman_image_set_filter (mask_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
225 printf ("src_fmt=%08X, dst_fmt=%08X\n", src_fmt, dst_fmt);
226 printf ("op=%d, scale_x=%d, scale_y=%d, repeat=%d\n",
227 op, scale_x, scale_y, repeat);
228 printf ("translate_x=%d, translate_y=%d\n",
229 translate_x, translate_y);
230 printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
231 src_width, src_height, dst_width, dst_height);
232 printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
233 src_x, src_y, dst_x, dst_y);
234 printf ("w=%d, h=%d\n", w, h);
237 if (lcg_rand_n (8) == 0)
239 pixman_box16_t clip_boxes[2];
240 int n = lcg_rand_n (2) + 1;
242 for (i = 0; i < n; i++)
244 clip_boxes[i].x1 = lcg_rand_n (src_width);
245 clip_boxes[i].y1 = lcg_rand_n (src_height);
247 clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1);
249 clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1);
253 printf ("source clip box: [%d,%d-%d,%d]\n",
254 clip_boxes[i].x1, clip_boxes[i].y1,
255 clip_boxes[i].x2, clip_boxes[i].y2);
259 pixman_region_init_rects (&clip, clip_boxes, n);
260 pixman_image_set_clip_region (src_img, &clip);
261 pixman_image_set_source_clipping (src_img, 1);
262 pixman_region_fini (&clip);
265 if (lcg_rand_n (8) == 0)
267 pixman_box16_t clip_boxes[2];
268 int n = lcg_rand_n (2) + 1;
270 for (i = 0; i < n; i++)
272 clip_boxes[i].x1 = lcg_rand_n (mask_width);
273 clip_boxes[i].y1 = lcg_rand_n (mask_height);
275 clip_boxes[i].x1 + lcg_rand_n (mask_width - clip_boxes[i].x1);
277 clip_boxes[i].y1 + lcg_rand_n (mask_height - clip_boxes[i].y1);
281 printf ("mask clip box: [%d,%d-%d,%d]\n",
282 clip_boxes[i].x1, clip_boxes[i].y1,
283 clip_boxes[i].x2, clip_boxes[i].y2);
287 pixman_region_init_rects (&clip, clip_boxes, n);
288 pixman_image_set_clip_region (mask_img, &clip);
289 pixman_image_set_source_clipping (mask_img, 1);
290 pixman_region_fini (&clip);
293 if (lcg_rand_n (8) == 0)
295 pixman_box16_t clip_boxes[2];
296 int n = lcg_rand_n (2) + 1;
297 for (i = 0; i < n; i++)
299 clip_boxes[i].x1 = lcg_rand_n (dst_width);
300 clip_boxes[i].y1 = lcg_rand_n (dst_height);
302 clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1);
304 clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1);
308 printf ("destination clip box: [%d,%d-%d,%d]\n",
309 clip_boxes[i].x1, clip_boxes[i].y1,
310 clip_boxes[i].x2, clip_boxes[i].y2);
313 pixman_region_init_rects (&clip, clip_boxes, n);
314 pixman_image_set_clip_region (dst_img, &clip);
315 pixman_region_fini (&clip);
318 if (lcg_rand_n (2) == 0)
319 pixman_image_composite (op, src_img, NULL, dst_img,
320 src_x, src_y, 0, 0, dst_x, dst_y, w, h);
322 pixman_image_composite (op, src_img, mask_img, dst_img,
323 src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h);
325 if (dst_fmt == PIXMAN_x8r8g8b8)
327 /* ignore unused part */
328 for (i = 0; i < dst_stride * dst_height / 4; i++)
329 dstbuf[i] &= 0xFFFFFF;
332 image_endian_swap (dst_img);
338 for (i = 0; i < dst_height; i++)
340 for (j = 0; j < dst_stride; j++)
341 printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
347 pixman_image_unref (src_img);
348 pixman_image_unref (mask_img);
349 pixman_image_unref (dst_img);
351 crc32 = compute_crc32 (0, dstbuf, dst_stride * dst_height);
356 FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
361 main (int argc, const char *argv[])
363 pixman_disable_out_of_bounds_workaround ();
365 return fuzzer_test_main("scaling", 8000000, 0x80DF1CB2,
366 test_composite, argc, argv);