Mesa (master): stw: Keep per-thread storage for current context and pixel format.

Michał Król michal at kemper.freedesktop.org
Fri Mar 20 12:07:04 UTC 2009


Module: Mesa
Branch: master
Commit: 4489f9efee58f2cba374298f2d43c96e5cd2ff41
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=4489f9efee58f2cba374298f2d43c96e5cd2ff41

Author: Michal Krol <michal at vmware.com>
Date:   Fri Mar 20 13:05:51 2009 +0100

stw: Keep per-thread storage for current context and pixel format.

---

 src/gallium/state_trackers/wgl/SConscript          |    1 +
 .../state_trackers/wgl/shared/stw_context.c        |   12 +--
 src/gallium/state_trackers/wgl/shared/stw_device.c |   23 +++++
 .../state_trackers/wgl/shared/stw_pixelformat.c    |    9 +-
 src/gallium/state_trackers/wgl/shared/stw_tls.c    |  101 ++++++++++++++++++++
 src/gallium/state_trackers/wgl/shared/stw_tls.h    |   53 ++++++++++
 src/gallium/state_trackers/wgl/shared/stw_winsys.h |    6 +
 7 files changed, 193 insertions(+), 12 deletions(-)

diff --git a/src/gallium/state_trackers/wgl/SConscript b/src/gallium/state_trackers/wgl/SConscript
index 2141b02..038a7a3 100644
--- a/src/gallium/state_trackers/wgl/SConscript
+++ b/src/gallium/state_trackers/wgl/SConscript
@@ -30,6 +30,7 @@ if env['platform'] in ['windows']:
         'shared/stw_arbextensionsstring.c',
         'shared/stw_getprocaddress.c',
         'shared/stw_arbpixelformat.c',
