3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #include "gtkgstglwidget.h"
28 #include <gst/video/video.h>
30 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
32 #include <gst/gl/x11/gstgldisplay_x11.h>
33 #include <gst/gl/x11/gstglcontext_glx.h>
36 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
37 #include <gdk/gdkwayland.h>
38 #include <gst/gl/wayland/gstgldisplay_wayland.h>
42 * SECTION:gtkgstglwidget
43 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
44 * @see_also: #GtkGLArea, #GstBuffer
46 * #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
49 #define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
53 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
57 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
58 GTK_TYPE_GST_GL_WIDGET, GtkGstGLWidgetPrivate))
60 struct _GtkGstGLWidgetPrivate
63 GstGLDisplay *display;
64 GdkGLContext *gdk_context;
65 GstGLContext *other_context;
66 GstGLContext *context;
74 GstGLOverlayCompositor *overlay_compositor;
77 static const GLfloat vertices[] = {
78 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
79 -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
80 -1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
81 1.0f, -1.0f, 0.0f, 1.0f, 1.0f
85 gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
87 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
88 const GstGLFuncs *gl = priv->context->gl_vtable;
90 gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
92 /* Load the vertex position */
93 gl->VertexAttribPointer (priv->attr_position, 3, GL_FLOAT, GL_FALSE,
94 5 * sizeof (GLfloat), (void *) 0);
96 /* Load the texture coordinate */
97 gl->VertexAttribPointer (priv->attr_texture, 2, GL_FLOAT, GL_FALSE,
98 5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
100 gl->EnableVertexAttribArray (priv->attr_position);
101 gl->EnableVertexAttribArray (priv->attr_texture);
105 gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
107 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
108 const GstGLFuncs *gl = priv->context->gl_vtable;
110 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
112 gl->DisableVertexAttribArray (priv->attr_position);
113 gl->DisableVertexAttribArray (priv->attr_texture);
117 gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
119 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
120 const GstGLFuncs *gl = priv->context->gl_vtable;
122 priv->shader = gst_gl_shader_new (priv->context);
124 gst_gl_shader_compile_with_default_vf_and_check (priv->shader,
125 &priv->attr_position, &priv->attr_texture);
127 if (gl->GenVertexArrays) {
128 gl->GenVertexArrays (1, &priv->vao);
129 gl->BindVertexArray (priv->vao);
132 gl->GenBuffers (1, &priv->vertex_buffer);
133 gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
134 gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
137 if (gl->GenVertexArrays) {
138 gtk_gst_gl_widget_bind_buffer (gst_widget);
139 gl->BindVertexArray (0);
142 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
144 priv->overlay_compositor =
145 gst_gl_overlay_compositor_new (priv->other_context);
147 priv->initted = TRUE;
151 _redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
153 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
154 const GstGLFuncs *gl = priv->context->gl_vtable;
155 const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
157 if (gst_widget->base.force_aspect_ratio) {
158 GstVideoRectangle src, dst, result;
159 gint widget_width, widget_height, widget_scale;
161 gl->ClearColor (0.0, 0.0, 0.0, 0.0);
162 gl->Clear (GL_COLOR_BUFFER_BIT);
164 widget_scale = gtk_widget_get_scale_factor ((GtkWidget *) gst_widget);
165 widget_width = gtk_widget_get_allocated_width ((GtkWidget *) gst_widget);
166 widget_height = gtk_widget_get_allocated_height ((GtkWidget *) gst_widget);
170 src.w = gst_widget->base.display_width;
171 src.h = gst_widget->base.display_height;
175 dst.w = widget_width * widget_scale;
176 dst.h = widget_height * widget_scale;
178 gst_video_sink_center_rect (src, dst, &result, TRUE);
180 gl->Viewport (result.x, result.y, result.w, result.h);
183 gst_gl_shader_use (priv->shader);
185 if (gl->BindVertexArray)
186 gl->BindVertexArray (priv->vao);
188 gtk_gst_gl_widget_bind_buffer (gst_widget);
190 gl->ActiveTexture (GL_TEXTURE0);
191 gl->BindTexture (GL_TEXTURE_2D, tex);
192 gst_gl_shader_set_uniform_1i (priv->shader, "tex", 0);
194 gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
196 if (gl->BindVertexArray)
197 gl->BindVertexArray (0);
199 gtk_gst_gl_widget_unbind_buffer (gst_widget);
201 gl->BindTexture (GL_TEXTURE_2D, 0);
205 _draw_black (GstGLContext * context)
207 const GstGLFuncs *gl = context->gl_vtable;
209 gl->ClearColor (0.0, 0.0, 0.0, 0.0);
210 gl->Clear (GL_COLOR_BUFFER_BIT);
214 gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
216 GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (widget)->priv;
217 GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (widget);
219 GTK_GST_BASE_WIDGET_LOCK (widget);
221 if (!priv->context || !priv->other_context)
224 gst_gl_context_activate (priv->other_context, TRUE);
227 gtk_gst_gl_widget_init_redisplay (GTK_GST_GL_WIDGET (widget));
229 if (!priv->initted || !base_widget->negotiated) {
230 _draw_black (priv->other_context);
234 /* Upload latest buffer */
235 if (base_widget->pending_buffer) {
236 GstBuffer *buffer = base_widget->pending_buffer;
237 GstVideoFrame gl_frame;
238 GstGLSyncMeta *sync_meta;
240 if (!gst_video_frame_map (&gl_frame, &base_widget->v_info, buffer,
241 GST_MAP_READ | GST_MAP_GL)) {
242 _draw_black (priv->other_context);
246 gst_gl_overlay_compositor_upload_overlays (priv->overlay_compositor,
249 sync_meta = gst_buffer_get_gl_sync_meta (buffer);
251 gst_gl_sync_meta_set_sync_point (sync_meta, priv->context);
252 gst_gl_sync_meta_wait (sync_meta, priv->other_context);
255 priv->current_tex = *(guint *) gl_frame.data[0];
257 gst_video_frame_unmap (&gl_frame);
259 if (base_widget->buffer)
260 gst_buffer_unref (base_widget->buffer);
262 /* Keep the buffer to ensure current_tex stay valid */
263 base_widget->buffer = buffer;
264 base_widget->pending_buffer = NULL;
267 GST_DEBUG ("rendering buffer %p with gdk context %p",
268 base_widget->buffer, context);
270 _redraw_texture (GTK_GST_GL_WIDGET (widget), priv->current_tex);
271 gst_gl_overlay_compositor_draw_overlays (priv->overlay_compositor);
274 if (priv->other_context)
275 gst_gl_context_activate (priv->other_context, FALSE);
277 GTK_GST_BASE_WIDGET_UNLOCK (widget);
281 typedef void (*ThreadFunc) (gpointer data);
283 struct invoke_context
293 _invoke_func (struct invoke_context *info)
295 g_mutex_lock (&info->lock);
296 info->func (info->data);
298 g_cond_signal (&info->cond);
299 g_mutex_unlock (&info->lock);
301 return G_SOURCE_REMOVE;
305 _invoke_on_main (ThreadFunc func, gpointer data)
307 GMainContext *main_context = g_main_context_default ();
308 struct invoke_context info;
310 g_mutex_init (&info.lock);
311 g_cond_init (&info.cond);
316 g_main_context_invoke (main_context, (GSourceFunc) _invoke_func, &info);
318 g_mutex_lock (&info.lock);
320 g_cond_wait (&info.cond, &info.lock);
321 g_mutex_unlock (&info.lock);
323 g_mutex_clear (&info.lock);
324 g_cond_clear (&info.cond);
328 _reset_gl (GtkGstGLWidget * gst_widget)
330 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
331 const GstGLFuncs *gl = priv->other_context->gl_vtable;
333 if (!priv->gdk_context)
334 priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
336 if (priv->gdk_context == NULL)
339 gdk_gl_context_make_current (priv->gdk_context);
340 gst_gl_context_activate (priv->other_context, TRUE);
343 gl->DeleteVertexArrays (1, &priv->vao);
347 if (priv->vertex_buffer) {
348 gl->DeleteBuffers (1, &priv->vertex_buffer);
349 priv->vertex_buffer = 0;
353 gst_object_unref (priv->upload);
358 gst_object_unref (priv->shader);
362 if (priv->overlay_compositor)
363 gst_object_unref (priv->overlay_compositor);
365 gst_gl_context_activate (priv->other_context, FALSE);
367 gst_object_unref (priv->other_context);
368 priv->other_context = NULL;
370 gdk_gl_context_clear_current ();
372 g_object_unref (priv->gdk_context);
373 priv->gdk_context = NULL;
377 gtk_gst_gl_widget_finalize (GObject * object)
379 GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (object)->priv;
380 GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (object);
382 if (priv->other_context)
383 _invoke_on_main ((ThreadFunc) _reset_gl, base_widget);
386 gst_object_unref (priv->context);
389 gst_object_unref (priv->display);
391 gtk_gst_base_widget_finalize (object);
392 G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
396 gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
398 GObjectClass *gobject_klass = (GObjectClass *) klass;
399 GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
401 g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
402 gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
404 gobject_klass->finalize = gtk_gst_gl_widget_finalize;
405 gl_widget_klass->render = gtk_gst_gl_widget_render;
409 gtk_gst_gl_widget_init (GtkGstGLWidget * gst_widget)
411 GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (gst_widget);
413 GtkGstGLWidgetPrivate *priv;
415 gtk_gst_base_widget_init (base_widget);
417 gst_widget->priv = priv = GTK_GST_GL_WIDGET_GET_PRIVATE (gst_widget);
419 display = gdk_display_get_default ();
421 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
422 if (GDK_IS_X11_DISPLAY (display))
423 priv->display = (GstGLDisplay *)
424 gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
427 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
428 if (GDK_IS_WAYLAND_DISPLAY (display)) {
429 struct wl_display *wayland_display =
430 gdk_wayland_display_get_wl_display (display);
431 priv->display = (GstGLDisplay *)
432 gst_gl_display_wayland_new_with_display (wayland_display);
439 priv->display = gst_gl_display_new ();
441 gtk_gl_area_set_has_alpha (GTK_GL_AREA (gst_widget),
442 !base_widget->ignore_alpha);
446 _get_gl_context (GtkGstGLWidget * gst_widget)
448 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
449 GstGLPlatform platform;
453 gtk_widget_realize (GTK_WIDGET (gst_widget));
455 if (priv->gdk_context)
456 g_object_unref (priv->gdk_context);
457 priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
458 if (priv->gdk_context == NULL) {
459 GError *error = gtk_gl_area_get_error (GTK_GL_AREA (gst_widget));
461 GST_ERROR_OBJECT (gst_widget, "Error creating GdkGLContext : %s",
462 error ? error->message : "No error set by Gdk");
463 g_assert_not_reached ();
467 g_object_ref (priv->gdk_context);
469 gdk_gl_context_make_current (priv->gdk_context);
471 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
472 if (GST_IS_GL_DISPLAY_X11 (priv->display)) {
473 platform = GST_GL_PLATFORM_GLX;
474 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
475 gl_handle = gst_gl_context_get_current_gl_context (platform);
477 priv->other_context =
478 gst_gl_context_new_wrapped (priv->display, gl_handle,
482 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
483 if (GST_IS_GL_DISPLAY_WAYLAND (priv->display)) {
484 platform = GST_GL_PLATFORM_EGL;
485 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
486 gl_handle = gst_gl_context_get_current_gl_context (platform);
488 priv->other_context =
489 gst_gl_context_new_wrapped (priv->display, gl_handle,
498 if (priv->other_context) {
499 GError *error = NULL;
501 gst_gl_context_activate (priv->other_context, TRUE);
502 if (!gst_gl_context_fill_info (priv->other_context, &error)) {
503 GST_ERROR ("failed to retreive gdk context info: %s", error->message);
504 g_object_unref (priv->other_context);
505 priv->other_context = NULL;
507 gst_gl_context_activate (priv->other_context, FALSE);
513 gtk_gst_gl_widget_new (void)
515 return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
519 gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * gst_widget)
521 GtkGstGLWidgetPrivate *priv = gst_widget->priv;
522 GError *error = NULL;
524 g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (gst_widget), FALSE);
526 GTK_GST_BASE_WIDGET_LOCK (gst_widget);
528 if (priv->display && priv->gdk_context && priv->other_context) {
529 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
533 if (!priv->other_context) {
534 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
535 _invoke_on_main ((ThreadFunc) _get_gl_context, gst_widget);
536 GTK_GST_BASE_WIDGET_LOCK (gst_widget);
539 if (!GST_GL_IS_CONTEXT (priv->other_context)) {
540 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
544 if (!gst_gl_display_create_context (priv->display, priv->other_context,
545 &priv->context, &error)) {
546 g_clear_error (&error);
547 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
551 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
556 gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
558 if (!gst_widget->priv->other_context)
561 return gst_object_ref (gst_widget->priv->other_context);
565 gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
567 if (!gst_widget->priv->context)
570 return gst_object_ref (gst_widget->priv->context);
574 gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
576 if (!gst_widget->priv->display)
579 return gst_object_ref (gst_widget->priv->display);