5 #include <X11/Xutil.h> /* for XDestroyImage */
6 #include <pixman.h> /* for pixman blt functions */
12 const uint32_t *real, const uint32_t *ref,
13 int x, int y, int w, int h)
17 for (j = y - 2; j <= y + 2; j++) {
21 for (i = x - 2; i <= x + 2; i++) {
25 len += sprintf(buf+len, "%08x ", real[j*w+i]);
28 len += sprintf(buf+len, "\t");
30 for (i = x - 2; i <= x + 2; i++) {
34 len += sprintf(buf+len, "%08x ", ref[j*w+i]);
37 len += sprintf(buf+len, "\n");
41 static void fill_rect(struct test_display *t,
43 XRenderPictFormat *format,
44 int use_window, int tx, int ty,
45 uint8_t op, int x, int y, int w, int h,
46 uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
53 XSetWindowAttributes attr;
55 attr.override_redirect = 1;
56 tmp = XCreateWindow(t->dpy, DefaultRootWindow(t->dpy),
62 DefaultScreen(t->dpy)),
63 CWOverrideRedirect, &attr);
64 XMapWindow(t->dpy, tmp);
66 tmp = XCreatePixmap(t->dpy, DefaultRootWindow(t->dpy),
69 src = XRenderCreatePicture(t->dpy, tmp, format, 0, NULL);
70 color.red = red * alpha;
71 color.green = green * alpha;
72 color.blue = blue * alpha;
73 color.alpha = alpha << 8 | alpha;
74 XRenderFillRectangle(t->dpy, PictOpSrc, src, &color, 0, 0, w, h);
75 XRenderComposite(t->dpy, op, src, 0, p, 0, 0, 0, 0, x, y, w, h);
77 XRenderFreePicture(t->dpy, src);
79 XDestroyWindow(t->dpy, tmp);
81 XFreePixmap(t->dpy, tmp);
84 static void pixel_tests(struct test *t, int reps, int sets, enum target target)
86 struct test_target tt;
88 uint32_t *cells = malloc(t->real.width*t->real.height*4);
91 } *pixels = malloc(reps*sizeof(*pixels));
94 test_target_create_render(&t->real, target, &tt);
96 printf("Testing setting of single pixels (%s): ",
97 test_target_name(target));
100 for (s = 0; s < sets; s++) {
101 for (r = 0; r < reps; r++) {
102 int x = rand() % (tt.width - 1);
103 int y = rand() % (tt.height - 1);
104 uint8_t red = rand();
105 uint8_t green = rand();
106 uint8_t blue = rand();
107 uint8_t alpha = rand();
109 fill_rect(&t->real, tt.picture, tt.format,
111 PictOpSrc, x, y, 1, 1,
112 red, green, blue, alpha);
116 cells[y*tt.width+x] = color(red, green, blue, alpha);
119 test_init_image(&image, &t->real.shm, tt.format, 1, 1);
121 for (r = 0; r < reps; r++) {
122 uint32_t x = pixels[r].x;
123 uint32_t y = pixels[r].y;
126 XShmGetImage(t->real.dpy, tt.draw, &image,
129 result = *(uint32_t *)image.data;
130 if (!pixel_equal(image.depth, result,
131 cells[y*tt.width+x])) {
132 uint32_t mask = depth_mask(image.depth);
134 die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead\n",
136 cells[y*tt.width+x] & mask,
143 printf("passed [%d iterations x %d]\n", reps, sets);
145 test_target_destroy_render(&t->real, &tt);
150 static void clear(struct test_display *dpy, struct test_target *tt)
152 XRenderColor render_color = {0};
153 XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
154 0, 0, tt->width, tt->height);
157 static void area_tests(struct test *t, int reps, int sets, enum target target)
159 struct test_target tt;
161 uint32_t *cells = calloc(sizeof(uint32_t), t->real.width*t->real.height);
164 printf("Testing area sets (%s): ", test_target_name(target));
167 test_target_create_render(&t->real, target, &tt);
168 clear(&t->real, &tt);
170 test_init_image(&image, &t->real.shm, tt.format, tt.width, tt.height);
172 for (s = 0; s < sets; s++) {
173 for (r = 0; r < reps; r++) {
174 int w = 1 + rand() % (tt.width - 1);
175 int h = 1 + rand() % (tt.height - 1);
176 uint8_t red = rand();
177 uint8_t green = rand();
178 uint8_t blue = rand();
179 uint8_t alpha = rand();
181 x = rand() % (2*tt.width) - tt.width;
182 y = rand() % (2*tt.height) - tt.height;
184 fill_rect(&t->real, tt.picture, tt.format,
186 PictOpSrc, x, y, w, h,
187 red, green, blue, alpha);
193 if (x >= tt.width || y >= tt.height)
196 if (x + w > tt.width)
198 if (y + h > tt.height)
200 if (w <= 0 || h <= 0)
203 pixman_fill(cells, tt.width, 32, x, y, w, h,
204 color(red, green, blue, alpha));
207 XShmGetImage(t->real.dpy, tt.draw, &image, 0, 0, AllPlanes);
209 for (y = 0; y < tt.height; y++) {
210 for (x = 0; x < tt.width; x++) {
211 uint32_t result = *(uint32_t *)
213 y*image.bytes_per_line +
214 x*image.bits_per_pixel/8);
215 if (!pixel_equal(image.depth, result, cells[y*tt.width+x])) {
217 uint32_t mask = depth_mask(image.depth);
219 (uint32_t*)image.data, cells,
220 x, y, tt.width, tt.height);
222 die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead\n%s",
224 cells[y*tt.width+x] & mask,
233 printf("passed [%d iterations x %d]\n", reps, sets);
235 test_target_destroy_render(&t->real, &tt);
239 static void rect_tests(struct test *t, int reps, int sets, enum target target, int use_window)
241 struct test_target real, ref;
243 printf("Testing area fills (%s, using %s source): ",
244 test_target_name(target), use_window ? "window" : "pixmap");
247 test_target_create_render(&t->real, target, &real);
248 clear(&t->real, &real);
250 test_target_create_render(&t->ref, target, &ref);
251 clear(&t->ref, &ref);
253 for (s = 0; s < sets; s++) {
254 for (r = 0; r < reps; r++) {
257 uint8_t red = rand();
258 uint8_t green = rand();
259 uint8_t blue = rand();
260 uint8_t alpha = rand();
264 x = rand() % (real.width - 1);
265 y = rand() % (real.height - 1);
266 w = 1 + rand() % (real.width - x - 1);
267 h = 1 + rand() % (real.height - y - 1);
268 tmpx = w == real.width ? 0 : rand() % (real.width - w);
269 tmpy = h == real.height ? 0 : rand() % (real.height - h);
270 } while (((tmpx+w > x && tmpx < x+w) ||
271 (tmpy+h > y && tmpy < y+h)) &&
276 fill_rect(&t->real, real.picture, real.format,
277 use_window, tmpx, tmpy,
278 PictOpSrc, x, y, w, h,
279 red, green, blue, alpha);
280 fill_rect(&t->ref, ref.picture, ref.format,
281 use_window, tmpx, tmpy,
282 PictOpSrc, x, y, w, h,
283 red, green, blue, alpha);
288 real.draw, real.format,
289 ref.draw, ref.format,
290 0, 0, real.width, real.height,
294 printf("passed [%d iterations x %d]\n", reps, sets);
296 test_target_destroy_render(&t->real, &real);
297 test_target_destroy_render(&t->ref, &ref);
300 int main(int argc, char **argv)
305 test_init(&test, argc, argv);
307 for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
309 int sets = 1 << (12 - i);
315 for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
316 pixel_tests(&test, reps, sets, t);
317 area_tests(&test, reps, sets, t);
318 rect_tests(&test, reps, sets, t, 0);
320 rect_tests(&test, reps, sets, t, 1);