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