]> git.cworth.org Git - apitrace/commitdiff
glretrace: Put currentContext on TLS.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Fri, 19 Oct 2012 17:42:41 +0000 (18:42 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Fri, 19 Oct 2012 17:42:41 +0000 (18:42 +0100)
Inspired on Imre's similar change.

retrace/glretrace.hpp
retrace/glretrace.py
retrace/glretrace_cgl.cpp
retrace/glretrace_egl.cpp
retrace/glretrace_main.cpp
retrace/glretrace_wgl.cpp
retrace/glretrace_ws.cpp
specs/gltypes.py

index 87ced60bf345f7bff893d51f92d4357735b718bc..90c4c81693419233af27686eddcd8d23db4475f7 100644 (file)
@@ -65,7 +65,8 @@ extern bool insideList;
 extern bool insideGlBeginEnd;
 
 
-extern Context *currentContext;
+Context *
+getCurrentContext(void);
 
 
 int
index f2cc37232032387af218e300cf6b318eb43e1e3c..32d6a63e95b2108fa21f11e36d5c588a06630dd9 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::currentContext) {'
+            print r'    if (retrace::debug && !glretrace::getCurrentContext()) {'
             print r'        retrace::warning(call) << "no current context\n";'
             print r'    }'
 
@@ -299,8 +299,9 @@ class GlRetracer(Retracer):
         )
 
         if function.name in ('glUseProgram', 'glUseProgramObjectARB'):
-            print r'    if (glretrace::currentContext) {'
-            print r'        glretrace::currentContext->activeProgram = call.arg(0).toUInt();'
+            print r'    glretrace::Context *currentContext = glretrace::getCurrentContext();'
+            print r'    if (currentContext) {'
+            print r'        currentContext->activeProgram = call.arg(0).toUInt();'
             print r'    }'
 
         # Only profile if not inside a list as the queries get inserted into list
@@ -479,7 +480,8 @@ class GlRetracer(Retracer):
             print '    GLint program = -1;'
             print '    if (glretrace::insideList) {'
             print '        // glUseProgram & glUseProgramObjectARB are display-list-able'
-            print '        program = _program_map[glretrace::currentContext->activeProgram];'
+            print r'    glretrace::Context *currentContext = glretrace::getCurrentContext();'
+            print '        program = _program_map[currentContext->activeProgram];'
             print '    } else {'
             print '        GLint pipeline = 0;'
             print '        if (_pipelineHasBeenBound) {'
