drisw_glx.c revision 29ec84b0a80e1fe2e6f58f91ab63f2f9ebd012a6
1/*
2 * Copyright 2008 George Sapountzis
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#ifdef GLX_DIRECT_RENDERING
25
26#include <X11/Xlib.h>
27#include "glxclient.h"
28#include <dlfcn.h>
29#include "dri_common.h"
30
31typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
32typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
33typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
34
35struct __GLXDRIdisplayPrivateRec
36{
37   __GLXDRIdisplay base;
38};
39
40struct __GLXDRIcontextPrivateRec
41{
42   __GLXDRIcontext base;
43   __DRIcontext *driContext;
44   __GLXscreenConfigs *psc;
45};
46
47struct __GLXDRIdrawablePrivateRec
48{
49   __GLXDRIdrawable base;
50
51   GC gc;
52   GC swapgc;
53
54   XVisualInfo *visinfo;
55   XImage *ximage;
56   int bpp;
57};
58
59/**
60 * swrast loader functions
61 */
62
63static Bool
64XCreateDrawable(__GLXDRIdrawablePrivate * pdp,
65                Display * dpy, XID drawable, int visualid)
66{
67   XGCValues gcvalues;
68   long visMask;
69   XVisualInfo visTemp;
70   int num_visuals;
71
72   /* create GC's */
73   pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
74   pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
75
76   gcvalues.function = GXcopy;
77   gcvalues.graphics_exposures = False;
78   XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
79   XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
80   XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
81
82   /* create XImage  */
83   visTemp.screen = DefaultScreen(dpy);
84   visTemp.visualid = visualid;
85   visMask = (VisualScreenMask | VisualIDMask);
86   pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
87
88   pdp->ximage = XCreateImage(dpy,
89                              pdp->visinfo->visual,
90                              pdp->visinfo->depth,
91                              ZPixmap, 0,             /* format, offset */
92                              NULL,                   /* data */
93                              0, 0,                   /* size */
94                              32,                     /* bitmap_pad */
95                              0);                     /* bytes_per_line */
96
97   /* get the true number of bits per pixel */
98   pdp->bpp = pdp->ximage->bits_per_pixel;
99
100   return True;
101}
102
103static void
104XDestroyDrawable(__GLXDRIdrawablePrivate * pdp, Display * dpy, XID drawable)
105{
106   XDestroyImage(pdp->ximage);
107   XFree(pdp->visinfo);
108
109   XFreeGC(dpy, pdp->gc);
110   XFreeGC(dpy, pdp->swapgc);
111}
112
113static void
114swrastGetDrawableInfo(__DRIdrawable * draw,
115                      int *x, int *y, int *w, int *h, void *loaderPrivate)
116{
117   __GLXDRIdrawablePrivate *pdp = loaderPrivate;
118   __GLXDRIdrawable *pdraw = &(pdp->base);
119   Display *dpy = pdraw->psc->dpy;
120   Drawable drawable;
121
122   Window root;
123   Status stat;
124   unsigned int bw, depth;
125
126   drawable = pdraw->xDrawable;
127
128   stat = XGetGeometry(dpy, drawable, &root,
129                       x, y, (unsigned int *) w, (unsigned int *) h,
130                       &bw, &depth);
131}
132
133static inline int
134bytes_per_line(int w, int bpp, unsigned mul)
135{
136   unsigned mask = mul - 1;
137
138   return ((w * bpp + mask) & ~mask) / 8;
139}
140
141static void
142swrastPutImage(__DRIdrawable * draw, int op,
143               int x, int y, int w, int h, char *data, void *loaderPrivate)
144{
145   __GLXDRIdrawablePrivate *pdp = loaderPrivate;
146   __GLXDRIdrawable *pdraw = &(pdp->base);
147   Display *dpy = pdraw->psc->dpy;
148   Drawable drawable;
149   XImage *ximage;
150   GC gc;
151
152   switch (op) {
153   case __DRI_SWRAST_IMAGE_OP_DRAW:
154      gc = pdp->gc;
155      break;
156   case __DRI_SWRAST_IMAGE_OP_SWAP:
157      gc = pdp->swapgc;
158      break;
159   default:
160      return;
161   }
162
163   drawable = pdraw->xDrawable;
164
165   ximage = pdp->ximage;
166   ximage->data = data;
167   ximage->width = w;
168   ximage->height = h;
169   ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32);
170
171   XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
172
173   ximage->data = NULL;
174}
175
176static void
177swrastGetImage(__DRIdrawable * draw,
178               int x, int y, int w, int h, char *data, void *loaderPrivate)
179{
180   __GLXDRIdrawablePrivate *pdp = loaderPrivate;
181   __GLXDRIdrawable *pdraw = &(pdp->base);
182   Display *dpy = pdraw->psc->dpy;
183   Drawable drawable;
184   XImage *ximage;
185
186   drawable = pdraw->xDrawable;
187
188   ximage = pdp->ximage;
189   ximage->data = data;
190   ximage->width = w;
191   ximage->height = h;
192   ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32);
193
194   XGetSubImage(dpy, drawable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
195
196   ximage->data = NULL;
197}
198
199static const __DRIswrastLoaderExtension swrastLoaderExtension = {
200   {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
201   swrastGetDrawableInfo,
202   swrastPutImage,
203   swrastGetImage
204};
205
206static const __DRIextension *loader_extensions[] = {
207   &systemTimeExtension.base,
208   &swrastLoaderExtension.base,
209   NULL
210};
211
212/**
213 * GLXDRI functions
214 */
215
216static void
217driDestroyContext(__GLXDRIcontext * context,
218                  __GLXscreenConfigs * psc, Display * dpy)
219{
220   __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
221   const __DRIcoreExtension *core = pcp->psc->core;
222
223   (*core->destroyContext) (pcp->driContext);
224
225   Xfree(pcp);
226}
227
228static Bool
229driBindContext(__GLXDRIcontext * context,
230               __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
231{
232   __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
233   const __DRIcoreExtension *core = pcp->psc->core;
234
235   return (*core->bindContext) (pcp->driContext,
236                                draw->driDrawable, read->driDrawable);
237}
238
239static void
240driUnbindContext(__GLXDRIcontext * context)
241{
242   __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
243   const __DRIcoreExtension *core = pcp->psc->core;
244
245   (*core->unbindContext) (pcp->driContext);
246}
247
248static __GLXDRIcontext *
249driCreateContext(__GLXscreenConfigs * psc,
250                 const __GLcontextModes * mode,
251                 GLXContext gc, GLXContext shareList, int renderType)
252{
253   __GLXDRIcontextPrivate *pcp, *pcp_shared;
254   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
255   const __DRIcoreExtension *core;
256   __DRIcontext *shared = NULL;
257
258   if (!psc || !psc->driScreen)
259      return NULL;
260
261   core = psc->core;
262
263   if (shareList) {
264      pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
265      shared = pcp_shared->driContext;
266   }
267
268   pcp = Xmalloc(sizeof *pcp);
269   if (pcp == NULL)
270      return NULL;
271
272   pcp->psc = psc;
273   pcp->driContext =
274      (*core->createNewContext) (psc->__driScreen,
275                                 config->driConfig, shared, pcp);
276   if (pcp->driContext == NULL) {
277      Xfree(pcp);
278      return NULL;
279   }
280
281   pcp->base.destroyContext = driDestroyContext;
282   pcp->base.bindContext = driBindContext;
283   pcp->base.unbindContext = driUnbindContext;
284
285   return &pcp->base;
286}
287
288static void
289driDestroyDrawable(__GLXDRIdrawable * pdraw)
290{
291   __GLXDRIdrawablePrivate *pdp = (__GLXDRIdrawablePrivate *) pdraw;
292   const __DRIcoreExtension *core = pdraw->psc->core;
293
294   (*core->destroyDrawable) (pdraw->driDrawable);
295
296   XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
297   Xfree(pdp);
298}
299
300static __GLXDRIdrawable *
301driCreateDrawable(__GLXscreenConfigs * psc,
302                  XID xDrawable,
303                  GLXDrawable drawable, const __GLcontextModes * modes)
304{
305   __GLXDRIdrawable *pdraw;
306   __GLXDRIdrawablePrivate *pdp;
307   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
308   const __DRIswrastExtension *swrast = psc->swrast;
309
310   /* Old dri can't handle GLX 1.3+ drawable constructors. */
311   if (xDrawable != drawable)
312      return NULL;
313
314   pdp = Xmalloc(sizeof(*pdp));
315   if (!pdp)
316      return NULL;
317
318   pdraw = &(pdp->base);
319   pdraw->xDrawable = xDrawable;
320   pdraw->drawable = drawable;
321   pdraw->psc = psc;
322
323   XCreateDrawable(pdp, psc->dpy, xDrawable, modes->visualID);
324
325   /* Create a new drawable */
326   pdraw->driDrawable =
327      (*swrast->createNewDrawable) (psc->__driScreen, config->driConfig, pdp);
328
329   if (!pdraw->driDrawable) {
330      XDestroyDrawable(pdp, psc->dpy, xDrawable);
331      Xfree(pdp);
332      return NULL;
333   }
334
335   pdraw->destroyDrawable = driDestroyDrawable;
336
337   return pdraw;
338}
339
340static int64_t
341driSwapBuffers(__GLXDRIdrawable * pdraw,
342               int64_t target_msc, int64_t divisor, int64_t remainder)
343{
344   (void) target_msc;
345   (void) divisor;
346   (void) remainder;
347
348   (*pdraw->psc->core->swapBuffers) (pdraw->driDrawable);
349
350   return 0;
351}
352
353static void
354driDestroyScreen(__GLXscreenConfigs * psc)
355{
356   /* Free the direct rendering per screen data */
357   (*psc->core->destroyScreen) (psc->__driScreen);
358   psc->__driScreen = NULL;
359   if (psc->driver)
360      dlclose(psc->driver);
361}
362
363static void *
364driOpenSwrast(void)
365{
366   void *driver = NULL;
367
368   if (driver == NULL)
369      driver = driOpenDriver("swrast");
370
371   if (driver == NULL)
372      driver = driOpenDriver("swrastg");
373
374   return driver;
375}
376
377static __GLXDRIscreen *
378driCreateScreen(__GLXscreenConfigs * psc, int screen,
379                __GLXdisplayPrivate * priv)
380{
381   __GLXDRIscreen *psp;
382   const __DRIconfig **driver_configs;
383   const __DRIextension **extensions;
384   int i;
385
386   psp = Xcalloc(1, sizeof *psp);
387   if (psp == NULL)
388      return NULL;
389
390   psc->driver = driOpenSwrast();
391   if (psc->driver == NULL)
392      goto handle_error;
393
394   extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
395   if (extensions == NULL) {
396      ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
397      goto handle_error;
398   }
399
400   for (i = 0; extensions[i]; i++) {
401      if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
402         psc->core = (__DRIcoreExtension *) extensions[i];
403      if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
404         psc->swrast = (__DRIswrastExtension *) extensions[i];
405   }
406
407   if (psc->core == NULL || psc->swrast == NULL) {
408      ErrorMessageF("core dri extension not found\n");
409      goto handle_error;
410   }
411
412   psc->__driScreen =
413      psc->swrast->createNewScreen(screen,
414                                   loader_extensions, &driver_configs, psc);
415   if (psc->__driScreen == NULL) {
416      ErrorMessageF("failed to create dri screen\n");
417      goto handle_error;
418   }
419
420   driBindExtensions(psc);
421   driBindCommonExtensions(psc);
422
423   psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
424   psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
425
426   psc->driver_configs = driver_configs;
427
428   psp->destroyScreen = driDestroyScreen;
429   psp->createContext = driCreateContext;
430   psp->createDrawable = driCreateDrawable;
431   psp->swapBuffers = driSwapBuffers;
432   psp->waitX = NULL;
433   psp->waitGL = NULL;
434
435   return psp;
436
437 handle_error:
438   Xfree(psp);
439
440   if (psc->driver)
441      dlclose(psc->driver);
442
443   ErrorMessageF("reverting to indirect rendering\n");
444
445   return NULL;
446}
447
448/* Called from __glXFreeDisplayPrivate.
449 */
450static void
451driDestroyDisplay(__GLXDRIdisplay * dpy)
452{
453   Xfree(dpy);
454}
455
456/*
457 * Allocate, initialize and return a __DRIdisplayPrivate object.
458 * This is called from __glXInitialize() when we are given a new
459 * display pointer.
460 */
461_X_HIDDEN __GLXDRIdisplay *
462driswCreateDisplay(Display * dpy)
463{
464   __GLXDRIdisplayPrivate *pdpyp;
465
466   pdpyp = Xmalloc(sizeof *pdpyp);
467   if (pdpyp == NULL)
468      return NULL;
469
470   pdpyp->base.destroyDisplay = driDestroyDisplay;
471   pdpyp->base.createScreen = driCreateScreen;
472
473   return &pdpyp->base;
474}
475
476#endif /* GLX_DIRECT_RENDERING */
477