upload tizen 2.0 source
[framework/graphics/pixman.git] / TC / testcase / utc_composite_traps_test.c
1 #include <tet_api.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "utils.h"
8
9 static void startup(void);
10 static void cleanup(void);
11
12 void (*tet_startup)(void) = startup;
13 void (*tet_cleanup)(void) = cleanup;
14
15 static void utc_pixman_composite_traps(void);
16
17 struct tet_testlist tet_testlist[] = {
18         { utc_pixman_composite_traps, 1 },
19         { NULL, 0 },
20 };
21
22 static void startup(void)
23 {
24         /* start of TC */
25 }
26
27 static void cleanup(void)
28 {
29         /* end of TC */
30 }
31
32 #define MAX_SRC_WIDTH  48
33 #define MAX_SRC_HEIGHT 48
34 #define MAX_DST_WIDTH  48
35 #define MAX_DST_HEIGHT 48
36 #define MAX_STRIDE     4
37
38 static pixman_format_code_t formats[] =
39 {
40     PIXMAN_a8r8g8b8, PIXMAN_a8, PIXMAN_r5g6b5, PIXMAN_a1, PIXMAN_a4
41 };
42
43 static pixman_format_code_t mask_formats[] =
44 {
45     PIXMAN_a1, PIXMAN_a4, PIXMAN_a8,
46 };
47
48 static pixman_op_t operators[] =
49 {
50     PIXMAN_OP_OVER, PIXMAN_OP_ADD, PIXMAN_OP_SRC, PIXMAN_OP_IN
51 };
52
53 #define RANDOM_ELT(array)                                               \
54     ((array)[lcg_rand_n(ARRAY_LENGTH((array)))])
55
56 static void
57 destroy_bits (pixman_image_t *image, void *data)
58 {
59     fence_free (data);
60 }
61
62 static pixman_fixed_t
63 random_fixed (int n)
64 {
65     return lcg_rand_N (n << 16);
66 }
67
68 /*
69  * Composite operation with pseudorandom images
70  */
71 uint32_t
72 test_composite (int      testnum,
73                 int      verbose)
74 {
75     int                i;
76     pixman_image_t *   src_img;
77     pixman_image_t *   dst_img;
78     pixman_region16_t  clip;
79     int                dst_width, dst_height;
80     int                dst_stride;
81     int                dst_x, dst_y;
82     int                dst_bpp;
83     pixman_op_t        op;
84     uint32_t *         dst_bits;
85     uint32_t           crc32;
86     pixman_format_code_t mask_format, dst_format;
87     pixman_trapezoid_t *traps;
88     int src_x, src_y;
89     int n_traps;
90
91     static pixman_color_t colors[] =
92     {
93         { 0xffff, 0xffff, 0xffff, 0xffff },
94         { 0x0000, 0x0000, 0x0000, 0x0000 },
95         { 0xabcd, 0xabcd, 0x0000, 0xabcd },
96         { 0x0000, 0x0000, 0x0000, 0xffff },
97         { 0x0101, 0x0101, 0x0101, 0x0101 },
98         { 0x7777, 0x6666, 0x5555, 0x9999 },
99     };
100
101     FLOAT_REGS_CORRUPTION_DETECTOR_START ();
102
103     lcg_srand (testnum);
104
105     op = RANDOM_ELT (operators);
106     mask_format = RANDOM_ELT (mask_formats);
107
108     /* Create source image */
109
110     if (lcg_rand_n (4) == 0)
111     {
112         src_img = pixman_image_create_solid_fill (
113             &(colors[lcg_rand_n (ARRAY_LENGTH (colors))]));
114
115         src_x = 10;
116         src_y = 234;
117     }
118     else
119     {
120         pixman_format_code_t src_format = RANDOM_ELT(formats);
121         int src_bpp = (PIXMAN_FORMAT_BPP (src_format) + 7) / 8;
122         int src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
123         int src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
124         int src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp;
125         uint32_t *bits;
126
127         src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2);
128         src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2);
129
130         src_stride = (src_stride + 3) & ~3;
131
132         bits = (uint32_t *)make_random_bytes (src_stride * src_height);
133
134         src_img = pixman_image_create_bits (
135             src_format, src_width, src_height, bits, src_stride);
136
137         pixman_image_set_destroy_function (src_img, destroy_bits, bits);
138
139         if (lcg_rand_n (8) == 0)
140         {
141             pixman_box16_t clip_boxes[2];
142             int            n = lcg_rand_n (2) + 1;
143
144             for (i = 0; i < n; i++)
145             {
146                 clip_boxes[i].x1 = lcg_rand_n (src_width);
147                 clip_boxes[i].y1 = lcg_rand_n (src_height);
148                 clip_boxes[i].x2 =
149                     clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1);
150                 clip_boxes[i].y2 =
151                     clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1);
152
153                 if (verbose)
154                 {
155                     printf ("source clip box: [%d,%d-%d,%d]\n",
156                             clip_boxes[i].x1, clip_boxes[i].y1,
157                             clip_boxes[i].x2, clip_boxes[i].y2);
158                 }
159             }
160
161             pixman_region_init_rects (&clip, clip_boxes, n);
162             pixman_image_set_clip_region (src_img, &clip);
163             pixman_image_set_source_clipping (src_img, 1);
164             pixman_region_fini (&clip);
165         }
166
167         image_endian_swap (src_img);
168     }
169
170     /* Create destination image */
171     {
172         dst_format = RANDOM_ELT(formats);
173         dst_bpp = (PIXMAN_FORMAT_BPP (dst_format) + 7) / 8;
174         dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1;
175         dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1;
176         dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp;
177         dst_stride = (dst_stride + 3) & ~3;
178
179         dst_bits = (uint32_t *)make_random_bytes (dst_stride * dst_height);
180
181         dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2);
182         dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2);
183
184         dst_img = pixman_image_create_bits (
185             dst_format, dst_width, dst_height, dst_bits, dst_stride);
186
187         image_endian_swap (dst_img);
188     }
189
190     /* Create traps */
191     {
192         int i;
193
194         n_traps = lcg_rand_n (25);
195         traps = fence_malloc (n_traps * sizeof (pixman_trapezoid_t));
196
197         for (i = 0; i < n_traps; ++i)
198         {
199             pixman_trapezoid_t *t = &(traps[i]);
200
201             t->top = random_fixed (MAX_DST_HEIGHT) - MAX_DST_HEIGHT / 2;
202             t->bottom = t->top + random_fixed (MAX_DST_HEIGHT);
203             t->left.p1.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2;
204             t->left.p1.y = t->top - random_fixed (50);
205             t->left.p2.x = random_fixed (MAX_DST_WIDTH) - MAX_DST_WIDTH / 2;
206             t->left.p2.y = t->bottom + random_fixed (50);
207             t->right.p1.x = t->left.p1.x + random_fixed (MAX_DST_WIDTH);
208             t->right.p1.y = t->top - random_fixed (50);
209             t->right.p2.x = t->left.p2.x + random_fixed (MAX_DST_WIDTH);
210             t->right.p2.y = t->bottom - random_fixed (50);
211         }
212     }
213
214     if (lcg_rand_n (8) == 0)
215     {
216         pixman_box16_t clip_boxes[2];
217         int            n = lcg_rand_n (2) + 1;
218         for (i = 0; i < n; i++)
219         {
220             clip_boxes[i].x1 = lcg_rand_n (dst_width);
221             clip_boxes[i].y1 = lcg_rand_n (dst_height);
222             clip_boxes[i].x2 =
223                 clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1);
224             clip_boxes[i].y2 =
225                 clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1);
226
227             if (verbose)
228             {
229                 printf ("destination clip box: [%d,%d-%d,%d]\n",
230                         clip_boxes[i].x1, clip_boxes[i].y1,
231                         clip_boxes[i].x2, clip_boxes[i].y2);
232             }
233         }
234         pixman_region_init_rects (&clip, clip_boxes, n);
235         pixman_image_set_clip_region (dst_img, &clip);
236         pixman_region_fini (&clip);
237     }
238
239     pixman_composite_trapezoids (op, src_img, dst_img, mask_format,
240                                  src_x, src_y, dst_x, dst_y, n_traps, traps);
241
242     if (dst_format == PIXMAN_x8r8g8b8)
243     {
244         /* ignore unused part */
245         for (i = 0; i < dst_stride * dst_height / 4; i++)
246             dst_bits[i] &= 0xFFFFFF;
247     }
248
249     image_endian_swap (dst_img);
250
251     if (verbose)
252     {
253         int j;
254
255         for (i = 0; i < dst_height; i++)
256         {
257             for (j = 0; j < dst_stride; j++)
258                 printf ("%02X ", *((uint8_t *)dst_bits + i * dst_stride + j));
259
260             printf ("\n");
261         }
262     }
263
264     crc32 = compute_crc32 (0, dst_bits, dst_stride * dst_height);
265
266     fence_free (dst_bits);
267
268     pixman_image_unref (src_img);
269     pixman_image_unref (dst_img);
270     fence_free (traps);
271
272     FLOAT_REGS_CORRUPTION_DETECTOR_FINISH ();
273     return crc32;
274 }
275
276 static void utc_pixman_composite_traps(void)
277 {
278     int ret;
279
280     ret = fuzzer_test_main("composite traps", 40000, 0xE3112106,
281                             test_composite, 1, NULL);
282
283     if(ret)
284         dts_fail("utc_pixman_composite_traps");
285
286     dts_pass("utc_pixman_composite_traps");
287 }