1#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4
5#include <X11/Xlib.h>
6#include <X11/Xutil.h>
7#include <X11/extensions/Xfixes.h>
8#include <unistd.h>
9#include <fcntl.h>
10#include <string.h>
11#include <time.h>
12
13#include <xf86drm.h>
14#include <drm.h>
15
16#include "dri2.h"
17
18#define COUNT 60
19
20static int dri2_open(Display *dpy)
21{
22	drm_auth_t auth;
23	char *driver, *device;
24	int fd;
25
26	if (!DRI2Connect(dpy, DefaultRootWindow(dpy), &driver, &device))
27		return -1;
28
29	printf ("Connecting to %s driver on %s\n", driver, device);
30
31	fd = open(device, O_RDWR);
32	if (fd < 0)
33		return -1;
34
35	if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
36		return -1;
37
38	if (!DRI2Authenticate(dpy, DefaultRootWindow(dpy), auth.magic))
39		return -1;
40
41	return fd;
42}
43
44static void run(Display *dpy, int width, int height,
45		unsigned int *attachments, int nattachments,
46		const char *name)
47{
48	Window win;
49	XSetWindowAttributes attr;
50	int count, loop;
51	DRI2Buffer *buffers;
52
53	/* Be nasty and install a fullscreen window on top so that we
54	 * can guarantee we do not get clipped by children.
55	 */
56	attr.override_redirect = 1;
57	loop = 100;
58	do {
59		win = XCreateWindow(dpy, DefaultRootWindow(dpy),
60				    0, 0, width, height, 0,
61				    DefaultDepth(dpy, DefaultScreen(dpy)),
62				    InputOutput,
63				    DefaultVisual(dpy, DefaultScreen(dpy)),
64				    CWOverrideRedirect, &attr);
65		XMapWindow(dpy, win);
66
67		DRI2CreateDrawable(dpy, win);
68
69		buffers = DRI2GetBuffers(dpy, win, &width, &height,
70					 attachments, nattachments, &count);
71		if (count != nattachments)
72			return;
73
74		free(buffers);
75		for (count = 0; count < loop; count++)
76			DRI2SwapBuffers(dpy, win, 0, 0, 0);
77		XDestroyWindow(dpy, win);
78	} while (--loop);
79
80	XSync(dpy, 1);
81	sleep(2);
82	XSync(dpy, 1);
83}
84
85int main(void)
86{
87	Display *dpy;
88	int width, height, fd;
89	unsigned int attachments[] = {
90		DRI2BufferBackLeft,
91		DRI2BufferFrontLeft,
92	};
93
94	dpy = XOpenDisplay (NULL);
95	if (dpy == NULL)
96		return 77;
97
98	fd = dri2_open(dpy);
99	if (fd < 0)
100		return 1;
101
102	width = WidthOfScreen(DefaultScreenOfDisplay(dpy));
103	height = HeightOfScreen(DefaultScreenOfDisplay(dpy));
104	run(dpy, width, height, attachments, 1, "fullscreen");
105	run(dpy, width, height, attachments, 2, "fullscreen (with front)");
106
107	width /= 2;
108	height /= 2;
109	run(dpy, width, height, attachments, 1, "windowed");
110	run(dpy, width, height, attachments, 2, "windowed (with front)");
111
112	return 0;
113}
114