From: José Fonseca Date: Sat, 2 Apr 2011 19:22:17 +0000 (+0100) Subject: Separate WS abstraction from retracing. X-Git-Url: https://git.notmuchmail.org/git?a=commitdiff_plain;h=818d47d1c34f6e23e42e846ada471d0124e38ebe;p=apitrace Separate WS abstraction from retracing. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d919e8..f6802e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,8 +195,10 @@ endif () if (WIN32) set (os os_win32.cpp) + set (glws glws_wgl.cpp) else (WIN32) set (os os_posix.cpp) + set (glws glws_glx.cpp) endif (WIN32) add_library (trace trace_model.cpp trace_parser.cpp ${os}) @@ -223,8 +225,8 @@ include_directories ( add_executable (glretrace glretrace.cpp - glretrace_xlib.cpp glstate.cpp + ${glws} image.cpp ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp ) diff --git a/glretrace.hpp b/glretrace.hpp deleted file mode 100644 index a275f52..0000000 --- a/glretrace.hpp +++ /dev/null @@ -1,123 +0,0 @@ -/************************************************************************** - * - * Copyright 2011 Jose Fonseca - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - **************************************************************************/ - -/* - * Abstraction for GL window system specific APIs (GLX, WGL). - */ - -#ifndef _GLRETRACE_HPP_ -#define _GLRETRACE_HPP_ - - -namespace glretrace { - - -class Visual -{ -public: - unsigned long redMask; - unsigned long greenMask; - unsigned long blueMask; - unsigned long alphaMask; - bool doubleBuffer; - - virtual ~Visual() {} -}; - - -class Drawable -{ -public: - const Visual *visual; - unsigned width; - unsigned height; - - Drawable(const Visual *vis) : - visual(vis) - {} - - virtual ~Drawable() {} - - virtual void - resize(unsigned w, unsigned h) { - width = w; - height = h; - } - - virtual void swapBuffers(void) = 0; -}; - - -class Context -{ -public: - const Visual *visual; - - Context(const Visual *vis) : - visual(vis) - {} - - virtual ~Context() {} -}; - - -class WindowSystem -{ -public: - Drawable *currentDrawable; - Context *currentContext; - - - inline WindowSystem() : - currentDrawable(NULL), - currentContext(NULL) - {} - - virtual ~WindowSystem() {} - - virtual Visual * - createVisual(bool doubleBuffer=false) = 0; - - virtual Drawable * - createDrawable(const Visual *visual) = 0; - - virtual Context * - createContext(const Visual *visual) = 0; - - virtual bool - makeCurrent(Drawable *drawable, Context *context) = 0; - - virtual bool - processEvents(void) = 0; -}; - - -WindowSystem *createNativeWindowSystem(void); - - -} /* namespace glretrace */ - - -#endif /* _GLRETRACE_HPP_ */ diff --git a/glretrace.py b/glretrace.py index 2f2b8ff..312c9a3 100644 --- a/glretrace.py +++ b/glretrace.py @@ -171,7 +171,7 @@ if __name__ == '__main__': #include "glproc.hpp" #include "glstate.hpp" -#include "glretrace.hpp" +#include "glws.hpp" static bool double_buffer = false; static bool insideGlBeginEnd = false; @@ -240,10 +240,10 @@ checkGlError(void) { print r''' static Trace::Parser parser; -static glretrace::WindowSystem *__ws = NULL; -static glretrace::Visual *__visual = NULL; -static glretrace::Drawable *__drawable = NULL; -static glretrace::Context *__context = NULL; +static glws::WindowSystem *__ws = NULL; +static glws::Visual *__visual = NULL; +static glws::Drawable *__drawable = NULL; +static glws::Context *__context = NULL; #include "image.hpp" @@ -421,7 +421,7 @@ int main(int argc, char **argv) } } - __ws = glretrace::createNativeWindowSystem(); + __ws = glws::createNativeWindowSystem(); __visual = __ws->createVisual(double_buffer); __drawable = __ws->createDrawable(__visual); __drawable->resize(__window_width, __window_height); diff --git a/glretrace_xlib.cpp b/glretrace_xlib.cpp deleted file mode 100644 index a6764c5..0000000 --- a/glretrace_xlib.cpp +++ /dev/null @@ -1,232 +0,0 @@ -/************************************************************************** - * - * Copyright 2011 Jose Fonseca - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - **************************************************************************/ - -#include "glimports.hpp" -#include "glretrace.hpp" - - -namespace glretrace { - - -class XlibVisual : public Visual -{ -public: - XVisualInfo *visinfo; - - XlibVisual(XVisualInfo *vi) : - visinfo(vi) - {} - - ~XlibVisual() { - XFree(visinfo); - } -}; - - -class XlibDrawable : public Drawable -{ -public: - Display *display; - Window window; - - XlibDrawable(const Visual *vis, Display *dpy, Window win) : - Drawable(vis), - display(dpy), - window(win) - {} - - ~XlibDrawable() { - XDestroyWindow(display, window); - } - - void - resize(unsigned w, unsigned h) { - Drawable::resize(w, h); - XResizeWindow(display, window, w, h); - } - - void swapBuffers(void) { - glXSwapBuffers(display, window); - } -}; - - -class XlibContext : public Context -{ -public: - Display *display; - GLXContext context; - - XlibContext(const Visual *vis, Display *dpy, GLXContext ctx) : - Context(vis), - display(dpy), - context(ctx) - {} - - ~XlibContext() { - glXDestroyContext(display, context); - } -}; - - -class XlibWindowSystem : public WindowSystem -{ -private: - Display *display; - int screen; - -public: - Drawable *currentDrawable; - Context *currentContext; - - XlibWindowSystem() { - display = XOpenDisplay(NULL); - screen = DefaultScreen(display); - } - - ~XlibWindowSystem() { - XCloseDisplay(display); - } - - Visual * - createVisual(bool doubleBuffer) { - int single_attribs[] = { - GLX_RGBA, - GLX_RED_SIZE, 1, - GLX_GREEN_SIZE, 1, - GLX_BLUE_SIZE, 1, - GLX_DEPTH_SIZE, 1, - None - }; - - int double_attribs[] = { - GLX_RGBA, - GLX_RED_SIZE, 1, - GLX_GREEN_SIZE, 1, - GLX_BLUE_SIZE, 1, - GLX_DOUBLEBUFFER, - GLX_DEPTH_SIZE, 1, - None - }; - - XVisualInfo *visinfo; - - visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs); - - return new XlibVisual(visinfo); - } - - Drawable * - createDrawable(const Visual *visual) - { - XVisualInfo *visinfo = dynamic_cast(visual)->visinfo; - - Window root = RootWindow(display, screen); - - /* window attributes */ - XSetWindowAttributes attr; - attr.background_pixel = 0; - attr.border_pixel = 0; - attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone); - attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; - - unsigned long mask; - mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; - - int x = 0, y = 0, width = 256, height = 256; - - Window window = XCreateWindow( - display, root, - x, y, width, height, - 0, - visinfo->depth, - InputOutput, - visinfo->visual, - mask, - &attr); - - XSizeHints sizehints; - sizehints.x = x; - sizehints.y = y; - sizehints.width = width; - sizehints.height = height; - sizehints.flags = USSize | USPosition; - XSetNormalHints(display, window, &sizehints); - - const char *name = "glretrace"; - XSetStandardProperties( - display, window, name, name, - None, (char **)NULL, 0, &sizehints); - - XMapWindow(display, window); - - return new XlibDrawable(visual, display, window); - } - - Context * - createContext(const Visual *visual) - { - XVisualInfo *visinfo = dynamic_cast(visual)->visinfo; - GLXContext context = glXCreateContext(display, visinfo, NULL, True); - return new XlibContext(visual, display, context); - } - - bool - makeCurrent(Drawable *drawable, Context *context) - { - Window win = drawable ? dynamic_cast(drawable)->window : NULL; - GLXContext ctx = context ? dynamic_cast(context)->context : NULL; - - bool ret = glXMakeCurrent(display, win, ctx); - - if (drawable && context && ret) { - currentDrawable = drawable; - currentContext = context; - } else { - currentDrawable = NULL; - currentContext = NULL; - } - - return ret; - } - - bool - processEvents(void) { - while (XPending(display) > 0) { - XEvent event; - XNextEvent(display, &event); - // TODO - } - return true; - } -}; - - -WindowSystem *createNativeWindowSystem(void) { - return new XlibWindowSystem(); -} - - -} /* namespace glretrace */ diff --git a/glws.hpp b/glws.hpp new file mode 100644 index 0000000..8f4a845 --- /dev/null +++ b/glws.hpp @@ -0,0 +1,123 @@ +/************************************************************************** + * + * Copyright 2011 Jose Fonseca + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + +/* + * Abstraction for GL window system specific APIs (GLX, WGL). + */ + +#ifndef _GLWS_HPP_ +#define _GLWS_HPP_ + + +namespace glws { + + +class Visual +{ +public: + unsigned long redMask; + unsigned long greenMask; + unsigned long blueMask; + unsigned long alphaMask; + bool doubleBuffer; + + virtual ~Visual() {} +}; + + +class Drawable +{ +public: + const Visual *visual; + unsigned width; + unsigned height; + + Drawable(const Visual *vis) : + visual(vis) + {} + + virtual ~Drawable() {} + + virtual void + resize(unsigned w, unsigned h) { + width = w; + height = h; + } + + virtual void swapBuffers(void) = 0; +}; + + +class Context +{ +public: + const Visual *visual; + + Context(const Visual *vis) : + visual(vis) + {} + + virtual ~Context() {} +}; + + +class WindowSystem +{ +public: + Drawable *currentDrawable; + Context *currentContext; + + + inline WindowSystem() : + currentDrawable(NULL), + currentContext(NULL) + {} + + virtual ~WindowSystem() {} + + virtual Visual * + createVisual(bool doubleBuffer=false) = 0; + + virtual Drawable * + createDrawable(const Visual *visual) = 0; + + virtual Context * + createContext(const Visual *visual) = 0; + + virtual bool + makeCurrent(Drawable *drawable, Context *context) = 0; + + virtual bool + processEvents(void) = 0; +}; + + +WindowSystem *createNativeWindowSystem(void); + + +} /* namespace glws */ + + +#endif /* _GLWS_HPP_ */ diff --git a/glws_glx.cpp b/glws_glx.cpp new file mode 100644 index 0000000..be0a82b --- /dev/null +++ b/glws_glx.cpp @@ -0,0 +1,232 @@ +/************************************************************************** + * + * Copyright 2011 Jose Fonseca + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + +#include "glimports.hpp" +#include "glws.hpp" + + +namespace glws { + + +class XlibVisual : public Visual +{ +public: + XVisualInfo *visinfo; + + XlibVisual(XVisualInfo *vi) : + visinfo(vi) + {} + + ~XlibVisual() { + XFree(visinfo); + } +}; + + +class XlibDrawable : public Drawable +{ +public: + Display *display; + Window window; + + XlibDrawable(const Visual *vis, Display *dpy, Window win) : + Drawable(vis), + display(dpy), + window(win) + {} + + ~XlibDrawable() { + XDestroyWindow(display, window); + } + + void + resize(unsigned w, unsigned h) { + Drawable::resize(w, h); + XResizeWindow(display, window, w, h); + } + + void swapBuffers(void) { + glXSwapBuffers(display, window); + } +}; + + +class XlibContext : public Context +{ +public: + Display *display; + GLXContext context; + + XlibContext(const Visual *vis, Display *dpy, GLXContext ctx) : + Context(vis), + display(dpy), + context(ctx) + {} + + ~XlibContext() { + glXDestroyContext(display, context); + } +}; + + +class XlibWindowSystem : public WindowSystem +{ +private: + Display *display; + int screen; + +public: + Drawable *currentDrawable; + Context *currentContext; + + XlibWindowSystem() { + display = XOpenDisplay(NULL); + screen = DefaultScreen(display); + } + + ~XlibWindowSystem() { + XCloseDisplay(display); + } + + Visual * + createVisual(bool doubleBuffer) { + int single_attribs[] = { + GLX_RGBA, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DEPTH_SIZE, 1, + None + }; + + int double_attribs[] = { + GLX_RGBA, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DOUBLEBUFFER, + GLX_DEPTH_SIZE, 1, + None + }; + + XVisualInfo *visinfo; + + visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs); + + return new XlibVisual(visinfo); + } + + Drawable * + createDrawable(const Visual *visual) + { + XVisualInfo *visinfo = dynamic_cast(visual)->visinfo; + + Window root = RootWindow(display, screen); + + /* window attributes */ + XSetWindowAttributes attr; + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + + unsigned long mask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + int x = 0, y = 0, width = 256, height = 256; + + Window window = XCreateWindow( + display, root, + x, y, width, height, + 0, + visinfo->depth, + InputOutput, + visinfo->visual, + mask, + &attr); + + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(display, window, &sizehints); + + const char *name = "glretrace"; + XSetStandardProperties( + display, window, name, name, + None, (char **)NULL, 0, &sizehints); + + XMapWindow(display, window); + + return new XlibDrawable(visual, display, window); + } + + Context * + createContext(const Visual *visual) + { + XVisualInfo *visinfo = dynamic_cast(visual)->visinfo; + GLXContext context = glXCreateContext(display, visinfo, NULL, True); + return new XlibContext(visual, display, context); + } + + bool + makeCurrent(Drawable *drawable, Context *context) + { + Window win = drawable ? dynamic_cast(drawable)->window : NULL; + GLXContext ctx = context ? dynamic_cast(context)->context : NULL; + + bool ret = glXMakeCurrent(display, win, ctx); + + if (drawable && context && ret) { + currentDrawable = drawable; + currentContext = context; + } else { + currentDrawable = NULL; + currentContext = NULL; + } + + return ret; + } + + bool + processEvents(void) { + while (XPending(display) > 0) { + XEvent event; + XNextEvent(display, &event); + // TODO + } + return true; + } +}; + + +WindowSystem *createNativeWindowSystem(void) { + return new XlibWindowSystem(); +} + + +} /* namespace glretrace */