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 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
);
68 static /* const */ XExtensionHooks dri2ExtensionHooks
= {
73 NULL
, /* create_font */
75 DRI2CloseDisplay
, /* close_display */
76 DRI2WireToEvent
, /* wire_to_event */
77 DRI2EventToWire
, /* event_to_wire */
78 DRI2Error
, /* error */
79 NULL
, /* error_string */
82 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay
,
89 DRI2WireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
91 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
92 XExtDisplayInfo
*glx_info
= __glXFindDisplay(dpy
);
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
);
104 aevent
->type
= glx_info
->codes
->first_event
+ GLX_BufferSwapComplete
;
105 aevent
->send_event
= (awire
->type
& 0x80) != 0;
106 aevent
->display
= dpy
;
107 aevent
->drawable
= awire
->drawable
;
108 switch (awire
->event_type
) {
109 case DRI2_EXCHANGE_COMPLETE
:
110 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
112 case DRI2_BLIT_COMPLETE
:
113 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
115 case DRI2_FLIP_COMPLETE
:
116 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
119 /* unknown swap completion type */
122 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
123 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
124 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
128 #ifdef DRI2_InvalidateBuffers
129 case DRI2_InvalidateBuffers
:
131 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
133 dri2InvalidateBuffers(dpy
, awire
->drawable
);
138 /* client doesn't support server event */
145 /* We don't actually support this. It doesn't make sense for clients to
146 * send each other DRI2 events.
149 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
151 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
153 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
155 switch (event
->type
) {
157 /* client doesn't support server event */
165 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
167 if (err
->majorCode
== codes
->major_opcode
&&
168 err
->errorCode
== BadDrawable
&&
169 err
->minorCode
== X_DRI2CopyRegion
)
176 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
178 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
180 if (XextHasExtension(info
)) {
181 *eventBase
= info
->codes
->first_event
;
182 *errorBase
= info
->codes
->first_error
;
190 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
192 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
193 xDRI2QueryVersionReply rep
;
194 xDRI2QueryVersionReq
*req
;
197 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
200 GetReq(DRI2QueryVersion
, req
);
201 req
->reqType
= info
->codes
->major_opcode
;
202 req
->dri2ReqType
= X_DRI2QueryVersion
;
203 req
->majorVersion
= DRI2_MAJOR
;
204 req
->minorVersion
= DRI2_MINOR
;
205 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
210 *major
= rep
.majorVersion
;
211 *minor
= rep
.minorVersion
;
215 switch (rep
.minorVersion
) {
228 for (i
= 0; i
< nevents
; i
++) {
229 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
230 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
237 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
239 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
240 xDRI2ConnectReply rep
;
241 xDRI2ConnectReq
*req
;
243 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
246 GetReq(DRI2Connect
, req
);
247 req
->reqType
= info
->codes
->major_opcode
;
248 req
->dri2ReqType
= X_DRI2Connect
;
249 req
->window
= window
;
250 req
->driverType
= DRI2DriverDRI
;
251 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
257 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
263 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
264 if (*driverName
== NULL
) {
266 ((rep
.driverNameLength
+ 3) & ~3) +
267 ((rep
.deviceNameLength
+ 3) & ~3));
272 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
273 (*driverName
)[rep
.driverNameLength
] = '\0';
275 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
276 if (*deviceName
== NULL
) {
278 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
283 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
284 (*deviceName
)[rep
.deviceNameLength
] = '\0';
293 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
295 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
296 xDRI2AuthenticateReq
*req
;
297 xDRI2AuthenticateReply rep
;
299 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
302 GetReq(DRI2Authenticate
, req
);
303 req
->reqType
= info
->codes
->major_opcode
;
304 req
->dri2ReqType
= X_DRI2Authenticate
;
305 req
->window
= window
;
308 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
317 return rep
.authenticated
;
321 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
323 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
324 xDRI2CreateDrawableReq
*req
;
326 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
329 GetReq(DRI2CreateDrawable
, req
);
330 req
->reqType
= info
->codes
->major_opcode
;
331 req
->dri2ReqType
= X_DRI2CreateDrawable
;
332 req
->drawable
= drawable
;
338 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
340 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
341 xDRI2DestroyDrawableReq
*req
;
343 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
348 GetReq(DRI2DestroyDrawable
, req
);
349 req
->reqType
= info
->codes
->major_opcode
;
350 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
351 req
->drawable
= drawable
;
357 DRI2GetBuffers(Display
* dpy
, XID drawable
,
358 int *width
, int *height
,
359 unsigned int *attachments
, int count
, int *outCount
)
361 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
362 xDRI2GetBuffersReply rep
;
363 xDRI2GetBuffersReq
*req
;
365 xDRI2Buffer repBuffer
;
369 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
372 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
373 req
->reqType
= info
->codes
->major_opcode
;
374 req
->dri2ReqType
= X_DRI2GetBuffers
;
375 req
->drawable
= drawable
;
377 p
= (CARD32
*) & req
[1];
378 for (i
= 0; i
< count
; i
++)
379 p
[i
] = attachments
[i
];
381 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
388 *height
= rep
.height
;
389 *outCount
= rep
.count
;
391 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
392 if (buffers
== NULL
) {
393 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
399 for (i
= 0; i
< rep
.count
; i
++) {
400 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
401 buffers
[i
].attachment
= repBuffer
.attachment
;
402 buffers
[i
].name
= repBuffer
.name
;
403 buffers
[i
].pitch
= repBuffer
.pitch
;
404 buffers
[i
].cpp
= repBuffer
.cpp
;
405 buffers
[i
].flags
= repBuffer
.flags
;
416 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
417 int *width
, int *height
,
418 unsigned int *attachments
, int count
, int *outCount
)
420 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
421 xDRI2GetBuffersReply rep
;
422 xDRI2GetBuffersReq
*req
;
424 xDRI2Buffer repBuffer
;
428 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
431 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
432 req
->reqType
= info
->codes
->major_opcode
;
433 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
434 req
->drawable
= drawable
;
436 p
= (CARD32
*) & req
[1];
437 for (i
= 0; i
< (count
* 2); i
++)
438 p
[i
] = attachments
[i
];
440 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
447 *height
= rep
.height
;
448 *outCount
= rep
.count
;
450 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
451 if (buffers
== NULL
) {
452 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
458 for (i
= 0; i
< rep
.count
; i
++) {
459 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
460 buffers
[i
].attachment
= repBuffer
.attachment
;
461 buffers
[i
].name
= repBuffer
.name
;
462 buffers
[i
].pitch
= repBuffer
.pitch
;
463 buffers
[i
].cpp
= repBuffer
.cpp
;
464 buffers
[i
].flags
= repBuffer
.flags
;
475 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
476 CARD32 dest
, CARD32 src
)
478 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
479 xDRI2CopyRegionReq
*req
;
480 xDRI2CopyRegionReply rep
;
482 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
485 GetReq(DRI2CopyRegion
, req
);
486 req
->reqType
= info
->codes
->major_opcode
;
487 req
->dri2ReqType
= X_DRI2CopyRegion
;
488 req
->drawable
= drawable
;
489 req
->region
= region
;
493 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
499 #ifdef X_DRI2SwapBuffers
501 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
504 req
->target_msc_hi
= target
>> 32;
505 req
->target_msc_lo
= target
& 0xffffffff;
506 req
->divisor_hi
= divisor
>> 32;
507 req
->divisor_lo
= divisor
& 0xffffffff;
508 req
->remainder_hi
= remainder
>> 32;
509 req
->remainder_lo
= remainder
& 0xffffffff;
513 vals_to_card64(CARD32 lo
, CARD32 hi
)
515 return (CARD64
)hi
<< 32 | lo
;
518 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
519 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
521 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
522 xDRI2SwapBuffersReq
*req
;
523 xDRI2SwapBuffersReply rep
;
525 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
528 GetReq(DRI2SwapBuffers
, req
);
529 req
->reqType
= info
->codes
->major_opcode
;
530 req
->dri2ReqType
= X_DRI2SwapBuffers
;
531 req
->drawable
= drawable
;
532 load_swap_req(req
, target_msc
, divisor
, remainder
);
534 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
536 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
544 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
547 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
551 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
554 GetReq(DRI2GetMSC
, req
);
555 req
->reqType
= info
->codes
->major_opcode
;
556 req
->dri2ReqType
= X_DRI2GetMSC
;
557 req
->drawable
= drawable
;
559 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
565 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
566 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
567 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
578 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
581 req
->target_msc_hi
= target
>> 32;
582 req
->target_msc_lo
= target
& 0xffffffff;
583 req
->divisor_hi
= divisor
>> 32;
584 req
->divisor_lo
= divisor
& 0xffffffff;
585 req
->remainder_hi
= remainder
>> 32;
586 req
->remainder_lo
= remainder
& 0xffffffff;
589 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
590 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
592 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
593 xDRI2WaitMSCReq
*req
;
596 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
599 GetReq(DRI2WaitMSC
, req
);
600 req
->reqType
= info
->codes
->major_opcode
;
601 req
->dri2ReqType
= X_DRI2WaitMSC
;
602 req
->drawable
= drawable
;
603 load_msc_req(req
, target_msc
, divisor
, remainder
);
605 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
611 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
612 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
613 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
624 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
626 req
->target_sbc_hi
= target
>> 32;
627 req
->target_sbc_lo
= target
& 0xffffffff;
630 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
631 CARD64
*msc
, CARD64
*sbc
)
633 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
634 xDRI2WaitSBCReq
*req
;
637 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
640 GetReq(DRI2WaitSBC
, req
);
641 req
->reqType
= info
->codes
->major_opcode
;
642 req
->dri2ReqType
= X_DRI2WaitSBC
;
643 req
->drawable
= drawable
;
644 load_sbc_req(req
, target_sbc
);
646 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
652 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
653 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
654 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
663 #ifdef X_DRI2SwapInterval
664 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
666 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
667 xDRI2SwapIntervalReq
*req
;
669 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
672 GetReq(DRI2SwapInterval
, req
);
673 req
->reqType
= info
->codes
->major_opcode
;
674 req
->dri2ReqType
= X_DRI2SwapInterval
;
675 req
->drawable
= drawable
;
676 req
->interval
= interval
;
682 #endif /* GLX_DIRECT_RENDERING */