2 * Copyright © 2008 Red Hat, Inc.
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.
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.
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.
30 * Kristian Høgsberg (krh@redhat.com)
34 #ifdef GLX_DIRECT_RENDERING
37 #include <X11/Xlibint.h>
38 #include <X11/extensions/Xext.h>
39 #include <X11/extensions/extutil.h>
40 #include <X11/extensions/dri2proto.h>
43 #include "glxclient.h"
44 #include "GL/glxext.h"
46 /* Allow the build to work with an older versions of dri2proto.h and
52 #define X_DRI2GetBuffersWithFormat 7
56 static char dri2ExtensionName[] = DRI2_NAME;
57 static XExtensionInfo *dri2Info;
58 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
61 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire);
63 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire);
65 DRI2Error(Display *display, xError *err, XExtCodes *codes, int *ret_code);
67 static /* const */ XExtensionHooks dri2ExtensionHooks = {
72 NULL, /* create_font */
74 DRI2CloseDisplay, /* close_display */
75 DRI2WireToEvent, /* wire_to_event */
76 DRI2EventToWire, /* event_to_wire */
77 DRI2Error, /* error */
78 NULL, /* error_string */
81 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
88 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire)
90 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
92 XextCheckExtension(dpy, info, dri2ExtensionName, False);
94 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
96 #ifdef X_DRI2SwapBuffers
97 case DRI2_BufferSwapComplete:
99 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
100 xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
102 /* Ignore swap events if we're not looking for them */
103 aevent->type = dri2GetSwapEventType(dpy, awire->drawable);
107 aevent->serial = _XSetLastRequestRead(dpy, (xGenericReply *) wire);
108 aevent->send_event = (awire->type & 0x80) != 0;
109 aevent->display = dpy;
110 aevent->drawable = awire->drawable;
111 switch (awire->event_type) {
112 case DRI2_EXCHANGE_COMPLETE:
113 aevent->event_type = GLX_EXCHANGE_COMPLETE_INTEL;
115 case DRI2_BLIT_COMPLETE:
116 aevent->event_type = GLX_COPY_COMPLETE_INTEL;
118 case DRI2_FLIP_COMPLETE:
119 aevent->event_type = GLX_FLIP_COMPLETE_INTEL;
122 /* unknown swap completion type */
125 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
126 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
127 aevent->sbc = ((CARD64)awire->sbc_hi << 32) | awire->sbc_lo;
131 #ifdef DRI2_InvalidateBuffers
132 case DRI2_InvalidateBuffers:
134 xDRI2InvalidateBuffers *awire = (xDRI2InvalidateBuffers *)wire;
136 dri2InvalidateBuffers(dpy, awire->drawable);
141 /* client doesn't support server event */
148 /* We don't actually support this. It doesn't make sense for clients to
149 * send each other DRI2 events.
152 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire)
154 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
156 XextCheckExtension(dpy, info, dri2ExtensionName, False);
158 switch (event->type) {
160 /* client doesn't support server event */
168 DRI2Error(Display *display, xError *err, XExtCodes *codes, int *ret_code)
170 if (err->majorCode == codes->major_opcode &&
171 err->errorCode == BadDrawable &&
172 err->minorCode == X_DRI2CopyRegion)
175 /* If the X drawable was destroyed before the GLX drawable, the
176 * DRI2 drawble will be gone by the time we call
177 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
178 if (err->majorCode == codes->major_opcode &&
179 err->errorCode == BadDrawable &&
180 err->minorCode == X_DRI2DestroyDrawable)
183 /* If the server is non-local DRI2Connect will raise BadRequest.
184 * Swallow this so that DRI2Connect can signal this in its return code */
185 if (err->majorCode == codes->major_opcode &&
186 err->minorCode == X_DRI2Connect &&
187 err->errorCode == BadRequest) {
196 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
198 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
200 if (XextHasExtension(info)) {
201 *eventBase = info->codes->first_event;
202 *errorBase = info->codes->first_error;
210 DRI2QueryVersion(Display * dpy, int *major, int *minor)
212 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
213 xDRI2QueryVersionReply rep;
214 xDRI2QueryVersionReq *req;
217 XextCheckExtension(dpy, info, dri2ExtensionName, False);
220 GetReq(DRI2QueryVersion, req);
221 req->reqType = info->codes->major_opcode;
222 req->dri2ReqType = X_DRI2QueryVersion;
223 req->majorVersion = DRI2_MAJOR;
224 req->minorVersion = DRI2_MINOR;
225 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
230 *major = rep.majorVersion;
231 *minor = rep.minorVersion;
235 switch (rep.minorVersion) {
248 for (i = 0; i < nevents; i++) {
249 XESetWireToEvent (dpy, info->codes->first_event + i, DRI2WireToEvent);
250 XESetEventToWire (dpy, info->codes->first_event + i, DRI2EventToWire);
257 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
259 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
260 xDRI2ConnectReply rep;
261 xDRI2ConnectReq *req;
263 XextCheckExtension(dpy, info, dri2ExtensionName, False);
266 GetReq(DRI2Connect, req);
267 req->reqType = info->codes->major_opcode;
268 req->dri2ReqType = X_DRI2Connect;
269 req->window = window;
270 req->driverType = DRI2DriverDRI;
271 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
277 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
283 *driverName = Xmalloc(rep.driverNameLength + 1);
284 if (*driverName == NULL) {
286 ((rep.driverNameLength + 3) & ~3) +
287 ((rep.deviceNameLength + 3) & ~3));
292 _XReadPad(dpy, *driverName, rep.driverNameLength);
293 (*driverName)[rep.driverNameLength] = '\0';
295 *deviceName = Xmalloc(rep.deviceNameLength + 1);
296 if (*deviceName == NULL) {
298 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
303 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
304 (*deviceName)[rep.deviceNameLength] = '\0';
313 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
315 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
316 xDRI2AuthenticateReq *req;
317 xDRI2AuthenticateReply rep;
319 XextCheckExtension(dpy, info, dri2ExtensionName, False);
322 GetReq(DRI2Authenticate, req);
323 req->reqType = info->codes->major_opcode;
324 req->dri2ReqType = X_DRI2Authenticate;
325 req->window = window;
328 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
337 return rep.authenticated;
341 DRI2CreateDrawable(Display * dpy, XID drawable)
343 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
344 xDRI2CreateDrawableReq *req;
346 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
349 GetReq(DRI2CreateDrawable, req);
350 req->reqType = info->codes->major_opcode;
351 req->dri2ReqType = X_DRI2CreateDrawable;
352 req->drawable = drawable;
358 DRI2DestroyDrawable(Display * dpy, XID drawable)
360 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
361 xDRI2DestroyDrawableReq *req;
363 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
368 GetReq(DRI2DestroyDrawable, req);
369 req->reqType = info->codes->major_opcode;
370 req->dri2ReqType = X_DRI2DestroyDrawable;
371 req->drawable = drawable;
377 DRI2GetBuffers(Display * dpy, XID drawable,
378 int *width, int *height,
379 unsigned int *attachments, int count, int *outCount)
381 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
382 xDRI2GetBuffersReply rep;
383 xDRI2GetBuffersReq *req;
385 xDRI2Buffer repBuffer;
389 XextCheckExtension(dpy, info, dri2ExtensionName, False);
392 GetReqExtra(DRI2GetBuffers, count * 4, req);
393 req->reqType = info->codes->major_opcode;
394 req->dri2ReqType = X_DRI2GetBuffers;
395 req->drawable = drawable;
397 p = (CARD32 *) & req[1];
398 for (i = 0; i < count; i++)
399 p[i] = attachments[i];
401 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
408 *height = rep.height;
409 *outCount = rep.count;
411 buffers = Xmalloc(rep.count * sizeof buffers[0]);
412 if (buffers == NULL) {
413 _XEatData(dpy, rep.count * sizeof repBuffer);
419 for (i = 0; i < rep.count; i++) {
420 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
421 buffers[i].attachment = repBuffer.attachment;
422 buffers[i].name = repBuffer.name;
423 buffers[i].pitch = repBuffer.pitch;
424 buffers[i].cpp = repBuffer.cpp;
425 buffers[i].flags = repBuffer.flags;
436 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
437 int *width, int *height,
438 unsigned int *attachments, int count, int *outCount)
440 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
441 xDRI2GetBuffersReply rep;
442 xDRI2GetBuffersReq *req;
444 xDRI2Buffer repBuffer;
448 XextCheckExtension(dpy, info, dri2ExtensionName, False);
451 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
452 req->reqType = info->codes->major_opcode;
453 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
454 req->drawable = drawable;
456 p = (CARD32 *) & req[1];
457 for (i = 0; i < (count * 2); i++)
458 p[i] = attachments[i];
460 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
467 *height = rep.height;
468 *outCount = rep.count;
470 buffers = Xmalloc(rep.count * sizeof buffers[0]);
471 if (buffers == NULL) {
472 _XEatData(dpy, rep.count * sizeof repBuffer);
478 for (i = 0; i < rep.count; i++) {
479 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
480 buffers[i].attachment = repBuffer.attachment;
481 buffers[i].name = repBuffer.name;
482 buffers[i].pitch = repBuffer.pitch;
483 buffers[i].cpp = repBuffer.cpp;
484 buffers[i].flags = repBuffer.flags;
495 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
496 CARD32 dest, CARD32 src)
498 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
499 xDRI2CopyRegionReq *req;
500 xDRI2CopyRegionReply rep;
502 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
505 GetReq(DRI2CopyRegion, req);
506 req->reqType = info->codes->major_opcode;
507 req->dri2ReqType = X_DRI2CopyRegion;
508 req->drawable = drawable;
509 req->region = region;
513 _XReply(dpy, (xReply *) & rep, 0, xFalse);
519 #ifdef X_DRI2SwapBuffers
521 load_swap_req(xDRI2SwapBuffersReq *req, CARD64 target, CARD64 divisor,
524 req->target_msc_hi = target >> 32;
525 req->target_msc_lo = target & 0xffffffff;
526 req->divisor_hi = divisor >> 32;
527 req->divisor_lo = divisor & 0xffffffff;
528 req->remainder_hi = remainder >> 32;
529 req->remainder_lo = remainder & 0xffffffff;
533 vals_to_card64(CARD32 lo, CARD32 hi)
535 return (CARD64)hi << 32 | lo;
538 void DRI2SwapBuffers(Display *dpy, XID drawable, CARD64 target_msc,
539 CARD64 divisor, CARD64 remainder, CARD64 *count)
541 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
542 xDRI2SwapBuffersReq *req;
543 xDRI2SwapBuffersReply rep;
545 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
548 GetReq(DRI2SwapBuffers, req);
549 req->reqType = info->codes->major_opcode;
550 req->dri2ReqType = X_DRI2SwapBuffers;
551 req->drawable = drawable;
552 load_swap_req(req, target_msc, divisor, remainder);
554 _XReply(dpy, (xReply *)&rep, 0, xFalse);
556 *count = vals_to_card64(rep.swap_lo, rep.swap_hi);
564 Bool DRI2GetMSC(Display *dpy, XID drawable, CARD64 *ust, CARD64 *msc,
567 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
571 XextCheckExtension (dpy, info, dri2ExtensionName, False);
574 GetReq(DRI2GetMSC, req);
575 req->reqType = info->codes->major_opcode;
576 req->dri2ReqType = X_DRI2GetMSC;
577 req->drawable = drawable;
579 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
585 *ust = vals_to_card64(rep.ust_lo, rep.ust_hi);
586 *msc = vals_to_card64(rep.msc_lo, rep.msc_hi);
587 *sbc = vals_to_card64(rep.sbc_lo, rep.sbc_hi);
598 load_msc_req(xDRI2WaitMSCReq *req, CARD64 target, CARD64 divisor,
601 req->target_msc_hi = target >> 32;
602 req->target_msc_lo = target & 0xffffffff;
603 req->divisor_hi = divisor >> 32;
604 req->divisor_lo = divisor & 0xffffffff;
605 req->remainder_hi = remainder >> 32;
606 req->remainder_lo = remainder & 0xffffffff;
609 Bool DRI2WaitMSC(Display *dpy, XID drawable, CARD64 target_msc, CARD64 divisor,
610 CARD64 remainder, CARD64 *ust, CARD64 *msc, CARD64 *sbc)
612 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
613 xDRI2WaitMSCReq *req;
616 XextCheckExtension (dpy, info, dri2ExtensionName, False);
619 GetReq(DRI2WaitMSC, req);
620 req->reqType = info->codes->major_opcode;
621 req->dri2ReqType = X_DRI2WaitMSC;
622 req->drawable = drawable;
623 load_msc_req(req, target_msc, divisor, remainder);
625 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
631 *ust = ((CARD64)rep.ust_hi << 32) | (CARD64)rep.ust_lo;
632 *msc = ((CARD64)rep.msc_hi << 32) | (CARD64)rep.msc_lo;
633 *sbc = ((CARD64)rep.sbc_hi << 32) | (CARD64)rep.sbc_lo;
644 load_sbc_req(xDRI2WaitSBCReq *req, CARD64 target)
646 req->target_sbc_hi = target >> 32;
647 req->target_sbc_lo = target & 0xffffffff;
650 Bool DRI2WaitSBC(Display *dpy, XID drawable, CARD64 target_sbc, CARD64 *ust,
651 CARD64 *msc, CARD64 *sbc)
653 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
654 xDRI2WaitSBCReq *req;
657 XextCheckExtension (dpy, info, dri2ExtensionName, False);
660 GetReq(DRI2WaitSBC, req);
661 req->reqType = info->codes->major_opcode;
662 req->dri2ReqType = X_DRI2WaitSBC;
663 req->drawable = drawable;
664 load_sbc_req(req, target_sbc);
666 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
672 *ust = ((CARD64)rep.ust_hi << 32) | rep.ust_lo;
673 *msc = ((CARD64)rep.msc_hi << 32) | rep.msc_lo;
674 *sbc = ((CARD64)rep.sbc_hi << 32) | rep.sbc_lo;
683 #ifdef X_DRI2SwapInterval
684 void DRI2SwapInterval(Display *dpy, XID drawable, int interval)
686 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
687 xDRI2SwapIntervalReq *req;
689 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
692 GetReq(DRI2SwapInterval, req);
693 req->reqType = info->codes->major_opcode;
694 req->dri2ReqType = X_DRI2SwapInterval;
695 req->drawable = drawable;
696 req->interval = interval;
702 #endif /* GLX_DIRECT_RENDERING */