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