[gtk/wip.win32.fixes: 30/39] gtkgstsink.c: Support EGL on Windows as well




commit d011e81793faf2cecd37dc3497e91b053230a5b9
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Mon May 17 16:04:53 2021 +0800

    gtkgstsink.c: Support EGL on Windows as well
    
    Add support to look for and use the EGL context in Windows if it was activated
    instead of desktop OpenGL.
    
    GstGL may have been built with or without EGL/libANGLE support, so if it were,
    set in the environment that we want to use a Win32 winsys and a GL API that
    corresponds to the GL API that is used in our GdkGLContext.  If we are using
    EGL, set the API to glesv2 as it is needed for proper libANGLE support.
    
    Unfortunately things must be set via envvars as GstGL do not have APIs for
    Windows to set these things up in other ways.

 modules/media/gtkgstsink.c | 71 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 54 insertions(+), 17 deletions(-)
---
diff --git a/modules/media/gtkgstsink.c b/modules/media/gtkgstsink.c
index a08e4646c7..bdbc1ad9ea 100644
--- a/modules/media/gtkgstsink.c
+++ b/modules/media/gtkgstsink.c
@@ -31,9 +31,6 @@
 #if GST_GL_HAVE_PLATFORM_GLX
 #include <gst/gl/x11/gstgldisplay_x11.h>
 #endif
-#if GST_GL_HAVE_PLATFORM_EGL
-#include <gst/gl/egl/gstgldisplay_egl.h>
-#endif
 #endif
 
 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
@@ -42,11 +39,15 @@
 #include <gst/gl/wayland/gstgldisplay_wayland.h>
 #endif
 
-#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (GDK_WINDOWING_WIN32)
+#if GST_GL_HAVE_WINDOW_WIN32 && (GST_GL_HAVE_PLATFORM_WGL || GST_GL_HAVE_PLATFORM_EGL) && defined 
(GDK_WINDOWING_WIN32)
 #include <gdk/win32/gdkwin32.h>
 #include <epoxy/wgl.h>
 #endif
 
+#if GST_GL_HAVE_PLATFORM_EGL && (GST_GL_HAVE_WINDOW_WIN32 || GST_GL_HAVE_WINDOW_X11)
+#include <gst/gl/egl/gstgldisplay_egl.h>
+#endif
+
 #include <gst/gl/gstglfuncs.h>
 
 enum {
@@ -353,11 +354,18 @@ gtk_gst_sink_show_frame (GstVideoSink *vsink,
   return GST_FLOW_OK;
 }
 
-#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (GDK_WINDOWING_WIN32)
-#define HANDLE_EXTERNAL_WGL_MAKE_CURRENT epoxy_handle_external_wglMakeCurrent()
+#if GST_GL_HAVE_WINDOW_WIN32 && (GST_GL_HAVE_PLATFORM_WGL || GST_GL_HAVE_PLATFORM_EGL) && defined 
(GDK_WINDOWING_WIN32)
+#define HANDLE_EXTERNAL_WGL_MAKE_CURRENT(ctx) handle_wgl_makecurrent(ctx)
 #define DEACTIVATE_WGL_CONTEXT(ctx) deactivate_gdk_wgl_context(ctx)
 #define REACTIVATE_WGL_CONTEXT(ctx) reactivate_gdk_wgl_context(ctx)
 
+static void
+handle_wgl_makecurrent (GdkGLContext *ctx)
+{
+  if (!gdk_gl_context_get_use_es (ctx))
+    epoxy_handle_external_wglMakeCurrent();
+}
+
 static void
 deactivate_gdk_wgl_context (GdkGLContext *ctx)
 {
@@ -375,7 +383,7 @@ reactivate_gdk_wgl_context (GdkGLContext *ctx)
     gdk_gl_context_make_current (ctx);
 }
 #else
-#define HANDLE_EXTERNAL_WGL_MAKE_CURRENT
+#define HANDLE_EXTERNAL_WGL_MAKE_CURRENT(ctx)
 #define DEACTIVATE_WGL_CONTEXT(ctx)
 #define REACTIVATE_WGL_CONTEXT(ctx)
 #endif
@@ -391,7 +399,7 @@ gtk_gst_sink_initialize_gl (GtkGstSink *self)
 
   display = gdk_gl_context_get_display (self->gdk_context);
 
-  HANDLE_EXTERNAL_WGL_MAKE_CURRENT;
+  HANDLE_EXTERNAL_WGL_MAKE_CURRENT (self->gdk_context);
   gdk_gl_context_make_current (self->gdk_context);
 
 #ifdef HAVE_GST_X11_SUPPORT
@@ -459,25 +467,54 @@ gtk_gst_sink_initialize_gl (GtkGstSink *self)
     }
   else
 #endif
