Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / glx / x11 / dri2_glx.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
2 /*
3 * Copyright © 2008 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Soft-
7 * ware"), to deal in the Software without restriction, including without
8 * limitation the rights to use, copy, modify, merge, publish, distribute,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, provided that the above copyright
11 * notice(s) and this permission notice appear in all copies of the Soft-
12 * ware and that both the above copyright notice(s) and this permission
13 * notice appear in supporting documentation.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
18 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
19 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
20 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
23 * MANCE OF THIS SOFTWARE.
24 *
25 * Except as contained in this notice, the name of a copyright holder shall
26 * not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization of
28 * the copyright holder.
29 *
30 * Authors:
31 * Kristian Høgsberg (krh@redhat.com)
32 */
33
34 #ifdef GLX_DIRECT_RENDERING
35
36 #include <X11/Xlib.h>
37 #include <X11/extensions/Xfixes.h>
38 #include <X11/extensions/Xdamage.h>
39 #include "glxclient.h"
40 #include "glcontextmodes.h"
41 #include "xf86dri.h"
42 #include <dlfcn.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include "xf86drm.h"
48 #include "dri2.h"
49 #include "dri_common.h"
50
51 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
52 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
53 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
54
55 struct __GLXDRIdisplayPrivateRec {
56 __GLXDRIdisplay base;
57
58 /*
59 ** XFree86-DRI version information
60 */
61 int driMajor;
62 int driMinor;
63 int driPatch;
64 };
65
66 struct __GLXDRIcontextPrivateRec {
67 __GLXDRIcontext base;
68 __DRIcontext *driContext;
69 __GLXscreenConfigs *psc;
70 };
71
72 struct __GLXDRIdrawablePrivateRec {
73 __GLXDRIdrawable base;
74 __DRIbuffer buffers[5];
75 int bufferCount;
76 int width, height;
77 };
78
79 static void dri2DestroyContext(__GLXDRIcontext *context,
80 __GLXscreenConfigs *psc, Display *dpy)
81 {
82 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
83 const __DRIcoreExtension *core = pcp->psc->core;
84
85 (*core->destroyContext)(pcp->driContext);
86
87 Xfree(pcp);
88 }
89
90 static Bool dri2BindContext(__GLXDRIcontext *context,
91 __GLXDRIdrawable *draw, __GLXDRIdrawable *read)
92 {
93 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
94 const __DRIcoreExtension *core = pcp->psc->core;
95
96 return (*core->bindContext)(pcp->driContext,
97 draw->driDrawable,
98 read->driDrawable);
99 }
100
101 static void dri2UnbindContext(__GLXDRIcontext *context)
102 {
103 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
104 const __DRIcoreExtension *core = pcp->psc->core;
105
106 (*core->unbindContext)(pcp->driContext);
107 }
108
109 static __GLXDRIcontext *dri2CreateContext(__GLXscreenConfigs *psc,
110 const __GLcontextModes *mode,
111 GLXContext gc,
112 GLXContext shareList, int renderType)
113 {
114 __GLXDRIcontextPrivate *pcp, *pcp_shared;
115 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
116 __DRIcontext *shared = NULL;
117
118 if (shareList) {
119 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
120 shared = pcp_shared->driContext;
121 }
122
123 pcp = Xmalloc(sizeof *pcp);
124 if (pcp == NULL)
125 return NULL;
126
127 pcp->psc = psc;
128 pcp->driContext =
129 (*psc->dri2->createNewContext)(psc->__driScreen,
130 config->driConfig, shared, pcp);
131 gc->__driContext = pcp->driContext;
132
133 if (pcp->driContext == NULL) {
134 Xfree(pcp);
135 return NULL;
136 }
137
138 pcp->base.destroyContext = dri2DestroyContext;
139 pcp->base.bindContext = dri2BindContext;
140 pcp->base.unbindContext = dri2UnbindContext;
141
142 return &pcp->base;
143 }
144
145 static void dri2DestroyDrawable(__GLXDRIdrawable *pdraw)
146 {
147 const __DRIcoreExtension *core = pdraw->psc->core;
148
149 (*core->destroyDrawable)(pdraw->driDrawable);
150 DRI2DestroyDrawable(pdraw->psc->dpy, pdraw->drawable);
151 Xfree(pdraw);
152 }
153
154 static __GLXDRIdrawable *dri2CreateDrawable(__GLXscreenConfigs *psc,
155 XID xDrawable,
156 GLXDrawable drawable,
157 const __GLcontextModes *modes)
158 {
159 __GLXDRIdrawablePrivate *pdraw;
160 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
161
162 pdraw = Xmalloc(sizeof(*pdraw));
163 if (!pdraw)
164 return NULL;
165
166 pdraw->base.destroyDrawable = dri2DestroyDrawable;
167 pdraw->base.xDrawable = xDrawable;
168 pdraw->base.drawable = drawable;
169 pdraw->base.psc = psc;
170
171 DRI2CreateDrawable(psc->dpy, xDrawable);
172
173 /* Create a new drawable */
174 pdraw->base.driDrawable =
175 (*psc->dri2->createNewDrawable)(psc->__driScreen,
176 config->driConfig, pdraw);
177
178 if (!pdraw->base.driDrawable) {
179 DRI2DestroyDrawable(psc->dpy, drawable);
180 Xfree(pdraw);
181 return NULL;
182 }
183
184 return &pdraw->base;
185 }
186
187 static void dri2CopySubBuffer(__GLXDRIdrawable *pdraw,
188 int x, int y, int width, int height)
189 {
190 XRectangle xrect;
191 XserverRegion region;
192
193 xrect.x = x;
194 xrect.y = y;
195 xrect.width = width;
196 xrect.height = height;
197
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);
202 }
203
204 static void dri2SwapBuffers(__GLXDRIdrawable *pdraw)
205 {
206 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
207
208 dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
209 }
210
211 static void dri2DestroyScreen(__GLXscreenConfigs *psc)
212 {
213 /* Free the direct rendering per screen data */
214 (*psc->core->destroyScreen)(psc->__driScreen);
215 close(psc->fd);
216 psc->__driScreen = NULL;
217 }
218
219 static __DRIbuffer *
220 dri2GetBuffers(__DRIdrawable *driDrawable,
221 int *width, int *height,
222 unsigned int *attachments, int count,
223 int *out_count, void *loaderPrivate)
224 {
225 __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
226 DRI2Buffer *buffers;
227 int i;
228
229 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
230 width, height, attachments, count, out_count);
231 if (buffers == NULL)
232 return NULL;
233
234 pdraw->width = *width;
235 pdraw->height = *height;
236
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;
245 }
246
247 Xfree(buffers);
248
249 return pdraw->buffers;
250 }
251
252 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
253 { __DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION },
254 dri2GetBuffers,
255 };
256
257 static const __DRIextension *loader_extensions[] = {
258 &dri2LoaderExtension.base,
259 &systemTimeExtension.base,
260 NULL
261 };
262
263 static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
264 __GLXdisplayPrivate *priv)
265 {
266 const __DRIconfig **driver_configs;
267 const __DRIextension **extensions;
268 __GLXDRIscreen *psp;
269 char *driverName, *deviceName;
270 drm_magic_t magic;
271 int i;
272
273 psp = Xmalloc(sizeof *psp);
274 if (psp == NULL)
275 return NULL;
276
277 /* Initialize per screen dynamic client GLX extensions */
278 psc->ext_list_first_time = GL_TRUE;
279
280 if (!DRI2Connect(psc->dpy, RootWindow(psc->dpy, screen),
281 &driverName, &deviceName))
282 return NULL;
283
284 psc->driver = driOpenDriver(driverName);
285 if (psc->driver == NULL)
286 goto handle_error;
287
288 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
289 if (extensions == NULL) {
290 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
291 goto handle_error;
292 }
293
294 for (i = 0; extensions[i]; i++) {
295 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
296 psc->core = (__DRIcoreExtension *) extensions[i];
297 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
298 psc->dri2 = (__DRIdri2Extension *) extensions[i];
299 }
300
301 if (psc->core == NULL || psc->dri2 == NULL) {
302 ErrorMessageF("core dri or dri2 extension not found\n");
303 goto handle_error;
304 }
305
306 psc->fd = open(deviceName, O_RDWR);
307 if (psc->fd < 0) {
308 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
309 return NULL;
310 }
311
312 if (drmGetMagic(psc->fd, &magic))
313 return NULL;
314
315 if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic))
316 return NULL;
317
318 psc->__driScreen =
319 psc->dri2->createNewScreen(screen, psc->fd,
320 loader_extensions, &driver_configs, psc);
321 if (psc->__driScreen == NULL) {
322 ErrorMessageF("failed to create dri screen\n");
323 return NULL;
324 }
325
326 driBindExtensions(psc, 1);
327
328 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
329 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
330
331 psp->destroyScreen = dri2DestroyScreen;
332 psp->createContext = dri2CreateContext;
333 psp->createDrawable = dri2CreateDrawable;
334 psp->swapBuffers = dri2SwapBuffers;
335 psp->copySubBuffer = dri2CopySubBuffer;
336
337 Xfree(driverName);
338 Xfree(deviceName);
339
340 return psp;
341
342 handle_error:
343 Xfree(driverName);
344 Xfree(deviceName);
345
346 /* FIXME: clean up here */
347
348 return NULL;
349 }
350
351 /* Called from __glXFreeDisplayPrivate.
352 */
353 static void dri2DestroyDisplay(__GLXDRIdisplay *dpy)
354 {
355 Xfree(dpy);
356 }
357
358 /*
359 * Allocate, initialize and return a __DRIdisplayPrivate object.
360 * This is called from __glXInitialize() when we are given a new
361 * display pointer.
362 */
363 _X_HIDDEN __GLXDRIdisplay *dri2CreateDisplay(Display *dpy)
364 {
365 __GLXDRIdisplayPrivate *pdp;
366 int eventBase, errorBase;
367
368 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
369 return NULL;
370
371 pdp = Xmalloc(sizeof *pdp);
372 if (pdp == NULL)
373 return NULL;
374
375 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
376 Xfree(pdp);
377 return NULL;
378 }
379
380 pdp->driPatch = 0;
381
382 pdp->base.destroyDisplay = dri2DestroyDisplay;
383 pdp->base.createScreen = dri2CreateScreen;
384
385 return &pdp->base;
386 }
387
388 #endif /* GLX_DIRECT_RENDERING */