2 * Copyright 2008 George Sapountzis
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:
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
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
24 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
27 #include "glxclient.h"
29 #include "dri_common.h"
38 struct glx_context base;
39 __DRIcontext *driContext;
45 struct glx_screen base;
47 __DRIscreen *driScreen;
48 __GLXDRIscreen vtable;
49 const __DRIcoreExtension *core;
50 const __DRIswrastExtension *swrast;
51 const __DRIconfig **driver_configs;
58 __GLXDRIdrawable base;
63 __DRIdrawable *driDrawable;
69 XCreateDrawable(struct drisw_drawable * pdp,
70 Display * dpy, XID drawable, int visualid)
78 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
79 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
81 gcvalues.function = GXcopy;
82 gcvalues.graphics_exposures = False;
83 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
84 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
85 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
88 visTemp.screen = DefaultScreen(dpy);
89 visTemp.visualid = visualid;
90 visMask = (VisualScreenMask | VisualIDMask);
91 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
94 pdp->ximage = XCreateImage(dpy,
97 ZPixmap, 0, /* format, offset */
99 0, 0, /* width, height */
101 0); /* bytes_per_line */
104 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
105 * the conversion for us.
107 if (pdp->ximage->bits_per_pixel == 24)
108 pdp->ximage->bits_per_pixel = 32;
114 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
116 XDestroyImage(pdp->ximage);
119 XFreeGC(dpy, pdp->gc);
120 XFreeGC(dpy, pdp->swapgc);
124 * swrast loader functions
128 swrastGetDrawableInfo(__DRIdrawable * draw,
129 int *x, int *y, int *w, int *h,
132 struct drisw_drawable *pdp = loaderPrivate;
133 __GLXDRIdrawable *pdraw = &(pdp->base);
134 Display *dpy = pdraw->psc->dpy;
138 unsigned uw, uh, bw, depth;
140 drawable = pdraw->xDrawable;
142 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
148 * Align renderbuffer pitch.
150 * This should be chosen by the driver and the loader (libGL, xserver/glx)
151 * should use the driver provided pitch.
153 * It seems that the xorg loader (that is the xserver loading swrast_dri for
154 * indirect rendering, not client-side libGL) requires that the pitch is
155 * exactly the image width padded to 32 bits. XXX
157 * The above restriction can probably be overcome by using ScratchPixmap and
158 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
159 * the scratch pixmap to 'pitch / cpp'.
162 bytes_per_line(unsigned pitch_bits, unsigned mul)
164 unsigned mask = mul - 1;
166 return ((pitch_bits + mask) & ~mask) / 8;
170 swrastPutImage(__DRIdrawable * draw, int op,
171 int x, int y, int w, int h,
172 char *data, void *loaderPrivate)
174 struct drisw_drawable *pdp = loaderPrivate;
175 __GLXDRIdrawable *pdraw = &(pdp->base);
176 Display *dpy = pdraw->psc->dpy;
182 case __DRI_SWRAST_IMAGE_OP_DRAW:
185 case __DRI_SWRAST_IMAGE_OP_SWAP:
192 drawable = pdraw->xDrawable;
194 ximage = pdp->ximage;
198 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
200 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
206 swrastGetImage(__DRIdrawable * read,
207 int x, int y, int w, int h,
208 char *data, void *loaderPrivate)
210 struct drisw_drawable *prp = loaderPrivate;
211 __GLXDRIdrawable *pread = &(prp->base);
212 Display *dpy = pread->psc->dpy;
216 readable = pread->xDrawable;
218 ximage = prp->ximage;
222 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
224 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
229 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
230 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
231 swrastGetDrawableInfo,
236 static const __DRIextension *loader_extensions[] = {
237 &systemTimeExtension.base,
238 &swrastLoaderExtension.base,
247 drisw_destroy_context(struct glx_context *context)
249 struct drisw_context *pcp = (struct drisw_context *) context;
250 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
252 driReleaseDrawables(&pcp->base);
255 glx_send_destroy_context(psc->base.dpy, context->xid);
257 if (context->extensions)
258 XFree((char *) context->extensions);
260 (*psc->core->destroyContext) (pcp->driContext);
266 drisw_bind_context(struct glx_context *context, struct glx_context *old,
267 GLXDrawable draw, GLXDrawable read)
269 struct drisw_context *pcp = (struct drisw_context *) context;
270 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
271 struct drisw_drawable *pdraw, *pread;
273 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
274 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
276 driReleaseDrawables(&pcp->base);
278 if (pdraw == NULL || pread == NULL)
279 return GLXBadDrawable;
281 if ((*psc->core->bindContext) (pcp->driContext,
282 pdraw->driDrawable, pread->driDrawable))
285 return GLXBadContext;
289 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
291 struct drisw_context *pcp = (struct drisw_context *) context;
292 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
294 (*psc->core->unbindContext) (pcp->driContext);
297 static const struct glx_context_vtable drisw_context_vtable = {
298 drisw_destroy_context,
300 drisw_unbind_context,
306 NULL, /* get_proc_address */
309 static struct glx_context *
310 drisw_create_context(struct glx_screen *base,
311 struct glx_config *config_base,
312 struct glx_context *shareList, int renderType)
314 struct drisw_context *pcp, *pcp_shared;
315 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
316 struct drisw_screen *psc = (struct drisw_screen *) base;
317 __DRIcontext *shared = NULL;
319 if (!psc->base.driScreen)
323 pcp_shared = (struct drisw_context *) shareList;
324 shared = pcp_shared->driContext;
327 pcp = Xmalloc(sizeof *pcp);
331 memset(pcp, 0, sizeof *pcp);
332 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
338 (*psc->core->createNewContext) (psc->driScreen,
339 config->driConfig, shared, pcp);
340 if (pcp->driContext == NULL) {
345 pcp->base.vtable = &drisw_context_vtable;
351 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
353 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
354 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
356 (*psc->core->destroyDrawable) (pdp->driDrawable);
358 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
362 static __GLXDRIdrawable *
363 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
364 GLXDrawable drawable, struct glx_config *modes)
366 struct drisw_drawable *pdp;
367 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
368 struct drisw_screen *psc = (struct drisw_screen *) base;
370 const __DRIswrastExtension *swrast = psc->swrast;
372 pdp = Xmalloc(sizeof(*pdp));
376 memset(pdp, 0, sizeof *pdp);
377 pdp->base.xDrawable = xDrawable;
378 pdp->base.drawable = drawable;
379 pdp->base.psc = &psc->base;
381 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
383 /* Create a new drawable */
385 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
387 if (!pdp->driDrawable) {
388 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
393 pdp->base.destroyDrawable = driswDestroyDrawable;
399 driswSwapBuffers(__GLXDRIdrawable * pdraw,
400 int64_t target_msc, int64_t divisor, int64_t remainder)
402 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
403 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
409 (*psc->core->swapBuffers) (pdp->driDrawable);
415 driswDestroyScreen(struct glx_screen *base)
417 struct drisw_screen *psc = (struct drisw_screen *) base;
419 /* Free the direct rendering per screen data */
420 (*psc->core->destroyScreen) (psc->driScreen);
421 driDestroyConfigs(psc->driver_configs);
422 psc->driScreen = NULL;
424 dlclose(psc->driver);
433 driver = driOpenDriver("swrast");
436 driver = driOpenDriver("swrastg");
441 static const struct glx_screen_vtable drisw_screen_vtable = {
445 static struct glx_screen *
446 driswCreateScreen(int screen, struct glx_display *priv)
449 const __DRIconfig **driver_configs;
450 const __DRIextension **extensions;
451 struct drisw_screen *psc;
454 psc = Xcalloc(1, sizeof *psc);
458 memset(psc, 0, sizeof *psc);
459 if (!glx_screen_init(&psc->base, screen, priv)) {
464 psc->driver = driOpenSwrast();
465 if (psc->driver == NULL)
468 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
469 if (extensions == NULL) {
470 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
474 for (i = 0; extensions[i]; i++) {
475 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
476 psc->core = (__DRIcoreExtension *) extensions[i];
477 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
478 psc->swrast = (__DRIswrastExtension *) extensions[i];
481 if (psc->core == NULL || psc->swrast == NULL) {
482 ErrorMessageF("core dri extension not found\n");
487 psc->swrast->createNewScreen(screen, loader_extensions,
488 &driver_configs, psc);
489 if (psc->driScreen == NULL) {
490 ErrorMessageF("failed to create dri screen\n");
495 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
497 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
499 psc->driver_configs = driver_configs;
501 psc->base.vtable = &drisw_screen_vtable;
503 psc->base.driScreen = psp;
504 psp->destroyScreen = driswDestroyScreen;
505 psp->createDrawable = driswCreateDrawable;
506 psp->swapBuffers = driswSwapBuffers;
512 dlclose(psc->driver);
513 glx_screen_cleanup(&psc->base);
516 ErrorMessageF("reverting to indirect rendering\n");
521 /* Called from __glXFreeDisplayPrivate.
524 driswDestroyDisplay(__GLXDRIdisplay * dpy)
530 * Allocate, initialize and return a __DRIdisplayPrivate object.
531 * This is called from __glXInitialize() when we are given a new
534 _X_HIDDEN __GLXDRIdisplay *
535 driswCreateDisplay(Display * dpy)
537 struct drisw_display *pdpyp;
539 pdpyp = Xmalloc(sizeof *pdpyp);
543 pdpyp->base.destroyDisplay = driswDestroyDisplay;
544 pdpyp->base.createScreen = driswCreateScreen;
549 #endif /* GLX_DIRECT_RENDERING */