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
;
102 /* Ignore swap events if we're not looking for them */
103 aevent
->type
= dri2GetSwapEventType(dpy
, awire
->drawable
);
107 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
108 aevent
->send_event
= (awire
->type
& 0x80) != 0;
109 aevent
->display
= dpy
;
110 aevent
->drawable
= awire
->drawable
;
111 switch (awire
->event_type
) {
112 case DRI2_EXCHANGE_COMPLETE
:
113 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
115 case DRI2_BLIT_COMPLETE
:
116 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
118 case DRI2_FLIP_COMPLETE
:
119 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
122 /* unknown swap completion type */
125 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
126 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
127 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
131 #ifdef DRI2_InvalidateBuffers
132 case DRI2_InvalidateBuffers
:
134 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
136 dri2InvalidateBuffers(dpy
, awire
->drawable
);
141 /* client doesn't support server event */
148 /* We don't actually support this. It doesn't make sense for clients to
149 * send each other DRI2 events.
152 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
154 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
156 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
158 switch (event
->type
) {
160 /* client doesn't support server event */
168 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
170 if (err
->majorCode
== codes
->major_opcode
&&
171 err
->errorCode
== BadDrawable
&&
172 err
->minorCode
== X_DRI2CopyRegion
)
175 /* If the X drawable was destroyed before the GLX drawable, the
176 * DRI2 drawble will be gone by the time we call
177 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
178 if (err
->majorCode
== codes
->major_opcode
&&
179 err
->errorCode
== BadDrawable
&&
180 err
->minorCode
== X_DRI2DestroyDrawable
)
187 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
189 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
191 if (XextHasExtension(info
)) {
192 *eventBase
= info
->codes
->first_event
;
193 *errorBase
= info
->codes
->first_error
;
201 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
203 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
204 xDRI2QueryVersionReply rep
;
205 xDRI2QueryVersionReq
*req
;
208 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
211 GetReq(DRI2QueryVersion
, req
);
212 req
->reqType
= info
->codes
->major_opcode
;
213 req
->dri2ReqType
= X_DRI2QueryVersion
;
214 req
->majorVersion
= DRI2_MAJOR
;
215 req
->minorVersion
= DRI2_MINOR
;
216 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
221 *major
= rep
.majorVersion
;
222 *minor
= rep
.minorVersion
;
226 switch (rep
.minorVersion
) {
239 for (i
= 0; i
< nevents
; i
++) {
240 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
241 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
248 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
250 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
251 xDRI2ConnectReply rep
;
252 xDRI2ConnectReq
*req
;
254 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
257 GetReq(DRI2Connect
, req
);
258 req
->reqType
= info
->codes
->major_opcode
;
259 req
->dri2ReqType
= X_DRI2Connect
;
260 req
->window
= window
;
261 req
->driverType
= DRI2DriverDRI
;
262 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
268 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
274 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
275 if (*driverName
== NULL
) {
277 ((rep
.driverNameLength
+ 3) & ~3) +
278 ((rep
.deviceNameLength
+ 3) & ~3));
283 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
284 (*driverName
)[rep
.driverNameLength
] = '\0';
286 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
287 if (*deviceName
== NULL
) {
289 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
294 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
295 (*deviceName
)[rep
.deviceNameLength
] = '\0';
304 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
306 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
307 xDRI2AuthenticateReq
*req
;
308 xDRI2AuthenticateReply rep
;
310 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
313 GetReq(DRI2Authenticate
, req
);
314 req
->reqType
= info
->codes
->major_opcode
;
315 req
->dri2ReqType
= X_DRI2Authenticate
;
316 req
->window
= window
;
319 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
328 return rep
.authenticated
;
332 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
334 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
335 xDRI2CreateDrawableReq
*req
;
337 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
340 GetReq(DRI2CreateDrawable
, req
);
341 req
->reqType
= info
->codes
->major_opcode
;
342 req
->dri2ReqType
= X_DRI2CreateDrawable
;
343 req
->drawable
= drawable
;
349 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
351 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
352 xDRI2DestroyDrawableReq
*req
;
354 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
359 GetReq(DRI2DestroyDrawable
, req
);
360 req
->reqType
= info
->codes
->major_opcode
;
361 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
362 req
->drawable
= drawable
;
368 DRI2GetBuffers(Display
* dpy
, XID drawable
,
369 int *width
, int *height
,
370 unsigned int *attachments
, int count
, int *outCount
)
372 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
373 xDRI2GetBuffersReply rep
;
374 xDRI2GetBuffersReq
*req
;
376 xDRI2Buffer repBuffer
;
380 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
383 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
384 req
->reqType
= info
->codes
->major_opcode
;
385 req
->dri2ReqType
= X_DRI2GetBuffers
;
386 req
->drawable
= drawable
;
388 p
= (CARD32
*) & req
[1];
389 for (i
= 0; i
< count
; i
++)
390 p
[i
] = attachments
[i
];
392 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
399 *height
= rep
.height
;
400 *outCount
= rep
.count
;
402 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
403 if (buffers
== NULL
) {
404 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
410 for (i
= 0; i
< rep
.count
; i
++) {
411 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
412 buffers
[i
].attachment
= repBuffer
.attachment
;
413 buffers
[i
].name
= repBuffer
.name
;
414 buffers
[i
].pitch
= repBuffer
.pitch
;
415 buffers
[i
].cpp
= repBuffer
.cpp
;
416 buffers
[i
].flags
= repBuffer
.flags
;
427 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
428 int *width
, int *height
,
429 unsigned int *attachments
, int count
, int *outCount
)
431 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
432 xDRI2GetBuffersReply rep
;
433 xDRI2GetBuffersReq
*req
;
435 xDRI2Buffer repBuffer
;
439 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
442 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
443 req
->reqType
= info
->codes
->major_opcode
;
444 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
445 req
->drawable
= drawable
;
447 p
= (CARD32
*) & req
[1];
448 for (i
= 0; i
< (count
* 2); i
++)
449 p
[i
] = attachments
[i
];
451 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
458 *height
= rep
.height
;
459 *outCount
= rep
.count
;
461 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
462 if (buffers
== NULL
) {
463 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
469 for (i
= 0; i
< rep
.count
; i
++) {
470 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
471 buffers
[i
].attachment
= repBuffer
.attachment
;
472 buffers
[i
].name
= repBuffer
.name
;
473 buffers
[i
].pitch
= repBuffer
.pitch
;
474 buffers
[i
].cpp
= repBuffer
.cpp
;
475 buffers
[i
].flags
= repBuffer
.flags
;
486 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
487 CARD32 dest
, CARD32 src
)
489 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
490 xDRI2CopyRegionReq
*req
;
491 xDRI2CopyRegionReply rep
;
493 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
496 GetReq(DRI2CopyRegion
, req
);
497 req
->reqType
= info
->codes
->major_opcode
;
498 req
->dri2ReqType
= X_DRI2CopyRegion
;
499 req
->drawable
= drawable
;
500 req
->region
= region
;
504 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
510 #ifdef X_DRI2SwapBuffers
512 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
515 req
->target_msc_hi
= target
>> 32;
516 req
->target_msc_lo
= target
& 0xffffffff;
517 req
->divisor_hi
= divisor
>> 32;
518 req
->divisor_lo
= divisor
& 0xffffffff;
519 req
->remainder_hi
= remainder
>> 32;
520 req
->remainder_lo
= remainder
& 0xffffffff;
524 vals_to_card64(CARD32 lo
, CARD32 hi
)
526 return (CARD64
)hi
<< 32 | lo
;
529 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
530 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
532 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
533 xDRI2SwapBuffersReq
*req
;
534 xDRI2SwapBuffersReply rep
;
536 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
539 GetReq(DRI2SwapBuffers
, req
);
540 req
->reqType
= info
->codes
->major_opcode
;
541 req
->dri2ReqType
= X_DRI2SwapBuffers
;
542 req
->drawable
= drawable
;
543 load_swap_req(req
, target_msc
, divisor
, remainder
);
545 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
547 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
555 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
558 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
562 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
565 GetReq(DRI2GetMSC
, req
);
566 req
->reqType
= info
->codes
->major_opcode
;
567 req
->dri2ReqType
= X_DRI2GetMSC
;
568 req
->drawable
= drawable
;
570 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
576 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
577 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
578 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
589 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
592 req
->target_msc_hi
= target
>> 32;
593 req
->target_msc_lo
= target
& 0xffffffff;
594 req
->divisor_hi
= divisor
>> 32;
595 req
->divisor_lo
= divisor
& 0xffffffff;
596 req
->remainder_hi
= remainder
>> 32;
597 req
->remainder_lo
= remainder
& 0xffffffff;
600 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
601 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
603 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
604 xDRI2WaitMSCReq
*req
;
607 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
610 GetReq(DRI2WaitMSC
, req
);
611 req
->reqType
= info
->codes
->major_opcode
;
612 req
->dri2ReqType
= X_DRI2WaitMSC
;
613 req
->drawable
= drawable
;
614 load_msc_req(req
, target_msc
, divisor
, remainder
);
616 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
622 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
623 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
624 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
635 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
637 req
->target_sbc_hi
= target
>> 32;
638 req
->target_sbc_lo
= target
& 0xffffffff;
641 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
642 CARD64
*msc
, CARD64
*sbc
)
644 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
645 xDRI2WaitSBCReq
*req
;
648 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
651 GetReq(DRI2WaitSBC
, req
);
652 req
->reqType
= info
->codes
->major_opcode
;
653 req
->dri2ReqType
= X_DRI2WaitSBC
;
654 req
->drawable
= drawable
;
655 load_sbc_req(req
, target_sbc
);
657 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
663 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
664 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
665 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
674 #ifdef X_DRI2SwapInterval
675 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
677 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
678 xDRI2SwapIntervalReq
*req
;
680 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
683 GetReq(DRI2SwapInterval
, req
);
684 req
->reqType
= info
->codes
->major_opcode
;
685 req
->dri2ReqType
= X_DRI2SwapInterval
;
686 req
->drawable
= drawable
;
687 req
->interval
= interval
;
693 #endif /* GLX_DIRECT_RENDERING */