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
);
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 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
103 (glx_info
->codes
->first_event
+ GLX_BufferSwapComplete
) & 0x75;
104 aevent
->send_event
= (awire
->type
& 0x80) != 0;
105 aevent
->display
= dpy
;
106 aevent
->drawable
= awire
->drawable
;
107 switch (awire
->event_type
) {
108 case DRI2_EXCHANGE_COMPLETE
:
109 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
111 case DRI2_BLIT_COMPLETE
:
112 aevent
->event_type
= GLX_BLIT_COMPLETE_INTEL
;
114 case DRI2_FLIP_COMPLETE
:
115 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
118 /* unknown swap completion type */
121 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
122 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
123 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
127 #ifdef DRI2_InvalidateBuffers
128 case DRI2_InvalidateBuffers
:
130 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
132 dri2InvalidateBuffers(dpy
, awire
->drawable
);
137 /* client doesn't support server event */
144 /* We don't actually support this. It doesn't make sense for clients to
145 * send each other DRI2 events.
148 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
150 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
152 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
154 switch (event
->type
) {
156 /* client doesn't support server event */
164 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
166 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
168 if (XextHasExtension(info
)) {
169 *eventBase
= info
->codes
->first_event
;
170 *errorBase
= info
->codes
->first_error
;
178 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
180 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
181 xDRI2QueryVersionReply rep
;
182 xDRI2QueryVersionReq
*req
;
185 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
188 GetReq(DRI2QueryVersion
, req
);
189 req
->reqType
= info
->codes
->major_opcode
;
190 req
->dri2ReqType
= X_DRI2QueryVersion
;
191 req
->majorVersion
= DRI2_MAJOR
;
192 req
->minorVersion
= DRI2_MINOR
;
193 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
198 *major
= rep
.majorVersion
;
199 *minor
= rep
.minorVersion
;
203 switch (rep
.minorVersion
) {
216 for (i
= 0; i
< nevents
; i
++) {
217 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
218 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
225 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
227 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
228 xDRI2ConnectReply rep
;
229 xDRI2ConnectReq
*req
;
231 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
234 GetReq(DRI2Connect
, req
);
235 req
->reqType
= info
->codes
->major_opcode
;
236 req
->dri2ReqType
= X_DRI2Connect
;
237 req
->window
= window
;
238 req
->driverType
= DRI2DriverDRI
;
239 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
245 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
251 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
252 if (*driverName
== NULL
) {
254 ((rep
.driverNameLength
+ 3) & ~3) +
255 ((rep
.deviceNameLength
+ 3) & ~3));
260 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
261 (*driverName
)[rep
.driverNameLength
] = '\0';
263 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
264 if (*deviceName
== NULL
) {
266 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
271 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
272 (*deviceName
)[rep
.deviceNameLength
] = '\0';
281 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
283 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
284 xDRI2AuthenticateReq
*req
;
285 xDRI2AuthenticateReply rep
;
287 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
290 GetReq(DRI2Authenticate
, req
);
291 req
->reqType
= info
->codes
->major_opcode
;
292 req
->dri2ReqType
= X_DRI2Authenticate
;
293 req
->window
= window
;
296 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
305 return rep
.authenticated
;
309 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
311 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
312 xDRI2CreateDrawableReq
*req
;
314 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
317 GetReq(DRI2CreateDrawable
, req
);
318 req
->reqType
= info
->codes
->major_opcode
;
319 req
->dri2ReqType
= X_DRI2CreateDrawable
;
320 req
->drawable
= drawable
;
326 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
328 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
329 xDRI2DestroyDrawableReq
*req
;
331 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
336 GetReq(DRI2DestroyDrawable
, req
);
337 req
->reqType
= info
->codes
->major_opcode
;
338 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
339 req
->drawable
= drawable
;
345 DRI2GetBuffers(Display
* dpy
, XID drawable
,
346 int *width
, int *height
,
347 unsigned int *attachments
, int count
, int *outCount
)
349 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
350 xDRI2GetBuffersReply rep
;
351 xDRI2GetBuffersReq
*req
;
353 xDRI2Buffer repBuffer
;
357 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
360 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
361 req
->reqType
= info
->codes
->major_opcode
;
362 req
->dri2ReqType
= X_DRI2GetBuffers
;
363 req
->drawable
= drawable
;
365 p
= (CARD32
*) & req
[1];
366 for (i
= 0; i
< count
; i
++)
367 p
[i
] = attachments
[i
];
369 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
376 *height
= rep
.height
;
377 *outCount
= rep
.count
;
379 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
380 if (buffers
== NULL
) {
381 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
387 for (i
= 0; i
< rep
.count
; i
++) {
388 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
389 buffers
[i
].attachment
= repBuffer
.attachment
;
390 buffers
[i
].name
= repBuffer
.name
;
391 buffers
[i
].pitch
= repBuffer
.pitch
;
392 buffers
[i
].cpp
= repBuffer
.cpp
;
393 buffers
[i
].flags
= repBuffer
.flags
;
404 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
405 int *width
, int *height
,
406 unsigned int *attachments
, int count
, int *outCount
)
408 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
409 xDRI2GetBuffersReply rep
;
410 xDRI2GetBuffersReq
*req
;
412 xDRI2Buffer repBuffer
;
416 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
419 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
420 req
->reqType
= info
->codes
->major_opcode
;
421 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
422 req
->drawable
= drawable
;
424 p
= (CARD32
*) & req
[1];
425 for (i
= 0; i
< (count
* 2); i
++)
426 p
[i
] = attachments
[i
];
428 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
435 *height
= rep
.height
;
436 *outCount
= rep
.count
;
438 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
439 if (buffers
== NULL
) {
440 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
446 for (i
= 0; i
< rep
.count
; i
++) {
447 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
448 buffers
[i
].attachment
= repBuffer
.attachment
;
449 buffers
[i
].name
= repBuffer
.name
;
450 buffers
[i
].pitch
= repBuffer
.pitch
;
451 buffers
[i
].cpp
= repBuffer
.cpp
;
452 buffers
[i
].flags
= repBuffer
.flags
;
463 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
464 CARD32 dest
, CARD32 src
)
466 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
467 xDRI2CopyRegionReq
*req
;
468 xDRI2CopyRegionReply rep
;
470 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
473 GetReq(DRI2CopyRegion
, req
);
474 req
->reqType
= info
->codes
->major_opcode
;
475 req
->dri2ReqType
= X_DRI2CopyRegion
;
476 req
->drawable
= drawable
;
477 req
->region
= region
;
481 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
487 #ifdef X_DRI2SwapBuffers
489 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
492 req
->target_msc_hi
= target
>> 32;
493 req
->target_msc_lo
= target
& 0xffffffff;
494 req
->divisor_hi
= divisor
>> 32;
495 req
->divisor_lo
= divisor
& 0xffffffff;
496 req
->remainder_hi
= remainder
>> 32;
497 req
->remainder_lo
= remainder
& 0xffffffff;
501 vals_to_card64(CARD32 lo
, CARD32 hi
)
503 return (CARD64
)hi
<< 32 | lo
;
506 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
507 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
509 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
510 xDRI2SwapBuffersReq
*req
;
511 xDRI2SwapBuffersReply rep
;
513 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
516 GetReq(DRI2SwapBuffers
, req
);
517 req
->reqType
= info
->codes
->major_opcode
;
518 req
->dri2ReqType
= X_DRI2SwapBuffers
;
519 req
->drawable
= drawable
;
520 load_swap_req(req
, target_msc
, divisor
, remainder
);
522 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
524 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
532 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
535 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
539 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
542 GetReq(DRI2GetMSC
, req
);
543 req
->reqType
= info
->codes
->major_opcode
;
544 req
->dri2ReqType
= X_DRI2GetMSC
;
545 req
->drawable
= drawable
;
547 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
553 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
554 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
555 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
566 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
569 req
->target_msc_hi
= target
>> 32;
570 req
->target_msc_lo
= target
& 0xffffffff;
571 req
->divisor_hi
= divisor
>> 32;
572 req
->divisor_lo
= divisor
& 0xffffffff;
573 req
->remainder_hi
= remainder
>> 32;
574 req
->remainder_lo
= remainder
& 0xffffffff;
577 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
578 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
580 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
581 xDRI2WaitMSCReq
*req
;
584 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
587 GetReq(DRI2WaitMSC
, req
);
588 req
->reqType
= info
->codes
->major_opcode
;
589 req
->dri2ReqType
= X_DRI2WaitMSC
;
590 req
->drawable
= drawable
;
591 load_msc_req(req
, target_msc
, divisor
, remainder
);
593 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
599 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
600 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
601 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
612 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
614 req
->target_sbc_hi
= target
>> 32;
615 req
->target_sbc_lo
= target
& 0xffffffff;
618 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
619 CARD64
*msc
, CARD64
*sbc
)
621 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
622 xDRI2WaitSBCReq
*req
;
625 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
628 GetReq(DRI2WaitSBC
, req
);
629 req
->reqType
= info
->codes
->major_opcode
;
630 req
->dri2ReqType
= X_DRI2WaitSBC
;
631 req
->drawable
= drawable
;
632 load_sbc_req(req
, target_sbc
);
634 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
640 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
641 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
642 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
651 #ifdef X_DRI2SwapInterval
652 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
654 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
655 xDRI2SwapIntervalReq
*req
;
657 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
660 GetReq(DRI2SwapInterval
, req
);
661 req
->reqType
= info
->codes
->major_opcode
;
662 req
->dri2ReqType
= X_DRI2SwapInterval
;
663 req
->drawable
= drawable
;
664 req
->interval
= interval
;
670 #endif /* GLX_DIRECT_RENDERING */