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 XExtDisplayInfo
*glx_info
= __glXFindDisplay(dpy
);
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 xDRI2BufferSwapComplete
*awire
= (xDRI2BufferSwapComplete
*)wire
;
102 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, awire
->drawable
, NULL
);
104 /* Ignore swap events if we're not looking for them */
105 if (!(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
108 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
109 aevent
->type
= glx_info
->codes
->first_event
+ GLX_BufferSwapComplete
;
110 aevent
->send_event
= (awire
->type
& 0x80) != 0;
111 aevent
->display
= dpy
;
112 aevent
->drawable
= awire
->drawable
;
113 switch (awire
->event_type
) {
114 case DRI2_EXCHANGE_COMPLETE
:
115 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
117 case DRI2_BLIT_COMPLETE
:
118 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
120 case DRI2_FLIP_COMPLETE
:
121 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
124 /* unknown swap completion type */
127 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
128 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
129 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
133 #ifdef DRI2_InvalidateBuffers
134 case DRI2_InvalidateBuffers
:
136 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
138 dri2InvalidateBuffers(dpy
, awire
->drawable
);
143 /* client doesn't support server event */
150 /* We don't actually support this. It doesn't make sense for clients to
151 * send each other DRI2 events.
154 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
156 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
158 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
160 switch (event
->type
) {
162 /* client doesn't support server event */
170 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
172 if (err
->majorCode
== codes
->major_opcode
&&
173 err
->errorCode
== BadDrawable
&&
174 err
->minorCode
== X_DRI2CopyRegion
)
181 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
183 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
185 if (XextHasExtension(info
)) {
186 *eventBase
= info
->codes
->first_event
;
187 *errorBase
= info
->codes
->first_error
;
195 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
197 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
198 xDRI2QueryVersionReply rep
;
199 xDRI2QueryVersionReq
*req
;
202 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
205 GetReq(DRI2QueryVersion
, req
);
206 req
->reqType
= info
->codes
->major_opcode
;
207 req
->dri2ReqType
= X_DRI2QueryVersion
;
208 req
->majorVersion
= DRI2_MAJOR
;
209 req
->minorVersion
= DRI2_MINOR
;
210 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
215 *major
= rep
.majorVersion
;
216 *minor
= rep
.minorVersion
;
220 switch (rep
.minorVersion
) {
233 for (i
= 0; i
< nevents
; i
++) {
234 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
235 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
242 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
244 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
245 xDRI2ConnectReply rep
;
246 xDRI2ConnectReq
*req
;
248 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
251 GetReq(DRI2Connect
, req
);
252 req
->reqType
= info
->codes
->major_opcode
;
253 req
->dri2ReqType
= X_DRI2Connect
;
254 req
->window
= window
;
255 req
->driverType
= DRI2DriverDRI
;
256 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
262 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
268 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
269 if (*driverName
== NULL
) {
271 ((rep
.driverNameLength
+ 3) & ~3) +
272 ((rep
.deviceNameLength
+ 3) & ~3));
277 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
278 (*driverName
)[rep
.driverNameLength
] = '\0';
280 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
281 if (*deviceName
== NULL
) {
283 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
288 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
289 (*deviceName
)[rep
.deviceNameLength
] = '\0';
298 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
300 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
301 xDRI2AuthenticateReq
*req
;
302 xDRI2AuthenticateReply rep
;
304 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
307 GetReq(DRI2Authenticate
, req
);
308 req
->reqType
= info
->codes
->major_opcode
;
309 req
->dri2ReqType
= X_DRI2Authenticate
;
310 req
->window
= window
;
313 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
322 return rep
.authenticated
;
326 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
328 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
329 xDRI2CreateDrawableReq
*req
;
331 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
334 GetReq(DRI2CreateDrawable
, req
);
335 req
->reqType
= info
->codes
->major_opcode
;
336 req
->dri2ReqType
= X_DRI2CreateDrawable
;
337 req
->drawable
= drawable
;
343 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
345 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
346 xDRI2DestroyDrawableReq
*req
;
348 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
353 GetReq(DRI2DestroyDrawable
, req
);
354 req
->reqType
= info
->codes
->major_opcode
;
355 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
356 req
->drawable
= drawable
;
362 DRI2GetBuffers(Display
* dpy
, XID drawable
,
363 int *width
, int *height
,
364 unsigned int *attachments
, int count
, int *outCount
)
366 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
367 xDRI2GetBuffersReply rep
;
368 xDRI2GetBuffersReq
*req
;
370 xDRI2Buffer repBuffer
;
374 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
377 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
378 req
->reqType
= info
->codes
->major_opcode
;
379 req
->dri2ReqType
= X_DRI2GetBuffers
;
380 req
->drawable
= drawable
;
382 p
= (CARD32
*) & req
[1];
383 for (i
= 0; i
< count
; i
++)
384 p
[i
] = attachments
[i
];
386 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
393 *height
= rep
.height
;
394 *outCount
= rep
.count
;
396 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
397 if (buffers
== NULL
) {
398 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
404 for (i
= 0; i
< rep
.count
; i
++) {
405 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
406 buffers
[i
].attachment
= repBuffer
.attachment
;
407 buffers
[i
].name
= repBuffer
.name
;
408 buffers
[i
].pitch
= repBuffer
.pitch
;
409 buffers
[i
].cpp
= repBuffer
.cpp
;
410 buffers
[i
].flags
= repBuffer
.flags
;
421 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
422 int *width
, int *height
,
423 unsigned int *attachments
, int count
, int *outCount
)
425 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
426 xDRI2GetBuffersReply rep
;
427 xDRI2GetBuffersReq
*req
;
429 xDRI2Buffer repBuffer
;
433 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
436 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
437 req
->reqType
= info
->codes
->major_opcode
;
438 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
439 req
->drawable
= drawable
;
441 p
= (CARD32
*) & req
[1];
442 for (i
= 0; i
< (count
* 2); i
++)
443 p
[i
] = attachments
[i
];
445 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
452 *height
= rep
.height
;
453 *outCount
= rep
.count
;
455 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
456 if (buffers
== NULL
) {
457 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
463 for (i
= 0; i
< rep
.count
; i
++) {
464 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
465 buffers
[i
].attachment
= repBuffer
.attachment
;
466 buffers
[i
].name
= repBuffer
.name
;
467 buffers
[i
].pitch
= repBuffer
.pitch
;
468 buffers
[i
].cpp
= repBuffer
.cpp
;
469 buffers
[i
].flags
= repBuffer
.flags
;
480 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
481 CARD32 dest
, CARD32 src
)
483 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
484 xDRI2CopyRegionReq
*req
;
485 xDRI2CopyRegionReply rep
;
487 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
490 GetReq(DRI2CopyRegion
, req
);
491 req
->reqType
= info
->codes
->major_opcode
;
492 req
->dri2ReqType
= X_DRI2CopyRegion
;
493 req
->drawable
= drawable
;
494 req
->region
= region
;
498 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
504 #ifdef X_DRI2SwapBuffers
506 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
509 req
->target_msc_hi
= target
>> 32;
510 req
->target_msc_lo
= target
& 0xffffffff;
511 req
->divisor_hi
= divisor
>> 32;
512 req
->divisor_lo
= divisor
& 0xffffffff;
513 req
->remainder_hi
= remainder
>> 32;
514 req
->remainder_lo
= remainder
& 0xffffffff;
518 vals_to_card64(CARD32 lo
, CARD32 hi
)
520 return (CARD64
)hi
<< 32 | lo
;
523 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
524 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
526 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
527 xDRI2SwapBuffersReq
*req
;
528 xDRI2SwapBuffersReply rep
;
530 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
533 GetReq(DRI2SwapBuffers
, req
);
534 req
->reqType
= info
->codes
->major_opcode
;
535 req
->dri2ReqType
= X_DRI2SwapBuffers
;
536 req
->drawable
= drawable
;
537 load_swap_req(req
, target_msc
, divisor
, remainder
);
539 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
541 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
549 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
552 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
556 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
559 GetReq(DRI2GetMSC
, req
);
560 req
->reqType
= info
->codes
->major_opcode
;
561 req
->dri2ReqType
= X_DRI2GetMSC
;
562 req
->drawable
= drawable
;
564 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
570 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
571 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
572 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
583 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
586 req
->target_msc_hi
= target
>> 32;
587 req
->target_msc_lo
= target
& 0xffffffff;
588 req
->divisor_hi
= divisor
>> 32;
589 req
->divisor_lo
= divisor
& 0xffffffff;
590 req
->remainder_hi
= remainder
>> 32;
591 req
->remainder_lo
= remainder
& 0xffffffff;
594 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
595 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
597 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
598 xDRI2WaitMSCReq
*req
;
601 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
604 GetReq(DRI2WaitMSC
, req
);
605 req
->reqType
= info
->codes
->major_opcode
;
606 req
->dri2ReqType
= X_DRI2WaitMSC
;
607 req
->drawable
= drawable
;
608 load_msc_req(req
, target_msc
, divisor
, remainder
);
610 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
616 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
617 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
618 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
629 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
631 req
->target_sbc_hi
= target
>> 32;
632 req
->target_sbc_lo
= target
& 0xffffffff;
635 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
636 CARD64
*msc
, CARD64
*sbc
)
638 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
639 xDRI2WaitSBCReq
*req
;
642 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
645 GetReq(DRI2WaitSBC
, req
);
646 req
->reqType
= info
->codes
->major_opcode
;
647 req
->dri2ReqType
= X_DRI2WaitSBC
;
648 req
->drawable
= drawable
;
649 load_sbc_req(req
, target_sbc
);
651 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
657 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
658 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
659 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
668 #ifdef X_DRI2SwapInterval
669 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
671 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
672 xDRI2SwapIntervalReq
*req
;
674 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
677 GetReq(DRI2SwapInterval
, req
);
678 req
->reqType
= info
->codes
->major_opcode
;
679 req
->dri2ReqType
= X_DRI2SwapInterval
;
680 req
->drawable
= drawable
;
681 req
->interval
= interval
;
687 #endif /* GLX_DIRECT_RENDERING */