scaling-test.c revision de17ff4a
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
24static pixman_format_code_t
25get_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
48uint32_t
49test_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    if (prng_rand_n (2))
151    {
152	srcbuf += (src_stride / 4) * (src_height - 1);
153	src_stride = - src_stride;
154    }
155
156    if (prng_rand_n (2))
157    {
158	maskbuf += (mask_stride / 4) * (mask_height - 1);
159	mask_stride = - mask_stride;
160    }
161
162    if (prng_rand_n (2))
163    {
164	dstbuf += (dst_stride / 4) * (dst_height - 1);
165	dst_stride = - dst_stride;
166    }
167
168    src_img = pixman_image_create_bits (
169        src_fmt, src_width, src_height, srcbuf, src_stride);
170
171    mask_img = pixman_image_create_bits (
172        PIXMAN_a8, mask_width, mask_height, maskbuf, mask_stride);
173
174    dst_img = pixman_image_create_bits (
175        dst_fmt, dst_width, dst_height, dstbuf, dst_stride);
176
177    image_endian_swap (src_img);
178    image_endian_swap (dst_img);
179
180    if (prng_rand_n (4) > 0)
181    {
182	scale_x = -32768 * 3 + prng_rand_n (65536 * 5);
183	scale_y = -32768 * 3 + prng_rand_n (65536 * 5);
184	translate_x = prng_rand_n (65536);
185	translate_y = prng_rand_n (65536);
186	pixman_transform_init_scale (&transform, scale_x, scale_y);
187	pixman_transform_translate (&transform, NULL, translate_x, translate_y);
188	pixman_image_set_transform (src_img, &transform);
189    }
190
191    if (prng_rand_n (2) > 0)
192    {
193	mask_scale_x = -32768 * 3 + prng_rand_n (65536 * 5);
194	mask_scale_y = -32768 * 3 + prng_rand_n (65536 * 5);
195	mask_translate_x = prng_rand_n (65536);
196	mask_translate_y = prng_rand_n (65536);
197	pixman_transform_init_scale (&transform, mask_scale_x, mask_scale_y);
198	pixman_transform_translate (&transform, NULL, mask_translate_x, mask_translate_y);
199	pixman_image_set_transform (mask_img, &transform);
200    }
201
202    switch (prng_rand_n (4))
203    {
204    case 0:
205	mask_repeat = PIXMAN_REPEAT_NONE;
206	break;
207
208    case 1:
209	mask_repeat = PIXMAN_REPEAT_NORMAL;
210	break;
211
212    case 2:
213	mask_repeat = PIXMAN_REPEAT_PAD;
214	break;
215
216    case 3:
217	mask_repeat = PIXMAN_REPEAT_REFLECT;
218	break;
219
220    default:
221        break;
222    }
223    pixman_image_set_repeat (mask_img, mask_repeat);
224
225    switch (prng_rand_n (4))
226    {
227    case 0:
228	repeat = PIXMAN_REPEAT_NONE;
229	break;
230
231    case 1:
232	repeat = PIXMAN_REPEAT_NORMAL;
233	break;
234
235    case 2:
236	repeat = PIXMAN_REPEAT_PAD;
237	break;
238
239    case 3:
240	repeat = PIXMAN_REPEAT_REFLECT;
241	break;
242
243    default:
244        break;
245    }
246    pixman_image_set_repeat (src_img, repeat);
247
248    if (prng_rand_n (2))
249	pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
250    else
251	pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
252
253    if (prng_rand_n (2))
254	pixman_image_set_filter (mask_img, PIXMAN_FILTER_NEAREST, NULL, 0);
255    else
256	pixman_image_set_filter (mask_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
257
258    if (verbose)
259    {
260	printf ("src_fmt=%s, dst_fmt=%s\n",
261		format_name (src_fmt), format_name (dst_fmt));
262	printf ("op=%s, scale_x=%d, scale_y=%d, repeat=%d\n",
263	        operator_name (op), scale_x, scale_y, repeat);
264	printf ("translate_x=%d, translate_y=%d\n",
265	        translate_x, translate_y);
266	printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
267	        src_width, src_height, dst_width, dst_height);
268	printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
269	        src_x, src_y, dst_x, dst_y);
270	printf ("w=%d, h=%d\n", w, h);
271    }
272
273    if (prng_rand_n (8) == 0)
274    {
275	pixman_box16_t clip_boxes[2];
276	int            n = prng_rand_n (2) + 1;
277
278	for (i = 0; i < n; i++)
279	{
280	    clip_boxes[i].x1 = prng_rand_n (src_width);
281	    clip_boxes[i].y1 = prng_rand_n (src_height);
282	    clip_boxes[i].x2 =
283		clip_boxes[i].x1 + prng_rand_n (src_width - clip_boxes[i].x1);
284	    clip_boxes[i].y2 =
285		clip_boxes[i].y1 + prng_rand_n (src_height - clip_boxes[i].y1);
286
287	    if (verbose)
288	    {
289		printf ("source clip box: [%d,%d-%d,%d]\n",
290		        clip_boxes[i].x1, clip_boxes[i].y1,
291		        clip_boxes[i].x2, clip_boxes[i].y2);
292	    }
293	}
294
295	pixman_region_init_rects (&clip, clip_boxes, n);
296	pixman_image_set_clip_region (src_img, &clip);
297	pixman_image_set_source_clipping (src_img, 1);
298	pixman_region_fini (&clip);
299    }
300
301    if (prng_rand_n (8) == 0)
302    {
303	pixman_box16_t clip_boxes[2];
304	int            n = prng_rand_n (2) + 1;
305
306	for (i = 0; i < n; i++)
307	{
308	    clip_boxes[i].x1 = prng_rand_n (mask_width);
309	    clip_boxes[i].y1 = prng_rand_n (mask_height);
310	    clip_boxes[i].x2 =
311		clip_boxes[i].x1 + prng_rand_n (mask_width - clip_boxes[i].x1);
312	    clip_boxes[i].y2 =
313		clip_boxes[i].y1 + prng_rand_n (mask_height - clip_boxes[i].y1);
314
315	    if (verbose)
316	    {
317		printf ("mask clip box: [%d,%d-%d,%d]\n",
318		        clip_boxes[i].x1, clip_boxes[i].y1,
319		        clip_boxes[i].x2, clip_boxes[i].y2);
320	    }
321	}
322
323	pixman_region_init_rects (&clip, clip_boxes, n);
324	pixman_image_set_clip_region (mask_img, &clip);
325	pixman_image_set_source_clipping (mask_img, 1);
326	pixman_region_fini (&clip);
327    }
328
329    if (prng_rand_n (8) == 0)
330    {
331	pixman_box16_t clip_boxes[2];
332	int            n = prng_rand_n (2) + 1;
333	for (i = 0; i < n; i++)
334	{
335	    clip_boxes[i].x1 = prng_rand_n (dst_width);
336	    clip_boxes[i].y1 = prng_rand_n (dst_height);
337	    clip_boxes[i].x2 =
338		clip_boxes[i].x1 + prng_rand_n (dst_width - clip_boxes[i].x1);
339	    clip_boxes[i].y2 =
340		clip_boxes[i].y1 + prng_rand_n (dst_height - clip_boxes[i].y1);
341
342	    if (verbose)
343	    {
344		printf ("destination clip box: [%d,%d-%d,%d]\n",
345		        clip_boxes[i].x1, clip_boxes[i].y1,
346		        clip_boxes[i].x2, clip_boxes[i].y2);
347	    }
348	}
349	pixman_region_init_rects (&clip, clip_boxes, n);
350	pixman_image_set_clip_region (dst_img, &clip);
351	pixman_region_fini (&clip);
352    }
353
354    if (prng_rand_n (2) == 0)
355	pixman_image_composite (op, src_img, NULL, dst_img,
356                            src_x, src_y, 0, 0, dst_x, dst_y, w, h);
357    else
358	pixman_image_composite (op, src_img, mask_img, dst_img,
359                            src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h);
360
361    crc32 = compute_crc32_for_image (0, dst_img);
362
363    if (verbose)
364	print_image (dst_img);
365
366    pixman_image_unref (src_img);
367    pixman_image_unref (mask_img);
368    pixman_image_unref (dst_img);
369
370    if (src_stride < 0)
371	srcbuf += (src_stride / 4) * (src_height - 1);
372
373    if (mask_stride < 0)
374	maskbuf += (mask_stride / 4) * (mask_height - 1);
375
376    if (dst_stride < 0)
377	dstbuf += (dst_stride / 4) * (dst_height - 1);
378
379    free (srcbuf);
380    free (maskbuf);
381    free (dstbuf);
382
383    FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
384    return crc32;
385}
386
387#if BILINEAR_INTERPOLATION_BITS == 7
388#define CHECKSUM 0x92E0F068
389#elif BILINEAR_INTERPOLATION_BITS == 4
390#define CHECKSUM 0x8EFFA1E5
391#else
392#define CHECKSUM 0x00000000
393#endif
394
395int
396main (int argc, const char *argv[])
397{
398    pixman_disable_out_of_bounds_workaround ();
399
400    return fuzzer_test_main("scaling", 8000000, CHECKSUM,
401			    test_composite, argc, argv);
402}
403