test: Do not include config.h unless HAVE_CONFIG_H is defined
[profile/ivi/pixman.git] / test / blitters-test.c
1 /*
2  * Test program, which stresses the use of different color formats and
3  * compositing operations.
4  *
5  * Script 'fuzzer-find-diff.pl' can be used to narrow down the problem in
6  * the case of test failure.
7  */
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "utils.h"
12
13 static pixman_indexed_t rgb_palette[9];
14 static pixman_indexed_t y_palette[9];
15
16 /* The first eight format in the list are by far the most widely
17  * used formats, so we test those more than the others
18  */
19 #define N_MOST_LIKELY_FORMATS 8
20
21 /* Create random image for testing purposes */
22 static pixman_image_t *
23 create_random_image (pixman_format_code_t *allowed_formats,
24                      int                   max_width,
25                      int                   max_height,
26                      int                   max_extra_stride,
27                      pixman_format_code_t *used_fmt)
28 {
29     int n = 0, i, width, height, stride;
30     pixman_format_code_t fmt;
31     uint32_t *buf;
32     pixman_image_t *img;
33
34     while (allowed_formats[n] != PIXMAN_null)
35         n++;
36
37     if (n > N_MOST_LIKELY_FORMATS && lcg_rand_n (4) != 0)
38         n = N_MOST_LIKELY_FORMATS;
39     fmt = allowed_formats[lcg_rand_n (n)];
40
41     width = lcg_rand_n (max_width) + 1;
42     height = lcg_rand_n (max_height) + 1;
43     stride = (width * PIXMAN_FORMAT_BPP (fmt) + 7) / 8 +
44         lcg_rand_n (max_extra_stride + 1);
45     stride = (stride + 3) & ~3;
46
47     /* do the allocation */
48     buf = aligned_malloc (64, stride * height);
49
50     /* initialize image with random data */
51     for (i = 0; i < stride * height; i++)
52     {
53         /* generation is biased to having more 0 or 255 bytes as
54          * they are more likely to be special-cased in code
55          */
56         *((uint8_t *)buf + i) = lcg_rand_n (4) ? lcg_rand_n (256) :
57             (lcg_rand_n (2) ? 0 : 255);
58     }
59
60     img = pixman_image_create_bits (fmt, width, height, buf, stride);
61
62     if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
63     {
64         pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)]));
65     }
66     else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
67     {
68         pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)]));
69     }
70
71     image_endian_swap (img);
72
73     if (used_fmt) *used_fmt = fmt;
74     return img;
75 }
76
77 /* Free random image, and optionally update crc32 based on its data */
78 static uint32_t
79 free_random_image (uint32_t initcrc,
80                    pixman_image_t *img,
81                    pixman_format_code_t fmt)
82 {
83     uint32_t crc32 = 0;
84     int stride = pixman_image_get_stride (img);
85     uint32_t *data = pixman_image_get_data (img);
86     int height = pixman_image_get_height (img);
87
88     if (fmt != PIXMAN_null)
89     {
90         /* mask unused 'x' part */
91         if (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt) &&
92             PIXMAN_FORMAT_DEPTH (fmt) != 0)
93         {
94             int i;
95             uint32_t *data = pixman_image_get_data (img);
96             uint32_t mask = (1 << PIXMAN_FORMAT_DEPTH (fmt)) - 1;
97
98             if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_BGRA ||
99                 PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_RGBA)
100             {
101                 mask <<= (PIXMAN_FORMAT_BPP (fmt) - PIXMAN_FORMAT_DEPTH (fmt));
102             }
103
104             for (i = 0; i < 32; i++)
105                 mask |= mask << (i * PIXMAN_FORMAT_BPP (fmt));
106
107             for (i = 0; i < stride * height / 4; i++)
108                 data[i] &= mask;
109         }
110
111         /* swap endiannes in order to provide identical results on both big
112          * and litte endian systems
113          */
114         image_endian_swap (img);
115         crc32 = compute_crc32 (initcrc, data, stride * height);
116     }
117
118     pixman_image_unref (img);
119     free (data);
120
121     return crc32;
122 }
123
124 static pixman_op_t op_list[] = {
125     PIXMAN_OP_SRC,
126     PIXMAN_OP_OVER,
127     PIXMAN_OP_ADD,
128     PIXMAN_OP_CLEAR,
129     PIXMAN_OP_SRC,
130     PIXMAN_OP_DST,
131     PIXMAN_OP_OVER,
132     PIXMAN_OP_OVER_REVERSE,
133     PIXMAN_OP_IN,
134     PIXMAN_OP_IN_REVERSE,
135     PIXMAN_OP_OUT,
136     PIXMAN_OP_OUT_REVERSE,
137     PIXMAN_OP_ATOP,
138     PIXMAN_OP_ATOP_REVERSE,
139     PIXMAN_OP_XOR,
140     PIXMAN_OP_ADD,
141     PIXMAN_OP_SATURATE,
142     PIXMAN_OP_DISJOINT_CLEAR,
143     PIXMAN_OP_DISJOINT_SRC,
144     PIXMAN_OP_DISJOINT_DST,
145     PIXMAN_OP_DISJOINT_OVER,
146     PIXMAN_OP_DISJOINT_OVER_REVERSE,
147     PIXMAN_OP_DISJOINT_IN,
148     PIXMAN_OP_DISJOINT_IN_REVERSE,
149     PIXMAN_OP_DISJOINT_OUT,
150     PIXMAN_OP_DISJOINT_OUT_REVERSE,
151     PIXMAN_OP_DISJOINT_ATOP,
152     PIXMAN_OP_DISJOINT_ATOP_REVERSE,
153     PIXMAN_OP_DISJOINT_XOR,
154     PIXMAN_OP_CONJOINT_CLEAR,
155     PIXMAN_OP_CONJOINT_SRC,
156     PIXMAN_OP_CONJOINT_DST,
157     PIXMAN_OP_CONJOINT_OVER,
158     PIXMAN_OP_CONJOINT_OVER_REVERSE,
159     PIXMAN_OP_CONJOINT_IN,
160     PIXMAN_OP_CONJOINT_IN_REVERSE,
161     PIXMAN_OP_CONJOINT_OUT,
162     PIXMAN_OP_CONJOINT_OUT_REVERSE,
163     PIXMAN_OP_CONJOINT_ATOP,
164     PIXMAN_OP_CONJOINT_ATOP_REVERSE,
165     PIXMAN_OP_CONJOINT_XOR,
166     PIXMAN_OP_MULTIPLY,
167     PIXMAN_OP_SCREEN,
168     PIXMAN_OP_OVERLAY,
169     PIXMAN_OP_DARKEN,
170     PIXMAN_OP_LIGHTEN,
171     PIXMAN_OP_COLOR_DODGE,
172     PIXMAN_OP_COLOR_BURN,
173     PIXMAN_OP_HARD_LIGHT,
174     PIXMAN_OP_DIFFERENCE,
175     PIXMAN_OP_EXCLUSION,
176 #if 0 /* these use floating point math and are not always bitexact on different platforms */
177     PIXMAN_OP_SOFT_LIGHT,
178     PIXMAN_OP_HSL_HUE,
179     PIXMAN_OP_HSL_SATURATION,
180     PIXMAN_OP_HSL_COLOR,
181     PIXMAN_OP_HSL_LUMINOSITY,
182 #endif
183 };
184
185 static pixman_format_code_t img_fmt_list[] = {
186     PIXMAN_a8r8g8b8,
187     PIXMAN_a8b8g8r8,
188     PIXMAN_x8r8g8b8,
189     PIXMAN_x8b8g8r8,
190     PIXMAN_r5g6b5,
191     PIXMAN_b5g6r5,
192     PIXMAN_a8,
193     PIXMAN_a1,
194     PIXMAN_r3g3b2,
195     PIXMAN_b8g8r8a8,
196     PIXMAN_b8g8r8x8,
197     PIXMAN_r8g8b8a8,
198     PIXMAN_r8g8b8x8,
199     PIXMAN_x14r6g6b6,
200     PIXMAN_r8g8b8,
201     PIXMAN_b8g8r8,
202     PIXMAN_x2r10g10b10,
203     PIXMAN_a2r10g10b10,
204     PIXMAN_x2b10g10r10,
205     PIXMAN_a2b10g10r10,
206     PIXMAN_a1r5g5b5,
207     PIXMAN_x1r5g5b5,
208     PIXMAN_a1b5g5r5,
209     PIXMAN_x1b5g5r5,
210     PIXMAN_a4r4g4b4,
211     PIXMAN_x4r4g4b4,
212     PIXMAN_a4b4g4r4,
213     PIXMAN_x4b4g4r4,
214     PIXMAN_r3g3b2,
215     PIXMAN_b2g3r3,
216     PIXMAN_a2r2g2b2,
217     PIXMAN_a2b2g2r2,
218     PIXMAN_c8,
219     PIXMAN_g8,
220     PIXMAN_x4c4,
221     PIXMAN_x4g4,
222     PIXMAN_c4,
223     PIXMAN_g4,
224     PIXMAN_g1,
225     PIXMAN_x4a4,
226     PIXMAN_a4,
227     PIXMAN_r1g2b1,
228     PIXMAN_b1g2r1,
229     PIXMAN_a1r1g1b1,
230     PIXMAN_a1b1g1r1,
231     PIXMAN_null
232 };
233
234 static pixman_format_code_t mask_fmt_list[] = {
235     PIXMAN_a8r8g8b8,
236     PIXMAN_a8,
237     PIXMAN_a4,
238     PIXMAN_a1,
239     PIXMAN_null
240 };
241
242
243 /*
244  * Composite operation with pseudorandom images
245  */
246 uint32_t
247 test_composite (int testnum, int verbose)
248 {
249     int i;
250     pixman_image_t *src_img = NULL;
251     pixman_image_t *dst_img = NULL;
252     pixman_image_t *mask_img = NULL;
253     int src_width, src_height;
254     int dst_width, dst_height;
255     int src_stride, dst_stride;
256     int src_x, src_y;
257     int dst_x, dst_y;
258     int mask_x, mask_y;
259     int w, h;
260     pixman_op_t op;
261     pixman_format_code_t src_fmt, dst_fmt, mask_fmt;
262     uint32_t *dstbuf, *srcbuf, *maskbuf;
263     uint32_t crc32;
264     int max_width, max_height, max_extra_stride;
265     FLOAT_REGS_CORRUPTION_DETECTOR_START ();
266
267     max_width = max_height = 24 + testnum / 10000;
268     max_extra_stride = 4 + testnum / 1000000;
269
270     if (max_width > 256)
271         max_width = 256;
272
273     if (max_height > 16)
274         max_height = 16;
275
276     if (max_extra_stride > 8)
277         max_extra_stride = 8;
278
279     lcg_srand (testnum);
280
281     op = op_list[lcg_rand_n (sizeof (op_list) / sizeof (op_list[0]))];
282
283     if (lcg_rand_n (8))
284     {
285         /* normal image */
286         src_img = create_random_image (img_fmt_list, max_width, max_height,
287                                        max_extra_stride, &src_fmt);
288     }
289     else
290     {
291         /* solid case */
292         src_img = create_random_image (img_fmt_list, 1, 1,
293                                        max_extra_stride, &src_fmt);
294
295         pixman_image_set_repeat (src_img, PIXMAN_REPEAT_NORMAL);
296     }
297
298     dst_img = create_random_image (img_fmt_list, max_width, max_height,
299                                    max_extra_stride, &dst_fmt);
300
301     src_width = pixman_image_get_width (src_img);
302     src_height = pixman_image_get_height (src_img);
303     src_stride = pixman_image_get_stride (src_img);
304
305     dst_width = pixman_image_get_width (dst_img);
306     dst_height = pixman_image_get_height (dst_img);
307     dst_stride = pixman_image_get_stride (dst_img);
308
309     dstbuf = pixman_image_get_data (dst_img);
310     srcbuf = pixman_image_get_data (src_img);
311
312     src_x = lcg_rand_n (src_width);
313     src_y = lcg_rand_n (src_height);
314     dst_x = lcg_rand_n (dst_width);
315     dst_y = lcg_rand_n (dst_height);
316
317     mask_img = NULL;
318     mask_fmt = PIXMAN_null;
319     mask_x = 0;
320     mask_y = 0;
321     maskbuf = NULL;
322
323     if ((src_fmt == PIXMAN_x8r8g8b8 || src_fmt == PIXMAN_x8b8g8r8) &&
324         (lcg_rand_n (4) == 0))
325     {
326         /* PIXBUF */
327         mask_fmt = lcg_rand_n (2) ? PIXMAN_a8r8g8b8 : PIXMAN_a8b8g8r8;
328         mask_img = pixman_image_create_bits (mask_fmt,
329                                              src_width,
330                                              src_height,
331                                              srcbuf,
332                                              src_stride);
333         mask_x = src_x;
334         mask_y = src_y;
335         maskbuf = srcbuf;
336     }
337     else if (lcg_rand_n (2))
338     {
339         if (lcg_rand_n (2))
340         {
341             mask_img = create_random_image (mask_fmt_list, max_width, max_height,
342                                            max_extra_stride, &mask_fmt);
343         }
344         else
345         {
346             /* solid case */
347             mask_img = create_random_image (mask_fmt_list, 1, 1,
348                                            max_extra_stride, &mask_fmt);
349             pixman_image_set_repeat (mask_img, PIXMAN_REPEAT_NORMAL);
350         }
351
352         if (lcg_rand_n (2))
353             pixman_image_set_component_alpha (mask_img, 1);
354
355         mask_x = lcg_rand_n (pixman_image_get_width (mask_img));
356         mask_y = lcg_rand_n (pixman_image_get_height (mask_img));
357     }
358
359
360     w = lcg_rand_n (dst_width - dst_x + 1);
361     h = lcg_rand_n (dst_height - dst_y + 1);
362
363     if (verbose)
364     {
365         printf ("op=%d, src_fmt=%08X, dst_fmt=%08X, mask_fmt=%08X\n",
366             op, src_fmt, dst_fmt, mask_fmt);
367         printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
368             src_width, src_height, dst_width, dst_height);
369         printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
370             src_x, src_y, dst_x, dst_y);
371         printf ("src_stride=%d, dst_stride=%d\n",
372             src_stride, dst_stride);
373         printf ("w=%d, h=%d\n", w, h);
374     }
375
376     pixman_image_composite (op, src_img, mask_img, dst_img,
377                             src_x, src_y, mask_x, mask_y, dst_x, dst_y, w, h);
378
379     if (verbose)
380     {
381         int j;
382
383         printf ("---\n");
384         for (i = 0; i < dst_height; i++)
385         {
386             for (j = 0; j < dst_stride; j++)
387             {
388                 if (j == (dst_width * PIXMAN_FORMAT_BPP (dst_fmt) + 7) / 8)
389                     printf ("| ");
390
391                 printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
392             }
393             printf ("\n");
394         }
395         printf ("---\n");
396     }
397
398     free_random_image (0, src_img, PIXMAN_null);
399     crc32 = free_random_image (0, dst_img, dst_fmt);
400
401     if (mask_img)
402     {
403         if (srcbuf == maskbuf)
404             pixman_image_unref(mask_img);
405         else
406             free_random_image (0, mask_img, PIXMAN_null);
407     }
408
409     FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
410     return crc32;
411 }
412
413 int
414 main (int argc, const char *argv[])
415 {
416     int i;
417
418     for (i = 1; i <= 8; i++)
419     {
420         initialize_palette (&(rgb_palette[i]), i, TRUE);
421         initialize_palette (&(y_palette[i]), i, FALSE);
422     }
423
424     return fuzzer_test_main("blitters", 2000000,
425                             0xB610300B,
426                             test_composite, argc, argv);
427 }