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)
36 #include <X11/Xlibint.h>
37 #include <X11/extensions/Xext.h>
38 #include <X11/extensions/extutil.h>
39 #include <X11/extensions/dri2proto.h>
43 #include "glxclient.h"
44 #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
)
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
);
104 (glx_info
->codes
->first_event
+ GLX_BufferSwapComplete
) & 0x75;
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_BLIT_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 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
167 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
169 if (XextHasExtension(info
)) {
170 *eventBase
= info
->codes
->first_event
;
171 *errorBase
= info
->codes
->first_error
;
179 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
181 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
182 xDRI2QueryVersionReply rep
;
183 xDRI2QueryVersionReq
*req
;
186 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
189 GetReq(DRI2QueryVersion
, req
);
190 req
->reqType
= info
->codes
->major_opcode
;
191 req
->dri2ReqType
= X_DRI2QueryVersion
;
192 req
->majorVersion
= DRI2_MAJOR
;
193 req
->minorVersion
= DRI2_MINOR
;
194 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
199 *major
= rep
.majorVersion
;
200 *minor
= rep
.minorVersion
;
204 switch (rep
.minorVersion
) {
217 for (i
= 0; i
< nevents
; i
++) {
218 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
219 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
226 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
228 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
229 xDRI2ConnectReply rep
;
230 xDRI2ConnectReq
*req
;
232 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
235 GetReq(DRI2Connect
, req
);
236 req
->reqType
= info
->codes
->major_opcode
;
237 req
->dri2ReqType
= X_DRI2Connect
;
238 req
->window
= window
;
239 req
->driverType
= DRI2DriverDRI
;
240 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
246 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
252 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
253 if (*driverName
== NULL
) {
255 ((rep
.driverNameLength
+ 3) & ~3) +
256 ((rep
.deviceNameLength
+ 3) & ~3));
261 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
262 (*driverName
)[rep
.driverNameLength
] = '\0';
264 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
265 if (*deviceName
== NULL
) {
267 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
272 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
273 (*deviceName
)[rep
.deviceNameLength
] = '\0';
282 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
284 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
285 xDRI2AuthenticateReq
*req
;
286 xDRI2AuthenticateReply rep
;
288 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
291 GetReq(DRI2Authenticate
, req
);
292 req
->reqType
= info
->codes
->major_opcode
;
293 req
->dri2ReqType
= X_DRI2Authenticate
;
294 req
->window
= window
;
297 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
306 return rep
.authenticated
;
310 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
312 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
313 xDRI2CreateDrawableReq
*req
;
315 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
318 GetReq(DRI2CreateDrawable
, req
);
319 req
->reqType
= info
->codes
->major_opcode
;
320 req
->dri2ReqType
= X_DRI2CreateDrawable
;
321 req
->drawable
= drawable
;
327 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
329 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
330 xDRI2DestroyDrawableReq
*req
;
332 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
337 GetReq(DRI2DestroyDrawable
, req
);
338 req
->reqType
= info
->codes
->major_opcode
;
339 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
340 req
->drawable
= drawable
;
346 DRI2GetBuffers(Display
* dpy
, XID drawable
,
347 int *width
, int *height
,
348 unsigned int *attachments
, int count
, int *outCount
)
350 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
351 xDRI2GetBuffersReply rep
;
352 xDRI2GetBuffersReq
*req
;
354 xDRI2Buffer repBuffer
;
358 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
361 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
362 req
->reqType
= info
->codes
->major_opcode
;
363 req
->dri2ReqType
= X_DRI2GetBuffers
;
364 req
->drawable
= drawable
;
366 p
= (CARD32
*) & req
[1];
367 for (i
= 0; i
< count
; i
++)
368 p
[i
] = attachments
[i
];
370 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
377 *height
= rep
.height
;
378 *outCount
= rep
.count
;
380 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
381 if (buffers
== NULL
) {
382 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
388 for (i
= 0; i
< rep
.count
; i
++) {
389 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
390 buffers
[i
].attachment
= repBuffer
.attachment
;
391 buffers
[i
].name
= repBuffer
.name
;
392 buffers
[i
].pitch
= repBuffer
.pitch
;
393 buffers
[i
].cpp
= repBuffer
.cpp
;
394 buffers
[i
].flags
= repBuffer
.flags
;
405 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
406 int *width
, int *height
,
407 unsigned int *attachments
, int count
, int *outCount
)
409 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
410 xDRI2GetBuffersReply rep
;
411 xDRI2GetBuffersReq
*req
;
413 xDRI2Buffer repBuffer
;
417 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
420 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
421 req
->reqType
= info
->codes
->major_opcode
;
422 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
423 req
->drawable
= drawable
;
425 p
= (CARD32
*) & req
[1];
426 for (i
= 0; i
< (count
* 2); i
++)
427 p
[i
] = attachments
[i
];
429 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
436 *height
= rep
.height
;
437 *outCount
= rep
.count
;
439 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
440 if (buffers
== NULL
) {
441 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
447 for (i
= 0; i
< rep
.count
; i
++) {
448 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
449 buffers
[i
].attachment
= repBuffer
.attachment
;
450 buffers
[i
].name
= repBuffer
.name
;
451 buffers
[i
].pitch
= repBuffer
.pitch
;
452 buffers
[i
].cpp
= repBuffer
.cpp
;
453 buffers
[i
].flags
= repBuffer
.flags
;
464 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
465 CARD32 dest
, CARD32 src
)
467 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
468 xDRI2CopyRegionReq
*req
;
470 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
473 GetReq(DRI2CopyRegion
, req
);
474 req
->reqType
= info
->codes
->major_opcode
;
475 req
->dri2ReqType
= X_DRI2CopyRegion
;
476 req
->drawable
= drawable
;
477 req
->region
= region
;
485 #ifdef X_DRI2SwapBuffers
487 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
490 req
->target_msc_hi
= target
>> 32;
491 req
->target_msc_lo
= target
& 0xffffffff;
492 req
->divisor_hi
= divisor
>> 32;
493 req
->divisor_lo
= divisor
& 0xffffffff;
494 req
->remainder_hi
= remainder
>> 32;
495 req
->remainder_lo
= remainder
& 0xffffffff;
499 vals_to_card64(CARD32 lo
, CARD32 hi
)
501 return (CARD64
)hi
<< 32 | lo
;
504 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
505 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
507 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
508 xDRI2SwapBuffersReq
*req
;
509 xDRI2SwapBuffersReply rep
;
511 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
514 GetReq(DRI2SwapBuffers
, req
);
515 req
->reqType
= info
->codes
->major_opcode
;
516 req
->dri2ReqType
= X_DRI2SwapBuffers
;
517 req
->drawable
= drawable
;
518 load_swap_req(req
, target_msc
, divisor
, remainder
);
520 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
522 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
530 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
533 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
537 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
540 GetReq(DRI2GetMSC
, req
);
541 req
->reqType
= info
->codes
->major_opcode
;
542 req
->dri2ReqType
= X_DRI2GetMSC
;
543 req
->drawable
= drawable
;
545 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
551 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
552 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
553 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
564 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
567 req
->target_msc_hi
= target
>> 32;
568 req
->target_msc_lo
= target
& 0xffffffff;
569 req
->divisor_hi
= divisor
>> 32;
570 req
->divisor_lo
= divisor
& 0xffffffff;
571 req
->remainder_hi
= remainder
>> 32;
572 req
->remainder_lo
= remainder
& 0xffffffff;
575 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
576 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
578 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
579 xDRI2WaitMSCReq
*req
;
582 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
585 GetReq(DRI2WaitMSC
, req
);
586 req
->reqType
= info
->codes
->major_opcode
;
587 req
->dri2ReqType
= X_DRI2WaitMSC
;
588 req
->drawable
= drawable
;
589 load_msc_req(req
, target_msc
, divisor
, remainder
);
591 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
597 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
598 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
599 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
610 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
612 req
->target_sbc_hi
= target
>> 32;
613 req
->target_sbc_lo
= target
& 0xffffffff;
616 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
617 CARD64
*msc
, CARD64
*sbc
)
619 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
620 xDRI2WaitSBCReq
*req
;
623 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
626 GetReq(DRI2WaitSBC
, req
);
627 req
->reqType
= info
->codes
->major_opcode
;
628 req
->dri2ReqType
= X_DRI2WaitSBC
;
629 req
->drawable
= drawable
;
630 load_sbc_req(req
, target_sbc
);
632 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
638 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
639 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
640 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
649 #ifdef X_DRI2SwapInterval
650 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
652 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
653 xDRI2SwapIntervalReq
*req
;
655 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
658 GetReq(DRI2SwapInterval
, req
);
659 req
->reqType
= info
->codes
->major_opcode
;
660 req
->dri2ReqType
= X_DRI2SwapInterval
;
661 req
->drawable
= drawable
;
662 req
->interval
= interval
;