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
);
91 XExtDisplayInfo
*glx_info
= __glXFindDisplay(dpy
);
93 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
95 switch ((wire
->u
.u
.type
& 0x7f) - info
->codes
->first_event
) {
97 #ifdef X_DRI2SwapBuffers
98 case DRI2_BufferSwapComplete
:
100 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
101 xDRI2BufferSwapComplete
*awire
= (xDRI2BufferSwapComplete
*)wire
;
102 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
103 aevent
->type
= glx_info
->codes
->first_event
+ GLX_BufferSwapComplete
;
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_COPY_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 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
166 if (err
->majorCode
== codes
->major_opcode
&&
167 err
->errorCode
== BadDrawable
&&
168 err
->minorCode
== X_DRI2CopyRegion
)
175 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
177 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
179 if (XextHasExtension(info
)) {
180 *eventBase
= info
->codes
->first_event
;
181 *errorBase
= info
->codes
->first_error
;
189 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
191 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
192 xDRI2QueryVersionReply rep
;
193 xDRI2QueryVersionReq
*req
;
196 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
199 GetReq(DRI2QueryVersion
, req
);
200 req
->reqType
= info
->codes
->major_opcode
;
201 req
->dri2ReqType
= X_DRI2QueryVersion
;
202 req
->majorVersion
= DRI2_MAJOR
;
203 req
->minorVersion
= DRI2_MINOR
;
204 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
209 *major
= rep
.majorVersion
;
210 *minor
= rep
.minorVersion
;
214 switch (rep
.minorVersion
) {
227 for (i
= 0; i
< nevents
; i
++) {
228 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
229 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
236 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
238 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
239 xDRI2ConnectReply rep
;
240 xDRI2ConnectReq
*req
;
242 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
245 GetReq(DRI2Connect
, req
);
246 req
->reqType
= info
->codes
->major_opcode
;
247 req
->dri2ReqType
= X_DRI2Connect
;
248 req
->window
= window
;
249 req
->driverType
= DRI2DriverDRI
;
250 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
256 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
262 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
263 if (*driverName
== NULL
) {
265 ((rep
.driverNameLength
+ 3) & ~3) +
266 ((rep
.deviceNameLength
+ 3) & ~3));
271 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
272 (*driverName
)[rep
.driverNameLength
] = '\0';
274 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
275 if (*deviceName
== NULL
) {
277 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
282 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
283 (*deviceName
)[rep
.deviceNameLength
] = '\0';
292 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
294 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
295 xDRI2AuthenticateReq
*req
;
296 xDRI2AuthenticateReply rep
;
298 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
301 GetReq(DRI2Authenticate
, req
);
302 req
->reqType
= info
->codes
->major_opcode
;
303 req
->dri2ReqType
= X_DRI2Authenticate
;
304 req
->window
= window
;
307 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
316 return rep
.authenticated
;
320 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
322 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
323 xDRI2CreateDrawableReq
*req
;
325 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
328 GetReq(DRI2CreateDrawable
, req
);
329 req
->reqType
= info
->codes
->major_opcode
;
330 req
->dri2ReqType
= X_DRI2CreateDrawable
;
331 req
->drawable
= drawable
;
337 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
339 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
340 xDRI2DestroyDrawableReq
*req
;
342 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
347 GetReq(DRI2DestroyDrawable
, req
);
348 req
->reqType
= info
->codes
->major_opcode
;
349 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
350 req
->drawable
= drawable
;
356 DRI2GetBuffers(Display
* dpy
, XID drawable
,
357 int *width
, int *height
,
358 unsigned int *attachments
, int count
, int *outCount
)
360 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
361 xDRI2GetBuffersReply rep
;
362 xDRI2GetBuffersReq
*req
;
364 xDRI2Buffer repBuffer
;
368 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
371 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
372 req
->reqType
= info
->codes
->major_opcode
;
373 req
->dri2ReqType
= X_DRI2GetBuffers
;
374 req
->drawable
= drawable
;
376 p
= (CARD32
*) & req
[1];
377 for (i
= 0; i
< count
; i
++)
378 p
[i
] = attachments
[i
];
380 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
387 *height
= rep
.height
;
388 *outCount
= rep
.count
;
390 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
391 if (buffers
== NULL
) {
392 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
398 for (i
= 0; i
< rep
.count
; i
++) {
399 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
400 buffers
[i
].attachment
= repBuffer
.attachment
;
401 buffers
[i
].name
= repBuffer
.name
;
402 buffers
[i
].pitch
= repBuffer
.pitch
;
403 buffers
[i
].cpp
= repBuffer
.cpp
;
404 buffers
[i
].flags
= repBuffer
.flags
;
415 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
416 int *width
, int *height
,
417 unsigned int *attachments
, int count
, int *outCount
)
419 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
420 xDRI2GetBuffersReply rep
;
421 xDRI2GetBuffersReq
*req
;
423 xDRI2Buffer repBuffer
;
427 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
430 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
431 req
->reqType
= info
->codes
->major_opcode
;
432 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
433 req
->drawable
= drawable
;
435 p
= (CARD32
*) & req
[1];
436 for (i
= 0; i
< (count
* 2); i
++)
437 p
[i
] = attachments
[i
];
439 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
446 *height
= rep
.height
;
447 *outCount
= rep
.count
;
449 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
450 if (buffers
== NULL
) {
451 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
457 for (i
= 0; i
< rep
.count
; i
++) {
458 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
459 buffers
[i
].attachment
= repBuffer
.attachment
;
460 buffers
[i
].name
= repBuffer
.name
;
461 buffers
[i
].pitch
= repBuffer
.pitch
;
462 buffers
[i
].cpp
= repBuffer
.cpp
;
463 buffers
[i
].flags
= repBuffer
.flags
;
474 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
475 CARD32 dest
, CARD32 src
)
477 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
478 xDRI2CopyRegionReq
*req
;
479 xDRI2CopyRegionReply rep
;
481 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
484 GetReq(DRI2CopyRegion
, req
);
485 req
->reqType
= info
->codes
->major_opcode
;
486 req
->dri2ReqType
= X_DRI2CopyRegion
;
487 req
->drawable
= drawable
;
488 req
->region
= region
;
492 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
498 #ifdef X_DRI2SwapBuffers
500 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
503 req
->target_msc_hi
= target
>> 32;
504 req
->target_msc_lo
= target
& 0xffffffff;
505 req
->divisor_hi
= divisor
>> 32;
506 req
->divisor_lo
= divisor
& 0xffffffff;
507 req
->remainder_hi
= remainder
>> 32;
508 req
->remainder_lo
= remainder
& 0xffffffff;
512 vals_to_card64(CARD32 lo
, CARD32 hi
)
514 return (CARD64
)hi
<< 32 | lo
;
517 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
518 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
520 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
521 xDRI2SwapBuffersReq
*req
;
522 xDRI2SwapBuffersReply rep
;
524 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
527 GetReq(DRI2SwapBuffers
, req
);
528 req
->reqType
= info
->codes
->major_opcode
;
529 req
->dri2ReqType
= X_DRI2SwapBuffers
;
530 req
->drawable
= drawable
;
531 load_swap_req(req
, target_msc
, divisor
, remainder
);
533 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
535 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
543 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
546 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
550 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
553 GetReq(DRI2GetMSC
, req
);
554 req
->reqType
= info
->codes
->major_opcode
;
555 req
->dri2ReqType
= X_DRI2GetMSC
;
556 req
->drawable
= drawable
;
558 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
564 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
565 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
566 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
577 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
580 req
->target_msc_hi
= target
>> 32;
581 req
->target_msc_lo
= target
& 0xffffffff;
582 req
->divisor_hi
= divisor
>> 32;
583 req
->divisor_lo
= divisor
& 0xffffffff;
584 req
->remainder_hi
= remainder
>> 32;
585 req
->remainder_lo
= remainder
& 0xffffffff;
588 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
589 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
591 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
592 xDRI2WaitMSCReq
*req
;
595 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
598 GetReq(DRI2WaitMSC
, req
);
599 req
->reqType
= info
->codes
->major_opcode
;
600 req
->dri2ReqType
= X_DRI2WaitMSC
;
601 req
->drawable
= drawable
;
602 load_msc_req(req
, target_msc
, divisor
, remainder
);
604 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
610 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
611 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
612 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
623 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
625 req
->target_sbc_hi
= target
>> 32;
626 req
->target_sbc_lo
= target
& 0xffffffff;
629 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
630 CARD64
*msc
, CARD64
*sbc
)
632 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
633 xDRI2WaitSBCReq
*req
;
636 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
639 GetReq(DRI2WaitSBC
, req
);
640 req
->reqType
= info
->codes
->major_opcode
;
641 req
->dri2ReqType
= X_DRI2WaitSBC
;
642 req
->drawable
= drawable
;
643 load_sbc_req(req
, target_sbc
);
645 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
651 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
652 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
653 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
662 #ifdef X_DRI2SwapInterval
663 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
665 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
666 xDRI2SwapIntervalReq
*req
;
668 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
671 GetReq(DRI2SwapInterval
, req
);
672 req
->reqType
= info
->codes
->major_opcode
;
673 req
->dri2ReqType
= X_DRI2SwapInterval
;
674 req
->drawable
= drawable
;
675 req
->interval
= interval
;
681 #endif /* GLX_DIRECT_RENDERING */