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, Picture p,
42 int x, int y, int w, int h,
43 uint8_t red, uint8_t green, uint8_t blue)
48 XRenderPictFormat *format;
50 format = XRenderFindStandardFormat(t->dpy, PictStandardRGB24);
52 tmp = XCreatePixmap(t->dpy, DefaultRootWindow(t->dpy),
55 src = XRenderCreatePicture(t->dpy, tmp, format, 0, NULL);
56 c.red = (int)red << 8 | red;
57 c.green = (int)green << 8 | green;
58 c.blue = (int)blue << 8 | blue;
60 XRenderFillRectangle(t->dpy, PictOpSrc, src, &c, 0, 0, w, h);
61 XRenderComposite(t->dpy, PictOpOver, src, 0, p, 0, 0, 0, 0, x, y, w, h);
63 XRenderFreePicture(t->dpy, src);
64 XFreePixmap(t->dpy, tmp);
67 static void pixel_tests(struct test *t, int reps, int sets, enum target target)
69 struct test_target tt;
71 uint32_t *cells = malloc(t->real.width*t->real.height*4);
74 } *pixels = malloc(reps*sizeof(*pixels));
77 test_target_create_render(&t->real, target, &tt);
79 printf("Testing setting of single pixels (%s): ",
80 test_target_name(target));
83 for (s = 0; s < sets; s++) {
84 for (r = 0; r < reps; r++) {
85 int x = rand() % (tt.width - 1);
86 int y = rand() % (tt.height - 1);
88 uint8_t green = rand();
89 uint8_t blue = rand();
91 fill_rect(&t->real, tt.picture,
97 cells[y*tt.width+x] = color(red, green, blue, 0xff);
100 test_init_image(&image, &t->real.shm, tt.format, 1, 1);
102 for (r = 0; r < reps; r++) {
103 uint32_t x = pixels[r].x;
104 uint32_t y = pixels[r].y;
107 XShmGetImage(t->real.dpy, tt.draw, &image,
110 result = *(uint32_t *)image.data;
111 if (!pixel_equal(image.depth, result,
112 cells[y*tt.width+x])) {
113 uint32_t mask = depth_mask(image.depth);
115 die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead\n",
117 cells[y*tt.width+x] & mask,
124 printf("passed [%d iterations x %d]\n", reps, sets);
126 test_target_destroy_render(&t->real, &tt);
131 static void clear(struct test_display *dpy, struct test_target *tt)
133 XRenderColor render_color = {0};
134 XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
135 0, 0, tt->width, tt->height);
138 static void area_tests(struct test *t, int reps, int sets, enum target target)
140 struct test_target tt;
142 uint32_t *cells = calloc(sizeof(uint32_t), t->real.width*t->real.height);
145 printf("Testing area sets (%s): ", test_target_name(target));
148 test_target_create_render(&t->real, target, &tt);
149 clear(&t->real, &tt);
151 test_init_image(&image, &t->real.shm, tt.format, tt.width, tt.height);
153 for (s = 0; s < sets; s++) {
154 for (r = 0; r < reps; r++) {
155 int w = 1 + rand() % (tt.width - 1);
156 int h = 1 + rand() % (tt.height - 1);
157 uint8_t red = rand();
158 uint8_t green = rand();
159 uint8_t blue = rand();
161 x = rand() % (2*tt.width) - tt.width;
162 y = rand() % (2*tt.height) - tt.height;
164 fill_rect(&t->real, tt.picture,
172 if (x >= tt.width || y >= tt.height)
175 if (x + w > tt.width)
177 if (y + h > tt.height)
179 if (w <= 0 || h <= 0)
182 pixman_fill(cells, tt.width, 32, x, y, w, h,
183 color(red, green, blue, 0xff));
186 XShmGetImage(t->real.dpy, tt.draw, &image, 0, 0, AllPlanes);
188 for (y = 0; y < tt.height; y++) {
189 for (x = 0; x < tt.width; x++) {
190 uint32_t result = *(uint32_t *)
192 y*image.bytes_per_line +
193 x*image.bits_per_pixel/8);
194 if (!pixel_equal(image.depth, result, cells[y*tt.width+x])) {
196 uint32_t mask = depth_mask(image.depth);
198 (uint32_t*)image.data, cells,
199 x, y, tt.width, tt.height);
201 die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead (set %d, reps %d)\n%s",
203 cells[y*tt.width+x] & mask,
206 result, s, reps, buf);
212 printf("passed [%d iterations x %d]\n", reps, sets);
214 test_target_destroy_render(&t->real, &tt);
218 static void rect_tests(struct test *t, int reps, int sets, enum target target, int use_window)
220 struct test_target real, ref;
222 printf("Testing area fills (%s, using %s source): ",
223 test_target_name(target), use_window ? "window" : "pixmap");
226 test_target_create_render(&t->real, target, &real);
227 clear(&t->real, &real);
229 test_target_create_render(&t->ref, target, &ref);
230 clear(&t->ref, &ref);
232 for (s = 0; s < sets; s++) {
233 for (r = 0; r < reps; r++) {
235 uint8_t red = rand();
236 uint8_t green = rand();
237 uint8_t blue = rand();
239 x = rand() % (real.width - 1);
240 y = rand() % (real.height - 1);
241 w = 1 + rand() % (real.width - x - 1);
242 h = 1 + rand() % (real.height - y - 1);
244 fill_rect(&t->real, real.picture,
247 fill_rect(&t->ref, ref.picture,
253 real.draw, real.format,
254 ref.draw, ref.format,
255 0, 0, real.width, real.height,
259 printf("passed [%d iterations x %d]\n", reps, sets);
261 test_target_destroy_render(&t->real, &real);
262 test_target_destroy_render(&t->ref, &ref);
265 int main(int argc, char **argv)
270 test_init(&test, argc, argv);
272 for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
274 int sets = 1 << (12 - i);
279 pixel_tests(&test, reps, sets, PIXMAP);
280 area_tests(&test, reps, sets, PIXMAP);
281 rect_tests(&test, reps, sets, PIXMAP, 0);