103b705cfSriastradh#include <stdint.h> 203b705cfSriastradh#include <stdio.h> 303b705cfSriastradh#include <stdlib.h> 403b705cfSriastradh 503b705cfSriastradh#include <X11/Xutil.h> /* for XDestroyImage */ 603b705cfSriastradh#include <pixman.h> /* for pixman blt functions */ 703b705cfSriastradh 803b705cfSriastradh#include "test.h" 903b705cfSriastradh 1003b705cfSriastradhstatic void 1103b705cfSriastradhshow_cells(char *buf, 1242542f5fSchristos const uint32_t *out, const uint32_t *ref, 1303b705cfSriastradh int x, int y, int w, int h) 1403b705cfSriastradh{ 1503b705cfSriastradh int i, j, len = 0; 1603b705cfSriastradh 1703b705cfSriastradh for (j = y - 2; j <= y + 2; j++) { 1803b705cfSriastradh if (j < 0 || j >= h) 1903b705cfSriastradh continue; 2003b705cfSriastradh 2103b705cfSriastradh for (i = x - 2; i <= x + 2; i++) { 2203b705cfSriastradh if (i < 0 || i >= w) 2303b705cfSriastradh continue; 2403b705cfSriastradh 2542542f5fSchristos len += sprintf(buf+len, "%08x ", out[j*w+i]); 2603b705cfSriastradh } 2703b705cfSriastradh 2803b705cfSriastradh len += sprintf(buf+len, "\t"); 2903b705cfSriastradh 3003b705cfSriastradh for (i = x - 2; i <= x + 2; i++) { 3103b705cfSriastradh if (i < 0 || i >= w) 3203b705cfSriastradh continue; 3303b705cfSriastradh 3403b705cfSriastradh len += sprintf(buf+len, "%08x ", ref[j*w+i]); 3503b705cfSriastradh } 3603b705cfSriastradh 3703b705cfSriastradh len += sprintf(buf+len, "\n"); 3803b705cfSriastradh } 3903b705cfSriastradh} 4003b705cfSriastradh 4103b705cfSriastradhstatic void fill_rect(struct test_display *t, Picture p, 4203b705cfSriastradh int x, int y, int w, int h, 4303b705cfSriastradh uint8_t red, uint8_t green, uint8_t blue) 4403b705cfSriastradh{ 4503b705cfSriastradh Drawable tmp; 4603b705cfSriastradh XRenderColor c; 4703b705cfSriastradh Picture src; 4803b705cfSriastradh XRenderPictFormat *format; 4903b705cfSriastradh 5003b705cfSriastradh format = XRenderFindStandardFormat(t->dpy, PictStandardRGB24); 5103b705cfSriastradh 5203b705cfSriastradh tmp = XCreatePixmap(t->dpy, DefaultRootWindow(t->dpy), 5303b705cfSriastradh w, h, format->depth); 5403b705cfSriastradh 5503b705cfSriastradh src = XRenderCreatePicture(t->dpy, tmp, format, 0, NULL); 5603b705cfSriastradh c.red = (int)red << 8 | red; 5703b705cfSriastradh c.green = (int)green << 8 | green; 5803b705cfSriastradh c.blue = (int)blue << 8 | blue; 5903b705cfSriastradh c.alpha = 0xffff; 6003b705cfSriastradh XRenderFillRectangle(t->dpy, PictOpSrc, src, &c, 0, 0, w, h); 6103b705cfSriastradh XRenderComposite(t->dpy, PictOpOver, src, 0, p, 0, 0, 0, 0, x, y, w, h); 6203b705cfSriastradh 6303b705cfSriastradh XRenderFreePicture(t->dpy, src); 6403b705cfSriastradh XFreePixmap(t->dpy, tmp); 6503b705cfSriastradh} 6603b705cfSriastradh 6703b705cfSriastradhstatic void pixel_tests(struct test *t, int reps, int sets, enum target target) 6803b705cfSriastradh{ 6903b705cfSriastradh struct test_target tt; 7003b705cfSriastradh XImage image; 7142542f5fSchristos uint32_t *cells = malloc(t->out.width*t->out.height*4); 7203b705cfSriastradh struct { 7303b705cfSriastradh uint16_t x, y; 7403b705cfSriastradh } *pixels = malloc(reps*sizeof(*pixels)); 7503b705cfSriastradh int r, s; 7603b705cfSriastradh 7742542f5fSchristos test_target_create_render(&t->out, target, &tt); 7803b705cfSriastradh 7903b705cfSriastradh printf("Testing setting of single pixels (%s): ", 8003b705cfSriastradh test_target_name(target)); 8103b705cfSriastradh fflush(stdout); 8203b705cfSriastradh 8303b705cfSriastradh for (s = 0; s < sets; s++) { 8403b705cfSriastradh for (r = 0; r < reps; r++) { 8503b705cfSriastradh int x = rand() % (tt.width - 1); 8603b705cfSriastradh int y = rand() % (tt.height - 1); 8703b705cfSriastradh uint8_t red = rand(); 8803b705cfSriastradh uint8_t green = rand(); 8903b705cfSriastradh uint8_t blue = rand(); 9003b705cfSriastradh 9142542f5fSchristos fill_rect(&t->out, tt.picture, 9203b705cfSriastradh x, y, 1, 1, 9303b705cfSriastradh red, green, blue); 9403b705cfSriastradh 9503b705cfSriastradh pixels[r].x = x; 9603b705cfSriastradh pixels[r].y = y; 9703b705cfSriastradh cells[y*tt.width+x] = color(red, green, blue, 0xff); 9803b705cfSriastradh } 9903b705cfSriastradh 10042542f5fSchristos test_init_image(&image, &t->out.shm, tt.format, 1, 1); 10103b705cfSriastradh 10203b705cfSriastradh for (r = 0; r < reps; r++) { 10303b705cfSriastradh uint32_t x = pixels[r].x; 10403b705cfSriastradh uint32_t y = pixels[r].y; 10503b705cfSriastradh uint32_t result; 10603b705cfSriastradh 10742542f5fSchristos XShmGetImage(t->out.dpy, tt.draw, &image, 10803b705cfSriastradh x, y, AllPlanes); 10903b705cfSriastradh 11003b705cfSriastradh result = *(uint32_t *)image.data; 11103b705cfSriastradh if (!pixel_equal(image.depth, result, 11203b705cfSriastradh cells[y*tt.width+x])) { 11303b705cfSriastradh uint32_t mask = depth_mask(image.depth); 11403b705cfSriastradh 11503b705cfSriastradh die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead\n", 11603b705cfSriastradh x, y, 11703b705cfSriastradh cells[y*tt.width+x] & mask, 11803b705cfSriastradh cells[y*tt.width+x], 11903b705cfSriastradh result & mask, 12003b705cfSriastradh result); 12103b705cfSriastradh } 12203b705cfSriastradh } 12303b705cfSriastradh } 12403b705cfSriastradh printf("passed [%d iterations x %d]\n", reps, sets); 12503b705cfSriastradh 12642542f5fSchristos test_target_destroy_render(&t->out, &tt); 12703b705cfSriastradh free(pixels); 12803b705cfSriastradh free(cells); 12903b705cfSriastradh} 13003b705cfSriastradh 13103b705cfSriastradhstatic void clear(struct test_display *dpy, struct test_target *tt) 13203b705cfSriastradh{ 13303b705cfSriastradh XRenderColor render_color = {0}; 13403b705cfSriastradh XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color, 13503b705cfSriastradh 0, 0, tt->width, tt->height); 13603b705cfSriastradh} 13703b705cfSriastradh 13803b705cfSriastradhstatic void area_tests(struct test *t, int reps, int sets, enum target target) 13903b705cfSriastradh{ 14003b705cfSriastradh struct test_target tt; 14103b705cfSriastradh XImage image; 14242542f5fSchristos uint32_t *cells = calloc(sizeof(uint32_t), t->out.width*t->out.height); 14303b705cfSriastradh int r, s, x, y; 14403b705cfSriastradh 14503b705cfSriastradh printf("Testing area sets (%s): ", test_target_name(target)); 14603b705cfSriastradh fflush(stdout); 14703b705cfSriastradh 14842542f5fSchristos test_target_create_render(&t->out, target, &tt); 14942542f5fSchristos clear(&t->out, &tt); 15003b705cfSriastradh 15142542f5fSchristos test_init_image(&image, &t->out.shm, tt.format, tt.width, tt.height); 15203b705cfSriastradh 15303b705cfSriastradh for (s = 0; s < sets; s++) { 15403b705cfSriastradh for (r = 0; r < reps; r++) { 15503b705cfSriastradh int w = 1 + rand() % (tt.width - 1); 15603b705cfSriastradh int h = 1 + rand() % (tt.height - 1); 15703b705cfSriastradh uint8_t red = rand(); 15803b705cfSriastradh uint8_t green = rand(); 15903b705cfSriastradh uint8_t blue = rand(); 16003b705cfSriastradh 16103b705cfSriastradh x = rand() % (2*tt.width) - tt.width; 16203b705cfSriastradh y = rand() % (2*tt.height) - tt.height; 16303b705cfSriastradh 16442542f5fSchristos fill_rect(&t->out, tt.picture, 16503b705cfSriastradh x, y, w, h, 16603b705cfSriastradh red, green, blue); 16703b705cfSriastradh 16803b705cfSriastradh if (x < 0) 16903b705cfSriastradh w += x, x = 0; 17003b705cfSriastradh if (y < 0) 17103b705cfSriastradh h += y, y = 0; 17203b705cfSriastradh if (x >= tt.width || y >= tt.height) 17303b705cfSriastradh continue; 17403b705cfSriastradh 17503b705cfSriastradh if (x + w > tt.width) 17603b705cfSriastradh w = tt.width - x; 17703b705cfSriastradh if (y + h > tt.height) 17803b705cfSriastradh h = tt.height - y; 17903b705cfSriastradh if (w <= 0 || h <= 0) 18003b705cfSriastradh continue; 18103b705cfSriastradh 18203b705cfSriastradh pixman_fill(cells, tt.width, 32, x, y, w, h, 18303b705cfSriastradh color(red, green, blue, 0xff)); 18403b705cfSriastradh } 18503b705cfSriastradh 18642542f5fSchristos XShmGetImage(t->out.dpy, tt.draw, &image, 0, 0, AllPlanes); 18703b705cfSriastradh 18803b705cfSriastradh for (y = 0; y < tt.height; y++) { 18903b705cfSriastradh for (x = 0; x < tt.width; x++) { 19003b705cfSriastradh uint32_t result = *(uint32_t *) 19103b705cfSriastradh (image.data + 19203b705cfSriastradh y*image.bytes_per_line + 19303b705cfSriastradh x*image.bits_per_pixel/8); 19403b705cfSriastradh if (!pixel_equal(image.depth, result, cells[y*tt.width+x])) { 19503b705cfSriastradh char buf[600]; 19603b705cfSriastradh uint32_t mask = depth_mask(image.depth); 19703b705cfSriastradh show_cells(buf, 19803b705cfSriastradh (uint32_t*)image.data, cells, 19903b705cfSriastradh x, y, tt.width, tt.height); 20003b705cfSriastradh 20103b705cfSriastradh die("failed to set pixel (%d,%d) to %08x [%08x], found %08x [%08x] instead (set %d, reps %d)\n%s", 20203b705cfSriastradh x, y, 20303b705cfSriastradh cells[y*tt.width+x] & mask, 20403b705cfSriastradh cells[y*tt.width+x], 20503b705cfSriastradh result & mask, 20603b705cfSriastradh result, s, reps, buf); 20703b705cfSriastradh } 20803b705cfSriastradh } 20903b705cfSriastradh } 21003b705cfSriastradh } 21103b705cfSriastradh 21203b705cfSriastradh printf("passed [%d iterations x %d]\n", reps, sets); 21303b705cfSriastradh 21442542f5fSchristos test_target_destroy_render(&t->out, &tt); 21503b705cfSriastradh free(cells); 21603b705cfSriastradh} 21703b705cfSriastradh 21803b705cfSriastradhstatic void rect_tests(struct test *t, int reps, int sets, enum target target, int use_window) 21903b705cfSriastradh{ 22042542f5fSchristos struct test_target out, ref; 22103b705cfSriastradh int r, s; 22203b705cfSriastradh printf("Testing area fills (%s, using %s source): ", 22303b705cfSriastradh test_target_name(target), use_window ? "window" : "pixmap"); 22403b705cfSriastradh fflush(stdout); 22503b705cfSriastradh 22642542f5fSchristos test_target_create_render(&t->out, target, &out); 22742542f5fSchristos clear(&t->out, &out); 22803b705cfSriastradh 22903b705cfSriastradh test_target_create_render(&t->ref, target, &ref); 23003b705cfSriastradh clear(&t->ref, &ref); 23103b705cfSriastradh 23203b705cfSriastradh for (s = 0; s < sets; s++) { 23303b705cfSriastradh for (r = 0; r < reps; r++) { 23403b705cfSriastradh int x, y, w, h; 23503b705cfSriastradh uint8_t red = rand(); 23603b705cfSriastradh uint8_t green = rand(); 23703b705cfSriastradh uint8_t blue = rand(); 23803b705cfSriastradh 23942542f5fSchristos x = rand() % (out.width - 1); 24042542f5fSchristos y = rand() % (out.height - 1); 24142542f5fSchristos w = 1 + rand() % (out.width - x - 1); 24242542f5fSchristos h = 1 + rand() % (out.height - y - 1); 24303b705cfSriastradh 24442542f5fSchristos fill_rect(&t->out, out.picture, 24503b705cfSriastradh x, y, w, h, 24603b705cfSriastradh red, green, blue); 24703b705cfSriastradh fill_rect(&t->ref, ref.picture, 24803b705cfSriastradh x, y, w, h, 24903b705cfSriastradh red, green, blue); 25003b705cfSriastradh } 25103b705cfSriastradh 25203b705cfSriastradh test_compare(t, 25342542f5fSchristos out.draw, out.format, 25403b705cfSriastradh ref.draw, ref.format, 25542542f5fSchristos 0, 0, out.width, out.height, 25603b705cfSriastradh ""); 25703b705cfSriastradh } 25803b705cfSriastradh 25903b705cfSriastradh printf("passed [%d iterations x %d]\n", reps, sets); 26003b705cfSriastradh 26142542f5fSchristos test_target_destroy_render(&t->out, &out); 26203b705cfSriastradh test_target_destroy_render(&t->ref, &ref); 26303b705cfSriastradh} 26403b705cfSriastradh 26503b705cfSriastradhint main(int argc, char **argv) 26603b705cfSriastradh{ 26703b705cfSriastradh struct test test; 26803b705cfSriastradh int i; 26903b705cfSriastradh 27003b705cfSriastradh test_init(&test, argc, argv); 27103b705cfSriastradh 27203b705cfSriastradh for (i = 0; i <= DEFAULT_ITERATIONS; i++) { 27342542f5fSchristos int reps = REPS(i), sets = SETS(i); 27403b705cfSriastradh 27503b705cfSriastradh pixel_tests(&test, reps, sets, PIXMAP); 27603b705cfSriastradh area_tests(&test, reps, sets, PIXMAP); 27703b705cfSriastradh rect_tests(&test, reps, sets, PIXMAP, 0); 27803b705cfSriastradh } 27903b705cfSriastradh 28003b705cfSriastradh return 0; 28103b705cfSriastradh} 282