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