drisw_glx.c revision 5016b09f897dd9d93ee1fc86949e0c3d7bd3b36f
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#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
25
26#include <X11/Xlib.h>
27#include "glxclient.h"
28#include <dlfcn.h>
29#include "dri_common.h"
30
31struct drisw_display
32{
33   __GLXDRIdisplay base;
34};
35
36struct drisw_context
37{
38   struct glx_context base;
39   __DRIcontext *driContext;
40
41};
42
43struct drisw_screen
44{
45   struct glx_screen base;
46
47   __DRIscreen *driScreen;
48   __GLXDRIscreen vtable;
49   const __DRIcoreExtension *core;
50   const __DRIswrastExtension *swrast;
51   const __DRItexBufferExtension *texBuffer;
52
53   const __DRIconfig **driver_configs;
54
55   void *driver;
56};
57
58struct drisw_drawable
59{
60   __GLXDRIdrawable base;
61
62   GC gc;
63   GC swapgc;
64
65   __DRIdrawable *driDrawable;
66   XVisualInfo *visinfo;
67   XImage *ximage;
68};
69
70static Bool
71XCreateDrawable(struct drisw_drawable * pdp,
72                Display * dpy, XID drawable, int visualid)
73{
74   XGCValues gcvalues;
75   long visMask;
76   XVisualInfo visTemp;
77   int num_visuals;
78
79   /* create GC's */
80   pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
81   pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
82
83   gcvalues.function = GXcopy;
84   gcvalues.graphics_exposures = False;
85   XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
86   XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
87   XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
88
89   /* visual */
90   visTemp.screen = DefaultScreen(dpy);
91   visTemp.visualid = visualid;
92   visMask = (VisualScreenMask | VisualIDMask);
93   pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
94
95   /* create XImage */
96   pdp->ximage = XCreateImage(dpy,
97                              pdp->visinfo->visual,
98                              pdp->visinfo->depth,
99                              ZPixmap, 0,             /* format, offset */
100                              NULL,                   /* data */
101                              0, 0,                   /* width, height */
102                              32,                     /* bitmap_pad */
103                              0);                     /* bytes_per_line */
104
105  /**
106   * swrast does not handle 24-bit depth with 24 bpp, so let X do the
107   * the conversion for us.
108   */
109  if (pdp->ximage->bits_per_pixel == 24)
110     pdp->ximage->bits_per_pixel = 32;
111
112   return True;
113}
114
115static void
116XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
117{
118   XDestroyImage(pdp->ximage);
119   XFree(pdp->visinfo);
120
121   XFreeGC(dpy, pdp->gc);
122   XFreeGC(dpy, pdp->swapgc);
123}
124
125/**
126 * swrast loader functions
127 */
128
129static void
130swrastGetDrawableInfo(__DRIdrawable * draw,
131                      int *x, int *y, int *w, int *h,
132                      void *loaderPrivate)
133{
134   struct drisw_drawable *pdp = loaderPrivate;
135   __GLXDRIdrawable *pdraw = &(pdp->base);
136   Display *dpy = pdraw->psc->dpy;
137   Drawable drawable;
138
139   Window root;
140   unsigned uw, uh, bw, depth;
141
142   drawable = pdraw->xDrawable;
143
144   XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
145   *w = uw;
146   *h = uh;
147}
148
149/**
150 * Align renderbuffer pitch.
151 *
152 * This should be chosen by the driver and the loader (libGL, xserver/glx)
153 * should use the driver provided pitch.
154 *
155 * It seems that the xorg loader (that is the xserver loading swrast_dri for
156 * indirect rendering, not client-side libGL) requires that the pitch is
157 * exactly the image width padded to 32 bits. XXX
158 *
159 * The above restriction can probably be overcome by using ScratchPixmap and
160 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
161 * the scratch pixmap to 'pitch / cpp'.
162 */
163static inline int
164bytes_per_line(unsigned pitch_bits, unsigned mul)
165{
166   unsigned mask = mul - 1;
167
168   return ((pitch_bits + mask) & ~mask) / 8;
169}
170
171static void
172swrastPutImage(__DRIdrawable * draw, int op,
173               int x, int y, int w, int h,
174               char *data, void *loaderPrivate)
175{
176   struct drisw_drawable *pdp = loaderPrivate;
177   __GLXDRIdrawable *pdraw = &(pdp->base);
178   Display *dpy = pdraw->psc->dpy;
179   Drawable drawable;
180   XImage *ximage;
181   GC gc;
182
183   switch (op) {
184   case __DRI_SWRAST_IMAGE_OP_DRAW:
185      gc = pdp->gc;
186      break;
187   case __DRI_SWRAST_IMAGE_OP_SWAP:
188      gc = pdp->swapgc;
189      break;
190   default:
191      return;
192   }
193
194   drawable = pdraw->xDrawable;
195
196   ximage = pdp->ximage;
197   ximage->data = data;
198   ximage->width = w;
199   ximage->height = h;
200   ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
201
202   XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
203
204   ximage->data = NULL;
205}
206
207static void
208swrastGetImage(__DRIdrawable * read,
209               int x, int y, int w, int h,
210               char *data, void *loaderPrivate)
211{
212   struct drisw_drawable *prp = loaderPrivate;
213   __GLXDRIdrawable *pread = &(prp->base);
214   Display *dpy = pread->psc->dpy;
215   Drawable readable;
216   XImage *ximage;
217
218   readable = pread->xDrawable;
219
220   ximage = prp->ximage;
221   ximage->data = data;
222   ximage->width = w;
223   ximage->height = h;
224   ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
225
226   XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
227
228   ximage->data = NULL;
229}
230
231static const __DRIswrastLoaderExtension swrastLoaderExtension = {
232   {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
233   swrastGetDrawableInfo,
234   swrastPutImage,
235   swrastGetImage
236};
237
238static const __DRIextension *loader_extensions[] = {
239   &systemTimeExtension.base,
240   &swrastLoaderExtension.base,
241   NULL
242};
243
244/**
245 * GLXDRI functions
246 */
247
248static void
249drisw_destroy_context(struct glx_context *context)
250{
251   struct drisw_context *pcp = (struct drisw_context *) context;
252   struct drisw_screen *psc = (struct drisw_screen *) context->psc;
253
254   driReleaseDrawables(&pcp->base);
255
256   if (context->xid)
257      glx_send_destroy_context(psc->base.dpy, context->xid);
258
259   if (context->extensions)
260      XFree((char *) context->extensions);
261
262   (*psc->core->destroyContext) (pcp->driContext);
263
264   Xfree(pcp);
265}
266
267static int
268drisw_bind_context(struct glx_context *context, struct glx_context *old,
269		   GLXDrawable draw, GLXDrawable read)
270{
271   struct drisw_context *pcp = (struct drisw_context *) context;
272   struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
273   struct drisw_drawable *pdraw, *pread;
274
275   pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
276   pread = (struct drisw_drawable *) driFetchDrawable(context, read);
277
278   driReleaseDrawables(&pcp->base);
279
280   if (pdraw == NULL || pread == NULL)
281      return GLXBadDrawable;
282
283   if ((*psc->core->bindContext) (pcp->driContext,
284				  pdraw->driDrawable, pread->driDrawable))
285      return Success;
286
287   return GLXBadContext;
288}
289
290static void
291drisw_unbind_context(struct glx_context *context, struct glx_context *new)
292{
293   struct drisw_context *pcp = (struct drisw_context *) context;
294   struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
295
296   (*psc->core->unbindContext) (pcp->driContext);
297}
298
299static void
300drisw_bind_tex_image(Display * dpy,
301		    GLXDrawable drawable,
302		    int buffer, const int *attrib_list)
303{
304   struct glx_context *gc = __glXGetCurrentContext();
305   struct drisw_context *pcp = (struct drisw_context *) gc;
306   __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
307   struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
308   struct drisw_screen *psc;
309
310   __glXInitialize(dpy);
311
312   if (pdraw != NULL) {
313      psc = (struct drisw_screen *) base->psc;
314
315      if (!psc->texBuffer)
316         return;
317
318      if (psc->texBuffer->base.version >= 2 &&
319        psc->texBuffer->setTexBuffer2 != NULL) {
320	      (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
321					   pdraw->base.textureTarget,
322					   pdraw->base.textureFormat,
323					   pdraw->driDrawable);
324      }
325      else {
326	      (*psc->texBuffer->setTexBuffer) (pcp->driContext,
327					  pdraw->base.textureTarget,
328					  pdraw->driDrawable);
329      }
330   }
331}
332
333static void
334drisw_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
335{
336#if __DRI_TEX_BUFFER_VERSION >= 3
337   struct glx_context *gc = __glXGetCurrentContext();
338   struct dri2_context *pcp = (struct dri2_context *) gc;
339   __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
340   struct glx_display *dpyPriv = __glXInitialize(dpy);
341   struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
342   struct dri2_screen *psc;
343
344   if (pdraw != NULL) {
345      psc = (struct dri2_screen *) base->psc;
346
347      if (!psc->texBuffer)
348         return;
349
350      if (psc->texBuffer->base.version >= 3 &&
351          psc->texBuffer->releaseTexBuffer != NULL) {
352         (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
353                                           pdraw->base.textureTarget,
354                                           pdraw->driDrawable);
355      }
356   }
357#endif
358}
359
360static const struct glx_context_vtable drisw_context_vtable = {
361   drisw_destroy_context,
362   drisw_bind_context,
363   drisw_unbind_context,
364   NULL,
365   NULL,
366   DRI_glXUseXFont,
367   drisw_bind_tex_image,
368   drisw_release_tex_image,
369   NULL, /* get_proc_address */
370};
371
372static struct glx_context *
373drisw_create_context(struct glx_screen *base,
374		     struct glx_config *config_base,
375		     struct glx_context *shareList, int renderType)
376{
377   struct drisw_context *pcp, *pcp_shared;
378   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
379   struct drisw_screen *psc = (struct drisw_screen *) base;
380   __DRIcontext *shared = NULL;
381
382   if (!psc->base.driScreen)
383      return NULL;
384
385   if (shareList) {
386      pcp_shared = (struct drisw_context *) shareList;
387      shared = pcp_shared->driContext;
388   }
389
390   pcp = Xmalloc(sizeof *pcp);
391   if (pcp == NULL)
392      return NULL;
393
394   memset(pcp, 0, sizeof *pcp);
395   if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
396      Xfree(pcp);
397      return NULL;
398   }
399
400   pcp->driContext =
401      (*psc->core->createNewContext) (psc->driScreen,
402				      config->driConfig, shared, pcp);
403   if (pcp->driContext == NULL) {
404      Xfree(pcp);
405      return NULL;
406   }
407
408   pcp->base.vtable = &drisw_context_vtable;
409
410   return &pcp->base;
411}
412
413static void
414driswDestroyDrawable(__GLXDRIdrawable * pdraw)
415{
416   struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
417   struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
418
419   (*psc->core->destroyDrawable) (pdp->driDrawable);
420
421   XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
422   Xfree(pdp);
423}
424
425static __GLXDRIdrawable *
426driswCreateDrawable(struct glx_screen *base, XID xDrawable,
427		    GLXDrawable drawable, struct glx_config *modes)
428{
429   struct drisw_drawable *pdp;
430   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
431   struct drisw_screen *psc = (struct drisw_screen *) base;
432
433   const __DRIswrastExtension *swrast = psc->swrast;
434
435   pdp = Xmalloc(sizeof(*pdp));
436   if (!pdp)
437      return NULL;
438
439   memset(pdp, 0, sizeof *pdp);
440   pdp->base.xDrawable = xDrawable;
441   pdp->base.drawable = drawable;
442   pdp->base.psc = &psc->base;
443
444   XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
445
446   /* Create a new drawable */
447   pdp->driDrawable =
448      (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
449
450   if (!pdp->driDrawable) {
451      XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
452      Xfree(pdp);
453      return NULL;
454   }
455
456   pdp->base.destroyDrawable = driswDestroyDrawable;
457
458   return &pdp->base;
459}
460
461static int64_t
462driswSwapBuffers(__GLXDRIdrawable * pdraw,
463                 int64_t target_msc, int64_t divisor, int64_t remainder)
464{
465   struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
466   struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
467
468   (void) target_msc;
469   (void) divisor;
470   (void) remainder;
471
472   (*psc->core->swapBuffers) (pdp->driDrawable);
473
474   return 0;
475}
476
477static void
478driswDestroyScreen(struct glx_screen *base)
479{
480   struct drisw_screen *psc = (struct drisw_screen *) base;
481
482   /* Free the direct rendering per screen data */
483   (*psc->core->destroyScreen) (psc->driScreen);
484   driDestroyConfigs(psc->driver_configs);
485   psc->driScreen = NULL;
486   if (psc->driver)
487      dlclose(psc->driver);
488}
489
490static void *
491driOpenSwrast(void)
492{
493   void *driver = NULL;
494
495   if (driver == NULL)
496      driver = driOpenDriver("swrast");
497
498   return driver;
499}
500
501static const struct glx_screen_vtable drisw_screen_vtable = {
502   drisw_create_context
503};
504
505static void
506driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
507{
508   int i;
509
510   __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
511
512   /* FIXME: Figure out what other extensions can be ported here from dri2. */
513   for (i = 0; extensions[i]; i++) {
514      if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
515	 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
516	 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
517      }
518   }
519}
520
521static struct glx_screen *
522driswCreateScreen(int screen, struct glx_display *priv)
523{
524   __GLXDRIscreen *psp;
525   const __DRIconfig **driver_configs;
526   const __DRIextension **extensions;
527   struct drisw_screen *psc;
528   struct glx_config *configs = NULL, *visuals = NULL;
529   int i;
530
531   psc = Xcalloc(1, sizeof *psc);
532   if (psc == NULL)
533      return NULL;
534
535   memset(psc, 0, sizeof *psc);
536   if (!glx_screen_init(&psc->base, screen, priv)) {
537      Xfree(psc);
538      return NULL;
539   }
540
541   psc->driver = driOpenSwrast();
542   if (psc->driver == NULL)
543      goto handle_error;
544
545   extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
546   if (extensions == NULL) {
547      ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
548      goto handle_error;
549   }
550
551   for (i = 0; extensions[i]; i++) {
552      if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
553	 psc->core = (__DRIcoreExtension *) extensions[i];
554      if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
555	 psc->swrast = (__DRIswrastExtension *) extensions[i];
556   }
557
558   if (psc->core == NULL || psc->swrast == NULL) {
559      ErrorMessageF("core dri extension not found\n");
560      goto handle_error;
561   }
562
563   psc->driScreen =
564      psc->swrast->createNewScreen(screen, loader_extensions,
565				   &driver_configs, psc);
566   if (psc->driScreen == NULL) {
567      ErrorMessageF("failed to create dri screen\n");
568      goto handle_error;
569   }
570
571   extensions = psc->core->getExtensions(psc->driScreen);
572   driswBindExtensions(psc, extensions);
573
574   configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
575   visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
576
577   if (!configs || !visuals)
578       goto handle_error;
579
580   glx_config_destroy_list(psc->base.configs);
581   psc->base.configs = configs;
582   glx_config_destroy_list(psc->base.visuals);
583   psc->base.visuals = visuals;
584
585   psc->driver_configs = driver_configs;
586
587   psc->base.vtable = &drisw_screen_vtable;
588   psp = &psc->vtable;
589   psc->base.driScreen = psp;
590   psp->destroyScreen = driswDestroyScreen;
591   psp->createDrawable = driswCreateDrawable;
592   psp->swapBuffers = driswSwapBuffers;
593
594   return &psc->base;
595
596 handle_error:
597   if (configs)
598       glx_config_destroy_list(configs);
599   if (visuals)
600       glx_config_destroy_list(visuals);
601   if (psc->driScreen)
602       psc->core->destroyScreen(psc->driScreen);
603   psc->driScreen = NULL;
604
605   if (psc->driver)
606      dlclose(psc->driver);
607   glx_screen_cleanup(&psc->base);
608   Xfree(psc);
609
610   ErrorMessageF("reverting to indirect rendering\n");
611
612   return NULL;
613}
614
615/* Called from __glXFreeDisplayPrivate.
616 */
617static void
618driswDestroyDisplay(__GLXDRIdisplay * dpy)
619{
620   Xfree(dpy);
621}
622
623/*
624 * Allocate, initialize and return a __DRIdisplayPrivate object.
625 * This is called from __glXInitialize() when we are given a new
626 * display pointer.
627 */
628_X_HIDDEN __GLXDRIdisplay *
629driswCreateDisplay(Display * dpy)
630{
631   struct drisw_display *pdpyp;
632
633   pdpyp = Xmalloc(sizeof *pdpyp);
634   if (pdpyp == NULL)
635      return NULL;
636
637   pdpyp->base.destroyDisplay = driswDestroyDisplay;
638   pdpyp->base.createScreen = driswCreateScreen;
639
640   return &pdpyp->base;
641}
642
643#endif /* GLX_DIRECT_RENDERING */
644