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
);
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_COPY_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
;
129 #ifdef DRI2_InvalidateBuffers
130 case DRI2_InvalidateBuffers
:
132 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
134 dri2InvalidateBuffers(dpy
, awire
->drawable
);
139 /* client doesn't support server event */
146 /* We don't actually support this. It doesn't make sense for clients to
147 * send each other DRI2 events.
150 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
152 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
154 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
156 switch (event
->type
) {
158 /* client doesn't support server event */
166 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
168 if (err
->majorCode
== codes
->major_opcode
&&
169 err
->errorCode
== BadDrawable
&&
170 err
->minorCode
== X_DRI2CopyRegion
)
177 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
179 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
181 if (XextHasExtension(info
)) {
182 *eventBase
= info
->codes
->first_event
;
183 *errorBase
= info
->codes
->first_error
;
191 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
193 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
194 xDRI2QueryVersionReply rep
;
195 xDRI2QueryVersionReq
*req
;
198 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
201 GetReq(DRI2QueryVersion
, req
);
202 req
->reqType
= info
->codes
->major_opcode
;
203 req
->dri2ReqType
= X_DRI2QueryVersion
;
204 req
->majorVersion
= DRI2_MAJOR
;
205 req
->minorVersion
= DRI2_MINOR
;
206 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
211 *major
= rep
.majorVersion
;
212 *minor
= rep
.minorVersion
;
216 switch (rep
.minorVersion
) {
229 for (i
= 0; i
< nevents
; i
++) {
230 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
231 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
238 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
240 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
241 xDRI2ConnectReply rep
;
242 xDRI2ConnectReq
*req
;
244 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
247 GetReq(DRI2Connect
, req
);
248 req
->reqType
= info
->codes
->major_opcode
;
249 req
->dri2ReqType
= X_DRI2Connect
;
250 req
->window
= window
;
251 req
->driverType
= DRI2DriverDRI
;
252 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
258 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
264 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
265 if (*driverName
== NULL
) {
267 ((rep
.driverNameLength
+ 3) & ~3) +
268 ((rep
.deviceNameLength
+ 3) & ~3));
273 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
274 (*driverName
)[rep
.driverNameLength
] = '\0';
276 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
277 if (*deviceName
== NULL
) {
279 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
284 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
285 (*deviceName
)[rep
.deviceNameLength
] = '\0';
294 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
296 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
297 xDRI2AuthenticateReq
*req
;
298 xDRI2AuthenticateReply rep
;
300 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
303 GetReq(DRI2Authenticate
, req
);
304 req
->reqType
= info
->codes
->major_opcode
;
305 req
->dri2ReqType
= X_DRI2Authenticate
;
306 req
->window
= window
;
309 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
318 return rep
.authenticated
;
322 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
324 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
325 xDRI2CreateDrawableReq
*req
;
327 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
330 GetReq(DRI2CreateDrawable
, req
);
331 req
->reqType
= info
->codes
->major_opcode
;
332 req
->dri2ReqType
= X_DRI2CreateDrawable
;
333 req
->drawable
= drawable
;
339 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
341 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
342 xDRI2DestroyDrawableReq
*req
;
344 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
349 GetReq(DRI2DestroyDrawable
, req
);
350 req
->reqType
= info
->codes
->major_opcode
;
351 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
352 req
->drawable
= drawable
;
358 DRI2GetBuffers(Display
* dpy
, XID drawable
,
359 int *width
, int *height
,
360 unsigned int *attachments
, int count
, int *outCount
)
362 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
363 xDRI2GetBuffersReply rep
;
364 xDRI2GetBuffersReq
*req
;
366 xDRI2Buffer repBuffer
;
370 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
373 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
374 req
->reqType
= info
->codes
->major_opcode
;
375 req
->dri2ReqType
= X_DRI2GetBuffers
;
376 req
->drawable
= drawable
;
378 p
= (CARD32
*) & req
[1];
379 for (i
= 0; i
< count
; i
++)
380 p
[i
] = attachments
[i
];
382 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
389 *height
= rep
.height
;
390 *outCount
= rep
.count
;
392 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
393 if (buffers
== NULL
) {
394 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
400 for (i
= 0; i
< rep
.count
; i
++) {
401 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
402 buffers
[i
].attachment
= repBuffer
.attachment
;
403 buffers
[i
].name
= repBuffer
.name
;
404 buffers
[i
].pitch
= repBuffer
.pitch
;
405 buffers
[i
].cpp
= repBuffer
.cpp
;
406 buffers
[i
].flags
= repBuffer
.flags
;
417 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
418 int *width
, int *height
,
419 unsigned int *attachments
, int count
, int *outCount
)
421 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
422 xDRI2GetBuffersReply rep
;
423 xDRI2GetBuffersReq
*req
;
425 xDRI2Buffer repBuffer
;
429 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
432 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
433 req
->reqType
= info
->codes
->major_opcode
;
434 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
435 req
->drawable
= drawable
;
437 p
= (CARD32
*) & req
[1];
438 for (i
= 0; i
< (count
* 2); i
++)
439 p
[i
] = attachments
[i
];
441 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
448 *height
= rep
.height
;
449 *outCount
= rep
.count
;
451 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
452 if (buffers
== NULL
) {
453 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
459 for (i
= 0; i
< rep
.count
; i
++) {
460 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
461 buffers
[i
].attachment
= repBuffer
.attachment
;
462 buffers
[i
].name
= repBuffer
.name
;
463 buffers
[i
].pitch
= repBuffer
.pitch
;
464 buffers
[i
].cpp
= repBuffer
.cpp
;
465 buffers
[i
].flags
= repBuffer
.flags
;
476 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
477 CARD32 dest
, CARD32 src
)
479 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
480 xDRI2CopyRegionReq
*req
;
481 xDRI2CopyRegionReply rep
;
483 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
486 GetReq(DRI2CopyRegion
, req
);
487 req
->reqType
= info
->codes
->major_opcode
;
488 req
->dri2ReqType
= X_DRI2CopyRegion
;
489 req
->drawable
= drawable
;
490 req
->region
= region
;
494 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
500 #ifdef X_DRI2SwapBuffers
502 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
505 req
->target_msc_hi
= target
>> 32;
506 req
->target_msc_lo
= target
& 0xffffffff;
507 req
->divisor_hi
= divisor
>> 32;
508 req
->divisor_lo
= divisor
& 0xffffffff;
509 req
->remainder_hi
= remainder
>> 32;
510 req
->remainder_lo
= remainder
& 0xffffffff;
514 vals_to_card64(CARD32 lo
, CARD32 hi
)
516 return (CARD64
)hi
<< 32 | lo
;
519 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
520 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
522 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
523 xDRI2SwapBuffersReq
*req
;
524 xDRI2SwapBuffersReply rep
;
526 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
529 GetReq(DRI2SwapBuffers
, req
);
530 req
->reqType
= info
->codes
->major_opcode
;
531 req
->dri2ReqType
= X_DRI2SwapBuffers
;
532 req
->drawable
= drawable
;
533 load_swap_req(req
, target_msc
, divisor
, remainder
);
535 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
537 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
545 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
548 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
552 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
555 GetReq(DRI2GetMSC
, req
);
556 req
->reqType
= info
->codes
->major_opcode
;
557 req
->dri2ReqType
= X_DRI2GetMSC
;
558 req
->drawable
= drawable
;
560 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
566 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
567 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
568 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
579 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
582 req
->target_msc_hi
= target
>> 32;
583 req
->target_msc_lo
= target
& 0xffffffff;
584 req
->divisor_hi
= divisor
>> 32;
585 req
->divisor_lo
= divisor
& 0xffffffff;
586 req
->remainder_hi
= remainder
>> 32;
587 req
->remainder_lo
= remainder
& 0xffffffff;
590 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
591 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
593 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
594 xDRI2WaitMSCReq
*req
;
597 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
600 GetReq(DRI2WaitMSC
, req
);
601 req
->reqType
= info
->codes
->major_opcode
;
602 req
->dri2ReqType
= X_DRI2WaitMSC
;
603 req
->drawable
= drawable
;
604 load_msc_req(req
, target_msc
, divisor
, remainder
);
606 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
612 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
613 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
614 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
625 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
627 req
->target_sbc_hi
= target
>> 32;
628 req
->target_sbc_lo
= target
& 0xffffffff;
631 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
632 CARD64
*msc
, CARD64
*sbc
)
634 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
635 xDRI2WaitSBCReq
*req
;
638 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
641 GetReq(DRI2WaitSBC
, req
);
642 req
->reqType
= info
->codes
->major_opcode
;
643 req
->dri2ReqType
= X_DRI2WaitSBC
;
644 req
->drawable
= drawable
;
645 load_sbc_req(req
, target_sbc
);
647 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
653 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
654 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
655 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
664 #ifdef X_DRI2SwapInterval
665 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
667 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
668 xDRI2SwapIntervalReq
*req
;
670 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
673 GetReq(DRI2SwapInterval
, req
);
674 req
->reqType
= info
->codes
->major_opcode
;
675 req
->dri2ReqType
= X_DRI2SwapInterval
;
676 req
->drawable
= drawable
;
677 req
->interval
= interval
;
683 #endif /* GLX_DIRECT_RENDERING */