[Mesa-dev] [PATCH 2/2] st/wgl: Couple of fixes to opengl32.dll's wglCreateContext/wglDeleteContext dispatch.

Jose Fonseca jfonseca at vmware.com
Tue Apr 14 06:29:30 PDT 2015


- Use GetModuleHandle instead of LoadLibrary to avoid incrementing the
  opengl32.dll reference count (otherwise the opengl32.dll will linger
  in memory forever.)

- Ensure we use our fake wglCreateContext/wglDeleteContext when using
  Mesa as a drop-in replacement for opengl32.dll

Untested.  Just noticed by accident.
---
 src/gallium/state_trackers/wgl/stw_ext_context.c | 18 ++++++------
 src/gallium/state_trackers/wgl/stw_ext_context.h | 36 ++++++++++++++++++++++++
 src/gallium/state_trackers/wgl/stw_wgl.c         | 21 ++++++++++++++
 3 files changed, 66 insertions(+), 9 deletions(-)
 create mode 100644 src/gallium/state_trackers/wgl/stw_ext_context.h

diff --git a/src/gallium/state_trackers/wgl/stw_ext_context.c b/src/gallium/state_trackers/wgl/stw_ext_context.c
index 8a96cac..6af2062 100644
--- a/src/gallium/state_trackers/wgl/stw_ext_context.c
+++ b/src/gallium/state_trackers/wgl/stw_ext_context.c
@@ -33,6 +33,11 @@
 #include "stw_icd.h"
 #include "stw_context.h"
 #include "stw_device.h"
+#include "stw_ext_context.h"
+
+
+wglCreateContext_t wglCreateContext_func = 0;
+wglDeleteContext_t wglDeleteContext_func = 0;
 
 
 /**
@@ -50,12 +55,7 @@
 HGLRC WINAPI
 wglCreateContextAttribsARB(HDC hDC, HGLRC hShareContext, const int *attribList)
 {
-   typedef HGLRC (WINAPI *wglCreateContext_t)(HDC hdc);
-   typedef BOOL (WINAPI *wglDeleteContext_t)(HGLRC hglrc);
    HGLRC context;
-   static HMODULE opengl_lib = 0;
-   static wglCreateContext_t wglCreateContext_func = 0;
-   static wglDeleteContext_t wglDeleteContext_func = 0;
 
    int majorVersion = 1, minorVersion = 0, layerPlane = 0;
    int contextFlags = 0x0;
@@ -135,11 +135,11 @@ wglCreateContextAttribsARB(HDC hDC, HGLRC hShareContext, const int *attribList)
    }
 
    /* Get pointer to OPENGL32.DLL's wglCreate/DeleteContext() functions */
-   if (opengl_lib == 0) {
-      /* Open the OPENGL32.DLL library */
-      opengl_lib = LoadLibraryA("OPENGL32.DLL");
+   if (!wglCreateContext_func || !wglDeleteContext_func) {
+      /* Get the OPENGL32.DLL library */
+      HMODULE opengl_lib = GetModuleHandleA("opengl32.dll");
       if (!opengl_lib) {
-         _debug_printf("wgl: LoadLibrary(OPENGL32.DLL) failed\n");
+         _debug_printf("wgl: GetModuleHandleA(\"opengl32.dll\") failed\n");
          return 0;
       }
 
diff --git a/src/gallium/state_trackers/wgl/stw_ext_context.h b/src/gallium/state_trackers/wgl/stw_ext_context.h
new file mode 100644
index 0000000..9cb12b4
--- /dev/null
+++ b/src/gallium/state_trackers/wgl/stw_ext_context.h
@@ -0,0 +1,36 @@
+/*
+ * Mesa 3-D graphics library
+ *
+ * Copyright (C) 2011 Morgan Armand <morgan.devel at gmail.com>
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <windows.h>
+#include <GL/gl.h>
+
+
+typedef HGLRC (WINAPI *wglCreateContext_t)(HDC hdc);
+typedef BOOL (WINAPI *wglDeleteContext_t)(HGLRC hglrc);
+
+extern wglCreateContext_t wglCreateContext_func;
+extern wglDeleteContext_t wglDeleteContext_func;
+
diff --git a/src/gallium/state_trackers/wgl/stw_wgl.c b/src/gallium/state_trackers/wgl/stw_wgl.c
index 0650fbb..5146e6a 100644
--- a/src/gallium/state_trackers/wgl/stw_wgl.c
+++ b/src/gallium/state_trackers/wgl/stw_wgl.c
@@ -45,8 +45,12 @@
 #include "stw_context.h"
 #include "stw_pixelformat.h"
 #include "stw_wgl.h"
+#include "stw_ext_context.h"
 
 
+static void
+overrideOpenGL32EntryPoints(void);
+
 WINGDIAPI BOOL APIENTRY
 wglCopyContext(
    HGLRC hglrcSrc,
@@ -62,6 +66,7 @@ WINGDIAPI HGLRC APIENTRY
 wglCreateContext(
    HDC hdc )
 {
+   overrideOpenGL32EntryPoints();
    return (HGLRC) DrvCreateContext(hdc);
 }
 
@@ -70,6 +75,7 @@ wglCreateLayerContext(
    HDC hdc,
    int iLayerPlane )
 {
+   overrideOpenGL32EntryPoints();
    return (HGLRC) DrvCreateLayerContext( hdc, iLayerPlane );
 }
 
@@ -334,3 +340,18 @@ wglRealizeLayerPalette(
 
    return FALSE;
 }
+
+
+/* When this library is used as a opengl32.dll drop-in replacement, ensure we
+ * use the wglCreate/Destroy entrypoints above, and not the true opengl32.dll,
+ * which could happen if this library's name is not opengl32.dll exactly.
+ *
+ * For example, Qt 5.4 bundles this as opengl32sw.dll:
+ * https://blog.qt.io/blog/2014/11/27/qt-weekly-21-dynamic-opengl-implementation-loading-in-qt-5-4/
+ */
+static void
+overrideOpenGL32EntryPoints(void)
+{
+   wglCreateContext_func = &wglCreateContext;
+   wglDeleteContext_func = &wglDeleteContext;
+}
-- 
2.1.0



More information about the mesa-dev mailing list