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>
42 #include "glxclient.h"
43 #include "GL/glxext.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
)
60 DRI2WireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
);
62 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
);
64 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
);
66 static /* const */ XExtensionHooks dri2ExtensionHooks
= {
71 NULL
, /* create_font */
73 DRI2CloseDisplay
, /* close_display */
74 DRI2WireToEvent
, /* wire_to_event */
75 DRI2EventToWire
, /* event_to_wire */
76 DRI2Error
, /* error */
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 struct glx_drawable
*glxDraw
;
92 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
94 switch ((wire
->u
.u
.type
& 0x7f) - info
->codes
->first_event
) {
96 #ifdef X_DRI2SwapBuffers
97 case DRI2_BufferSwapComplete
:
99 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
100 xDRI2BufferSwapComplete2
*awire
= (xDRI2BufferSwapComplete2
*)wire
;
101 __GLXDRIdrawable
*pdraw
;
103 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, awire
->drawable
);
107 /* Ignore swap events if we're not looking for them */
108 aevent
->type
= dri2GetSwapEventType(dpy
, awire
->drawable
);
112 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
113 aevent
->send_event
= (awire
->type
& 0x80) != 0;
114 aevent
->display
= dpy
;
115 aevent
->drawable
= awire
->drawable
;
116 switch (awire
->event_type
) {
117 case DRI2_EXCHANGE_COMPLETE
:
118 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
120 case DRI2_BLIT_COMPLETE
:
121 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
123 case DRI2_FLIP_COMPLETE
:
124 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
127 /* unknown swap completion type */
130 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
131 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
133 glxDraw
= GetGLXDrawable(dpy
, pdraw
->drawable
);
134 if (awire
->sbc
< glxDraw
->lastEventSbc
)
135 glxDraw
->eventSbcWrap
+= 0x100000000;
136 glxDraw
->lastEventSbc
= awire
->sbc
;
137 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
142 #ifdef DRI2_InvalidateBuffers
143 case DRI2_InvalidateBuffers
:
145 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
147 dri2InvalidateBuffers(dpy
, awire
->drawable
);
152 /* client doesn't support server event */
159 /* We don't actually support this. It doesn't make sense for clients to
160 * send each other DRI2 events.
163 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
165 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
167 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
169 switch (event
->type
) {
171 /* client doesn't support server event */
179 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
181 if (err
->majorCode
== codes
->major_opcode
&&
182 err
->errorCode
== BadDrawable
&&
183 err
->minorCode
== X_DRI2CopyRegion
)
186 /* If the X drawable was destroyed before the GLX drawable, the
187 * DRI2 drawble will be gone by the time we call
188 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
189 if (err
->majorCode
== codes
->major_opcode
&&
190 err
->errorCode
== BadDrawable
&&
191 err
->minorCode
== X_DRI2DestroyDrawable
)
194 /* If the server is non-local DRI2Connect will raise BadRequest.
195 * Swallow this so that DRI2Connect can signal this in its return code */
196 if (err
->majorCode
== codes
->major_opcode
&&
197 err
->minorCode
== X_DRI2Connect
&&
198 err
->errorCode
== BadRequest
) {
207 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
209 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
211 if (XextHasExtension(info
)) {
212 *eventBase
= info
->codes
->first_event
;
213 *errorBase
= info
->codes
->first_error
;
221 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
223 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
224 xDRI2QueryVersionReply rep
;
225 xDRI2QueryVersionReq
*req
;
228 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
231 GetReq(DRI2QueryVersion
, req
);
232 req
->reqType
= info
->codes
->major_opcode
;
233 req
->dri2ReqType
= X_DRI2QueryVersion
;
234 req
->majorVersion
= DRI2_MAJOR
;
235 req
->minorVersion
= DRI2_MINOR
;
236 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
241 *major
= rep
.majorVersion
;
242 *minor
= rep
.minorVersion
;
246 switch (rep
.minorVersion
) {
259 for (i
= 0; i
< nevents
; i
++) {
260 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
261 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
268 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
270 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
271 xDRI2ConnectReply rep
;
272 xDRI2ConnectReq
*req
;
274 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
277 GetReq(DRI2Connect
, req
);
278 req
->reqType
= info
->codes
->major_opcode
;
279 req
->dri2ReqType
= X_DRI2Connect
;
280 req
->window
= window
;
282 req
->driverType
= DRI2DriverDRI
;
283 #ifdef DRI2DriverPrimeShift
285 char *prime
= getenv("DRI_PRIME");
289 primeid
= strtoul(prime
, NULL
, 0);
292 ((primeid
& DRI2DriverPrimeMask
) << DRI2DriverPrimeShift
);
297 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
303 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
309 *driverName
= malloc(rep
.driverNameLength
+ 1);
310 if (*driverName
== NULL
) {
312 ((rep
.driverNameLength
+ 3) & ~3) +
313 ((rep
.deviceNameLength
+ 3) & ~3));
318 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
319 (*driverName
)[rep
.driverNameLength
] = '\0';
321 *deviceName
= malloc(rep
.deviceNameLength
+ 1);
322 if (*deviceName
== NULL
) {
324 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
329 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
330 (*deviceName
)[rep
.deviceNameLength
] = '\0';
339 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
341 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
342 xDRI2AuthenticateReq
*req
;
343 xDRI2AuthenticateReply rep
;
345 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
348 GetReq(DRI2Authenticate
, req
);
349 req
->reqType
= info
->codes
->major_opcode
;
350 req
->dri2ReqType
= X_DRI2Authenticate
;
351 req
->window
= window
;
354 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
363 return rep
.authenticated
;
367 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
369 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
370 xDRI2CreateDrawableReq
*req
;
372 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
375 GetReq(DRI2CreateDrawable
, req
);
376 req
->reqType
= info
->codes
->major_opcode
;
377 req
->dri2ReqType
= X_DRI2CreateDrawable
;
378 req
->drawable
= drawable
;
384 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
386 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
387 xDRI2DestroyDrawableReq
*req
;
389 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
394 GetReq(DRI2DestroyDrawable
, req
);
395 req
->reqType
= info
->codes
->major_opcode
;
396 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
397 req
->drawable
= drawable
;
403 DRI2GetBuffers(Display
* dpy
, XID drawable
,
404 int *width
, int *height
,
405 unsigned int *attachments
, int count
, int *outCount
)
407 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
408 xDRI2GetBuffersReply rep
;
409 xDRI2GetBuffersReq
*req
;
411 xDRI2Buffer repBuffer
;
415 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
418 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
419 req
->reqType
= info
->codes
->major_opcode
;
420 req
->dri2ReqType
= X_DRI2GetBuffers
;
421 req
->drawable
= drawable
;
423 p
= (CARD32
*) & req
[1];
424 for (i
= 0; i
< count
; i
++)
425 p
[i
] = attachments
[i
];
427 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
434 *height
= rep
.height
;
435 *outCount
= rep
.count
;
437 buffers
= malloc(rep
.count
* sizeof buffers
[0]);
438 if (buffers
== NULL
) {
439 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
445 for (i
= 0; i
< rep
.count
; i
++) {
446 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
447 buffers
[i
].attachment
= repBuffer
.attachment
;
448 buffers
[i
].name
= repBuffer
.name
;
449 buffers
[i
].pitch
= repBuffer
.pitch
;
450 buffers
[i
].cpp
= repBuffer
.cpp
;
451 buffers
[i
].flags
= repBuffer
.flags
;
462 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
463 int *width
, int *height
,
464 unsigned int *attachments
, int count
, int *outCount
)
466 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
467 xDRI2GetBuffersReply rep
;
468 xDRI2GetBuffersReq
*req
;
470 xDRI2Buffer repBuffer
;
474 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
477 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
478 req
->reqType
= info
->codes
->major_opcode
;
479 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
480 req
->drawable
= drawable
;
482 p
= (CARD32
*) & req
[1];
483 for (i
= 0; i
< (count
* 2); i
++)
484 p
[i
] = attachments
[i
];
486 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
493 *height
= rep
.height
;
494 *outCount
= rep
.count
;
496 buffers
= malloc(rep
.count
* sizeof buffers
[0]);
497 if (buffers
== NULL
) {
498 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
504 for (i
= 0; i
< rep
.count
; i
++) {
505 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
506 buffers
[i
].attachment
= repBuffer
.attachment
;
507 buffers
[i
].name
= repBuffer
.name
;
508 buffers
[i
].pitch
= repBuffer
.pitch
;
509 buffers
[i
].cpp
= repBuffer
.cpp
;
510 buffers
[i
].flags
= repBuffer
.flags
;
521 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
522 CARD32 dest
, CARD32 src
)
524 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
525 xDRI2CopyRegionReq
*req
;
526 xDRI2CopyRegionReply rep
;
528 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
531 GetReq(DRI2CopyRegion
, req
);
532 req
->reqType
= info
->codes
->major_opcode
;
533 req
->dri2ReqType
= X_DRI2CopyRegion
;
534 req
->drawable
= drawable
;
535 req
->region
= region
;
539 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
545 #endif /* GLX_DIRECT_RENDERING */