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;
131 unsigned uw, uh, bw, depth;
133 drawable = pdraw->xDrawable;
135 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
141 * Align renderbuffer pitch.
143 * This should be chosen by the driver and the loader (libGL, xserver/glx)
144 * should use the driver provided pitch.
146 * It seems that the xorg loader (that is the xserver loading swrast_dri for
147 * indirect rendering, not client-side libGL) requires that the pitch is
148 * exactly the image width padded to 32 bits. XXX
150 * The above restriction can probably be overcome by using ScratchPixmap and
151 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
152 * the scratch pixmap to 'pitch / cpp'.
155 bytes_per_line(unsigned pitch_bits, unsigned mul)
157 unsigned mask = mul - 1;
159 return ((pitch_bits + mask) & ~mask) / 8;
163 swrastPutImage(__DRIdrawable * draw, int op,
164 int x, int y, int w, int h,
165 char *data, void *loaderPrivate)
167 struct drisw_drawable *pdp = loaderPrivate;
168 __GLXDRIdrawable *pdraw = &(pdp->base);
169 Display *dpy = pdraw->psc->dpy;
175 case __DRI_SWRAST_IMAGE_OP_DRAW:
178 case __DRI_SWRAST_IMAGE_OP_SWAP:
185 drawable = pdraw->xDrawable;
187 ximage = pdp->ximage;
191 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
193 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
199 swrastGetImage(__DRIdrawable * read,
200 int x, int y, int w, int h,
201 char *data, void *loaderPrivate)
203 struct drisw_drawable *prp = loaderPrivate;
204 __GLXDRIdrawable *pread = &(prp->base);
205 Display *dpy = pread->psc->dpy;
209 readable = pread->xDrawable;
211 ximage = prp->ximage;
215 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
217 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
222 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
223 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
224 swrastGetDrawableInfo,
229 static const __DRIextension *loader_extensions[] = {
230 &systemTimeExtension.base,
231 &swrastLoaderExtension.base,
240 drisw_destroy_context(struct glx_context *context)
242 struct drisw_context *pcp = (struct drisw_context *) context;
243 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
245 driReleaseDrawables(&pcp->base);
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 driReleaseDrawables(&pcp->base);
271 if (pdraw == NULL || pread == NULL)
272 return GLXBadDrawable;
274 if ((*psc->core->bindContext) (pcp->driContext,
275 pdraw->driDrawable, pread->driDrawable))
278 return GLXBadContext;
282 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
284 struct drisw_context *pcp = (struct drisw_context *) context;
285 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
287 (*psc->core->unbindContext) (pcp->driContext);
290 static const struct glx_context_vtable drisw_context_vtable = {
291 drisw_destroy_context,
293 drisw_unbind_context,
299 NULL, /* get_proc_address */
302 static struct glx_context *
303 drisw_create_context(struct glx_screen *base,
304 struct glx_config *config_base,
305 struct glx_context *shareList, int renderType)
307 struct drisw_context *pcp, *pcp_shared;
308 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
309 struct drisw_screen *psc = (struct drisw_screen *) base;
310 __DRIcontext *shared = NULL;
312 if (!psc->base.driScreen)
316 pcp_shared = (struct drisw_context *) shareList;
317 shared = pcp_shared->driContext;
320 pcp = Xmalloc(sizeof *pcp);
324 memset(pcp, 0, sizeof *pcp);
325 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
331 (*psc->core->createNewContext) (psc->driScreen,
332 config->driConfig, shared, pcp);
333 if (pcp->driContext == NULL) {
338 pcp->base.vtable = &drisw_context_vtable;
344 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
346 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
347 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
349 (*psc->core->destroyDrawable) (pdp->driDrawable);
351 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
355 static __GLXDRIdrawable *
356 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
357 GLXDrawable drawable, struct glx_config *modes)
359 struct drisw_drawable *pdp;
360 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
361 struct drisw_screen *psc = (struct drisw_screen *) base;
363 const __DRIswrastExtension *swrast = psc->swrast;
365 pdp = Xmalloc(sizeof(*pdp));
369 memset(pdp, 0, sizeof *pdp);
370 pdp->base.xDrawable = xDrawable;
371 pdp->base.drawable = drawable;
372 pdp->base.psc = &psc->base;
374 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
376 /* Create a new drawable */
378 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
380 if (!pdp->driDrawable) {
381 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
386 pdp->base.destroyDrawable = driswDestroyDrawable;
392 driswSwapBuffers(__GLXDRIdrawable * pdraw,
393 int64_t target_msc, int64_t divisor, int64_t remainder)
395 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
396 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
402 (*psc->core->swapBuffers) (pdp->driDrawable);
408 driswDestroyScreen(struct glx_screen *base)
410 struct drisw_screen *psc = (struct drisw_screen *) base;
412 /* Free the direct rendering per screen data */
413 (*psc->core->destroyScreen) (psc->driScreen);
414 driDestroyConfigs(psc->driver_configs);
415 psc->driScreen = NULL;
417 dlclose(psc->driver);
426 driver = driOpenDriver("swrast");
429 driver = driOpenDriver("swrastg");
434 static const struct glx_screen_vtable drisw_screen_vtable = {
438 static struct glx_screen *
439 driswCreateScreen(int screen, struct glx_display *priv)
442 const __DRIconfig **driver_configs;
443 const __DRIextension **extensions;
444 struct drisw_screen *psc;
447 psc = Xcalloc(1, sizeof *psc);
451 memset(psc, 0, sizeof *psc);
452 if (!glx_screen_init(&psc->base, screen, priv)) {
457 psc->driver = driOpenSwrast();
458 if (psc->driver == NULL)
461 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
462 if (extensions == NULL) {
463 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
467 for (i = 0; extensions[i]; i++) {
468 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
469 psc->core = (__DRIcoreExtension *) extensions[i];
470 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
471 psc->swrast = (__DRIswrastExtension *) extensions[i];
474 if (psc->core == NULL || psc->swrast == NULL) {
475 ErrorMessageF("core dri extension not found\n");
480 psc->swrast->createNewScreen(screen, loader_extensions,
481 &driver_configs, psc);
482 if (psc->driScreen == NULL) {
483 ErrorMessageF("failed to create dri screen\n");
488 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
490 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
492 psc->driver_configs = driver_configs;
494 psc->base.vtable = &drisw_screen_vtable;
496 psc->base.driScreen = psp;
497 psp->destroyScreen = driswDestroyScreen;
498 psp->createDrawable = driswCreateDrawable;
499 psp->swapBuffers = driswSwapBuffers;
505 dlclose(psc->driver);
506 glx_screen_cleanup(&psc->base);
509 ErrorMessageF("reverting to indirect rendering\n");
514 /* Called from __glXFreeDisplayPrivate.
517 driswDestroyDisplay(__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 = driswDestroyDisplay;
537 pdpyp->base.createScreen = driswCreateScreen;
542 #endif /* GLX_DIRECT_RENDERING */