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