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
->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
)
182 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
184 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
186 if (XextHasExtension(info
)) {
187 *eventBase
= info
->codes
->first_event
;
188 *errorBase
= info
->codes
->first_error
;
196 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
198 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
199 xDRI2QueryVersionReply rep
;
200 xDRI2QueryVersionReq
*req
;
203 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
206 GetReq(DRI2QueryVersion
, req
);
207 req
->reqType
= info
->codes
->major_opcode
;
208 req
->dri2ReqType
= X_DRI2QueryVersion
;
209 req
->majorVersion
= DRI2_MAJOR
;
210 req
->minorVersion
= DRI2_MINOR
;
211 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
216 *major
= rep
.majorVersion
;
217 *minor
= rep
.minorVersion
;
221 switch (rep
.minorVersion
) {
234 for (i
= 0; i
< nevents
; i
++) {
235 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
236 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
243 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
245 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
246 xDRI2ConnectReply rep
;
247 xDRI2ConnectReq
*req
;
249 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
252 GetReq(DRI2Connect
, req
);
253 req
->reqType
= info
->codes
->major_opcode
;
254 req
->dri2ReqType
= X_DRI2Connect
;
255 req
->window
= window
;
256 req
->driverType
= DRI2DriverDRI
;
257 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
263 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
269 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
270 if (*driverName
== NULL
) {
272 ((rep
.driverNameLength
+ 3) & ~3) +
273 ((rep
.deviceNameLength
+ 3) & ~3));
278 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
279 (*driverName
)[rep
.driverNameLength
] = '\0';
281 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
282 if (*deviceName
== NULL
) {
284 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
289 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
290 (*deviceName
)[rep
.deviceNameLength
] = '\0';
299 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
301 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
302 xDRI2AuthenticateReq
*req
;
303 xDRI2AuthenticateReply rep
;
305 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
308 GetReq(DRI2Authenticate
, req
);
309 req
->reqType
= info
->codes
->major_opcode
;
310 req
->dri2ReqType
= X_DRI2Authenticate
;
311 req
->window
= window
;
314 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
323 return rep
.authenticated
;
327 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
329 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
330 xDRI2CreateDrawableReq
*req
;
332 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
335 GetReq(DRI2CreateDrawable
, req
);
336 req
->reqType
= info
->codes
->major_opcode
;
337 req
->dri2ReqType
= X_DRI2CreateDrawable
;
338 req
->drawable
= drawable
;
344 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
346 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
347 xDRI2DestroyDrawableReq
*req
;
349 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
354 GetReq(DRI2DestroyDrawable
, req
);
355 req
->reqType
= info
->codes
->major_opcode
;
356 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
357 req
->drawable
= drawable
;
363 DRI2GetBuffers(Display
* dpy
, XID drawable
,
364 int *width
, int *height
,
365 unsigned int *attachments
, int count
, int *outCount
)
367 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
368 xDRI2GetBuffersReply rep
;
369 xDRI2GetBuffersReq
*req
;
371 xDRI2Buffer repBuffer
;
375 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
378 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
379 req
->reqType
= info
->codes
->major_opcode
;
380 req
->dri2ReqType
= X_DRI2GetBuffers
;
381 req
->drawable
= drawable
;
383 p
= (CARD32
*) & req
[1];
384 for (i
= 0; i
< count
; i
++)
385 p
[i
] = attachments
[i
];
387 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
394 *height
= rep
.height
;
395 *outCount
= rep
.count
;
397 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
398 if (buffers
== NULL
) {
399 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
405 for (i
= 0; i
< rep
.count
; i
++) {
406 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
407 buffers
[i
].attachment
= repBuffer
.attachment
;
408 buffers
[i
].name
= repBuffer
.name
;
409 buffers
[i
].pitch
= repBuffer
.pitch
;
410 buffers
[i
].cpp
= repBuffer
.cpp
;
411 buffers
[i
].flags
= repBuffer
.flags
;
422 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
423 int *width
, int *height
,
424 unsigned int *attachments
, int count
, int *outCount
)
426 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
427 xDRI2GetBuffersReply rep
;
428 xDRI2GetBuffersReq
*req
;
430 xDRI2Buffer repBuffer
;
434 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
437 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
438 req
->reqType
= info
->codes
->major_opcode
;
439 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
440 req
->drawable
= drawable
;
442 p
= (CARD32
*) & req
[1];
443 for (i
= 0; i
< (count
* 2); i
++)
444 p
[i
] = attachments
[i
];
446 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
453 *height
= rep
.height
;
454 *outCount
= rep
.count
;
456 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
457 if (buffers
== NULL
) {
458 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
464 for (i
= 0; i
< rep
.count
; i
++) {
465 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
466 buffers
[i
].attachment
= repBuffer
.attachment
;
467 buffers
[i
].name
= repBuffer
.name
;
468 buffers
[i
].pitch
= repBuffer
.pitch
;
469 buffers
[i
].cpp
= repBuffer
.cpp
;
470 buffers
[i
].flags
= repBuffer
.flags
;
481 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
482 CARD32 dest
, CARD32 src
)
484 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
485 xDRI2CopyRegionReq
*req
;
486 xDRI2CopyRegionReply rep
;
488 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
491 GetReq(DRI2CopyRegion
, req
);
492 req
->reqType
= info
->codes
->major_opcode
;
493 req
->dri2ReqType
= X_DRI2CopyRegion
;
494 req
->drawable
= drawable
;
495 req
->region
= region
;
499 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
505 #ifdef X_DRI2SwapBuffers
507 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
510 req
->target_msc_hi
= target
>> 32;
511 req
->target_msc_lo
= target
& 0xffffffff;
512 req
->divisor_hi
= divisor
>> 32;
513 req
->divisor_lo
= divisor
& 0xffffffff;
514 req
->remainder_hi
= remainder
>> 32;
515 req
->remainder_lo
= remainder
& 0xffffffff;
519 vals_to_card64(CARD32 lo
, CARD32 hi
)
521 return (CARD64
)hi
<< 32 | lo
;
524 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
525 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
527 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
528 xDRI2SwapBuffersReq
*req
;
529 xDRI2SwapBuffersReply rep
;
531 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
534 GetReq(DRI2SwapBuffers
, req
);
535 req
->reqType
= info
->codes
->major_opcode
;
536 req
->dri2ReqType
= X_DRI2SwapBuffers
;
537 req
->drawable
= drawable
;
538 load_swap_req(req
, target_msc
, divisor
, remainder
);
540 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
542 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
550 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
553 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
557 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
560 GetReq(DRI2GetMSC
, req
);
561 req
->reqType
= info
->codes
->major_opcode
;
562 req
->dri2ReqType
= X_DRI2GetMSC
;
563 req
->drawable
= drawable
;
565 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
571 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
572 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
573 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
584 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
587 req
->target_msc_hi
= target
>> 32;
588 req
->target_msc_lo
= target
& 0xffffffff;
589 req
->divisor_hi
= divisor
>> 32;
590 req
->divisor_lo
= divisor
& 0xffffffff;
591 req
->remainder_hi
= remainder
>> 32;
592 req
->remainder_lo
= remainder
& 0xffffffff;
595 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
596 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
598 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
599 xDRI2WaitMSCReq
*req
;
602 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
605 GetReq(DRI2WaitMSC
, req
);
606 req
->reqType
= info
->codes
->major_opcode
;
607 req
->dri2ReqType
= X_DRI2WaitMSC
;
608 req
->drawable
= drawable
;
609 load_msc_req(req
, target_msc
, divisor
, remainder
);
611 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
617 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
618 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
619 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
630 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
632 req
->target_sbc_hi
= target
>> 32;
633 req
->target_sbc_lo
= target
& 0xffffffff;
636 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
637 CARD64
*msc
, CARD64
*sbc
)
639 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
640 xDRI2WaitSBCReq
*req
;
643 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
646 GetReq(DRI2WaitSBC
, req
);
647 req
->reqType
= info
->codes
->major_opcode
;
648 req
->dri2ReqType
= X_DRI2WaitSBC
;
649 req
->drawable
= drawable
;
650 load_sbc_req(req
, target_sbc
);
652 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
658 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
659 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
660 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
669 #ifdef X_DRI2SwapInterval
670 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
672 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
673 xDRI2SwapIntervalReq
*req
;
675 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
678 GetReq(DRI2SwapInterval
, req
);
679 req
->reqType
= info
->codes
->major_opcode
;
680 req
->dri2ReqType
= X_DRI2SwapInterval
;
681 req
->drawable
= drawable
;
682 req
->interval
= interval
;
688 #endif /* GLX_DIRECT_RENDERING */