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
38 #include <X11/Xlibint.h>
39 #include <X11/extensions/Xext.h>
40 #include <X11/extensions/extutil.h>
41 #include <X11/extensions/dri2proto.h>
44 #include "glxclient.h"
45 #include "GL/glxext.h"
47 /* Allow the build to work with an older versions of dri2proto.h and
53 #define X_DRI2GetBuffersWithFormat 7
57 static char dri2ExtensionName
[] = DRI2_NAME
;
58 static XExtensionInfo
*dri2Info
;
59 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay
, dri2Info
)
62 DRI2WireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
);
64 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
);
66 static /* const */ XExtensionHooks dri2ExtensionHooks
= {
71 NULL
, /* create_font */
73 DRI2CloseDisplay
, /* close_display */
74 DRI2WireToEvent
, /* wire_to_event */
75 DRI2EventToWire
, /* event_to_wire */
77 NULL
, /* error_string */
80 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay
,
84 DRI2NumberEvents
, NULL
)
87 DRI2WireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
89 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
90 XExtDisplayInfo
*glx_info
= __glXFindDisplay(dpy
);
91 static int glx_event_base
;
92 static Bool found_glx_info
= False
;
94 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
96 switch ((wire
->u
.u
.type
& 0x7f) - info
->codes
->first_event
) {
98 #ifdef X_DRI2SwapBuffers
99 case DRI2_BufferSwapComplete
:
101 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
102 xDRI2BufferSwapComplete
*awire
= (xDRI2BufferSwapComplete
*)wire
;
103 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
105 (glx_info
->codes
->first_event
+ GLX_BufferSwapComplete
) & 0x75;
106 aevent
->send_event
= (awire
->type
& 0x80) != 0;
107 aevent
->display
= dpy
;
108 aevent
->drawable
= awire
->drawable
;
109 switch (awire
->event_type
) {
110 case DRI2_EXCHANGE_COMPLETE
:
111 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
113 case DRI2_BLIT_COMPLETE
:
114 aevent
->event_type
= GLX_BLIT_COMPLETE_INTEL
;
116 case DRI2_FLIP_COMPLETE
:
117 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
120 /* unknown swap completion type */
123 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
124 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
125 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
129 #ifdef DRI2_InvalidateBuffers
130 case DRI2_InvalidateBuffers
:
132 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
134 dri2InvalidateBuffers(dpy
, awire
->drawable
);
139 /* client doesn't support server event */
146 /* We don't actually support this. It doesn't make sense for clients to
147 * send each other DRI2 events.
150 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
152 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
154 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
156 switch (event
->type
) {
158 /* client doesn't support server event */
166 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
168 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
170 if (XextHasExtension(info
)) {
171 *eventBase
= info
->codes
->first_event
;
172 *errorBase
= info
->codes
->first_error
;
180 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
182 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
183 xDRI2QueryVersionReply rep
;
184 xDRI2QueryVersionReq
*req
;
186 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
189 GetReq(DRI2QueryVersion
, req
);
190 req
->reqType
= info
->codes
->major_opcode
;
191 req
->dri2ReqType
= X_DRI2QueryVersion
;
192 req
->majorVersion
= DRI2_MAJOR
;
193 req
->minorVersion
= DRI2_MINOR
;
194 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
199 *major
= rep
.majorVersion
;
200 *minor
= rep
.minorVersion
;
208 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
210 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
211 xDRI2ConnectReply rep
;
212 xDRI2ConnectReq
*req
;
214 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
217 GetReq(DRI2Connect
, req
);
218 req
->reqType
= info
->codes
->major_opcode
;
219 req
->dri2ReqType
= X_DRI2Connect
;
220 req
->window
= window
;
221 req
->driverType
= DRI2DriverDRI
;
222 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
228 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
234 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
235 if (*driverName
== NULL
) {
237 ((rep
.driverNameLength
+ 3) & ~3) +
238 ((rep
.deviceNameLength
+ 3) & ~3));
243 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
244 (*driverName
)[rep
.driverNameLength
] = '\0';
246 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
247 if (*deviceName
== NULL
) {
249 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
254 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
255 (*deviceName
)[rep
.deviceNameLength
] = '\0';
264 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
266 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
267 xDRI2AuthenticateReq
*req
;
268 xDRI2AuthenticateReply rep
;
270 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
273 GetReq(DRI2Authenticate
, req
);
274 req
->reqType
= info
->codes
->major_opcode
;
275 req
->dri2ReqType
= X_DRI2Authenticate
;
276 req
->window
= window
;
279 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
288 return rep
.authenticated
;
292 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
294 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
295 xDRI2CreateDrawableReq
*req
;
297 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
300 GetReq(DRI2CreateDrawable
, req
);
301 req
->reqType
= info
->codes
->major_opcode
;
302 req
->dri2ReqType
= X_DRI2CreateDrawable
;
303 req
->drawable
= drawable
;
309 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
311 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
312 xDRI2DestroyDrawableReq
*req
;
314 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
319 GetReq(DRI2DestroyDrawable
, req
);
320 req
->reqType
= info
->codes
->major_opcode
;
321 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
322 req
->drawable
= drawable
;
328 DRI2GetBuffers(Display
* dpy
, XID drawable
,
329 int *width
, int *height
,
330 unsigned int *attachments
, int count
, int *outCount
)
332 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
333 xDRI2GetBuffersReply rep
;
334 xDRI2GetBuffersReq
*req
;
336 xDRI2Buffer repBuffer
;
340 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
343 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
344 req
->reqType
= info
->codes
->major_opcode
;
345 req
->dri2ReqType
= X_DRI2GetBuffers
;
346 req
->drawable
= drawable
;
348 p
= (CARD32
*) & req
[1];
349 for (i
= 0; i
< count
; i
++)
350 p
[i
] = attachments
[i
];
352 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
359 *height
= rep
.height
;
360 *outCount
= rep
.count
;
362 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
363 if (buffers
== NULL
) {
364 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
370 for (i
= 0; i
< rep
.count
; i
++) {
371 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
372 buffers
[i
].attachment
= repBuffer
.attachment
;
373 buffers
[i
].name
= repBuffer
.name
;
374 buffers
[i
].pitch
= repBuffer
.pitch
;
375 buffers
[i
].cpp
= repBuffer
.cpp
;
376 buffers
[i
].flags
= repBuffer
.flags
;
387 DRI2GetBuffersWithFormat(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 * 2), req
);
403 req
->reqType
= info
->codes
->major_opcode
;
404 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
405 req
->drawable
= drawable
;
407 p
= (CARD32
*) & req
[1];
408 for (i
= 0; i
< (count
* 2); 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 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
447 CARD32 dest
, CARD32 src
)
449 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
450 xDRI2CopyRegionReq
*req
;
451 xDRI2CopyRegionReply rep
;
453 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
456 GetReq(DRI2CopyRegion
, req
);
457 req
->reqType
= info
->codes
->major_opcode
;
458 req
->dri2ReqType
= X_DRI2CopyRegion
;
459 req
->drawable
= drawable
;
460 req
->region
= region
;
464 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
470 #ifdef X_DRI2SwapBuffers
472 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
475 req
->target_msc_hi
= target
>> 32;
476 req
->target_msc_lo
= target
& 0xffffffff;
477 req
->divisor_hi
= divisor
>> 32;
478 req
->divisor_lo
= divisor
& 0xffffffff;
479 req
->remainder_hi
= remainder
>> 32;
480 req
->remainder_lo
= remainder
& 0xffffffff;
484 vals_to_card64(CARD32 lo
, CARD32 hi
)
486 return (CARD64
)hi
<< 32 | lo
;
489 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
490 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
492 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
493 xDRI2SwapBuffersReq
*req
;
494 xDRI2SwapBuffersReply rep
;
496 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
499 GetReq(DRI2SwapBuffers
, req
);
500 req
->reqType
= info
->codes
->major_opcode
;
501 req
->dri2ReqType
= X_DRI2SwapBuffers
;
502 req
->drawable
= drawable
;
503 load_swap_req(req
, target_msc
, divisor
, remainder
);
505 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
507 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
515 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
518 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
522 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
525 GetReq(DRI2GetMSC
, req
);
526 req
->reqType
= info
->codes
->major_opcode
;
527 req
->dri2ReqType
= X_DRI2GetMSC
;
528 req
->drawable
= drawable
;
530 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
536 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
537 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
538 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
549 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
552 req
->target_msc_hi
= target
>> 32;
553 req
->target_msc_lo
= target
& 0xffffffff;
554 req
->divisor_hi
= divisor
>> 32;
555 req
->divisor_lo
= divisor
& 0xffffffff;
556 req
->remainder_hi
= remainder
>> 32;
557 req
->remainder_lo
= remainder
& 0xffffffff;
560 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
561 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
563 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
564 xDRI2WaitMSCReq
*req
;
567 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
570 GetReq(DRI2WaitMSC
, req
);
571 req
->reqType
= info
->codes
->major_opcode
;
572 req
->dri2ReqType
= X_DRI2WaitMSC
;
573 req
->drawable
= drawable
;
574 load_msc_req(req
, target_msc
, divisor
, remainder
);
576 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
582 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
583 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
584 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
595 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
597 req
->target_sbc_hi
= target
>> 32;
598 req
->target_sbc_lo
= target
& 0xffffffff;
601 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
602 CARD64
*msc
, CARD64
*sbc
)
604 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
605 xDRI2WaitSBCReq
*req
;
608 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
611 GetReq(DRI2WaitSBC
, req
);
612 req
->reqType
= info
->codes
->major_opcode
;
613 req
->dri2ReqType
= X_DRI2WaitSBC
;
614 req
->drawable
= drawable
;
615 load_sbc_req(req
, target_sbc
);
617 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
623 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
624 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
625 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
634 #ifdef X_DRI2SwapInterval
635 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
637 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
638 xDRI2SwapIntervalReq
*req
;
640 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
643 GetReq(DRI2SwapInterval
, req
);
644 req
->reqType
= info
->codes
->major_opcode
;
645 req
->dri2ReqType
= X_DRI2SwapInterval
;
646 req
->drawable
= drawable
;
647 req
->interval
= interval
;
653 #endif /* GLX_DIRECT_RENDERING */