Merge branch 'trap-fixes'
[profile/ivi/pixman.git] / test / fetch-test.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "pixman.h"
5 #include <config.h>
6
7 #define SIZE 1024
8
9 static pixman_indexed_t mono_palette =
10 {
11     .rgba = { 0x00000000, 0x00ffffff },
12 };
13
14
15 typedef struct {
16     pixman_format_code_t format;
17     int width, height;
18     int stride;
19     uint32_t src[SIZE];
20     uint32_t dst[SIZE];
21     pixman_indexed_t *indexed;
22 } testcase_t;
23
24 static testcase_t testcases[] =
25 {
26     {
27         .format = PIXMAN_a8r8g8b8,
28         .width = 2, .height = 2,
29         .stride = 8,
30         .src = { 0x00112233, 0x44556677,
31                  0x8899aabb, 0xccddeeff },
32         .dst = { 0x00112233, 0x44556677,
33                  0x8899aabb, 0xccddeeff },
34         .indexed = NULL,
35     },
36     {
37         .format = PIXMAN_g1,
38         .width = 8, .height = 2,
39         .stride = 4,
40 #ifdef WORDS_BIGENDIAN
41         .src =
42         {
43             0xaa000000,
44             0x55000000
45         },
46 #else
47         .src =
48         {
49             0x00000055,
50             0x000000aa
51         },
52 #endif
53         .dst =
54         {
55             0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000,
56             0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff
57         },
58         .indexed = &mono_palette,
59     },
60 #if 0
61     {
62         .format = PIXMAN_g8,
63         .width = 4, .height = 2,
64         .stride = 4,
65         .src = { 0x01234567,
66                  0x89abcdef },
67         .dst = { 0x00010101, 0x00232323, 0x00454545, 0x00676767,
68                  0x00898989, 0x00ababab, 0x00cdcdcd, 0x00efefef, },
69     },
70 #endif
71     /* FIXME: make this work on big endian */
72     {
73         .format = PIXMAN_yv12,
74         .width = 8, .height = 2,
75         .stride = 8,
76 #ifdef WORDS_BIGENDIAN
77         .src =
78         {
79             0x00ff00ff, 0x00ff00ff,
80             0xff00ff00, 0xff00ff00,
81             0x80ff8000,
82             0x800080ff
83         },
84 #else
85         .src =
86         {
87             0xff00ff00, 0xff00ff00,
88             0x00ff00ff, 0x00ff00ff,
89             0x0080ff80,
90             0xff800080
91         },
92 #endif
93         .dst =
94         {
95             0xff000000, 0xffffffff, 0xffb80000, 0xffffe113,
96             0xff000000, 0xffffffff, 0xff0023ee, 0xff4affff,
97             0xffffffff, 0xff000000, 0xffffe113, 0xffb80000,
98             0xffffffff, 0xff000000, 0xff4affff, 0xff0023ee,
99         },
100     },
101 };
102
103 int n_test_cases = sizeof(testcases)/sizeof(testcases[0]);
104
105
106 static uint32_t
107 reader (const void *src, int size)
108 {
109     switch (size)
110     {
111     case 1:
112         return *(uint8_t *)src;
113     case 2:
114         return *(uint16_t *)src;
115     case 4:
116         return *(uint32_t *)src;
117     default:
118         assert(0);
119     }
120 }
121
122
123 static void
124 writer (void *src, uint32_t value, int size)
125 {
126     switch (size)
127     {
128     case 1:
129         *(uint8_t *)src = value;
130         break;
131     case 2:
132         *(uint16_t *)src = value;
133         break;
134     case 4:
135         *(uint32_t *)src = value;
136         break;
137     default:
138         assert(0);
139     }
140 }
141
142
143 int
144 main (int argc, char **argv)
145 {
146     uint32_t dst[SIZE];
147     pixman_image_t *src_img;
148     pixman_image_t *dst_img;
149     int i, j, x, y;
150     int ret = 0;
151
152     for (i = 0; i < n_test_cases; ++i)
153     {
154         for (j = 0; j < 2; ++j)
155         {
156             src_img = pixman_image_create_bits (testcases[i].format,
157                                                 testcases[i].width,
158                                                 testcases[i].height,
159                                                 testcases[i].src,
160                                                 testcases[i].stride);
161             pixman_image_set_indexed(src_img, testcases[i].indexed);
162
163             dst_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
164                                                 testcases[i].width,
165                                                 testcases[i].height,
166                                                 dst,
167                                                 testcases[i].width*4);
168
169             if (j)
170             {
171                 pixman_image_set_accessors (src_img, reader, writer);
172                 pixman_image_set_accessors (dst_img, reader, writer);
173             }
174
175             pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img,
176                                     0, 0, 0, 0, 0, 0, testcases[i].width, testcases[i].height);
177
178             pixman_image_unref (src_img);
179             pixman_image_unref (dst_img);
180
181             for (y = 0; y < testcases[i].height; ++y)
182             {
183                 for (x = 0; x < testcases[i].width; ++x)
184                 {
185                     int offset = y * testcases[i].width + x;
186
187                     if (dst[offset] != testcases[i].dst[offset])
188                     {
189                         printf ("test %i%c: pixel mismatch at (x=%d,y=%d): %08x expected, %08x obtained\n",
190                                 i + 1, 'a' + j,
191                                 x, y,
192                                 testcases[i].dst[offset], dst[offset]);
193                         ret = 1;
194                     }
195                 }
196             }
197         }
198     }
199
200     return ret;
201 }