diff --git a/lib/gst/plugin/gstclapperglbaseimport.c b/lib/gst/plugin/gstclapperglbaseimport.c new file mode 100644 index 00000000..25149cb9 --- /dev/null +++ b/lib/gst/plugin/gstclapperglbaseimport.c @@ -0,0 +1,414 @@ +/* + * Copyright (C) 2022 Rafał Dzięgiel + * + * 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, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +#include "gstclapperglbaseimport.h" +#include "gstgtkutils.h" + +#if GST_CLAPPER_GL_HAVE_WAYLAND +#include +#include +#endif + +#if GST_CLAPPER_GL_HAVE_X11 +#include +#endif + +#if GST_CLAPPER_GL_HAVE_X11_GLX +#include +#endif +#if GST_CLAPPER_GL_HAVE_X11_EGL +#include +#endif + +#define GST_CAT_DEFAULT gst_clapper_gl_base_import_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +#define parent_class gst_clapper_gl_base_import_parent_class +G_DEFINE_TYPE (GstClapperGLBaseImport, gst_clapper_gl_base_import, GST_TYPE_CLAPPER_BASE_IMPORT); + +static void +gst_clapper_gl_base_import_init (GstClapperGLBaseImport *self) +{ + g_mutex_init (&self->lock); +} + +static void +gst_clapper_gl_base_import_finalize (GObject *object) +{ + GstClapperGLBaseImport *self = GST_CLAPPER_GL_BASE_IMPORT_CAST (object); + + g_clear_object (&self->gdk_context); + gst_clear_object (&self->gst_context); + gst_clear_object (&self->wrapped_context); + gst_clear_object (&self->gst_display); + + g_mutex_clear (&self->lock); + + GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object)); +} + +static GstGLContext * +wrap_current_gl (GstGLDisplay *display, GdkGLAPI gdk_gl_api, GstGLPlatform platform) +{ + GstGLAPI gst_gl_api = GST_GL_API_NONE; + + switch (gdk_gl_api) { + case GDK_GL_API_GL: + gst_gl_api = GST_GL_API_OPENGL | GST_GL_API_OPENGL3; + break; + case GDK_GL_API_GLES: + gst_gl_api = GST_GL_API_GLES2; + break; + default: + g_assert_not_reached (); + break; + } + + if (gst_gl_api != GST_GL_API_NONE) { + guintptr gl_handle; + + gst_gl_display_filter_gl_api (display, gst_gl_api); + + if ((gl_handle = gst_gl_context_get_current_gl_context (platform))) + return gst_gl_context_new_wrapped (display, gl_handle, platform, gst_gl_api); + } + + return NULL; +} + +static gboolean +retrieve_gl_context_on_main (GstClapperGLBaseImport *self) +{ + GstClapperGLBaseImportClass *gl_bi_class = GST_CLAPPER_GL_BASE_IMPORT_GET_CLASS (self); + GdkDisplay *gdk_display; + GdkGLContext *gdk_context; + GError *error = NULL; + GdkGLAPI gdk_gl_api; + GstGLPlatform platform = GST_GL_PLATFORM_NONE; + gint gl_major = 0, gl_minor = 0; + + if (!gtk_init_check ()) { + GST_ERROR_OBJECT (self, "Could not ensure GTK initialization"); + return FALSE; + } + + gdk_display = gdk_display_get_default (); + + if (!(gdk_context = gdk_display_create_gl_context (gdk_display, &error))) { + GST_ERROR_OBJECT (self, "Error creating Gdk GL context: %s", + error ? error->message : "No error set by Gdk"); + g_clear_error (&error); + + return FALSE; + } + + if (!gl_bi_class->gdk_context_realize (self, gdk_context)) { + GST_ERROR_OBJECT (self, "Could not realize Gdk context: %" GST_PTR_FORMAT, + gdk_context); + g_object_unref (gdk_context); + + return FALSE; + } + gdk_gl_api = gdk_gl_context_get_api (gdk_context); + + GST_CLAPPER_GL_BASE_IMPORT_LOCK (self); + + self->gdk_context = gdk_context; + +#if GST_CLAPPER_GL_HAVE_WAYLAND + if (GDK_IS_WAYLAND_DISPLAY (gdk_display)) { + struct wl_display *wayland_display = + gdk_wayland_display_get_wl_display (gdk_display); + self->gst_display = (GstGLDisplay *) + gst_gl_display_wayland_new_with_display (wayland_display); + } +#endif + +#if GST_CLAPPER_GL_HAVE_X11 + if (GDK_IS_X11_DISPLAY (gdk_display)) { + gpointer display_ptr; +#if GST_CLAPPER_GL_HAVE_X11_EGL + display_ptr = gdk_x11_display_get_egl_display (gdk_display); + if (display_ptr) { + self->gst_display = (GstGLDisplay *) + gst_gl_display_egl_new_with_egl_display (display_ptr); + } +#endif +#if GST_CLAPPER_GL_HAVE_X11_GLX + if (!self->gst_display) { + display_ptr = gdk_x11_display_get_xdisplay (gdk_display); + self->gst_display = (GstGLDisplay *) + gst_gl_display_x11_new_with_display (display_ptr); + } + } +#endif +#endif + + /* Fallback to generic display */ + if (G_UNLIKELY (!self->gst_display)) { + GST_WARNING_OBJECT (self, "Unknown Gdk display!"); + self->gst_display = gst_gl_display_new (); + } + +#if GST_CLAPPER_GL_HAVE_WAYLAND + if (GST_IS_GL_DISPLAY_WAYLAND (self->gst_display)) { + platform = GST_GL_PLATFORM_EGL; + GST_INFO_OBJECT (self, "Using EGL on Wayland"); + goto have_display; + } +#endif +#if GST_CLAPPER_GL_HAVE_X11_EGL + if (GST_IS_GL_DISPLAY_EGL (self->gst_display)) { + platform = GST_GL_PLATFORM_EGL; + GST_INFO_OBJECT (self, "Using EGL on x11"); + goto have_display; + } +#endif +#if GST_CLAPPER_GL_HAVE_X11_GLX + if (GST_IS_GL_DISPLAY_X11 (self->gst_display)) { + platform = GST_GL_PLATFORM_GLX; + GST_INFO_OBJECT (self, "Using GLX on x11"); + goto have_display; + } +#endif + + g_clear_object (&self->gdk_context); + gst_clear_object (&self->gst_display); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + GST_ERROR_OBJECT (self, "Unsupported GL platform"); + return FALSE; + +have_display: + gdk_gl_context_make_current (self->gdk_context); + + self->wrapped_context = wrap_current_gl (self->gst_display, gdk_gl_api, platform); + if (!self->wrapped_context) { + GST_ERROR ("Could not retrieve Gdk OpenGL context"); + gdk_gl_context_clear_current (); + + g_clear_object (&self->gdk_context); + gst_clear_object (&self->gst_display); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + return FALSE; + } + + GST_INFO ("Retrieved Gdk OpenGL context %" GST_PTR_FORMAT, self->wrapped_context); + gst_gl_context_activate (self->wrapped_context, TRUE); + + if (!gst_gl_context_fill_info (self->wrapped_context, &error)) { + GST_ERROR ("Failed to fill Gdk context info: %s", error->message); + g_clear_error (&error); + + gst_gl_context_activate (self->wrapped_context, FALSE); + + gst_clear_object (&self->wrapped_context); + g_clear_object (&self->gdk_context); + gst_clear_object (&self->gst_display); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + return FALSE; + } + + gst_gl_context_get_gl_version (self->wrapped_context, &gl_major, &gl_minor); + GST_INFO ("Using OpenGL%s %i.%i", (gdk_gl_api == GDK_GL_API_GLES) ? " ES" : "", + gl_major, gl_minor); + + /* Deactivate in both places */ + gst_gl_context_activate (self->wrapped_context, FALSE); + gdk_gl_context_clear_current (); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + return TRUE; +} + +static gboolean +ensure_gl_context (GstClapperGLBaseImport *self) +{ + GstGLDisplay *gst_display = NULL; + GstGLContext *gst_context = NULL; + GError *error = NULL; + gboolean has_gdk_contexts; + + GST_CLAPPER_GL_BASE_IMPORT_LOCK (self); + has_gdk_contexts = (self->gdk_context && self->wrapped_context); + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + if (!has_gdk_contexts) { + if (!(! !gst_gtk_invoke_on_main ( + (GThreadFunc) (GCallback) retrieve_gl_context_on_main, self))) { + GST_ERROR_OBJECT (self, "Could not retrieve Gdk GL context"); + + return FALSE; + } + } + + GST_CLAPPER_GL_BASE_IMPORT_LOCK (self); + + gst_display = gst_object_ref (self->gst_display); + + /* GstGLDisplay operations require object lock to be held */ + GST_OBJECT_LOCK (gst_display); + + if (!self->gst_context) { + GST_TRACE_OBJECT (self, "Creating new GstGLContext"); + + if (!gst_gl_display_create_context (gst_display, self->wrapped_context, + &self->gst_context, &error)) { + GST_WARNING ("Could not create OpenGL context: %s", + error ? error->message : "Unknown"); + g_clear_error (&error); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + return FALSE; + } + } + + gst_context = gst_object_ref (self->gst_context); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + /* Calls `set_context` internally, so we cannot be locked here */ + gst_gl_display_add_context (gst_display, gst_context); + gst_gl_element_propagate_display_context (GST_ELEMENT_CAST (self), gst_display); + + GST_OBJECT_UNLOCK (gst_display); + + gst_object_unref (gst_display); + gst_object_unref (gst_context); + + return TRUE; +} + +static void +gst_clapper_gl_base_import_set_context (GstElement *element, GstContext *context) +{ + GstClapperGLBaseImport *self = GST_CLAPPER_GL_BASE_IMPORT_CAST (element); + + GST_DEBUG_OBJECT (self, "Set context"); + + GST_CLAPPER_GL_BASE_IMPORT_LOCK (self); + gst_gl_handle_set_context (element, context, &self->gst_display, + &self->wrapped_context); + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + + GST_ELEMENT_CLASS (parent_class)->set_context (element, context); +} + +static GstStateChangeReturn +gst_clapper_gl_base_import_change_state (GstElement *element, GstStateChange transition) +{ + GstClapperGLBaseImport *self = GST_CLAPPER_GL_BASE_IMPORT_CAST (element); + GstStateChangeReturn ret; + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + if (ret == GST_STATE_CHANGE_FAILURE) + return ret; + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + if (!ensure_gl_context (self)) + return GST_STATE_CHANGE_FAILURE; + break; + default: + break; + } + + return ret; +} + +static gboolean +gst_clapper_gl_base_import_query (GstBaseTransform *bt, + GstPadDirection direction, GstQuery *query) +{ + GstClapperGLBaseImport *self = GST_CLAPPER_GL_BASE_IMPORT_CAST (bt); + gboolean res; + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_CONTEXT: + GST_CLAPPER_GL_BASE_IMPORT_LOCK (self); + res = gst_gl_handle_context_query (GST_ELEMENT_CAST (self), query, + self->gst_display, self->gst_context, self->wrapped_context); + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (self); + break; + default: + res = GST_BASE_TRANSFORM_CLASS (parent_class)->query (bt, direction, query); + break; + } + + return res; +} + +static gboolean +gst_clapper_gl_base_import_gdk_context_realize (GstClapperGLBaseImport *self, GdkGLContext *gdk_context) +{ + GError *error = NULL; + gboolean success; + + GST_DEBUG_OBJECT (self, "Realizing GdkGLContext with default implementation"); + + gdk_gl_context_set_allowed_apis (gdk_context, GDK_GL_API_GLES); + if (!(success = gdk_gl_context_realize (gdk_context, &error))) { + GST_WARNING_OBJECT (self, "Could not realize Gdk context with GLES: %s", error->message); + g_clear_error (&error); + } + if (!success) { + gdk_gl_context_set_allowed_apis (gdk_context, GDK_GL_API_GL); + if (!(success = gdk_gl_context_realize (gdk_context, &error))) { + GST_WARNING_OBJECT (self, "Could not realize Gdk context with GL: %s", error->message); + g_clear_error (&error); + } + } + + return success; +} + +static void +gst_clapper_gl_base_import_class_init (GstClapperGLBaseImportClass *klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + GstElementClass *gstelement_class = (GstElementClass *) klass; + GstBaseTransformClass *gstbasetransform_class = (GstBaseTransformClass *) klass; + GstClapperGLBaseImportClass *gl_bi_class = (GstClapperGLBaseImportClass *) klass; + + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "clapperglbaseimport", 0, + "Clapper GL Base Import"); + + gobject_class->finalize = gst_clapper_gl_base_import_finalize; + + gstelement_class->set_context = gst_clapper_gl_base_import_set_context; + gstelement_class->change_state = gst_clapper_gl_base_import_change_state; + + gstbasetransform_class->query = gst_clapper_gl_base_import_query; + + gl_bi_class->gdk_context_realize = gst_clapper_gl_base_import_gdk_context_realize; +} diff --git a/lib/gst/plugin/gstclapperglbaseimport.h b/lib/gst/plugin/gstclapperglbaseimport.h new file mode 100644 index 00000000..a4c9db8c --- /dev/null +++ b/lib/gst/plugin/gstclapperglbaseimport.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2022 Rafał Dzięgiel + * + * 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, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#pragma once + +#include +#include + +#include "gstclapperbaseimport.h" + +G_BEGIN_DECLS + +#define GST_TYPE_CLAPPER_GL_BASE_IMPORT (gst_clapper_gl_base_import_get_type()) +#define GST_IS_CLAPPER_GL_BASE_IMPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_CLAPPER_GL_BASE_IMPORT)) +#define GST_IS_CLAPPER_GL_BASE_IMPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_CLAPPER_GL_BASE_IMPORT)) +#define GST_CLAPPER_GL_BASE_IMPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_CLAPPER_GL_BASE_IMPORT, GstClapperGLBaseImportClass)) +#define GST_CLAPPER_GL_BASE_IMPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_CLAPPER_GL_BASE_IMPORT, GstClapperGLBaseImport)) +#define GST_CLAPPER_GL_BASE_IMPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_CLAPPER_GL_BASE_IMPORT, GstClapperGLBaseImportClass)) +#define GST_CLAPPER_GL_BASE_IMPORT_CAST(obj) ((GstClapperGLBaseImport *)(obj)) + +#define GST_CLAPPER_GL_BASE_IMPORT_GET_LOCK(obj) (&GST_CLAPPER_GL_BASE_IMPORT_CAST(obj)->lock) +#define GST_CLAPPER_GL_BASE_IMPORT_LOCK(obj) g_mutex_lock (GST_CLAPPER_GL_BASE_IMPORT_GET_LOCK(obj)) +#define GST_CLAPPER_GL_BASE_IMPORT_UNLOCK(obj) g_mutex_unlock (GST_CLAPPER_GL_BASE_IMPORT_GET_LOCK(obj)) + +#define GST_CLAPPER_GL_HAVE_WAYLAND (GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)) +#define GST_CLAPPER_GL_HAVE_X11 (GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)) +#define GST_CLAPPER_GL_HAVE_X11_GLX (GST_CLAPPER_GL_HAVE_X11 && GST_GL_HAVE_PLATFORM_GLX) +#define GST_CLAPPER_GL_HAVE_X11_EGL (GST_CLAPPER_GL_HAVE_X11 && GST_GL_HAVE_PLATFORM_EGL) + +typedef struct _GstClapperGLBaseImport GstClapperGLBaseImport; +typedef struct _GstClapperGLBaseImportClass GstClapperGLBaseImportClass; + +#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC +G_DEFINE_AUTOPTR_CLEANUP_FUNC (GstClapperGLBaseImport, gst_object_unref) +#endif + +struct _GstClapperGLBaseImport +{ + GstClapperBaseImport parent; + + GMutex lock; + + GdkGLContext *gdk_context; + GstGLContext *gst_context; + GstGLContext *wrapped_context; + GstGLDisplay *gst_display; +}; + +struct _GstClapperGLBaseImportClass +{ + GstClapperBaseImportClass parent_class; + + gboolean (* gdk_context_realize) (GstClapperGLBaseImport *gl_bi, + GdkGLContext *gdk_context); +}; + +GType gst_clapper_gl_base_import_get_type (void); + +G_END_DECLS diff --git a/lib/gst/plugin/gstclapperglimport.c b/lib/gst/plugin/gstclapperglimport.c new file mode 100644 index 00000000..46142838 --- /dev/null +++ b/lib/gst/plugin/gstclapperglimport.c @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2022 Rafał Dzięgiel + * + * 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, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstclapperglimport.h" +#include "gstclappergdkmemory.h" + +#define GST_CAT_DEFAULT gst_clapper_gl_import_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +static GstStaticPadTemplate gst_clapper_gl_import_sink_template = + GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ( + GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, + "{ " GST_CLAPPER_GDK_GL_TEXTURE_FORMATS " }") ", " + "texture-target = (string) { " GST_GL_TEXTURE_TARGET_2D_STR " }" + "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_GL_MEMORY ", " + GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, + "{ " GST_CLAPPER_GDK_GL_TEXTURE_FORMATS " }") ", " + "texture-target = (string) { " GST_GL_TEXTURE_TARGET_2D_STR " }")); + +static GstStaticPadTemplate gst_clapper_gl_import_src_template = + GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ( + GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_CLAPPER_GDK_MEMORY, + "{ " GST_CLAPPER_GDK_GL_TEXTURE_FORMATS " }") + "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_CLAPPER_GDK_MEMORY ", " + GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, + "{ " GST_CLAPPER_GDK_GL_TEXTURE_FORMATS " }"))); + +#define parent_class gst_clapper_gl_import_parent_class +G_DEFINE_TYPE (GstClapperGLImport, gst_clapper_gl_import, GST_TYPE_CLAPPER_GL_BASE_IMPORT); +GST_ELEMENT_REGISTER_DEFINE (clapperglimport, "clapperglimport", GST_RANK_NONE, + GST_TYPE_CLAPPER_GL_IMPORT); + +static GstBufferPool * +gst_clapper_gl_import_create_upstream_pool (GstClapperBaseImport *bi, GstStructure **config) +{ + GstClapperGLBaseImport *gl_bi = GST_CLAPPER_GL_BASE_IMPORT_CAST (bi); + GstBufferPool *pool; + GstGLContext *context; + + GST_DEBUG_OBJECT (bi, "Creating new pool"); + + GST_CLAPPER_GL_BASE_IMPORT_LOCK (gl_bi); + context = gst_object_ref (gl_bi->gst_context); + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (gl_bi); + + pool = gst_gl_buffer_pool_new (context); + gst_object_unref (context); + + *config = gst_buffer_pool_get_config (pool); + + gst_buffer_pool_config_add_option (*config, GST_BUFFER_POOL_OPTION_GL_SYNC_META); + + return pool; +} + +static void +video_frame_unmap_and_free (GstVideoFrame *frame) +{ + gst_video_frame_unmap (frame); + g_slice_free (GstVideoFrame, frame); +} + +static GstFlowReturn +gst_clapper_gl_import_transform (GstBaseTransform *bt, + GstBuffer *in_buf, GstBuffer *out_buf) +{ + GstClapperGLBaseImport *gl_bi = GST_CLAPPER_GL_BASE_IMPORT_CAST (bt); + GstClapperBaseImport *bi = GST_CLAPPER_BASE_IMPORT_CAST (bt); + GstVideoFrame *frame; + GstMapInfo info; + GstMemory *memory; + GstClapperGdkMemory *clapper_memory; + GstGLSyncMeta *sync_meta; + + frame = g_slice_new (GstVideoFrame); + + if (!gst_clapper_base_import_map_buffers (bi, in_buf, out_buf, + GST_MAP_READ | GST_MAP_GL, GST_MAP_WRITE, frame, &info, &memory)) { + g_slice_free (GstVideoFrame, frame); + + return GST_FLOW_ERROR; + } + + clapper_memory = GST_CLAPPER_GDK_MEMORY_CAST (memory); + + GST_CLAPPER_GL_BASE_IMPORT_LOCK (gl_bi); + + /* Must have context active here for both sync meta + * and Gdk texture format auto-detection to work */ + gdk_gl_context_make_current (gl_bi->gdk_context); + gst_gl_context_activate (gl_bi->wrapped_context, TRUE); + + sync_meta = gst_buffer_get_gl_sync_meta (in_buf); + + /* Wait for all previous OpenGL commands to complete, + * before we start using the input texture */ + if (sync_meta) { + gst_gl_sync_meta_set_sync_point (sync_meta, gl_bi->gst_context); + gst_gl_sync_meta_wait (sync_meta, gl_bi->wrapped_context); + } + + /* Keep input data alive as long as necessary, + * unmap only after texture is destroyed */ + clapper_memory->texture = gdk_gl_texture_new ( + gl_bi->gdk_context, + *(guint *) GST_VIDEO_FRAME_PLANE_DATA (frame, 0), + GST_VIDEO_FRAME_WIDTH (frame), + GST_VIDEO_FRAME_HEIGHT (frame), + (GDestroyNotify) video_frame_unmap_and_free, + frame); + + gst_gl_context_activate (gl_bi->wrapped_context, FALSE); + gdk_gl_context_clear_current (); + + GST_CLAPPER_GL_BASE_IMPORT_UNLOCK (gl_bi); + + gst_memory_unmap (memory, &info); + + return GST_FLOW_OK; +} + +static void +gst_clapper_gl_import_init (GstClapperGLImport *self) +{ +} + +static void +gst_clapper_gl_import_class_init (GstClapperGLImportClass *klass) +{ + GstElementClass *gstelement_class = (GstElementClass *) klass; + GstBaseTransformClass *gstbasetransform_class = (GstBaseTransformClass *) klass; + GstClapperBaseImportClass *bi_class = (GstClapperBaseImportClass *) klass; + + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "clapperglimport", 0, + "Clapper GL Import"); + + gstbasetransform_class->transform = gst_clapper_gl_import_transform; + + bi_class->create_upstream_pool = gst_clapper_gl_import_create_upstream_pool; + + gst_element_class_set_metadata (gstelement_class, + "Clapper GL import", + "Filter/Video", "Imports GL memory into ClapperGdkMemory", + "Rafał Dzięgiel "); + + gst_element_class_add_static_pad_template (gstelement_class, + &gst_clapper_gl_import_sink_template); + gst_element_class_add_static_pad_template (gstelement_class, + &gst_clapper_gl_import_src_template); +} diff --git a/lib/gst/plugin/gstclapperglimport.h b/lib/gst/plugin/gstclapperglimport.h new file mode 100644 index 00000000..6bf558a7 --- /dev/null +++ b/lib/gst/plugin/gstclapperglimport.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 Rafał Dzięgiel + * + * 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, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#pragma once + +#include "gstclapperglbaseimport.h" + +G_BEGIN_DECLS + +#define GST_TYPE_CLAPPER_GL_IMPORT (gst_clapper_gl_import_get_type()) +G_DECLARE_FINAL_TYPE (GstClapperGLImport, gst_clapper_gl_import, GST, CLAPPER_GL_IMPORT, GstClapperGLBaseImport) + +#define GST_CLAPPER_GL_IMPORT_CAST(obj) ((GstClapperGLImport *)(obj)) + +struct _GstClapperGLImport +{ + GstClapperGLBaseImport parent; +}; + +GST_ELEMENT_REGISTER_DECLARE (clapperglimport); + +G_END_DECLS diff --git a/lib/gst/plugin/gstplugin.c b/lib/gst/plugin/gstplugin.c index ba170cd7..01d5b3d9 100644 --- a/lib/gst/plugin/gstplugin.c +++ b/lib/gst/plugin/gstplugin.c @@ -22,6 +22,7 @@ #endif #include "gstclapperimport.h" +#include "gstclapperglimport.h" #include "gstclappersink.h" #include "gstclappergdkmemory.h" @@ -31,6 +32,7 @@ plugin_init (GstPlugin *plugin) gboolean res = FALSE; res |= GST_ELEMENT_REGISTER (clapperimport, plugin); + res |= GST_ELEMENT_REGISTER (clapperglimport, plugin); res |= GST_ELEMENT_REGISTER (clappersink, plugin); if (res) diff --git a/lib/gst/plugin/meson.build b/lib/gst/plugin/meson.build index 7f4ce76e..e8c4f1a7 100644 --- a/lib/gst/plugin/meson.build +++ b/lib/gst/plugin/meson.build @@ -50,7 +50,9 @@ gst_clapper_plugin_sources = [ 'gstclappergdkmemory.c', 'gstclappergdkbufferpool.c', 'gstclapperbaseimport.c', + 'gstclapperglbaseimport.c', 'gstclapperimport.c', + 'gstclapperglimport.c', 'gstclappersink.c', 'gstclapperpaintable.c', 'gstgtkutils.c',