summaryrefslogtreecommitdiff
path: root/test/dri2-test.c
blob: bdf01f388ba023208a06e495dcd1203e82d79758 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xrandr.h>
#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <xcb/xcbext.h>
#include <xcb/dri2.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>

#include <xf86drm.h>
#include <drm.h>

#include "dri2.h"

#define COUNT 60

static int prime[] = { 0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41, 43, 47, 51, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131 };

static inline XRRScreenResources *_XRRGetScreenResourcesCurrent(Display *dpy, Window window)
{
	XRRScreenResources *res;

	res = XRRGetScreenResourcesCurrent(dpy, window);
	if (res == NULL)
		res = XRRGetScreenResources(dpy, window);

	return res;
}

static XRRModeInfo *lookup_mode(XRRScreenResources *res, int id)
{
	int i;

	for (i = 0; i < res->nmode; i++) {
		if (res->modes[i].id == id)
			return &res->modes[i];
	}

	return NULL;
}

static int dri2_open(Display *dpy)
{
	drm_auth_t auth;
	char *driver, *device;
	int fd;

	if (!DRI2Connect(dpy, DefaultRootWindow(dpy), &driver, &device))
		return -1;

	printf ("Connecting to %s driver on %s\n", driver, device);

	fd = open(device, O_RDWR);
	if (fd < 0)
		return -1;

	if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
		return -1;

	if (!DRI2Authenticate(dpy, DefaultRootWindow(dpy), auth.magic))
		return -1;

	return fd;
}

