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
65 struct __GLXDRIcontextPrivateRec
{
67 __DRIcontext
*driContext
;
68 __GLXscreenConfigs
*psc
;
71 struct __GLXDRIdrawablePrivateRec
{
72 __GLXDRIdrawable base
;
73 __DRIbuffer buffers
[5];
78 static void dri2DestroyContext(__GLXDRIcontext
*context
,
79 __GLXscreenConfigs
*psc
, Display
*dpy
)
81 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
82 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
84 (*core
->destroyContext
)(pcp
->driContext
);
89 static Bool
dri2BindContext(__GLXDRIcontext
*context
,
90 __GLXDRIdrawable
*draw
, __GLXDRIdrawable
*read
)
92 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
93 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
95 return (*core
->bindContext
)(pcp
->driContext
,
100 static void dri2UnbindContext(__GLXDRIcontext
*context
)
102 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
103 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
105 (*core
->unbindContext
)(pcp
->driContext
);
108 static __GLXDRIcontext
*dri2CreateContext(__GLXscreenConfigs
*psc
,
109 const __GLcontextModes
*mode
,
111 GLXContext shareList
, int renderType
)
113 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
114 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
115 __DRIcontext
*shared
= NULL
;
118 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
119 shared
= pcp_shared
->driContext
;
122 pcp
= Xmalloc(sizeof *pcp
);
128 (*psc
->dri2
->createNewContext
)(psc
->__driScreen
,
129 config
->driConfig
, shared
, pcp
);
130 gc
->__driContext
= pcp
->driContext
;
132 if (pcp
->driContext
== NULL
) {
137 pcp
->base
.destroyContext
= dri2DestroyContext
;
138 pcp
->base
.bindContext
= dri2BindContext
;
139 pcp
->base
.unbindContext
= dri2UnbindContext
;
144 static void dri2DestroyDrawable(__GLXDRIdrawable
*pdraw
)
146 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
148 (*core
->destroyDrawable
)(pdraw
->driDrawable
);
149 DRI2DestroyDrawable(pdraw
->psc
->dpy
, pdraw
->drawable
);
153 static __GLXDRIdrawable
*dri2CreateDrawable(__GLXscreenConfigs
*psc
,
155 GLXDrawable drawable
,
156 const __GLcontextModes
*modes
)
158 __GLXDRIdrawablePrivate
*pdraw
;
159 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
161 pdraw
= Xmalloc(sizeof(*pdraw
));
165 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
166 pdraw
->base
.xDrawable
= xDrawable
;
167 pdraw
->base
.drawable
= drawable
;
168 pdraw
->base
.psc
= psc
;
170 DRI2CreateDrawable(psc
->dpy
, xDrawable
);
172 /* Create a new drawable */
173 pdraw
->base
.driDrawable
=
174 (*psc
->dri2
->createNewDrawable
)(psc
->__driScreen
,
175 config
->driConfig
, pdraw
);
177 if (!pdraw
->base
.driDrawable
) {
178 DRI2DestroyDrawable(psc
->dpy
, drawable
);
186 static void dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
,
187 int x
, int y
, int width
, int height
)
189 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
191 XserverRegion region
;
194 xrect
.y
= priv
->height
- y
- height
;
196 xrect
.height
= height
;
198 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
199 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->drawable
, region
,
200 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
201 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
204 static void dri2SwapBuffers(__GLXDRIdrawable
*pdraw
)
206 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
208 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
211 static void dri2DestroyScreen(__GLXscreenConfigs
*psc
)
213 /* Free the direct rendering per screen data */
214 (*psc
->core
->destroyScreen
)(psc
->__driScreen
);
216 psc
->__driScreen
= NULL
;
220 dri2GetBuffers(__DRIdrawable
*driDrawable
,
221 int *width
, int *height
,
222 unsigned int *attachments
, int count
,
223 int *out_count
, void *loaderPrivate
)
225 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
229 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
230 width
, height
, attachments
, count
, out_count
);
234 pdraw
->width
= *width
;
235 pdraw
->height
= *height
;
237 /* This assumes the DRI2 buffer attachment tokens matches the
238 * __DRIbuffer tokens. */
239 for (i
= 0; i
< *out_count
; i
++) {
240 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
241 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
242 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
243 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
244 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
249 return pdraw
->buffers
;
252 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
253 { __DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
257 static const __DRIextension
*loader_extensions
[] = {
258 &dri2LoaderExtension
.base
,
259 &systemTimeExtension
.base
,
263 static __GLXDRIscreen
*dri2CreateScreen(__GLXscreenConfigs
*psc
, int screen
,
264 __GLXdisplayPrivate
*priv
)
266 const __DRIconfig
**driver_configs
;
267 const __DRIextension
**extensions
;
269 char *driverName
, *deviceName
;
273 psp
= Xmalloc(sizeof *psp
);
277 /* Initialize per screen dynamic client GLX extensions */
278 psc
->ext_list_first_time
= GL_TRUE
;
280 if (!DRI2Connect(psc
->dpy
, RootWindow(psc
->dpy
, screen
),
281 &driverName
, &deviceName
))
284 psc
->driver
= driOpenDriver(driverName
);
285 if (psc
->driver
== NULL
) {
286 ErrorMessageF("driver pointer missing\n");
290 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
291 if (extensions
== NULL
) {
292 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
296 for (i
= 0; extensions
[i
]; i
++) {
297 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
298 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
299 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
300 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
303 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
304 ErrorMessageF("core dri or dri2 extension not found\n");
308 psc
->fd
= open(deviceName
, O_RDWR
);
310 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
314 if (drmGetMagic(psc
->fd
, &magic
)) {
315 ErrorMessageF("failed to get magic\n");
319 if (!DRI2Authenticate(psc
->dpy
, RootWindow(psc
->dpy
, screen
), magic
)) {
320 ErrorMessageF("failed to authenticate magic %d\n", magic
);
325 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
326 loader_extensions
, &driver_configs
, psc
);
327 if (psc
->__driScreen
== NULL
) {
328 ErrorMessageF("failed to create dri screen\n");
332 driBindExtensions(psc
, 1);
334 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
335 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
337 psp
->destroyScreen
= dri2DestroyScreen
;
338 psp
->createContext
= dri2CreateContext
;
339 psp
->createDrawable
= dri2CreateDrawable
;
340 psp
->swapBuffers
= dri2SwapBuffers
;
342 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
344 psp
->copySubBuffer
= dri2CopySubBuffer
;
345 __glXEnableDirectExtension(psc
, "GLX_MESA_copy_sub_buffer");
356 /* FIXME: clean up here */
361 /* Called from __glXFreeDisplayPrivate.
363 static void dri2DestroyDisplay(__GLXDRIdisplay
*dpy
)
369 * Allocate, initialize and return a __DRIdisplayPrivate object.
370 * This is called from __glXInitialize() when we are given a new
373 _X_HIDDEN __GLXDRIdisplay
*dri2CreateDisplay(Display
*dpy
)
375 __GLXDRIdisplayPrivate
*pdp
;
376 int eventBase
, errorBase
;
378 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
381 pdp
= Xmalloc(sizeof *pdp
);
385 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
392 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
393 pdp
->base
.createScreen
= dri2CreateScreen
;
398 #endif /* GLX_DIRECT_RENDERING */