solid-test.c revision 6ca29ff0
1/*
2 * Copyright © 2015 RISC OS Open Ltd
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  The copyright holders make no
11 * representations about the suitability of this software for any purpose.  It
12 * is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
19 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
20 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21 * SOFTWARE.
22 *
23 * Author:  Ben Avison (bavison@riscosopen.org)
24 *
25 */
26
27#include "utils.h"
28#include <stdlib.h>
29#include <stdio.h>
30
31#define WIDTH 32
32#define HEIGHT 32
33
34static const pixman_op_t op_list[] = {
35    PIXMAN_OP_SRC,
36    PIXMAN_OP_OVER,
37    PIXMAN_OP_ADD,
38    PIXMAN_OP_CLEAR,
39    PIXMAN_OP_SRC,
40    PIXMAN_OP_DST,
41    PIXMAN_OP_OVER,
42    PIXMAN_OP_OVER_REVERSE,
43    PIXMAN_OP_IN,
44    PIXMAN_OP_IN_REVERSE,
45    PIXMAN_OP_OUT,
46    PIXMAN_OP_OUT_REVERSE,
47    PIXMAN_OP_ATOP,
48    PIXMAN_OP_ATOP_REVERSE,
49    PIXMAN_OP_XOR,
50    PIXMAN_OP_ADD,
51    PIXMAN_OP_MULTIPLY,
52    PIXMAN_OP_SCREEN,
53    PIXMAN_OP_OVERLAY,
54    PIXMAN_OP_DARKEN,
55    PIXMAN_OP_LIGHTEN,
56    PIXMAN_OP_HARD_LIGHT,
57    PIXMAN_OP_DIFFERENCE,
58    PIXMAN_OP_EXCLUSION,
59#if 0 /* these use floating point math and are not always bitexact on different platforms */
60    PIXMAN_OP_SATURATE,
61    PIXMAN_OP_DISJOINT_CLEAR,
62    PIXMAN_OP_DISJOINT_SRC,
63    PIXMAN_OP_DISJOINT_DST,
64    PIXMAN_OP_DISJOINT_OVER,
65    PIXMAN_OP_DISJOINT_OVER_REVERSE,
66    PIXMAN_OP_DISJOINT_IN,
67    PIXMAN_OP_DISJOINT_IN_REVERSE,
68    PIXMAN_OP_DISJOINT_OUT,
69    PIXMAN_OP_DISJOINT_OUT_REVERSE,
70    PIXMAN_OP_DISJOINT_ATOP,
71    PIXMAN_OP_DISJOINT_ATOP_REVERSE,
72    PIXMAN_OP_DISJOINT_XOR,
73    PIXMAN_OP_CONJOINT_CLEAR,
74    PIXMAN_OP_CONJOINT_SRC,
75    PIXMAN_OP_CONJOINT_DST,
76    PIXMAN_OP_CONJOINT_OVER,
77    PIXMAN_OP_CONJOINT_OVER_REVERSE,
78    PIXMAN_OP_CONJOINT_IN,
79    PIXMAN_OP_CONJOINT_IN_REVERSE,
80    PIXMAN_OP_CONJOINT_OUT,
81    PIXMAN_OP_CONJOINT_OUT_REVERSE,
82    PIXMAN_OP_CONJOINT_ATOP,
83    PIXMAN_OP_CONJOINT_ATOP_REVERSE,
84    PIXMAN_OP_CONJOINT_XOR,
85    PIXMAN_OP_COLOR_DODGE,
86    PIXMAN_OP_COLOR_BURN,
87    PIXMAN_OP_SOFT_LIGHT,
88    PIXMAN_OP_HSL_HUE,
89    PIXMAN_OP_HSL_SATURATION,
90    PIXMAN_OP_HSL_COLOR,
91    PIXMAN_OP_HSL_LUMINOSITY,
92#endif
93};
94
95/* The first eight format in the list are by far the most widely
96 * used formats, so we test those more than the others
97 */
98#define N_MOST_LIKELY_FORMATS 8
99
100static const pixman_format_code_t img_fmt_list[] = {
101    PIXMAN_a8r8g8b8,
102    PIXMAN_a8b8g8r8,
103    PIXMAN_x8r8g8b8,
104    PIXMAN_x8b8g8r8,
105    PIXMAN_r5g6b5,
106    PIXMAN_b5g6r5,
107    PIXMAN_a8,
108    PIXMAN_a1,
109    PIXMAN_r3g3b2,
110    PIXMAN_b8g8r8a8,
111    PIXMAN_b8g8r8x8,
112    PIXMAN_r8g8b8a8,
113    PIXMAN_r8g8b8x8,
114    PIXMAN_x14r6g6b6,
115    PIXMAN_r8g8b8,
116    PIXMAN_b8g8r8,
117#if 0 /* These are going to use floating point in the near future */
118    PIXMAN_x2r10g10b10,
119    PIXMAN_a2r10g10b10,
120    PIXMAN_x2b10g10r10,
121    PIXMAN_a2b10g10r10,
122#endif
123    PIXMAN_a1r5g5b5,
124    PIXMAN_x1r5g5b5,
125    PIXMAN_a1b5g5r5,
126    PIXMAN_x1b5g5r5,
127    PIXMAN_a4r4g4b4,
128    PIXMAN_x4r4g4b4,
129    PIXMAN_a4b4g4r4,
130    PIXMAN_x4b4g4r4,
131    PIXMAN_r3g3b2,
132    PIXMAN_b2g3r3,
133    PIXMAN_a2r2g2b2,
134    PIXMAN_a2b2g2r2,
135    PIXMAN_c8,
136    PIXMAN_g8,
137    PIXMAN_x4c4,
138    PIXMAN_x4g4,
139    PIXMAN_c4,
140    PIXMAN_g4,
141    PIXMAN_g1,
142    PIXMAN_x4a4,
143    PIXMAN_a4,
144    PIXMAN_r1g2b1,
145    PIXMAN_b1g2r1,
146    PIXMAN_a1r1g1b1,
147    PIXMAN_a1b1g1r1,
148    PIXMAN_null
149};
150
151static const pixman_format_code_t mask_fmt_list[] = {
152    PIXMAN_a8r8g8b8,
153    PIXMAN_a8,
154    PIXMAN_a4,
155    PIXMAN_a1,
156    PIXMAN_null
157};
158
159static pixman_indexed_t rgb_palette[9];
160static pixman_indexed_t y_palette[9];
161
162static pixman_format_code_t
163random_format (const pixman_format_code_t *allowed_formats)
164{
165    int n = 0;
166
167    while (allowed_formats[n] != PIXMAN_null)
168        n++;
169
170    if (n > N_MOST_LIKELY_FORMATS && prng_rand_n (4) != 0)
171        n = N_MOST_LIKELY_FORMATS;
172
173    return allowed_formats[prng_rand_n (n)];
174}
175
176static pixman_image_t *
177create_multi_pixel_image (const pixman_format_code_t *allowed_formats,
178                          uint32_t                   *buffer,
179                          pixman_format_code_t       *used_fmt)
180{
181    pixman_format_code_t fmt;
182    pixman_image_t *img;
183    int stride;
184
185    fmt = random_format (allowed_formats);
186    stride = (WIDTH * PIXMAN_FORMAT_BPP (fmt) + 31) / 32 * 4;
187    img = pixman_image_create_bits (fmt, WIDTH, HEIGHT, buffer, stride);
188
189    if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
190        pixman_image_set_indexed (img, &(rgb_palette[PIXMAN_FORMAT_BPP (fmt)]));
191    else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
192        pixman_image_set_indexed (img, &(y_palette[PIXMAN_FORMAT_BPP (fmt)]));
193
194    prng_randmemset (buffer, WIDTH * HEIGHT * 4, 0);
195    image_endian_swap (img);
196
197    if (used_fmt)
198        *used_fmt = fmt;
199
200    return img;
201}
202
203static pixman_image_t *
204create_solid_image (const pixman_format_code_t *allowed_formats,
205                    uint32_t                   *buffer,
206                    pixman_format_code_t       *used_fmt)
207{
208    if (prng_rand_n (2))
209    {
210        /* Use a repeating 1x1 bitmap image for solid */
211        pixman_format_code_t fmt;
212        pixman_image_t      *img, *dummy_img;
213        uint32_t             bpp, dummy_buf;
214
215        fmt = random_format (allowed_formats);
216        bpp = PIXMAN_FORMAT_BPP (fmt);
217        img = pixman_image_create_bits (fmt, 1, 1, buffer, 4);
218        pixman_image_set_repeat (img, PIXMAN_REPEAT_NORMAL);
219
220        if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_COLOR)
221            pixman_image_set_indexed (img, &(rgb_palette[bpp]));
222        else if (PIXMAN_FORMAT_TYPE (fmt) == PIXMAN_TYPE_GRAY)
223            pixman_image_set_indexed (img, &(y_palette[bpp]));
224
225        /* Force the flags to be calculated for image with initial
226         * bitmap contents of 0 or 2^bpp-1 by plotting from it into a
227         * separate throwaway image. It is simplest to write all 0s
228         * or all 1s to the first word irrespective of the colour
229         * depth even though we actually only care about the first
230         * pixel since the stride has to be a whole number of words.
231         */
232        *buffer = prng_rand_n (2) ? 0xFFFFFFFFu : 0;
233        dummy_img = pixman_image_create_bits (PIXMAN_a8r8g8b8, 1, 1,
234                                              &dummy_buf, 4);
235        pixman_image_composite (PIXMAN_OP_SRC, img, NULL, dummy_img,
236                                0, 0, 0, 0, 0, 0, 1, 1);
237        pixman_image_unref (dummy_img);
238
239        /* Now set the bitmap contents to a random value */
240        prng_randmemset (buffer, 4, 0);
241        image_endian_swap (img);
242
243        if (used_fmt)
244            *used_fmt = fmt;
245
246        return img;
247    }
248    else
249    {
250        /* Use a native solid image */
251        pixman_color_t color;
252        pixman_image_t *img;
253
254        color.alpha = prng_rand_n (UINT16_MAX + 1);
255        color.red   = prng_rand_n (UINT16_MAX + 1);
256        color.green = prng_rand_n (UINT16_MAX + 1);
257        color.blue  = prng_rand_n (UINT16_MAX + 1);
258        img = pixman_image_create_solid_fill (&color);
259
260        if (used_fmt)
261            *used_fmt = PIXMAN_solid;
262
263        return img;
264    }
265}
266
267static uint32_t
268test_solid (int testnum, int verbose)
269{
270    pixman_op_t          op;
271    uint32_t             src_buf[WIDTH * HEIGHT];
272    uint32_t             dst_buf[WIDTH * HEIGHT];
273    uint32_t             mask_buf[WIDTH * HEIGHT];
274    pixman_image_t      *src_img;
275    pixman_image_t      *dst_img;
276    pixman_image_t      *mask_img = NULL;
277    pixman_format_code_t src_fmt, dst_fmt, mask_fmt = PIXMAN_null;
278    pixman_bool_t        ca = 0;
279    uint32_t             crc32;
280
281    prng_srand (testnum);
282
283    op = op_list[prng_rand_n (ARRAY_LENGTH (op_list))];
284
285    dst_img = create_multi_pixel_image (img_fmt_list, dst_buf, &dst_fmt);
286    switch (prng_rand_n (3))
287    {
288    case 0: /* Solid source, no mask */
289        src_img = create_solid_image (img_fmt_list, src_buf, &src_fmt);
290        break;
291    case 1: /* Solid source, bitmap mask */
292        src_img = create_solid_image (img_fmt_list, src_buf, &src_fmt);
293        mask_img = create_multi_pixel_image (mask_fmt_list, mask_buf, &mask_fmt);
294        break;
295    case 2: /* Bitmap image, solid mask */
296        src_img = create_multi_pixel_image (img_fmt_list, src_buf, &src_fmt);
297        mask_img = create_solid_image (mask_fmt_list, mask_buf, &mask_fmt);
298        break;
299    default:
300        abort ();
301    }
302
303    if (mask_img)
304    {
305        ca = prng_rand_n (2);
306        pixman_image_set_component_alpha (mask_img, ca);
307    }
308
309    if (verbose)
310    {
311        printf ("op=%s\n", operator_name (op));
312        printf ("src_fmt=%s, dst_fmt=%s, mask_fmt=%s\n",
313                format_name (src_fmt), format_name (dst_fmt),
314                format_name (mask_fmt));
315        printf ("src_size=%u, mask_size=%u, component_alpha=%u\n",
316                src_fmt == PIXMAN_solid ? 1 : src_img->bits.width,
317                !mask_img || mask_fmt == PIXMAN_solid ? 1 : mask_img->bits.width,
318                ca);
319    }
320
321    pixman_image_composite (op, src_img, mask_img, dst_img,
322                            0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
323
324    if (verbose)
325        print_image (dst_img);
326
327    crc32 = compute_crc32_for_image (0, dst_img);
328
329    pixman_image_unref (src_img);
330    pixman_image_unref (dst_img);
331    if (mask_img)
332        pixman_image_unref (mask_img);
333
334    return crc32;
335}
336
337int
338main (int argc, const char *argv[])
339{
340    int i;
341
342    prng_srand (0);
343
344    for (i = 1; i <= 8; i++)
345    {
346        initialize_palette (&(rgb_palette[i]), i, TRUE);
347        initialize_palette (&(y_palette[i]), i, FALSE);
348    }
349
350    return fuzzer_test_main ("solid", 500000,
351                             0xC30FD380,
352			     test_solid, argc, argv);
353}
354