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