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
,
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
;
187 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
190 GetReq(DRI2QueryVersion
, req
);
191 req
->reqType
= info
->codes
->major_opcode
;
192 req
->dri2ReqType
= X_DRI2QueryVersion
;
193 req
->majorVersion
= DRI2_MAJOR
;
194 req
->minorVersion
= DRI2_MINOR
;
195 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
200 *major
= rep
.majorVersion
;
201 *minor
= rep
.minorVersion
;
205 switch (rep
.minorVersion
) {
218 for (i
= 0; i
< nevents
; i
++) {
219 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
220 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
227 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
229 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
230 xDRI2ConnectReply rep
;
231 xDRI2ConnectReq
*req
;
233 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
236 GetReq(DRI2Connect
, req
);
237 req
->reqType
= info
->codes
->major_opcode
;
238 req
->dri2ReqType
= X_DRI2Connect
;
239 req
->window
= window
;
240 req
->driverType
= DRI2DriverDRI
;
241 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
247 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
253 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
254 if (*driverName
== NULL
) {
256 ((rep
.driverNameLength
+ 3) & ~3) +
257 ((rep
.deviceNameLength
+ 3) & ~3));
262 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
263 (*driverName
)[rep
.driverNameLength
] = '\0';
265 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
266 if (*deviceName
== NULL
) {
268 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
273 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
274 (*deviceName
)[rep
.deviceNameLength
] = '\0';
283 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
285 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
286 xDRI2AuthenticateReq
*req
;
287 xDRI2AuthenticateReply rep
;
289 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
292 GetReq(DRI2Authenticate
, req
);
293 req
->reqType
= info
->codes
->major_opcode
;
294 req
->dri2ReqType
= X_DRI2Authenticate
;
295 req
->window
= window
;
298 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
307 return rep
.authenticated
;
311 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
313 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
314 xDRI2CreateDrawableReq
*req
;
316 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
319 GetReq(DRI2CreateDrawable
, req
);
320 req
->reqType
= info
->codes
->major_opcode
;
321 req
->dri2ReqType
= X_DRI2CreateDrawable
;
322 req
->drawable
= drawable
;
328 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
330 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
331 xDRI2DestroyDrawableReq
*req
;
333 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
338 GetReq(DRI2DestroyDrawable
, req
);
339 req
->reqType
= info
->codes
->major_opcode
;
340 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
341 req
->drawable
= drawable
;
347 DRI2GetBuffers(Display
* dpy
, XID drawable
,
348 int *width
, int *height
,
349 unsigned int *attachments
, int count
, int *outCount
)
351 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
352 xDRI2GetBuffersReply rep
;
353 xDRI2GetBuffersReq
*req
;
355 xDRI2Buffer repBuffer
;
359 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
362 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
363 req
->reqType
= info
->codes
->major_opcode
;
364 req
->dri2ReqType
= X_DRI2GetBuffers
;
365 req
->drawable
= drawable
;
367 p
= (CARD32
*) & req
[1];
368 for (i
= 0; i
< count
; i
++)
369 p
[i
] = attachments
[i
];
371 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
378 *height
= rep
.height
;
379 *outCount
= rep
.count
;
381 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
382 if (buffers
== NULL
) {
383 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
389 for (i
= 0; i
< rep
.count
; i
++) {
390 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
391 buffers
[i
].attachment
= repBuffer
.attachment
;
392 buffers
[i
].name
= repBuffer
.name
;
393 buffers
[i
].pitch
= repBuffer
.pitch
;
394 buffers
[i
].cpp
= repBuffer
.cpp
;
395 buffers
[i
].flags
= repBuffer
.flags
;
406 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
407 int *width
, int *height
,
408 unsigned int *attachments
, int count
, int *outCount
)
410 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
411 xDRI2GetBuffersReply rep
;
412 xDRI2GetBuffersReq
*req
;
414 xDRI2Buffer repBuffer
;
418 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
421 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
422 req
->reqType
= info
->codes
->major_opcode
;
423 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
424 req
->drawable
= drawable
;
426 p
= (CARD32
*) & req
[1];
427 for (i
= 0; i
< (count
* 2); i
++)
428 p
[i
] = attachments
[i
];
430 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
437 *height
= rep
.height
;
438 *outCount
= rep
.count
;
440 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
441 if (buffers
== NULL
) {
442 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
448 for (i
= 0; i
< rep
.count
; i
++) {
449 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
450 buffers
[i
].attachment
= repBuffer
.attachment
;
451 buffers
[i
].name
= repBuffer
.name
;
452 buffers
[i
].pitch
= repBuffer
.pitch
;
453 buffers
[i
].cpp
= repBuffer
.cpp
;
454 buffers
[i
].flags
= repBuffer
.flags
;
465 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
466 CARD32 dest
, CARD32 src
)
468 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
469 xDRI2CopyRegionReq
*req
;
470 xDRI2CopyRegionReply rep
;
472 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
475 GetReq(DRI2CopyRegion
, req
);
476 req
->reqType
= info
->codes
->major_opcode
;
477 req
->dri2ReqType
= X_DRI2CopyRegion
;
478 req
->drawable
= drawable
;
479 req
->region
= region
;
483 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
489 #ifdef X_DRI2SwapBuffers
491 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
494 req
->target_msc_hi
= target
>> 32;
495 req
->target_msc_lo
= target
& 0xffffffff;
496 req
->divisor_hi
= divisor
>> 32;
497 req
->divisor_lo
= divisor
& 0xffffffff;
498 req
->remainder_hi
= remainder
>> 32;
499 req
->remainder_lo
= remainder
& 0xffffffff;
503 vals_to_card64(CARD32 lo
, CARD32 hi
)
505 return (CARD64
)hi
<< 32 | lo
;
508 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
509 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
511 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
512 xDRI2SwapBuffersReq
*req
;
513 xDRI2SwapBuffersReply rep
;
515 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
518 GetReq(DRI2SwapBuffers
, req
);
519 req
->reqType
= info
->codes
->major_opcode
;
520 req
->dri2ReqType
= X_DRI2SwapBuffers
;
521 req
->drawable
= drawable
;
522 load_swap_req(req
, target_msc
, divisor
, remainder
);
524 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
526 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
534 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
537 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
541 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
544 GetReq(DRI2GetMSC
, req
);
545 req
->reqType
= info
->codes
->major_opcode
;
546 req
->dri2ReqType
= X_DRI2GetMSC
;
547 req
->drawable
= drawable
;
549 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
555 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
556 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
557 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
568 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
571 req
->target_msc_hi
= target
>> 32;
572 req
->target_msc_lo
= target
& 0xffffffff;
573 req
->divisor_hi
= divisor
>> 32;
574 req
->divisor_lo
= divisor
& 0xffffffff;
575 req
->remainder_hi
= remainder
>> 32;
576 req
->remainder_lo
= remainder
& 0xffffffff;
579 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
580 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
582 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
583 xDRI2WaitMSCReq
*req
;
586 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
589 GetReq(DRI2WaitMSC
, req
);
590 req
->reqType
= info
->codes
->major_opcode
;
591 req
->dri2ReqType
= X_DRI2WaitMSC
;
592 req
->drawable
= drawable
;
593 load_msc_req(req
, target_msc
, divisor
, remainder
);
595 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
601 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
602 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
603 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
614 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
616 req
->target_sbc_hi
= target
>> 32;
617 req
->target_sbc_lo
= target
& 0xffffffff;
620 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
621 CARD64
*msc
, CARD64
*sbc
)
623 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
624 xDRI2WaitSBCReq
*req
;
627 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
630 GetReq(DRI2WaitSBC
, req
);
631 req
->reqType
= info
->codes
->major_opcode
;
632 req
->dri2ReqType
= X_DRI2WaitSBC
;
633 req
->drawable
= drawable
;
634 load_sbc_req(req
, target_sbc
);
636 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
642 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
643 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
644 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
653 #ifdef X_DRI2SwapInterval
654 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
656 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
657 xDRI2SwapIntervalReq
*req
;
659 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
662 GetReq(DRI2SwapInterval
, req
);
663 req
->reqType
= info
->codes
->major_opcode
;
664 req
->dri2ReqType
= X_DRI2SwapInterval
;
665 req
->drawable
= drawable
;
666 req
->interval
= interval
;
672 #endif /* GLX_DIRECT_RENDERING */