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 (glxDraw
!= NULL
) {
135 if (awire
->sbc
< glxDraw
->lastEventSbc
)
136 glxDraw
->eventSbcWrap
+= 0x100000000;
137 glxDraw
->lastEventSbc
= awire
->sbc
;
138 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
140 aevent
->sbc
= awire
->sbc
;
146 #ifdef DRI2_InvalidateBuffers
147 case DRI2_InvalidateBuffers
:
149 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
151 dri2InvalidateBuffers(dpy
, awire
->drawable
);
156 /* client doesn't support server event */
163 /* We don't actually support this. It doesn't make sense for clients to
164 * send each other DRI2 events.
167 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
169 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
171 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
173 switch (event
->type
) {
175 /* client doesn't support server event */
183 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
185 if (err
->majorCode
== codes
->major_opcode
&&
186 err
->errorCode
== BadDrawable
&&
187 err
->minorCode
== X_DRI2CopyRegion
)
190 /* If the X drawable was destroyed before the GLX drawable, the
191 * DRI2 drawble will be gone by the time we call
192 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
193 if (err
->majorCode
== codes
->major_opcode
&&
194 err
->errorCode
== BadDrawable
&&
195 err
->minorCode
== X_DRI2DestroyDrawable
)
198 /* If the server is non-local DRI2Connect will raise BadRequest.
199 * Swallow this so that DRI2Connect can signal this in its return code */
200 if (err
->majorCode
== codes
->major_opcode
&&
201 err
->minorCode
== X_DRI2Connect
&&
202 err
->errorCode
== BadRequest
) {
211 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
213 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
215 if (XextHasExtension(info
)) {
216 *eventBase
= info
->codes
->first_event
;
217 *errorBase
= info
->codes
->first_error
;
225 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
227 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
228 xDRI2QueryVersionReply rep
;
229 xDRI2QueryVersionReq
*req
;
232 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
235 GetReq(DRI2QueryVersion
, req
);
236 req
->reqType
= info
->codes
->major_opcode
;
237 req
->dri2ReqType
= X_DRI2QueryVersion
;
238 req
->majorVersion
= DRI2_MAJOR
;
239 req
->minorVersion
= DRI2_MINOR
;
240 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
245 *major
= rep
.majorVersion
;
246 *minor
= rep
.minorVersion
;
250 switch (rep
.minorVersion
) {
263 for (i
= 0; i
< nevents
; i
++) {
264 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
265 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
272 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
274 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
275 xDRI2ConnectReply rep
;
276 xDRI2ConnectReq
*req
;
278 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
281 GetReq(DRI2Connect
, req
);
282 req
->reqType
= info
->codes
->major_opcode
;
283 req
->dri2ReqType
= X_DRI2Connect
;
284 req
->window
= window
;
286 req
->driverType
= DRI2DriverDRI
;
287 #ifdef DRI2DriverPrimeShift
289 char *prime
= getenv("DRI_PRIME");
293 primeid
= strtoul(prime
, NULL
, 0);
296 ((primeid
& DRI2DriverPrimeMask
) << DRI2DriverPrimeShift
);
301 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
307 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
313 *driverName
= malloc(rep
.driverNameLength
+ 1);
314 if (*driverName
== NULL
) {
316 ((rep
.driverNameLength
+ 3) & ~3) +
317 ((rep
.deviceNameLength
+ 3) & ~3));
322 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
323 (*driverName
)[rep
.driverNameLength
] = '\0';
325 *deviceName
= malloc(rep
.deviceNameLength
+ 1);
326 if (*deviceName
== NULL
) {
328 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
333 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
334 (*deviceName
)[rep
.deviceNameLength
] = '\0';
343 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
345 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
346 xDRI2AuthenticateReq
*req
;
347 xDRI2AuthenticateReply rep
;
349 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
352 GetReq(DRI2Authenticate
, req
);
353 req
->reqType
= info
->codes
->major_opcode
;
354 req
->dri2ReqType
= X_DRI2Authenticate
;
355 req
->window
= window
;
358 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
367 return rep
.authenticated
;
371 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
373 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
374 xDRI2CreateDrawableReq
*req
;
376 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
379 GetReq(DRI2CreateDrawable
, req
);
380 req
->reqType
= info
->codes
->major_opcode
;
381 req
->dri2ReqType
= X_DRI2CreateDrawable
;
382 req
->drawable
= drawable
;
388 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
390 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
391 xDRI2DestroyDrawableReq
*req
;
393 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
398 GetReq(DRI2DestroyDrawable
, req
);
399 req
->reqType
= info
->codes
->major_opcode
;
400 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
401 req
->drawable
= drawable
;
407 DRI2GetBuffers(Display
* dpy
, XID drawable
,
408 int *width
, int *height
,
409 unsigned int *attachments
, int count
, int *outCount
)
411 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
412 xDRI2GetBuffersReply rep
;
413 xDRI2GetBuffersReq
*req
;
415 xDRI2Buffer repBuffer
;
419 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
422 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
423 req
->reqType
= info
->codes
->major_opcode
;
424 req
->dri2ReqType
= X_DRI2GetBuffers
;
425 req
->drawable
= drawable
;
427 p
= (CARD32
*) & req
[1];
428 for (i
= 0; i
< count
; i
++)
429 p
[i
] = attachments
[i
];
431 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
438 *height
= rep
.height
;
439 *outCount
= rep
.count
;
441 buffers
= malloc(rep
.count
* sizeof buffers
[0]);
442 if (buffers
== NULL
) {
443 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
449 for (i
= 0; i
< rep
.count
; i
++) {
450 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
451 buffers
[i
].attachment
= repBuffer
.attachment
;
452 buffers
[i
].name
= repBuffer
.name
;
453 buffers
[i
].pitch
= repBuffer
.pitch
;
454 buffers
[i
].cpp
= repBuffer
.cpp
;
455 buffers
[i
].flags
= repBuffer
.flags
;
466 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
467 int *width
, int *height
,
468 unsigned int *attachments
, int count
, int *outCount
)
470 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
471 xDRI2GetBuffersReply rep
;
472 xDRI2GetBuffersReq
*req
;
474 xDRI2Buffer repBuffer
;
478 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
481 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
482 req
->reqType
= info
->codes
->major_opcode
;
483 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
484 req
->drawable
= drawable
;
486 p
= (CARD32
*) & req
[1];
487 for (i
= 0; i
< (count
* 2); i
++)
488 p
[i
] = attachments
[i
];
490 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
497 *height
= rep
.height
;
498 *outCount
= rep
.count
;
500 buffers
= malloc(rep
.count
* sizeof buffers
[0]);
501 if (buffers
== NULL
) {
502 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
508 for (i
= 0; i
< rep
.count
; i
++) {
509 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
510 buffers
[i
].attachment
= repBuffer
.attachment
;
511 buffers
[i
].name
= repBuffer
.name
;
512 buffers
[i
].pitch
= repBuffer
.pitch
;
513 buffers
[i
].cpp
= repBuffer
.cpp
;
514 buffers
[i
].flags
= repBuffer
.flags
;
525 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
526 CARD32 dest
, CARD32 src
)
528 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
529 xDRI2CopyRegionReq
*req
;
530 xDRI2CopyRegionReply rep
;
532 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
535 GetReq(DRI2CopyRegion
, req
);
536 req
->reqType
= info
->codes
->major_opcode
;
537 req
->dri2ReqType
= X_DRI2CopyRegion
;
538 req
->drawable
= drawable
;
539 req
->region
= region
;
543 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
549 #endif /* GLX_DIRECT_RENDERING */