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)
33 #ifdef GLX_DIRECT_RENDERING
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/extensions/Xdamage.h>
38 #include "glxclient.h"
39 #include "glcontextmodes.h"
44 #include <sys/types.h>
48 #include "dri_common.h"
50 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate
;
51 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate
;
52 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate
;
54 struct __GLXDRIdisplayPrivateRec
{
58 ** XFree86-DRI version information
64 unsigned long configureSeqno
;
65 Bool (*oldConfigProc
)(Display
*, XEvent
*, xEvent
*);
68 struct __GLXDRIcontextPrivateRec
{
70 __DRIcontext
*driContext
;
71 __GLXscreenConfigs
*psc
;
74 struct __GLXDRIdrawablePrivateRec
{
75 __GLXDRIdrawable base
;
76 __DRIbuffer buffers
[5];
79 unsigned long configureSeqno
;
82 static void dri2DestroyContext(__GLXDRIcontext
*context
,
83 __GLXscreenConfigs
*psc
, Display
*dpy
)
85 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
86 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
88 (*core
->destroyContext
)(pcp
->driContext
);
93 static Bool
dri2BindContext(__GLXDRIcontext
*context
,
94 __GLXDRIdrawable
*draw
, __GLXDRIdrawable
*read
)
96 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
97 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
99 return (*core
->bindContext
)(pcp
->driContext
,
104 static void dri2UnbindContext(__GLXDRIcontext
*context
)
106 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
107 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
109 (*core
->unbindContext
)(pcp
->driContext
);
112 static __GLXDRIcontext
*dri2CreateContext(__GLXscreenConfigs
*psc
,
113 const __GLcontextModes
*mode
,
115 GLXContext shareList
, int renderType
)
117 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
118 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
119 __DRIcontext
*shared
= NULL
;
122 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
123 shared
= pcp_shared
->driContext
;
126 pcp
= Xmalloc(sizeof *pcp
);
132 (*psc
->dri2
->createNewContext
)(psc
->__driScreen
,
133 config
->driConfig
, shared
, pcp
);
134 gc
->__driContext
= pcp
->driContext
;
136 if (pcp
->driContext
== NULL
) {
141 pcp
->base
.destroyContext
= dri2DestroyContext
;
142 pcp
->base
.bindContext
= dri2BindContext
;
143 pcp
->base
.unbindContext
= dri2UnbindContext
;
148 static void dri2DestroyDrawable(__GLXDRIdrawable
*pdraw
)
150 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
152 (*core
->destroyDrawable
)(pdraw
->driDrawable
);
153 DRI2DestroyDrawable(pdraw
->psc
->dpy
, pdraw
->drawable
);
157 static __GLXDRIdrawable
*dri2CreateDrawable(__GLXscreenConfigs
*psc
,
159 GLXDrawable drawable
,
160 const __GLcontextModes
*modes
)
162 __GLXDRIdrawablePrivate
*pdraw
;
163 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
165 pdraw
= Xmalloc(sizeof(*pdraw
));
169 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
170 pdraw
->base
.xDrawable
= xDrawable
;
171 pdraw
->base
.drawable
= drawable
;
172 pdraw
->base
.psc
= psc
;
173 pdraw
->bufferCount
= 0;
174 pdraw
->configureSeqno
= ~0;
176 DRI2CreateDrawable(psc
->dpy
, xDrawable
);
178 /* Create a new drawable */
179 pdraw
->base
.driDrawable
=
180 (*psc
->dri2
->createNewDrawable
)(psc
->__driScreen
,
181 config
->driConfig
, pdraw
);
183 if (!pdraw
->base
.driDrawable
) {
184 DRI2DestroyDrawable(psc
->dpy
, drawable
);
192 static void dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
,
193 int x
, int y
, int width
, int height
)
195 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
197 XserverRegion region
;
200 xrect
.y
= priv
->height
- y
- height
;
202 xrect
.height
= height
;
204 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
205 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->drawable
, region
,
206 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
207 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
210 static void dri2SwapBuffers(__GLXDRIdrawable
*pdraw
)
212 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
214 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
217 static void dri2DestroyScreen(__GLXscreenConfigs
*psc
)
219 /* Free the direct rendering per screen data */
220 (*psc
->core
->destroyScreen
)(psc
->__driScreen
);
222 psc
->__driScreen
= NULL
;
226 dri2GetBuffers(__DRIdrawable
*driDrawable
,
227 int *width
, int *height
,
228 unsigned int *attachments
, int count
,
229 int *out_count
, void *loaderPrivate
)
231 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
232 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(pdraw
->base
.psc
->dpy
);
233 __GLXDRIdisplayPrivate
*pdp
= (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;
238 * Check if a ConfigureNotify has come in since we last asked for the
239 * buffers associated with this drawable. If not, we can assume that they're
240 * the same set at glViewport time, and save a synchronous round-trip to the
243 if (pdraw
->configureSeqno
== pdp
->configureSeqno
&&
244 count
== pdraw
->bufferCount
) {
245 for (i
= 0; i
< count
; i
++) {
246 if (pdraw
->buffers
[i
].attachment
!= attachments
[i
])
250 *out_count
= pdraw
->bufferCount
;
251 return pdraw
->buffers
;
254 pdraw
->configureSeqno
= pdp
->configureSeqno
;
256 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
257 width
, height
, attachments
, count
, out_count
);
261 pdraw
->width
= *width
;
262 pdraw
->height
= *height
;
263 pdraw
->bufferCount
= *out_count
;
265 /* This assumes the DRI2 buffer attachment tokens matches the
266 * __DRIbuffer tokens. */
267 for (i
= 0; i
< *out_count
; i
++) {
268 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
269 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
270 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
271 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
272 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
277 return pdraw
->buffers
;
280 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
281 { __DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
285 static const __DRIextension
*loader_extensions
[] = {
286 &dri2LoaderExtension
.base
,
287 &systemTimeExtension
.base
,
291 static __GLXDRIscreen
*dri2CreateScreen(__GLXscreenConfigs
*psc
, int screen
,
292 __GLXdisplayPrivate
*priv
)
294 const __DRIconfig
**driver_configs
;
295 const __DRIextension
**extensions
;
297 char *driverName
, *deviceName
;
301 psp
= Xmalloc(sizeof *psp
);
305 /* Initialize per screen dynamic client GLX extensions */
306 psc
->ext_list_first_time
= GL_TRUE
;
308 if (!DRI2Connect(psc
->dpy
, RootWindow(psc
->dpy
, screen
),
309 &driverName
, &deviceName
))
312 psc
->driver
= driOpenDriver(driverName
);
313 if (psc
->driver
== NULL
) {
314 ErrorMessageF("driver pointer missing\n");
318 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
319 if (extensions
== NULL
) {
320 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
324 for (i
= 0; extensions
[i
]; i
++) {
325 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
326 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
327 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
328 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
331 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
332 ErrorMessageF("core dri or dri2 extension not found\n");
336 psc
->fd
= open(deviceName
, O_RDWR
);
338 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
342 if (drmGetMagic(psc
->fd
, &magic
)) {
343 ErrorMessageF("failed to get magic\n");
347 if (!DRI2Authenticate(psc
->dpy
, RootWindow(psc
->dpy
, screen
), magic
)) {
348 ErrorMessageF("failed to authenticate magic %d\n", magic
);
353 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
354 loader_extensions
, &driver_configs
, psc
);
355 if (psc
->__driScreen
== NULL
) {
356 ErrorMessageF("failed to create dri screen\n");
360 driBindExtensions(psc
, 1);
362 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
363 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
365 psp
->destroyScreen
= dri2DestroyScreen
;
366 psp
->createContext
= dri2CreateContext
;
367 psp
->createDrawable
= dri2CreateDrawable
;
368 psp
->swapBuffers
= dri2SwapBuffers
;
370 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
372 psp
->copySubBuffer
= dri2CopySubBuffer
;
373 __glXEnableDirectExtension(psc
, "GLX_MESA_copy_sub_buffer");
384 /* FIXME: clean up here */
389 /* Called from __glXFreeDisplayPrivate.
391 static void dri2DestroyDisplay(__GLXDRIdisplay
*dpy
)
397 * Makes a note on receiving ConfigureNotify that we need to re-check the
398 * DRI2 buffers, as window sizes may have resulted in reallocation.
400 static Bool
dri2ConfigureNotifyProc(Display
*dpy
, XEvent
*re
, xEvent
*event
)
402 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(dpy
);
403 __GLXDRIdisplayPrivate
*pdp
;
406 /* We should always be able to find our pdp, as it only gets torn down
407 * when the Display is torn down.
409 pdp
= (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;
411 ret
= pdp
->oldConfigProc(dpy
, re
, event
);
413 pdp
->configureSeqno
= re
->xconfigure
.serial
;
419 * Allocate, initialize and return a __DRIdisplayPrivate object.
420 * This is called from __glXInitialize() when we are given a new
423 _X_HIDDEN __GLXDRIdisplay
*dri2CreateDisplay(Display
*dpy
)
425 __GLXDRIdisplayPrivate
*pdp
;
426 int eventBase
, errorBase
;
428 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
431 pdp
= Xmalloc(sizeof *pdp
);
435 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
440 pdp
->oldConfigProc
= XESetWireToEvent(dpy
, ConfigureNotify
,
441 dri2ConfigureNotifyProc
);
444 pdp
->configureSeqno
= 0;
446 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
447 pdp
->base
.createScreen
= dri2CreateScreen
;
452 #endif /* GLX_DIRECT_RENDERING */