summaryrefslogtreecommitdiff
path: root/test/mixed-stress.c
blob: 42e2c0824b84fccfb3cc8a502faa7ba038fdd948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xutil.h> /* for XDestroyImage */
#include <pixman.h>

#include "test.h"

static void _render_copy(struct test_target *tt,
			 int x, int y, int w, int h,
			 uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
	XRenderColor color;
	Picture src;
	Pixmap tmp;

	tmp = XCreatePixmap(tt->dpy->dpy, DefaultRootWindow(tt->dpy->dpy),
			    10+w, 20+h, tt->format->depth);
	src = XRenderCreatePicture(tt->dpy->dpy, tmp, tt->format, 0, NULL);

	/* magenta border */
	color.red = 0xffff;
	color.green = 0;
	color.blue = 0xffff;
	color.alpha = 0xffff;
	XRenderFillRectangle(tt->dpy->dpy, PictOpSrc, src, &color, 0, 0, w+10, h+20);

	color.red = red * alpha;
	color.green = green * alpha;
	color.blue = blue * alpha;
	color.alpha = alpha << 8 | alpha;
	XRenderFillRectangle(tt->dpy->dpy, PictOpSrc, src, &color, 5, 10, w, h);

	XRenderComposite(tt->dpy->dpy, PictOpSrc,
			 src, 0, tt->picture,
			 5, 10,
			 0, 0,
			 x, y,
			 w, h);

	XRenderFreePicture(tt->dpy->dpy, src);
	XFreePixmap(tt->dpy->dpy, tmp);
}

static void render_copy(struct test_target *real,
			struct test_target *ref)
{
	int x = rand() % (2*real->width) - real->width;
	int y = rand() % (2*real->height) - real->height;
	int w = rand() % (2*real->width);
	int h = rand() % (2*real->height);
	int red = rand() & 0xff;
	int green = rand() & 0xff;
	int blue = rand() & 0xff;
	int alpha = rand() & 0xff;

	_render_copy(real, x, y, w, h, red, green, blue, alpha);
	_render_copy(ref, x, y, w, h, red, green, blue, alpha);
}

static void fill_rect(struct test_target *tt,
		      int alu, int color,
		      int x, int y, int w, int h)
{
	XGCValues val;

	val.function = alu;
	val.foreground = color;
	XChangeGC(tt->dpy->dpy, tt->gc, GCFunction | GCForeground, &val);

	XFillRectangle(tt->dpy->dpy, tt->draw, tt->gc, x, y, w, h);
}

static void clear(struct test_target *tt)
{
	fill_rect(tt,
		  GXcopy, 0,
		  0, 0, tt->width, tt->height);
}

static void basic_fill(struct test_target *real,
		       struct test_target *ref)
{
	int x = rand() % (2*real->width) - real->width;
	int y = rand() % (2*real->height) - real->height;
	int w = rand() % (2*real->width);
	int h = rand() % (2*real->height);
	int color = rand();
	int alu = rand() % 16;

	fill_rect(real, alu, color, x, y, w, h);
	fill_rect(ref, alu, color, x, y, w, h);
}

static void basic_copy(struct test_target *real,
		       struct test_target *ref)
{
	int sx = rand() % (2*real->width) - ref->width;
	int sy = rand() % (2*real->height) - ref->height;
	int dx = rand() % (2*real->width) - ref->width;
	int dy = rand() % (2*real->height) - ref->height;
	int w = rand() % (2*real->width);
	int h = rand() % (2*real->height);
	XGCValues val;

	val.function = rand() % 16;

	XChangeGC(real->dpy->dpy, real->gc, GCFunction, &val);
	XCopyArea(real->dpy->dpy,
		  real->draw, real->draw, real->gc,
		  sx, sy, w, h, dx, dy);

	XChangeGC(ref->dpy->dpy, ref->gc, GCFunction, &val);
	XCopyArea(ref->dpy->dpy,
		  ref->draw, ref->draw, ref->gc,
		  sx, sy, w, h, dx, dy);
}

static void _put(struct test_target *tt,
		 int x, int y, int w,int h, int color, int alu)
{
	XImage image;
	XGCValues val;

	val.function = alu;

	test_init_image(&image, &tt->dpy->shm, tt->format, w, h);
	pixman_fill((uint32_t*)image.data,
		    image.bytes_per_line/sizeof(uint32_t),
		    image.bits_per_pixel,
		    0, 0, w, h, color);

	XChangeGC(tt->dpy->dpy, tt->gc, GCFunction, &val);
	if (rand() & 1) {
		XShmPutImage(tt->dpy->dpy, tt->draw, tt->gc, &image,
			     0, 0, x, y, w, h, 0);
		XSync(tt->dpy->dpy, 1);
	} else {
		XPutImage(tt->dpy->dpy, tt->draw, tt->gc, &image,
			  0, 0, x, y, w, h);
	}
}

static void basic_put(struct test_target *real,
		      struct test_target *ref)
{
	int x = rand() % (2*real->width) - real->width;
	int y = rand() % (2*real->height) - real->height;
	int w = rand() % real->width;
	int h = rand() % real->height;
	int color = rand();
	int alu = rand() % 16;

	_put(real, x, y, w, h, color, alu);
	_put(ref, x, y, w, h, color, alu);
}

static void rect_tests(struct test *test, int iterations, enum target target)
{
	struct test_target real, ref;
	void (* const ops[])(struct test_target *, struct test_target *) = {
		basic_copy,
		basic_fill,
		basic_put,
		render_copy,
	};
	int n;

	printf("Running mixed ops stress against %s: ",
	       test_target_name(target));
	fflush(stdout);

	test_target_create_render(&test->real, target, &real);
	test_target_create_render(&test->ref, target, &ref);

	clear(&real);
	clear(&ref);

	for (n = 0; n < iterations; n++)
		ops[rand() % ARRAY_SIZE(ops)](&real, &ref);

	test_compare(test,
		     real.draw, real.format,
		     ref.draw, ref.format,
		     0, 0, real.width, real.height,
		     "");

	printf("passed [%d iterations]\n", n);

	test_target_destroy_render(&test->real, &real);
	test_target_destroy_render(&test->ref, &ref);
}

int main(int argc, char **argv)
{
	struct test test;
	int i;

	test_init(&test, argc, argv);

	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
		int iterations = 1 << i;
		rect_tests(&test, iterations, 0);
		rect_tests(&test, iterations, 1);
	}

	return 0;
}