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)
35 #include <X11/Xlibint.h>
36 #include <X11/extensions/Xext.h>
37 #include <X11/extensions/extutil.h>
38 #include <X11/extensions/dri2proto.h>
41 #include <GL/glxext.h>
45 /* Allow the build to work with an older versions of dri2proto.h and
51 #define X_DRI2GetBuffersWithFormat 7
55 static char dri2ExtensionName[] = DRI2_NAME;
56 static XExtensionInfo *dri2Info;
57 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
60 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire);
62 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire);
64 DRI2Error(Display *display, xError *err, XExtCodes *codes, int *ret_code);
66 static /* const */ XExtensionHooks dri2ExtensionHooks = {
71 NULL, /* create_font */
73 DRI2CloseDisplay, /* close_display */
74 DRI2WireToEvent, /* wire_to_event */
75 DRI2EventToWire, /* event_to_wire */
76 DRI2Error, /* error */
77 NULL, /* error_string */
80 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
87 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire)
89 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
91 XextCheckExtension(dpy, info, dri2ExtensionName, False);
93 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
94 #ifdef X_DRI2SwapBuffers
95 case DRI2_BufferSwapComplete:
96 /* Ignore swap events if we're not looking for them */
97 printf("BufferSwapComplete\n");
100 #ifdef DRI2_InvalidateBuffers
101 case DRI2_InvalidateBuffers:
102 printf("InvalidateBuffers\n");
106 /* client doesn't support server event */
113 /* We don't actually support this. It doesn't make sense for clients to
114 * send each other DRI2 events.
117 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire)
119 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
121 XextCheckExtension(dpy, info, dri2ExtensionName, False);
123 switch (event->type) {
125 /* client doesn't support server event */
133 DRI2Error(Display *display, xError *err, XExtCodes *codes, int *ret_code)
135 if (err->majorCode == codes->major_opcode &&
136 err->errorCode == BadDrawable &&
137 err->minorCode == X_DRI2CopyRegion)
140 /* If the X drawable was destroyed before the GLX drawable, the
141 * DRI2 drawble will be gone by the time we call
142 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
143 if (err->majorCode == codes->major_opcode &&
144 err->errorCode == BadDrawable &&
145 err->minorCode == X_DRI2DestroyDrawable)
148 /* If the server is non-local DRI2Connect will raise BadRequest.
149 * Swallow this so that DRI2Connect can signal this in its return code */
150 if (err->majorCode == codes->major_opcode &&
151 err->minorCode == X_DRI2Connect &&
152 err->errorCode == BadRequest) {
161 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
163 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
165 if (XextHasExtension(info)) {
166 *eventBase = info->codes->first_event;
167 *errorBase = info->codes->first_error;
175 DRI2QueryVersion(Display * dpy, int *major, int *minor)
177 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
178 xDRI2QueryVersionReply rep;
179 xDRI2QueryVersionReq *req;
182 XextCheckExtension(dpy, info, dri2ExtensionName, False);
185 GetReq(DRI2QueryVersion, req);
186 req->reqType = info->codes->major_opcode;
187 req->dri2ReqType = X_DRI2QueryVersion;
188 req->majorVersion = DRI2_MAJOR;
189 req->minorVersion = DRI2_MINOR;
190 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
195 *major = rep.majorVersion;
196 *minor = rep.minorVersion;
200 switch (rep.minorVersion) {
213 for (i = 0; i < nevents; i++) {
214 XESetWireToEvent (dpy, info->codes->first_event + i, DRI2WireToEvent);
215 XESetEventToWire (dpy, info->codes->first_event + i, DRI2EventToWire);
222 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
224 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
225 xDRI2ConnectReply rep;
226 xDRI2ConnectReq *req;
228 XextCheckExtension(dpy, info, dri2ExtensionName, False);
231 GetReq(DRI2Connect, req);
232 req->reqType = info->codes->major_opcode;
233 req->dri2ReqType = X_DRI2Connect;
234 req->window = window;
235 req->driverType = DRI2DriverDRI;
236 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
242 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
248 *driverName = Xmalloc(rep.driverNameLength + 1);
249 if (*driverName == NULL) {
251 ((rep.driverNameLength + 3) & ~3) +
252 ((rep.deviceNameLength + 3) & ~3));
257 _XReadPad(dpy, *driverName, rep.driverNameLength);
258 (*driverName)[rep.driverNameLength] = '\0';
260 *deviceName = Xmalloc(rep.deviceNameLength + 1);
261 if (*deviceName == NULL) {
263 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
268 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
269 (*deviceName)[rep.deviceNameLength] = '\0';
278 DRI2Authenticate(Display * dpy, XID window, unsigned int magic)
280 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
281 xDRI2AuthenticateReq *req;
282 xDRI2AuthenticateReply rep;
284 XextCheckExtension(dpy, info, dri2ExtensionName, False);
287 GetReq(DRI2Authenticate, req);
288 req->reqType = info->codes->major_opcode;
289 req->dri2ReqType = X_DRI2Authenticate;
290 req->window = window;
293 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
302 return rep.authenticated;
306 DRI2CreateDrawable(Display * dpy, XID drawable)
308 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
309 xDRI2CreateDrawableReq *req;
311 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
314 GetReq(DRI2CreateDrawable, req);
315 req->reqType = info->codes->major_opcode;
316 req->dri2ReqType = X_DRI2CreateDrawable;
317 req->drawable = drawable;
323 DRI2DestroyDrawable(Display * dpy, XID drawable)
325 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
326 xDRI2DestroyDrawableReq *req;
328 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
333 GetReq(DRI2DestroyDrawable, req);
334 req->reqType = info->codes->major_opcode;
335 req->dri2ReqType = X_DRI2DestroyDrawable;
336 req->drawable = drawable;
342 DRI2GetBuffers(Display * dpy, XID drawable,
343 int *width, int *height,
344 unsigned int *attachments, int count, int *outCount)
346 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
347 xDRI2GetBuffersReply rep;
348 xDRI2GetBuffersReq *req;
350 xDRI2Buffer repBuffer;
354 XextCheckExtension(dpy, info, dri2ExtensionName, False);
357 GetReqExtra(DRI2GetBuffers, count * 4, req);
358 req->reqType = info->codes->major_opcode;
359 req->dri2ReqType = X_DRI2GetBuffers;
360 req->drawable = drawable;
362 p = (uint32_t *) & req[1];
363 for (i = 0; i < count; i++)
364 p[i] = attachments[i];
366 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
373 *height = rep.height;
374 *outCount = rep.count;
376 buffers = Xmalloc(rep.count * sizeof buffers[0]);
377 if (buffers == NULL) {
378 _XEatData(dpy, rep.count * sizeof repBuffer);
384 for (i = 0; i < rep.count; i++) {
385 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
386 buffers[i].attachment = repBuffer.attachment;
387 buffers[i].name = repBuffer.name;
388 buffers[i].pitch = repBuffer.pitch;
389 buffers[i].cpp = repBuffer.cpp;
390 buffers[i].flags = repBuffer.flags;
401 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
402 int *width, int *height,
403 unsigned int *attachments, int count, int *outCount)
405 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
406 xDRI2GetBuffersReply rep;
407 xDRI2GetBuffersReq *req;
409 xDRI2Buffer repBuffer;
413 XextCheckExtension(dpy, info, dri2ExtensionName, False);
416 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
417 req->reqType = info->codes->major_opcode;
418 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
419 req->drawable = drawable;
421 p = (uint32_t *) & req[1];
422 for (i = 0; i < (count * 2); i++)
423 p[i] = attachments[i];
425 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
432 *height = rep.height;
433 *outCount = rep.count;
435 buffers = Xmalloc(rep.count * sizeof buffers[0]);
436 if (buffers == NULL) {
437 _XEatData(dpy, rep.count * sizeof repBuffer);
443 for (i = 0; i < rep.count; i++) {
444 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
445 buffers[i].attachment = repBuffer.attachment;
446 buffers[i].name = repBuffer.name;
447 buffers[i].pitch = repBuffer.pitch;
448 buffers[i].cpp = repBuffer.cpp;
449 buffers[i].flags = repBuffer.flags;
460 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
461 uint32_t dest, uint32_t src)
463 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
464 xDRI2CopyRegionReq *req;
465 xDRI2CopyRegionReply rep;
467 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
470 GetReq(DRI2CopyRegion, req);
471 req->reqType = info->codes->major_opcode;
472 req->dri2ReqType = X_DRI2CopyRegion;
473 req->drawable = drawable;
474 req->region = region;
478 (void) _XReply(dpy, (xReply *) & rep, 0, xFalse);
484 #ifdef X_DRI2SwapBuffers
486 load_swap_req(xDRI2SwapBuffersReq *req, uint64_t target, uint64_t divisor,
489 req->target_msc_hi = target >> 32;
490 req->target_msc_lo = target & 0xffffffff;
491 req->divisor_hi = divisor >> 32;
492 req->divisor_lo = divisor & 0xffffffff;
493 req->remainder_hi = remainder >> 32;
494 req->remainder_lo = remainder & 0xffffffff;
498 vals_to_card64(uint32_t lo, uint32_t hi)
500 return (uint64_t)hi << 32 | lo;
503 uint64_t DRI2SwapBuffers(Display *dpy, XID drawable,
504 uint64_t target_msc, uint64_t divisor, uint64_t remainder)
506 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
507 xDRI2SwapBuffersReq *req;
508 xDRI2SwapBuffersReply rep;
511 XextCheckExtension (dpy, info, dri2ExtensionName, 0);
514 GetReq(DRI2SwapBuffers, req);
515 req->reqType = info->codes->major_opcode;
516 req->dri2ReqType = X_DRI2SwapBuffers;
517 req->drawable = drawable;
518 load_swap_req(req, target_msc, divisor, remainder);
520 (void) _XReply(dpy, (xReply *)&rep, 0, xFalse);
522 count = vals_to_card64(rep.swap_lo, rep.swap_hi);
532 Bool DRI2GetMSC(Display *dpy, XID drawable, uint64_t *ust, uint64_t *msc,
535 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
539 XextCheckExtension (dpy, info, dri2ExtensionName, False);
542 GetReq(DRI2GetMSC, req);
543 req->reqType = info->codes->major_opcode;
544 req->dri2ReqType = X_DRI2GetMSC;
545 req->drawable = drawable;
547 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
553 *ust = vals_to_card64(rep.ust_lo, rep.ust_hi);
554 *msc = vals_to_card64(rep.msc_lo, rep.msc_hi);
555 *sbc = vals_to_card64(rep.sbc_lo, rep.sbc_hi);
566 load_msc_req(xDRI2WaitMSCReq *req, uint64_t target, uint64_t divisor,
569 req->target_msc_hi = target >> 32;
570 req->target_msc_lo = target & 0xffffffff;
571 req->divisor_hi = divisor >> 32;
572 req->divisor_lo = divisor & 0xffffffff;
573 req->remainder_hi = remainder >> 32;
574 req->remainder_lo = remainder & 0xffffffff;
577 Bool DRI2WaitMSC(Display *dpy, XID drawable, uint64_t target_msc, uint64_t divisor,
578 uint64_t remainder, uint64_t *ust, uint64_t *msc, uint64_t *sbc)
580 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
581 xDRI2WaitMSCReq *req;
584 XextCheckExtension (dpy, info, dri2ExtensionName, False);
587 GetReq(DRI2WaitMSC, req);
588 req->reqType = info->codes->major_opcode;
589 req->dri2ReqType = X_DRI2WaitMSC;
590 req->drawable = drawable;
591 load_msc_req(req, target_msc, divisor, remainder);
593 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
599 *ust = ((uint64_t)rep.ust_hi << 32) | (uint64_t)rep.ust_lo;
600 *msc = ((uint64_t)rep.msc_hi << 32) | (uint64_t)rep.msc_lo;
601 *sbc = ((uint64_t)rep.sbc_hi << 32) | (uint64_t)rep.sbc_lo;
612 load_sbc_req(xDRI2WaitSBCReq *req, uint64_t target)
614 req->target_sbc_hi = target >> 32;
615 req->target_sbc_lo = target & 0xffffffff;
618 Bool DRI2WaitSBC(Display *dpy, XID drawable, uint64_t target_sbc, uint64_t *ust,
619 uint64_t *msc, uint64_t *sbc)
621 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
622 xDRI2WaitSBCReq *req;
625 XextCheckExtension (dpy, info, dri2ExtensionName, False);
628 GetReq(DRI2WaitSBC, req);
629 req->reqType = info->codes->major_opcode;
630 req->dri2ReqType = X_DRI2WaitSBC;
631 req->drawable = drawable;
632 load_sbc_req(req, target_sbc);
634 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
640 *ust = ((uint64_t)rep.ust_hi << 32) | rep.ust_lo;
641 *msc = ((uint64_t)rep.msc_hi << 32) | rep.msc_lo;
642 *sbc = ((uint64_t)rep.sbc_hi << 32) | rep.sbc_lo;
651 #ifdef X_DRI2SwapInterval
652 void DRI2SwapInterval(Display *dpy, XID drawable, int interval)
654 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
655 xDRI2SwapIntervalReq *req;
657 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
660 GetReq(DRI2SwapInterval, req);
661 req->reqType = info->codes->major_opcode;
662 req->dri2ReqType = X_DRI2SwapInterval;
663 req->drawable = drawable;
664 req->interval = interval;