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>
45 /* Allow the build to work with an older versions of dri2proto.h and
51 #define X_DRI2GetBuffersWithFormat 7
55 static char dri2ExtensionName
[] = DRI2_NAME
;
56 static XExtensionInfo
*dri2Info
;
57 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay
, dri2Info
)
59 static /* const */ XExtensionHooks dri2ExtensionHooks
= {
64 NULL
, /* create_font */
66 DRI2CloseDisplay
, /* close_display */
67 NULL
, /* wire_to_event */
68 NULL
, /* event_to_wire */
70 NULL
, /* error_string */
73 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay
,
80 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
82 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
84 if (XextHasExtension(info
)) {
85 *eventBase
= info
->codes
->first_event
;
86 *errorBase
= info
->codes
->first_error
;
94 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
96 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
97 xDRI2QueryVersionReply rep
;
98 xDRI2QueryVersionReq
*req
;
100 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
103 GetReq(DRI2QueryVersion
, req
);
104 req
->reqType
= info
->codes
->major_opcode
;
105 req
->dri2ReqType
= X_DRI2QueryVersion
;
106 req
->majorVersion
= DRI2_MAJOR
;
107 req
->minorVersion
= DRI2_MINOR
;
108 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
113 *major
= rep
.majorVersion
;
114 *minor
= rep
.minorVersion
;
122 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
124 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
125 xDRI2ConnectReply rep
;
126 xDRI2ConnectReq
*req
;
128 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
131 GetReq(DRI2Connect
, req
);
132 req
->reqType
= info
->codes
->major_opcode
;
133 req
->dri2ReqType
= X_DRI2Connect
;
134 req
->window
= window
;
135 req
->driverType
= DRI2DriverDRI
;
136 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
142 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
148 *driverName
= Xmalloc(rep
.driverNameLength
+ 1);
149 if (*driverName
== NULL
) {
151 ((rep
.driverNameLength
+ 3) & ~3) +
152 ((rep
.deviceNameLength
+ 3) & ~3));
157 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
158 (*driverName
)[rep
.driverNameLength
] = '\0';
160 *deviceName
= Xmalloc(rep
.deviceNameLength
+ 1);
161 if (*deviceName
== NULL
) {
163 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
168 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
169 (*deviceName
)[rep
.deviceNameLength
] = '\0';
178 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
180 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
181 xDRI2AuthenticateReq
*req
;
182 xDRI2AuthenticateReply rep
;
184 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
187 GetReq(DRI2Authenticate
, req
);
188 req
->reqType
= info
->codes
->major_opcode
;
189 req
->dri2ReqType
= X_DRI2Authenticate
;
190 req
->window
= window
;
193 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
202 return rep
.authenticated
;
206 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
208 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
209 xDRI2CreateDrawableReq
*req
;
211 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
214 GetReq(DRI2CreateDrawable
, req
);
215 req
->reqType
= info
->codes
->major_opcode
;
216 req
->dri2ReqType
= X_DRI2CreateDrawable
;
217 req
->drawable
= drawable
;
223 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
225 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
226 xDRI2DestroyDrawableReq
*req
;
228 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
233 GetReq(DRI2DestroyDrawable
, req
);
234 req
->reqType
= info
->codes
->major_opcode
;
235 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
236 req
->drawable
= drawable
;
242 DRI2GetBuffers(Display
* dpy
, XID drawable
,
243 int *width
, int *height
,
244 unsigned int *attachments
, int count
, int *outCount
)
246 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
247 xDRI2GetBuffersReply rep
;
248 xDRI2GetBuffersReq
*req
;
250 xDRI2Buffer repBuffer
;
254 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
257 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
258 req
->reqType
= info
->codes
->major_opcode
;
259 req
->dri2ReqType
= X_DRI2GetBuffers
;
260 req
->drawable
= drawable
;
262 p
= (CARD32
*) & req
[1];
263 for (i
= 0; i
< count
; i
++)
264 p
[i
] = attachments
[i
];
266 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
273 *height
= rep
.height
;
274 *outCount
= rep
.count
;
276 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
277 if (buffers
== NULL
) {
278 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
284 for (i
= 0; i
< rep
.count
; i
++) {
285 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
286 buffers
[i
].attachment
= repBuffer
.attachment
;
287 buffers
[i
].name
= repBuffer
.name
;
288 buffers
[i
].pitch
= repBuffer
.pitch
;
289 buffers
[i
].cpp
= repBuffer
.cpp
;
290 buffers
[i
].flags
= repBuffer
.flags
;
301 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
302 int *width
, int *height
,
303 unsigned int *attachments
, int count
, int *outCount
)
305 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
306 xDRI2GetBuffersReply rep
;
307 xDRI2GetBuffersReq
*req
;
309 xDRI2Buffer repBuffer
;
313 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
316 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
317 req
->reqType
= info
->codes
->major_opcode
;
318 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
319 req
->drawable
= drawable
;
321 p
= (CARD32
*) & req
[1];
322 for (i
= 0; i
< (count
* 2); i
++)
323 p
[i
] = attachments
[i
];
325 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
332 *height
= rep
.height
;
333 *outCount
= rep
.count
;
335 buffers
= Xmalloc(rep
.count
* sizeof buffers
[0]);
336 if (buffers
== NULL
) {
337 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
343 for (i
= 0; i
< rep
.count
; i
++) {
344 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
345 buffers
[i
].attachment
= repBuffer
.attachment
;
346 buffers
[i
].name
= repBuffer
.name
;
347 buffers
[i
].pitch
= repBuffer
.pitch
;
348 buffers
[i
].cpp
= repBuffer
.cpp
;
349 buffers
[i
].flags
= repBuffer
.flags
;
360 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
361 CARD32 dest
, CARD32 src
)
363 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
364 xDRI2CopyRegionReq
*req
;
365 xDRI2CopyRegionReply rep
;
367 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
370 GetReq(DRI2CopyRegion
, req
);
371 req
->reqType
= info
->codes
->major_opcode
;
372 req
->dri2ReqType
= X_DRI2CopyRegion
;
373 req
->drawable
= drawable
;
374 req
->region
= region
;
378 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
385 load_swap_req(xDRI2SwapBuffersReq
*req
, CARD64 target
, CARD64 divisor
,
388 req
->target_msc_hi
= target
>> 32;
389 req
->target_msc_lo
= target
& 0xffffffff;
390 req
->divisor_hi
= divisor
>> 32;
391 req
->divisor_lo
= divisor
& 0xffffffff;
392 req
->remainder_hi
= remainder
>> 32;
393 req
->remainder_lo
= remainder
& 0xffffffff;
397 vals_to_card64(CARD32 lo
, CARD32 hi
)
399 return (CARD64
)hi
<< 32 | lo
;
402 void DRI2SwapBuffers(Display
*dpy
, XID drawable
, CARD64 target_msc
,
403 CARD64 divisor
, CARD64 remainder
, CARD64
*count
)
405 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
406 xDRI2SwapBuffersReq
*req
;
407 xDRI2SwapBuffersReply rep
;
409 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
412 GetReq(DRI2SwapBuffers
, req
);
413 req
->reqType
= info
->codes
->major_opcode
;
414 req
->dri2ReqType
= X_DRI2SwapBuffers
;
415 req
->drawable
= drawable
;
416 load_swap_req(req
, target_msc
, divisor
, remainder
);
418 _XReply(dpy
, (xReply
*)&rep
, 0, xFalse
);
420 *count
= vals_to_card64(rep
.swap_lo
, rep
.swap_hi
);
426 Bool
DRI2GetMSC(Display
*dpy
, XID drawable
, CARD64
*ust
, CARD64
*msc
,
429 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
433 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
436 GetReq(DRI2GetMSC
, req
);
437 req
->reqType
= info
->codes
->major_opcode
;
438 req
->dri2ReqType
= X_DRI2GetMSC
;
439 req
->drawable
= drawable
;
441 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
447 *ust
= vals_to_card64(rep
.ust_lo
, rep
.ust_hi
);
448 *msc
= vals_to_card64(rep
.msc_lo
, rep
.msc_hi
);
449 *sbc
= vals_to_card64(rep
.sbc_lo
, rep
.sbc_hi
);
458 load_msc_req(xDRI2WaitMSCReq
*req
, CARD64 target
, CARD64 divisor
,
461 req
->target_msc_hi
= target
>> 32;
462 req
->target_msc_lo
= target
& 0xffffffff;
463 req
->divisor_hi
= divisor
>> 32;
464 req
->divisor_lo
= divisor
& 0xffffffff;
465 req
->remainder_hi
= remainder
>> 32;
466 req
->remainder_lo
= remainder
& 0xffffffff;
469 Bool
DRI2WaitMSC(Display
*dpy
, XID drawable
, CARD64 target_msc
, CARD64 divisor
,
470 CARD64 remainder
, CARD64
*ust
, CARD64
*msc
, CARD64
*sbc
)
472 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
473 xDRI2WaitMSCReq
*req
;
476 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
479 GetReq(DRI2WaitMSC
, req
);
480 req
->reqType
= info
->codes
->major_opcode
;
481 req
->dri2ReqType
= X_DRI2WaitMSC
;
482 req
->drawable
= drawable
;
483 load_msc_req(req
, target_msc
, divisor
, remainder
);
485 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
491 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | (CARD64
)rep
.ust_lo
;
492 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | (CARD64
)rep
.msc_lo
;
493 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | (CARD64
)rep
.sbc_lo
;
502 load_sbc_req(xDRI2WaitSBCReq
*req
, CARD64 target
)
504 req
->target_sbc_hi
= target
>> 32;
505 req
->target_sbc_lo
= target
& 0xffffffff;
508 Bool
DRI2WaitSBC(Display
*dpy
, XID drawable
, CARD64 target_sbc
, CARD64
*ust
,
509 CARD64
*msc
, CARD64
*sbc
)
511 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
512 xDRI2WaitSBCReq
*req
;
515 XextCheckExtension (dpy
, info
, dri2ExtensionName
, False
);
518 GetReq(DRI2WaitSBC
, req
);
519 req
->reqType
= info
->codes
->major_opcode
;
520 req
->dri2ReqType
= X_DRI2WaitSBC
;
521 req
->drawable
= drawable
;
522 load_sbc_req(req
, target_sbc
);
524 if (!_XReply(dpy
, (xReply
*)&rep
, 0, xFalse
)) {
530 *ust
= ((CARD64
)rep
.ust_hi
<< 32) | rep
.ust_lo
;
531 *msc
= ((CARD64
)rep
.msc_hi
<< 32) | rep
.msc_lo
;
532 *sbc
= ((CARD64
)rep
.sbc_hi
<< 32) | rep
.sbc_lo
;
540 void DRI2SwapInterval(Display
*dpy
, XID drawable
, int interval
)
542 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
543 xDRI2SwapIntervalReq
*req
;
545 XextSimpleCheckExtension (dpy
, info
, dri2ExtensionName
);
548 GetReq(DRI2SwapInterval
, req
);
549 req
->reqType
= info
->codes
->major_opcode
;
550 req
->dri2ReqType
= X_DRI2SwapInterval
;
551 req
->drawable
= drawable
;
552 req
->interval
= interval
;
557 #endif /* GLX_DIRECT_RENDERING */