1b4b94579Smrg#include <assert.h>
2b4b94579Smrg#include <stdlib.h>
3b4b94579Smrg#include <stdio.h>
49ad247e8Sjmcneill#include "utils.h"
5b4b94579Smrg
6b4b94579Smrg#define SIZE 1024
7b4b94579Smrg
8952204abSmrgstatic pixman_indexed_t mono_palette =
9952204abSmrg{
106ba797d6Smrg    0, { 0x00000000, 0x00ffffff },
11b4b94579Smrg};
12b4b94579Smrg
13b4b94579Smrg
14b4b94579Smrgtypedef struct {
15b4b94579Smrg    pixman_format_code_t format;
16b4b94579Smrg    int width, height;
17b4b94579Smrg    int stride;
18b4b94579Smrg    uint32_t src[SIZE];
19b4b94579Smrg    uint32_t dst[SIZE];
20b4b94579Smrg    pixman_indexed_t *indexed;
21b4b94579Smrg} testcase_t;
22b4b94579Smrg
23952204abSmrgstatic testcase_t testcases[] =
24952204abSmrg{
25b4b94579Smrg    {
266ba797d6Smrg	PIXMAN_a8r8g8b8,
276ba797d6Smrg	2, 2,
286ba797d6Smrg	8,
296ba797d6Smrg	{ 0x00112233, 0x44556677,
306ba797d6Smrg	  0x8899aabb, 0xccddeeff },
316ba797d6Smrg	{ 0x00112233, 0x44556677,
326ba797d6Smrg	  0x8899aabb, 0xccddeeff },
336ba797d6Smrg	NULL,
34b4b94579Smrg    },
35b4b94579Smrg    {
366ba797d6Smrg	PIXMAN_r8g8b8a8,
376ba797d6Smrg	2, 2,
386ba797d6Smrg	8,
396ba797d6Smrg	{ 0x11223300, 0x55667744,
406ba797d6Smrg	  0x99aabb88, 0xddeeffcc },
416ba797d6Smrg	{ 0x00112233, 0x44556677,
426ba797d6Smrg	  0x8899aabb, 0xccddeeff },
436ba797d6Smrg	NULL,
446ba797d6Smrg    },
456ba797d6Smrg    {
466ba797d6Smrg	PIXMAN_g1,
476ba797d6Smrg	8, 2,
486ba797d6Smrg	4,
49a450e446Smrg#ifdef WORDS_BIGENDIAN
50952204abSmrg	{
51952204abSmrg	    0xaa000000,
52952204abSmrg	    0x55000000
53952204abSmrg	},
54a450e446Smrg#else
55952204abSmrg	{
56952204abSmrg	    0x00000055,
57952204abSmrg	    0x000000aa
58952204abSmrg	},
59a450e446Smrg#endif
60952204abSmrg	{
61952204abSmrg	    0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000,
62952204abSmrg	    0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff
63952204abSmrg	},
646ba797d6Smrg	&mono_palette,
65b4b94579Smrg    },
66b4b94579Smrg#if 0
67b4b94579Smrg    {
686ba797d6Smrg	PIXMAN_g8,
696ba797d6Smrg	4, 2,
706ba797d6Smrg	4,
716ba797d6Smrg	{ 0x01234567,
726ba797d6Smrg	  0x89abcdef },
736ba797d6Smrg	{ 0x00010101, 0x00232323, 0x00454545, 0x00676767,
746ba797d6Smrg	  0x00898989, 0x00ababab, 0x00cdcdcd, 0x00efefef, },
75b4b94579Smrg    },
76b4b94579Smrg#endif
77a450e446Smrg    /* FIXME: make this work on big endian */
78b4b94579Smrg    {
796ba797d6Smrg	PIXMAN_yv12,
806ba797d6Smrg	8, 2,
816ba797d6Smrg	8,
82a450e446Smrg#ifdef WORDS_BIGENDIAN
83952204abSmrg	{
84952204abSmrg	    0x00ff00ff, 0x00ff00ff,
85952204abSmrg	    0xff00ff00, 0xff00ff00,
86952204abSmrg	    0x80ff8000,
87952204abSmrg	    0x800080ff
88a450e446Smrg	},
89a450e446Smrg#else
90952204abSmrg	{
91952204abSmrg	    0xff00ff00, 0xff00ff00,
92952204abSmrg	    0x00ff00ff, 0x00ff00ff,
93952204abSmrg	    0x0080ff80,
94952204abSmrg	    0xff800080
95952204abSmrg	},
96a450e446Smrg#endif
97952204abSmrg	{
98952204abSmrg	    0xff000000, 0xffffffff, 0xffb80000, 0xffffe113,
99952204abSmrg	    0xff000000, 0xffffffff, 0xff0023ee, 0xff4affff,
100952204abSmrg	    0xffffffff, 0xff000000, 0xffffe113, 0xffb80000,
101952204abSmrg	    0xffffffff, 0xff000000, 0xff4affff, 0xff0023ee,
102b4b94579Smrg	},
103b4b94579Smrg    },
104b4b94579Smrg};
105b4b94579Smrg
1069ad247e8Sjmcneillint n_test_cases = ARRAY_LENGTH (testcases);
107b4b94579Smrg
108b4b94579Smrg
109b4b94579Smrgstatic uint32_t
110b4b94579Smrgreader (const void *src, int size)
111b4b94579Smrg{
112b4b94579Smrg    switch (size)
113b4b94579Smrg    {
114b4b94579Smrg    case 1:
115b4b94579Smrg	return *(uint8_t *)src;
116b4b94579Smrg    case 2:
117b4b94579Smrg	return *(uint16_t *)src;
118b4b94579Smrg    case 4:
119b4b94579Smrg	return *(uint32_t *)src;
120b4b94579Smrg    default:
121b4b94579Smrg	assert(0);
1226ba797d6Smrg	return 0; /* silence MSVC */
123b4b94579Smrg    }
124b4b94579Smrg}
125b4b94579Smrg
126b4b94579Smrg
127b4b94579Smrgstatic void
128b4b94579Smrgwriter (void *src, uint32_t value, int size)
129b4b94579Smrg{
130b4b94579Smrg    switch (size)
131b4b94579Smrg    {
132b4b94579Smrg    case 1:
133b4b94579Smrg	*(uint8_t *)src = value;
134b4b94579Smrg	break;
135b4b94579Smrg    case 2:
136b4b94579Smrg	*(uint16_t *)src = value;
137b4b94579Smrg	break;
138b4b94579Smrg    case 4:
139b4b94579Smrg	*(uint32_t *)src = value;
140b4b94579Smrg	break;
141b4b94579Smrg    default:
142b4b94579Smrg	assert(0);
143b4b94579Smrg    }
144b4b94579Smrg}
145b4b94579Smrg
146b4b94579Smrg
147b4b94579Smrgint
148b4b94579Smrgmain (int argc, char **argv)
149b4b94579Smrg{
150b4b94579Smrg    uint32_t dst[SIZE];
151b4b94579Smrg    pixman_image_t *src_img;
152b4b94579Smrg    pixman_image_t *dst_img;
153b4b94579Smrg    int i, j, x, y;
154b4b94579Smrg    int ret = 0;
155b4b94579Smrg
156952204abSmrg    for (i = 0; i < n_test_cases; ++i)
157952204abSmrg    {
158952204abSmrg	for (j = 0; j < 2; ++j)
159952204abSmrg	{
160b4b94579Smrg	    src_img = pixman_image_create_bits (testcases[i].format,
161952204abSmrg						testcases[i].width,
162b4b94579Smrg						testcases[i].height,
163b4b94579Smrg						testcases[i].src,
164b4b94579Smrg						testcases[i].stride);
165b4b94579Smrg	    pixman_image_set_indexed(src_img, testcases[i].indexed);
166b4b94579Smrg
167b4b94579Smrg	    dst_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
168952204abSmrg						testcases[i].width,
169b4b94579Smrg						testcases[i].height,
170b4b94579Smrg						dst,
171b4b94579Smrg						testcases[i].width*4);
172b4b94579Smrg
173952204abSmrg	    if (j)
174952204abSmrg	    {
175b4b94579Smrg		pixman_image_set_accessors (src_img, reader, writer);
176b4b94579Smrg		pixman_image_set_accessors (dst_img, reader, writer);
177b4b94579Smrg	    }
178952204abSmrg
179b4b94579Smrg	    pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img,
180b4b94579Smrg				    0, 0, 0, 0, 0, 0, testcases[i].width, testcases[i].height);
181b4b94579Smrg
182b4b94579Smrg	    pixman_image_unref (src_img);
183b4b94579Smrg	    pixman_image_unref (dst_img);
184b4b94579Smrg
185b4b94579Smrg	    for (y = 0; y < testcases[i].height; ++y)
186952204abSmrg	    {
187952204abSmrg		for (x = 0; x < testcases[i].width; ++x)
188952204abSmrg		{
189952204abSmrg		    int offset = y * testcases[i].width + x;
190952204abSmrg
191952204abSmrg		    if (dst[offset] != testcases[i].dst[offset])
192952204abSmrg		    {
193b4b94579Smrg			printf ("test %i%c: pixel mismatch at (x=%d,y=%d): %08x expected, %08x obtained\n",
194b4b94579Smrg			        i + 1, 'a' + j,
195952204abSmrg			        x, y,
196b4b94579Smrg			        testcases[i].dst[offset], dst[offset]);
197b4b94579Smrg			ret = 1;
198b4b94579Smrg		    }
199b4b94579Smrg		}
200952204abSmrg	    }
201b4b94579Smrg	}
202b4b94579Smrg    }
203952204abSmrg
204b4b94579Smrg    return ret;
205b4b94579Smrg}
206