mesa: merge gallium-0.2 into gallium-master-merge
[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 <dlfcn.h>
42 #include <fcntl.h>
43 #include <unistd.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 unsigned long configureSeqno;
65 Bool (*oldConfigProc)(Display *, XEvent *, xEvent *);
66 };
67
68 struct __GLXDRIcontextPrivateRec {
69 __GLXDRIcontext base;
70 __DRIcontext *driContext;
71 __GLXscreenConfigs *psc;
72 };
73
74 struct __GLXDRIdrawablePrivateRec {
75 __GLXDRIdrawable base;
76 __DRIbuffer buffers[5];
77 int bufferCount;
78 int width, height;
79 unsigned long configureSeqno;
80 };
81
82 static void 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 dri2BindContext(__GLXDRIcontext *context,
94 __GLXDRIdrawable *draw, __GLXDRIdrawable *read)
95 {
96 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
97 const __DRIcoreExtension *core = pcp->psc->core;
98
99 return (*core->bindContext)(pcp->driContext,
100 draw->driDrawable,
101 read->driDrawable);
102 }
103
104 static void dri2UnbindContext(__GLXDRIcontext *context)
105 {
106 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
107 const __DRIcoreExtension *core = pcp->psc->core;
108
109 (*core->unbindContext)(pcp->driContext);
110 }
111
112 static __GLXDRIcontext *dri2CreateContext(__GLXscreenConfigs *psc,
113 const __GLcontextModes *mode,
114 GLXContext gc,
115 GLXContext shareList, int renderType)
116 {
117 __GLXDRIcontextPrivate *pcp, *pcp_shared;
118 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
119 __DRIcontext *shared = NULL;
120
121 if (shareList) {
122 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
123 shared = pcp_shared->driContext;
124 }
125
126 pcp = Xmalloc(sizeof *pcp);
127 if (pcp == NULL)
128 return NULL;
129
130 pcp->psc = psc;
131 pcp->driContext =
132 (*psc->dri2->createNewContext)(psc->__driScreen,
133 config->driConfig, shared, pcp);
134 gc->__driContext = pcp->driContext;
135
136 if (pcp->driContext == NULL) {
137 Xfree(pcp);
138 return NULL;
139 }
140
141 pcp->base.destroyContext = dri2DestroyContext;
142 pcp->base.bindContext = dri2BindContext;
143 pcp->base.unbindContext = dri2UnbindContext;
144
145 return &pcp->base;
146 }
147
148 static void dri2DestroyDrawable(__GLXDRIdrawable *pdraw)
149 {
150 const __DRIcoreExtension *core = pdraw->psc->core;
151
152 (*core->destroyDrawable)(pdraw->driDrawable);
153 DRI2DestroyDrawable(pdraw->psc->dpy, pdraw->drawable);
154 Xfree(pdraw);
155 }
156
157 static __GLXDRIdrawable *dri2CreateDrawable(__GLXscreenConfigs *psc,
158 XID xDrawable,
159 GLXDrawable drawable,
160 const __GLcontextModes *modes)
161 {
162 __GLXDRIdrawablePrivate *pdraw;
163 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
164
165 pdraw = Xmalloc(sizeof(*pdraw));
166 if (!pdraw)
167 return NULL;
168
169 pdraw->base.destroyDrawable = dri2DestroyDrawable;
170 pdraw->base.xDrawable = xDrawable;
171 pdraw->base.drawable = drawable;
172 pdraw->base.psc = psc;
173 pdraw->configureSeqno = ~0;
174
175 DRI2CreateDrawable(psc->dpy, xDrawable);
176
177 /* Create a new drawable */
178 pdraw->base.driDrawable =
179 (*psc->dri2->createNewDrawable)(psc->__driScreen,
180 config->driConfig, pdraw);
181
182 if (!pdraw->base.driDrawable) {
183 DRI2DestroyDrawable(psc->dpy, drawable);
184 Xfree(pdraw);
185 return NULL;
186 }
187
188 return &pdraw->base;
189 }
190
191 static void dri2CopySubBuffer(__GLXDRIdrawable *pdraw,
192 int x, int y, int width, int height)
193 {
194 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
195 XRectangle xrect;
196 XserverRegion region;
197
198 xrect.x = x;
199 xrect.y = priv->height - y - height;
200 xrect.width = width;
201 xrect.height = height;
202
203 region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
204 DRI2CopyRegion(pdraw->psc->dpy, pdraw->drawable, region,
205 DRI2BufferFrontLeft, DRI2BufferBackLeft);
206 XFixesDestroyRegion(pdraw->psc->dpy, region);
207 }
208
209 static void dri2SwapBuffers(__GLXDRIdrawable *pdraw)
210 {
211 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
212
213 dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
214 }
215
216 static void dri2DestroyScreen(__GLXscreenConfigs *psc)
217 {
218 /* Free the direct rendering per screen data */
219 (*psc->core->destroyScreen)(psc->__driScreen);
220 close(psc->fd);
221 psc->__driScreen = NULL;
222 }
223
224 static __DRIbuffer *
225 dri2GetBuffers(__DRIdrawable *driDrawable,
226 int *width, int *height,
227 unsigned int *attachments, int count,
228 int *out_count, void *loaderPrivate)
229 {
230 __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
231 __GLXdisplayPrivate *dpyPriv = __glXInitialize(pdraw->base.psc->dpy);
232 __GLXDRIdisplayPrivate *pdp = (__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;
233 DRI2Buffer *buffers;
234 int i;
235
236 /**
237 * Check if a ConfigureNotify has come in since we last asked for the
238 * buffers associated with this drawable. If not, we can assume that they're
239 * the same set at glViewport time, and save a synchronous round-trip to the
240 * X Server.
241 */
242 if (pdraw->configureSeqno == pdp->configureSeqno &&
243 count == pdraw->bufferCount) {
244 for (i = 0; i < count; i++) {
245 if (pdraw->buffers[i].attachment != attachments[i])
246 break;
247 }
248 if (i == count) {
249 *out_count = pdraw->bufferCount;
250 return pdraw->buffers;
251 }
252 }
253 pdraw->configureSeqno = pdp->configureSeqno;
254
255 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
256 width, height, attachments, count, out_count);
257 if (buffers == NULL)
258 return NULL;
259
260 pdraw->width = *width;
261 pdraw->height = *height;
262 pdraw->bufferCount = *out_count;
263
264 /* This assumes the DRI2 buffer attachment tokens matches the
265 * __DRIbuffer tokens. */
266 for (i = 0; i < *out_count; i++) {
267 pdraw->buffers[i].attachment = buffers[i].attachment;
268 pdraw->buffers[i].name = buffers[i].name;
269 pdraw->buffers[i].pitch = buffers[i].pitch;
270 pdraw->buffers[i].cpp = buffers[i].cpp;
271 pdraw->buffers[i].flags = buffers[i].flags;
272 }
273
274 Xfree(buffers);
275
276 return pdraw->buffers;
277 }
278
279 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
280 { __DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION },
281 dri2GetBuffers,
282 };
283
284 static const __DRIextension *loader_extensions[] = {
285 &dri2LoaderExtension.base,
286 &systemTimeExtension.base,
287 NULL
288 };
289
290 static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
291 __GLXdisplayPrivate *priv)
292 {
293 const __DRIconfig **driver_configs;
294 const __DRIextension **extensions;
295 __GLXDRIscreen *psp;
296 char *driverName, *deviceName;
297 drm_magic_t magic;
298 int i;
299
300 psp = Xmalloc(sizeof *psp);
301 if (psp == NULL)
302 return NULL;
303
304 /* Initialize per screen dynamic client GLX extensions */
305 psc->ext_list_first_time = GL_TRUE;
306
307 if (!DRI2Connect(psc->dpy, RootWindow(psc->dpy, screen),
308 &driverName, &deviceName))
309 return NULL;
310
311 psc->driver = driOpenDriver(driverName);
312 if (psc->driver == NULL) {
313 ErrorMessageF("driver pointer missing\n");
314 goto handle_error;
315 }
316
317 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
318 if (extensions == NULL) {
319 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
320 goto handle_error;
321 }
322
323 for (i = 0; extensions[i]; i++) {
324 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
325 psc->core = (__DRIcoreExtension *) extensions[i];
326 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
327 psc->dri2 = (__DRIdri2Extension *) extensions[i];
328 }
329
330 if (psc->core == NULL || psc->dri2 == NULL) {
331 ErrorMessageF("core dri or dri2 extension not found\n");
332 goto handle_error;
333 }
334
335 psc->fd = open(deviceName, O_RDWR);
336 if (psc->fd < 0) {
337 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
338 return NULL;
339 }
340
341 if (drmGetMagic(psc->fd, &magic)) {
342 ErrorMessageF("failed to get magic\n");
343 return NULL;
344 }
345
346 if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic)) {
347 ErrorMessageF("failed to authenticate magic %d\n", magic);
348 return NULL;
349 }
350
351 psc->__driScreen =
352 psc->dri2->createNewScreen(screen, psc->fd,
353 loader_extensions, &driver_configs, psc);
354 if (psc->__driScreen == NULL) {
355 ErrorMessageF("failed to create dri screen\n");
356 return NULL;
357 }
358
359 driBindExtensions(psc, 1);
360
361 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
362 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
363
364 psp->destroyScreen = dri2DestroyScreen;
365 psp->createContext = dri2CreateContext;
366 psp->createDrawable = dri2CreateDrawable;
367 psp->swapBuffers = dri2SwapBuffers;
368
369 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
370 * available.*/
371 psp->copySubBuffer = dri2CopySubBuffer;
372 __glXEnableDirectExtension(psc, "GLX_MESA_copy_sub_buffer");
373
374 Xfree(driverName);
375 Xfree(deviceName);
376
377 return psp;
378
379 handle_error:
380 Xfree(driverName);
381 Xfree(deviceName);
382
383 /* FIXME: clean up here */
384
385 return NULL;
386 }
387
388 /* Called from __glXFreeDisplayPrivate.
389 */
390 static void dri2DestroyDisplay(__GLXDRIdisplay *dpy)
391 {
392 Xfree(dpy);
393 }
394
395 /**
396 * Makes a note on receiving ConfigureNotify that we need to re-check the
397 * DRI2 buffers, as window sizes may have resulted in reallocation.
398 */
399 static Bool dri2ConfigureNotifyProc(Display *dpy, XEvent *re, xEvent *event)
400 {
401 __GLXdisplayPrivate *dpyPriv = __glXInitialize(dpy);
402 __GLXDRIdisplayPrivate *pdp;
403 Bool ret;
404
405 /* We should always be able to find our pdp, as it only gets torn down
406 * when the Display is torn down.
407 */
408 pdp = (__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;
409
410 ret = pdp->oldConfigProc(dpy, re, event);
411
412 pdp->configureSeqno = re->xconfigure.serial;
413
414 return ret;
415 }
416
417 /*
418 * Allocate, initialize and return a __DRIdisplayPrivate object.
419 * This is called from __glXInitialize() when we are given a new
420 * display pointer.
421 */
422 _X_HIDDEN __GLXDRIdisplay *dri2CreateDisplay(Display *dpy)
423 {
424 __GLXDRIdisplayPrivate *pdp;
425 int eventBase, errorBase;
426
427 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
428 return NULL;
429
430 pdp = Xmalloc(sizeof *pdp);
431 if (pdp == NULL)
432 return NULL;
433
434 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
435 Xfree(pdp);
436 return NULL;
437 }
438
439 pdp->oldConfigProc = XESetWireToEvent(dpy, ConfigureNotify,
440 dri2ConfigureNotifyProc);
441
442 pdp->driPatch = 0;
443
444 pdp->base.destroyDisplay = dri2DestroyDisplay;
445 pdp->base.createScreen = dri2CreateScreen;
446
447 return &pdp->base;
448 }
449
450 #endif /* GLX_DIRECT_RENDERING */