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
9428d7b3dSmrg#define SIZE 20000
10428d7b3dSmrgstruct draw {
11428d7b3dSmrg	Pixmap a, b;
12428d7b3dSmrg	Picture pa, pb;
13428d7b3dSmrg	GC gc;
14428d7b3dSmrg	XRenderPictFormat *format;
15428d7b3dSmrg};
16428d7b3dSmrg
17428d7b3dSmrgstatic void target_init(struct test_display *t, struct draw *tt, int size)
18428d7b3dSmrg{
19428d7b3dSmrg	XRenderColor color;
20428d7b3dSmrg
21428d7b3dSmrg	tt->format = XRenderFindStandardFormat(t->dpy, PictStandardARGB32);
22428d7b3dSmrg
23428d7b3dSmrg	tt->a = XCreatePixmap(t->dpy, DefaultRootWindow(t->dpy),
24428d7b3dSmrg			      size, size, tt->format->depth);
25428d7b3dSmrg	tt->pa = XRenderCreatePicture(t->dpy, tt->a, tt->format, 0, NULL);
26428d7b3dSmrg
27428d7b3dSmrg	color.alpha = 0xffff;
28428d7b3dSmrg	color.red = 0xffff;
29428d7b3dSmrg	color.green = 0;
30428d7b3dSmrg	color.blue = 0;
31428d7b3dSmrg	XRenderFillRectangle(t->dpy, PictOpSrc, tt->pa, &color, 0, 0, size, size);
32428d7b3dSmrg
33428d7b3dSmrg	tt->b = XCreatePixmap(t->dpy, DefaultRootWindow(t->dpy),
34428d7b3dSmrg			      size, size, tt->format->depth);
35428d7b3dSmrg	tt->pb = XRenderCreatePicture(t->dpy, tt->b, tt->format, 0, NULL);
36428d7b3dSmrg
37428d7b3dSmrg	color.alpha = 0xffff;
38428d7b3dSmrg	color.red = 0;
39428d7b3dSmrg	color.green = 0;
40428d7b3dSmrg	color.blue = 0xffff;
41428d7b3dSmrg	XRenderFillRectangle(t->dpy, PictOpSrc, tt->pb, &color, 0, 0, size, size);
42428d7b3dSmrg}
43428d7b3dSmrg
44428d7b3dSmrgstatic void target_fini(struct test_display *t, struct draw *tt)
45428d7b3dSmrg{
46428d7b3dSmrg	XRenderFreePicture(t->dpy, tt->pa);
47428d7b3dSmrg	XFreePixmap(t->dpy, tt->a);
48428d7b3dSmrg
49428d7b3dSmrg	XRenderFreePicture(t->dpy, tt->pb);
50428d7b3dSmrg	XFreePixmap(t->dpy, tt->b);
51428d7b3dSmrg}
52428d7b3dSmrg
53428d7b3dSmrgint main(int argc, char **argv)
54428d7b3dSmrg{
55428d7b3dSmrg	struct test test;
56428d7b3dSmrg	struct draw out, ref;
57428d7b3dSmrg	int size, i;
58428d7b3dSmrg
59428d7b3dSmrg	test_init(&test, argc, argv);
60428d7b3dSmrg
61428d7b3dSmrg	/* Copy back and forth betwenn two pixmaps, gradually getting larger */
62428d7b3dSmrg	for (size = 1; size <= SIZE; size = (size * 3 + 1) / 2) {
63428d7b3dSmrg		target_init(&test.out, &out, size);
64428d7b3dSmrg		target_init(&test.ref, &ref, size);
65428d7b3dSmrg
66428d7b3dSmrg		printf("size=%d\n", size);
67428d7b3dSmrg		for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
68428d7b3dSmrg			int reps = REPS(i);
69428d7b3dSmrg			do {
70428d7b3dSmrg				int sx = rand() % (2*size) - size;
71428d7b3dSmrg				int sy = rand() % (2*size) - size;
72428d7b3dSmrg
73428d7b3dSmrg				int dx = rand() % (2*size) - size;
74428d7b3dSmrg				int dy = rand() % (2*size) - size;
75428d7b3dSmrg
76428d7b3dSmrg				int w = rand() % size;
77428d7b3dSmrg				int h = rand() % size;
78428d7b3dSmrg
79428d7b3dSmrg				int order = rand() & 1;
80428d7b3dSmrg
81428d7b3dSmrg				XRenderComposite(test.out.dpy, PictOpSrc,
82428d7b3dSmrg						 order ? out.pa : out.pb,
83428d7b3dSmrg						 0,
84428d7b3dSmrg						 (!order) ? out.pa : out.pb,
85428d7b3dSmrg						 sx, sy,
86428d7b3dSmrg						 0, 0,
87428d7b3dSmrg						 dx, dy,
88428d7b3dSmrg						 w, h);
89428d7b3dSmrg
90428d7b3dSmrg				XRenderComposite(test.ref.dpy, PictOpSrc,
91428d7b3dSmrg						 order ? ref.pa : ref.pb,
92428d7b3dSmrg						 0,
93428d7b3dSmrg						 (!order) ? ref.pa : ref.pb,
94428d7b3dSmrg						 sx, sy,
95428d7b3dSmrg						 0, 0,
96428d7b3dSmrg						 dx, dy,
97428d7b3dSmrg						 w, h);
98428d7b3dSmrg			} while (--reps);
99428d7b3dSmrg		}
100428d7b3dSmrg
101428d7b3dSmrg		test_compare(&test,
102428d7b3dSmrg			     out.a, out.format,
103428d7b3dSmrg			     ref.a, ref.format,
104428d7b3dSmrg			     0, 0, size, size,
105428d7b3dSmrg			     "");
106428d7b3dSmrg		test_compare(&test,
107428d7b3dSmrg			     out.b, out.format,
108428d7b3dSmrg			     ref.b, ref.format,
109428d7b3dSmrg			     0, 0, size, size,
110428d7b3dSmrg			     "");
111428d7b3dSmrg
112428d7b3dSmrg		target_fini(&test.out, &out);
113428d7b3dSmrg		target_fini(&test.ref, &ref);
114428d7b3dSmrg	}
115428d7b3dSmrg
116428d7b3dSmrg	return 0;
117428d7b3dSmrg}
118