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
);
91 struct glx_drawable
*glxDraw
;
93 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
95 switch ((wire
->u
.u
.type
& 0x7f) - info
->codes
->first_event
) {
97 #ifdef X_DRI2SwapBuffers
98 case DRI2_BufferSwapComplete
:
100 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
101 xDRI2BufferSwapComplete2
*awire
= (xDRI2BufferSwapComplete2
*)wire
;
102 __GLXDRIdrawable
*pdraw
;
104 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, awire
->drawable
);
106 /* Ignore swap events if we're not looking for them */
107 aevent
->type
= dri2GetSwapEventType(dpy
, awire
->drawable
);
111 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
112 aevent
->send_event
= (awire
->type
& 0x80) != 0;
113 aevent
->display
= dpy
;
114 aevent
->drawable
= awire
->drawable
;
115 switch (awire
->event_type
) {
116 case DRI2_EXCHANGE_COMPLETE
:
117 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
119 case DRI2_BLIT_COMPLETE
:
120 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
122 case DRI2_FLIP_COMPLETE
:
123 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
126 /* unknown swap completion type */
129 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
130 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
132 glxDraw
= GetGLXDrawable(dpy
, pdraw
->drawable
);
133 if (awire
->sbc
< glxDraw
->lastEventSbc
)
134 glxDraw
->eventSbcWrap
+= 0x100000000;
135 glxDraw
->lastEventSbc
= awire
->sbc
;
136 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
141 #ifdef DRI2_InvalidateBuffers
142 case DRI2_InvalidateBuffers
:
144 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
146 dri2InvalidateBuffers(dpy
, awire
->drawable
);
151 /* client doesn't support server event */
158 /* We don't actually support this. It doesn't make sense for clients to
159 * send each other DRI2 events.
162 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
164 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
166 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
168 switch (event
->type
) {
170 /* client doesn't support server event */
178 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
180 if (err
->majorCode
== codes
->major_opcode
&&
181 err
->errorCode
== BadDrawable
&&
182 err
->minorCode
== X_DRI2CopyRegion
)
185 /* If the X drawable was destroyed before the GLX drawable, the
186 * DRI2 drawble will be gone by the time we call
187 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
188 if (err
->majorCode
== codes
->major_opcode
&&
189 err
->errorCode
== BadDrawable
&&
190 err
->minorCode
== X_DRI2DestroyDrawable
)
197 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
199 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
201 if (XextHasExtension(info
)) {
202 *eventBase
= info
->codes
->first_event
;
203 *errorBase
= info
->codes
->first_error
;
211 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
213 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
214 xDRI2QueryVersionReply rep
;
215 xDRI2QueryVersionReq
*req
;
218 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
221 GetReq(DRI2QueryVersion
, req
);
222 req
->reqType
= info
->codes
->major_opcode
;
223 req
->dri2ReqType
= X_DRI2QueryVersion
;
224 req
->majorVersion
= DRI2_MAJOR
;
225 req
->minorVersion
= DRI2_MINOR
;
226 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
231 *major
= rep
.majorVersion
;
232 *minor
= rep
.minorVersion
;
236 switch (rep
.minorVersion
) {
249 for (i
= 0; i
< nevents
; i
++) {
250 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
251 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
258 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
260 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
261 xDRI2ConnectReply rep
;
262 xDRI2ConnectReq
*req
;
264 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
267 GetReq(DRI2Connect
, req
);
268 req
->reqType
= info
->codes
->major_opcode
;
269 req
->dri2ReqType
= X_DRI2Connect
;
270 req
->window
= window
;
271 req
->driverType
= DRI2DriverDRI
;
272 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
278 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
284 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
285 if (*driverName
== NULL
) {
287 ((rep
.driverNameLength
+ 3) & ~3) +
288 ((rep
.deviceNameLength
+ 3) & ~3));
293 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
294 (*driverName
)[rep
.driverNameLength
] = '\0';
296 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
297 if (*deviceName
== NULL
) {
299 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
304 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
305 (*deviceName
)[rep
.deviceNameLength
] = '\0';
314 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
316 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
317 xDRI2AuthenticateReq
*req
;
318 xDRI2AuthenticateReply rep
;
320 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
323 GetReq(DRI2Authenticate
, req
);
324 req
->reqType
= info
->codes
->major_opcode
;
325 req
->dri2ReqType
= X_DRI2Authenticate
;
326 req
->window
= window
;
329 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
338 return rep
.authenticated
;
342 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
344 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
345 xDRI2CreateDrawableReq
*req
;
347 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
350 GetReq(DRI2CreateDrawable
, req
);
351 req
->reqType
= info
->codes
->major_opcode
;
352 req
->dri2ReqType
= X_DRI2CreateDrawable
;
353 req
->drawable
= drawable
;
359 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
361 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
362 xDRI2DestroyDrawableReq
*req
;
364 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
369 GetReq(DRI2DestroyDrawable
, req
);
370 req
->reqType
= info
->codes
->major_opcode
;
371 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
372 req
->drawable
= drawable
;
378 DRI2GetBuffers(Display
* dpy
, XID drawable
,
379 int *width
, int *height
,
380 unsigned int *attachments
, int count
, int *outCount
)
382 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
383 xDRI2GetBuffersReply rep
;
384 xDRI2GetBuffersReq
*req
;
386 xDRI2Buffer repBuffer
;
390 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
393 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
394 req
->reqType
= info
->codes
->major_opcode
;
395 req
->dri2ReqType
= X_DRI2GetBuffers
;
396 req
->drawable
= drawable
;
398 p
= (CARD32
*) & req
[1];
399 for (i
= 0; i
< count
; i
++)
400 p
[i
] = attachments
[i
];
402 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
409 *height
= rep
.height
;
410 *outCount
= rep
.count
;
412 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
413 if (buffers
== NULL
) {
414 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
420 for (i
= 0; i
< rep
.count
; i
++) {
421 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
422 buffers
[i
].attachment
= repBuffer
.attachment
;
423 buffers
[i
].name
= repBuffer
.name
;
424 buffers
[i
].pitch
= repBuffer
.pitch
;
425 buffers
[i
].cpp
= repBuffer
.cpp
;
426 buffers
[i
].flags
= repBuffer
.flags
;
437 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
438 int *width
, int *height
,
439 unsigned int *attachments
, int count
, int *outCount
)
441 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
442 xDRI2GetBuffersReply rep
;
443 xDRI2GetBuffersReq
*req
;
445 xDRI2Buffer repBuffer
;
449 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
452 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
453 req
->reqType
= info
->codes
->major_opcode
;
454 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
455 req
->drawable
= drawable
;
457 p
= (CARD32
*) & req
[1];
458 for (i
= 0; i
< (count
* 2); i
++)
459 p
[i
] = attachments
[i
];
461 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
468 *height
= rep
.height
;
469 *outCount
= rep
.count
;
471 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
472 if (buffers
== NULL
) {
473 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
479 for (i
= 0; i
< rep
.count
; i
++) {
480 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
481 buffers
[i
].attachment
= repBuffer
.attachment
;
482 buffers
[i
].name
= repBuffer
.name
;
483 buffers
[i
].pitch
= repBuffer
.pitch
;
484 buffers
[i
].cpp
= repBuffer
.cpp
;
485 buffers
[i
].flags
= repBuffer
.flags
;
496 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
497 CARD32 dest
, CARD32 src
)
499 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
500 xDRI2CopyRegionReq
*req
;
501 xDRI2CopyRegionReply rep
;
503 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
506 GetReq(DRI2CopyRegion
, req
);
507 req
->reqType
= info
->codes
->major_opcode
;
508 req
->dri2ReqType
= X_DRI2CopyRegion
;
509 req
->drawable
= drawable
;
510 req
->region
= region
;
514 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
520 #ifdef X_DRI2SwapBuffers
522 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
525 req
->target_msc_hi
= target
>> 32;
526 req
->target_msc_lo
= target
& 0xffffffff;
527 req
->divisor_hi
= divisor
>> 32;
528 req
->divisor_lo
= divisor
& 0xffffffff;
529 req
->remainder_hi
= remainder
>> 32;
530 req
->remainder_lo
= remainder
& 0xffffffff;
534 vals_to_card64(CARD32 lo
, CARD32 hi
)
536 return (CARD64
)hi
<< 32 | lo
;
539 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
540 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
542 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
543 xDRI2SwapBuffersReq
*req
;
544 xDRI2SwapBuffersReply rep
;
546 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
549 GetReq(DRI2SwapBuffers
, req
);
550 req
->reqType
= info
->codes
->major_opcode
;
551 req
->dri2ReqType
= X_DRI2SwapBuffers
;
552 req
->drawable
= drawable
;
553 load_swap_req(req
, target_msc
, divisor
, remainder
);
555 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
557 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
565 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
568 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
572 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
575 GetReq(DRI2GetMSC
, req
);
576 req
->reqType
= info
->codes
->major_opcode
;
577 req
->dri2ReqType
= X_DRI2GetMSC
;
578 req
->drawable
= drawable
;
580 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
586 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
587 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
588 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
599 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
602 req
->target_msc_hi
= target
>> 32;
603 req
->target_msc_lo
= target
& 0xffffffff;
604 req
->divisor_hi
= divisor
>> 32;
605 req
->divisor_lo
= divisor
& 0xffffffff;
606 req
->remainder_hi
= remainder
>> 32;
607 req
->remainder_lo
= remainder
& 0xffffffff;
610 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
611 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
613 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
614 xDRI2WaitMSCReq
*req
;
617 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
620 GetReq(DRI2WaitMSC
, req
);
621 req
->reqType
= info
->codes
->major_opcode
;
622 req
->dri2ReqType
= X_DRI2WaitMSC
;
623 req
->drawable
= drawable
;
624 load_msc_req(req
, target_msc
, divisor
, remainder
);
626 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
632 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
633 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
634 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
645 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
647 req
->target_sbc_hi
= target
>> 32;
648 req
->target_sbc_lo
= target
& 0xffffffff;
651 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
652 CARD64
*msc
, CARD64
*sbc
)
654 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
655 xDRI2WaitSBCReq
*req
;
658 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
661 GetReq(DRI2WaitSBC
, req
);
662 req
->reqType
= info
->codes
->major_opcode
;
663 req
->dri2ReqType
= X_DRI2WaitSBC
;
664 req
->drawable
= drawable
;
665 load_sbc_req(req
, target_sbc
);
667 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
673 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
674 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
675 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
684 #ifdef X_DRI2SwapInterval
685 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
687 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
688 xDRI2SwapIntervalReq
*req
;
690 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
693 GetReq(DRI2SwapInterval
, req
);
694 req
->reqType
= info
->codes
->major_opcode
;
695 req
->dri2ReqType
= X_DRI2SwapInterval
;
696 req
->drawable
= drawable
;
697 req
->interval
= interval
;
703 #endif /* GLX_DIRECT_RENDERING */