Revision: 170 http://trac.macosforge.org/projects/xquartz/changeset/170 Author: gstaplin@apple.com Date: 2009-02-18 20:22:27 -0800 (Wed, 18 Feb 2009) Log Message: ----------- Add create/destroy test for GLXPixmaps. Modified Paths: -------------- AppleSGLX/trunk/tests/glxpixmap/glxpixmap.mk AppleSGLX/trunk/tests/tests.mk Added Paths: ----------- AppleSGLX/trunk/tests/glxpixmap/glxpixmap_create_destroy.c Modified: AppleSGLX/trunk/tests/glxpixmap/glxpixmap.mk =================================================================== --- AppleSGLX/trunk/tests/glxpixmap/glxpixmap.mk 2009-02-18 19:52:08 UTC (rev 169) +++ AppleSGLX/trunk/tests/glxpixmap/glxpixmap.mk 2009-02-19 04:22:27 UTC (rev 170) @@ -1,2 +1,6 @@ $(TEST_BUILD_DIR)/glxpixmap: tests/glxpixmap/glxpixmap.c $(LIBGL) $(CC) tests/glxpixmap/glxpixmap.c -Iinclude -I/usr/X11/include -o $(TEST_BUILD_DIR)/glxpixmap $(LINK_TEST) + +$(TEST_BUILD_DIR)/glxpixmap_create_destroy: tests/glxpixmap/glxpixmap_create_destroy.c $(LIBGL) + $(CC) tests/glxpixmap/glxpixmap_create_destroy.c -Iinclude -I/usr/X11/include -o $(TEST_BUILD_DIR)/glxpixmap_create_destroy $(LINK_TEST) + Added: AppleSGLX/trunk/tests/glxpixmap/glxpixmap_create_destroy.c =================================================================== --- AppleSGLX/trunk/tests/glxpixmap/glxpixmap_create_destroy.c (rev 0) +++ AppleSGLX/trunk/tests/glxpixmap/glxpixmap_create_destroy.c 2009-02-19 04:22:27 UTC (rev 170) @@ -0,0 +1,212 @@ + + +/* + * A demonstration of using the GLXPixmap functions. This program is in + * the public domain. + * + * Brian Paul + */ + + +#include <GL/gl.h> +#include <GL/glx.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static GLXContext ctx; +static XVisualInfo *visinfo; +static GC gc; + +static Window make_rgb_window( Display *dpy, + unsigned int width, unsigned int height ) +{ + const int sbAttrib[] = { GLX_RGBA, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + None }; + const int dbAttrib[] = { GLX_RGBA, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DOUBLEBUFFER, + None }; + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + + scrnum = DefaultScreen( dpy ); + root = RootWindow( dpy, scrnum ); + + visinfo = glXChooseVisual( dpy, scrnum, (int *) sbAttrib ); + if (!visinfo) { + visinfo = glXChooseVisual( dpy, scrnum, (int *) dbAttrib ); + if (!visinfo) { + printf("Error: couldn't get an RGB visual\n"); + exit(1); + } + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + /* TODO: share root colormap if possible */ + attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( dpy, root, 0, 0, width, height, + 0, visinfo->depth, InputOutput, + visinfo->visual, mask, &attr ); + + /* make an X GC so we can do XCopyArea later */ + gc = XCreateGC( dpy, win, 0, NULL ); + + /* need indirect context */ + ctx = glXCreateContext( dpy, visinfo, NULL, False ); + if (!ctx) { + printf("Error: glXCreateContext failed\n"); + exit(-1); + } + + printf("Direct rendering: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No"); + + return win; +} + + +static GLXPixmap make_pixmap( Display *dpy, Window win, + unsigned int width, unsigned int height, + Pixmap *pixmap) +{ + Pixmap pm; + GLXPixmap glxpm; + XWindowAttributes attr; + + pm = XCreatePixmap( dpy, win, width, height, visinfo->depth ); + if (!pm) { + printf("Error: XCreatePixmap failed\n"); + exit(-1); + } + + XGetWindowAttributes( dpy, win, &attr ); + + printf("pm %lx\n", pm); + /* + * IMPORTANT: + * Use the glXCreateGLXPixmapMESA funtion when using Mesa because + * Mesa needs to know the colormap associated with a pixmap in order + * to render correctly. This is because Mesa allows RGB rendering + * into any kind of visual, not just TrueColor or DirectColor. + */ +#ifdef GLX_MESA_pixmap_colormap + if (strstr(glXQueryExtensionsString(dpy, 0), "GLX_MESA_pixmap_colormap")) { + /* stand-alone Mesa, specify the colormap */ + glxpm = glXCreateGLXPixmapMESA( dpy, visinfo, pm, attr.colormap ); + } + else { + glxpm = glXCreateGLXPixmap( dpy, visinfo, pm ); + } +#else + /* This will work with Mesa too if the visual is TrueColor or DirectColor */ + glxpm = glXCreateGLXPixmap( dpy, visinfo, pm ); +#endif + + if (!glxpm) { + printf("Error: GLXCreateGLXPixmap failed\n"); + exit(-1); + } + + *pixmap = pm; + + return glxpm; +} + + + +static void event_loop( Display *dpy, GLXPixmap pm ) +{ + XEvent event; + + while (1) { + XNextEvent( dpy, &event ); + + switch (event.type) { + case Expose: + printf("Redraw\n"); + /* copy the image from GLXPixmap to window */ + XCopyArea( dpy, pm, event.xany.window, /* src, dest */ + gc, 0, 0, 300, 300, /* gc, src pos, size */ + 0, 0 ); /* dest pos */ + break; + case ConfigureNotify: + /* nothing */ + break; + } + } +} + + + +int main( int argc, char *argv[] ) +{ + Display *dpy; + Window win; + Pixmap pm; + GLXPixmap glxpm; + int eventbase, errorbase; + + dpy = XOpenDisplay(NULL); + + if(NULL == dpy) { + fprintf(stderr, "error: opening display\n"); + return EXIT_FAILURE; + } + + if(!glXQueryExtension(dpy, &eventbase, &errorbase)) { + fprintf(stderr, "GLX is not available!\n"); + return EXIT_FAILURE; + } + + + win = make_rgb_window( dpy, 300, 300 ); + + XMapWindow(dpy, win); + + while(1) { + glxpm = make_pixmap( dpy, win, 300, 300, &pm ); + + printf("glxpm 0x%lx\n", glxpm); + + if(!glXMakeCurrent(dpy, glxpm, ctx)) { + fprintf(stderr, "glXMakeCurrent failed!\n"); + return EXIT_FAILURE; + } + + printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER)); + + /* Render an image into the pixmap */ + glShadeModel( GL_FLAT ); + glClearColor( 0.5, 0.5, 0.5, 1.0 ); + glClear( GL_COLOR_BUFFER_BIT ); + glViewport( 0, 0, 300, 300 ); + glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 ); + glColor3f( 0.0, 0.0, 1.0 ); + glRectf( -0.75, -0.75, 0.75, 0.75 ); + glFlush(); + + glFinish(); + + glXMakeCurrent(dpy, None, ctx); + + glXDestroyGLXPixmap(dpy, glxpm); + } + + /* NOT REACHED */ + event_loop( dpy, pm ); + + return 0; +} Modified: AppleSGLX/trunk/tests/tests.mk =================================================================== --- AppleSGLX/trunk/tests/tests.mk 2009-02-18 19:52:08 UTC (rev 169) +++ AppleSGLX/trunk/tests/tests.mk 2009-02-19 04:22:27 UTC (rev 170) @@ -25,4 +25,5 @@ $(TEST_BUILD_DIR)/create_destroy_context_alone \ $(TEST_BUILD_DIR)/create_destroy_context_with_drawable \ $(TEST_BUILD_DIR)/create_destroy_context_with_drawable_2 \ - $(TEST_BUILD_DIR)/render_types + $(TEST_BUILD_DIR)/render_types \ + $(TEST_BUILD_DIR)/glxpixmap_create_destroy