g3dvl: Use sobel filter for chroma interpolation
[mesa/nouveau-pmpeg.git] / src / glx / drisw_glx.c
bloba150c618b18df0ac886fa06462425d9ef64d7b1e
1 /*
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
13 * Software.
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
21 * SOFTWARE.
24 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
26 #include <X11/Xlib.h>
27 #include "glxclient.h"
28 #include <dlfcn.h>
29 #include "dri_common.h"
31 struct drisw_display
33 __GLXDRIdisplay base;
36 struct drisw_context
38 struct glx_context base;
39 __DRIcontext *driContext;
43 struct drisw_screen
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;
55 void *driver;
58 struct drisw_drawable
60 __GLXDRIdrawable base;
62 GC gc;
63 GC swapgc;
65 __DRIdrawable *driDrawable;
66 XVisualInfo *visinfo;
67 XImage *ximage;
70 static Bool
71 XCreateDrawable(struct drisw_drawable * pdp,
72 Display * dpy, XID drawable, int visualid)
74 XGCValues gcvalues;
75 long visMask;
76 XVisualInfo visTemp;
77 int num_visuals;
79 /* create GC's */
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);
89 /* visual */
90 visTemp.screen = DefaultScreen(dpy);
91 visTemp.visualid = visualid;
92 visMask = (VisualScreenMask | VisualIDMask);
93 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
95 /* create XImage */
96 pdp->ximage = XCreateImage(dpy,
97 pdp->visinfo->visual,
98 pdp->visinfo->depth,
99 ZPixmap, 0, /* format, offset */
100 NULL, /* data */
101 0, 0, /* width, height */
102 32, /* bitmap_pad */
103 0); /* bytes_per_line */
106 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
107 * the conversion for us.
109 if (pdp->ximage->bits_per_pixel == 24)
110 pdp->ximage->bits_per_pixel = 32;
112 return True;
115 static void
116 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
118 XDestroyImage(pdp->ximage);
119 XFree(pdp->visinfo);
121 XFreeGC(dpy, pdp->gc);
122 XFreeGC(dpy, pdp->swapgc);
126 * swrast loader functions
129 static void
130 swrastGetDrawableInfo(__DRIdrawable * draw,
131 int *x, int *y, int *w, int *h,
132 void *loaderPrivate)
134 struct drisw_drawable *pdp = loaderPrivate;
135 __GLXDRIdrawable *pdraw = &(pdp->base);
136 Display *dpy = pdraw->psc->dpy;
137 Drawable drawable;
139 Window root;
140 unsigned uw, uh, bw, depth;
142 drawable = pdraw->xDrawable;
144 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
145 *w = uw;
146 *h = uh;
150 * Align renderbuffer pitch.
152 * This should be chosen by the driver and the loader (libGL, xserver/glx)
153 * should use the driver provided pitch.
155 * It seems that the xorg loader (that is the xserver loading swrast_dri for
156 * indirect rendering, not client-side libGL) requires that the pitch is
157 * exactly the image width padded to 32 bits. XXX
159 * The above restriction can probably be overcome by using ScratchPixmap and
160 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
161 * the scratch pixmap to 'pitch / cpp'.
163 static inline int
164 bytes_per_line(unsigned pitch_bits, unsigned mul)
166 unsigned mask = mul - 1;
168 return ((pitch_bits + mask) & ~mask) / 8;
171 static void
172 swrastPutImage(__DRIdrawable * draw, int op,
173 int x, int y, int w, int h,
174 char *data, void *loaderPrivate)
176 struct drisw_drawable *pdp = loaderPrivate;
177 __GLXDRIdrawable *pdraw = &(pdp->base);
178 Display *dpy = pdraw->psc->dpy;
179 Drawable drawable;
180 XImage *ximage;
181 GC gc;
183 switch (op) {
184 case __DRI_SWRAST_IMAGE_OP_DRAW:
185 gc = pdp->gc;
186 break;
187 case __DRI_SWRAST_IMAGE_OP_SWAP:
188 gc = pdp->swapgc;
189 break;
190 default:
191 return;
194 drawable = pdraw->xDrawable;
196 ximage = pdp->ximage;
197 ximage->data = data;
198 ximage->width = w;
199 ximage->height = h;
200 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
202 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
204 ximage->data = NULL;
207 static void
208 swrastGetImage(__DRIdrawable * read,
209 int x, int y, int w, int h,
210 char *data, void *loaderPrivate)
212 struct drisw_drawable *prp = loaderPrivate;
213 __GLXDRIdrawable *pread = &(prp->base);
214 Display *dpy = pread->psc->dpy;
215 Drawable readable;
216 XImage *ximage;
218 readable = pread->xDrawable;
220 ximage = prp->ximage;
221 ximage->data = data;
222 ximage->width = w;
223 ximage->height = h;
224 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
226 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
228 ximage->data = NULL;
231 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
232 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
233 swrastGetDrawableInfo,
234 swrastPutImage,
235 swrastGetImage
238 static const __DRIextension *loader_extensions[] = {
239 &systemTimeExtension.base,
240 &swrastLoaderExtension.base,
241 NULL
245 * GLXDRI functions
248 static void
249 drisw_destroy_context(struct glx_context *context)
251 struct drisw_context *pcp = (struct drisw_context *) context;
252 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
254 driReleaseDrawables(&pcp->base);
256 if (context->xid)
257 glx_send_destroy_context(psc->base.dpy, context->xid);
259 if (context->extensions)
260 XFree((char *) context->extensions);
262 (*psc->core->destroyContext) (pcp->driContext);
264 Xfree(pcp);
267 static int
268 drisw_bind_context(struct glx_context *context, struct glx_context *old,
269 GLXDrawable draw, GLXDrawable read)
271 struct drisw_context *pcp = (struct drisw_context *) context;
272 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
273 struct drisw_drawable *pdraw, *pread;
275 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
276 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
278 driReleaseDrawables(&pcp->base);
280 if (pdraw == NULL || pread == NULL)
281 return GLXBadDrawable;
283 if ((*psc->core->bindContext) (pcp->driContext,
284 pdraw->driDrawable, pread->driDrawable))
285 return Success;
287 return GLXBadContext;
290 static void
291 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
293 struct drisw_context *pcp = (struct drisw_context *) context;
294 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
296 (*psc->core->unbindContext) (pcp->driContext);
299 static void
300 drisw_bind_tex_image(Display * dpy,
301 GLXDrawable drawable,
302 int buffer, const int *attrib_list)
304 struct glx_context *gc = __glXGetCurrentContext();
305 struct drisw_context *pcp = (struct drisw_context *) gc;
306 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
307 struct glx_display *dpyPriv = __glXInitialize(dpy);
308 struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
309 struct drisw_screen *psc;
311 if (pdraw != NULL) {
312 psc = (struct drisw_screen *) base->psc;
314 if (!psc->texBuffer)
315 return;
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,
322 pdraw->driDrawable);
324 else {
325 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
326 pdraw->base.textureTarget,
327 pdraw->driDrawable);
332 static void
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;
343 if (pdraw != NULL) {
344 psc = (struct dri2_screen *) base->psc;
346 if (!psc->texBuffer)
347 return;
349 if (psc->texBuffer->base.version >= 3 &&
350 psc->texBuffer->releaseTexBuffer != NULL) {
351 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
352 pdraw->base.textureTarget,
353 pdraw->driDrawable);
356 #endif
359 static const struct glx_context_vtable drisw_context_vtable = {
360 drisw_destroy_context,
361 drisw_bind_context,
362 drisw_unbind_context,
363 NULL,
364 NULL,
365 DRI_glXUseXFont,
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)
382 return NULL;
384 if (shareList) {
385 pcp_shared = (struct drisw_context *) shareList;
386 shared = pcp_shared->driContext;
389 pcp = Xmalloc(sizeof *pcp);
390 if (pcp == NULL)
391 return NULL;
393 memset(pcp, 0, sizeof *pcp);
394 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
395 Xfree(pcp);
396 return NULL;
399 pcp->driContext =
400 (*psc->core->createNewContext) (psc->driScreen,
401 config->driConfig, shared, pcp);
402 if (pcp->driContext == NULL) {
403 Xfree(pcp);
404 return NULL;
407 pcp->base.vtable = &drisw_context_vtable;
409 return &pcp->base;
412 static void
413 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
415 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
416 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
418 (*psc->core->destroyDrawable) (pdp->driDrawable);
420 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
421 Xfree(pdp);
424 static __GLXDRIdrawable *
425 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
426 GLXDrawable drawable, struct glx_config *modes)
428 struct drisw_drawable *pdp;
429 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
430 struct drisw_screen *psc = (struct drisw_screen *) base;
432 const __DRIswrastExtension *swrast = psc->swrast;
434 pdp = Xmalloc(sizeof(*pdp));
435 if (!pdp)
436 return NULL;
438 memset(pdp, 0, sizeof *pdp);
439 pdp->base.xDrawable = xDrawable;
440 pdp->base.drawable = drawable;
441 pdp->base.psc = &psc->base;
443 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
445 /* Create a new drawable */
446 pdp->driDrawable =
447 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
449 if (!pdp->driDrawable) {
450 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
451 Xfree(pdp);
452 return NULL;
455 pdp->base.destroyDrawable = driswDestroyDrawable;
457 return &pdp->base;
460 static int64_t
461 driswSwapBuffers(__GLXDRIdrawable * pdraw,
462 int64_t target_msc, int64_t divisor, int64_t remainder)
464 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
465 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
467 (void) target_msc;
468 (void) divisor;
469 (void) remainder;
471 (*psc->core->swapBuffers) (pdp->driDrawable);
473 return 0;
476 static void
477 driswDestroyScreen(struct glx_screen *base)
479 struct drisw_screen *psc = (struct drisw_screen *) base;
481 /* Free the direct rendering per screen data */
482 (*psc->core->destroyScreen) (psc->driScreen);
483 driDestroyConfigs(psc->driver_configs);
484 psc->driScreen = NULL;
485 if (psc->driver)
486 dlclose(psc->driver);
489 static void *
490 driOpenSwrast(void)
492 void *driver = NULL;
494 if (driver == NULL)
495 driver = driOpenDriver("swrast");
497 return driver;
500 static const struct glx_screen_vtable drisw_screen_vtable = {
501 drisw_create_context
504 static void
505 driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
507 int i;
509 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
511 /* FIXME: Figure out what other extensions can be ported here from dri2. */
512 for (i = 0; extensions[i]; i++) {
513 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
514 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
515 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
520 static struct glx_screen *
521 driswCreateScreen(int screen, struct glx_display *priv)
523 __GLXDRIscreen *psp;
524 const __DRIconfig **driver_configs;
525 const __DRIextension **extensions;
526 struct drisw_screen *psc;
527 int i;
529 psc = Xcalloc(1, sizeof *psc);
530 if (psc == NULL)
531 return NULL;
533 memset(psc, 0, sizeof *psc);
534 if (!glx_screen_init(&psc->base, screen, priv)) {
535 Xfree(psc);
536 return NULL;
539 psc->driver = driOpenSwrast();
540 if (psc->driver == NULL)
541 goto handle_error;
543 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
544 if (extensions == NULL) {
545 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
546 goto handle_error;
549 for (i = 0; extensions[i]; i++) {
550 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
551 psc->core = (__DRIcoreExtension *) extensions[i];
552 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
553 psc->swrast = (__DRIswrastExtension *) extensions[i];
556 if (psc->core == NULL || psc->swrast == NULL) {
557 ErrorMessageF("core dri extension not found\n");
558 goto handle_error;
561 psc->driScreen =
562 psc->swrast->createNewScreen(screen, loader_extensions,
563 &driver_configs, psc);
564 if (psc->driScreen == NULL) {
565 ErrorMessageF("failed to create dri screen\n");
566 goto handle_error;
569 extensions = psc->core->getExtensions(psc->driScreen);
570 driswBindExtensions(psc, extensions);
572 psc->base.configs =
573 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
574 psc->base.visuals =
575 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
577 psc->driver_configs = driver_configs;
579 psc->base.vtable = &drisw_screen_vtable;
580 psp = &psc->vtable;
581 psc->base.driScreen = psp;
582 psp->destroyScreen = driswDestroyScreen;
583 psp->createDrawable = driswCreateDrawable;
584 psp->swapBuffers = driswSwapBuffers;
586 return &psc->base;
588 handle_error:
589 if (psc->driver)
590 dlclose(psc->driver);
591 glx_screen_cleanup(&psc->base);
592 Xfree(psc);
594 ErrorMessageF("reverting to indirect rendering\n");
596 return NULL;
599 /* Called from __glXFreeDisplayPrivate.
601 static void
602 driswDestroyDisplay(__GLXDRIdisplay * dpy)
604 Xfree(dpy);
608 * Allocate, initialize and return a __DRIdisplayPrivate object.
609 * This is called from __glXInitialize() when we are given a new
610 * display pointer.
612 _X_HIDDEN __GLXDRIdisplay *
613 driswCreateDisplay(Display * dpy)
615 struct drisw_display *pdpyp;
617 pdpyp = Xmalloc(sizeof *pdpyp);
618 if (pdpyp == NULL)
619 return NULL;
621 pdpyp->base.destroyDisplay = driswDestroyDisplay;
622 pdpyp->base.createScreen = driswCreateScreen;
624 return &pdpyp->base;
627 #endif /* GLX_DIRECT_RENDERING */