summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChun-wei Fan <fanchunwei@src.gnome.org>2023-03-27 10:45:05 +0800
committerChun-wei Fan <fanchunwei@src.gnome.org>2023-04-19 11:31:26 +0800
commitf9fc556f4a413f44142db38fa95bf8f27c79d1b3 (patch)
treef16ebcca3683d63aae01d9679a65208e975aa4c6
parent4986c622c18f7eaa72b2bb8b214f930493db5e77 (diff)
downloadgtk+-backport-5702-3-24.tar.gz
GDK-Win32: Backport MR !5702 into gtk-3-24backport-5702-3-24
Since we might be using GdkGLContext across different threads and may be using GdkGLContext with different libraries or programs that make the WGL context current outside of GTK/GDK, make things safer for such applications or libraries, such as GstGL. This will make some wgl*() calls in ->make_current() and ->dispose() link directly to the system's/ICD opengl32.dll instead of going through the function pointers that are acquired via libepoxy, which can be rendered invalid when the WGL context is made current outside of GdkGLContext, leading to weird crashes. By linking directly to opengl32.dll, we are assured that these wgl*() calls will be carried out correctly. Note that this is not done when initializing or realizing the WGL context as we are in the same thread as these are all carried out within GTK/GDK, as it should not be necessary to do so there.
-rw-r--r--gdk/win32/gdkglcontext-win32-wgl-private.c56
-rw-r--r--gdk/win32/gdkglcontext-win32.c12
-rw-r--r--gdk/win32/gdkglcontext-win32.h16
-rw-r--r--gdk/win32/meson.build4
4 files changed, 81 insertions, 7 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * 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 <windows.h>
+#include <GL/gl.h>
+
+#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.c b/gdk/win32/gdkglcontext-win32.c
index d303df2a63..f5507f1232 100644
--- a/gdk/win32/gdkglcontext-win32.c
+++ b/gdk/win32/gdkglcontext-win32.c
@@ -397,12 +397,12 @@ gdk_win32_gl_context_dispose_wgl (GObject *gobject)
{
GdkWindow *window = gdk_gl_context_get_window (context);
- 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;
gdk_win32_gl_context_cleanup (context);
@@ -843,7 +843,7 @@ gdk_win32_display_make_wgl_context_current (GdkDisplay *display,
if (context == NULL)
{
- wglMakeCurrent(NULL, NULL);
+ gdk_win32_private_wglMakeCurrent (NULL, NULL);
return TRUE;
}
@@ -851,8 +851,8 @@ gdk_win32_display_make_wgl_context_current (GdkDisplay *display,
context_win32 = GDK_WIN32_GL_CONTEXT (context);
window = gdk_gl_context_get_window (context);
- if (!wglMakeCurrent (context_win32->gl_hdc,
- GDK_WIN32_GL_CONTEXT_WGL (context)->wgl_context))
+ if (!gdk_win32_private_wglMakeCurrent (context_win32->gl_hdc,
+ GDK_WIN32_GL_CONTEXT_WGL (context)->wgl_context))
{
GDK_NOTE (OPENGL, g_print ("Making WGL context current failed\n"));
return FALSE;
diff --git a/gdk/win32/gdkglcontext-win32.h b/gdk/win32/gdkglcontext-win32.h
index a4b8b2dda2..864941b901 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 <epoxy/gl.h>
#include <epoxy/wgl.h>
@@ -33,8 +34,17 @@
#include "gdkvisual.h"
#include "gdkwindow.h"
+#else
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+# include <GL/gl.h>
+
+# include <glib.h>
+#endif
+
G_BEGIN_DECLS
+#ifndef DONT_INCLUDE_LIBEPOXY
void
gdk_win32_window_invalidate_egl_framebuffer (GdkWindow *window);
@@ -51,6 +61,12 @@ gdk_win32_window_invalidate_for_new_frame (GdkWindow *window,
gboolean
gdk_win32_display_make_gl_context_current (GdkDisplay *display,
GdkGLContext *context);
+#endif
+
+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
diff --git a/gdk/win32/meson.build b/gdk/win32/meson.build
index ce35090aec..084389fae2 100644
--- a/gdk/win32/meson.build
+++ b/gdk/win32/meson.build
@@ -11,6 +11,7 @@ gdk_win32_sources = files(
'gdkevents-win32.c',
'gdkgeometry-win32.c',
'gdkglcontext-win32.c',
+ 'gdkglcontext-win32-wgl-private.c',
'gdkglobals-win32.c',
'gdkkeys-win32.c',
'gdkkeys-win32-impl.c',
@@ -51,7 +52,8 @@ install_headers('gdkwin32.h', subdir: 'gtk-3.0/gdk')
gdk_win32_deps = [ # FIXME
pangowin32_dep,
- meson.get_compiler('c').find_library('hid')
+ cc.find_library('hid'),
+ cc.find_library('opengl32')
]
libgdk_win32 = static_library('gdk-win32',