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;
437 uint32_t ctx_attribs[2 * 4];
438 unsigned num_ctx_attribs = 0;
440 if (!psc->base.driScreen)
443 if (psc->swrast->base.version < 3)
446 /* Remap the GLX tokens to DRI2 tokens.
448 if (!dri2_convert_glx_attribs(num_attribs, attribs,
449 &major_ver, &minor_ver, &flags, &api, &reset,
453 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION)
457 pcp_shared = (struct drisw_context *) shareList;
458 shared = pcp_shared->driContext;
461 pcp = Xmalloc(sizeof *pcp);
465 memset(pcp, 0, sizeof *pcp);
466 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
471 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
472 ctx_attribs[num_ctx_attribs++] = major_ver;
473 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
474 ctx_attribs[num_ctx_attribs++] = minor_ver;
477 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
479 /* The current __DRI_CTX_FLAG_* values are identical to the
480 * GLX_CONTEXT_*_BIT values.
482 ctx_attribs[num_ctx_attribs++] = flags;
486 (*psc->swrast->createContextAttribs) (psc->driScreen,
494 if (pcp->driContext == NULL) {
499 pcp->base.vtable = &drisw_context_vtable;
505 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
507 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
508 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
510 (*psc->core->destroyDrawable) (pdp->driDrawable);
512 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
516 static __GLXDRIdrawable *
517 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
518 GLXDrawable drawable, struct glx_config *modes)
520 struct drisw_drawable *pdp;
521 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
522 struct drisw_screen *psc = (struct drisw_screen *) base;
524 const __DRIswrastExtension *swrast = psc->swrast;
526 pdp = Xmalloc(sizeof(*pdp));
530 memset(pdp, 0, sizeof *pdp);
531 pdp->base.xDrawable = xDrawable;
532 pdp->base.drawable = drawable;
533 pdp->base.psc = &psc->base;
535 ret = XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
541 /* Create a new drawable */
543 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
545 if (!pdp->driDrawable) {
546 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
551 pdp->base.destroyDrawable = driswDestroyDrawable;
557 driswSwapBuffers(__GLXDRIdrawable * pdraw,
558 int64_t target_msc, int64_t divisor, int64_t remainder)
560 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
561 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
567 (*psc->core->swapBuffers) (pdp->driDrawable);
573 driswDestroyScreen(struct glx_screen *base)
575 struct drisw_screen *psc = (struct drisw_screen *) base;
577 /* Free the direct rendering per screen data */
578 (*psc->core->destroyScreen) (psc->driScreen);
579 driDestroyConfigs(psc->driver_configs);
580 psc->driScreen = NULL;
582 dlclose(psc->driver);
585 #define SWRAST_DRIVER_NAME "swrast"
593 driver = driOpenDriver(SWRAST_DRIVER_NAME);
598 static const struct glx_screen_vtable drisw_screen_vtable = {
599 drisw_create_context,
600 drisw_create_context_attribs
604 driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
608 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
610 if (psc->swrast->base.version >= 3) {
611 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
612 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
614 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
616 __glXEnableDirectExtension(&psc->base,
617 "GLX_EXT_create_context_es2_profile");
620 /* FIXME: Figure out what other extensions can be ported here from dri2. */
621 for (i = 0; extensions[i]; i++) {
622 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
623 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
624 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
629 static struct glx_screen *
630 driswCreateScreen(int screen, struct glx_display *priv)
633 const __DRIconfig **driver_configs;
634 const __DRIextension **extensions;
635 struct drisw_screen *psc;
636 struct glx_config *configs = NULL, *visuals = NULL;
639 psc = Xcalloc(1, sizeof *psc);
643 memset(psc, 0, sizeof *psc);
644 if (!glx_screen_init(&psc->base, screen, priv)) {
649 psc->driver = driOpenSwrast();
650 if (psc->driver == NULL)
653 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
654 if (extensions == NULL) {
655 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
659 for (i = 0; extensions[i]; i++) {
660 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
661 psc->core = (__DRIcoreExtension *) extensions[i];
662 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
663 psc->swrast = (__DRIswrastExtension *) extensions[i];
666 if (psc->core == NULL || psc->swrast == NULL) {
667 ErrorMessageF("core dri extension not found\n");
672 psc->swrast->createNewScreen(screen, loader_extensions,
673 &driver_configs, psc);
674 if (psc->driScreen == NULL) {
675 ErrorMessageF("failed to create dri screen\n");
679 extensions = psc->core->getExtensions(psc->driScreen);
680 driswBindExtensions(psc, extensions);
682 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
683 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
685 if (!configs || !visuals)
688 glx_config_destroy_list(psc->base.configs);
689 psc->base.configs = configs;
690 glx_config_destroy_list(psc->base.visuals);
691 psc->base.visuals = visuals;
693 psc->driver_configs = driver_configs;
695 psc->base.vtable = &drisw_screen_vtable;
697 psc->base.driScreen = psp;
698 psp->destroyScreen = driswDestroyScreen;
699 psp->createDrawable = driswCreateDrawable;
700 psp->swapBuffers = driswSwapBuffers;
706 glx_config_destroy_list(configs);
708 glx_config_destroy_list(visuals);
710 psc->core->destroyScreen(psc->driScreen);
711 psc->driScreen = NULL;
714 dlclose(psc->driver);
715 glx_screen_cleanup(&psc->base);
718 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME);
723 /* Called from __glXFreeDisplayPrivate.
726 driswDestroyDisplay(__GLXDRIdisplay * dpy)
732 * Allocate, initialize and return a __DRIdisplayPrivate object.
733 * This is called from __glXInitialize() when we are given a new
736 _X_HIDDEN __GLXDRIdisplay *
737 driswCreateDisplay(Display * dpy)
739 struct drisw_display *pdpyp;
741 pdpyp = Xmalloc(sizeof *pdpyp);
745 pdpyp->base.destroyDisplay = driswDestroyDisplay;
746 pdpyp->base.createScreen = driswCreateScreen;
751 #endif /* GLX_DIRECT_RENDERING */