1#include <stdint.h> 2#include <stdio.h> 3#include <stdlib.h> 4 5#include <X11/Xutil.h> /* for XDestroyImage */ 6#include <pixman.h> /* for pixman blt functions */ 7 8#include "test.h" 9 10static void 11show_cells(char *buf, 12 const uint32_t *out, const uint32_t *ref, 13 int x, int y, int w, int h) 14{ 15 int i, j, len = 0; 16 17 for (j = y - 2; j <= y + 2; j++) { 18 if (j < 0 || j >= h) 19 continue; 20 21 for (i = x - 2; i <= x + 2; i++) { 22 if (i < 0 || i >= w) 23 continue; 24 25 len += sprintf(buf+len, "%08x ", out[j*w+i]); 26 } 27 28 len += sprintf(buf+len, "\t"); 29 30 for (i = x - 2; i <= x + 2; i++) { 31 if (i < 0 || i >= w) 32 continue; 33 34 len += sprintf(buf+len, "%08x ", ref[j*w+i]); 35 } 36 37 len += sprintf(buf+len, "\n"); 38 } 39} 40 41static 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) 44{ 45 Drawable tmp; 46 XRenderColor c; 47 Picture src; 48 XRenderPictFormat *format; 49 50 format = XRenderFindStandardFormat(t->dpy, PictStandardRGB24); 51 52 tmp = XCreatePixmap(t->dpy, DefaultRootWindow(t->dpy), 53 w, h, format->depth); 54 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; 59 c.alpha = 0xffff; 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); 62 63 XRenderFreePicture(t->dpy, src); 64 XFreePixmap(t->dpy, tmp); 65} 66 67static void pixel_tests(struct test *t, int reps, int sets, enum target target) 68{ 69 struct test_target tt; 70 XImage image; 71 uint32_t *cells = malloc(t->out.width*t->out.height*4); 72 struct { 73 uint16_t x, y; 74 } *pixels = malloc(reps*sizeof(*pixels)); 75 int r, s; 76 77 test_target_create_render(&t->out, target, &tt); 78 79 printf("Testing setting of single pixels (%s): ", 80 test_target_name(target)); 81 fflush(stdout); 82 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); 87 uint8_t red = rand(); 88 uint8_t green = rand(); 89 uint8_t blue = rand(); 90 91 fill_rect(&t->out, tt.picture, 92 x, y, 1, 1, 93 red, green, blue); 94 95 pixels[r].x = x; 96 pixels[r].y = y; 97 cells[y*tt.width+x] = color(red, green, blue, 0xff); 98 } 99 100 test_init_image(&image, &t->out.shm, tt.format, 1, 1); 101 102 for (r = 0; r < reps; r++) { 103 uint32_t x = pixels[r].x; 104 uint32_t y = pixels[r].y; 105 uint32_t result; 106 107 XShmGetImage(t->out.dpy, tt.draw, &image, 108 x, y, AllPlanes); 109 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); 114 115 die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead\n", 116 x, y, 117 cells[y*tt.width+x] & mask, 118 cells[y*tt.width+x], 119 result & mask, 120 result); 121 } 122 } 123 } 124 printf("passed [%d iterations x %d]\n", reps, sets); 125 126 test_target_destroy_render(&t->out, &tt); 127 free(pixels); 128 free(cells); 129} 130 131static void clear(struct test_display *dpy, struct test_target *tt) 132{ 133 XRenderColor render_color = {0}; 134 XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color, 135 0, 0, tt->width, tt->height); 136} 137 138static void area_tests(struct test *t, int reps, int sets, enum target target) 139{ 140 struct test_target tt; 141 XImage image; 142 uint32_t *cells = calloc(sizeof(uint32_t), t->out.width*t->out.height); 143 int r, s, x, y; 144 145 printf("Testing area sets (%s): ", test_target_name(target)); 146 fflush(stdout); 147 148 test_target_create_render(&t->out, target, &tt); 149 clear(&t->out, &tt); 150 151 test_init_image(&image, &t->out.shm, tt.format, tt.width, tt.height); 152 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(); 160 161 x = rand() % (2*tt.width) - tt.width; 162 y = rand() % (2*tt.height) - tt.height; 163 164 fill_rect(&t->out, tt.picture, 165 x, y, w, h, 166 red, green, blue); 167 168 if (x < 0) 169 w += x, x = 0; 170 if (y < 0) 171 h += y, y = 0; 172 if (x >= tt.width || y >= tt.height) 173 continue; 174 175 if (x + w > tt.width) 176 w = tt.width - x; 177 if (y + h > tt.height) 178 h = tt.height - y; 179 if (w <= 0 || h <= 0) 180 continue; 181 182 pixman_fill(cells, tt.width, 32, x, y, w, h, 183 color(red, green, blue, 0xff)); 184 } 185 186 XShmGetImage(t->out.dpy, tt.draw, &image, 0, 0, AllPlanes); 187 188 for (y = 0; y < tt.height; y++) { 189 for (x = 0; x < tt.width; x++) { 190 uint32_t result = *(uint32_t *) 191 (image.data + 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])) { 195 char buf[600]; 196 uint32_t mask = depth_mask(image.depth); 197 show_cells(buf, 198 (uint32_t*)image.data, cells, 199 x, y, tt.width, tt.height); 200 201 die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead (set %d, reps %d)\n%s", 202 x, y, 203 cells[y*tt.width+x] & mask, 204 cells[y*tt.width+x], 205 result & mask, 206 result, s, reps, buf); 207 } 208 } 209 } 210 } 211 212 printf("passed [%d iterations x %d]\n", reps, sets); 213 214 test_target_destroy_render(&t->out, &tt); 215 free(cells); 216} 217 218static void rect_tests(struct test *t, int reps, int sets, enum target target, int use_window) 219{ 220 struct test_target out, ref; 221 int r, s; 222 printf("Testing area fills (%s, using %s source): ", 223 test_target_name(target), use_window ? "window" : "pixmap"); 224 fflush(stdout); 225 226 test_target_create_render(&t->out, target, &out); 227 clear(&t->out, &out); 228 229 test_target_create_render(&t->ref, target, &ref); 230 clear(&t->ref, &ref); 231 232 for (s = 0; s < sets; s++) { 233 for (r = 0; r < reps; r++) { 234 int x, y, w, h; 235 uint8_t red = rand(); 236 uint8_t green = rand(); 237 uint8_t blue = rand(); 238 239 x = rand() % (out.width - 1); 240 y = rand() % (out.height - 1); 241 w = 1 + rand() % (out.width - x - 1); 242 h = 1 + rand() % (out.height - y - 1); 243 244 fill_rect(&t->out, out.picture, 245 x, y, w, h, 246 red, green, blue); 247 fill_rect(&t->ref, ref.picture, 248 x, y, w, h, 249 red, green, blue); 250 } 251 252 test_compare(t, 253 out.draw, out.format, 254 ref.draw, ref.format, 255 0, 0, out.width, out.height, 256 ""); 257 } 258 259 printf("passed [%d iterations x %d]\n", reps, sets); 260 261 test_target_destroy_render(&t->out, &out); 262 test_target_destroy_render(&t->ref, &ref); 263} 264 265int main(int argc, char **argv) 266{ 267 struct test test; 268 int i; 269 270 test_init(&test, argc, argv); 271 272 for (i = 0; i <= DEFAULT_ITERATIONS; i++) { 273 int reps = REPS(i), sets = SETS(i); 274 275 pixel_tests(&test, reps, sets, PIXMAP); 276 area_tests(&test, reps, sets, PIXMAP); 277 rect_tests(&test, reps, sets, PIXMAP, 0); 278 } 279 280 return 0; 281} 282