-#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (GDK_WINDOWING_WIN32)
-  if (GDK_IS_WIN32_DISPLAY (display) &&
-      !gdk_gl_context_get_use_es (self->gdk_context))
+#if GST_GL_HAVE_WINDOW_WIN32 && (GST_GL_HAVE_PLATFORM_WGL || GST_GL_HAVE_PLATFORM_EGL) && defined 
(GDK_WINDOWING_WIN32)
+  if (GDK_IS_WIN32_DISPLAY (display))
     {
-      platform = GST_GL_PLATFORM_WGL;
+      gboolean is_gles = gdk_gl_context_get_use_es (self->gdk_context);
+      const gchar *gl_type = is_gles ? "EGL" : "WGL";
+
+      platform = is_gles ? GST_GL_PLATFORM_EGL : GST_GL_PLATFORM_WGL;
 
-      GST_DEBUG_OBJECT (self, "got WGL on Win32!");
+      GST_DEBUG_OBJECT (self, "got %s on Win32!", gl_type);
 
       gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
       gl_handle = gst_gl_context_get_current_gl_context (platform);
 
       if (gl_handle)
         {
-          self->gst_display = gst_gl_display_new ();
+          gchar *gl_api_str = NULL;
+          gpointer display_ptr = NULL;
+
+#ifdef GST_GL_HAVE_PLATFORM_EGL
+          /*
+           * We must force a win32 GstGL display type and if using desktop GL, the GL_Platform to be WGL
+           * and an appropriate GstGL API depending on the gl_api we receive.  We also ensure that we use
+           * an EGL GstGL API if we are using EGL in GDK.  Envvars are required, unfortunately, so that
+           * gst_gl_display_new() does things correctly if we have GstGL built with both EGL and WGL
+           * support, otherwise gst_gl_display_new() will assume an EGL display, which won't work for us
+           */
+          gl_api_str = gst_gl_api_to_string (gl_api);
+          g_setenv ("GST_GL_API", gl_api_str, TRUE);
+          g_free (gl_api_str);
+
+          if (gl_api == GST_GL_API_OPENGL3 || gl_api == GST_GL_API_OPENGL)
+            {
+              g_setenv ("GST_GL_WINDOW", "win32", TRUE);
+              g_setenv("GST_GL_PLATFORM", "wgl", TRUE);
+              self->gst_display = gst_gl_display_new ();
+            }
+          else
+            {
+              gpointer display_ptr = gdk_win32_display_get_egl_display (display);
+              self->gst_display = GST_GL_DISPLAY (gst_gl_display_egl_new_with_egl_display (display_ptr));
+            }
+#endif
+
           self->gst_app_context = gst_gl_context_new_wrapped (self->gst_display, gl_handle, platform, 
gl_api);
         }
       else
         {
-          GST_ERROR_OBJECT (self, "Failed to get handle from GdkGLContext, not using WGL");
+          GST_ERROR_OBJECT (self, "Failed to get handle from GdkGLContext, not using %s", gl_type);
              return;
         }
     }
@@ -498,7 +535,7 @@ gtk_gst_sink_initialize_gl (GtkGstSink *self)
       g_clear_error (&error);
       g_clear_object (&self->gst_app_context);
       g_clear_object (&self->gst_display);
-      HANDLE_EXTERNAL_WGL_MAKE_CURRENT;
+      HANDLE_EXTERNAL_WGL_MAKE_CURRENT (self->gdk_context);;
       return;
     }
   else
@@ -515,7 +552,7 @@ gtk_gst_sink_initialize_gl (GtkGstSink *self)
       g_clear_object (&self->gst_display);
     }
 
-  HANDLE_EXTERNAL_WGL_MAKE_CURRENT;
+  HANDLE_EXTERNAL_WGL_MAKE_CURRENT (self->gdk_context);
   REACTIVATE_WGL_CONTEXT (self->gdk_context);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]