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