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
;
101 __GLXDRIdrawable
*pdraw
;
102 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
104 /* Ignore swap events if we're not looking for them */
105 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, awire
->drawable
);
106 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
109 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
110 aevent
->type
= glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
111 aevent
->send_event
= (awire
->type
& 0x80) != 0;
112 aevent
->display
= dpy
;
113 aevent
->drawable
= awire
->drawable
;
114 switch (awire
->event_type
) {
115 case DRI2_EXCHANGE_COMPLETE
:
116 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
118 case DRI2_BLIT_COMPLETE
:
119 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
121 case DRI2_FLIP_COMPLETE
:
122 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
125 /* unknown swap completion type */
128 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
129 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
130 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
134 #ifdef DRI2_InvalidateBuffers
135 case DRI2_InvalidateBuffers
:
137 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
139 dri2InvalidateBuffers(dpy
, awire
->drawable
);
144 /* client doesn't support server event */
151 /* We don't actually support this. It doesn't make sense for clients to
152 * send each other DRI2 events.
155 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
157 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
159 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
161 switch (event
->type
) {
163 /* client doesn't support server event */
171 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
173 if (err
->majorCode
== codes
->major_opcode
&&
174 err
->errorCode
== BadDrawable
&&
175 err
->minorCode
== X_DRI2CopyRegion
)
178 /* If the X drawable was destroyed before the GLX drawable, the
179 * DRI2 drawble will be gone by the time we call
180 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
181 if (err
->majorCode
== codes
->major_opcode
&&
182 err
->errorCode
== BadDrawable
&&
183 err
->minorCode
== X_DRI2DestroyDrawable
)
190 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
192 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
194 if (XextHasExtension(info
)) {
195 *eventBase
= info
->codes
->first_event
;
196 *errorBase
= info
->codes
->first_error
;
204 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
206 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
207 xDRI2QueryVersionReply rep
;
208 xDRI2QueryVersionReq
*req
;
211 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
214 GetReq(DRI2QueryVersion
, req
);
215 req
->reqType
= info
->codes
->major_opcode
;
216 req
->dri2ReqType
= X_DRI2QueryVersion
;
217 req
->majorVersion
= DRI2_MAJOR
;
218 req
->minorVersion
= DRI2_MINOR
;
219 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
224 *major
= rep
.majorVersion
;
225 *minor
= rep
.minorVersion
;
229 switch (rep
.minorVersion
) {
242 for (i
= 0; i
< nevents
; i
++) {
243 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
244 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
251 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
253 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
254 xDRI2ConnectReply rep
;
255 xDRI2ConnectReq
*req
;
257 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
260 GetReq(DRI2Connect
, req
);
261 req
->reqType
= info
->codes
->major_opcode
;
262 req
->dri2ReqType
= X_DRI2Connect
;
263 req
->window
= window
;
264 req
->driverType
= DRI2DriverDRI
;
265 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
271 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
277 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
278 if (*driverName
== NULL
) {
280 ((rep
.driverNameLength
+ 3) & ~3) +
281 ((rep
.deviceNameLength
+ 3) & ~3));
286 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
287 (*driverName
)[rep
.driverNameLength
] = '\0';
289 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
290 if (*deviceName
== NULL
) {
292 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
297 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
298 (*deviceName
)[rep
.deviceNameLength
] = '\0';
307 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
309 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
310 xDRI2AuthenticateReq
*req
;
311 xDRI2AuthenticateReply rep
;
313 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
316 GetReq(DRI2Authenticate
, req
);
317 req
->reqType
= info
->codes
->major_opcode
;
318 req
->dri2ReqType
= X_DRI2Authenticate
;
319 req
->window
= window
;
322 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
331 return rep
.authenticated
;
335 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
337 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
338 xDRI2CreateDrawableReq
*req
;
340 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
343 GetReq(DRI2CreateDrawable
, req
);
344 req
->reqType
= info
->codes
->major_opcode
;
345 req
->dri2ReqType
= X_DRI2CreateDrawable
;
346 req
->drawable
= drawable
;
352 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
354 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
355 xDRI2DestroyDrawableReq
*req
;
357 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
362 GetReq(DRI2DestroyDrawable
, req
);
363 req
->reqType
= info
->codes
->major_opcode
;
364 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
365 req
->drawable
= drawable
;
371 DRI2GetBuffers(Display
* dpy
, XID drawable
,
372 int *width
, int *height
,
373 unsigned int *attachments
, int count
, int *outCount
)
375 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
376 xDRI2GetBuffersReply rep
;
377 xDRI2GetBuffersReq
*req
;
379 xDRI2Buffer repBuffer
;
383 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
386 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
387 req
->reqType
= info
->codes
->major_opcode
;
388 req
->dri2ReqType
= X_DRI2GetBuffers
;
389 req
->drawable
= drawable
;
391 p
= (CARD32
*) & req
[1];
392 for (i
= 0; i
< count
; i
++)
393 p
[i
] = attachments
[i
];
395 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
402 *height
= rep
.height
;
403 *outCount
= rep
.count
;
405 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
406 if (buffers
== NULL
) {
407 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
413 for (i
= 0; i
< rep
.count
; i
++) {
414 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
415 buffers
[i
].attachment
= repBuffer
.attachment
;
416 buffers
[i
].name
= repBuffer
.name
;
417 buffers
[i
].pitch
= repBuffer
.pitch
;
418 buffers
[i
].cpp
= repBuffer
.cpp
;
419 buffers
[i
].flags
= repBuffer
.flags
;
430 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
431 int *width
, int *height
,
432 unsigned int *attachments
, int count
, int *outCount
)
434 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
435 xDRI2GetBuffersReply rep
;
436 xDRI2GetBuffersReq
*req
;
438 xDRI2Buffer repBuffer
;
442 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
445 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
446 req
->reqType
= info
->codes
->major_opcode
;
447 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
448 req
->drawable
= drawable
;
450 p
= (CARD32
*) & req
[1];
451 for (i
= 0; i
< (count
* 2); i
++)
452 p
[i
] = attachments
[i
];
454 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
461 *height
= rep
.height
;
462 *outCount
= rep
.count
;
464 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
465 if (buffers
== NULL
) {
466 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
472 for (i
= 0; i
< rep
.count
; i
++) {
473 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
474 buffers
[i
].attachment
= repBuffer
.attachment
;
475 buffers
[i
].name
= repBuffer
.name
;
476 buffers
[i
].pitch
= repBuffer
.pitch
;
477 buffers
[i
].cpp
= repBuffer
.cpp
;
478 buffers
[i
].flags
= repBuffer
.flags
;
489 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
490 CARD32 dest
, CARD32 src
)
492 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
493 xDRI2CopyRegionReq
*req
;
494 xDRI2CopyRegionReply rep
;
496 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
499 GetReq(DRI2CopyRegion
, req
);
500 req
->reqType
= info
->codes
->major_opcode
;
501 req
->dri2ReqType
= X_DRI2CopyRegion
;
502 req
->drawable
= drawable
;
503 req
->region
= region
;
507 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
513 #ifdef X_DRI2SwapBuffers
515 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
518 req
->target_msc_hi
= target
>> 32;
519 req
->target_msc_lo
= target
& 0xffffffff;
520 req
->divisor_hi
= divisor
>> 32;
521 req
->divisor_lo
= divisor
& 0xffffffff;
522 req
->remainder_hi
= remainder
>> 32;
523 req
->remainder_lo
= remainder
& 0xffffffff;
527 vals_to_card64(CARD32 lo
, CARD32 hi
)
529 return (CARD64
)hi
<< 32 | lo
;
532 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
533 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
535 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
536 xDRI2SwapBuffersReq
*req
;
537 xDRI2SwapBuffersReply rep
;
539 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
542 GetReq(DRI2SwapBuffers
, req
);
543 req
->reqType
= info
->codes
->major_opcode
;
544 req
->dri2ReqType
= X_DRI2SwapBuffers
;
545 req
->drawable
= drawable
;
546 load_swap_req(req
, target_msc
, divisor
, remainder
);
548 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
550 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
558 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
561 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
565 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
568 GetReq(DRI2GetMSC
, req
);
569 req
->reqType
= info
->codes
->major_opcode
;
570 req
->dri2ReqType
= X_DRI2GetMSC
;
571 req
->drawable
= drawable
;
573 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
579 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
580 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
581 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
592 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
595 req
->target_msc_hi
= target
>> 32;
596 req
->target_msc_lo
= target
& 0xffffffff;
597 req
->divisor_hi
= divisor
>> 32;
598 req
->divisor_lo
= divisor
& 0xffffffff;
599 req
->remainder_hi
= remainder
>> 32;
600 req
->remainder_lo
= remainder
& 0xffffffff;
603 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
604 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
606 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
607 xDRI2WaitMSCReq
*req
;
610 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
613 GetReq(DRI2WaitMSC
, req
);
614 req
->reqType
= info
->codes
->major_opcode
;
615 req
->dri2ReqType
= X_DRI2WaitMSC
;
616 req
->drawable
= drawable
;
617 load_msc_req(req
, target_msc
, divisor
, remainder
);
619 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
625 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
626 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
627 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
638 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
640 req
->target_sbc_hi
= target
>> 32;
641 req
->target_sbc_lo
= target
& 0xffffffff;
644 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
645 CARD64
*msc
, CARD64
*sbc
)
647 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
648 xDRI2WaitSBCReq
*req
;
651 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
654 GetReq(DRI2WaitSBC
, req
);
655 req
->reqType
= info
->codes
->major_opcode
;
656 req
->dri2ReqType
= X_DRI2WaitSBC
;
657 req
->drawable
= drawable
;
658 load_sbc_req(req
, target_sbc
);
660 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
666 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
667 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
668 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
677 #ifdef X_DRI2SwapInterval
678 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
680 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
681 xDRI2SwapIntervalReq
*req
;
683 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
686 GetReq(DRI2SwapInterval
, req
);
687 req
->reqType
= info
->codes
->major_opcode
;
688 req
->dri2ReqType
= X_DRI2SwapInterval
;
689 req
->drawable
= drawable
;
690 req
->interval
= interval
;
696 #endif /* GLX_DIRECT_RENDERING */