]> git.cworth.org Git - apitrace/commitdiff
Cleanup glretrace<->glws integration.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Thu, 26 Apr 2012 22:18:28 +0000 (23:18 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Thu, 26 Apr 2012 22:18:28 +0000 (23:18 +0100)
Less code duplication.

retrace/CMakeLists.txt
retrace/glretrace.hpp
retrace/glretrace.py
retrace/glretrace_cgl.cpp
retrace/glretrace_egl.cpp
retrace/glretrace_glx.cpp
retrace/glretrace_main.cpp
retrace/glretrace_wgl.cpp
retrace/glretrace_ws.cpp [new file with mode: 0644]

index 6b5e61a494800e95577454415f37d66d2ff48f28..dcf9da4423de374189a330eb09d159d7fcb12a53 100644 (file)
@@ -51,6 +51,7 @@ add_library (glretrace_common
     glretrace_wgl.cpp
     glretrace_egl.cpp
     glretrace_main.cpp
+    glretrace_ws.cpp
     glstate.cpp
     glstate_images.cpp
     glstate_params.cpp
index 100d5a8991870cd1ce9ead988a103467c3f7ebab..fea187cf05ed19b474d255bb828000d255a71464 100644 (file)
@@ -34,10 +34,26 @@ namespace glretrace {
 
 
 extern bool insideGlBeginEnd;
-extern glws::Profile defaultProfile;
-extern glws::Visual *visual[glws::PROFILE_MAX];
-extern glws::Drawable *drawable;
-extern glws::Context *context;
+
+
+extern glws::Drawable *currentDrawable;
+extern glws::Context *currentContext;
+
+glws::Drawable *
+createDrawable(glws::Profile profile);
+
+glws::Drawable *
+createDrawable(void);
+
+glws::Context *
+createContext(glws::Context *shareContext, glws::Profile profile);
+
+glws::Context *
+createContext(glws::Context *shareContext = 0);
+
+bool
+makeCurrent(trace::Call &call, glws::Drawable *drawable, glws::Context *context);
+
 
 void
 checkGlError(trace::Call &call);
index c0edb202a885fc6c6f3cf83e973a8813c5f99bfc..b7659ff1dd6bcd37e6268a3fa7b22c2c43a55527 100644 (file)
@@ -250,7 +250,7 @@ class GlRetracer(Retracer):
             print '    glretrace::insideGlBeginEnd = false;'
 
         if function.name.startswith('gl') and not function.name.startswith('glX'):
-            print r'    if (retrace::debug && !glretrace::context) {'
+            print r'    if (retrace::debug && !glretrace::currentContext) {'
             print r'        retrace::warning(call) << "no current context\n";'
             print r'    }'
 
index fccd532d12ced030586bb9d7f462362f0a15e96b..0c91ba27fccb7a46d5e1a19e78e4c6295cb64e2e 100644 (file)
@@ -53,7 +53,7 @@ getDrawable(unsigned long drawable_id) {
     DrawableMap::const_iterator it;
     it = drawable_map.find(drawable_id);
     if (it == drawable_map.end()) {
-        return (drawable_map[drawable_id] = glws::createDrawable(visual[glretrace::defaultProfile]));
+        return (drawable_map[drawable_id] = glretrace::createDrawable());
     }
 
     return it->second;
@@ -70,7 +70,7 @@ getContext(unsigned long long ctx) {
     it = context_map.find(ctx);
     if (it == context_map.end()) {
         glws::Context *context;
-        context_map[ctx] = context = glws::createContext(visual[glretrace::defaultProfile], sharedContext, glretrace::defaultProfile, retrace::debug);
+        context_map[ctx] = context = glretrace::createContext(sharedContext);
         if (!sharedContext) {
             sharedContext = context;
         }
@@ -87,22 +87,14 @@ static void retrace_CGLSetCurrentContext(trace::Call &call) {
     glws::Drawable *new_drawable = getDrawable(ctx);
     glws::Context *new_context = getContext(ctx);
 
-    bool result = glws::makeCurrent(new_drawable, new_context);
-
-    if (new_drawable && new_context && result) {
-        drawable = new_drawable;
-        context = new_context;
-    } else {
-        drawable = NULL;
-        context = NULL;
-    }
+    glretrace::makeCurrent(call, new_drawable, new_context);
 }
 
 
 static void retrace_CGLFlushDrawable(trace::Call &call) {
-    if (drawable && context) {
+    if (currentDrawable && currentContext) {
         if (retrace::doubleBuffer) {
-            drawable->swapBuffers();
+            currentDrawable->swapBuffers();
         } else {
             glFlush();
         }
index 1f8020a45ffe5c6694d012d31aba90aa6a69297e..70fe3546835e704aa00ec1687025758a864fdf5d 100644 (file)
@@ -102,9 +102,7 @@ static void createDrawable(unsigned long long orig_config, unsigned long long or
         profile = last_profile;
     }
 
-    glws::Visual *visual = glretrace::visual[profile];
-
-    glws::Drawable *drawable = glws::createDrawable(visual);
+    glws::Drawable *drawable = glretrace::createDrawable(profile);
     drawable_map[orig_surface] = drawable;
 }
 
@@ -128,7 +126,7 @@ static void retrace_eglDestroySurface(trace::Call &call) {
     it = drawable_map.find(orig_surface);
 
     if (it != drawable_map.end()) {
-        if (it->second != drawable) {
+        if (it->second != currentDrawable) {
             // TODO: reference count
             delete it->second;
         }
@@ -169,7 +167,7 @@ static void retrace_eglCreateContext(trace::Call &call) {
     }
 
 
-    glws::Context *context = glws::createContext(glretrace::visual[profile], share_context, profile, retrace::debug);
+    glws::Context *context = glretrace::createContext(share_context, profile);
     if (!context) {
         const char *name;
         switch (profile) {
@@ -212,34 +210,15 @@ static void retrace_eglMakeCurrent(trace::Call &call) {
     glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
     glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
 
-    if (new_drawable == drawable && new_context == context) {
-        return;
-    }
-
-    if (drawable && context) {
-        glFlush();
-        if (!retrace::doubleBuffer) {
-            frame_complete(call);
-        }
-    }
-
-    bool result = glws::makeCurrent(new_drawable, new_context);
-
-    if (new_drawable && new_context && result) {
-        drawable = new_drawable;
-        context = new_context;
-    } else {
-        drawable = NULL;
-        context = NULL;
-    }
+    glretrace::makeCurrent(call, new_drawable, new_context);
 }
 
 
 static void retrace_eglSwapBuffers(trace::Call &call) {
     frame_complete(call);
 
-    if (retrace::doubleBuffer && drawable) {
-        drawable->swapBuffers();
+    if (retrace::doubleBuffer && currentDrawable) {
+        currentDrawable->swapBuffers();
     } else {
         glFlush();
     }
index 20afe5107e4720d94ce5f55b8d051df4e447549a..9d1b5e5099d015941ff3adfd52716a78e4af7fb7 100644 (file)
@@ -47,7 +47,7 @@ getDrawable(unsigned long drawable_id) {
     DrawableMap::const_iterator it;
     it = drawable_map.find(drawable_id);
     if (it == drawable_map.end()) {
-        return (drawable_map[drawable_id] = glws::createDrawable(visual[glretrace::defaultProfile]));
+        return (drawable_map[drawable_id] = glretrace::createDrawable());
     }
 
     return it->second;
@@ -62,7 +62,7 @@ getContext(unsigned long long context_ptr) {
     ContextMap::const_iterator it;
     it = context_map.find(context_ptr);
     if (it == context_map.end()) {
-        return (context_map[context_ptr] = glws::createContext(visual[glretrace::defaultProfile], NULL, glretrace::defaultProfile, retrace::debug));
+        return (context_map[context_ptr] = glretrace::createContext());
     }
 
     return it->second;
@@ -72,7 +72,7 @@ static void retrace_glXCreateContext(trace::Call &call) {
     unsigned long long orig_context = call.ret->toUIntPtr();
     glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
 
-    glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+    glws::Context *context = glretrace::createContext(share_context);
     context_map[orig_context] = context;
 }
 
@@ -80,7 +80,7 @@ static void retrace_glXCreateContextAttribsARB(trace::Call &call) {
     unsigned long long orig_context = call.ret->toUIntPtr();
     glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
 
-    glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+    glws::Context *context = glretrace::createContext(share_context);
     context_map[orig_context] = context;
 }
 
@@ -88,26 +88,7 @@ static void retrace_glXMakeCurrent(trace::Call &call) {
     glws::Drawable *new_drawable = getDrawable(call.arg(1).toUInt());
     glws::Context *new_context = getContext(call.arg(2).toUIntPtr());
 
-    if (new_drawable == drawable && new_context == context) {
-        return;
-    }
-
-    if (drawable && context) {
-        glFlush();
-        if (!retrace::doubleBuffer) {
-            frame_complete(call);
-        }
-    }
-
-    bool result = glws::makeCurrent(new_drawable, new_context);
-
-    if (new_drawable && new_context && result) {
-        drawable = new_drawable;
-        context = new_context;
-    } else {
-        drawable = NULL;
-        context = NULL;
-    }
+    glretrace::makeCurrent(call, new_drawable, new_context);
 }
 
 
@@ -124,7 +105,7 @@ static void retrace_glXDestroyContext(trace::Call &call) {
 static void retrace_glXSwapBuffers(trace::Call &call) {
     frame_complete(call);
     if (retrace::doubleBuffer) {
-        drawable->swapBuffers();
+        currentDrawable->swapBuffers();
     } else {
         glFlush();
     }
@@ -134,7 +115,7 @@ static void retrace_glXCreateNewContext(trace::Call &call) {
     unsigned long long orig_context = call.ret->toUIntPtr();
     glws::Context *share_context = getContext(call.arg(3).toUIntPtr());
 
-    glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+    glws::Context *context = glretrace::createContext(share_context);
     context_map[orig_context] = context;
 }
 
@@ -142,26 +123,7 @@ static void retrace_glXMakeContextCurrent(trace::Call &call) {
     glws::Drawable *new_drawable = getDrawable(call.arg(1).toUInt());
     glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
 
-    if (new_drawable == drawable && new_context == context) {
-        return;
-    }
-
-    if (drawable && context) {
-        glFlush();
-        if (!retrace::doubleBuffer) {
-            frame_complete(call);
-        }
-    }
-
-    bool result = glws::makeCurrent(new_drawable, new_context);
-
-    if (new_drawable && new_context && result) {
-        drawable = new_drawable;
-        context = new_context;
-    } else {
-        drawable = NULL;
-        context = NULL;
-    }
+    glretrace::makeCurrent(call, new_drawable, new_context);
 }
 
 const retrace::Entry glretrace::glx_callbacks[] = {
index 429208b009ec3204da174577720605aa6a8f21dc..c75d3683ec1fec3b9421a24025ebcbff3b498e88 100644 (file)
 namespace glretrace {
 
 bool insideGlBeginEnd = false;
-glws::Profile defaultProfile = glws::PROFILE_COMPAT;
-glws::Visual *visual[glws::PROFILE_MAX];
-glws::Drawable *drawable = NULL;
-glws::Context *context = NULL;
+
 
 void
 checkGlError(trace::Call &call) {
@@ -85,51 +82,15 @@ checkGlError(trace::Call &call) {
     os << "\n";
 }
 
-/**
- * Grow the current drawble.
- *
- * We need to infer the drawable size from GL calls because the drawable sizes
- * are specified by OS specific calls which we do not trace.
- */
-void
-updateDrawable(int width, int height) {
-    if (!drawable) {
-        return;
-    }
-
-    if (drawable->visible &&
-        width  <= drawable->width &&
-        height <= drawable->height) {
-        return;
-    }
-
-    // Ignore zero area viewports
-    if (width == 0 || height == 0) {
-        return;
-    }
-
-    // Check for bound framebuffer last, as this may have a performance impact.
-    GLint draw_framebuffer = 0;
-    glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
-    if (draw_framebuffer != 0) {
-        return;
-    }
-
-    drawable->resize(width, height);
-    drawable->show();
-
-    glScissor(0, 0, width, height);
-}
-
 
 void frame_complete(trace::Call &call) {
     retrace::frameComplete(call);
 
-    if (!drawable) {
+    if (!currentDrawable) {
         return;
     }
 
-    if (!drawable->visible) {
+    if (!currentDrawable->visible) {
         retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
     }
 }
@@ -140,16 +101,7 @@ void frame_complete(trace::Call &call) {
 
 void
 retrace::setUp(void) {
-    if (retrace::coreProfile) {
-        glretrace::defaultProfile = glws::PROFILE_CORE;
-    }
-
     glws::init();
-
-    glretrace::visual[glws::PROFILE_COMPAT] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_COMPAT);
-    glretrace::visual[glws::PROFILE_CORE] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_CORE);
-    glretrace::visual[glws::PROFILE_ES1] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_ES1);
-    glretrace::visual[glws::PROFILE_ES2] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_ES2);
 }
 
 
@@ -166,7 +118,7 @@ retrace::addCallbacks(retrace::Retracer &retracer)
 
 image::Image *
 retrace::getSnapshot(void) {
-    if (!glretrace::drawable) {
+    if (!glretrace::currentDrawable) {
         return NULL;
     }
 
@@ -178,8 +130,8 @@ bool
 retrace::dumpState(std::ostream &os)
 {
     if (glretrace::insideGlBeginEnd ||
-        !glretrace::drawable ||
-        !glretrace::context) {
+        !glretrace::currentDrawable ||
+        !glretrace::currentContext) {
         return false;
     }
 
@@ -201,9 +153,5 @@ retrace::waitForInput(void) {
 
 void
 retrace::cleanUp(void) {
-    for (int n = 0; n < glws::PROFILE_MAX; n++) {
-        delete glretrace::visual[n];
-    }
-
     glws::cleanup();
 }
index 5ede7e130022c1541d013c34fa3c594dd7b2fcf4..c3e109657846c03fda5e127b5fccfd5ee76fa3c0 100644 (file)
@@ -48,7 +48,7 @@ getDrawable(unsigned long long hdc) {
     DrawableMap::const_iterator it;
     it = drawable_map.find(hdc);
     if (it == drawable_map.end()) {
-        return (drawable_map[hdc] = glws::createDrawable(visual[glretrace::defaultProfile]));
+        return (drawable_map[hdc] = glretrace::createDrawable());
     }
 
     return it->second;
@@ -56,7 +56,7 @@ getDrawable(unsigned long long hdc) {
 
 static void retrace_wglCreateContext(trace::Call &call) {
     unsigned long long orig_context = call.ret->toUIntPtr();
-    glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], NULL, glretrace::defaultProfile, retrace::debug);
+    glws::Context *context = glretrace::createContext();
     context_map[orig_context] = context;
 }
 
@@ -64,25 +64,10 @@ static void retrace_wglDeleteContext(trace::Call &call) {
 }
 
 static void retrace_wglMakeCurrent(trace::Call &call) {
-    if (drawable && context) {
-        glFlush();
-        if (!retrace::doubleBuffer) {
-            frame_complete(call);
-        }
-    }
-    
     glws::Drawable *new_drawable = getDrawable(call.arg(0).toUIntPtr());
     glws::Context *new_context = context_map[call.arg(1).toUIntPtr()];
 
-    bool result = glws::makeCurrent(new_drawable, new_context);
-
-    if (new_drawable && new_context && result) {
-        drawable = new_drawable;
-        context = new_context;
-    } else {
-        drawable = NULL;
-        context = NULL;
-    }
+    glretrace::makeCurrent(call, new_drawable, new_context);
 }
 
 static void retrace_wglCopyContext(trace::Call &call) {
@@ -100,7 +85,7 @@ static void retrace_wglSetPixelFormat(trace::Call &call) {
 static void retrace_wglSwapBuffers(trace::Call &call) {
     frame_complete(call);
     if (retrace::doubleBuffer) {
-        drawable->swapBuffers();
+        currentDrawable->swapBuffers();
     } else {
         glFlush();
     }
@@ -113,11 +98,10 @@ static void retrace_wglShareLists(trace::Call &call) {
     glws::Context *share_context = context_map[hglrc1];
     glws::Context *old_context = context_map[hglrc2];
 
-    glws::Context *new_context =
-        glws::createContext(old_context->visual, share_context, glretrace::defaultProfile, retrace::debug);
+    glws::Context *new_context = glretrace::createContext(share_context);
     if (new_context) {
-        if (context == old_context) {
-            glws::makeCurrent(drawable, new_context);
+        if (currentContext == old_context) {
+            glretrace::makeCurrent(call, currentDrawable, new_context);
         }
 
         context_map[hglrc2] = new_context;
@@ -181,7 +165,7 @@ static void retrace_wglCreatePbufferARB(trace::Call &call) {
     int iHeight = call.arg(3).toUInt();
 
     unsigned long long orig_pbuffer = call.ret->toUIntPtr();
-    glws::Drawable *drawable = glws::createDrawable(glretrace::visual[glretrace::defaultProfile]);
+    glws::Drawable *drawable = glretrace::createDrawable();
 
     drawable->resize(iWidth, iHeight);
     drawable->show();
@@ -223,7 +207,7 @@ static void retrace_wglCreateContextAttribsARB(trace::Call &call) {
         share_context = context_map[call.arg(1).toUIntPtr()];
     }
 
-    glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+    glws::Context *context = glretrace::createContext(share_context);
     context_map[orig_context] = context;
 }
 
diff --git a/retrace/glretrace_ws.cpp b/retrace/glretrace_ws.cpp
new file mode 100644 (file)
index 0000000..47b1369
--- /dev/null
@@ -0,0 +1,183 @@
+/**************************************************************************
+ *
+ * Copyright 2011-2012 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.
+ *
+ **************************************************************************/
+
+
+/**
+ * Window system helpers for glretrace.
+ */
+
+
+#include <string.h>
+
+#include "retrace.hpp"
+#include "glproc.hpp"
+#include "glstate.hpp"
+#include "glretrace.hpp"
+
+
+namespace glretrace {
+
+
+glws::Drawable *currentDrawable = NULL;
+glws::Context *currentContext = NULL;
+
+
+static glws::Visual *
+visuals[glws::PROFILE_MAX];
+
+
+inline glws::Visual *
+getVisual(glws::Profile profile) {
+    glws::Visual * & visual = visuals[profile];
+    if (!visual) {
+        visual = glws::createVisual(retrace::doubleBuffer, profile);
+    }
+    return visual;
+}
+
+
+inline glws::Profile
+getDefaultProfile(void)
+{
+    if (retrace::coreProfile) {
+        return glws::PROFILE_CORE;
+    } else {
+        return glws::PROFILE_COMPAT;
+    }
+}
+
+
+glws::Drawable *
+createDrawable(glws::Profile profile) {
+    glws::Drawable *draw = glws::createDrawable(getVisual(profile));
+    if (!draw) {
+        std::cerr << "error: failed to create OpenGL drawable\n";
+        exit(1);
+        return NULL;
+    }
+
+    return draw;
+}
+
+
+glws::Drawable *
+createDrawable(void) {
+    return glretrace::createDrawable(getDefaultProfile());
+}
+
+
+glws::Context *
+createContext(glws::Context *shareContext, glws::Profile profile) {
+    glws::Context *ctx = glws::createContext(getVisual(profile), shareContext, profile, retrace::debug);
+    if (!ctx) {
+        std::cerr << "error: failed to create OpenGL context\n";
+        exit(1);
+        return NULL;
+    }
+
+    return ctx;
+}
+
+
+glws::Context *
+createContext(glws::Context *shareContext) {
+    return createContext(shareContext, getDefaultProfile());
+}
+
+
+bool
+makeCurrent(trace::Call &call, glws::Drawable *drawable, glws::Context *context)
+{
+    if (drawable == currentDrawable && context == currentContext) {
+        return true;
+    }
+
+    if (currentDrawable && currentContext) {
+        glFlush();
+        if (!retrace::doubleBuffer) {
+            frame_complete(call);
+        }
+    }
+
+    bool success = glws::makeCurrent(drawable, context);
+
+    if (!success) {
+        std::cerr << "error: failed to make current OpenGL context and drawable\n";
+        exit(1);
+        return false;
+    }
+
+    if (drawable && context) {
+        currentDrawable = drawable;
+        currentContext = context;
+    } else {
+        currentDrawable = NULL;
+        currentContext = NULL;
+    }
+
+    return true;
+}
+
+
+
+
+/**
+ * Grow the current drawble.
+ *
+ * We need to infer the drawable size from GL calls because the drawable sizes
+ * are specified by OS specific calls which we do not trace.
+ */
+void
+updateDrawable(int width, int height) {
+    if (!currentDrawable) {
+        return;
+    }
+
+    if (!currentDrawable->visible &&
+        width  <= currentDrawable->width &&
+        height <= currentDrawable->height) {
+        return;
+    }
+
+    // Ignore zero area viewports
+    if (width == 0 || height == 0) {
+        return;
+    }
+
+    // Check for bound framebuffer last, as this may have a performance impact.
+    GLint draw_framebuffer = 0;
+    glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
+    if (draw_framebuffer != 0) {
+        return;
+    }
+
+    currentDrawable->resize(width, height);
+    currentDrawable->show();
+
+    glScissor(0, 0, width, height);
+}
+
+
+} /* namespace glretrace */