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
;
131 /* client doesn't support server event */
138 /* We don't actually support this. It doesn't make sense for clients to
139 * send each other DRI2 events.
142 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
144 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
146 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
148 switch (event
->type
) {
150 /* client doesn't support server event */
158 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
160 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
162 if (XextHasExtension(info
)) {
163 *eventBase
= info
->codes
->first_event
;
164 *errorBase
= info
->codes
->first_error
;
172 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
174 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
175 xDRI2QueryVersionReply rep
;
176 xDRI2QueryVersionReq
*req
;
178 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
181 GetReq(DRI2QueryVersion
, req
);
182 req
->reqType
= info
->codes
->major_opcode
;
183 req
->dri2ReqType
= X_DRI2QueryVersion
;
184 req
->majorVersion
= DRI2_MAJOR
;
185 req
->minorVersion
= DRI2_MINOR
;
186 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
191 *major
= rep
.majorVersion
;
192 *minor
= rep
.minorVersion
;
200 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
202 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
203 xDRI2ConnectReply rep
;
204 xDRI2ConnectReq
*req
;
206 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
209 GetReq(DRI2Connect
, req
);
210 req
->reqType
= info
->codes
->major_opcode
;
211 req
->dri2ReqType
= X_DRI2Connect
;
212 req
->window
= window
;
213 req
->driverType
= DRI2DriverDRI
;
214 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
220 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
226 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
227 if (*driverName
== NULL
) {
229 ((rep
.driverNameLength
+ 3) & ~3) +
230 ((rep
.deviceNameLength
+ 3) & ~3));
235 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
236 (*driverName
)[rep
.driverNameLength
] = '\0';
238 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
239 if (*deviceName
== NULL
) {
241 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
246 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
247 (*deviceName
)[rep
.deviceNameLength
] = '\0';
256 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
258 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
259 xDRI2AuthenticateReq
*req
;
260 xDRI2AuthenticateReply rep
;
262 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
265 GetReq(DRI2Authenticate
, req
);
266 req
->reqType
= info
->codes
->major_opcode
;
267 req
->dri2ReqType
= X_DRI2Authenticate
;
268 req
->window
= window
;
271 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
280 return rep
.authenticated
;
284 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
286 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
287 xDRI2CreateDrawableReq
*req
;
289 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
292 GetReq(DRI2CreateDrawable
, req
);
293 req
->reqType
= info
->codes
->major_opcode
;
294 req
->dri2ReqType
= X_DRI2CreateDrawable
;
295 req
->drawable
= drawable
;
301 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
303 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
304 xDRI2DestroyDrawableReq
*req
;
306 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
311 GetReq(DRI2DestroyDrawable
, req
);
312 req
->reqType
= info
->codes
->major_opcode
;
313 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
314 req
->drawable
= drawable
;
320 DRI2GetBuffers(Display
* dpy
, XID drawable
,
321 int *width
, int *height
,
322 unsigned int *attachments
, int count
, int *outCount
)
324 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
325 xDRI2GetBuffersReply rep
;
326 xDRI2GetBuffersReq
*req
;
328 xDRI2Buffer repBuffer
;
332 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
335 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
336 req
->reqType
= info
->codes
->major_opcode
;
337 req
->dri2ReqType
= X_DRI2GetBuffers
;
338 req
->drawable
= drawable
;
340 p
= (CARD32
*) & req
[1];
341 for (i
= 0; i
< count
; i
++)
342 p
[i
] = attachments
[i
];
344 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
351 *height
= rep
.height
;
352 *outCount
= rep
.count
;
354 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
355 if (buffers
== NULL
) {
356 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
362 for (i
= 0; i
< rep
.count
; i
++) {
363 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
364 buffers
[i
].attachment
= repBuffer
.attachment
;
365 buffers
[i
].name
= repBuffer
.name
;
366 buffers
[i
].pitch
= repBuffer
.pitch
;
367 buffers
[i
].cpp
= repBuffer
.cpp
;
368 buffers
[i
].flags
= repBuffer
.flags
;
379 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
380 int *width
, int *height
,
381 unsigned int *attachments
, int count
, int *outCount
)
383 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
384 xDRI2GetBuffersReply rep
;
385 xDRI2GetBuffersReq
*req
;
387 xDRI2Buffer repBuffer
;
391 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
394 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
395 req
->reqType
= info
->codes
->major_opcode
;
396 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
397 req
->drawable
= drawable
;
399 p
= (CARD32
*) & req
[1];
400 for (i
= 0; i
< (count
* 2); i
++)
401 p
[i
] = attachments
[i
];
403 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
410 *height
= rep
.height
;
411 *outCount
= rep
.count
;
413 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
414 if (buffers
== NULL
) {
415 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
421 for (i
= 0; i
< rep
.count
; i
++) {
422 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
423 buffers
[i
].attachment
= repBuffer
.attachment
;
424 buffers
[i
].name
= repBuffer
.name
;
425 buffers
[i
].pitch
= repBuffer
.pitch
;
426 buffers
[i
].cpp
= repBuffer
.cpp
;
427 buffers
[i
].flags
= repBuffer
.flags
;
438 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
439 CARD32 dest
, CARD32 src
)
441 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
442 xDRI2CopyRegionReq
*req
;
443 xDRI2CopyRegionReply rep
;
445 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
448 GetReq(DRI2CopyRegion
, req
);
449 req
->reqType
= info
->codes
->major_opcode
;
450 req
->dri2ReqType
= X_DRI2CopyRegion
;
451 req
->drawable
= drawable
;
452 req
->region
= region
;
456 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
462 #ifdef X_DRI2SwapBuffers
464 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
467 req
->target_msc_hi
= target
>> 32;
468 req
->target_msc_lo
= target
& 0xffffffff;
469 req
->divisor_hi
= divisor
>> 32;
470 req
->divisor_lo
= divisor
& 0xffffffff;
471 req
->remainder_hi
= remainder
>> 32;
472 req
->remainder_lo
= remainder
& 0xffffffff;
476 vals_to_card64(CARD32 lo
, CARD32 hi
)
478 return (CARD64
)hi
<< 32 | lo
;
481 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
482 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
484 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
485 xDRI2SwapBuffersReq
*req
;
486 xDRI2SwapBuffersReply rep
;
488 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
491 GetReq(DRI2SwapBuffers
, req
);
492 req
->reqType
= info
->codes
->major_opcode
;
493 req
->dri2ReqType
= X_DRI2SwapBuffers
;
494 req
->drawable
= drawable
;
495 load_swap_req(req
, target_msc
, divisor
, remainder
);
497 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
499 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
507 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
510 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
514 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
517 GetReq(DRI2GetMSC
, req
);
518 req
->reqType
= info
->codes
->major_opcode
;
519 req
->dri2ReqType
= X_DRI2GetMSC
;
520 req
->drawable
= drawable
;
522 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
528 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
529 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
530 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
541 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
544 req
->target_msc_hi
= target
>> 32;
545 req
->target_msc_lo
= target
& 0xffffffff;
546 req
->divisor_hi
= divisor
>> 32;
547 req
->divisor_lo
= divisor
& 0xffffffff;
548 req
->remainder_hi
= remainder
>> 32;
549 req
->remainder_lo
= remainder
& 0xffffffff;
552 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
553 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
555 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
556 xDRI2WaitMSCReq
*req
;
559 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
562 GetReq(DRI2WaitMSC
, req
);
563 req
->reqType
= info
->codes
->major_opcode
;
564 req
->dri2ReqType
= X_DRI2WaitMSC
;
565 req
->drawable
= drawable
;
566 load_msc_req(req
, target_msc
, divisor
, remainder
);
568 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
574 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
575 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
576 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
587 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
589 req
->target_sbc_hi
= target
>> 32;
590 req
->target_sbc_lo
= target
& 0xffffffff;
593 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
594 CARD64
*msc
, CARD64
*sbc
)
596 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
597 xDRI2WaitSBCReq
*req
;
600 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
603 GetReq(DRI2WaitSBC
, req
);
604 req
->reqType
= info
->codes
->major_opcode
;
605 req
->dri2ReqType
= X_DRI2WaitSBC
;
606 req
->drawable
= drawable
;
607 load_sbc_req(req
, target_sbc
);
609 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
615 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
616 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
617 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
626 #ifdef X_DRI2SwapInterval
627 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
629 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
630 xDRI2SwapIntervalReq
*req
;
632 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
635 GetReq(DRI2SwapInterval
, req
);
636 req
->reqType
= info
->codes
->major_opcode
;
637 req
->dri2ReqType
= X_DRI2SwapInterval
;
638 req
->drawable
= drawable
;
639 req
->interval
= interval
;
645 #endif /* GLX_DIRECT_RENDERING */