1428d7b3dSmrg#include <stdint.h>
2428d7b3dSmrg#include <stdio.h>
3428d7b3dSmrg#include <stdlib.h>
4428d7b3dSmrg
5428d7b3dSmrg#include <X11/Xutil.h> /* for XDestroyImage */
6428d7b3dSmrg
7428d7b3dSmrg#include "test.h"
8428d7b3dSmrg
9428d7b3dSmrgstatic void draw_rect(struct test_display *t, Drawable d, uint8_t alu,
10428d7b3dSmrg		      int x, int y, int w, int h, uint32_t fg, int lw)
11428d7b3dSmrg{
12428d7b3dSmrg	XGCValues val;
13428d7b3dSmrg	GC gc;
14428d7b3dSmrg
15428d7b3dSmrg	val.function = alu;
16428d7b3dSmrg	val.foreground = fg;
17428d7b3dSmrg	val.line_width = lw;
18428d7b3dSmrg
19428d7b3dSmrg	gc = XCreateGC(t->dpy, d, GCForeground | GCFunction | GCLineWidth, &val);
20428d7b3dSmrg	XDrawRectangle(t->dpy, d, gc, x, y, w, h);
21428d7b3dSmrg	XFreeGC(t->dpy, gc);
22428d7b3dSmrg}
23428d7b3dSmrg
24428d7b3dSmrgstatic void clear(struct test_display *dpy, struct test_target *tt)
25428d7b3dSmrg{
26428d7b3dSmrg	XRenderColor render_color = {0};
27428d7b3dSmrg	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
28428d7b3dSmrg			     0, 0, tt->width, tt->height);
29428d7b3dSmrg}
30428d7b3dSmrg
31428d7b3dSmrgstatic void zrect_tests(struct test *t, int reps, int sets, enum target target)
32428d7b3dSmrg{
33428d7b3dSmrg	struct test_target out, ref;
34428d7b3dSmrg	int r, s;
35428d7b3dSmrg
36428d7b3dSmrg	printf("Testing empty rects (%s): ", test_target_name(target));
37428d7b3dSmrg	fflush(stdout);
38428d7b3dSmrg
39428d7b3dSmrg	test_target_create_render(&t->out, target, &out);
40428d7b3dSmrg	clear(&t->out, &out);
41428d7b3dSmrg
42428d7b3dSmrg	test_target_create_render(&t->ref, target, &ref);
43428d7b3dSmrg	clear(&t->ref, &ref);
44428d7b3dSmrg
45428d7b3dSmrg	for (s = 0; s < sets; s++) {
46428d7b3dSmrg		for (r = 0; r < reps; r++) {
47428d7b3dSmrg			int x = rand() % (2*out.width) - out.width;
48428d7b3dSmrg			int y = rand() % (2*out.height) - out.height;
49428d7b3dSmrg			uint8_t alu = rand() % (GXset + 1);
50428d7b3dSmrg			uint32_t fg = rand();
51428d7b3dSmrg			uint32_t lw = rand() % 4;
52428d7b3dSmrg
53428d7b3dSmrg			draw_rect(&t->out, out.draw, alu,
54428d7b3dSmrg				  x, y, 0, 0, fg, lw);
55428d7b3dSmrg			draw_rect(&t->ref, ref.draw, alu,
56428d7b3dSmrg				  x, y, 0, 0, fg, lw);
57428d7b3dSmrg		}
58428d7b3dSmrg
59428d7b3dSmrg		test_compare(t,
60428d7b3dSmrg			     out.draw, out.format,
61428d7b3dSmrg			     ref.draw, ref.format,
62428d7b3dSmrg			     0, 0, out.width, out.height,
63428d7b3dSmrg			     "");
64428d7b3dSmrg	}
65428d7b3dSmrg
66428d7b3dSmrg	printf("passed [%d iterations x %d]\n", reps, sets);
67428d7b3dSmrg
68428d7b3dSmrg	test_target_destroy_render(&t->out, &out);
69428d7b3dSmrg	test_target_destroy_render(&t->ref, &ref);
70428d7b3dSmrg}
71428d7b3dSmrg
72428d7b3dSmrgstatic void hrect_tests(struct test *t, int reps, int sets, enum target target)
73428d7b3dSmrg{
74428d7b3dSmrg	struct test_target out, ref;
75428d7b3dSmrg	int r, s;
76428d7b3dSmrg
77428d7b3dSmrg	printf("Testing horizontal rects (%s): ", test_target_name(target));
78428d7b3dSmrg	fflush(stdout);
79428d7b3dSmrg
80428d7b3dSmrg	test_target_create_render(&t->out, target, &out);
81428d7b3dSmrg	clear(&t->out, &out);
82428d7b3dSmrg
83428d7b3dSmrg	test_target_create_render(&t->ref, target, &ref);
84428d7b3dSmrg	clear(&t->ref, &ref);
85428d7b3dSmrg
86428d7b3dSmrg	for (s = 0; s < sets; s++) {
87428d7b3dSmrg		for (r = 0; r < reps; r++) {
88428d7b3dSmrg			int x = rand() % (2*out.width) - out.width;
89428d7b3dSmrg			int y = rand() % (2*out.height) - out.height;
90428d7b3dSmrg			int w = rand() % (2*out.width);
91428d7b3dSmrg			uint8_t alu = rand() % (GXset + 1);
92428d7b3dSmrg			uint32_t fg = rand();
93428d7b3dSmrg			uint32_t lw = rand() % 4;
94428d7b3dSmrg
95428d7b3dSmrg			draw_rect(&t->out, out.draw, alu,
96428d7b3dSmrg				  x, y, w, 0, fg, lw);
97428d7b3dSmrg			draw_rect(&t->ref, ref.draw, alu,
98428d7b3dSmrg				  x, y, w, 0, fg, lw);
99428d7b3dSmrg		}
100428d7b3dSmrg
101428d7b3dSmrg		test_compare(t,
102428d7b3dSmrg			     out.draw, out.format,
103428d7b3dSmrg			     ref.draw, ref.format,
104428d7b3dSmrg			     0, 0, out.width, out.height,
105428d7b3dSmrg			     "");
106428d7b3dSmrg	}
107428d7b3dSmrg
108428d7b3dSmrg	printf("passed [%d iterations x %d]\n", reps, sets);
109428d7b3dSmrg
110428d7b3dSmrg	test_target_destroy_render(&t->out, &out);
111428d7b3dSmrg	test_target_destroy_render(&t->ref, &ref);
112428d7b3dSmrg}
113428d7b3dSmrg
114428d7b3dSmrgstatic void vrect_tests(struct test *t, int reps, int sets, enum target target)
115428d7b3dSmrg{
116428d7b3dSmrg	struct test_target out, ref;
117428d7b3dSmrg	int r, s;
118428d7b3dSmrg
119428d7b3dSmrg	printf("Testing vertical rects (%s): ", test_target_name(target));
120428d7b3dSmrg	fflush(stdout);
121428d7b3dSmrg
122428d7b3dSmrg	test_target_create_render(&t->out, target, &out);
123428d7b3dSmrg	clear(&t->out, &out);
124428d7b3dSmrg
125428d7b3dSmrg	test_target_create_render(&t->ref, target, &ref);
126428d7b3dSmrg	clear(&t->ref, &ref);
127428d7b3dSmrg
128428d7b3dSmrg	for (s = 0; s < sets; s++) {
129428d7b3dSmrg		for (r = 0; r < reps; r++) {
130428d7b3dSmrg			int x = rand() % (2*out.width) - out.width;
131428d7b3dSmrg			int y = rand() % (2*out.height) - out.height;
132428d7b3dSmrg			int h = rand() % (2*out.width);
133428d7b3dSmrg			uint8_t alu = rand() % (GXset + 1);
134428d7b3dSmrg			uint32_t fg = rand();
135428d7b3dSmrg			uint32_t lw = rand() % 4;
136428d7b3dSmrg
137428d7b3dSmrg			draw_rect(&t->out, out.draw, alu,
138428d7b3dSmrg				  x, y, 0, h, fg, lw);
139428d7b3dSmrg			draw_rect(&t->ref, ref.draw, alu,
140428d7b3dSmrg				  x, y, 0, h, fg, lw);
141428d7b3dSmrg		}
142428d7b3dSmrg
143428d7b3dSmrg		test_compare(t,
144428d7b3dSmrg			     out.draw, out.format,
145428d7b3dSmrg			     ref.draw, ref.format,
146428d7b3dSmrg			     0, 0, out.width, out.height,
147428d7b3dSmrg			     "");
148428d7b3dSmrg	}
149428d7b3dSmrg
150428d7b3dSmrg	printf("passed [%d iterations x %d]\n", reps, sets);
151428d7b3dSmrg
152428d7b3dSmrg	test_target_destroy_render(&t->out, &out);
153428d7b3dSmrg	test_target_destroy_render(&t->ref, &ref);
154428d7b3dSmrg}
155428d7b3dSmrg
156428d7b3dSmrgstatic void rect_tests(struct test *t, int reps, int sets, enum target target)
157428d7b3dSmrg{
158428d7b3dSmrg	struct test_target out, ref;
159428d7b3dSmrg	int r, s;
160428d7b3dSmrg
161428d7b3dSmrg	printf("Testing general (%s): ", test_target_name(target));
162428d7b3dSmrg	fflush(stdout);
163428d7b3dSmrg
164428d7b3dSmrg	test_target_create_render(&t->out, target, &out);
165428d7b3dSmrg	clear(&t->out, &out);
166428d7b3dSmrg
167428d7b3dSmrg	test_target_create_render(&t->ref, target, &ref);
168428d7b3dSmrg	clear(&t->ref, &ref);
169428d7b3dSmrg
170428d7b3dSmrg	for (s = 0; s < sets; s++) {
171428d7b3dSmrg		for (r = 0; r < reps; r++) {
172428d7b3dSmrg			int x = rand() % (2*out.width) - out.width;
173428d7b3dSmrg			int y = rand() % (2*out.height) - out.height;
174428d7b3dSmrg			int w = rand() % (2*out.width);
175428d7b3dSmrg			int h = rand() % (2*out.height);
176428d7b3dSmrg			uint8_t alu = rand() % (GXset + 1);
177428d7b3dSmrg			uint32_t fg = rand();
178428d7b3dSmrg			uint32_t lw = rand() % 4;
179428d7b3dSmrg
180428d7b3dSmrg			draw_rect(&t->out, out.draw, alu,
181428d7b3dSmrg				  x, y, w, h, fg, lw);
182428d7b3dSmrg			draw_rect(&t->ref, ref.draw, alu,
183428d7b3dSmrg				  x, y, w, h, fg, lw);
184428d7b3dSmrg		}
185428d7b3dSmrg
186428d7b3dSmrg		test_compare(t,
187428d7b3dSmrg			     out.draw, out.format,
188428d7b3dSmrg			     ref.draw, ref.format,
189428d7b3dSmrg			     0, 0, out.width, out.height,
190428d7b3dSmrg			     "");
191428d7b3dSmrg	}
192428d7b3dSmrg
193428d7b3dSmrg	printf("passed [%d iterations x %d]\n", reps, sets);
194428d7b3dSmrg
195428d7b3dSmrg	test_target_destroy_render(&t->out, &out);
196428d7b3dSmrg	test_target_destroy_render(&t->ref, &ref);
197428d7b3dSmrg}
198428d7b3dSmrg
199428d7b3dSmrgint main(int argc, char **argv)
200428d7b3dSmrg{
201428d7b3dSmrg	struct test test;
202428d7b3dSmrg	int i;
203428d7b3dSmrg
204428d7b3dSmrg	test_init(&test, argc, argv);
205428d7b3dSmrg
206428d7b3dSmrg	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
207428d7b3dSmrg		int reps = REPS(i), sets = SETS(i);
208428d7b3dSmrg		enum target t;
209428d7b3dSmrg
210428d7b3dSmrg		for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
211428d7b3dSmrg			zrect_tests(&test, reps, sets, t);
212428d7b3dSmrg			hrect_tests(&test, reps, sets, t);
213428d7b3dSmrg			vrect_tests(&test, reps, sets, t);
214428d7b3dSmrg			rect_tests(&test, reps, sets, t);
215428d7b3dSmrg		}
216428d7b3dSmrg	}
217428d7b3dSmrg
218428d7b3dSmrg	return 0;
219428d7b3dSmrg}
220