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_data
;
57 static XExtensionInfo
*dri2Info
= &_dri2Info_data
;
58 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay
, dri2Info
)
61 DRI2WireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
);
63 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
);
65 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
);
67 static /* const */ XExtensionHooks dri2ExtensionHooks
= {
72 NULL
, /* create_font */
74 DRI2CloseDisplay
, /* close_display */
75 DRI2WireToEvent
, /* wire_to_event */
76 DRI2EventToWire
, /* event_to_wire */
77 DRI2Error
, /* error */
78 NULL
, /* error_string */
81 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay
,
88 DRI2WireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
90 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
91 struct glx_drawable
*glxDraw
;
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 xDRI2BufferSwapComplete2
*awire
= (xDRI2BufferSwapComplete2
*)wire
;
102 __GLXDRIdrawable
*pdraw
;
104 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, awire
->drawable
);
108 /* Ignore swap events if we're not looking for them */
109 aevent
->type
= dri2GetSwapEventType(dpy
, awire
->drawable
);
113 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
114 aevent
->send_event
= (awire
->type
& 0x80) != 0;
115 aevent
->display
= dpy
;
116 aevent
->drawable
= awire
->drawable
;
117 switch (awire
->event_type
) {
118 case DRI2_EXCHANGE_COMPLETE
:
119 aevent
->event_type
= GLX_EXCHANGE_COMPLETE_INTEL
;
121 case DRI2_BLIT_COMPLETE
:
122 aevent
->event_type
= GLX_COPY_COMPLETE_INTEL
;
124 case DRI2_FLIP_COMPLETE
:
125 aevent
->event_type
= GLX_FLIP_COMPLETE_INTEL
;
128 /* unknown swap completion type */
131 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
132 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
134 glxDraw
= GetGLXDrawable(dpy
, pdraw
->drawable
);
135 if (glxDraw
!= NULL
) {
136 if (awire
->sbc
< glxDraw
->lastEventSbc
)
137 glxDraw
->eventSbcWrap
+= 0x100000000;
138 glxDraw
->lastEventSbc
= awire
->sbc
;
139 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
141 aevent
->sbc
= awire
->sbc
;
147 #ifdef DRI2_InvalidateBuffers
148 case DRI2_InvalidateBuffers
:
150 xDRI2InvalidateBuffers
*awire
= (xDRI2InvalidateBuffers
*)wire
;
152 dri2InvalidateBuffers(dpy
, awire
->drawable
);
157 /* client doesn't support server event */
164 /* We don't actually support this. It doesn't make sense for clients to
165 * send each other DRI2 events.
168 DRI2EventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
170 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
172 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
174 switch (event
->type
) {
176 /* client doesn't support server event */
184 DRI2Error(Display
*display
, xError
*err
, XExtCodes
*codes
, int *ret_code
)
186 if (err
->majorCode
== codes
->major_opcode
&&
187 err
->errorCode
== BadDrawable
&&
188 err
->minorCode
== X_DRI2CopyRegion
)
191 /* If the X drawable was destroyed before the GLX drawable, the
192 * DRI2 drawble will be gone by the time we call
193 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
194 if (err
->majorCode
== codes
->major_opcode
&&
195 err
->errorCode
== BadDrawable
&&
196 err
->minorCode
== X_DRI2DestroyDrawable
)
199 /* If the server is non-local DRI2Connect will raise BadRequest.
200 * Swallow this so that DRI2Connect can signal this in its return code */
201 if (err
->majorCode
== codes
->major_opcode
&&
202 err
->minorCode
== X_DRI2Connect
&&
203 err
->errorCode
== BadRequest
) {
212 DRI2QueryExtension(Display
* dpy
, int *eventBase
, int *errorBase
)
214 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
216 if (XextHasExtension(info
)) {
217 *eventBase
= info
->codes
->first_event
;
218 *errorBase
= info
->codes
->first_error
;
226 DRI2QueryVersion(Display
* dpy
, int *major
, int *minor
)
228 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
229 xDRI2QueryVersionReply rep
;
230 xDRI2QueryVersionReq
*req
;
233 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
236 GetReq(DRI2QueryVersion
, req
);
237 req
->reqType
= info
->codes
->major_opcode
;
238 req
->dri2ReqType
= X_DRI2QueryVersion
;
239 req
->majorVersion
= DRI2_MAJOR
;
240 req
->minorVersion
= DRI2_MINOR
;
241 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
246 *major
= rep
.majorVersion
;
247 *minor
= rep
.minorVersion
;
251 switch (rep
.minorVersion
) {
264 for (i
= 0; i
< nevents
; i
++) {
265 XESetWireToEvent (dpy
, info
->codes
->first_event
+ i
, DRI2WireToEvent
);
266 XESetEventToWire (dpy
, info
->codes
->first_event
+ i
, DRI2EventToWire
);
273 DRI2Connect(Display
* dpy
, XID window
, char **driverName
, char **deviceName
)
275 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
276 xDRI2ConnectReply rep
;
277 xDRI2ConnectReq
*req
;
279 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
282 GetReq(DRI2Connect
, req
);
283 req
->reqType
= info
->codes
->major_opcode
;
284 req
->dri2ReqType
= X_DRI2Connect
;
285 req
->window
= window
;
287 req
->driverType
= DRI2DriverDRI
;
288 #ifdef DRI2DriverPrimeShift
290 char *prime
= getenv("DRI_PRIME");
294 primeid
= strtoul(prime
, NULL
, 0);
297 ((primeid
& DRI2DriverPrimeMask
) << DRI2DriverPrimeShift
);
302 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
308 if (rep
.driverNameLength
== 0 && rep
.deviceNameLength
== 0) {
314 *driverName
= malloc(rep
.driverNameLength
+ 1);
315 if (*driverName
== NULL
) {
317 ((rep
.driverNameLength
+ 3) & ~3) +
318 ((rep
.deviceNameLength
+ 3) & ~3));
323 _XReadPad(dpy
, *driverName
, rep
.driverNameLength
);
324 (*driverName
)[rep
.driverNameLength
] = '\0';
326 *deviceName
= malloc(rep
.deviceNameLength
+ 1);
327 if (*deviceName
== NULL
) {
329 _XEatData(dpy
, ((rep
.deviceNameLength
+ 3) & ~3));
334 _XReadPad(dpy
, *deviceName
, rep
.deviceNameLength
);
335 (*deviceName
)[rep
.deviceNameLength
] = '\0';
344 DRI2Authenticate(Display
* dpy
, XID window
, drm_magic_t magic
)
346 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
347 xDRI2AuthenticateReq
*req
;
348 xDRI2AuthenticateReply rep
;
350 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
353 GetReq(DRI2Authenticate
, req
);
354 req
->reqType
= info
->codes
->major_opcode
;
355 req
->dri2ReqType
= X_DRI2Authenticate
;
356 req
->window
= window
;
359 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
368 return rep
.authenticated
;
372 DRI2CreateDrawable(Display
* dpy
, XID drawable
)
374 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
375 xDRI2CreateDrawableReq
*req
;
377 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
380 GetReq(DRI2CreateDrawable
, req
);
381 req
->reqType
= info
->codes
->major_opcode
;
382 req
->dri2ReqType
= X_DRI2CreateDrawable
;
383 req
->drawable
= drawable
;
389 DRI2DestroyDrawable(Display
* dpy
, XID drawable
)
391 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
392 xDRI2DestroyDrawableReq
*req
;
394 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
399 GetReq(DRI2DestroyDrawable
, req
);
400 req
->reqType
= info
->codes
->major_opcode
;
401 req
->dri2ReqType
= X_DRI2DestroyDrawable
;
402 req
->drawable
= drawable
;
408 DRI2GetBuffers(Display
* dpy
, XID drawable
,
409 int *width
, int *height
,
410 unsigned int *attachments
, int count
, int *outCount
)
412 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
413 xDRI2GetBuffersReply rep
;
414 xDRI2GetBuffersReq
*req
;
416 xDRI2Buffer repBuffer
;
420 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
423 GetReqExtra(DRI2GetBuffers
, count
* 4, req
);
424 req
->reqType
= info
->codes
->major_opcode
;
425 req
->dri2ReqType
= X_DRI2GetBuffers
;
426 req
->drawable
= drawable
;
428 p
= (CARD32
*) & req
[1];
429 for (i
= 0; i
< count
; i
++)
430 p
[i
] = attachments
[i
];
432 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
439 *height
= rep
.height
;
440 *outCount
= rep
.count
;
442 buffers
= malloc(rep
.count
* sizeof buffers
[0]);
443 if (buffers
== NULL
) {
444 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
450 for (i
= 0; i
< rep
.count
; i
++) {
451 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
452 buffers
[i
].attachment
= repBuffer
.attachment
;
453 buffers
[i
].name
= repBuffer
.name
;
454 buffers
[i
].pitch
= repBuffer
.pitch
;
455 buffers
[i
].cpp
= repBuffer
.cpp
;
456 buffers
[i
].flags
= repBuffer
.flags
;
467 DRI2GetBuffersWithFormat(Display
* dpy
, XID drawable
,
468 int *width
, int *height
,
469 unsigned int *attachments
, int count
, int *outCount
)
471 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
472 xDRI2GetBuffersReply rep
;
473 xDRI2GetBuffersReq
*req
;
475 xDRI2Buffer repBuffer
;
479 XextCheckExtension(dpy
, info
, dri2ExtensionName
, False
);
482 GetReqExtra(DRI2GetBuffers
, count
* (4 * 2), req
);
483 req
->reqType
= info
->codes
->major_opcode
;
484 req
->dri2ReqType
= X_DRI2GetBuffersWithFormat
;
485 req
->drawable
= drawable
;
487 p
= (CARD32
*) & req
[1];
488 for (i
= 0; i
< (count
* 2); i
++)
489 p
[i
] = attachments
[i
];
491 if (!_XReply(dpy
, (xReply
*) & rep
, 0, xFalse
)) {
498 *height
= rep
.height
;
499 *outCount
= rep
.count
;
501 buffers
= malloc(rep
.count
* sizeof buffers
[0]);
502 if (buffers
== NULL
) {
503 _XEatData(dpy
, rep
.count
* sizeof repBuffer
);
509 for (i
= 0; i
< rep
.count
; i
++) {
510 _XReadPad(dpy
, (char *) &repBuffer
, sizeof repBuffer
);
511 buffers
[i
].attachment
= repBuffer
.attachment
;
512 buffers
[i
].name
= repBuffer
.name
;
513 buffers
[i
].pitch
= repBuffer
.pitch
;
514 buffers
[i
].cpp
= repBuffer
.cpp
;
515 buffers
[i
].flags
= repBuffer
.flags
;
526 DRI2CopyRegion(Display
* dpy
, XID drawable
, XserverRegion region
,
527 CARD32 dest
, CARD32 src
)
529 XExtDisplayInfo
*info
= DRI2FindDisplay(dpy
);
530 xDRI2CopyRegionReq
*req
;
531 xDRI2CopyRegionReply rep
;
533 XextSimpleCheckExtension(dpy
, info
, dri2ExtensionName
);
536 GetReq(DRI2CopyRegion
, req
);
537 req
->reqType
= info
->codes
->major_opcode
;
538 req
->dri2ReqType
= X_DRI2CopyRegion
;
539 req
->drawable
= drawable
;
540 req
->region
= region
;
544 _XReply(dpy
, (xReply
*) & rep
, 0, xFalse
);
550 #endif /* GLX_DIRECT_RENDERING */