dri2: Event driven buffer validation.
[mesa.git] / src / glx / dri2.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33
34 #ifdef GLX_DIRECT_RENDERING
35
36 #define NEED_REPLIES
37 #include <stdio.h>
38 #include <X11/Xlibint.h>
39 #include <X11/extensions/Xext.h>
40 #include <X11/extensions/extutil.h>
41 #include <X11/extensions/dri2proto.h>
42 #include "xf86drm.h"
43 #include "dri2.h"
44 #include "glxclient.h"
45 #include "GL/glxext.h"
46
47 /* Allow the build to work with an older versions of dri2proto.h and
48 * dri2tokens.h.
49 */
50 #if DRI2_MINOR < 1
51 #undef DRI2_MINOR
52 #define DRI2_MINOR 1
53 #define X_DRI2GetBuffersWithFormat 7
54 #endif
55
56
57 static char dri2ExtensionName[] = DRI2_NAME;
58 static XExtensionInfo *dri2Info;
59 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
60
61 static Bool
62 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire);
63 static Status
64 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire);
65
66 static /* const */ XExtensionHooks dri2ExtensionHooks = {
67 NULL, /* create_gc */
68 NULL, /* copy_gc */
69 NULL, /* flush_gc */
70 NULL, /* free_gc */
71 NULL, /* create_font */
72 NULL, /* free_font */
73 DRI2CloseDisplay, /* close_display */
74 DRI2WireToEvent, /* wire_to_event */
75 DRI2EventToWire, /* event_to_wire */
76 NULL, /* error */
77 NULL, /* error_string */
78 };
79
80 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
81 dri2Info,
82 dri2ExtensionName,
83 &dri2ExtensionHooks,
84 DRI2NumberEvents, NULL)
85
86 static Bool
87 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire)
88 {
89 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
90 XExtDisplayInfo *glx_info = __glXFindDisplay(dpy);
91 static int glx_event_base;
92 static Bool found_glx_info = False;
93
94 XextCheckExtension(dpy, info, dri2ExtensionName, False);
95
96 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
97
98 #ifdef X_DRI2SwapBuffers
99 case DRI2_BufferSwapComplete:
100 {
101 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
102 xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
103 aevent->serial = _XSetLastRequestRead(dpy, (xGenericReply *) wire);
104 aevent->type =
105 (glx_info->codes->first_event + GLX_BufferSwapComplete) & 0x75;
106 aevent->send_event = (awire->type & 0x80) != 0;
107 aevent->display = dpy;
108 aevent->drawable = awire->drawable;
109 switch (awire->event_type) {
110 case DRI2_EXCHANGE_COMPLETE:
111 aevent->event_type = GLX_EXCHANGE_COMPLETE_INTEL;
112 break;
113 case DRI2_BLIT_COMPLETE:
114 aevent->event_type = GLX_BLIT_COMPLETE_INTEL;
115 break;
116 case DRI2_FLIP_COMPLETE:
117 aevent->event_type = GLX_FLIP_COMPLETE_INTEL;
118 break;
119 default:
120 /* unknown swap completion type */
121 return False;
122 }
123 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
124 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
125 aevent->sbc = ((CARD64)awire->sbc_hi << 32) | awire->sbc_lo;
126 return True;
127 }
128 #endif
129 #ifdef DRI2_InvalidateBuffers
130 case DRI2_InvalidateBuffers:
131 {
132 xDRI2InvalidateBuffers *awire = (xDRI2InvalidateBuffers *)wire;
133
134 dri2InvalidateBuffers(dpy, awire->drawable);
135 return False;
136 }
137 #endif
138 default:
139 /* client doesn't support server event */
140 break;
141 }
142
143 return False;
144 }
145
146 /* We don't actually support this. It doesn't make sense for clients to
147 * send each other DRI2 events.
148 */
149 static Status
150 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire)
151 {
152 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
153
154 XextCheckExtension(dpy, info, dri2ExtensionName, False);
155
156 switch (event->type) {
157 default:
158 /* client doesn't support server event */
159 break;
160 }
161
162 return Success;
163 }
164
165 Bool
166 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
167 {
168 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
169
170 if (XextHasExtension(info)) {
171 *eventBase = info->codes->first_event;
172 *errorBase = info->codes->first_error;
173 return True;
174 }
175
176 return False;
177 }
178
179 Bool
180 DRI2QueryVersion(Display * dpy, int *major, int *minor)
181 {
182 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
183 xDRI2QueryVersionReply rep;
184 xDRI2QueryVersionReq *req;
185
186 XextCheckExtension(dpy, info, dri2ExtensionName, False);
187
188 LockDisplay(dpy);
189 GetReq(DRI2QueryVersion, req);
190 req->reqType = info->codes->major_opcode;
191 req->dri2ReqType = X_DRI2QueryVersion;
192 req->majorVersion = DRI2_MAJOR;
193 req->minorVersion = DRI2_MINOR;
194 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
195 UnlockDisplay(dpy);
196 SyncHandle();
197 return False;
198 }
199 *major = rep.majorVersion;
200 *minor = rep.minorVersion;
201 UnlockDisplay(dpy);
202 SyncHandle();
203
204 return True;
205 }
206
207 Bool
208 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
209 {
210 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
211 xDRI2ConnectReply rep;
212 xDRI2ConnectReq *req;
213
214 XextCheckExtension(dpy, info, dri2ExtensionName, False);
215
216 LockDisplay(dpy);
217 GetReq(DRI2Connect, req);
218 req->reqType = info->codes->major_opcode;
219 req->dri2ReqType = X_DRI2Connect;
220 req->window = window;
221 req->driverType = DRI2DriverDRI;
222 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
223 UnlockDisplay(dpy);
224 SyncHandle();
225 return False;
226 }
227
228 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
229 UnlockDisplay(dpy);
230 SyncHandle();
231 return False;
232 }
233
234 *driverName = Xmalloc(rep.driverNameLength + 1);
235 if (*driverName == NULL) {
236 _XEatData(dpy,
237 ((rep.driverNameLength + 3) & ~3) +
238 ((rep.deviceNameLength + 3) & ~3));
239 UnlockDisplay(dpy);
240 SyncHandle();
241 return False;
242 }
243 _XReadPad(dpy, *driverName, rep.driverNameLength);
244 (*driverName)[rep.driverNameLength] = '\0';
245
246 *deviceName = Xmalloc(rep.deviceNameLength + 1);
247 if (*deviceName == NULL) {
248 Xfree(*driverName);
249 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
250 UnlockDisplay(dpy);
251 SyncHandle();
252 return False;
253 }
254 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
255 (*deviceName)[rep.deviceNameLength] = '\0';
256
257 UnlockDisplay(dpy);
258 SyncHandle();
259
260 return True;
261 }
262
263 Bool
264 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
265 {
266 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
267 xDRI2AuthenticateReq *req;
268 xDRI2AuthenticateReply rep;
269
270 XextCheckExtension(dpy, info, dri2ExtensionName, False);
271
272 LockDisplay(dpy);
273 GetReq(DRI2Authenticate, req);
274 req->reqType = info->codes->major_opcode;
275 req->dri2ReqType = X_DRI2Authenticate;
276 req->window = window;
277 req->magic = magic;
278
279 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
280 UnlockDisplay(dpy);
281 SyncHandle();
282 return False;
283 }
284
285 UnlockDisplay(dpy);
286 SyncHandle();
287
288 return rep.authenticated;
289 }
290
291 void
292 DRI2CreateDrawable(Display * dpy, XID drawable)
293 {
294 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
295 xDRI2CreateDrawableReq *req;
296
297 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
298
299 LockDisplay(dpy);
300 GetReq(DRI2CreateDrawable, req);
301 req->reqType = info->codes->major_opcode;
302 req->dri2ReqType = X_DRI2CreateDrawable;
303 req->drawable = drawable;
304 UnlockDisplay(dpy);
305 SyncHandle();
306 }
307
308 void
309 DRI2DestroyDrawable(Display * dpy, XID drawable)
310 {
311 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
312 xDRI2DestroyDrawableReq *req;
313
314 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
315
316 XSync(dpy, False);
317
318 LockDisplay(dpy);
319 GetReq(DRI2DestroyDrawable, req);
320 req->reqType = info->codes->major_opcode;
321 req->dri2ReqType = X_DRI2DestroyDrawable;
322 req->drawable = drawable;
323 UnlockDisplay(dpy);
324 SyncHandle();
325 }
326
327 DRI2Buffer *
328 DRI2GetBuffers(Display * dpy, XID drawable,
329 int *width, int *height,
330 unsigned int *attachments, int count, int *outCount)
331 {
332 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
333 xDRI2GetBuffersReply rep;
334 xDRI2GetBuffersReq *req;
335 DRI2Buffer *buffers;
336 xDRI2Buffer repBuffer;
337 CARD32 *p;
338 int i;
339
340 XextCheckExtension(dpy, info, dri2ExtensionName, False);
341
342 LockDisplay(dpy);
343 GetReqExtra(DRI2GetBuffers, count * 4, req);
344 req->reqType = info->codes->major_opcode;
345 req->dri2ReqType = X_DRI2GetBuffers;
346 req->drawable = drawable;
347 req->count = count;
348 p = (CARD32 *) & req[1];
349 for (i = 0; i < count; i++)
350 p[i] = attachments[i];
351
352 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
353 UnlockDisplay(dpy);
354 SyncHandle();
355 return NULL;
356 }
357
358 *width = rep.width;
359 *height = rep.height;
360 *outCount = rep.count;
361
362 buffers = Xmalloc(rep.count * sizeof buffers[0]);
363 if (buffers == NULL) {
364 _XEatData(dpy, rep.count * sizeof repBuffer);
365 UnlockDisplay(dpy);
366 SyncHandle();
367 return NULL;
368 }
369
370 for (i = 0; i < rep.count; i++) {
371 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
372 buffers[i].attachment = repBuffer.attachment;
373 buffers[i].name = repBuffer.name;
374 buffers[i].pitch = repBuffer.pitch;
375 buffers[i].cpp = repBuffer.cpp;
376 buffers[i].flags = repBuffer.flags;
377 }
378
379 UnlockDisplay(dpy);
380 SyncHandle();
381
382 return buffers;
383 }
384
385
386 DRI2Buffer *
387 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
388 int *width, int *height,
389 unsigned int *attachments, int count, int *outCount)
390 {
391 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
392 xDRI2GetBuffersReply rep;
393 xDRI2GetBuffersReq *req;
394 DRI2Buffer *buffers;
395 xDRI2Buffer repBuffer;
396 CARD32 *p;
397 int i;
398
399 XextCheckExtension(dpy, info, dri2ExtensionName, False);
400
401 LockDisplay(dpy);
402 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
403 req->reqType = info->codes->major_opcode;
404 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
405 req->drawable = drawable;
406 req->count = count;
407 p = (CARD32 *) & req[1];
408 for (i = 0; i < (count * 2); i++)
409 p[i] = attachments[i];
410
411 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
412 UnlockDisplay(dpy);
413 SyncHandle();
414 return NULL;
415 }
416
417 *width = rep.width;
418 *height = rep.height;
419 *outCount = rep.count;
420
421 buffers = Xmalloc(rep.count * sizeof buffers[0]);
422 if (buffers == NULL) {
423 _XEatData(dpy, rep.count * sizeof repBuffer);
424 UnlockDisplay(dpy);
425 SyncHandle();
426 return NULL;
427 }
428
429 for (i = 0; i < rep.count; i++) {
430 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
431 buffers[i].attachment = repBuffer.attachment;
432 buffers[i].name = repBuffer.name;
433 buffers[i].pitch = repBuffer.pitch;
434 buffers[i].cpp = repBuffer.cpp;
435 buffers[i].flags = repBuffer.flags;
436 }
437
438 UnlockDisplay(dpy);
439 SyncHandle();
440
441 return buffers;
442 }
443
444
445 void
446 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
447 CARD32 dest, CARD32 src)
448 {
449 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
450 xDRI2CopyRegionReq *req;
451 xDRI2CopyRegionReply rep;
452
453 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
454
455 LockDisplay(dpy);
456 GetReq(DRI2CopyRegion, req);
457 req->reqType = info->codes->major_opcode;
458 req->dri2ReqType = X_DRI2CopyRegion;
459 req->drawable = drawable;
460 req->region = region;
461 req->dest = dest;
462 req->src = src;
463
464 _XReply(dpy, (xReply *) & rep, 0, xFalse);
465
466 UnlockDisplay(dpy);
467 SyncHandle();
468 }
469
470 #ifdef X_DRI2SwapBuffers
471 static void
472 load_swap_req(xDRI2SwapBuffersReq *req, CARD64 target, CARD64 divisor,
473 CARD64 remainder)
474 {
475 req->target_msc_hi = target >> 32;
476 req->target_msc_lo = target & 0xffffffff;
477 req->divisor_hi = divisor >> 32;
478 req->divisor_lo = divisor & 0xffffffff;
479 req->remainder_hi = remainder >> 32;
480 req->remainder_lo = remainder & 0xffffffff;
481 }
482
483 static CARD64
484 vals_to_card64(CARD32 lo, CARD32 hi)
485 {
486 return (CARD64)hi << 32 | lo;
487 }
488
489 void DRI2SwapBuffers(Display *dpy, XID drawable, CARD64 target_msc,
490 CARD64 divisor, CARD64 remainder, CARD64 *count)
491 {
492 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
493 xDRI2SwapBuffersReq *req;
494 xDRI2SwapBuffersReply rep;
495
496 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
497
498 LockDisplay(dpy);
499 GetReq(DRI2SwapBuffers, req);
500 req->reqType = info->codes->major_opcode;
501 req->dri2ReqType = X_DRI2SwapBuffers;
502 req->drawable = drawable;
503 load_swap_req(req, target_msc, divisor, remainder);
504
505 _XReply(dpy, (xReply *)&rep, 0, xFalse);
506
507 *count = vals_to_card64(rep.swap_lo, rep.swap_hi);
508
509 UnlockDisplay(dpy);
510 SyncHandle();
511 }
512 #endif
513
514 #ifdef X_DRI2GetMSC
515 Bool DRI2GetMSC(Display *dpy, XID drawable, CARD64 *ust, CARD64 *msc,
516 CARD64 *sbc)
517 {
518 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
519 xDRI2GetMSCReq *req;
520 xDRI2MSCReply rep;
521
522 XextCheckExtension (dpy, info, dri2ExtensionName, False);
523
524 LockDisplay(dpy);
525 GetReq(DRI2GetMSC, req);
526 req->reqType = info->codes->major_opcode;
527 req->dri2ReqType = X_DRI2GetMSC;
528 req->drawable = drawable;
529
530 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
531 UnlockDisplay(dpy);
532 SyncHandle();
533 return False;
534 }
535
536 *ust = vals_to_card64(rep.ust_lo, rep.ust_hi);
537 *msc = vals_to_card64(rep.msc_lo, rep.msc_hi);
538 *sbc = vals_to_card64(rep.sbc_lo, rep.sbc_hi);
539
540 UnlockDisplay(dpy);
541 SyncHandle();
542
543 return True;
544 }
545 #endif
546
547 #ifdef X_DRI2WaitMSC
548 static void
549 load_msc_req(xDRI2WaitMSCReq *req, CARD64 target, CARD64 divisor,
550 CARD64 remainder)
551 {
552 req->target_msc_hi = target >> 32;
553 req->target_msc_lo = target & 0xffffffff;
554 req->divisor_hi = divisor >> 32;
555 req->divisor_lo = divisor & 0xffffffff;
556 req->remainder_hi = remainder >> 32;
557 req->remainder_lo = remainder & 0xffffffff;
558 }
559
560 Bool DRI2WaitMSC(Display *dpy, XID drawable, CARD64 target_msc, CARD64 divisor,
561 CARD64 remainder, CARD64 *ust, CARD64 *msc, CARD64 *sbc)
562 {
563 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
564 xDRI2WaitMSCReq *req;
565 xDRI2MSCReply rep;
566
567 XextCheckExtension (dpy, info, dri2ExtensionName, False);
568
569 LockDisplay(dpy);
570 GetReq(DRI2WaitMSC, req);
571 req->reqType = info->codes->major_opcode;
572 req->dri2ReqType = X_DRI2WaitMSC;
573 req->drawable = drawable;
574 load_msc_req(req, target_msc, divisor, remainder);
575
576 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
577 UnlockDisplay(dpy);
578 SyncHandle();
579 return False;
580 }
581
582 *ust = ((CARD64)rep.ust_hi << 32) | (CARD64)rep.ust_lo;
583 *msc = ((CARD64)rep.msc_hi << 32) | (CARD64)rep.msc_lo;
584 *sbc = ((CARD64)rep.sbc_hi << 32) | (CARD64)rep.sbc_lo;
585
586 UnlockDisplay(dpy);
587 SyncHandle();
588
589 return True;
590 }
591 #endif
592
593 #ifdef X_DRI2WaitSBC
594 static void
595 load_sbc_req(xDRI2WaitSBCReq *req, CARD64 target)
596 {
597 req->target_sbc_hi = target >> 32;
598 req->target_sbc_lo = target & 0xffffffff;
599 }
600
601 Bool DRI2WaitSBC(Display *dpy, XID drawable, CARD64 target_sbc, CARD64 *ust,
602 CARD64 *msc, CARD64 *sbc)
603 {
604 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
605 xDRI2WaitSBCReq *req;
606 xDRI2MSCReply rep;
607
608 XextCheckExtension (dpy, info, dri2ExtensionName, False);
609
610 LockDisplay(dpy);
611 GetReq(DRI2WaitSBC, req);
612 req->reqType = info->codes->major_opcode;
613 req->dri2ReqType = X_DRI2WaitSBC;
614 req->drawable = drawable;
615 load_sbc_req(req, target_sbc);
616
617 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
618 UnlockDisplay(dpy);
619 SyncHandle();
620 return False;
621 }
622
623 *ust = ((CARD64)rep.ust_hi << 32) | rep.ust_lo;
624 *msc = ((CARD64)rep.msc_hi << 32) | rep.msc_lo;
625 *sbc = ((CARD64)rep.sbc_hi << 32) | rep.sbc_lo;
626
627 UnlockDisplay(dpy);
628 SyncHandle();
629
630 return True;
631 }
632 #endif
633
634 #ifdef X_DRI2SwapInterval
635 void DRI2SwapInterval(Display *dpy, XID drawable, int interval)
636 {
637 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
638 xDRI2SwapIntervalReq *req;
639
640 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
641
642 LockDisplay(dpy);
643 GetReq(DRI2SwapInterval, req);
644 req->reqType = info->codes->major_opcode;
645 req->dri2ReqType = X_DRI2SwapInterval;
646 req->drawable = drawable;
647 req->interval = interval;
648 UnlockDisplay(dpy);
649 SyncHandle();
650 }
651 #endif
652
653 #endif /* GLX_DIRECT_RENDERING */