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 */
107 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
109 XDestroyImage(pdp->ximage);
112 XFreeGC(dpy, pdp->gc);
113 XFreeGC(dpy, pdp->swapgc);
117 * swrast loader functions
121 swrastGetDrawableInfo(__DRIdrawable * draw,
122 int *x, int *y, int *w, int *h,
125 struct drisw_drawable *pdp = loaderPrivate;
126 __GLXDRIdrawable *pdraw = &(pdp->base);
127 Display *dpy = pdraw->psc->dpy;
132 unsigned uw, uh, bw, depth;
134 drawable = pdraw->xDrawable;
136 stat = XGetGeometry(dpy, drawable, &root,
137 x, y, &uw, &uh, &bw, &depth);
143 * Align renderbuffer pitch.
145 * This should be chosen by the driver and the loader (libGL, xserver/glx)
146 * should use the driver provided pitch.
148 * It seems that the xorg loader (that is the xserver loading swrast_dri for
149 * indirect rendering, not client-side libGL) requires that the pitch is
150 * exactly the image width padded to 32 bits. XXX
152 * The above restriction can probably be overcome by using ScratchPixmap and
153 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
154 * the scratch pixmap to 'pitch / cpp'.
157 bytes_per_line(unsigned pitch_bits, unsigned mul)
159 unsigned mask = mul - 1;
161 return ((pitch_bits + mask) & ~mask) / 8;
165 swrastPutImage(__DRIdrawable * draw, int op,
166 int x, int y, int w, int h,
167 char *data, void *loaderPrivate)
169 struct drisw_drawable *pdp = loaderPrivate;
170 __GLXDRIdrawable *pdraw = &(pdp->base);
171 Display *dpy = pdraw->psc->dpy;
177 case __DRI_SWRAST_IMAGE_OP_DRAW:
180 case __DRI_SWRAST_IMAGE_OP_SWAP:
187 drawable = pdraw->xDrawable;
189 ximage = pdp->ximage;
193 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
195 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
201 swrastGetImage(__DRIdrawable * read,
202 int x, int y, int w, int h,
203 char *data, void *loaderPrivate)
205 struct drisw_drawable *prp = loaderPrivate;
206 __GLXDRIdrawable *pread = &(prp->base);
207 Display *dpy = pread->psc->dpy;
211 readable = pread->xDrawable;
213 ximage = prp->ximage;
217 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
219 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
224 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
225 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
226 swrastGetDrawableInfo,
231 static const __DRIextension *loader_extensions[] = {
232 &systemTimeExtension.base,
233 &swrastLoaderExtension.base,
242 drisw_destroy_context(struct glx_context *context)
244 struct drisw_context *pcp = (struct drisw_context *) context;
245 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
248 glx_send_destroy_context(psc->base.dpy, context->xid);
250 if (context->extensions)
251 XFree((char *) context->extensions);
253 (*psc->core->destroyContext) (pcp->driContext);
259 drisw_bind_context(struct glx_context *context, struct glx_context *old,
260 GLXDrawable draw, GLXDrawable read)
262 struct drisw_context *pcp = (struct drisw_context *) context;
263 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
264 struct drisw_drawable *pdraw, *pread;
266 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
267 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
269 if (pdraw == NULL || pread == NULL)
270 return GLXBadDrawable;
272 if ((*psc->core->bindContext) (pcp->driContext,
273 pdraw->driDrawable, pread->driDrawable))
276 return GLXBadContext;
280 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
282 struct drisw_context *pcp = (struct drisw_context *) context;
283 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
285 (*psc->core->unbindContext) (pcp->driContext);
287 driReleaseDrawables(&pcp->base);
290 static const struct glx_context_vtable drisw_context_vtable = {
291 drisw_destroy_context,
293 drisw_unbind_context,
301 static struct glx_context *
302 drisw_create_context(struct glx_screen *base,
303 struct glx_config *config_base,
304 struct glx_context *shareList, int renderType)
306 struct drisw_context *pcp, *pcp_shared;
307 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
308 struct drisw_screen *psc = (struct drisw_screen *) base;
309 __DRIcontext *shared = NULL;
311 if (!psc->base.driScreen)
315 pcp_shared = (struct drisw_context *) shareList;
316 shared = pcp_shared->driContext;
319 pcp = Xmalloc(sizeof *pcp);
323 memset(pcp, 0, sizeof *pcp);
324 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
330 (*psc->core->createNewContext) (psc->driScreen,
331 config->driConfig, shared, pcp);
332 if (pcp->driContext == NULL) {
337 pcp->base.vtable = &drisw_context_vtable;
343 driDestroyDrawable(__GLXDRIdrawable * pdraw)
345 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
346 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
348 (*psc->core->destroyDrawable) (pdp->driDrawable);
350 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
354 static __GLXDRIdrawable *
355 driCreateDrawable(struct glx_screen *base, XID xDrawable,
356 GLXDrawable drawable, struct glx_config *modes)
358 struct drisw_drawable *pdp;
359 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
360 struct drisw_screen *psc = (struct drisw_screen *) base;
362 const __DRIswrastExtension *swrast = psc->swrast;
364 /* Old dri can't handle GLX 1.3+ drawable constructors. */
365 if (xDrawable != drawable)
368 pdp = Xmalloc(sizeof(*pdp));
372 memset(pdp, 0, sizeof *pdp);
373 pdp->base.xDrawable = xDrawable;
374 pdp->base.drawable = drawable;
375 pdp->base.psc = &psc->base;
377 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
379 /* Create a new drawable */
381 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
383 if (!pdp->driDrawable) {
384 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
389 pdp->base.destroyDrawable = driDestroyDrawable;
395 driSwapBuffers(__GLXDRIdrawable * pdraw,
396 int64_t target_msc, int64_t divisor, int64_t remainder)
398 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
399 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
405 (*psc->core->swapBuffers) (pdp->driDrawable);
411 driDestroyScreen(struct glx_screen *base)
413 struct drisw_screen *psc = (struct drisw_screen *) base;
415 /* Free the direct rendering per screen data */
416 (*psc->core->destroyScreen) (psc->driScreen);
417 driDestroyConfigs(psc->driver_configs);
418 psc->driScreen = NULL;
420 dlclose(psc->driver);
429 driver = driOpenDriver("swrast");
432 driver = driOpenDriver("swrastg");
437 static const struct glx_screen_vtable drisw_screen_vtable = {
441 static struct glx_screen *
442 driCreateScreen(int screen, struct glx_display *priv)
445 const __DRIconfig **driver_configs;
446 const __DRIextension **extensions;
447 struct drisw_screen *psc;
450 psc = Xcalloc(1, sizeof *psc);
454 memset(psc, 0, sizeof *psc);
455 if (!glx_screen_init(&psc->base, screen, priv))
458 psc->driver = driOpenSwrast();
459 if (psc->driver == NULL)
462 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
463 if (extensions == NULL) {
464 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
468 for (i = 0; extensions[i]; i++) {
469 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
470 psc->core = (__DRIcoreExtension *) extensions[i];
471 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
472 psc->swrast = (__DRIswrastExtension *) extensions[i];
475 if (psc->core == NULL || psc->swrast == NULL) {
476 ErrorMessageF("core dri extension not found\n");
481 psc->swrast->createNewScreen(screen, loader_extensions,
482 &driver_configs, psc);
483 if (psc->driScreen == NULL) {
484 ErrorMessageF("failed to create dri screen\n");
489 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
491 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
493 psc->driver_configs = driver_configs;
495 psc->base.vtable = &drisw_screen_vtable;
497 psc->base.driScreen = psp;
498 psp->destroyScreen = driDestroyScreen;
499 psp->createDrawable = driCreateDrawable;
500 psp->swapBuffers = driSwapBuffers;
506 dlclose(psc->driver);
509 ErrorMessageF("reverting to indirect rendering\n");
514 /* Called from __glXFreeDisplayPrivate.
517 driDestroyDisplay(__GLXDRIdisplay * dpy)
523 * Allocate, initialize and return a __DRIdisplayPrivate object.
524 * This is called from __glXInitialize() when we are given a new
527 _X_HIDDEN __GLXDRIdisplay *
528 driswCreateDisplay(Display * dpy)
530 struct drisw_display *pdpyp;
532 pdpyp = Xmalloc(sizeof *pdpyp);
536 pdpyp->base.destroyDisplay = driDestroyDisplay;
537 pdpyp->base.createScreen = driCreateScreen;
542 #endif /* GLX_DIRECT_RENDERING */