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 *dpy,
42		      Drawable d, XRenderPictFormat *format,
43		      int use_shm,
44		      uint8_t alu, int x, int y, int w, int h, uint32_t fg)
45{
46	XImage image;
47	XGCValues val;
48	GC gc;
49
50	test_init_image(&image, &dpy->shm, format, w, h);
51
52	pixman_fill((uint32_t*)image.data,
53		    image.bytes_per_line/sizeof(uint32_t),
54		    image.bits_per_pixel,
55		    0, 0, w, h, fg);
56
57	val.function = alu;
58	gc = XCreateGC(dpy->dpy, d, GCFunction, &val);
59	if (use_shm) {
60		XShmPutImage(dpy->dpy, d, gc, &image, 0, 0, x, y, w, h, 0);
61		XSync(dpy->dpy, 1);
62	} else {
63		XPutImage(dpy->dpy, d, gc, &image, 0, 0, x, y, w, h);
64	}
65	XFreeGC(dpy->dpy, gc);
66}
67
68static void pixel_tests(struct test *t, int reps, int sets, enum target target, int use_shm)
69{
70	struct test_target tt;
71	XImage image;
72	uint32_t *cells = malloc(t->out.width*t->out.height*4);
73	struct {
74		uint16_t x, y;
75	} *pixels = malloc(reps*sizeof(*pixels));
76	int r, s;
77
78	test_target_create_render(&t->out, target, &tt);
79
80	printf("Testing setting of single pixels (%s %s shm): ",
81	       test_target_name(target), use_shm ? "with" : "without" );
82	fflush(stdout);
83
84	for (s = 0; s < sets; s++) {
85		for (r = 0; r < reps; r++) {
86			int x = rand() % (tt.width - 1);
87			int y = rand() % (tt.height - 1);
88			int red = rand() % 0xff;
89			int green = rand() % 0xff;
90			int blue = rand() % 0xff;
91			int alpha = rand() % 0xff;
92			uint32_t fg = color(red, green, blue, alpha);
93
94			fill_rect(&t->out, tt.draw, tt.format, use_shm,
95				  GXcopy, x, y, 1, 1, fg);
96
97			pixels[r].x = x;
98			pixels[r].y = y;
99			cells[y*tt.width+x] = fg;
100		}
101
102		test_init_image(&image, &t->out.shm, tt.format, 1, 1);
103
104		for (r = 0; r < reps; r++) {
105			uint32_t result;
106			uint32_t x = pixels[r].x;
107			uint32_t y = pixels[r].y;
108
109			XShmGetImage(t->out.dpy, tt.draw, &image,
110				     x, y, AllPlanes);
111
112			result = *(uint32_t *)image.data;
113			if (!pixel_equal(image.depth, result,
114					 cells[y*tt.width+x])) {
115				uint32_t mask;
116
117				if (image.depth == 32)
118					mask = 0xffffffff;
119				else
120					mask = (1 << image.depth) - 1;
121				die("failed to set pixel (%d,%d) to %08x[%08x], found %08x instead\n",
122				    x, y,
123				    cells[y*tt.width+x] & mask,
124				    cells[y*tt.width+x],
125				    result & mask);
126			}
127		}
128	}
129	printf("passed [%d iterations x %d]\n", reps, sets);
130
131	test_target_destroy_render(&t->out, &tt);
132	free(pixels);
133	free(cells);
134}
135
136static void clear(struct test_display *dpy, struct test_target *tt)
137{
138	XRenderColor render_color = {0};
139	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
140			     0, 0, tt->width, tt->height);
141}
142
143static void area_tests(struct test *t, int reps, int sets, enum target target, int use_shm)
144{
145	struct test_target tt;
146	XImage image;
147	uint32_t *cells = calloc(sizeof(uint32_t), t->out.width*t->out.height);
148	int r, s, x, y;
149
150	printf("Testing area sets (%s %s shm): ",
151	       test_target_name(target), use_shm ? "with" : "without" );
152	fflush(stdout);
153
154	test_target_create_render(&t->out, target, &tt);
155	clear(&t->out, &tt);
156
157	test_init_image(&image, &t->out.shm, tt.format, tt.width, tt.height);
158
159	for (s = 0; s < sets; s++) {
160		for (r = 0; r < reps; r++) {
161			int red = rand() % 0xff;
162			int green = rand() % 0xff;
163			int blue = rand() % 0xff;
164			int alpha = rand() % 0xff;
165			uint32_t fg = color(red, green, blue, alpha);
166			int w, h;
167
168			x = rand() % tt.width;
169			y = rand() % tt.height;
170			w = rand() % (tt.width - x);
171			h = rand() % (tt.height - y);
172
173			fill_rect(&t->out, tt.draw, tt.format, use_shm,
174				  GXcopy, x, y, w, h, fg);
175
176			pixman_fill(cells, tt.width, 32, x, y, w, h, fg);
177		}
178
179		XShmGetImage(t->out.dpy, tt.draw, &image, 0, 0, AllPlanes);
180
181		for (y = 0; y < tt.height; y++) {
182			for (x = 0; x < tt.width; x++) {
183				uint32_t result =
184					*(uint32_t *)(image.data +
185						      y*image.bytes_per_line +
186						      image.bits_per_pixel*x/8);
187				if (!pixel_equal(image.depth, result, cells[y*tt.width+x])) {
188					uint32_t mask = depth_mask(image.depth);
189					char buf[600];
190
191					show_cells(buf,
192						   (uint32_t*)image.data, cells,
193						   x, y, tt.width, tt.height);
194
195					die("failed to set pixel (%d,%d) to %08x[%08x], found %08x [%08x] instead\n%s",
196					    x, y,
197					    cells[y*tt.width+x] & mask,
198					    cells[y*tt.width+x],
199					    result & mask, result,
200					    buf);
201				}
202			}
203		}
204	}
205
206	printf("passed [%d iterations x %d]\n", reps, sets);
207
208	test_target_destroy_render(&t->out, &tt);
209	free(cells);
210}
211
212static void rect_tests(struct test *t, int reps, int sets, enum target target, int use_shm)
213{
214	struct test_target out, ref;
215	int r, s;
216
217	printf("Testing area fills (%s): ", test_target_name(target));
218	fflush(stdout);
219
220	test_target_create_render(&t->out, target, &out);
221	clear(&t->out, &out);
222
223	test_target_create_render(&t->ref, target, &ref);
224	clear(&t->ref, &ref);
225
226	for (s = 0; s < sets; s++) {
227		for (r = 0; r < reps; r++) {
228			int x = rand() % out.width;
229			int y = rand() % out.height;
230			int w = rand() % (out.width - x);
231			int h = rand() % (out.height - y);
232			uint8_t alu = rand() % (GXset + 1);
233			int red = rand() % 0xff;
234			int green = rand() % 0xff;
235			int blue = rand() % 0xff;
236			int alpha = rand() % 0xff;
237			uint8_t fg = color(red, green, blue, alpha);
238
239			fill_rect(&t->out, out.draw, out.format, use_shm,
240				  alu, x, y, w, h, fg);
241			fill_rect(&t->ref, ref.draw, ref.format, use_shm,
242				  alu, x, y, w, h, fg);
243		}
244
245		test_compare(t,
246			     out.draw, out.format,
247			     ref.draw, ref.format,
248			     0, 0, out.width, out.height,
249			     "");
250	}
251
252	printf("passed [%d iterations x %d]\n", reps, sets);
253
254	test_target_destroy_render(&t->out, &out);
255	test_target_destroy_render(&t->ref, &ref);
256}
257int main(int argc, char **argv)
258{
259	struct test test;
260	int i;
261
262	test_init(&test, argc, argv);
263
264	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
265		int reps = REPS(i), sets = SETS(i);
266		enum target t;
267
268		for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
269			pixel_tests(&test, reps, sets, t, 0);
270			area_tests(&test, reps, sets, t, 0);
271			rect_tests(&test, reps, sets, t, 0);
272
273			pixel_tests(&test, reps, sets, t, 1);
274			area_tests(&test, reps, sets, t, 1);
275			rect_tests(&test, reps, sets, t, 1);
276		}
277	}
278
279	return 0;
280}
281