scaling-test.c revision 952204ab
1/*
2 * Test program, which can detect problems with nearest neighbout scaling
3 * implementation. Also SRC and OVER opetations tested for 16bpp and 32bpp
4 * images.
5 *
6 * Just run it without any command line arguments, and it will report either
7 *   "scaling test passed" - everything is ok
8 *   "scaling test failed!" - there is some problem
9 *
10 * In the case of failure, finding the problem involves the following steps:
11 * 1. Get the reference 'scaling-test' binary. It makes sense to disable all
12 *    the cpu specific optimizations in pixman and also configure it with
13 *    '--disable-shared' option. Those who are paranoid can also tweak the
14 *    sources to disable all fastpath functions. The resulting binary
15 *    can be renamed to something like 'scaling-test.ref'.
16 * 2. Compile the buggy binary (also with the '--disable-shared' option).
17 * 3. Run 'ruby scaling-test-bisect.rb ./scaling-test.ref ./scaling-test'
18 * 4. Look at the information about failed case (destination buffer content
19 *    will be shown) and try to figure out what is wrong. It is possible
20 *    to use debugging print to stderr in pixman to get more information,
21 *    this does not interfere with the testing script.
22 */
23#include <assert.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include "utils.h"
27
28#define MAX_SRC_WIDTH  10
29#define MAX_SRC_HEIGHT 10
30#define MAX_DST_WIDTH  10
31#define MAX_DST_HEIGHT 10
32#define MAX_STRIDE     4
33
34/*
35 * Composite operation with pseudorandom images
36 */
37uint32_t
38test_composite (uint32_t initcrc,
39		int      testnum,
40		int      verbose)
41{
42    int                i;
43    pixman_image_t *   src_img;
44    pixman_image_t *   dst_img;
45    pixman_transform_t transform;
46    pixman_region16_t  clip;
47    int                src_width, src_height;
48    int                dst_width, dst_height;
49    int                src_stride, dst_stride;
50    int                src_x, src_y;
51    int                dst_x, dst_y;
52    int                src_bpp;
53    int                dst_bpp;
54    int                w, h;
55    int                scale_x = 32768, scale_y = 32768;
56    int                op;
57    int                repeat = 0;
58    int                src_fmt, dst_fmt;
59    uint32_t *         srcbuf;
60    uint32_t *         dstbuf;
61    uint32_t           crc32;
62
63    lcg_srand (testnum);
64
65    src_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
66    dst_bpp = (lcg_rand_n (2) == 0) ? 2 : 4;
67    op = (lcg_rand_n (2) == 0) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
68
69    src_width = lcg_rand_n (MAX_SRC_WIDTH) + 1;
70    src_height = lcg_rand_n (MAX_SRC_HEIGHT) + 1;
71    dst_width = lcg_rand_n (MAX_DST_WIDTH) + 1;
72    dst_height = lcg_rand_n (MAX_DST_HEIGHT) + 1;
73    src_stride = src_width * src_bpp + lcg_rand_n (MAX_STRIDE) * src_bpp;
74    dst_stride = dst_width * dst_bpp + lcg_rand_n (MAX_STRIDE) * dst_bpp;
75
76    if (src_stride & 3)
77	src_stride += 2;
78
79    if (dst_stride & 3)
80	dst_stride += 2;
81
82    src_x = -(src_width / 4) + lcg_rand_n (src_width * 3 / 2);
83    src_y = -(src_height / 4) + lcg_rand_n (src_height * 3 / 2);
84    dst_x = -(dst_width / 4) + lcg_rand_n (dst_width * 3 / 2);
85    dst_y = -(dst_height / 4) + lcg_rand_n (dst_height * 3 / 2);
86    w = lcg_rand_n (dst_width * 3 / 2 - dst_x);
87    h = lcg_rand_n (dst_height * 3 / 2 - dst_y);
88
89    srcbuf = (uint32_t *)malloc (src_stride * src_height);
90    dstbuf = (uint32_t *)malloc (dst_stride * dst_height);
91
92    for (i = 0; i < src_stride * src_height; i++)
93	*((uint8_t *)srcbuf + i) = lcg_rand_n (256);
94
95    for (i = 0; i < dst_stride * dst_height; i++)
96	*((uint8_t *)dstbuf + i) = lcg_rand_n (256);
97
98    src_fmt = src_bpp == 4 ? (lcg_rand_n (2) == 0 ?
99                              PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
100
101    dst_fmt = dst_bpp == 4 ? (lcg_rand_n (2) == 0 ?
102                              PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8) : PIXMAN_r5g6b5;
103
104    src_img = pixman_image_create_bits (
105        src_fmt, src_width, src_height, srcbuf, src_stride);
106
107    dst_img = pixman_image_create_bits (
108        dst_fmt, dst_width, dst_height, dstbuf, dst_stride);
109
110    image_endian_swap (src_img, src_bpp * 8);
111    image_endian_swap (dst_img, dst_bpp * 8);
112
113    if (lcg_rand_n (8) > 0)
114    {
115	scale_x = 32768 + lcg_rand_n (65536);
116	scale_y = 32768 + lcg_rand_n (65536);
117	pixman_transform_init_scale (&transform, scale_x, scale_y);
118	pixman_image_set_transform (src_img, &transform);
119    }
120
121    switch (lcg_rand_n (4))
122    {
123    case 0:
124	repeat = PIXMAN_REPEAT_NONE;
125	break;
126
127    case 1:
128	repeat = PIXMAN_REPEAT_NORMAL;
129	break;
130
131    case 2:
132	repeat = PIXMAN_REPEAT_PAD;
133	break;
134
135    case 3:
136	repeat = PIXMAN_REPEAT_REFLECT;
137	break;
138
139    default:
140        break;
141    }
142    pixman_image_set_repeat (src_img, repeat);
143
144    if (lcg_rand_n (2))
145	pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
146    else
147	pixman_image_set_filter (src_img, PIXMAN_FILTER_BILINEAR, NULL, 0);
148
149    if (verbose)
150    {
151	printf ("src_fmt=%08X, dst_fmt=%08X\n", src_fmt, dst_fmt);
152	printf ("op=%d, scale_x=%d, scale_y=%d, repeat=%d\n",
153	        op, scale_x, scale_y, repeat);
154	printf ("src_width=%d, src_height=%d, dst_width=%d, dst_height=%d\n",
155	        src_width, src_height, dst_width, dst_height);
156	printf ("src_x=%d, src_y=%d, dst_x=%d, dst_y=%d\n",
157	        src_x, src_y, dst_x, dst_y);
158	printf ("w=%d, h=%d\n", w, h);
159    }
160
161    if (lcg_rand_n (8) == 0)
162    {
163	pixman_box16_t clip_boxes[2];
164	int            n = lcg_rand_n (2) + 1;
165
166	for (i = 0; i < n; i++)
167	{
168	    clip_boxes[i].x1 = lcg_rand_n (src_width);
169	    clip_boxes[i].y1 = lcg_rand_n (src_height);
170	    clip_boxes[i].x2 =
171		clip_boxes[i].x1 + lcg_rand_n (src_width - clip_boxes[i].x1);
172	    clip_boxes[i].y2 =
173		clip_boxes[i].y1 + lcg_rand_n (src_height - clip_boxes[i].y1);
174
175	    if (verbose)
176	    {
177		printf ("source clip box: [%d,%d-%d,%d]\n",
178		        clip_boxes[i].x1, clip_boxes[i].y1,
179		        clip_boxes[i].x2, clip_boxes[i].y2);
180	    }
181	}
182
183	pixman_region_init_rects (&clip, clip_boxes, n);
184	pixman_image_set_clip_region (src_img, &clip);
185	pixman_image_set_source_clipping (src_img, 1);
186	pixman_region_fini (&clip);
187    }
188
189    if (lcg_rand_n (8) == 0)
190    {
191	pixman_box16_t clip_boxes[2];
192	int            n = lcg_rand_n (2) + 1;
193	for (i = 0; i < n; i++)
194	{
195	    clip_boxes[i].x1 = lcg_rand_n (dst_width);
196	    clip_boxes[i].y1 = lcg_rand_n (dst_height);
197	    clip_boxes[i].x2 =
198		clip_boxes[i].x1 + lcg_rand_n (dst_width - clip_boxes[i].x1);
199	    clip_boxes[i].y2 =
200		clip_boxes[i].y1 + lcg_rand_n (dst_height - clip_boxes[i].y1);
201
202	    if (verbose)
203	    {
204		printf ("destination clip box: [%d,%d-%d,%d]\n",
205		        clip_boxes[i].x1, clip_boxes[i].y1,
206		        clip_boxes[i].x2, clip_boxes[i].y2);
207	    }
208	}
209	pixman_region_init_rects (&clip, clip_boxes, n);
210	pixman_image_set_clip_region (dst_img, &clip);
211	pixman_region_fini (&clip);
212    }
213
214    pixman_image_composite (op, src_img, NULL, dst_img,
215                            src_x, src_y, 0, 0, dst_x, dst_y, w, h);
216
217    if (dst_fmt == PIXMAN_x8r8g8b8)
218    {
219	/* ignore unused part */
220	for (i = 0; i < dst_stride * dst_height / 4; i++)
221	    dstbuf[i] &= 0xFFFFFF;
222    }
223
224    image_endian_swap (dst_img, dst_bpp * 8);
225
226    if (verbose)
227    {
228	int j;
229
230	for (i = 0; i < dst_height; i++)
231	{
232	    for (j = 0; j < dst_stride; j++)
233		printf ("%02X ", *((uint8_t *)dstbuf + i * dst_stride + j));
234
235	    printf ("\n");
236	}
237    }
238
239    pixman_image_unref (src_img);
240    pixman_image_unref (dst_img);
241
242    crc32 = compute_crc32 (initcrc, dstbuf, dst_stride * dst_height);
243    free (srcbuf);
244    free (dstbuf);
245    return crc32;
246}
247
248int
249main (int   argc, char *argv[])
250{
251    int      i, n = 0;
252    uint32_t crc = 0;
253
254    pixman_disable_out_of_bounds_workaround ();
255
256    if (argc >= 2)
257	n = atoi (argv[1]);
258
259    if (n == 0) n = 3000000;
260
261    if (n < 0)
262    {
263	crc = test_composite (0, -n, 1);
264	printf ("crc32=%08X\n", crc);
265    }
266    else
267    {
268	for (i = 1; i <= n; i++)
269	    crc = test_composite (crc, i, 0);
270
271	printf ("crc32=%08X\n", crc);
272
273	if (n == 3000000)
274	{
275	    /* predefined value for running with all the fastpath functions disabled  */
276	    /* it needs to be updated every time changes are introduced to this program! */
277
278	    if (crc == 0x2168ACD1)
279	    {
280		printf ("scaling test passed\n");
281	    }
282	    else
283	    {
284		printf ("scaling test failed!\n");
285		return 1;
286	    }
287	}
288    }
289
290    return 0;
291}
292