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 const uint8_t ops[] = {
1103b705cfSriastradh	PictOpClear,
1203b705cfSriastradh	PictOpSrc,
1303b705cfSriastradh	PictOpDst,
1403b705cfSriastradh};
1503b705cfSriastradh
1603b705cfSriastradhstatic void fill_rect(struct test_display *dpy, Picture p, uint8_t op,
1703b705cfSriastradh		      int x, int y, int w, int h,
1803b705cfSriastradh		      uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
1903b705cfSriastradh{
2003b705cfSriastradh	XRenderColor render_color;
2103b705cfSriastradh	Picture solid;
2203b705cfSriastradh
2303b705cfSriastradh	render_color.red   = red * alpha;
2403b705cfSriastradh	render_color.green = green * alpha;
2503b705cfSriastradh	render_color.blue  = blue * alpha;
2603b705cfSriastradh	render_color.alpha = alpha << 8 | alpha;
2703b705cfSriastradh
2803b705cfSriastradh	solid = XRenderCreateSolidFill(dpy->dpy, &render_color);
2903b705cfSriastradh	XRenderComposite(dpy->dpy, op, solid, 0, p, 0, 0, 0, 0, x, y, w,h);
3003b705cfSriastradh	XRenderFreePicture(dpy->dpy, solid);
3103b705cfSriastradh}
3203b705cfSriastradh
3303b705cfSriastradhstatic void pixel_tests(struct test *t, int reps, int sets, enum target target)
3403b705cfSriastradh{
3503b705cfSriastradh	struct test_target tt;
3603b705cfSriastradh	XImage image;
3742542f5fSchristos	uint32_t *cells = malloc(t->out.width*t->out.height*4);
3803b705cfSriastradh	struct {
3903b705cfSriastradh		uint16_t x, y;
4003b705cfSriastradh	} *pixels = malloc(reps*sizeof(*pixels));
4103b705cfSriastradh	int r, s;
4203b705cfSriastradh
4342542f5fSchristos	test_target_create_render(&t->out, target, &tt);
4403b705cfSriastradh
4503b705cfSriastradh	printf("Testing setting of single pixels (%s): ",
4603b705cfSriastradh	       test_target_name(target));
4703b705cfSriastradh	fflush(stdout);
4803b705cfSriastradh
4903b705cfSriastradh	for (s = 0; s < sets; s++) {
5003b705cfSriastradh		for (r = 0; r < reps; r++) {
5103b705cfSriastradh			int x = rand() % (tt.width - 1);
5203b705cfSriastradh			int y = rand() % (tt.height - 1);
5303b705cfSriastradh			int red = rand() % 0xff;
5403b705cfSriastradh			int green = rand() % 0xff;
5503b705cfSriastradh			int blue = rand() % 0xff;
5603b705cfSriastradh			int alpha = rand() % 0xff;
5703b705cfSriastradh
5842542f5fSchristos			fill_rect(&t->out, tt.picture, PictOpSrc,
5903b705cfSriastradh				  x, y, 1, 1,
6003b705cfSriastradh				  red, green, blue, alpha);
6103b705cfSriastradh
6203b705cfSriastradh			pixels[r].x = x;
6303b705cfSriastradh			pixels[r].y = y;
6403b705cfSriastradh			cells[y*tt.width+x] = color(red, green, blue, alpha);
6503b705cfSriastradh		}
6603b705cfSriastradh
6742542f5fSchristos		test_init_image(&image, &t->out.shm, tt.format, 1, 1);
6803b705cfSriastradh
6903b705cfSriastradh		for (r = 0; r < reps; r++) {
7003b705cfSriastradh			uint32_t result;
7103b705cfSriastradh			uint32_t x = pixels[r].x;
7203b705cfSriastradh			uint32_t y = pixels[r].y;
7303b705cfSriastradh
7442542f5fSchristos			XShmGetImage(t->out.dpy, tt.draw, &image,
7503b705cfSriastradh				     x, y, AllPlanes);
7603b705cfSriastradh
7703b705cfSriastradh			result = *(uint32_t *)image.data;
7803b705cfSriastradh			if (!pixel_equal(image.depth, result,
7903b705cfSriastradh					 cells[y*tt.width+x])) {
8003b705cfSriastradh				uint32_t mask;
8103b705cfSriastradh
8203b705cfSriastradh				if (image.depth == 32)
8303b705cfSriastradh					mask = 0xffffffff;
8403b705cfSriastradh				else
8503b705cfSriastradh					mask = (1 << image.depth) - 1;
8603b705cfSriastradh				die("failed to set pixel (%d,%d) to %08x[%08x], found %08x instead\n",
8703b705cfSriastradh				    x, y,
8803b705cfSriastradh				    cells[y*tt.width+x] & mask,
8903b705cfSriastradh				    cells[y*tt.width+x],
9003b705cfSriastradh				    result & mask);
9103b705cfSriastradh			}
9203b705cfSriastradh		}
9303b705cfSriastradh	}
9403b705cfSriastradh	printf("passed [%d iterations x %d]\n", reps, sets);
9503b705cfSriastradh
9642542f5fSchristos	test_target_destroy_render(&t->out, &tt);
9703b705cfSriastradh	free(pixels);
9803b705cfSriastradh	free(cells);
9903b705cfSriastradh}
10003b705cfSriastradh
10103b705cfSriastradhstatic void clear(struct test_display *dpy, struct test_target *tt)
10203b705cfSriastradh{
10303b705cfSriastradh	XRenderColor render_color = {0};
10403b705cfSriastradh	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
10503b705cfSriastradh			     0, 0, tt->width, tt->height);
10603b705cfSriastradh}
10703b705cfSriastradh
10803b705cfSriastradhstatic void area_tests(struct test *t, int reps, int sets, enum target target)
10903b705cfSriastradh{
11003b705cfSriastradh	struct test_target tt;
11103b705cfSriastradh	XImage image;
11242542f5fSchristos	uint32_t *cells = calloc(sizeof(uint32_t), t->out.width*t->out.height);
11303b705cfSriastradh	int r, s, x, y;
11403b705cfSriastradh
11503b705cfSriastradh	printf("Testing area sets (%s): ", test_target_name(target));
11603b705cfSriastradh	fflush(stdout);
11703b705cfSriastradh
11842542f5fSchristos	test_target_create_render(&t->out, target, &tt);
11942542f5fSchristos	clear(&t->out, &tt);
12003b705cfSriastradh
12142542f5fSchristos	test_init_image(&image, &t->out.shm, tt.format, tt.width, tt.height);
12203b705cfSriastradh
12303b705cfSriastradh	for (s = 0; s < sets; s++) {
12403b705cfSriastradh		for (r = 0; r < reps; r++) {
12503b705cfSriastradh			int w = rand() % tt.width;
12603b705cfSriastradh			int h = rand() % tt.height;
12703b705cfSriastradh			int red = rand() % 0xff;
12803b705cfSriastradh			int green = rand() % 0xff;
12903b705cfSriastradh			int blue = rand() % 0xff;
13003b705cfSriastradh			int alpha = rand() % 0xff;
13103b705cfSriastradh
13203b705cfSriastradh			x = rand() % (2*tt.width) - tt.width;
13303b705cfSriastradh			y = rand() % (2*tt.height) - tt.height;
13403b705cfSriastradh
13542542f5fSchristos			fill_rect(&t->out, tt.picture, PictOpSrc,
13603b705cfSriastradh				  x, y, w, h, red, green, blue, alpha);
13703b705cfSriastradh
13803b705cfSriastradh			if (x < 0)
13903b705cfSriastradh				w += x, x = 0;
14003b705cfSriastradh			if (y < 0)
14103b705cfSriastradh				h += y, y = 0;
14203b705cfSriastradh			if (x >= tt.width || y >= tt.height)
14303b705cfSriastradh				continue;
14403b705cfSriastradh
14503b705cfSriastradh			if (x + w > tt.width)
14603b705cfSriastradh				w = tt.width - x;
14703b705cfSriastradh			if (y + h > tt.height)
14803b705cfSriastradh				h = tt.height - y;
14903b705cfSriastradh			if (w <= 0 || h <= 0)
15003b705cfSriastradh				continue;
15103b705cfSriastradh
15203b705cfSriastradh			pixman_fill(cells, tt.width, 32, x, y, w, h,
15303b705cfSriastradh				    color(red, green, blue, alpha));
15403b705cfSriastradh		}
15503b705cfSriastradh
15642542f5fSchristos		XShmGetImage(t->out.dpy, tt.draw, &image, 0, 0, AllPlanes);
15703b705cfSriastradh
15803b705cfSriastradh		for (y = 0; y < tt.height; y++) {
15903b705cfSriastradh			for (x = 0; x < tt.width; x++) {
16003b705cfSriastradh				uint32_t result =
16103b705cfSriastradh					*(uint32_t *)(image.data +
16203b705cfSriastradh						      y*image.bytes_per_line +
16303b705cfSriastradh						      image.bits_per_pixel*x/8);
16403b705cfSriastradh				if (!pixel_equal(image.depth, result, cells[y*tt.width+x])) {
16503b705cfSriastradh					uint32_t mask;
16603b705cfSriastradh					if (image.depth == 32)
16703b705cfSriastradh						mask = 0xffffffff;
16803b705cfSriastradh					else
16903b705cfSriastradh						mask = (1 << image.depth) - 1;
17003b705cfSriastradh					die("failed to set pixel (%d,%d) to %08x[%08x], found %08x instead\n",
17103b705cfSriastradh					    x, y,
17203b705cfSriastradh					    cells[y*tt.width+x] & mask,
17303b705cfSriastradh					    cells[y*tt.width+x],
17403b705cfSriastradh					    result & mask);
17503b705cfSriastradh				}
17603b705cfSriastradh			}
17703b705cfSriastradh		}
17803b705cfSriastradh	}
17903b705cfSriastradh
18003b705cfSriastradh	printf("passed [%d iterations x %d]\n", reps, sets);
18103b705cfSriastradh
18242542f5fSchristos	test_target_destroy_render(&t->out, &tt);
18303b705cfSriastradh	free(cells);
18403b705cfSriastradh}
18503b705cfSriastradh
18603b705cfSriastradhstatic void rect_tests(struct test *t, int reps, int sets, enum target target)
18703b705cfSriastradh{
18842542f5fSchristos	struct test_target out, ref;
18903b705cfSriastradh	int r, s;
19003b705cfSriastradh
19103b705cfSriastradh	printf("Testing area fills (%s): ", test_target_name(target));
19203b705cfSriastradh	fflush(stdout);
19303b705cfSriastradh
19442542f5fSchristos	test_target_create_render(&t->out, target, &out);
19542542f5fSchristos	clear(&t->out, &out);
19603b705cfSriastradh
19703b705cfSriastradh	test_target_create_render(&t->ref, target, &ref);
19803b705cfSriastradh	clear(&t->ref, &ref);
19903b705cfSriastradh
20003b705cfSriastradh	for (s = 0; s < sets; s++) {
20103b705cfSriastradh		for (r = 0; r < reps; r++) {
20242542f5fSchristos			int x = rand() % (2*out.width) - out.width;
20342542f5fSchristos			int y = rand() % (2*out.height) - out.height;
20442542f5fSchristos			int w = rand() % out.width;
20542542f5fSchristos			int h = rand() % out.height;
20603b705cfSriastradh			int op = ops[rand() % sizeof(ops)];
20703b705cfSriastradh			int red = rand() % 0xff;
20803b705cfSriastradh			int green = rand() % 0xff;
20903b705cfSriastradh			int blue = rand() % 0xff;
21003b705cfSriastradh			int alpha = rand() % 0xff;
21103b705cfSriastradh
21242542f5fSchristos			fill_rect(&t->out, out.picture,
21303b705cfSriastradh				  op, x, y, w, h,
21403b705cfSriastradh				  red, green, blue, alpha);
21503b705cfSriastradh			fill_rect(&t->ref, ref.picture,
21603b705cfSriastradh				  op, x, y, w, h,
21703b705cfSriastradh				  red, green, blue, alpha);
21803b705cfSriastradh		}
21903b705cfSriastradh
22003b705cfSriastradh		test_compare(t,
22142542f5fSchristos			     out.draw, out.format,
22203b705cfSriastradh			     ref.draw, ref.format,
22342542f5fSchristos			     0, 0, out.width, out.height,
22403b705cfSriastradh			     "");
22503b705cfSriastradh	}
22603b705cfSriastradh
22703b705cfSriastradh	printf("passed [%d iterations x %d]\n", reps, sets);
22803b705cfSriastradh
22942542f5fSchristos	test_target_destroy_render(&t->out, &out);
23003b705cfSriastradh	test_target_destroy_render(&t->ref, &ref);
23103b705cfSriastradh}
23203b705cfSriastradh
23303b705cfSriastradhint main(int argc, char **argv)
23403b705cfSriastradh{
23503b705cfSriastradh	struct test test;
23603b705cfSriastradh	int i;
23703b705cfSriastradh
23803b705cfSriastradh	test_init(&test, argc, argv);
23903b705cfSriastradh
24003b705cfSriastradh	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
24142542f5fSchristos		int reps = REPS(i), sets = SETS(i);
24203b705cfSriastradh		enum target t;
24303b705cfSriastradh
24403b705cfSriastradh		for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
24503b705cfSriastradh			pixel_tests(&test, reps, sets, t);
24603b705cfSriastradh			area_tests(&test, reps, sets, t);
24703b705cfSriastradh			rect_tests(&test, reps, sets, t);
24803b705cfSriastradh		}
24903b705cfSriastradh	}
25003b705cfSriastradh
25103b705cfSriastradh	return 0;
25203b705cfSriastradh}
253