From b155f5d73e2aafd6b8b3118538fd896e66671937 Mon Sep 17 00:00:00 2001 From: Lubosz Sarnecki Date: Tue, 21 Jul 2015 21:27:45 -0400 Subject: [PATCH] gloverlaycompositor: Create own shader object Make gloverlaycompositor independent of the shader used in the sink. https://bugzilla.gnome.org/show_bug.cgi?id=745107 --- ext/gl/gstglimagesink.c | 6 +-- gst-libs/gst/gl/gstgloverlaycompositor.c | 49 +++++++++++++++++++----- gst-libs/gst/gl/gstgloverlaycompositor.h | 10 +++-- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/ext/gl/gstglimagesink.c b/ext/gl/gstglimagesink.c index 5c13ef46b9..adb56d08d2 100644 --- a/ext/gl/gstglimagesink.c +++ b/ext/gl/gstglimagesink.c @@ -1287,8 +1287,7 @@ prepare_next_buffer (GstGLImageSink * glimage_sink) } gst_gl_overlay_compositor_upload_overlays (glimage_sink->overlay_compositor, - next_buffer, glimage_sink->attr_position, glimage_sink->attr_texture, - glimage_sink->window_width, glimage_sink->window_height); + next_buffer, glimage_sink->window_width, glimage_sink->window_height); /* in_buffer invalid now */ if (!gst_video_frame_map (&gl_frame, info, next_buffer, @@ -1880,8 +1879,7 @@ gst_glimage_sink_on_draw (GstGLImageSink * gl_sink) gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0); - gst_gl_overlay_compositor_draw_overlays (gl_sink->overlay_compositor, - gl_sink->redisplay_shader); + gst_gl_overlay_compositor_draw_overlays (gl_sink->overlay_compositor); gst_gl_context_clear_shader (gl_sink->context); diff --git a/gst-libs/gst/gl/gstgloverlaycompositor.c b/gst-libs/gst/gl/gstgloverlaycompositor.c index 548556ba07..9b164abb3d 100644 --- a/gst-libs/gst/gl/gstgloverlaycompositor.c +++ b/gst-libs/gst/gl/gstgloverlaycompositor.c @@ -54,6 +54,20 @@ gst_gl_overlay_compositor_init (GstGLOverlayCompositor * compositor) { } +static void +gst_gl_overlay_compositor_init_gl (GstGLContext * context, + gpointer compositor_pointer) +{ + GstGLOverlayCompositor *compositor = + (GstGLOverlayCompositor *) compositor_pointer; + + if (!gst_gl_shader_compile_with_default_vf_and_check + (compositor->shader, &compositor->position_attrib, + &compositor->texcoord_attrib)) { + GST_ERROR ("could not initialize shader."); + } +} + GstGLOverlayCompositor * gst_gl_overlay_compositor_new (GstGLContext * context) { @@ -62,6 +76,11 @@ gst_gl_overlay_compositor_new (GstGLContext * context) compositor->context = gst_object_ref (context); + compositor->shader = gst_gl_shader_new (compositor->context); + + gst_gl_context_thread_add (compositor->context, + gst_gl_overlay_compositor_init_gl, compositor); + GST_DEBUG_OBJECT (compositor, "Created new GstGLOverlayCompositor"); return compositor; @@ -79,6 +98,11 @@ gst_gl_overlay_compositor_finalize (GObject * object) if (compositor->context) gst_object_unref (compositor->context); + if (compositor->shader) { + gst_object_unref (compositor->shader); + compositor->shader = NULL; + } + G_OBJECT_CLASS (gst_gl_overlay_compositor_parent_class)->finalize (object); } @@ -129,8 +153,7 @@ gst_gl_overlay_compositor_free_overlays (GstGLOverlayCompositor * compositor) void gst_gl_overlay_compositor_upload_overlays (GstGLOverlayCompositor * compositor, - GstBuffer * buf, GLint position_attrib, GLint texcoord_attrib, - guint window_width, guint window_height) + GstBuffer * buf, guint window_width, guint window_height) { GstVideoOverlayCompositionMeta *composition_meta; @@ -161,10 +184,11 @@ gst_gl_overlay_compositor_upload_overlays (GstGLOverlayCompositor * compositor, if (!_is_rectangle_in_overlays (compositor->overlays, rectangle)) { GstGLCompositionOverlay *overlay = gst_gl_composition_overlay_new (compositor->context, rectangle, - position_attrib, texcoord_attrib); + compositor->position_attrib, compositor->texcoord_attrib); + + gst_gl_composition_overlay_upload (overlay, buf, window_width, + window_height); - gst_gl_composition_overlay_upload (overlay, buf, - window_width, window_height); compositor->overlays = g_list_append (compositor->overlays, overlay); } } @@ -185,19 +209,24 @@ gst_gl_overlay_compositor_upload_overlays (GstGLOverlayCompositor * compositor, } void -gst_gl_overlay_compositor_draw_overlays (GstGLOverlayCompositor * compositor, - GstGLShader * shader) +gst_gl_overlay_compositor_draw_overlays (GstGLOverlayCompositor * compositor) { const GstGLFuncs *gl = compositor->context->gl_vtable; if (compositor->overlays != NULL) { GList *l; - glEnable (GL_BLEND); - glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + gl->Enable (GL_BLEND); + gl->BlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + gst_gl_shader_use (compositor->shader); + gl->ActiveTexture (GL_TEXTURE0); + gst_gl_shader_set_uniform_1i (compositor->shader, "tex", 0); + for (l = compositor->overlays; l != NULL; l = l->next) { GstGLCompositionOverlay *overlay = (GstGLCompositionOverlay *) l->data; - gst_gl_composition_overlay_draw (overlay, shader); + gst_gl_composition_overlay_draw (overlay, compositor->shader); } + gl->BindTexture (GL_TEXTURE_2D, 0); } } diff --git a/gst-libs/gst/gl/gstgloverlaycompositor.h b/gst-libs/gst/gl/gstgloverlaycompositor.h index c0675b42af..cd8ae858de 100644 --- a/gst-libs/gst/gl/gstgloverlaycompositor.h +++ b/gst-libs/gst/gl/gstgloverlaycompositor.h @@ -49,6 +49,10 @@ struct _GstGLOverlayCompositor guint last_window_height; GList * overlays; + + GstGLShader *shader; + GLint position_attrib; + GLint texcoord_attrib; }; /** @@ -65,11 +69,9 @@ GstGLOverlayCompositor *gst_gl_overlay_compositor_new (GstGLContext * context); void gst_gl_overlay_compositor_free_overlays (GstGLOverlayCompositor * compositor); void gst_gl_overlay_compositor_upload_overlays (GstGLOverlayCompositor * compositor, - GstBuffer * buf, GLint position_attrib, GLint texcoord_attrib, - guint window_width, guint window_height); + GstBuffer * buf, guint window_width, guint window_height); -void gst_gl_overlay_compositor_draw_overlays (GstGLOverlayCompositor * compositor, - GstGLShader *shader); +void gst_gl_overlay_compositor_draw_overlays (GstGLOverlayCompositor * compositor); GstCaps * gst_gl_overlay_compositor_add_caps(GstCaps * caps);