1428d7b3dSmrg#include <stdint.h> 2428d7b3dSmrg#include <stdio.h> 3428d7b3dSmrg#include <stdlib.h> 4428d7b3dSmrg 5428d7b3dSmrg#include <X11/Xutil.h> /* for XDestroyImage */ 6428d7b3dSmrg#include <pixman.h> /* for pixman blt functions */ 7428d7b3dSmrg 8428d7b3dSmrg#include "test.h" 9428d7b3dSmrg 10428d7b3dSmrgstatic const uint8_t ops[] = { 11428d7b3dSmrg PictOpClear, 12428d7b3dSmrg PictOpSrc, 13428d7b3dSmrg PictOpDst, 14428d7b3dSmrg}; 15428d7b3dSmrg 16428d7b3dSmrgstatic void fill_rect(struct test_display *dpy, 17428d7b3dSmrg Picture p, 18428d7b3dSmrg XRenderPictFormat *format, 19428d7b3dSmrg uint8_t op, int x, int y, int w, int h, 20428d7b3dSmrg uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) 21428d7b3dSmrg{ 22428d7b3dSmrg Display *d = dpy->dpy; 23428d7b3dSmrg XRenderColor render_color; 24428d7b3dSmrg Pixmap pixmap1, pixmap2; 25428d7b3dSmrg Picture tmp1, tmp2; 26428d7b3dSmrg XRenderPictureAttributes pa; 27428d7b3dSmrg GC gc; 28428d7b3dSmrg 29428d7b3dSmrg render_color.red = red * alpha; 30428d7b3dSmrg render_color.green = green * alpha; 31428d7b3dSmrg render_color.blue = blue * alpha; 32428d7b3dSmrg render_color.alpha = alpha << 8; 33428d7b3dSmrg 34428d7b3dSmrg pixmap1 = XCreatePixmap(d, dpy->root, 1, 1, format->depth); 35428d7b3dSmrg tmp1 = XRenderCreatePicture(d, pixmap1, format, 0, NULL); 36428d7b3dSmrg 37428d7b3dSmrg pixmap2 = XCreatePixmap(d, dpy->root, 1, 1, format->depth); 38428d7b3dSmrg pa.repeat = 1; 39428d7b3dSmrg tmp2 = XRenderCreatePicture(d, pixmap2, format, CPRepeat, &pa); 40428d7b3dSmrg 41428d7b3dSmrg gc = XCreateGC(d, pixmap1, 0, NULL); 42428d7b3dSmrg 43428d7b3dSmrg XRenderFillRectangle(d, PictOpSrc, tmp1, &render_color, 0, 0, 1,1); 44428d7b3dSmrg XCopyArea(d, pixmap1, pixmap2, gc, 0, 0, 1, 1, 0, 0); 45428d7b3dSmrg XRenderComposite(d, PictOpSrc, tmp2, 0, p, 0, 0, 0, 0, x, y, w,h); 46428d7b3dSmrg 47428d7b3dSmrg XFreeGC(d, gc); 48428d7b3dSmrg 49428d7b3dSmrg XRenderFreePicture(d, tmp2); 50428d7b3dSmrg XFreePixmap(d, pixmap2); 51428d7b3dSmrg 52428d7b3dSmrg XRenderFreePicture(d, tmp1); 53428d7b3dSmrg XFreePixmap(d, pixmap1); 54428d7b3dSmrg} 55428d7b3dSmrg 56428d7b3dSmrgstatic void pixel_tests(struct test *t, int reps, int sets, enum target target) 57428d7b3dSmrg{ 58428d7b3dSmrg struct test_target tt; 59428d7b3dSmrg XImage image; 60428d7b3dSmrg uint32_t *cells = malloc(t->out.width*t->out.height*4); 61428d7b3dSmrg struct { 62428d7b3dSmrg uint16_t x, y; 63428d7b3dSmrg } *pixels = malloc(reps*sizeof(*pixels)); 64428d7b3dSmrg int r, s; 65428d7b3dSmrg 66428d7b3dSmrg test_target_create_render(&t->out, target, &tt); 67428d7b3dSmrg 68428d7b3dSmrg printf("Testing setting of single pixels (%s): ", 69428d7b3dSmrg test_target_name(target)); 70428d7b3dSmrg fflush(stdout); 71428d7b3dSmrg 72428d7b3dSmrg for (s = 0; s < sets; s++) { 73428d7b3dSmrg for (r = 0; r < reps; r++) { 74428d7b3dSmrg int x = rand() % (tt.width - 1); 75428d7b3dSmrg int y = rand() % (tt.height - 1); 76428d7b3dSmrg int red = rand() % 0xff; 77428d7b3dSmrg int green = rand() % 0xff; 78428d7b3dSmrg int blue = rand() % 0xff; 79428d7b3dSmrg int alpha = rand() % 0xff; 80428d7b3dSmrg 81428d7b3dSmrg fill_rect(&t->out, tt.picture, tt.format, 82428d7b3dSmrg PictOpSrc, x, y, 1, 1, 83428d7b3dSmrg red, green, blue, alpha); 84428d7b3dSmrg 85428d7b3dSmrg pixels[r].x = x; 86428d7b3dSmrg pixels[r].y = y; 87428d7b3dSmrg cells[y*tt.width+x] = color(red, green, blue, alpha); 88428d7b3dSmrg } 89428d7b3dSmrg 90428d7b3dSmrg test_init_image(&image, &t->out.shm, tt.format, 1, 1); 91428d7b3dSmrg 92428d7b3dSmrg for (r = 0; r < reps; r++) { 93428d7b3dSmrg uint32_t result; 94428d7b3dSmrg uint32_t x = pixels[r].x; 95428d7b3dSmrg uint32_t y = pixels[r].y; 96428d7b3dSmrg 97428d7b3dSmrg XShmGetImage(t->out.dpy, tt.draw, &image, 98428d7b3dSmrg x, y, AllPlanes); 99428d7b3dSmrg 100428d7b3dSmrg result = *(uint32_t *)image.data; 101428d7b3dSmrg if (!pixel_equal(image.depth, result, 102428d7b3dSmrg cells[y*tt.width+x])) { 103428d7b3dSmrg uint32_t mask; 104428d7b3dSmrg 105428d7b3dSmrg if (image.depth == 32) 106428d7b3dSmrg mask = 0xffffffff; 107428d7b3dSmrg else 108428d7b3dSmrg mask = (1 << image.depth) - 1; 109428d7b3dSmrg die("failed to set pixel (%d,%d) to %08x, found %08x instead\n", 110428d7b3dSmrg x, y, 111428d7b3dSmrg cells[y*tt.width+x] & mask, 112428d7b3dSmrg result & mask); 113428d7b3dSmrg } 114428d7b3dSmrg } 115428d7b3dSmrg } 116428d7b3dSmrg printf("passed [%d iterations x %d]\n", reps, sets); 117428d7b3dSmrg 118428d7b3dSmrg test_target_destroy_render(&t->out, &tt); 119428d7b3dSmrg free(pixels); 120428d7b3dSmrg free(cells); 121428d7b3dSmrg} 122428d7b3dSmrg 123428d7b3dSmrgstatic void clear(struct test_display *dpy, struct test_target *tt) 124428d7b3dSmrg{ 125428d7b3dSmrg XRenderColor render_color = {0}; 126428d7b3dSmrg XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color, 127428d7b3dSmrg 0, 0, tt->width, tt->height); 128428d7b3dSmrg} 129428d7b3dSmrg 130428d7b3dSmrgstatic void area_tests(struct test *t, int reps, int sets, enum target target) 131428d7b3dSmrg{ 132428d7b3dSmrg struct test_target tt; 133428d7b3dSmrg XImage image; 134428d7b3dSmrg uint32_t *cells = calloc(sizeof(uint32_t), t->out.width*t->out.height); 135428d7b3dSmrg int r, s, x, y; 136428d7b3dSmrg 137428d7b3dSmrg printf("Testing area sets (%s): ", test_target_name(target)); 138428d7b3dSmrg fflush(stdout); 139428d7b3dSmrg 140428d7b3dSmrg test_target_create_render(&t->out, target, &tt); 141428d7b3dSmrg clear(&t->out, &tt); 142428d7b3dSmrg 143428d7b3dSmrg test_init_image(&image, &t->out.shm, tt.format, tt.width, tt.height); 144428d7b3dSmrg 145428d7b3dSmrg for (s = 0; s < sets; s++) { 146428d7b3dSmrg for (r = 0; r < reps; r++) { 147428d7b3dSmrg int w = rand() % tt.width; 148428d7b3dSmrg int h = rand() % tt.height; 149428d7b3dSmrg int red = rand() % 0xff; 150428d7b3dSmrg int green = rand() % 0xff; 151428d7b3dSmrg int blue = rand() % 0xff; 152428d7b3dSmrg int alpha = rand() % 0xff; 153428d7b3dSmrg 154428d7b3dSmrg x = rand() % (2*tt.width) - tt.width; 155428d7b3dSmrg y = rand() % (2*tt.height) - tt.height; 156428d7b3dSmrg 157428d7b3dSmrg fill_rect(&t->out, tt.picture, tt.format, 158428d7b3dSmrg PictOpSrc, x, y, w, h, 159428d7b3dSmrg red, green, blue, alpha); 160428d7b3dSmrg 161428d7b3dSmrg if (x < 0) 162428d7b3dSmrg w += x, x = 0; 163428d7b3dSmrg if (y < 0) 164428d7b3dSmrg h += y, y = 0; 165428d7b3dSmrg if (x >= tt.width || y >= tt.height) 166428d7b3dSmrg continue; 167428d7b3dSmrg 168428d7b3dSmrg if (x + w > tt.width) 169428d7b3dSmrg w = tt.width - x; 170428d7b3dSmrg if (y + h > tt.height) 171428d7b3dSmrg h = tt.height - y; 172428d7b3dSmrg if (w <= 0 || h <= 0) 173428d7b3dSmrg continue; 174428d7b3dSmrg 175428d7b3dSmrg pixman_fill(cells, tt.width, 32, x, y, w, h, 176428d7b3dSmrg color(red, green, blue, alpha)); 177428d7b3dSmrg } 178428d7b3dSmrg 179428d7b3dSmrg XShmGetImage(t->out.dpy, tt.draw, &image, 0, 0, AllPlanes); 180428d7b3dSmrg 181428d7b3dSmrg for (y = 0; y < tt.height; y++) { 182428d7b3dSmrg for (x = 0; x < tt.width; x++) { 183428d7b3dSmrg uint32_t result = 184428d7b3dSmrg *(uint32_t *)(image.data + 185428d7b3dSmrg y*image.bytes_per_line + 186428d7b3dSmrg image.bits_per_pixel*x/8); 187428d7b3dSmrg if (!pixel_equal(image.depth, result, 188428d7b3dSmrg cells[y*tt.width+x])) { 189428d7b3dSmrg uint32_t mask; 190428d7b3dSmrg 191428d7b3dSmrg if (image.depth == 32) 192428d7b3dSmrg mask = 0xffffffff; 193428d7b3dSmrg else 194428d7b3dSmrg mask = (1 << image.depth) - 1; 195428d7b3dSmrg die("failed to set pixel (%d,%d) to %08x, found %08x instead\n", 196428d7b3dSmrg x, y, 197428d7b3dSmrg cells[y*tt.width+x] & mask, 198428d7b3dSmrg result & mask); 199428d7b3dSmrg } 200428d7b3dSmrg } 201428d7b3dSmrg } 202428d7b3dSmrg } 203428d7b3dSmrg 204428d7b3dSmrg printf("passed [%d iterations x %d]\n", reps, sets); 205428d7b3dSmrg 206428d7b3dSmrg test_target_destroy_render(&t->out, &tt); 207428d7b3dSmrg free(cells); 208428d7b3dSmrg} 209428d7b3dSmrg 210428d7b3dSmrgstatic void rect_tests(struct test *t, int reps, int sets, enum target target) 211428d7b3dSmrg{ 212428d7b3dSmrg struct test_target out, ref; 213428d7b3dSmrg int r, s; 214428d7b3dSmrg 215428d7b3dSmrg printf("Testing area fills (%s): ", test_target_name(target)); 216428d7b3dSmrg fflush(stdout); 217428d7b3dSmrg 218428d7b3dSmrg test_target_create_render(&t->out, target, &out); 219428d7b3dSmrg clear(&t->out, &out); 220428d7b3dSmrg 221428d7b3dSmrg test_target_create_render(&t->ref, target, &ref); 222428d7b3dSmrg clear(&t->ref, &ref); 223428d7b3dSmrg 224428d7b3dSmrg for (s = 0; s < sets; s++) { 225428d7b3dSmrg for (r = 0; r < reps; r++) { 226428d7b3dSmrg int x = rand() % (2*out.width) - out.width; 227428d7b3dSmrg int y = rand() % (2*out.height) - out.height; 228428d7b3dSmrg int w = rand() % out.width; 229428d7b3dSmrg int h = rand() % out.height; 230428d7b3dSmrg int op = ops[rand() % sizeof(ops)]; 231428d7b3dSmrg int red = rand() % 0xff; 232428d7b3dSmrg int green = rand() % 0xff; 233428d7b3dSmrg int blue = rand() % 0xff; 234428d7b3dSmrg int alpha = rand() % 0xff; 235428d7b3dSmrg 236428d7b3dSmrg fill_rect(&t->out, out.picture, out.format, 237428d7b3dSmrg op, x, y, w, h, 238428d7b3dSmrg red, green, blue, alpha); 239428d7b3dSmrg fill_rect(&t->ref, ref.picture, ref.format, 240428d7b3dSmrg op, x, y, w, h, 241428d7b3dSmrg red, green, blue, alpha); 242428d7b3dSmrg } 243428d7b3dSmrg 244428d7b3dSmrg test_compare(t, 245428d7b3dSmrg out.draw, out.format, 246428d7b3dSmrg ref.draw, ref.format, 247428d7b3dSmrg 0, 0, out.width, out.height, 248428d7b3dSmrg ""); 249428d7b3dSmrg } 250428d7b3dSmrg 251428d7b3dSmrg printf("passed [%d iterations x %d]\n", reps, sets); 252428d7b3dSmrg 253428d7b3dSmrg test_target_destroy_render(&t->out, &out); 254428d7b3dSmrg test_target_destroy_render(&t->ref, &ref); 255428d7b3dSmrg} 256428d7b3dSmrg 257428d7b3dSmrgint main(int argc, char **argv) 258428d7b3dSmrg{ 259428d7b3dSmrg struct test test; 260428d7b3dSmrg int i; 261428d7b3dSmrg 262428d7b3dSmrg test_init(&test, argc, argv); 263428d7b3dSmrg 264428d7b3dSmrg for (i = 0; i <= DEFAULT_ITERATIONS; i++) { 265428d7b3dSmrg int reps = REPS(i), sets = SETS(i); 266428d7b3dSmrg enum target t; 267428d7b3dSmrg 268428d7b3dSmrg for (t = TARGET_FIRST; t <= TARGET_LAST; t++) { 269428d7b3dSmrg pixel_tests(&test, reps, sets, t); 270428d7b3dSmrg area_tests(&test, reps, sets, t); 271428d7b3dSmrg rect_tests(&test, reps, sets, t); 272428d7b3dSmrg } 273428d7b3dSmrg } 274428d7b3dSmrg 275428d7b3dSmrg return 0; 276428d7b3dSmrg} 277