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 const uint8_t ops[] = {
11	PictOpClear,
12	PictOpSrc,
13	PictOpDst,
14};
15
16static void fill_rect(struct test_display *dpy, Picture p, uint8_t op,
17		      int x, int y, int w, int h,
18		      uint8_t s_red, uint8_t s_green, uint8_t s_blue, uint8_t s_alpha,
19		      uint8_t m_red, uint8_t m_green, uint8_t m_blue, uint8_t m_alpha)
20{
21	XRenderColor render_color;
22	Picture source, mask;
23
24	render_color.red   = s_red * s_alpha;
25	render_color.green = s_green * s_alpha;
26	render_color.blue  = s_blue * s_alpha;
27	render_color.alpha = s_alpha << 8 | s_alpha;
28	source = XRenderCreateSolidFill(dpy->dpy, &render_color);
29
30	render_color.red   = m_red * m_alpha;
31	render_color.green = m_green * m_alpha;
32	render_color.blue  = m_blue * m_alpha;
33	render_color.alpha = m_alpha << 8 | m_alpha;
34	mask = XRenderCreateSolidFill(dpy->dpy, &render_color);
35
36	XRenderComposite(dpy->dpy, op, source, mask, p, 0, 0, 0, 0, x, y, w,h);
37
38	XRenderFreePicture(dpy->dpy, mask);
39	XRenderFreePicture(dpy->dpy, source);
40}
41
42static void clear(struct test_display *dpy, struct test_target *tt)
43{
44	XRenderColor render_color = {0};
45	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
46			     0, 0, tt->width, tt->height);
47}
48
49static void ref_tests(struct test *t, int reps, int sets, enum target target)
50{
51	struct test_target out, ref;
52	int r, s;
53
54	printf("Testing area fills (%s): ", test_target_name(target));
55	fflush(stdout);
56
57	test_target_create_render(&t->out, target, &out);
58	clear(&t->out, &out);
59
60	test_target_create_render(&t->ref, target, &ref);
61	clear(&t->ref, &ref);
62
63	for (s = 0; s < sets; s++) {
64		for (r = 0; r < reps; r++) {
65			int x = rand() % (2*out.width) - out.width;
66			int y = rand() % (2*out.height) - out.height;
67			int w = rand() % out.width;
68			int h = rand() % out.height;
69			int op = ops[rand() % sizeof(ops)];
70			int s_red = rand() % 0xff;
71			int s_green = rand() % 0xff;
72			int s_blue = rand() % 0xff;
73			int s_alpha = rand() % 0xff;
74			int m_red = rand() % 0xff;
75			int m_green = rand() % 0xff;
76			int m_blue = rand() % 0xff;
77			int m_alpha = rand() % 0xff;
78
79			fill_rect(&t->out, out.picture,
80				  op, x, y, w, h,
81				  s_red, s_green, s_blue, s_alpha,
82				  m_red, m_green, m_blue, m_alpha);
83			fill_rect(&t->ref, ref.picture,
84				  op, x, y, w, h,
85				  s_red, s_green, s_blue, s_alpha,
86				  m_red, m_green, m_blue, m_alpha);
87		}
88
89		test_compare(t,
90			     out.draw, out.format,
91			     ref.draw, ref.format,
92			     0, 0, out.width, out.height,
93			     "");
94	}
95
96	printf("passed [%d iterations x %d]\n", reps, sets);
97
98	test_target_destroy_render(&t->out, &out);
99	test_target_destroy_render(&t->ref, &ref);
100}
101
102int main(int argc, char **argv)
103{
104	struct test test;
105	int i;
106
107	test_init(&test, argc, argv);
108
109	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
110		int reps = REPS(i), sets = SETS(i);
111		enum target t;
112
113		for (t = TARGET_FIRST; t <= TARGET_LAST; t++)
114			ref_tests(&test, reps, sets, t);
115	}
116
117	return 0;
118}
119