mmx: add x8f8g8b8 fetcher
[profile/ivi/pixman.git] / test / affine-test.c
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
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     lcg_srand (testnum);
52
53     src_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
54     dst_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
55     op = (lcg_rand_n (2) == 0) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
56
57     src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
58     src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
59     dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1;
60     dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1;
61     src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp;
62     dst_stride = dst_width * dst_bpp + lcg_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) + lcg_rand_n (src_width * 3 / 2);
71     src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2);
72     dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2);
73     dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2);
74     w = lcg_rand_n (dst_width * 3 / 2 - dst_x);
75     h = lcg_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     for (i = 0; i < src_stride * src_height; i++)
81         *((uint8_t *)srcbuf + i) = lcg_rand_n (256);
82
83     for (i = 0; i < dst_stride * dst_height; i++)
84         *((uint8_t *)dstbuf + i) = lcg_rand_n (256);
85
86     src_fmt = src_bpp == 4 ? (lcg_rand_n (2) == 0 ?
87                               PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
88
89     dst_fmt = dst_bpp == 4 ? (lcg_rand_n (2) == 0 ?
90                               PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
91
92     src_img = pixman_image_create_bits (
93         src_fmt, src_width, src_height, srcbuf, src_stride);
94
95     dst_img = pixman_image_create_bits (
96         dst_fmt, dst_width, dst_height, dstbuf, dst_stride);
97
98     image_endian_swap (src_img);
99     image_endian_swap (dst_img);
100
101     pixman_transform_init_identity (&transform);
102
103     if (lcg_rand_n (3) > 0)
104     {
105         scale_x = -65536 * 3 + lcg_rand_N (65536 * 6);
106         if (lcg_rand_n (2))
107             scale_y = -65536 * 3 + lcg_rand_N (65536 * 6);
108         else
109             scale_y = scale_x;
110         pixman_transform_init_scale (&transform, scale_x, scale_y);
111     }
112     if (lcg_rand_n (3) > 0)
113     {
114         translate_x = -65536 * 3 + lcg_rand_N (6 * 65536);
115         if (lcg_rand_n (2))
116             translate_y = -65536 * 3 + lcg_rand_N (6 * 65536);
117         else
118             translate_y = translate_x;
119         pixman_transform_translate (&transform, NULL, translate_x, translate_y);
120     }
121
122     if (lcg_rand_n (4) > 0)
123     {
124         int c, s, tx = 0, ty = 0;
125         switch (lcg_rand_n (4))
126         {
127         case 0:
128             /* 90 degrees */
129             c = 0;
130             s = pixman_fixed_1;
131             tx = pixman_int_to_fixed (MAX_SRC_HEIGHT);
132             break;
133         case 1:
134             /* 180 degrees */
135             c = -pixman_fixed_1;
136             s = 0;
137             tx = pixman_int_to_fixed (MAX_SRC_WIDTH);
138             ty = pixman_int_to_fixed (MAX_SRC_HEIGHT);
139             break;
140         case 2:
141             /* 270 degrees */
142             c = 0;
143             s = -pixman_fixed_1;
144             ty = pixman_int_to_fixed (MAX_SRC_WIDTH);
145             break;
146         default:
147             /* arbitrary rotation */
148             c = lcg_rand_N (2 * 65536) - 65536;
149             s = lcg_rand_N (2 * 65536) - 65536;
150             break;
151         }
152         pixman_transform_rotate (&transform, NULL, c, s);
153         pixman_transform_translate (&transform, NULL, tx, ty);
154     }
155
156     if (lcg_rand_n (8) == 0)
157     {
158         /* Flip random bits */
159         int maxflipcount = 8;
160         while (maxflipcount--)
161         {
162             int i = lcg_rand_n (2);
163             int j = lcg_rand_n (3);
164             int bitnum = lcg_rand_n (32);
165             transform.matrix[i][j] ^= 1 << bitnum;
166             if (lcg_rand_n (2))
167                 break;
168         }
169     }
170
171     pixman_image_set_transform (src_img, &transform);
172
173     switch (lcg_rand_n (4))
174     {
175     case 0:
176         repeat = PIXMAN_REPEAT_NONE;
177         break;
178
179     case 1:
180         repeat = PIXMAN_REPEAT_NORMAL;
181         break;
182
183     case 2:
184         repeat = PIXMAN_REPEAT_PAD;
185         break;
186
187     case 3:
188         repeat = PIXMAN_REPEAT_REFLECT;
189         break;
190
191     default:
192         break;
193     }
194     pixman_image_set_repeat (src_img, repeat);
195
196     if (lcg_rand_n (2))
197         pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
198     else
199         pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
200
201     if (verbose)
202     {
203         printf ("src_fmt=%08X, dst_fmt=%08X\n", src_fmt, dst_fmt);
204         printf ("op=%d, scale_x=%d, scale_y=%d, repeat=%d\n",
205                 op, scale_x, scale_y, repeat);
206         printf ("translate_x=%d, translate_y=%d\n",
207                 translate_x, translate_y);
208         printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
209                 src_width, src_height, dst_width, dst_height);
210         printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
211                 src_x, src_y, dst_x, dst_y);
212         printf ("w=%d, h=%d\n", w, h);
213     }
214
215     if (lcg_rand_n (8) == 0)
216     {
217         pixman_box16_t clip_boxes[2];
218         int            n = lcg_rand_n (2) + 1;
219
220         for (i = 0; i < n; i++)
221         {
222             clip_boxes[i].x1 = lcg_rand_n (src_width);
223             clip_boxes[i].y1 = lcg_rand_n (src_height);
224             clip_boxes[i].x2 =
225                 clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1);
226             clip_boxes[i].y2 =
227                 clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1);
228
229             if (verbose)
230             {
231                 printf ("source clip box: [%d,%d-%d,%d]\n",
232                         clip_boxes[i].x1, clip_boxes[i].y1,
233                         clip_boxes[i].x2, clip_boxes[i].y2);
234             }
235         }
236
237         pixman_region_init_rects (&clip, clip_boxes, n);
238         pixman_image_set_clip_region (src_img, &clip);
239         pixman_image_set_source_clipping (src_img, 1);
240         pixman_region_fini (&clip);
241     }
242
243     if (lcg_rand_n (8) == 0)
244     {
245         pixman_box16_t clip_boxes[2];
246         int            n = lcg_rand_n (2) + 1;
247         for (i = 0; i < n; i++)
248         {
249             clip_boxes[i].x1 = lcg_rand_n (dst_width);
250             clip_boxes[i].y1 = lcg_rand_n (dst_height);
251             clip_boxes[i].x2 =
252                 clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1);
253             clip_boxes[i].y2 =
254                 clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1);
255
256             if (verbose)
257             {
258                 printf ("destination clip box: [%d,%d-%d,%d]\n",
259                         clip_boxes[i].x1, clip_boxes[i].y1,
260                         clip_boxes[i].x2, clip_boxes[i].y2);
261             }
262         }
263         pixman_region_init_rects (&clip, clip_boxes, n);
264         pixman_image_set_clip_region (dst_img, &clip);
265         pixman_region_fini (&clip);
266     }
267
268     pixman_image_composite (op, src_img, NULL, dst_img,
269                             src_x, src_y, 0, 0, dst_x, dst_y, w, h);
270
271     if (dst_fmt == PIXMAN_x8r8g8b8)
272     {
273         /* ignore unused part */
274         for (i = 0; i < dst_stride * dst_height / 4; i++)
275             dstbuf[i] &= 0xFFFFFF;
276     }
277
278     image_endian_swap (dst_img);
279
280     if (verbose)
281     {
282         int j;
283
284         for (i = 0; i < dst_height; i++)
285         {
286             for (j = 0; j < dst_stride; j++)
287                 printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
288
289             printf ("\n");
290         }
291     }
292
293     pixman_image_unref (src_img);
294     pixman_image_unref (dst_img);
295
296     crc32 = compute_crc32 (0, dstbuf, dst_stride * dst_height);
297     free (srcbuf);
298     free (dstbuf);
299
300     FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
301     return crc32;
302 }
303
304 int
305 main (int argc, const char *argv[])
306 {
307     pixman_disable_out_of_bounds_workaround ();
308
309     return fuzzer_test_main ("affine", 8000000, 0x1EF2175A,
310                              test_composite, argc, argv);
311 }