st/wgl: Couple of fixes to opengl32.dll's wglCreateContext/wglDeleteContext dispatch.

- 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.

Reviewed-by: Brian Paul <brianp@vmware.com>
This commit is contained in:
Jose Fonseca 2015-04-14 14:25:21 +01:00
parent 6635fb6cae
commit 1aa50339d8
3 changed files with 66 additions and 9 deletions

View file

@ -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;
}

View file

@ -0,0 +1,36 @@
/*
* Mesa 3-D graphics library
*
* Copyright (C) 2011 Morgan Armand <morgan.devel@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;

View file

@ -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;
}