]> git.cworth.org Git - apitrace/blobdiff - retrace/glretrace_ws.cpp
Handle visual creation errors.
[apitrace] / retrace / glretrace_ws.cpp
index aeaadb77be0af415295df5ca39bfa1ef563d2c41..319a2cbe80e65cfbb6b701c8c0d1b3f23c5dcd9c 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <string.h>
 
+#include "os_thread.hpp"
 #include "retrace.hpp"
 #include "glproc.hpp"
 #include "glstate.hpp"
@@ -40,9 +41,6 @@
 namespace glretrace {
 
 
-Context *currentContext = NULL;
-
-
 static glws::Visual *
 visuals[glws::PROFILE_MAX];
 
@@ -52,6 +50,10 @@ getVisual(glws::Profile profile) {
     glws::Visual * & visual = visuals[profile];
     if (!visual) {
         visual = glws::createVisual(retrace::doubleBuffer, profile);
+        if (!visual) {
+            std::cerr << "error: failed to create OpenGL visual\n";
+            exit(1);
+        }
     }
     return visual;
 }
@@ -68,29 +70,42 @@ getDefaultProfile(void)
 }
 
 
-glws::Drawable *
-createDrawable(glws::Profile profile) {
-    glws::Drawable *draw = glws::createDrawable(getVisual(profile));
+static glws::Drawable *
+createDrawableHelper(glws::Profile profile, int width = 32, int height = 32, bool pbuffer = false) {
+    glws::Visual *visual = getVisual(profile);
+    glws::Drawable *draw = glws::createDrawable(visual, width, height, pbuffer);
     if (!draw) {
         std::cerr << "error: failed to create OpenGL drawable\n";
         exit(1);
-        return NULL;
     }
 
     return draw;
 }
 
 
+glws::Drawable *
+createDrawable(glws::Profile profile) {
+    return createDrawableHelper(profile);
+}
+
+
 glws::Drawable *
 createDrawable(void) {
-    return glretrace::createDrawable(getDefaultProfile());
+    return createDrawable(getDefaultProfile());
+}
+
+
+glws::Drawable *
+createPbuffer(int width, int height) {
+    return createDrawableHelper(getDefaultProfile(), width, height, true);
 }
 
 
 Context *
 createContext(Context *shareContext, glws::Profile profile) {
+    glws::Visual *visual = getVisual(profile);
     glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
-    glws::Context *ctx = glws::createContext(getVisual(profile), shareWsContext, profile, retrace::debug);
+    glws::Context *ctx = glws::createContext(visual, shareWsContext, profile, retrace::debug);
     if (!ctx) {
         std::cerr << "error: failed to create OpenGL context\n";
         exit(1);
@@ -107,9 +122,14 @@ createContext(Context *shareContext) {
 }
 
 
+static os::thread_specific_ptr<Context>
+currentContextPtr;
+
+
 bool
 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
 {
+    Context *currentContext = currentContextPtr.release();
     glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
 
     if (drawable == currentDrawable && context == currentContext) {
@@ -138,21 +158,23 @@ makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
     }
 
     if (drawable && context) {
-        currentContext = context;
-        currentContext->drawable = drawable;
+        context->drawable = drawable;
+        currentContextPtr.reset(context);
         
         if (!context->used) {
             initContext();
             context->used = true;
         }
-    } else {
-        currentContext = NULL;
     }
 
     return true;
 }
 
 
+Context *
+getCurrentContext(void) {
+    return currentContextPtr.get();
+}
 
 
 /**
@@ -163,6 +185,7 @@ makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
  */
 void
 updateDrawable(int width, int height) {
+    Context *currentContext = getCurrentContext();
     if (!currentContext) {
         return;
     }
@@ -170,6 +193,10 @@ updateDrawable(int width, int height) {
     glws::Drawable *currentDrawable = currentContext->drawable;
     assert(currentDrawable);
 
+    if (currentDrawable->pbuffer) {
+        return;
+    }
+
     if (currentDrawable->visible &&
         width  <= currentDrawable->width &&
         height <= currentDrawable->height) {