+        'shared/stw_tls.c',
     ]
 
     wgl = env.ConvenienceLibrary(
diff --git a/src/gallium/state_trackers/wgl/shared/stw_context.c b/src/gallium/state_trackers/wgl/shared/stw_context.c
index d77daac..69f25d6 100644
--- a/src/gallium/state_trackers/wgl/shared/stw_context.c
+++ b/src/gallium/state_trackers/wgl/shared/stw_context.c
@@ -39,9 +39,7 @@
 #include "shared/stw_pixelformat.h"
 #include "stw_public.h"
 #include "stw_context.h"
-
-static HDC current_hdc = NULL;
-static UINT_PTR current_hglrc = 0;
+#include "stw_tls.h"
 
 BOOL
 stw_copy_context(
@@ -264,13 +262,13 @@ get_window_size( HDC hdc, GLuint *width, GLuint *height )
 UINT_PTR
 stw_get_current_context( void )
 {
-   return current_hglrc;
+   return stw_tls_get_data()->currentGLRC;
 }
 
 HDC
 stw_get_current_dc( void )
 {
-    return current_hdc;
+    return stw_tls_get_data()->currentDC;
 }
 
 BOOL
@@ -295,8 +293,8 @@ stw_make_current(
    if (ctx == NULL)
       return FALSE;
 
-   current_hdc = hdc;
-   current_hglrc = hglrc;
+   stw_tls_get_data()->currentDC = hdc;
+   stw_tls_get_data()->currentGLRC = hglrc;
 
    if (glcurctx != NULL) {
       curctx = (struct stw_context *) glcurctx->DriverCtx;
diff --git a/src/gallium/state_trackers/wgl/shared/stw_device.c b/src/gallium/state_trackers/wgl/shared/stw_device.c
index 0dca856..4bec036 100644
--- a/src/gallium/state_trackers/wgl/shared/stw_device.c
+++ b/src/gallium/state_trackers/wgl/shared/stw_device.c
@@ -35,6 +35,7 @@
 #include "shared/stw_winsys.h"
 #include "shared/stw_pixelformat.h"
 #include "shared/stw_public.h"
+#include "shared/stw_tls.h"
 
 #ifdef WIN32_THREADS
 extern _glthread_Mutex OneTimeLock;
@@ -70,6 +71,8 @@ st_init(const struct stw_winsys *stw_winsys)
    
    assert(!stw_dev);
 
+   stw_tls_init();
+
    stw_dev = &stw_dev_storage;
    memset(stw_dev, 0, sizeof(*stw_dev));
 
@@ -101,6 +104,24 @@ error1:
 }
 
 
+boolean
+st_init_thread(void)
+{
+   if (!stw_tls_init_thread()) {
+      return FALSE;
+   }
+
+   return TRUE;
+}
+
+
+void
+st_cleanup_thread(void)
+{
+   stw_tls_cleanup_thread();
+}
+
+
 void
 st_cleanup(void)
 {
@@ -133,6 +154,8 @@ st_cleanup(void)
    debug_memory_end(stw_dev->memdbg_no);
 #endif
 
+   stw_tls_cleanup();
+
    stw_dev = NULL;
 }
 
diff --git a/src/gallium/state_trackers/wgl/shared/stw_pixelformat.c b/src/gallium/state_trackers/wgl/shared/stw_pixelformat.c
index 2992a1a..b216ca5 100644
--- a/src/gallium/state_trackers/wgl/shared/stw_pixelformat.c
+++ b/src/gallium/state_trackers/wgl/shared/stw_pixelformat.c
@@ -28,6 +28,7 @@
 #include "util/u_debug.h"
 #include "stw_pixelformat.h"
 #include "stw_public.h"
+#include "stw_tls.h"
 
 #define MAX_PIXELFORMATS   16
 
@@ -35,8 +36,6 @@ static struct pixelformat_info pixelformats[MAX_PIXELFORMATS];
 static uint pixelformat_count = 0;
 static uint pixelformat_extended_count = 0;
 
-static uint currentpixelformat = 0;
-
 
 static void
 add_standard_pixelformats(
@@ -248,7 +247,7 @@ int
 stw_pixelformat_get(
    HDC hdc )
 {
-   return currentpixelformat;
+   return stw_tls_get_data()->currentPixelFormat;
 }
 
 
@@ -267,8 +266,8 @@ stw_pixelformat_set(
    if (index >= count)
       return FALSE;
 
-   currentpixelformat = iPixelFormat;
-   
+   stw_tls_get_data()->currentPixelFormat = iPixelFormat;
+
    /* Some applications mistakenly use the undocumented wglSetPixelFormat 
     * function instead of SetPixelFormat, so we call SetPixelFormat here to 
     * avoid opengl32.dll's wglCreateContext to fail */
diff --git a/src/gallium/state_trackers/wgl/shared/stw_tls.c b/src/gallium/state_trackers/wgl/shared/stw_tls.c
new file mode 100644
index 0000000..e72bafb
--- /dev/null
+++ b/src/gallium/state_trackers/wgl/shared/stw_tls.c
@@ -0,0 +1,101 @@
+/**************************************************************************
+ *
+ * Copyright 2009 VMware, Inc.
+ * 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, sub license, 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 (including the
+ * next paragraph) 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 <windows.h>
+
+#include "pipe/p_compiler.h"
+#include "util/u_memory.h"
+#include "stw_tls.h"
+
+static DWORD tlsIndex = TLS_OUT_OF_INDEXES;
+
+boolean
+stw_tls_init(void)
+{
+   tlsIndex = TlsAlloc();
+   if (tlsIndex == TLS_OUT_OF_INDEXES) {
+      return FALSE;
+   }
+
+   return TRUE;
+}
+
+boolean
+stw_tls_init_thread(void)
+{
+   struct stw_tls_data *data;
+
+   if (tlsIndex == TLS_OUT_OF_INDEXES) {
+      return FALSE;
+   }
+
+   data = MALLOC(sizeof(*data));
+   if (!data) {
+      return FALSE;
+   }
+
+   data->currentPixelFormat = 0;
+   data->currentDC = NULL;
+   data->currentGLRC = 0;
+
+   TlsSetValue(tlsIndex, data);
+
+   return TRUE;
+}
+
+void
+stw_tls_cleanup_thread(void)
+{
+   struct stw_tls_data *data;
+
+   if (tlsIndex == TLS_OUT_OF_INDEXES) {
+      return;
+   }
+
+   data = (struct stw_tls_data *) TlsGetValue(tlsIndex);
+   TlsSetValue(tlsIndex, NULL);
+   FREE(data);
+}
+
+void
+stw_tls_cleanup(void)
+{
+   if (tlsIndex != TLS_OUT_OF_INDEXES) {
+      TlsFree(tlsIndex);
+      tlsIndex = TLS_OUT_OF_INDEXES;
+   }
+}
+
+struct stw_tls_data *
+stw_tls_get_data(void)
+{
+   if (tlsIndex == TLS_OUT_OF_INDEXES) {
+      return NULL;
+   }
+
+   return (struct stw_tls_data *) TlsGetValue(tlsIndex);
+}
diff --git a/src/gallium/state_trackers/wgl/shared/stw_tls.h b/src/gallium/state_trackers/wgl/shared/stw_tls.h
new file mode 100644
index 0000000..23b61e6
--- /dev/null
+++ b/src/gallium/state_trackers/wgl/shared/stw_tls.h
@@ -0,0 +1,53 @@
+/**************************************************************************
+ *
+ * Copyright 2009 VMware, Inc.
+ * 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, sub license, 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 (including the
+ * next paragraph) 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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.
+ *
+ **************************************************************************/
+
+#ifndef STW_TLS_H
+#define STW_TLS_H
+
+struct stw_tls_data
+{
+   uint currentPixelFormat;
+   HDC currentDC;
+   UINT_PTR currentGLRC;
+};
+
+boolean
+stw_tls_init(void);
+
+boolean
+stw_tls_init_thread(void);
+
+void
+stw_tls_cleanup_thread(void);
+
+void
+stw_tls_cleanup(void);
+
+struct stw_tls_data *
+stw_tls_get_data(void);
+
+#endif /* STW_TLS_H */
diff --git a/src/gallium/state_trackers/wgl/shared/stw_winsys.h b/src/gallium/state_trackers/wgl/shared/stw_winsys.h
index a85a9a2..e4a1d4f 100644
--- a/src/gallium/state_trackers/wgl/shared/stw_winsys.h
+++ b/src/gallium/state_trackers/wgl/shared/stw_winsys.h
@@ -53,6 +53,12 @@ struct stw_winsys
 boolean
 st_init(const struct stw_winsys *stw_winsys);
 
+boolean
+st_init_thread(void);
+
+void
+st_cleanup_thread(void);
+
 void
 st_cleanup(void);
 




More information about the mesa-commit mailing list