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