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
)
193 /* If the server is non-local DRI2Connect will raise BadRequest.
194 * Swallow this so that DRI2Connect can signal this in its return code */
195 if (err
->majorCode
== codes
->major_opcode
&&
196 err
->minorCode
== X_DRI2Connect
&&
197 err
->errorCode
== BadRequest
) {
206 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
208 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
210 if (XextHasExtension(info
)) {
211 *eventBase
= info
->codes
->first_event
;
212 *errorBase
= info
->codes
->first_error
;
220 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
222 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
223 xDRI2QueryVersionReply rep
;
224 xDRI2QueryVersionReq
*req
;
227 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
230 GetReq(DRI2QueryVersion
, req
);
231 req
->reqType
= info
->codes
->major_opcode
;
232 req
->dri2ReqType
= X_DRI2QueryVersion
;
233 req
->majorVersion
= DRI2_MAJOR
;
234 req
->minorVersion
= DRI2_MINOR
;
235 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
240 *major
= rep
.majorVersion
;
241 *minor
= rep
.minorVersion
;
245 switch (rep
.minorVersion
) {
258 for (i
= 0; i
< nevents
; i
++) {
259 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
260 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
267 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
269 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
270 xDRI2ConnectReply rep
;
271 xDRI2ConnectReq
*req
;
273 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
276 GetReq(DRI2Connect
, req
);
277 req
->reqType
= info
->codes
->major_opcode
;
278 req
->dri2ReqType
= X_DRI2Connect
;
279 req
->window
= window
;
280 req
->driverType
= DRI2DriverDRI
;
281 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
287 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
293 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
294 if (*driverName
== NULL
) {
296 ((rep
.driverNameLength
+ 3) & ~3) +
297 ((rep
.deviceNameLength
+ 3) & ~3));
302 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
303 (*driverName
)[rep
.driverNameLength
] = '\0';
305 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
306 if (*deviceName
== NULL
) {
308 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
313 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
314 (*deviceName
)[rep
.deviceNameLength
] = '\0';
323 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
325 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
326 xDRI2AuthenticateReq
*req
;
327 xDRI2AuthenticateReply rep
;
329 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
332 GetReq(DRI2Authenticate
, req
);
333 req
->reqType
= info
->codes
->major_opcode
;
334 req
->dri2ReqType
= X_DRI2Authenticate
;
335 req
->window
= window
;
338 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
347 return rep
.authenticated
;
351 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
353 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
354 xDRI2CreateDrawableReq
*req
;
356 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
359 GetReq(DRI2CreateDrawable
, req
);
360 req
->reqType
= info
->codes
->major_opcode
;
361 req
->dri2ReqType
= X_DRI2CreateDrawable
;
362 req
->drawable
= drawable
;
368 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
370 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
371 xDRI2DestroyDrawableReq
*req
;
373 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
378 GetReq(DRI2DestroyDrawable
, req
);
379 req
->reqType
= info
->codes
->major_opcode
;
380 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
381 req
->drawable
= drawable
;
387 DRI2GetBuffers(Display
* dpy
, XID drawable
,
388 int *width
, int *height
,
389 unsigned int *attachments
, int count
, int *outCount
)
391 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
392 xDRI2GetBuffersReply rep
;
393 xDRI2GetBuffersReq
*req
;
395 xDRI2Buffer repBuffer
;
399 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
402 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
403 req
->reqType
= info
->codes
->major_opcode
;
404 req
->dri2ReqType
= X_DRI2GetBuffers
;
405 req
->drawable
= drawable
;
407 p
= (CARD32
*) & req
[1];
408 for (i
= 0; i
< count
; i
++)
409 p
[i
] = attachments
[i
];
411 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
418 *height
= rep
.height
;
419 *outCount
= rep
.count
;
421 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
422 if (buffers
== NULL
) {
423 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
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
;
446 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
447 int *width
, int *height
,
448 unsigned int *attachments
, int count
, int *outCount
)
450 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
451 xDRI2GetBuffersReply rep
;
452 xDRI2GetBuffersReq
*req
;
454 xDRI2Buffer repBuffer
;
458 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
461 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
462 req
->reqType
= info
->codes
->major_opcode
;
463 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
464 req
->drawable
= drawable
;
466 p
= (CARD32
*) & req
[1];
467 for (i
= 0; i
< (count
* 2); i
++)
468 p
[i
] = attachments
[i
];
470 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
477 *height
= rep
.height
;
478 *outCount
= rep
.count
;
480 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
481 if (buffers
== NULL
) {
482 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
488 for (i
= 0; i
< rep
.count
; i
++) {
489 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
490 buffers
[i
].attachment
= repBuffer
.attachment
;
491 buffers
[i
].name
= repBuffer
.name
;
492 buffers
[i
].pitch
= repBuffer
.pitch
;
493 buffers
[i
].cpp
= repBuffer
.cpp
;
494 buffers
[i
].flags
= repBuffer
.flags
;
505 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
506 CARD32 dest
, CARD32 src
)
508 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
509 xDRI2CopyRegionReq
*req
;
510 xDRI2CopyRegionReply rep
;
512 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
515 GetReq(DRI2CopyRegion
, req
);
516 req
->reqType
= info
->codes
->major_opcode
;
517 req
->dri2ReqType
= X_DRI2CopyRegion
;
518 req
->drawable
= drawable
;
519 req
->region
= region
;
523 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
529 #ifdef X_DRI2SwapBuffers
531 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
534 req
->target_msc_hi
= target
>> 32;
535 req
->target_msc_lo
= target
& 0xffffffff;
536 req
->divisor_hi
= divisor
>> 32;
537 req
->divisor_lo
= divisor
& 0xffffffff;
538 req
->remainder_hi
= remainder
>> 32;
539 req
->remainder_lo
= remainder
& 0xffffffff;
543 vals_to_card64(CARD32 lo
, CARD32 hi
)
545 return (CARD64
)hi
<< 32 | lo
;
548 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
549 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
551 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
552 xDRI2SwapBuffersReq
*req
;
553 xDRI2SwapBuffersReply rep
;
555 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
558 GetReq(DRI2SwapBuffers
, req
);
559 req
->reqType
= info
->codes
->major_opcode
;
560 req
->dri2ReqType
= X_DRI2SwapBuffers
;
561 req
->drawable
= drawable
;
562 load_swap_req(req
, target_msc
, divisor
, remainder
);
564 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
566 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
574 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
577 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
581 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
584 GetReq(DRI2GetMSC
, req
);
585 req
->reqType
= info
->codes
->major_opcode
;
586 req
->dri2ReqType
= X_DRI2GetMSC
;
587 req
->drawable
= drawable
;
589 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
595 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
596 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
597 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
608 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
611 req
->target_msc_hi
= target
>> 32;
612 req
->target_msc_lo
= target
& 0xffffffff;
613 req
->divisor_hi
= divisor
>> 32;
614 req
->divisor_lo
= divisor
& 0xffffffff;
615 req
->remainder_hi
= remainder
>> 32;
616 req
->remainder_lo
= remainder
& 0xffffffff;
619 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
620 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
622 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
623 xDRI2WaitMSCReq
*req
;
626 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
629 GetReq(DRI2WaitMSC
, req
);
630 req
->reqType
= info
->codes
->major_opcode
;
631 req
->dri2ReqType
= X_DRI2WaitMSC
;
632 req
->drawable
= drawable
;
633 load_msc_req(req
, target_msc
, divisor
, remainder
);
635 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
641 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
642 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
643 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
654 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
656 req
->target_sbc_hi
= target
>> 32;
657 req
->target_sbc_lo
= target
& 0xffffffff;
660 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
661 CARD64
*msc
, CARD64
*sbc
)
663 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
664 xDRI2WaitSBCReq
*req
;
667 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
670 GetReq(DRI2WaitSBC
, req
);
671 req
->reqType
= info
->codes
->major_opcode
;
672 req
->dri2ReqType
= X_DRI2WaitSBC
;
673 req
->drawable
= drawable
;
674 load_sbc_req(req
, target_sbc
);
676 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
682 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
683 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
684 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
693 #ifdef X_DRI2SwapInterval
694 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
696 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
697 xDRI2SwapIntervalReq
*req
;
699 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
702 GetReq(DRI2SwapInterval
, req
);
703 req
->reqType
= info
->codes
->major_opcode
;
704 req
->dri2ReqType
= X_DRI2SwapInterval
;
705 req
->drawable
= drawable
;
706 req
->interval
= interval
;
712 #endif /* GLX_DIRECT_RENDERING */