static void dri2_copy_swap(Display *dpy, Drawable d,
			   int width, int height, int has_front)
{
	XRectangle rect;
	XserverRegion region;

	rect.x = 0;
	rect.y = 0;
	rect.width = width;
	rect.height = height;

	region = XFixesCreateRegion(dpy, &rect, 1);
	DRI2CopyRegion(dpy, d, region, DRI2BufferFrontLeft, DRI2BufferBackLeft);
	if (has_front)
		DRI2CopyRegion(dpy, d, region, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
	XFixesDestroyRegion(dpy, region);
}

static double elapsed(const struct timespec *start,
		      const struct timespec *end)
{
	return (end->tv_sec - start->tv_sec) +
		1e-9*(end->tv_nsec - start->tv_nsec);
}

static uint64_t check_msc(Display *dpy, Window win, uint64_t last_msc)
{
	uint64_t current_msc, current_ust, current_sbc;
	DRI2GetMSC(dpy, win, &current_ust, &current_msc, &current_sbc);
	if (current_msc < last_msc) {
		printf("Invalid MSC: was %llu, now %llu\n",
		       (long long)last_msc, (long long)current_msc);
	}
	return current_msc;
}

static void wait_next_vblank(Display *dpy, Window win)
{
	uint64_t msc, ust, sbc;
	DRI2WaitMSC(dpy, win, 0, 1, 0, &ust, &msc, &sbc);
}

static void swap_buffers(xcb_connection_t *c, Window win,
		unsigned int *attachments, int nattachments)
{
	unsigned int seq[2];

	seq[0] = xcb_dri2_swap_buffers_unchecked(c, win,
						 0, 0, 0, 0, 0, 0).sequence;


	seq[1] = xcb_dri2_get_buffers_unchecked(c, win,
						nattachments, nattachments,
						attachments).sequence;

	xcb_flush(c);
	xcb_discard_reply(c, seq[0]);
	xcb_discard_reply(c, seq[1]);
}

static void run(Display *dpy, int width, int height,
		unsigned int *attachments, int nattachments,
		const char *name)
{
	xcb_connection_t *c = XGetXCBConnection(dpy);
	Window win;
	XSetWindowAttributes attr;
	DRI2Buffer *buffers;
	struct timespec start, end;
	uint64_t start_msc, end_msc;
	int modulus, remainder, count;

	/* Be nasty and install a fullscreen window on top so that we
	 * can guarantee we do not get clipped by children.
	 */
	attr.override_redirect = 1;
	win = XCreateWindow(dpy, DefaultRootWindow(dpy),
			 0, 0, width, height, 0,
			 DefaultDepth(dpy, DefaultScreen(dpy)),
			 InputOutput,
			 DefaultVisual(dpy, DefaultScreen(dpy)),
			 CWOverrideRedirect, &attr);
	XMapWindow(dpy, win);

	DRI2CreateDrawable(dpy, win);
	DRI2SwapInterval(dpy, win, 1);
	start_msc = check_msc(dpy, win, 0);

	buffers = DRI2GetBuffers(dpy, win, &width, &height,
				 attachments, nattachments, &count);
	if (count != nattachments)
		return;

	swap_buffers(c, win, attachments, nattachments);
	start_msc = check_msc(dpy, win, start_msc);
	clock_gettime(CLOCK_MONOTONIC, &start);
	for (count = 0; count < COUNT; count++)
		swap_buffers(c, win, attachments, nattachments);
	end_msc = check_msc(dpy, win, start_msc);
	clock_gettime(CLOCK_MONOTONIC, &end);
	printf("%d [%ld] %s (%dx%d) swaps in %fs.\n",
	       count, (long)(end_msc - start_msc),
	       name, width, height, elapsed(&start, &end));

	swap_buffers(c, win, attachments, nattachments);
	start_msc = check_msc(dpy, win, end_msc);
	clock_gettime(CLOCK_MONOTONIC, &start);
	for (count = 0; count < COUNT; count++)
		dri2_copy_swap(dpy, win, width, height, nattachments == 2);
	end_msc = check_msc(dpy, win, start_msc);
	clock_gettime(CLOCK_MONOTONIC, &end);

	printf("%d [%ld] %s (%dx%d) blits in %fs.\n",
	       count, (long)(end_msc - start_msc),
	       name, width, height, elapsed(&start, &end));

	DRI2SwapInterval(dpy, win, 0);
	wait_next_vblank(dpy, win);

	swap_buffers(c, win, attachments, nattachments);
	start_msc = check_msc(dpy, win, end_msc);
	clock_gettime(CLOCK_MONOTONIC, &start);
	for (count = 0; count < COUNT; count++)
		swap_buffers(c, win, attachments, nattachments);
	end_msc = check_msc(dpy, win, start_msc);
	clock_gettime(CLOCK_MONOTONIC, &end);
	printf("%d [%ld] %s (%dx%d) vblank=0 swaps in %fs.\n",
	       count, (long)(end_msc - start_msc),
	       name, width, height, elapsed(&start, &end));

	start_msc = check_msc(dpy, win, end_msc);
	clock_gettime(CLOCK_MONOTONIC, &start);
	for (count = 0; count < COUNT; count++)
		wait_next_vblank(dpy, win);
	end_msc = check_msc(dpy, win, start_msc);
	clock_gettime(CLOCK_MONOTONIC, &end);
	printf("%d [%ld] %s waits in %fs.\n",
	       count, (long)(end_msc - start_msc),
	       name, elapsed(&start, &end));

	printf("Testing past & future waits\n");
	for (modulus = 1; modulus <= 128; modulus <<= 1) {
		for (count = 0;  prime[count] < modulus; count++) {
			uint64_t msc, ust, sbc;
			uint64_t target;

			remainder = prime[count];

			DRI2WaitMSC(dpy, win, 0, 1, 0, &ust, &msc, &sbc);

			target = msc + modulus + 1;
			target &= -modulus;
			target += remainder;

			DRI2WaitMSC(dpy, win, target, modulus, remainder,
				    &ust, &msc, &sbc);
			if (msc != target) {
				printf("Missed future MSC (%d, %d): expected=%lld, found=%lld\n",
				       modulus, remainder,
				       (long long)target, (long long)msc);
			}

			target = msc;
			target &= -modulus;
			target += remainder;
			if (target <= msc)
				target += modulus;

			DRI2WaitMSC(dpy, win, msc, modulus, remainder,
				    &ust, &msc, &sbc);

			if (msc != target) {
				printf("Missed past MSC (%d, %d): expected=%lld, found=%lld\n",
				       modulus, remainder,
				       (long long)target, (long long)msc);
			}
		}
	}

	XDestroyWindow(dpy, win);
	free(buffers);

	XSync(dpy, 1);
}

int main(void)
{
	Display *dpy;
	int i, j, fd;
	unsigned int attachments[] = {
		DRI2BufferBackLeft,
		DRI2BufferFrontLeft,
	};
	XRRScreenResources *res;
	XRRCrtcInfo **original_crtc;
	Window root;
	uint64_t last_msc;

	dpy = XOpenDisplay(NULL);
	if (dpy == NULL)
		return 77;

	if (!XRRQueryVersion(dpy, &i, &j))
		return 77;

	fd = dri2_open(dpy);
	if (fd < 0)
		return 1;

	root = DefaultRootWindow(dpy);
	DRI2CreateDrawable(dpy, root);

	res = _XRRGetScreenResourcesCurrent(dpy, root);
	if (res == NULL)
		return 1;

	original_crtc = malloc(sizeof(XRRCrtcInfo *)*res->ncrtc);
	for (i = 0; i < res->ncrtc; i++)
		original_crtc[i] = XRRGetCrtcInfo(dpy, res, res->crtcs[i]);

	printf("noutput=%d, ncrtc=%d\n", res->noutput, res->ncrtc);
	last_msc = check_msc(dpy, root, 0);
	for (i = 0; i < res->ncrtc; i++)
		XRRSetCrtcConfig(dpy, res, res->crtcs[i], CurrentTime,
				 0, 0, None, RR_Rotate_0, NULL, 0);
	last_msc = check_msc(dpy, root, last_msc);

	for (i = 0; i < res->noutput; i++) {
		XRROutputInfo *output;
		XRRModeInfo *mode;

		output = XRRGetOutputInfo(dpy, res, res->outputs[i]);
		if (output == NULL)
			continue;

		mode = NULL;
		if (res->nmode)
			mode = lookup_mode(res, output->modes[0]);

		for (j = 0; mode && j < 2*output->ncrtc; j++) {
			int c = j;
			if (c >= output->ncrtc)
				c = 2*output->ncrtc - j - 1;

			printf("[%d, %d] -- OUTPUT:%ld, CRTC:%ld\n",
			       i, c, (long)res->outputs[i], (long)output->crtcs[c]);
			last_msc = check_msc(dpy, root, last_msc);
			XRRSetCrtcConfig(dpy, res, output->crtcs[c], CurrentTime,
					 0, 0, output->modes[0], RR_Rotate_0, &res->outputs[i], 1);
			last_msc = check_msc(dpy, root, last_msc);

			run(dpy, mode->width, mode->height, attachments, 1, "fullscreen");
			run(dpy, mode->width, mode->height, attachments, 2, "fullscreen (with front)");

			run(dpy, mode->width/2, mode->height/2, attachments, 1, "windowed");
			run(dpy, mode->width/2, mode->height/2, attachments, 2, "windowed (with front)");

			last_msc = check_msc(dpy, root, last_msc);
			XRRSetCrtcConfig(dpy, res, output->crtcs[c], CurrentTime,
					 0, 0, None, RR_Rotate_0, NULL, 0);
			last_msc = check_msc(dpy, root, last_msc);
		}

		XRRFreeOutputInfo(output);
	}

	for (i = 0; i < res->ncrtc; i++)
		XRRSetCrtcConfig(dpy, res, res->crtcs[i], CurrentTime,
				 original_crtc[i]->x,
				 original_crtc[i]->y,
				 original_crtc[i]->mode,
				 original_crtc[i]->rotation,
				 original_crtc[i]->outputs,
				 original_crtc[i]->noutput);
	return 0;
}