1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#define WIDTH 48
#define HEIGHT 48
static const pixman_format_code_t formats[] =
{
PIXMAN_a8r8g8b8,
PIXMAN_a2r10g10b10,
PIXMAN_a4r4g4b4,
PIXMAN_a8,
PIXMAN_rgba_float,
};
static const pixman_format_code_t alpha_formats[] =
{
PIXMAN_null,
PIXMAN_a8,
PIXMAN_a2r10g10b10,
PIXMAN_a4r4g4b4,
PIXMAN_rgba_float,
};
static const int origins[] =
{
0, 10, -100
};
static void
on_destroy (pixman_image_t *image, void *data)
{
uint32_t *bits = pixman_image_get_data (image);
fence_free (bits);
}
static pixman_image_t *
make_image (pixman_format_code_t format)
{
uint32_t *bits;
uint8_t bpp = PIXMAN_FORMAT_BPP (format) / 8;
pixman_image_t *image;
if (format != PIXMAN_rgba_float)
bits = (uint32_t *)make_random_bytes (WIDTH * HEIGHT * bpp);
else
bits = (uint32_t *)make_random_floats (WIDTH * HEIGHT * bpp);
image = pixman_image_create_bits (format, WIDTH, HEIGHT, bits, WIDTH * bpp);
if (image && bits)
pixman_image_set_destroy_function (image, on_destroy, NULL);
return image;
}
static float
get_alpha (pixman_image_t *image, int x, int y, int orig_x, int orig_y)
{
uint8_t *bits;
uint32_t r;
if (image->common.alpha_map)
{
if (x - orig_x >= 0 && x - orig_x < WIDTH &&
y - orig_y >= 0 && y - orig_y < HEIGHT)
{
image = (pixman_image_t *)image->common.alpha_map;
x -= orig_x;
y -= orig_y;
}
else
{
return 0.f;
}
}
bits = (uint8_t *)image->bits.bits;
if (image->bits.format == PIXMAN_a8)
{
r = bits[y * WIDTH + x];
return r / 255.f;
}
else if (image->bits.format == PIXMAN_a2r10g10b10)
{
r = ((uint32_t *)bits)[y * WIDTH + x] >> 30;
return r / 3.f;
}
else if (image->bits.format == PIXMAN_a8r8g8b8)
{
r = ((uint32_t *)bits)[y * WIDTH + x] >> 24;
return r / 255.f;
}
else if (image->bits.format == PIXMAN_a4r4g4b4)
{
r = ((uint16_t *)bits)[y * WIDTH + x] >> 12;
return r / 15.f;
}
else if (image->bits.format == PIXMAN_rgba_float)
{
return ((float *)bits)[y * WIDTH * 4 + x * 4 + 3];
}
else
{
assert (0);
return 0.f;
}
}
static uint16_t
get_red (pixman_image_t *image, int x, int y, int orig_x, int orig_y)
{
uint8_t *bits;
uint16_t r;
bits = (uint8_t *)image->bits.bits;
if (image->bits.format == PIXMAN_a8)
{
r = 0x00;
}
else if (image->bits.format == PIXMAN_a2r10g10b10)
{
r = ((uint32_t *)bits)[y * WIDTH + x] >> 14;
r &= 0xffc0;
r |= (r >> 10);
}
else if (image->bits.format == PIXMAN_a8r8g8b8)
{
r = ((uint32_t *)bits)[y * WIDTH + x] >> 16;
r &= 0xff;
r |= r << 8;
}
else if (image->bits.format == PIXMAN_a4r4g4b4)
{
r = ((uint16_t *)bits)[y * WIDTH + x] >> 8;
r &= 0xf;
r |= r << 4;
r |= r << 8;
}
else if (image->bits.format == PIXMAN_rgba_float)
{
double tmp = ((float *)bits)[y * WIDTH * 4 + x * 4];
return tmp * 65535.;
}
else
{
assert (0);
}
return r;
}
static float get_alpha_err(pixman_format_code_t sf, pixman_format_code_t saf,
pixman_format_code_t df, pixman_format_code_t daf)
{
pixman_format_code_t s = saf != PIXMAN_null ? saf : sf;
pixman_format_code_t d = daf != PIXMAN_null ? daf : df;
/* There are cases where we go through the 8 bit compositing
* path even with 10bpc and higher formats.
*/
if (PIXMAN_FORMAT_A(s) == PIXMAN_FORMAT_A(d))
return 1.f / 255.f;
else if (PIXMAN_FORMAT_A(s) > PIXMAN_FORMAT_A(d))
return 1.f / ((1 << PIXMAN_FORMAT_A(d)) - 1);
else
return 1.f / ((1 << PIXMAN_FORMAT_A(s)) - 1);
}
static int
run_test (int s, int d, int sa, int da, int soff, int doff)
{
pixman_format_code_t sf = formats[s];
pixman_format_code_t df = formats[d];
pixman_format_code_t saf = alpha_formats[sa];
pixman_format_code_t daf = alpha_formats[da];
pixman_image_t *src, *dst, *orig_dst, *alpha, *orig_alpha;
pixman_transform_t t1;
int j, k;
int n_red_bits;
soff = origins[soff];
doff = origins[doff];
n_red_bits = PIXMAN_FORMAT_R (df);
/* Source */
src = make_image (sf);
if (saf != PIXMAN_null)
{
alpha = make_image (saf);
pixman_image_set_alpha_map (src, alpha, soff, soff);
pixman_image_unref (alpha);
}
/* Destination */
orig_dst = make_image (df);
dst = make_image (df);
pixman_image_composite (PIXMAN_OP_SRC, orig_dst, NULL, dst,
0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
if (daf != PIXMAN_null)
{
orig_alpha = make_image (daf);
alpha = make_image (daf);
pixman_image_composite (PIXMAN_OP_SRC, orig_alpha, NULL, alpha,
0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
pixman_image_set_alpha_map (orig_dst, orig_alpha, doff, doff);
pixman_image_set_alpha_map (dst, alpha, doff, doff);
pixman_image_unref (orig_alpha);
pixman_image_unref (alpha);
}
/* Transformations, repeats and filters on destinations should be ignored,
* so just set some random ones.
*/
pixman_transform_init_identity (&t1);
pixman_transform_scale (&t1, NULL, pixman_int_to_fixed (100), pixman_int_to_fixed (11));
pixman_transform_rotate (&t1, NULL, pixman_double_to_fixed (0.5), pixman_double_to_fixed (0.11));
pixman_transform_translate (&t1, NULL, pixman_int_to_fixed (11), pixman_int_to_fixed (17));
pixman_image_set_transform (dst, &t1);
pixman_image_set_filter (dst, PIXMAN_FILTER_BILINEAR, NULL, 0);
pixman_image_set_repeat (dst, PIXMAN_REPEAT_REFLECT);
pixman_image_composite (PIXMAN_OP_ADD, src, NULL, dst,
0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
for (j = MAX (doff, 0); j < MIN (HEIGHT, HEIGHT + doff); ++j)
{
for (k = MAX (doff, 0); k < MIN (WIDTH, WIDTH + doff); ++k)
{
float sa, da, oda, refa;
uint16_t sr, dr, odr, refr;
float err;
err = get_alpha_err(sf, saf, df, daf);
sa = get_alpha (src, k, j, soff, soff);
da = get_alpha (dst, k, j, doff, doff);
oda = get_alpha (orig_dst, k, j, doff, doff);
if (sa + oda > 1.f)
refa = 1.f;
else
refa = sa + oda;
if (da - err > refa ||
da + err < refa)
{
printf ("\nWrong alpha value at (%d, %d). Should be %g; got %g. Source was %g, original dest was %g\n",
k, j, refa, da, sa, oda);
printf ("src: %s, alpha: %s, origin %d %d\ndst: %s, alpha: %s, origin: %d %d\n\n",
format_name (sf),
format_name (saf),
soff, soff,
format_name (df),
format_name (daf),
doff, doff);
return 1;
}
/* There are cases where we go through the 8 bit compositing
* path even with 10bpc formats. This results in incorrect
* results here, so only do the red check for narrow formats
*/
if (n_red_bits <= 8)
{
sr = get_red (src, k, j, soff, soff);
dr = get_red (dst, k, j, doff, doff);
odr = get_red (orig_dst, k, j, doff, doff);
if (sr + odr > 0xffff)
refr = 0xffff;
else
refr = sr + odr;
if (abs ((dr >> (16 - n_red_bits)) - (refr >> (16 - n_red_bits))) > 1)
{
printf ("%d red bits\n", n_red_bits);
printf ("\nWrong red value at (%d, %d). Should be 0x%x; got 0x%x. Source was 0x%x, original dest was 0x%x\n",
k, j, refr, dr, sr, odr);
printf ("src: %s, alpha: %s, origin %d %d\ndst: %s, alpha: %s, origin: %d %d\n\n",
format_name (sf),
format_name (saf),
soff, soff,
format_name (df),
format_name (daf),
doff, doff);
return 1;
}
}
}
}
pixman_image_set_alpha_map (src, NULL, 0, 0);
pixman_image_set_alpha_map (dst, NULL, 0, 0);
pixman_image_set_alpha_map (orig_dst, NULL, 0, 0);
pixman_image_unref (src);
pixman_image_unref (dst);
pixman_image_unref (orig_dst);
return 0;
}
int
main (int argc, char **argv)
{
int i, j, a, b, x, y;
prng_srand (0);
for (i = 0; i < ARRAY_LENGTH (formats); ++i)
{
for (j = 0; j < ARRAY_LENGTH (formats); ++j)
{
for (a = 0; a < ARRAY_LENGTH (alpha_formats); ++a)
{
for (b = 0; b < ARRAY_LENGTH (alpha_formats); ++b)
{
for (x = 0; x < ARRAY_LENGTH (origins); ++x)
{
for (y = 0; y < ARRAY_LENGTH (origins); ++y)
{
if (run_test (i, j, a, b, x, y) != 0)
return 1;
}
}
}
}
}
}
return 0;
}
|