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
) {
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 /* client doesn't support server event */
134 /* We don't actually support this. It doesn't make sense for clients to
135 * send each other DRI2 events.
138 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
140 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
142 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
144 switch (event
->type
) {
146 /* client doesn't support server event */
154 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
156 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
158 if (XextHasExtension(info
)) {
159 *eventBase
= info
->codes
->first_event
;
160 *errorBase
= info
->codes
->first_error
;
168 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
170 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
171 xDRI2QueryVersionReply rep
;
172 xDRI2QueryVersionReq
*req
;
174 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
177 GetReq(DRI2QueryVersion
, req
);
178 req
->reqType
= info
->codes
->major_opcode
;
179 req
->dri2ReqType
= X_DRI2QueryVersion
;
180 req
->majorVersion
= DRI2_MAJOR
;
181 req
->minorVersion
= DRI2_MINOR
;
182 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
187 *major
= rep
.majorVersion
;
188 *minor
= rep
.minorVersion
;
196 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
198 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
199 xDRI2ConnectReply rep
;
200 xDRI2ConnectReq
*req
;
202 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
205 GetReq(DRI2Connect
, req
);
206 req
->reqType
= info
->codes
->major_opcode
;
207 req
->dri2ReqType
= X_DRI2Connect
;
208 req
->window
= window
;
209 req
->driverType
= DRI2DriverDRI
;
210 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
216 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
222 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
223 if (*driverName
== NULL
) {
225 ((rep
.driverNameLength
+ 3) & ~3) +
226 ((rep
.deviceNameLength
+ 3) & ~3));
231 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
232 (*driverName
)[rep
.driverNameLength
] = '\0';
234 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
235 if (*deviceName
== NULL
) {
237 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
242 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
243 (*deviceName
)[rep
.deviceNameLength
] = '\0';
252 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
254 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
255 xDRI2AuthenticateReq
*req
;
256 xDRI2AuthenticateReply rep
;
258 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
261 GetReq(DRI2Authenticate
, req
);
262 req
->reqType
= info
->codes
->major_opcode
;
263 req
->dri2ReqType
= X_DRI2Authenticate
;
264 req
->window
= window
;
267 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
276 return rep
.authenticated
;
280 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
282 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
283 xDRI2CreateDrawableReq
*req
;
285 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
288 GetReq(DRI2CreateDrawable
, req
);
289 req
->reqType
= info
->codes
->major_opcode
;
290 req
->dri2ReqType
= X_DRI2CreateDrawable
;
291 req
->drawable
= drawable
;
297 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
299 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
300 xDRI2DestroyDrawableReq
*req
;
302 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
307 GetReq(DRI2DestroyDrawable
, req
);
308 req
->reqType
= info
->codes
->major_opcode
;
309 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
310 req
->drawable
= drawable
;
316 DRI2GetBuffers(Display
* dpy
, XID drawable
,
317 int *width
, int *height
,
318 unsigned int *attachments
, int count
, int *outCount
)
320 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
321 xDRI2GetBuffersReply rep
;
322 xDRI2GetBuffersReq
*req
;
324 xDRI2Buffer repBuffer
;
328 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
331 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
332 req
->reqType
= info
->codes
->major_opcode
;
333 req
->dri2ReqType
= X_DRI2GetBuffers
;
334 req
->drawable
= drawable
;
336 p
= (CARD32
*) & req
[1];
337 for (i
= 0; i
< count
; i
++)
338 p
[i
] = attachments
[i
];
340 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
347 *height
= rep
.height
;
348 *outCount
= rep
.count
;
350 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
351 if (buffers
== NULL
) {
352 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
358 for (i
= 0; i
< rep
.count
; i
++) {
359 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
360 buffers
[i
].attachment
= repBuffer
.attachment
;
361 buffers
[i
].name
= repBuffer
.name
;
362 buffers
[i
].pitch
= repBuffer
.pitch
;
363 buffers
[i
].cpp
= repBuffer
.cpp
;
364 buffers
[i
].flags
= repBuffer
.flags
;
375 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
376 int *width
, int *height
,
377 unsigned int *attachments
, int count
, int *outCount
)
379 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
380 xDRI2GetBuffersReply rep
;
381 xDRI2GetBuffersReq
*req
;
383 xDRI2Buffer repBuffer
;
387 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
390 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
391 req
->reqType
= info
->codes
->major_opcode
;
392 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
393 req
->drawable
= drawable
;
395 p
= (CARD32
*) & req
[1];
396 for (i
= 0; i
< (count
* 2); i
++)
397 p
[i
] = attachments
[i
];
399 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
406 *height
= rep
.height
;
407 *outCount
= rep
.count
;
409 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
410 if (buffers
== NULL
) {
411 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
417 for (i
= 0; i
< rep
.count
; i
++) {
418 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
419 buffers
[i
].attachment
= repBuffer
.attachment
;
420 buffers
[i
].name
= repBuffer
.name
;
421 buffers
[i
].pitch
= repBuffer
.pitch
;
422 buffers
[i
].cpp
= repBuffer
.cpp
;
423 buffers
[i
].flags
= repBuffer
.flags
;
434 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
435 CARD32 dest
, CARD32 src
)
437 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
438 xDRI2CopyRegionReq
*req
;
439 xDRI2CopyRegionReply rep
;
441 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
444 GetReq(DRI2CopyRegion
, req
);
445 req
->reqType
= info
->codes
->major_opcode
;
446 req
->dri2ReqType
= X_DRI2CopyRegion
;
447 req
->drawable
= drawable
;
448 req
->region
= region
;
452 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
459 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
462 req
->target_msc_hi
= target
>> 32;
463 req
->target_msc_lo
= target
& 0xffffffff;
464 req
->divisor_hi
= divisor
>> 32;
465 req
->divisor_lo
= divisor
& 0xffffffff;
466 req
->remainder_hi
= remainder
>> 32;
467 req
->remainder_lo
= remainder
& 0xffffffff;
471 vals_to_card64(CARD32 lo
, CARD32 hi
)
473 return (CARD64
)hi
<< 32 | lo
;
476 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
477 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
479 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
480 xDRI2SwapBuffersReq
*req
;
481 xDRI2SwapBuffersReply rep
;
483 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
486 GetReq(DRI2SwapBuffers
, req
);
487 req
->reqType
= info
->codes
->major_opcode
;
488 req
->dri2ReqType
= X_DRI2SwapBuffers
;
489 req
->drawable
= drawable
;
490 load_swap_req(req
, target_msc
, divisor
, remainder
);
492 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
494 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
500 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
503 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
507 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
510 GetReq(DRI2GetMSC
, req
);
511 req
->reqType
= info
->codes
->major_opcode
;
512 req
->dri2ReqType
= X_DRI2GetMSC
;
513 req
->drawable
= drawable
;
515 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
521 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
522 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
523 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
532 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
535 req
->target_msc_hi
= target
>> 32;
536 req
->target_msc_lo
= target
& 0xffffffff;
537 req
->divisor_hi
= divisor
>> 32;
538 req
->divisor_lo
= divisor
& 0xffffffff;
539 req
->remainder_hi
= remainder
>> 32;
540 req
->remainder_lo
= remainder
& 0xffffffff;
543 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
544 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
546 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
547 xDRI2WaitMSCReq
*req
;
550 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
553 GetReq(DRI2WaitMSC
, req
);
554 req
->reqType
= info
->codes
->major_opcode
;
555 req
->dri2ReqType
= X_DRI2WaitMSC
;
556 req
->drawable
= drawable
;
557 load_msc_req(req
, target_msc
, divisor
, remainder
);
559 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
565 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
566 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
567 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
576 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
578 req
->target_sbc_hi
= target
>> 32;
579 req
->target_sbc_lo
= target
& 0xffffffff;
582 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
583 CARD64
*msc
, CARD64
*sbc
)
585 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
586 xDRI2WaitSBCReq
*req
;
589 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
592 GetReq(DRI2WaitSBC
, req
);
593 req
->reqType
= info
->codes
->major_opcode
;
594 req
->dri2ReqType
= X_DRI2WaitSBC
;
595 req
->drawable
= drawable
;
596 load_sbc_req(req
, target_sbc
);
598 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
604 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
605 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
606 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
614 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
616 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
617 xDRI2SwapIntervalReq
*req
;
619 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
622 GetReq(DRI2SwapInterval
, req
);
623 req
->reqType
= info
->codes
->major_opcode
;
624 req
->dri2ReqType
= X_DRI2SwapInterval
;
625 req
->drawable
= drawable
;
626 req
->interval
= interval
;
631 #endif /* GLX_DIRECT_RENDERING */