1 /*
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.
6 *
7 * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in
8 * the case of test failure.
9 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "utils.h"
13
14 #define MAX_SRC_WIDTH 48
15 #define MAX_SRC_HEIGHT 8
16 #define MAX_DST_WIDTH 48
17 #define MAX_DST_HEIGHT 8
18 #define MAX_STRIDE 4
19
20 /*
21 * Composite operation with pseudorandom images
22 */
23
24 static pixman_format_code_t
get_format(int bpp)25 get_format (int bpp)
26 {
27 if (bpp == 4)
28 {
29 switch (prng_rand_n (4))
30 {
31 default:
32 case 0:
33 return PIXMAN_a8r8g8b8;
34 case 1:
35 return PIXMAN_x8r8g8b8;
36 case 2:
37 return PIXMAN_a8b8g8r8;
38 case 3:
39 return PIXMAN_x8b8g8r8;
40 }
41 }
42 else
43 {
44 return PIXMAN_r5g6b5;
45 }
46 }
47
48 uint32_t
test_composite(int testnum,int verbose)49 test_composite (int testnum,
50 int verbose)
51 {
52 int i;
53 pixman_image_t * src_img;
54 pixman_image_t * mask_img;
55 pixman_image_t * dst_img;
56 pixman_transform_t transform;
57 pixman_region16_t clip;
58 int src_width, src_height;
59 int mask_width, mask_height;
60 int dst_width, dst_height;
61 int src_stride, mask_stride, dst_stride;
62 int src_x, src_y;
63 int mask_x, mask_y;
64 int dst_x, dst_y;
65 int src_bpp;
66 int mask_bpp = 1;
67 int dst_bpp;
68 int w, h;
69 pixman_fixed_t scale_x = 65536, scale_y = 65536;
70 pixman_fixed_t translate_x = 0, translate_y = 0;
71 pixman_fixed_t mask_scale_x = 65536, mask_scale_y = 65536;
72 pixman_fixed_t mask_translate_x = 0, mask_translate_y = 0;
73 pixman_op_t op;
74 pixman_repeat_t repeat = PIXMAN_REPEAT_NONE;
75 pixman_repeat_t mask_repeat = PIXMAN_REPEAT_NONE;
76 pixman_format_code_t src_fmt, dst_fmt;
77 uint32_t * srcbuf;
78 uint32_t * dstbuf;
79 uint32_t * maskbuf;
80 uint32_t crc32;
81 FLOAT_REGS_CORRUPTION_DETECTOR_START ();
82
83 prng_srand (testnum);
84
85 src_bpp = (prng_rand_n (2) == 0) ? 2 : 4;
86 dst_bpp = (prng_rand_n (2) == 0) ? 2 : 4;
87 switch (prng_rand_n (3))
88 {
89 case 0:
90 op = PIXMAN_OP_SRC;
91 break;
92 case 1:
93 op = PIXMAN_OP_OVER;
94 break;
95 default:
96 op = PIXMAN_OP_ADD;
97 break;
98 }
99
100 src_width = prng_rand_n (MAX_SRC_WIDTH) + 1;
101 src_height = prng_rand_n (MAX_SRC_HEIGHT) + 1;
102
103 if (prng_rand_n (2))
104 {
105 mask_width = prng_rand_n (MAX_SRC_WIDTH) + 1;
106 mask_height = prng_rand_n (MAX_SRC_HEIGHT) + 1;
107 }
108 else
109 {
110 mask_width = mask_height = 1;
111 }
112
113 dst_width = prng_rand_n (MAX_DST_WIDTH) + 1;
114 dst_height = prng_rand_n (MAX_DST_HEIGHT) + 1;
115 src_stride = src_width * src_bpp + prng_rand_n (MAX_STRIDE) * src_bpp;
116 mask_stride = mask_width * mask_bpp + prng_rand_n (MAX_STRIDE) * mask_bpp;
117 dst_stride = dst_width * dst_bpp + prng_rand_n (MAX_STRIDE) * dst_bpp;
118
119 if (src_stride & 3)
120 src_stride += 2;
121
122 if (mask_stride & 1)
123 mask_stride += 1;
124 if (mask_stride & 2)
125 mask_stride += 2;
126
127 if (dst_stride & 3)
128 dst_stride += 2;
129
130 src_x = -(src_width / 4) + prng_rand_n (src_width * 3 / 2);
131 src_y = -(src_height / 4) + prng_rand_n (src_height * 3 / 2);
132 mask_x = -(mask_width / 4) + prng_rand_n (mask_width * 3 / 2);
133 mask_y = -(mask_height / 4) + prng_rand_n (mask_height * 3 / 2);
134 dst_x = -(dst_width / 4) + prng_rand_n (dst_width * 3 / 2);
135 dst_y = -(dst_height / 4) + prng_rand_n (dst_height * 3 / 2);
136 w = prng_rand_n (dst_width * 3 / 2 - dst_x);
137 h = prng_rand_n (dst_height * 3 / 2 - dst_y);
138
139 srcbuf = (uint32_t *)malloc (src_stride * src_height);
140 maskbuf = (uint32_t *)malloc (mask_stride * mask_height);
141 dstbuf = (uint32_t *)malloc (dst_stride * dst_height);
142
143 prng_randmemset (srcbuf, src_stride * src_height, 0);
144 prng_randmemset (maskbuf, mask_stride * mask_height, 0);
145 prng_randmemset (dstbuf, dst_stride * dst_height, 0);
146
147 src_fmt = get_format (src_bpp);
148 dst_fmt = get_format (dst_bpp);
149
150 src_img = pixman_image_create_bits (
151 src_fmt, src_width, src_height, srcbuf, src_stride);
152
153 mask_img = pixman_image_create_bits (
154 PIXMAN_a8, mask_width, mask_height, maskbuf, mask_stride);
155
156 dst_img = pixman_image_create_bits (
157 dst_fmt, dst_width, dst_height, dstbuf, dst_stride);
158
159 image_endian_swap (src_img);
160 image_endian_swap (dst_img);
161
162 if (prng_rand_n (4) > 0)
163 {
164 scale_x = -32768 * 3 + prng_rand_n (65536 * 5);
165 scale_y = -32768 * 3 + prng_rand_n (65536 * 5);
166 translate_x = prng_rand_n (65536);
167 translate_y = prng_rand_n (65536);
168 pixman_transform_init_scale (&transform, scale_x, scale_y);
169 pixman_transform_translate (&transform, NULL, translate_x, translate_y);
170 pixman_image_set_transform (src_img, &transform);
171 }
172
173 if (prng_rand_n (2) > 0)
174 {
175 mask_scale_x = -32768 * 3 + prng_rand_n (65536 * 5);
176 mask_scale_y = -32768 * 3 + prng_rand_n (65536 * 5);
177 mask_translate_x = prng_rand_n (65536);
178 mask_translate_y = prng_rand_n (65536);
179 pixman_transform_init_scale (&transform, mask_scale_x, mask_scale_y);
180 pixman_transform_translate (&transform, NULL, mask_translate_x, mask_translate_y);
181 pixman_image_set_transform (mask_img, &transform);
182 }
183
184 switch (prng_rand_n (4))
185 {
186 case 0:
187 mask_repeat = PIXMAN_REPEAT_NONE;
188 break;
189
190 case 1:
191 mask_repeat = PIXMAN_REPEAT_NORMAL;
192 break;
193
194 case 2:
195 mask_repeat = PIXMAN_REPEAT_PAD;
196 break;
197
198 case 3:
199 mask_repeat = PIXMAN_REPEAT_REFLECT;
200 break;
201
202 default:
203 break;
204 }
205 pixman_image_set_repeat (mask_img, mask_repeat);
206
207 switch (prng_rand_n (4))
208 {
209 case 0:
210 repeat = PIXMAN_REPEAT_NONE;
211 break;
212
213 case 1:
214 repeat = PIXMAN_REPEAT_NORMAL;
215 break;
216
217 case 2:
218 repeat = PIXMAN_REPEAT_PAD;
219 break;
220
221 case 3:
222 repeat = PIXMAN_REPEAT_REFLECT;
223 break;
224
225 default:
226 break;
227 }
228 pixman_image_set_repeat (src_img, repeat);
229
230 if (prng_rand_n (2))
231 pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
232 else
233 pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
234
235 if (prng_rand_n (2))
236 pixman_image_set_filter (mask_img, PIXMAN_FILTER_NEAREST, NULL, 0);
237 else
238 pixman_image_set_filter (mask_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
239
240 if (verbose)
241 {
242 printf ("src_fmt=%s, dst_fmt=%s\n",
243 format_name (src_fmt), format_name (dst_fmt));
244 printf ("op=%s, scale_x=%d, scale_y=%d, repeat=%d\n",
245 operator_name (op), scale_x, scale_y, repeat);
246 printf ("translate_x=%d, translate_y=%d\n",
247 translate_x, translate_y);
248 printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
249 src_width, src_height, dst_width, dst_height);
250 printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
251 src_x, src_y, dst_x, dst_y);
252 printf ("w=%d, h=%d\n", w, h);
253 }
254
255 if (prng_rand_n (8) == 0)
256 {
257 pixman_box16_t clip_boxes[2];
258 int n = prng_rand_n (2) + 1;
259
260 for (i = 0; i < n; i++)
261 {
262 clip_boxes[i].x1 = prng_rand_n (src_width);
263 clip_boxes[i].y1 = prng_rand_n (src_height);
264 clip_boxes[i].x2 =
265 clip_boxes[i].x1 + prng_rand_n (src_width - clip_boxes[i].x1);
266 clip_boxes[i].y2 =
267 clip_boxes[i].y1 + prng_rand_n (src_height - clip_boxes[i].y1);
268
269 if (verbose)
270 {
271 printf ("source clip box: [%d,%d-%d,%d]\n",
272 clip_boxes[i].x1, clip_boxes[i].y1,
273 clip_boxes[i].x2, clip_boxes[i].y2);
274 }
275 }
276
277 pixman_region_init_rects (&clip, clip_boxes, n);
278 pixman_image_set_clip_region (src_img, &clip);
279 pixman_image_set_source_clipping (src_img, 1);
280 pixman_region_fini (&clip);
281 }
282
283 if (prng_rand_n (8) == 0)
284 {
285 pixman_box16_t clip_boxes[2];
286 int n = prng_rand_n (2) + 1;
287
288 for (i = 0; i < n; i++)
289 {
290 clip_boxes[i].x1 = prng_rand_n (mask_width);
291 clip_boxes[i].y1 = prng_rand_n (mask_height);
292 clip_boxes[i].x2 =
293 clip_boxes[i].x1 + prng_rand_n (mask_width - clip_boxes[i].x1);
294 clip_boxes[i].y2 =
295 clip_boxes[i].y1 + prng_rand_n (mask_height - clip_boxes[i].y1);
296
297 if (verbose)
298 {
299 printf ("mask clip box: [%d,%d-%d,%d]\n",
300 clip_boxes[i].x1, clip_boxes[i].y1,
301 clip_boxes[i].x2, clip_boxes[i].y2);
302 }
303 }
304
305 pixman_region_init_rects (&clip, clip_boxes, n);
306 pixman_image_set_clip_region (mask_img, &clip);
307 pixman_image_set_source_clipping (mask_img, 1);
308 pixman_region_fini (&clip);
309 }
310
311 if (prng_rand_n (8) == 0)
312 {
313 pixman_box16_t clip_boxes[2];
314 int n = prng_rand_n (2) + 1;
315 for (i = 0; i < n; i++)
316 {
317 clip_boxes[i].x1 = prng_rand_n (dst_width);
318 clip_boxes[i].y1 = prng_rand_n (dst_height);
319 clip_boxes[i].x2 =
320 clip_boxes[i].x1 + prng_rand_n (dst_width - clip_boxes[i].x1);
321 clip_boxes[i].y2 =
322 clip_boxes[i].y1 + prng_rand_n (dst_height - clip_boxes[i].y1);
323
324 if (verbose)
325 {
326 printf ("destination clip box: [%d,%d-%d,%d]\n",
327 clip_boxes[i].x1, clip_boxes[i].y1,
328 clip_boxes[i].x2, clip_boxes[i].y2);
329 }
330 }
331 pixman_region_init_rects (&clip, clip_boxes, n);
332 pixman_image_set_clip_region (dst_img, &clip);
333 pixman_region_fini (&clip);
334 }
335
336 if (prng_rand_n (2) == 0)
337 pixman_image_composite (op, src_img, NULL, dst_img,
338 src_x, src_y, 0, 0, dst_x, dst_y, w, h);
339 else
340 pixman_image_composite (op, src_img, mask_img, dst_img,
341 src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h);
342
343 if (dst_fmt == PIXMAN_x8r8g8b8 || dst_fmt == PIXMAN_x8b8g8r8)
344 {
345 /* ignore unused part */
346 for (i = 0; i < dst_stride * dst_height / 4; i++)
347 dstbuf[i] &= 0xFFFFFF;
348 }
349
350 image_endian_swap (dst_img);
351
352 if (verbose)
353 {
354 int j;
355
356 for (i = 0; i < dst_height; i++)
357 {
358 for (j = 0; j < dst_stride; j++)
359 printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
360
361 printf ("\n");
362 }
363 }
364
365 pixman_image_unref (src_img);
366 pixman_image_unref (mask_img);
367 pixman_image_unref (dst_img);
368
369 crc32 = compute_crc32 (0, dstbuf, dst_stride * dst_height);
370 free (srcbuf);
371 free (maskbuf);
372 free (dstbuf);
373
374 FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
375 return crc32;
376 }
377
378 #if BILINEAR_INTERPOLATION_BITS == 8
379 #define CHECKSUM 0x9096E6B6
380 #elif BILINEAR_INTERPOLATION_BITS == 7
381 #define CHECKSUM 0xCE8EC6BA
382 #elif BILINEAR_INTERPOLATION_BITS == 4
383 #define CHECKSUM 0xAB1D39BE
384 #else
385 #define CHECKSUM 0x00000000
386 #endif
387
388 int
main(int argc,const char * argv[])389 main (int argc, const char *argv[])
390 {
391 pixman_disable_out_of_bounds_workaround ();
392
393 return fuzzer_test_main("scaling", 8000000, CHECKSUM,
394 test_composite, argc, argv);
395 }
396