Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / glx / x11 / dri2_glx.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
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.
13 *
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.
23 *
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.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33 #ifdef GLX_DIRECT_RENDERING
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/extensions/Xdamage.h>
38 #include "glxclient.h"
39 #include "glcontextmodes.h"
40 #include "xf86dri.h"
41 #include "sarea.h"
42 #include <dlfcn.h>
43 #include <sys/types.h>
44 #include <sys/mman.h>
45 #include "xf86drm.h"
46 #include "dri2.h"
47 #include "dri_common.h"
48
49 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
50 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
51 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
52
53 struct __GLXDRIdisplayPrivateRec {
54 __GLXDRIdisplay base;
55
56 /*
57 ** XFree86-DRI version information
58 */
59 int driMajor;
60 int driMinor;
61 int driPatch;
62 };
63
64 struct __GLXDRIcontextPrivateRec {
65 __GLXDRIcontext base;
66 __DRIcontext *driContext;
67 __GLXscreenConfigs *psc;
68 };
69
70 struct __GLXDRIdrawablePrivateRec {
71 __GLXDRIdrawable base;
72 __DRIbuffer buffers[5];
73 int bufferCount;
74 int width, height;
75 };
76
77 static void dri2DestroyContext(__GLXDRIcontext *context,
78 __GLXscreenConfigs *psc, Display *dpy)
79 {
80 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
81 const __DRIcoreExtension *core = pcp->psc->core;
82
83 (*core->destroyContext)(pcp->driContext);
84
85 Xfree(pcp);
86 }
87
88 static Bool dri2BindContext(__GLXDRIcontext *context,
89 __GLXDRIdrawable *draw, __GLXDRIdrawable *read)
90 {
91 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
92 const __DRIcoreExtension *core = pcp->psc->core;
93
94 return (*core->bindContext)(pcp->driContext,
95 draw->driDrawable,
96 read->driDrawable);
97 }
98
99 static void dri2UnbindContext(__GLXDRIcontext *context)
100 {
101 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
102 const __DRIcoreExtension *core = pcp->psc->core;
103
104 (*core->unbindContext)(pcp->driContext);
105 }
106
107 static __GLXDRIcontext *dri2CreateContext(__GLXscreenConfigs *psc,
108 const __GLcontextModes *mode,
109 GLXContext gc,
110 GLXContext shareList, int renderType)
111 {
112 __GLXDRIcontextPrivate *pcp, *pcp_shared;
113 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
114 __DRIcontext *shared = NULL;
115
116 if (shareList) {
117 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
118 shared = pcp_shared->driContext;
119 }
120
121 pcp = Xmalloc(sizeof *pcp);
122 if (pcp == NULL)
123 return NULL;
124
125 pcp->psc = psc;
126 pcp->driContext =
127 (*psc->dri2->createNewContext)(psc->__driScreen,
128 config->driConfig, shared, pcp);
129 gc->__driContext = pcp->driContext;
130
131 if (pcp->driContext == NULL) {
132 Xfree(pcp);
133 return NULL;
134 }
135
136 pcp->base.destroyContext = dri2DestroyContext;
137 pcp->base.bindContext = dri2BindContext;
138 pcp->base.unbindContext = dri2UnbindContext;
139
140 return &pcp->base;
141 }
142
143 static void dri2DestroyDrawable(__GLXDRIdrawable *pdraw)
144 {
145 const __DRIcoreExtension *core = pdraw->psc->core;
146
147 (*core->destroyDrawable)(pdraw->driDrawable);
148 DRI2DestroyDrawable(pdraw->psc->dpy, pdraw->drawable);
149 Xfree(pdraw);
150 }
151
152 static __GLXDRIdrawable *dri2CreateDrawable(__GLXscreenConfigs *psc,
153 XID xDrawable,
154 GLXDrawable drawable,
155 const __GLcontextModes *modes)
156 {
157 __GLXDRIdrawablePrivate *pdraw;
158 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
159
160 pdraw = Xmalloc(sizeof(*pdraw));
161 if (!pdraw)
162 return NULL;
163
164 pdraw->base.destroyDrawable = dri2DestroyDrawable;
165 pdraw->base.xDrawable = xDrawable;
166 pdraw->base.drawable = drawable;
167 pdraw->base.psc = psc;
168
169 DRI2CreateDrawable(psc->dpy, xDrawable);
170
171 /* Create a new drawable */
172 pdraw->base.driDrawable =
173 (*psc->dri2->createNewDrawable)(psc->__driScreen,
174 config->driConfig, pdraw);
175
176 if (!pdraw->base.driDrawable) {
177 DRI2DestroyDrawable(psc->dpy, drawable);
178 Xfree(pdraw);
179 return NULL;
180 }
181
182 return &pdraw->base;
183 }
184
185 static void dri2SwapBuffers(__GLXDRIdrawable *pdraw)
186 {
187 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
188
189 DRI2SwapBuffers(pdraw->psc->dpy, pdraw->drawable,
190 0, 0, priv->width, priv->height);
191 }
192
193 static void dri2DestroyScreen(__GLXscreenConfigs *psc)
194 {
195 /* Free the direct rendering per screen data */
196 (*psc->core->destroyScreen)(psc->__driScreen);
197 drmClose(psc->fd);
198 psc->__driScreen = NULL;
199 }
200
201 static __DRIbuffer *
202 dri2GetBuffers(__DRIdrawable *driDrawable,
203 int *width, int *height,
204 unsigned int *attachments, int count,
205 int *out_count, void *loaderPrivate)
206 {
207 __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
208 DRI2Buffer *buffers;
209 int i;
210
211 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
212 width, height, attachments, count, out_count);
213 if (buffers == NULL)
214 return NULL;
215
216 pdraw->width = *width;
217 pdraw->height = *height;
218
219 /* This assumes the DRI2 buffer attachment tokens matches the
220 * __DRIbuffer tokens. */
221 for (i = 0; i < *out_count; i++) {
222 pdraw->buffers[i].attachment = buffers[i].attachment;
223 pdraw->buffers[i].name = buffers[i].name;
224 pdraw->buffers[i].pitch = buffers[i].pitch;
225 pdraw->buffers[i].cpp = buffers[i].cpp;
226 pdraw->buffers[i].flags = buffers[i].flags;
227 }
228
229 Xfree(buffers);
230
231 return pdraw->buffers;
232 }
233
234 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
235 { __DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION },
236 dri2GetBuffers,
237 };
238
239 static const __DRIextension *loader_extensions[] = {
240 &dri2LoaderExtension.base,
241 &systemTimeExtension.base,
242 NULL
243 };
244
245 static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
246 __GLXdisplayPrivate *priv)
247 {
248 const __DRIconfig **driver_configs;
249 const __DRIextension **extensions;
250 __GLXDRIscreen *psp;
251 unsigned int sareaHandle;
252 char *driverName, *busID;
253 drm_magic_t magic;
254 int i;
255
256 psp = Xmalloc(sizeof *psp);
257 if (psp == NULL)
258 return NULL;
259
260 /* Initialize per screen dynamic client GLX extensions */
261 psc->ext_list_first_time = GL_TRUE;
262
263 if (!DRI2Connect(psc->dpy, screen, &driverName, &busID, &sareaHandle))
264 return NULL;
265
266 psc->driver = driOpenDriver(driverName);
267 if (psc->driver == NULL)
268 goto handle_error;
269
270 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
271 if (extensions == NULL) {
272 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
273 goto handle_error;
274 }
275
276 for (i = 0; extensions[i]; i++) {
277 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
278 psc->core = (__DRIcoreExtension *) extensions[i];
279 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
280 psc->dri2 = (__DRIdri2Extension *) extensions[i];
281 }
282
283 if (psc->core == NULL || psc->dri2 == NULL) {
284 ErrorMessageF("core dri or dri2 extension not found\n");
285 goto handle_error;
286 }
287
288 psc->fd = drmOpen(NULL, busID);
289 if (psc->fd < 0) {
290 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
291 return NULL;
292 }
293
294 if (drmGetMagic(psc->fd, &magic))
295 return NULL;
296
297 if (!DRI2AuthConnection(psc->dpy, screen, magic)) {
298 ErrorMessageF("failed to authenticate drm access\n");
299 return NULL;
300 }
301
302 psc->__driScreen =
303 psc->dri2->createNewScreen(screen, psc->fd,
304 loader_extensions, &driver_configs, psc);
305 if (psc->__driScreen == NULL) {
306 ErrorMessageF("failed to create dri screen\n");
307 return NULL;
308 }
309
310 driBindExtensions(psc, 1);
311
312 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
313 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
314
315 psp->destroyScreen = dri2DestroyScreen;
316 psp->createContext = dri2CreateContext;
317 psp->createDrawable = dri2CreateDrawable;
318 psp->swapBuffers = dri2SwapBuffers;
319
320 Xfree(driverName);
321 Xfree(busID);
322
323 return psp;
324
325 handle_error:
326 Xfree(driverName);
327 Xfree(busID);
328
329 /* FIXME: clean up here */
330
331 return NULL;
332 }
333
334 /* Called from __glXFreeDisplayPrivate.
335 */
336 static void dri2DestroyDisplay(__GLXDRIdisplay *dpy)
337 {
338 Xfree(dpy);
339 }
340
341 /*
342 * Allocate, initialize and return a __DRIdisplayPrivate object.
343 * This is called from __glXInitialize() when we are given a new
344 * display pointer.
345 */
346 _X_HIDDEN __GLXDRIdisplay *dri2CreateDisplay(Display *dpy)
347 {
348 __GLXDRIdisplayPrivate *pdp;
349 int eventBase, errorBase;
350
351 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
352 return NULL;
353
354 pdp = Xmalloc(sizeof *pdp);
355 if (pdp == NULL)
356 return NULL;
357
358 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
359 Xfree(pdp);
360 return NULL;
361 }
362
363 pdp->driPatch = 0;
364
365 pdp->base.destroyDisplay = dri2DestroyDisplay;
366 pdp->base.createScreen = dri2CreateScreen;
367
368 return &pdp->base;
369 }
370
371 #endif /* GLX_DIRECT_RENDERING */