From 627ee674e56938f941af41c5f1833f714b58a6fd Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Fri, 24 Mar 2023 11:08:26 +0800 Subject: [PATCH] GDK-Win32: Add wrapper functions for calling core wgl* functions We might be dealing with GL contexts from different threads, which have more gotchas when we are using libepoxy, so in case the function pointers for these are invalidated by wglMakeCurrent() calls outside of GTK/GDK, such as in GstGL, we want to use these functions that are directly linked to opengl32.dll provided by the system/ICD, by linking to opengl32.lib. This will ensure that we will indeed call the "correct" wgl* functions that we need. This should help fix issue #5685. --- gdk/win32/gdkglcontext-win32-wgl-private.c | 56 ++++++++++++++++++++++ gdk/win32/gdkglcontext-win32-wgl.c | 12 ++--- gdk/win32/gdkglcontext-win32.h | 17 +++++++ gdk/win32/meson.build | 2 + 4 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 gdk/win32/gdkglcontext-win32-wgl-private.c diff --git a/gdk/win32/gdkglcontext-win32-wgl-private.c b/gdk/win32/gdkglcontext-win32-wgl-private.c new file mode 100644 index 0000000000..7da5016f05 --- /dev/null +++ b/gdk/win32/gdkglcontext-win32-wgl-private.c @@ -0,0 +1,56 @@ +/* GDK - The GIMP Drawing Kit + * + * gdkglcontext-win32-wgl-private.c: Win32 specific OpenGL wrappers + * + * Copyright © 2023 Chun-wei Fan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library. If not, see . + */ + +/* + * These wrapper functions are used when we don't want to use the wgl*() core functions + * that we acquire via libepoxy (such as when we are disposing the Gdk(W)GLContext from, + * different threads, so for these calls, we are actually linking to the system's/ICD + * opengl32.dll directly, so that we are guaranteed that the "right" versions of these + * WGL calls are carried out. This must be a separate source file because we can't include + * the system's GL/gl.h with epoxy/(w)gl.h together in a single source file. We should not + * need to use these when we are creating/initializing a WGL context in GDK, since we should + * be in the same thread at this point. + */ + +#define DONT_INCLUDE_LIBEPOXY +#define WIN32_LEAN_AND_MEAN +#include +#include + +#include "gdkglcontext-win32.h" + +void +gdk_win32_private_wglDeleteContext (HGLRC hglrc) +{ + wglDeleteContext (hglrc); +} + +HGLRC +gdk_win32_private_wglGetCurrentContext (void) +{ + return wglGetCurrentContext (); +} + +BOOL +gdk_win32_private_wglMakeCurrent (HDC hdc, + HGLRC hglrc) +{ + return wglMakeCurrent (hdc, hglrc); +} diff --git a/gdk/win32/gdkglcontext-win32-wgl.c b/gdk/win32/gdkglcontext-win32-wgl.c index 4c32a74f88..572c2f37b7 100644 --- a/gdk/win32/gdkglcontext-win32-wgl.c +++ b/gdk/win32/gdkglcontext-win32-wgl.c @@ -60,12 +60,12 @@ gdk_win32_gl_context_wgl_dispose (GObject *gobject) if (context_wgl->wgl_context != NULL) { - if (wglGetCurrentContext () == context_wgl->wgl_context) - wglMakeCurrent (NULL, NULL); + if (gdk_win32_private_wglGetCurrentContext () == context_wgl->wgl_context) + gdk_win32_private_wglMakeCurrent (NULL, NULL); GDK_NOTE (OPENGL, g_print ("Destroying WGL context\n")); - wglDeleteContext (context_wgl->wgl_context); + gdk_win32_private_wglDeleteContext (context_wgl->wgl_context); context_wgl->wgl_context = NULL; } @@ -628,7 +628,7 @@ gdk_win32_gl_context_wgl_realize (GdkGLContext *context, static gboolean gdk_win32_gl_context_wgl_clear_current (GdkGLContext *context) { - return wglMakeCurrent (NULL, NULL); + return gdk_win32_private_wglMakeCurrent (NULL, NULL); } static gboolean @@ -636,7 +636,7 @@ gdk_win32_gl_context_wgl_is_current (GdkGLContext *context) { GdkWin32GLContextWGL *self = GDK_WIN32_GL_CONTEXT_WGL (context); - return self->wgl_context == wglGetCurrentContext (); + return self->wgl_context == gdk_win32_private_wglGetCurrentContext (); } static gboolean @@ -654,7 +654,7 @@ gdk_win32_gl_context_wgl_make_current (GdkGLContext *context, else hdc = GDK_WIN32_SURFACE (surface)->hdc; - if (!wglMakeCurrent (hdc, context_wgl->wgl_context)) + if (!gdk_win32_private_wglMakeCurrent (hdc, context_wgl->wgl_context)) return FALSE; if (!surfaceless && display_win32->hasWglEXTSwapControl) diff --git a/gdk/win32/gdkglcontext-win32.h b/gdk/win32/gdkglcontext-win32.h index 77995671ce..787c8b1a09 100644 --- a/gdk/win32/gdkglcontext-win32.h +++ b/gdk/win32/gdkglcontext-win32.h @@ -21,6 +21,7 @@ #ifndef __GDK_WIN32_GL_CONTEXT__ #define __GDK_WIN32_GL_CONTEXT__ +#ifndef DONT_INCLUDE_LIBEPOXY #include #include @@ -31,9 +32,18 @@ #include "gdkglcontextprivate.h" #include "gdkdisplayprivate.h" #include "gdksurface.h" +#else +# define WIN32_LEAN_AND_MEAN +# include +# include + +# include +#endif G_BEGIN_DECLS +#ifndef DONT_INCLUDE_LIBEPOXY + #define GDK_WIN32_GL_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_WIN32_GL_CONTEXT, GdkWin32GLContextClass)) #define GDK_WIN32_GL_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_WIN32_GL_CONTEXT, GdkWin32GLContextClass)) #define GDK_WIN32_IS_GL_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_WIN32_GL_CONTEXT)) @@ -78,6 +88,13 @@ GType gdk_win32_gl_context_egl_get_type (void) G_GNUC_CONST; void _gdk_win32_surface_invalidate_egl_framebuffer (GdkSurface *surface); +#endif /* !DONT_INCLUDE_LIBEPOXY */ + +HGLRC gdk_win32_private_wglGetCurrentContext (void); +BOOL gdk_win32_private_wglMakeCurrent (HDC hdc, + HGLRC hglrc); +void gdk_win32_private_wglDeleteContext (HGLRC hglrc); + G_END_DECLS #endif /* __GDK_WIN32_GL_CONTEXT__ */ diff --git a/gdk/win32/meson.build b/gdk/win32/meson.build index bcf7d7c78f..3dc16aabb6 100644 --- a/gdk/win32/meson.build +++ b/gdk/win32/meson.build @@ -23,6 +23,7 @@ gdk_win32_sources = gdk_win32_public_sources + files([ 'gdkdevice-wintab.c', 'gdkdrop-win32.c', 'gdkglobals-win32.c', + 'gdkglcontext-win32-wgl-private.c', 'gdkhdataoutputstream-win32.c', 'gdkinput-dmanipulation.c', 'gdkinput-winpointer.c', @@ -59,6 +60,7 @@ endif gdk_win32_deps = [ pangowin32_dep, # FIXME cc.find_library('hid'), + cc.find_library('opengl32'), ] libgdk_win32 = static_library('gdk-win32', -- 2.30.2