• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Test program, which can detect some problems with affine transformations
3  * in pixman. Testing is done by running lots of random SRC and OVER
4  * compositing operations a8r8g8b8, x8a8r8g8b8, r5g6b5 and a8 color formats
5  * with random scaled, rotated and translated transforms.
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 <assert.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include "utils.h"
14 
15 #define MAX_SRC_WIDTH  16
16 #define MAX_SRC_HEIGHT 16
17 #define MAX_DST_WIDTH  16
18 #define MAX_DST_HEIGHT 16
19 #define MAX_STRIDE     4
20 
21 /*
22  * Composite operation with pseudorandom images
23  */
24 uint32_t
test_composite(int testnum,int verbose)25 test_composite (int      testnum,
26 		int      verbose)
27 {
28     int                i;
29     pixman_image_t *   src_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                dst_width, dst_height;
35     int                src_stride, dst_stride;
36     int                src_x, src_y;
37     int                dst_x, dst_y;
38     int                src_bpp;
39     int                dst_bpp;
40     int                w, h;
41     pixman_fixed_t     scale_x = 65536, scale_y = 65536;
42     pixman_fixed_t     translate_x = 0, translate_y = 0;
43     pixman_op_t        op;
44     pixman_repeat_t    repeat = PIXMAN_REPEAT_NONE;
45     pixman_format_code_t src_fmt, dst_fmt;
46     uint32_t *         srcbuf;
47     uint32_t *         dstbuf;
48     uint32_t           crc32;
49     FLOAT_REGS_CORRUPTION_DETECTOR_START ();
50 
51     prng_srand (testnum);
52 
53     src_bpp = (prng_rand_n (2) == 0) ? 2 : 4;
54     dst_bpp = (prng_rand_n (2) == 0) ? 2 : 4;
55     op = (prng_rand_n (2) == 0) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
56 
57     src_width = prng_rand_n (MAX_SRC_WIDTH) + 1;
58     src_height = prng_rand_n (MAX_SRC_HEIGHT) + 1;
59     dst_width = prng_rand_n (MAX_DST_WIDTH) + 1;
60     dst_height = prng_rand_n (MAX_DST_HEIGHT) + 1;
61     src_stride = src_width * src_bpp + prng_rand_n (MAX_STRIDE) * src_bpp;
62     dst_stride = dst_width * dst_bpp + prng_rand_n (MAX_STRIDE) * dst_bpp;
63 
64     if (src_stride & 3)
65 	src_stride += 2;
66 
67     if (dst_stride & 3)
68 	dst_stride += 2;
69 
70     src_x = -(src_width / 4) + prng_rand_n (src_width * 3 / 2);
71     src_y = -(src_height / 4) + prng_rand_n (src_height * 3 / 2);
72     dst_x = -(dst_width / 4) + prng_rand_n (dst_width * 3 / 2);
73     dst_y = -(dst_height / 4) + prng_rand_n (dst_height * 3 / 2);
74     w = prng_rand_n (dst_width * 3 / 2 - dst_x);
75     h = prng_rand_n (dst_height * 3 / 2 - dst_y);
76 
77     srcbuf = (uint32_t *)malloc (src_stride * src_height);
78     dstbuf = (uint32_t *)malloc (dst_stride * dst_height);
79 
80     prng_randmemset (srcbuf, src_stride * src_height, 0);
81     prng_randmemset (dstbuf, dst_stride * dst_height, 0);
82 
83     src_fmt = src_bpp == 4 ? (prng_rand_n (2) == 0 ?
84                               PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
85 
86     dst_fmt = dst_bpp == 4 ? (prng_rand_n (2) == 0 ?
87                               PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
88 
89     src_img = pixman_image_create_bits (
90         src_fmt, src_width, src_height, srcbuf, src_stride);
91 
92     dst_img = pixman_image_create_bits (
93         dst_fmt, dst_width, dst_height, dstbuf, dst_stride);
94 
95     image_endian_swap (src_img);
96     image_endian_swap (dst_img);
97 
98     pixman_transform_init_identity (&transform);
99 
100     if (prng_rand_n (3) > 0)
101     {
102 	scale_x = -65536 * 3 + prng_rand_n (65536 * 6);
103 	if (prng_rand_n (2))
104 	    scale_y = -65536 * 3 + prng_rand_n (65536 * 6);
105 	else
106 	    scale_y = scale_x;
107 	pixman_transform_init_scale (&transform, scale_x, scale_y);
108     }
109     if (prng_rand_n (3) > 0)
110     {
111 	translate_x = -65536 * 3 + prng_rand_n (6 * 65536);
112 	if (prng_rand_n (2))
113 	    translate_y = -65536 * 3 + prng_rand_n (6 * 65536);
114 	else
115 	    translate_y = translate_x;
116 	pixman_transform_translate (&transform, NULL, translate_x, translate_y);
117     }
118 
119     if (prng_rand_n (4) > 0)
120     {
121 	int c, s, tx = 0, ty = 0;
122 	switch (prng_rand_n (4))
123 	{
124 	case 0:
125 	    /* 90 degrees */
126 	    c = 0;
127 	    s = pixman_fixed_1;
128 	    tx = pixman_int_to_fixed (MAX_SRC_HEIGHT);
129 	    break;
130 	case 1:
131 	    /* 180 degrees */
132 	    c = -pixman_fixed_1;
133 	    s = 0;
134 	    tx = pixman_int_to_fixed (MAX_SRC_WIDTH);
135 	    ty = pixman_int_to_fixed (MAX_SRC_HEIGHT);
136 	    break;
137 	case 2:
138 	    /* 270 degrees */
139 	    c = 0;
140 	    s = -pixman_fixed_1;
141 	    ty = pixman_int_to_fixed (MAX_SRC_WIDTH);
142 	    break;
143 	default:
144 	    /* arbitrary rotation */
145 	    c = prng_rand_n (2 * 65536) - 65536;
146 	    s = prng_rand_n (2 * 65536) - 65536;
147 	    break;
148 	}
149 	pixman_transform_rotate (&transform, NULL, c, s);
150 	pixman_transform_translate (&transform, NULL, tx, ty);
151     }
152 
153     if (prng_rand_n (8) == 0)
154     {
155 	/* Flip random bits */
156 	int maxflipcount = 8;
157 	while (maxflipcount--)
158 	{
159 	    int i = prng_rand_n (2);
160 	    int j = prng_rand_n (3);
161 	    int bitnum = prng_rand_n (32);
162 	    transform.matrix[i][j] ^= 1 << bitnum;
163 	    if (prng_rand_n (2))
164 		break;
165 	}
166     }
167 
168     pixman_image_set_transform (src_img, &transform);
169 
170     switch (prng_rand_n (4))
171     {
172     case 0:
173 	repeat = PIXMAN_REPEAT_NONE;
174 	break;
175 
176     case 1:
177 	repeat = PIXMAN_REPEAT_NORMAL;
178 	break;
179 
180     case 2:
181 	repeat = PIXMAN_REPEAT_PAD;
182 	break;
183 
184     case 3:
185 	repeat = PIXMAN_REPEAT_REFLECT;
186 	break;
187 
188     default:
189         break;
190     }
191     pixman_image_set_repeat (src_img, repeat);
192 
193     if (prng_rand_n (2))
194 	pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
195     else
196 	pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
197 
198     if (verbose)
199     {
200 #define M(r,c)								\
201 	transform.matrix[r][c]
202 
203 	printf ("src_fmt=%s, dst_fmt=%s\n", format_name (src_fmt), format_name (dst_fmt));
204 	printf ("op=%s, repeat=%d, transform=\n",
205 	        operator_name (op), repeat);
206 	printf (" { { { 0x%08x, 0x%08x, 0x%08x },\n"
207 		"     { 0x%08x, 0x%08x, 0x%08x },\n"
208 		"     { 0x%08x, 0x%08x, 0x%08x },\n"
209 		" } };\n",
210 		M(0,0), M(0,1), M(0,2),
211 		M(1,0), M(1,1), M(1,2),
212 		M(2,0), M(2,1), M(2,2));
213 	printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
214 	        src_width, src_height, dst_width, dst_height);
215 	printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
216 	        src_x, src_y, dst_x, dst_y);
217 	printf ("w=%d, h=%d\n", w, h);
218     }
219 
220     if (prng_rand_n (8) == 0)
221     {
222 	pixman_box16_t clip_boxes[2];
223 	int            n = prng_rand_n (2) + 1;
224 
225 	for (i = 0; i < n; i++)
226 	{
227 	    clip_boxes[i].x1 = prng_rand_n (src_width);
228 	    clip_boxes[i].y1 = prng_rand_n (src_height);
229 	    clip_boxes[i].x2 =
230 		clip_boxes[i].x1 + prng_rand_n (src_width - clip_boxes[i].x1);
231 	    clip_boxes[i].y2 =
232 		clip_boxes[i].y1 + prng_rand_n (src_height - clip_boxes[i].y1);
233 
234 	    if (verbose)
235 	    {
236 		printf ("source clip box: [%d,%d-%d,%d]\n",
237 		        clip_boxes[i].x1, clip_boxes[i].y1,
238 		        clip_boxes[i].x2, clip_boxes[i].y2);
239 	    }
240 	}
241 
242 	pixman_region_init_rects (&clip, clip_boxes, n);
243 	pixman_image_set_clip_region (src_img, &clip);
244 	pixman_image_set_source_clipping (src_img, 1);
245 	pixman_region_fini (&clip);
246     }
247 
248     if (prng_rand_n (8) == 0)
249     {
250 	pixman_box16_t clip_boxes[2];
251 	int            n = prng_rand_n (2) + 1;
252 	for (i = 0; i < n; i++)
253 	{
254 	    clip_boxes[i].x1 = prng_rand_n (dst_width);
255 	    clip_boxes[i].y1 = prng_rand_n (dst_height);
256 	    clip_boxes[i].x2 =
257 		clip_boxes[i].x1 + prng_rand_n (dst_width - clip_boxes[i].x1);
258 	    clip_boxes[i].y2 =
259 		clip_boxes[i].y1 + prng_rand_n (dst_height - clip_boxes[i].y1);
260 
261 	    if (verbose)
262 	    {
263 		printf ("destination clip box: [%d,%d-%d,%d]\n",
264 		        clip_boxes[i].x1, clip_boxes[i].y1,
265 		        clip_boxes[i].x2, clip_boxes[i].y2);
266 	    }
267 	}
268 	pixman_region_init_rects (&clip, clip_boxes, n);
269 	pixman_image_set_clip_region (dst_img, &clip);
270 	pixman_region_fini (&clip);
271     }
272 
273     pixman_image_composite (op, src_img, NULL, dst_img,
274                             src_x, src_y, 0, 0, dst_x, dst_y, w, h);
275 
276     if (dst_fmt == PIXMAN_x8r8g8b8)
277     {
278 	/* ignore unused part */
279 	for (i = 0; i < dst_stride * dst_height / 4; i++)
280 	    dstbuf[i] &= 0xFFFFFF;
281     }
282 
283     image_endian_swap (dst_img);
284 
285     if (verbose)
286     {
287 	int j;
288 
289 	for (i = 0; i < dst_height; i++)
290 	{
291 	    for (j = 0; j < dst_stride; j++)
292 		printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
293 
294 	    printf ("\n");
295 	}
296     }
297 
298     pixman_image_unref (src_img);
299     pixman_image_unref (dst_img);
300 
301     crc32 = compute_crc32 (0, dstbuf, dst_stride * dst_height);
302     free (srcbuf);
303     free (dstbuf);
304 
305     FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
306     return crc32;
307 }
308 
309 #if BILINEAR_INTERPOLATION_BITS == 8
310 #define CHECKSUM 0x2CDF1F07
311 #elif BILINEAR_INTERPOLATION_BITS == 7
312 #define CHECKSUM 0xBC00B1DF
313 #elif BILINEAR_INTERPOLATION_BITS == 4
314 #define CHECKSUM 0xA227306B
315 #else
316 #define CHECKSUM 0x00000000
317 #endif
318 
319 int
main(int argc,const char * argv[])320 main (int argc, const char *argv[])
321 {
322     pixman_disable_out_of_bounds_workaround ();
323 
324     return fuzzer_test_main ("affine", 8000000, CHECKSUM,
325 			     test_composite, argc, argv);
326 }
327