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"
39 __DRIcontext *driContext;
40 __GLXscreenConfigs *psc;
45 __GLXscreenConfigs base;
47 __GLXDRIscreen driScreen;
48 const __DRIcoreExtension *core;
49 const __DRIswrastExtension *swrast;
55 __GLXDRIdrawable base;
65 XCreateDrawable(struct drisw_drawable * pdp,
66 Display * dpy, XID drawable, int visualid)
74 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
75 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
77 gcvalues.function = GXcopy;
78 gcvalues.graphics_exposures = False;
79 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
80 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
81 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
84 visTemp.screen = DefaultScreen(dpy);
85 visTemp.visualid = visualid;
86 visMask = (VisualScreenMask | VisualIDMask);
87 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
90 pdp->ximage = XCreateImage(dpy,
93 ZPixmap, 0, /* format, offset */
95 0, 0, /* width, height */
97 0); /* bytes_per_line */
103 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
105 XDestroyImage(pdp->ximage);
108 XFreeGC(dpy, pdp->gc);
109 XFreeGC(dpy, pdp->swapgc);
113 * swrast loader functions
117 swrastGetDrawableInfo(__DRIdrawable * draw,
118 int *x, int *y, int *w, int *h,
121 struct drisw_drawable *pdp = loaderPrivate;
122 __GLXDRIdrawable *pdraw = &(pdp->base);
123 Display *dpy = pdraw->psc->dpy;
128 unsigned uw, uh, bw, depth;
130 drawable = pdraw->xDrawable;
132 stat = XGetGeometry(dpy, drawable, &root,
133 x, y, &uw, &uh, &bw, &depth);
139 * Align renderbuffer pitch.
141 * This should be chosen by the driver and the loader (libGL, xserver/glx)
142 * should use the driver provided pitch.
144 * It seems that the xorg loader (that is the xserver loading swrast_dri for
145 * indirect rendering, not client-side libGL) requires that the pitch is
146 * exactly the image width padded to 32 bits. XXX
148 * The above restriction can probably be overcome by using ScratchPixmap and
149 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
150 * the scratch pixmap to 'pitch / cpp'.
153 bytes_per_line(unsigned pitch_bits, unsigned mul)
155 unsigned mask = mul - 1;
157 return ((pitch_bits + mask) & ~mask) / 8;
161 swrastPutImage(__DRIdrawable * draw, int op,
162 int x, int y, int w, int h,
163 char *data, void *loaderPrivate)
165 struct drisw_drawable *pdp = loaderPrivate;
166 __GLXDRIdrawable *pdraw = &(pdp->base);
167 Display *dpy = pdraw->psc->dpy;
173 case __DRI_SWRAST_IMAGE_OP_DRAW:
176 case __DRI_SWRAST_IMAGE_OP_SWAP:
183 drawable = pdraw->xDrawable;
185 ximage = pdp->ximage;
189 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
191 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
197 swrastGetImage(__DRIdrawable * read,
198 int x, int y, int w, int h,
199 char *data, void *loaderPrivate)
201 struct drisw_drawable *prp = loaderPrivate;
202 __GLXDRIdrawable *pread = &(prp->base);
203 Display *dpy = pread->psc->dpy;
207 readable = pread->xDrawable;
209 ximage = prp->ximage;
213 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
215 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
220 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
221 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
222 swrastGetDrawableInfo,
227 static const __DRIextension *loader_extensions[] = {
228 &systemTimeExtension.base,
229 &swrastLoaderExtension.base,
238 driDestroyContext(__GLXDRIcontext *context,
239 __GLXscreenConfigs *base, Display *dpy)
241 struct drisw_context *pcp = (struct drisw_context *) context;
242 struct drisw_screen *psc = (struct drisw_screen *) base;
244 (*psc->core->destroyContext) (pcp->driContext);
250 driBindContext(__GLXDRIcontext * context,
251 __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
253 struct drisw_context *pcp = (struct drisw_context *) context;
254 struct drisw_screen *psc = (struct drisw_screen *) pcp->psc;
256 return (*psc->core->bindContext) (pcp->driContext,
257 draw->driDrawable, read->driDrawable);
261 driUnbindContext(__GLXDRIcontext * context)
263 struct drisw_context *pcp = (struct drisw_context *) context;
264 struct drisw_screen *psc = (struct drisw_screen *) pcp->psc;
266 (*psc->core->unbindContext) (pcp->driContext);
269 static __GLXDRIcontext *
270 driCreateContext(__GLXscreenConfigs *base,
271 const __GLcontextModes *mode,
272 GLXContext gc, GLXContext shareList, int renderType)
274 struct drisw_context *pcp, *pcp_shared;
275 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
276 struct drisw_screen *psc = (struct drisw_screen *) base;
277 __DRIcontext *shared = NULL;
279 if (!psc->base.driScreen)
283 pcp_shared = (struct drisw_context *) shareList->driContext;
284 shared = pcp_shared->driContext;
287 pcp = Xmalloc(sizeof *pcp);
291 pcp->psc = &psc->base;
293 (*psc->core->createNewContext) (psc->base.__driScreen,
294 config->driConfig, shared, pcp);
295 if (pcp->driContext == NULL) {
300 pcp->base.destroyContext = driDestroyContext;
301 pcp->base.bindContext = driBindContext;
302 pcp->base.unbindContext = driUnbindContext;
308 driDestroyDrawable(__GLXDRIdrawable * pdraw)
310 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
311 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
313 (*psc->core->destroyDrawable) (pdraw->driDrawable);
315 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
319 static __GLXDRIdrawable *
320 driCreateDrawable(__GLXscreenConfigs *base, XID xDrawable,
321 GLXDrawable drawable, const __GLcontextModes * modes)
323 __GLXDRIdrawable *pdraw;
324 struct drisw_drawable *pdp;
325 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
326 struct drisw_screen *psc = (struct drisw_screen *) base;
328 const __DRIswrastExtension *swrast = psc->swrast;
330 /* Old dri can't handle GLX 1.3+ drawable constructors. */
331 if (xDrawable != drawable)
334 pdp = Xmalloc(sizeof(*pdp));
338 pdraw = &(pdp->base);
339 pdraw->xDrawable = xDrawable;
340 pdraw->drawable = drawable;
341 pdraw->psc = &psc->base;
343 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
345 /* Create a new drawable */
347 (*swrast->createNewDrawable) (psc->base.__driScreen,
348 config->driConfig, pdp);
350 if (!pdraw->driDrawable) {
351 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
356 pdraw->destroyDrawable = driDestroyDrawable;
362 driSwapBuffers(__GLXDRIdrawable * pdraw,
363 int64_t target_msc, int64_t divisor, int64_t remainder)
365 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
366 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
372 (*psc->core->swapBuffers) (pdraw->driDrawable);
378 driDestroyScreen(__GLXscreenConfigs *base)
380 struct drisw_screen *psc = (struct drisw_screen *) base;
382 /* Free the direct rendering per screen data */
383 (*psc->core->destroyScreen) (psc->base.__driScreen);
384 psc->base.__driScreen = NULL;
386 dlclose(psc->driver);
395 driver = driOpenDriver("swrast");
398 driver = driOpenDriver("swrastg");
403 static __GLXscreenConfigs *
404 driCreateScreen(int screen, __GLXdisplayPrivate *priv)
407 const __DRIconfig **driver_configs;
408 const __DRIextension **extensions;
409 struct drisw_screen *psc;
412 psc = Xcalloc(1, sizeof *psc);
416 memset(psc, 0, sizeof *psc);
417 if (!glx_screen_init(&psc->base, screen, priv))
420 psc->driver = driOpenSwrast();
421 if (psc->driver == NULL)
424 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
425 if (extensions == NULL) {
426 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
430 for (i = 0; extensions[i]; i++) {
431 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
432 psc->core = (__DRIcoreExtension *) extensions[i];
433 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
434 psc->swrast = (__DRIswrastExtension *) extensions[i];
437 if (psc->core == NULL || psc->swrast == NULL) {
438 ErrorMessageF("core dri extension not found\n");
442 psc->base.__driScreen =
443 psc->swrast->createNewScreen(screen, loader_extensions,
444 &driver_configs, psc);
445 if (psc->base.__driScreen == NULL) {
446 ErrorMessageF("failed to create dri screen\n");
450 extensions = psc->core->getExtensions(psc->base.__driScreen);
451 driBindExtensions(&psc->base, extensions);
452 driBindCommonExtensions(&psc->base, extensions);
455 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
457 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
459 psc->base.driver_configs = driver_configs;
461 psp = &psc->driScreen;
462 psc->base.driScreen = psp;
463 psp->destroyScreen = driDestroyScreen;
464 psp->createContext = driCreateContext;
465 psp->createDrawable = driCreateDrawable;
466 psp->swapBuffers = driSwapBuffers;
476 dlclose(psc->driver);
478 ErrorMessageF("reverting to indirect rendering\n");
483 /* Called from __glXFreeDisplayPrivate.
486 driDestroyDisplay(__GLXDRIdisplay * dpy)
492 * Allocate, initialize and return a __DRIdisplayPrivate object.
493 * This is called from __glXInitialize() when we are given a new
496 _X_HIDDEN __GLXDRIdisplay *
497 driswCreateDisplay(Display * dpy)
499 struct drisw_display *pdpyp;
501 pdpyp = Xmalloc(sizeof *pdpyp);
505 pdpyp->base.destroyDisplay = driDestroyDisplay;
506 pdpyp->base.createScreen = driCreateScreen;
511 #endif /* GLX_DIRECT_RENDERING */