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 __DRItexBufferExtension *texBuffer;
53 const __DRIconfig **driver_configs;
60 __GLXDRIdrawable base;
65 __DRIdrawable *driDrawable;
71 XCreateDrawable(struct drisw_drawable * pdp,
72 Display * dpy, XID drawable, int visualid)
80 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
81 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
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);
90 visTemp.visualid = visualid;
91 visMask = VisualIDMask;
92 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
94 if (!pdp->visinfo || num_visuals == 0)
98 pdp->ximage = XCreateImage(dpy,
101 ZPixmap, 0, /* format, offset */
103 0, 0, /* width, height */
105 0); /* bytes_per_line */
108 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
109 * the conversion for us.
111 if (pdp->ximage->bits_per_pixel == 24)
112 pdp->ximage->bits_per_pixel = 32;
118 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
120 XDestroyImage(pdp->ximage);
123 XFreeGC(dpy, pdp->gc);
124 XFreeGC(dpy, pdp->swapgc);
128 * swrast loader functions
132 swrastGetDrawableInfo(__DRIdrawable * draw,
133 int *x, int *y, int *w, int *h,
136 struct drisw_drawable *pdp = loaderPrivate;
137 __GLXDRIdrawable *pdraw = &(pdp->base);
138 Display *dpy = pdraw->psc->dpy;
142 unsigned uw, uh, bw, depth;
144 drawable = pdraw->xDrawable;
146 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
152 * Align renderbuffer pitch.
154 * This should be chosen by the driver and the loader (libGL, xserver/glx)
155 * should use the driver provided pitch.
157 * It seems that the xorg loader (that is the xserver loading swrast_dri for
158 * indirect rendering, not client-side libGL) requires that the pitch is
159 * exactly the image width padded to 32 bits. XXX
161 * The above restriction can probably be overcome by using ScratchPixmap and
162 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
163 * the scratch pixmap to 'pitch / cpp'.
166 bytes_per_line(unsigned pitch_bits, unsigned mul)
168 unsigned mask = mul - 1;
170 return ((pitch_bits + mask) & ~mask) / 8;
174 swrastPutImage(__DRIdrawable * draw, int op,
175 int x, int y, int w, int h,
176 char *data, void *loaderPrivate)
178 struct drisw_drawable *pdp = loaderPrivate;
179 __GLXDRIdrawable *pdraw = &(pdp->base);
180 Display *dpy = pdraw->psc->dpy;
186 case __DRI_SWRAST_IMAGE_OP_DRAW:
189 case __DRI_SWRAST_IMAGE_OP_SWAP:
196 drawable = pdraw->xDrawable;
198 ximage = pdp->ximage;
202 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
204 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
210 swrastGetImage(__DRIdrawable * read,
211 int x, int y, int w, int h,
212 char *data, void *loaderPrivate)
214 struct drisw_drawable *prp = loaderPrivate;
215 __GLXDRIdrawable *pread = &(prp->base);
216 Display *dpy = pread->psc->dpy;
220 readable = pread->xDrawable;
222 ximage = prp->ximage;
226 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
228 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
233 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
234 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
235 swrastGetDrawableInfo,
240 static const __DRIextension *loader_extensions[] = {
241 &systemTimeExtension.base,
242 &swrastLoaderExtension.base,
251 drisw_destroy_context(struct glx_context *context)
253 struct drisw_context *pcp = (struct drisw_context *) context;
254 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
256 driReleaseDrawables(&pcp->base);
258 if (context->extensions)
259 XFree((char *) context->extensions);
261 (*psc->core->destroyContext) (pcp->driContext);
267 drisw_bind_context(struct glx_context *context, struct glx_context *old,
268 GLXDrawable draw, GLXDrawable read)
270 struct drisw_context *pcp = (struct drisw_context *) context;
271 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
272 struct drisw_drawable *pdraw, *pread;
274 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
275 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
277 driReleaseDrawables(&pcp->base);
279 if (pdraw == NULL || pread == NULL)
280 return GLXBadDrawable;
282 if ((*psc->core->bindContext) (pcp->driContext,
283 pdraw->driDrawable, pread->driDrawable))
286 return GLXBadContext;
290 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
292 struct drisw_context *pcp = (struct drisw_context *) context;
293 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
295 (*psc->core->unbindContext) (pcp->driContext);
299 drisw_bind_tex_image(Display * dpy,
300 GLXDrawable drawable,
301 int buffer, const int *attrib_list)
303 struct glx_context *gc = __glXGetCurrentContext();
304 struct drisw_context *pcp = (struct drisw_context *) gc;
305 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
306 struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
307 struct drisw_screen *psc;
309 __glXInitialize(dpy);
312 psc = (struct drisw_screen *) base->psc;
317 if (psc->texBuffer->base.version >= 2 &&
318 psc->texBuffer->setTexBuffer2 != NULL) {
319 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
320 pdraw->base.textureTarget,
321 pdraw->base.textureFormat,
325 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
326 pdraw->base.textureTarget,
333 drisw_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
335 #if __DRI_TEX_BUFFER_VERSION >= 3
336 struct glx_context *gc = __glXGetCurrentContext();
337 struct dri2_context *pcp = (struct dri2_context *) gc;
338 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
339 struct glx_display *dpyPriv = __glXInitialize(dpy);
340 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
341 struct dri2_screen *psc;
344 psc = (struct dri2_screen *) base->psc;
349 if (psc->texBuffer->base.version >= 3 &&
350 psc->texBuffer->releaseTexBuffer != NULL) {
351 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
352 pdraw->base.textureTarget,
359 static const struct glx_context_vtable drisw_context_vtable = {
360 drisw_destroy_context,
362 drisw_unbind_context,
366 drisw_bind_tex_image,
367 drisw_release_tex_image,
368 NULL, /* get_proc_address */
371 static struct glx_context *
372 drisw_create_context(struct glx_screen *base,
373 struct glx_config *config_base,
374 struct glx_context *shareList, int renderType)
376 struct drisw_context *pcp, *pcp_shared;
377 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
378 struct drisw_screen *psc = (struct drisw_screen *) base;
379 __DRIcontext *shared = NULL;
381 if (!psc->base.driScreen)
385 /* If the shareList context is not a DRISW context, we cannot possibly
386 * create a DRISW context that shares it.
388 if (shareList->vtable->destroy != drisw_destroy_context) {
392 pcp_shared = (struct drisw_context *) shareList;
393 shared = pcp_shared->driContext;
396 pcp = Xmalloc(sizeof *pcp);
400 memset(pcp, 0, sizeof *pcp);
401 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
407 (*psc->core->createNewContext) (psc->driScreen,
408 config->driConfig, shared, pcp);
409 if (pcp->driContext == NULL) {
414 pcp->base.vtable = &drisw_context_vtable;
419 static struct glx_context *
420 drisw_create_context_attribs(struct glx_screen *base,
421 struct glx_config *config_base,
422 struct glx_context *shareList,
423 unsigned num_attribs,
424 const uint32_t *attribs,
427 struct drisw_context *pcp, *pcp_shared;
428 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
429 struct drisw_screen *psc = (struct drisw_screen *) base;
430 __DRIcontext *shared = NULL;
432 uint32_t minor_ver = 1;
433 uint32_t major_ver = 0;
436 uint32_t ctx_attribs[2 * 4];
437 unsigned num_ctx_attribs = 0;
439 if (!psc->base.driScreen)
442 if (psc->swrast->base.version < 3)
445 /* Remap the GLX tokens to DRI2 tokens.
447 if (!dri2_convert_glx_attribs(num_attribs, attribs,
448 &major_ver, &minor_ver, &flags, &api,
453 pcp_shared = (struct drisw_context *) shareList;
454 shared = pcp_shared->driContext;
457 pcp = Xmalloc(sizeof *pcp);
461 memset(pcp, 0, sizeof *pcp);
462 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
467 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
468 ctx_attribs[num_ctx_attribs++] = major_ver;
469 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
470 ctx_attribs[num_ctx_attribs++] = minor_ver;
473 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
475 /* The current __DRI_CTX_FLAG_* values are identical to the
476 * GLX_CONTEXT_*_BIT values.
478 ctx_attribs[num_ctx_attribs++] = flags;
482 (*psc->swrast->createContextAttribs) (psc->driScreen,
490 if (pcp->driContext == NULL) {
495 pcp->base.vtable = &drisw_context_vtable;
501 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
503 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
504 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
506 (*psc->core->destroyDrawable) (pdp->driDrawable);
508 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
512 static __GLXDRIdrawable *
513 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
514 GLXDrawable drawable, struct glx_config *modes)
516 struct drisw_drawable *pdp;
517 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
518 struct drisw_screen *psc = (struct drisw_screen *) base;
520 const __DRIswrastExtension *swrast = psc->swrast;
522 pdp = Xmalloc(sizeof(*pdp));
526 memset(pdp, 0, sizeof *pdp);
527 pdp->base.xDrawable = xDrawable;
528 pdp->base.drawable = drawable;
529 pdp->base.psc = &psc->base;
531 ret = XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
537 /* Create a new drawable */
539 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
541 if (!pdp->driDrawable) {
542 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
547 pdp->base.destroyDrawable = driswDestroyDrawable;
553 driswSwapBuffers(__GLXDRIdrawable * pdraw,
554 int64_t target_msc, int64_t divisor, int64_t remainder)
556 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
557 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
563 (*psc->core->swapBuffers) (pdp->driDrawable);
569 driswDestroyScreen(struct glx_screen *base)
571 struct drisw_screen *psc = (struct drisw_screen *) base;
573 /* Free the direct rendering per screen data */
574 (*psc->core->destroyScreen) (psc->driScreen);
575 driDestroyConfigs(psc->driver_configs);
576 psc->driScreen = NULL;
578 dlclose(psc->driver);
587 driver = driOpenDriver("swrast");
592 static const struct glx_screen_vtable drisw_screen_vtable = {
593 drisw_create_context,
594 drisw_create_context_attribs
598 driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
602 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
604 if (psc->swrast->base.version >= 3) {
605 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
606 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
608 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
610 __glXEnableDirectExtension(&psc->base,
611 "GLX_EXT_create_context_es2_profile");
614 /* FIXME: Figure out what other extensions can be ported here from dri2. */
615 for (i = 0; extensions[i]; i++) {
616 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
617 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
618 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
623 static struct glx_screen *
624 driswCreateScreen(int screen, struct glx_display *priv)
627 const __DRIconfig **driver_configs;
628 const __DRIextension **extensions;
629 struct drisw_screen *psc;
630 struct glx_config *configs = NULL, *visuals = NULL;
633 psc = Xcalloc(1, sizeof *psc);
637 memset(psc, 0, sizeof *psc);
638 if (!glx_screen_init(&psc->base, screen, priv)) {
643 psc->driver = driOpenSwrast();
644 if (psc->driver == NULL)
647 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
648 if (extensions == NULL) {
649 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
653 for (i = 0; extensions[i]; i++) {
654 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
655 psc->core = (__DRIcoreExtension *) extensions[i];
656 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
657 psc->swrast = (__DRIswrastExtension *) extensions[i];
660 if (psc->core == NULL || psc->swrast == NULL) {
661 ErrorMessageF("core dri extension not found\n");
666 psc->swrast->createNewScreen(screen, loader_extensions,
667 &driver_configs, psc);
668 if (psc->driScreen == NULL) {
669 ErrorMessageF("failed to create dri screen\n");
673 extensions = psc->core->getExtensions(psc->driScreen);
674 driswBindExtensions(psc, extensions);
676 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
677 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
679 if (!configs || !visuals)
682 glx_config_destroy_list(psc->base.configs);
683 psc->base.configs = configs;
684 glx_config_destroy_list(psc->base.visuals);
685 psc->base.visuals = visuals;
687 psc->driver_configs = driver_configs;
689 psc->base.vtable = &drisw_screen_vtable;
691 psc->base.driScreen = psp;
692 psp->destroyScreen = driswDestroyScreen;
693 psp->createDrawable = driswCreateDrawable;
694 psp->swapBuffers = driswSwapBuffers;
700 glx_config_destroy_list(configs);
702 glx_config_destroy_list(visuals);
704 psc->core->destroyScreen(psc->driScreen);
705 psc->driScreen = NULL;
708 dlclose(psc->driver);
709 glx_screen_cleanup(&psc->base);
712 ErrorMessageF("reverting to indirect rendering\n");
717 /* Called from __glXFreeDisplayPrivate.
720 driswDestroyDisplay(__GLXDRIdisplay * dpy)
726 * Allocate, initialize and return a __DRIdisplayPrivate object.
727 * This is called from __glXInitialize() when we are given a new
730 _X_HIDDEN __GLXDRIdisplay *
731 driswCreateDisplay(Display * dpy)
733 struct drisw_display *pdpyp;
735 pdpyp = Xmalloc(sizeof *pdpyp);
739 pdpyp->base.destroyDisplay = driswDestroyDisplay;
740 pdpyp->base.createScreen = driswCreateScreen;
745 #endif /* GLX_DIRECT_RENDERING */