X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=glws_glx.cpp;h=3c49de81309dd25983da7a3eca51efee5d14f3a1;hb=9b42f061b37a00a0bcd314a830924803a956e17d;hp=1c73564d0ccb7ccab6ce586ab9e722535b2e5998;hpb=9a0ed9ce37a521e78fdfdfe3fa6042dcb6c08d39;p=apitrace diff --git a/glws_glx.cpp b/glws_glx.cpp index 1c73564..3c49de8 100644 --- a/glws_glx.cpp +++ b/glws_glx.cpp @@ -23,14 +23,14 @@ * **************************************************************************/ +#include #include + #include #include "glws.hpp" -#include -#include -#include +#include "glproc.hpp" namespace glws { @@ -39,18 +39,25 @@ namespace glws { static Display *display = NULL; static int screen = 0; +static unsigned glxVersion = 0; +static const char *extensions = 0; +static bool has_GLX_ARB_create_context = false; + class GlxVisual : public Visual { public: + GLXFBConfig fbconfig; XVisualInfo *visinfo; - GlxVisual(XVisualInfo *vi) : - visinfo(vi) + GlxVisual() : + fbconfig(0), + visinfo(0) {} ~GlxVisual() { XFree(visinfo); + XFree(fbconfig); } }; @@ -80,17 +87,6 @@ static void describeEvent(const XEvent &event) { } } -static void waitForEvent(Window window, int type) { - XFlush(display); - XEvent event; - do { - XNextEvent(display, &event); - describeEvent(event); - } while (event.type != type || - event.xany.window != window); -} - - class GlxDrawable : public Drawable { public: @@ -108,7 +104,7 @@ public: attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone); - attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + attr.event_mask = StructureNotifyMask; unsigned long mask; mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; @@ -141,12 +137,24 @@ public: glXWaitX(); } + void waitForEvent(int type) { + XEvent event; + do { + XWindowEvent(display, window, StructureNotifyMask, &event); + describeEvent(event); + } while (event.type != type); + } + ~GlxDrawable() { XDestroyWindow(display, window); } void resize(int w, int h) { + if (w == width && h == height) { + return; + } + glXWaitGL(); // We need to ensure that pending events are processed here, and XSync @@ -159,27 +167,31 @@ public: XResizeWindow(display, window, w, h); // Tell the window manager to respect the requested size - XSizeHints *size_hints; - size_hints = XAllocSizeHints(); - size_hints->max_width = size_hints->min_width = w; - size_hints->max_height = size_hints->min_height = h; - size_hints->flags = PMinSize | PMaxSize; - XSetWMNormalHints(display, window, size_hints); - XFree(size_hints); + XSizeHints size_hints; + size_hints.max_width = size_hints.min_width = w; + size_hints.max_height = size_hints.min_height = h; + size_hints.flags = PMinSize | PMaxSize; + XSetWMNormalHints(display, window, &size_hints); - waitForEvent(window, ConfigureNotify); + waitForEvent(ConfigureNotify); glXWaitX(); } void show(void) { - if (!visible) { - XMapWindow(display, window); + if (visible) { + return; + } - waitForEvent(window, Expose); + glXWaitGL(); - Drawable::show(); - } + XMapWindow(display, window); + + waitForEvent(MapNotify); + + glXWaitX(); + + Drawable::show(); } void swapBuffers(void) { @@ -205,14 +217,20 @@ public: void init(void) { + display = XOpenDisplay(NULL); if (!display) { - display = XOpenDisplay(NULL); - if (!display) { - std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n"; - exit(1); - } - screen = DefaultScreen(display); + std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n"; + exit(1); } + + screen = DefaultScreen(display); + + int major = 0, minor = 0; + glXQueryVersion(display, &major, &minor); + glxVersion = (major << 8) | minor; + + extensions = glXQueryExtensionsString(display, screen); + has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions); } void @@ -225,34 +243,47 @@ cleanup(void) { Visual * createVisual(bool doubleBuffer) { - int single_attribs[] = { - GLX_RGBA, - GLX_RED_SIZE, 1, - GLX_GREEN_SIZE, 1, - GLX_BLUE_SIZE, 1, - GLX_ALPHA_SIZE, 1, - GLX_DEPTH_SIZE, 1, - GLX_STENCIL_SIZE, 1, - None - }; - - int double_attribs[] = { - GLX_RGBA, - GLX_RED_SIZE, 1, - GLX_GREEN_SIZE, 1, - GLX_BLUE_SIZE, 1, - GLX_ALPHA_SIZE, 1, - GLX_DOUBLEBUFFER, - GLX_DEPTH_SIZE, 1, - GLX_STENCIL_SIZE, 1, - None - }; - - XVisualInfo *visinfo; + GlxVisual *visual = new GlxVisual; + + if (glxVersion >= 0x0103) { + Attributes attribs; + attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT); + attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT); + attribs.add(GLX_RED_SIZE, 1); + attribs.add(GLX_GREEN_SIZE, 1); + attribs.add(GLX_BLUE_SIZE, 1); + attribs.add(GLX_ALPHA_SIZE, 1); + attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE); + attribs.add(GLX_DEPTH_SIZE, 1); + attribs.add(GLX_STENCIL_SIZE, 1); + attribs.end(); + + int num_configs = 0; + GLXFBConfig * fbconfigs; + fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs); + assert(num_configs && fbconfigs); + visual->fbconfig = fbconfigs[0]; + assert(visual->fbconfig); + visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig); + assert(visual->visinfo); + } else { + Attributes attribs; + attribs.add(GLX_RGBA); + attribs.add(GLX_RED_SIZE, 1); + attribs.add(GLX_GREEN_SIZE, 1); + attribs.add(GLX_BLUE_SIZE, 1); + attribs.add(GLX_ALPHA_SIZE, 1); + if (doubleBuffer) { + attribs.add(GLX_DOUBLEBUFFER); + } + attribs.add(GLX_DEPTH_SIZE, 1); + attribs.add(GLX_STENCIL_SIZE, 1); + attribs.end(); - visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs); + visual->visinfo = glXChooseVisual(display, screen, attribs); + } - return new GlxVisual(visinfo); + return visual; } Drawable * @@ -262,9 +293,9 @@ createDrawable(const Visual *visual, int width, int height) } Context * -createContext(const Visual *visual, Context *shareContext) +createContext(const Visual *_visual, Context *shareContext) { - XVisualInfo *visinfo = dynamic_cast(visual)->visinfo; + const GlxVisual *visual = dynamic_cast(_visual); GLXContext share_context = NULL; GLXContext context; @@ -272,8 +303,22 @@ createContext(const Visual *visual, Context *shareContext) share_context = dynamic_cast(shareContext)->context; } - context = glXCreateContext(display, visinfo, - share_context, True); + if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) { + Attributes attribs; + attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE); + if (debug) { + attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB); + } + attribs.end(); + + context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs); + } else + if (glxVersion >= 0x103) { + context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True); + } else { + context = glXCreateContext(display, visual->visinfo, share_context, True); + } + return new GlxContext(visual, context); } @@ -292,7 +337,6 @@ makeCurrent(Drawable *drawable, Context *context) bool processEvents(void) { - XFlush(display); while (XPending(display) > 0) { XEvent event; XNextEvent(display, &event);