index d66fbb5bf8d69767eab5278e8dd5bb9a02f40618..613ee81d237073b9d272afd6c87becce1f188e01 100644 (file)
@@ -290,7 +290,7 @@ static void retrace_CGLTexImageIOSurface2D(trace::Call &call) {
 
     GLvoid * pixels = NULL;
 
-    if (glretrace::currentContext != context) {
+    if (glretrace::getCurrentContext() != context) {
         if (retrace::debug) {
             retrace::warning(call) << "current context mismatch\n";
         }
index 75688152f80419a8e2ffeb61b3ab2c654e7de865..79b16217b5b51f81bca0ce0f8a799bae02d0c07d 100644 (file)
@@ -127,6 +127,7 @@ static void retrace_eglDestroySurface(trace::Call &call) {
     it = drawable_map.find(orig_surface);
 
     if (it != drawable_map.end()) {
+        glretrace::Context *currentContext = glretrace::getCurrentContext();
         if (!currentContext || it->second != currentContext->drawable) {
             // TODO: reference count
             delete it->second;
index f29ad01a07fd07c4d64b204a83897ae95b098bf6..bd5378a4bb0d981ab0c69b0a247bb1bbdf92f954 100755 (executable)
@@ -174,12 +174,14 @@ flushQueries() {
 
 void
 beginProfile(trace::Call &call, bool isDraw) {
+    glretrace::Context *currentContext = glretrace::getCurrentContext();
+
     /* Create call query */
     CallQuery query;
     query.isDraw = isDraw;
     query.call = call.no;
     query.sig = call.sig;
-    query.program = glretrace::currentContext ? glretrace::currentContext->activeProgram : 0;
+    query.program = currentContext ? currentContext->activeProgram : 0;
 
     /* GPU profiling only for draw calls */
     if (isDraw) {
@@ -230,6 +232,8 @@ endProfile(trace::Call &call, bool isDraw) {
 
 void
 initContext() {
+    glretrace::Context *currentContext = glretrace::getCurrentContext();
+
     /* Ensure we have adequate extension support */
     assert(currentContext);
     supportsTimestamp   = currentContext->hasExtension("GL_ARB_timer_query");
@@ -261,6 +265,7 @@ initContext() {
 
     /* Setup debug message call back */
     if (retrace::debug && supportsDebugOutput) {
+        glretrace::Context *currentContext = glretrace::getCurrentContext();
         glDebugMessageCallbackARB(&debugOutputCallback, currentContext);
 
         if (DEBUG_OUTPUT_SYNCHRONOUS) {
@@ -309,6 +314,7 @@ frame_complete(trace::Call &call) {
 
     retrace::frameComplete(call);
 
+    glretrace::Context *currentContext = glretrace::getCurrentContext();
     if (!currentContext) {
         return;
     }
@@ -403,7 +409,7 @@ retrace::addCallbacks(retrace::Retracer &retracer)
 
 image::Image *
 retrace::getSnapshot(void) {
-    if (!glretrace::currentContext) {
+    if (!glretrace::getCurrentContext()) {
         return NULL;
     }
 
@@ -414,8 +420,10 @@ retrace::getSnapshot(void) {
 bool
 retrace::dumpState(std::ostream &os)
 {
+    glretrace::Context *currentContext = glretrace::getCurrentContext();
+
     if (glretrace::insideGlBeginEnd ||
-        !glretrace::currentContext) {
+        !currentContext) {
         return false;
     }
 
index 639f419218dd5f5bd41a34ccf1d1065af76b0234..9db4bd01728aa19d24f7b84005713d23e28def9c 100644 (file)
@@ -100,8 +100,11 @@ static void retrace_wglSwapBuffers(trace::Call &call) {
     if (retrace::doubleBuffer) {
         if (drawable) {
             drawable->swapBuffers();
-        } else if (currentContext) {
-            currentContext->drawable->swapBuffers();
+        } else {
+            glretrace::Context *currentContext = glretrace::getCurrentContext();
+            if (currentContext) {
+                currentContext->drawable->swapBuffers();
+            }
         }
     } else {
         glFlush();
@@ -117,6 +120,7 @@ static void retrace_wglShareLists(trace::Call &call) {
 
     Context *new_context = glretrace::createContext(share_context);
     if (new_context) {
+        glretrace::Context *currentContext = glretrace::getCurrentContext();
         if (currentContext == old_context) {
             glretrace::makeCurrent(call, currentContext->drawable, new_context);
         }
index aeaadb77be0af415295df5ca39bfa1ef563d2c41..66ef10534cdc00d2dad79309ac9dd4688387d2d7 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <string.h>
 
+#include "os_thread.hpp"
 #include "retrace.hpp"
 #include "glproc.hpp"
 #include "glstate.hpp"
@@ -40,7 +41,6 @@
 namespace glretrace {
 
 
-Context *currentContext = NULL;
 
 
 static glws::Visual *
@@ -107,9 +107,14 @@ createContext(Context *shareContext) {
 }
 
 
+typedef Context * CurrentData;
+static os::thread_specific_ptr<CurrentData> currentData;
+
+
 bool
 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
 {
+    Context *currentContext = getCurrentContext();
     glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
 
     if (drawable == currentDrawable && context == currentContext) {
@@ -137,22 +142,38 @@ makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
         currentContext->drawable = NULL;
     }
 
+    CurrentData *currentDataPtr = currentData.get();
+    if (!currentDataPtr) {
+        currentDataPtr = new CurrentData;
+        currentData.reset(currentDataPtr);
+    }
+
     if (drawable && context) {
-        currentContext = context;
-        currentContext->drawable = drawable;
+        context->drawable = drawable;
+        *currentData = context;
         
         if (!context->used) {
             initContext();
             context->used = true;
         }
     } else {
-        currentContext = NULL;
+        *currentData = NULL;
     }
 
     return true;
 }
 
 
+Context *
+getCurrentContext(void) {
+    CurrentData *currentDataPtr = currentData.get();
+    if (!currentDataPtr) {
+        return NULL;
+    }
+    return *currentDataPtr;
+}
+
+
 
 
 /**
@@ -163,6 +184,7 @@ makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
  */
 void
 updateDrawable(int width, int height) {
+    Context *currentContext = getCurrentContext();
     if (!currentContext) {
         return;
     }
index 4324ac7e389ad75a6fb9abfadc5127a685a3a10f..23af3279e54f2e51a33e8ebba9ad6166131a7d36 100644 (file)
@@ -100,7 +100,7 @@ GLshader = Handle("shader", GLuint)
 GLlocation = Handle("location", GLint, key=('program', GLhandleARB))
 GLlocationARB = Handle("location", GLint, key=('programObj', GLhandleARB))
 
-contextKey = ('reinterpret_cast<uintptr_t>(glretrace::currentContext)', UIntPtr)
+contextKey = ('reinterpret_cast<uintptr_t>(glretrace::getCurrentContext())', UIntPtr)
 
 GLprogramARB = Handle("programARB", GLuint)
 GLframebuffer = Handle("framebuffer", GLuint)