gtkglsink: expose the created display and context correctly
[platform/upstream/gst-plugins-good.git] / ext / gtk / gtkgstglwidget.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include "gtkgstglwidget.h"
28 #include "gstgtkutils.h"
29 #include <gst/gl/gstglfuncs.h>
30 #include <gst/video/video.h>
31
32 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (GDK_WINDOWING_X11)
33 #include <gdk/gdkx.h>
34 #include <gst/gl/x11/gstgldisplay_x11.h>
35 #include <gst/gl/x11/gstglcontext_glx.h>
36 #endif
37
38 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
39 #include <gdk/gdkwayland.h>
40 #include <gst/gl/wayland/gstgldisplay_wayland.h>
41 #endif
42
43 /**
44  * SECTION:gtkgstglwidget
45  * @title: GtkGstGlWidget
46  * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
47  * @see_also: #GtkGLArea, #GstBuffer
48  *
49  * #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
50  */
51
52 #define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
53 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
54
55 G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
56     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
57         "Gtk Gst GL Widget"););
58
59 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
60     GTK_TYPE_GST_GL_WIDGET, GtkGstGLWidgetPrivate))
61
62 struct _GtkGstGLWidgetPrivate
63 {
64   gboolean initted;
65   GstGLDisplay *display;
66   GdkGLContext *gdk_context;
67   GstGLContext *other_context;
68   GstGLContext *context;
69   GstGLUpload *upload;
70   GstGLShader *shader;
71   GLuint vao;
72   GLuint vertex_buffer;
73   GLint attr_position;
74   GLint attr_texture;
75   GLuint current_tex;
76   GstGLOverlayCompositor *overlay_compositor;
77 };
78
79 static const GLfloat vertices[] = {
80   1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
81   -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
82   -1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
83   1.0f, -1.0f, 0.0f, 1.0f, 1.0f
84 };
85
86 static void
87 gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
88 {
89   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
90   const GstGLFuncs *gl = priv->context->gl_vtable;
91
92   gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
93
94   /* Load the vertex position */
95   gl->VertexAttribPointer (priv->attr_position, 3, GL_FLOAT, GL_FALSE,
96       5 * sizeof (GLfloat), (void *) 0);
97
98   /* Load the texture coordinate */
99   gl->VertexAttribPointer (priv->attr_texture, 2, GL_FLOAT, GL_FALSE,
100       5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
101
102   gl->EnableVertexAttribArray (priv->attr_position);
103   gl->EnableVertexAttribArray (priv->attr_texture);
104 }
105
106 static void
107 gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
108 {
109   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
110   const GstGLFuncs *gl = priv->context->gl_vtable;
111
112   gl->BindBuffer (GL_ARRAY_BUFFER, 0);
113
114   gl->DisableVertexAttribArray (priv->attr_position);
115   gl->DisableVertexAttribArray (priv->attr_texture);
116 }
117
118 static void
119 gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
120 {
121   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
122   const GstGLFuncs *gl = priv->context->gl_vtable;
123   GError *error = NULL;
124
125   gst_gl_insert_debug_marker (priv->other_context, "initializing redisplay");
126   if (!(priv->shader = gst_gl_shader_new_default (priv->context, &error))) {
127     GST_ERROR ("Failed to initialize shader: %s", error->message);
128     return;
129   }
130
131   priv->attr_position =
132       gst_gl_shader_get_attribute_location (priv->shader, "a_position");
133   priv->attr_texture =
134       gst_gl_shader_get_attribute_location (priv->shader, "a_texcoord");
135
136   if (gl->GenVertexArrays) {
137     gl->GenVertexArrays (1, &priv->vao);
138     gl->BindVertexArray (priv->vao);
139   }
140
141   gl->GenBuffers (1, &priv->vertex_buffer);
142   gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
143   gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
144       GL_STATIC_DRAW);
145
146   if (gl->GenVertexArrays) {
147     gtk_gst_gl_widget_bind_buffer (gst_widget);
148     gl->BindVertexArray (0);
149   }
150
151   gl->BindBuffer (GL_ARRAY_BUFFER, 0);
152
153   priv->overlay_compositor =
154       gst_gl_overlay_compositor_new (priv->other_context);
155
156   priv->initted = TRUE;
157 }
158
159 static void
160 _redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
161 {
162   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
163   const GstGLFuncs *gl = priv->context->gl_vtable;
164   const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
165
166   if (gst_widget->base.force_aspect_ratio) {
167     GstVideoRectangle src, dst, result;
168     gint widget_width, widget_height, widget_scale;
169
170     gl->ClearColor (0.0, 0.0, 0.0, 0.0);
171     gl->Clear (GL_COLOR_BUFFER_BIT);
172
173     widget_scale = gtk_widget_get_scale_factor ((GtkWidget *) gst_widget);
174     widget_width = gtk_widget_get_allocated_width ((GtkWidget *) gst_widget);
175     widget_height = gtk_widget_get_allocated_height ((GtkWidget *) gst_widget);
176
177     src.x = 0;
178     src.y = 0;
179     src.w = gst_widget->base.display_width;
180     src.h = gst_widget->base.display_height;
181
182     dst.x = 0;
183     dst.y = 0;
184     dst.w = widget_width * widget_scale;
185     dst.h = widget_height * widget_scale;
186
187     gst_video_sink_center_rect (src, dst, &result, TRUE);
188
189     gl->Viewport (result.x, result.y, result.w, result.h);
190   }
191
192   gst_gl_shader_use (priv->shader);
193
194   if (gl->BindVertexArray)
195     gl->BindVertexArray (priv->vao);
196   gtk_gst_gl_widget_bind_buffer (gst_widget);
197
198   gl->ActiveTexture (GL_TEXTURE0);
199   gl->BindTexture (GL_TEXTURE_2D, tex);
200   gst_gl_shader_set_uniform_1i (priv->shader, "tex", 0);
201
202   gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
203
204   if (gl->BindVertexArray)
205     gl->BindVertexArray (0);
206   gtk_gst_gl_widget_unbind_buffer (gst_widget);
207
208   gl->BindTexture (GL_TEXTURE_2D, 0);
209 }
210
211 static inline void
212 _draw_black (GstGLContext * context)
213 {
214   const GstGLFuncs *gl = context->gl_vtable;
215
216   gst_gl_insert_debug_marker (context, "no buffer.  rendering black");
217   gl->ClearColor (0.0, 0.0, 0.0, 0.0);
218   gl->Clear (GL_COLOR_BUFFER_BIT);
219 }
220
221 static gboolean
222 gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
223 {
224   GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (widget)->priv;
225   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (widget);
226
227   GTK_GST_BASE_WIDGET_LOCK (widget);
228
229   if (!priv->context || !priv->other_context)
230     goto done;
231
232   gst_gl_context_activate (priv->other_context, TRUE);
233
234   if (!priv->initted)
235     gtk_gst_gl_widget_init_redisplay (GTK_GST_GL_WIDGET (widget));
236
237   if (!priv->initted || !base_widget->negotiated) {
238     _draw_black (priv->other_context);
239     goto done;
240   }
241
242   /* Upload latest buffer */
243   if (base_widget->pending_buffer) {
244     GstBuffer *buffer = base_widget->pending_buffer;
245     GstVideoFrame gl_frame;
246     GstGLSyncMeta *sync_meta;
247
248     if (!gst_video_frame_map (&gl_frame, &base_widget->v_info, buffer,
249             GST_MAP_READ | GST_MAP_GL)) {
250       _draw_black (priv->other_context);
251       goto done;
252     }
253
254     priv->current_tex = *(guint *) gl_frame.data[0];
255     gst_gl_insert_debug_marker (priv->other_context, "redrawing texture %u",
256         priv->current_tex);
257
258     gst_gl_overlay_compositor_upload_overlays (priv->overlay_compositor,
259         buffer);
260
261     sync_meta = gst_buffer_get_gl_sync_meta (buffer);
262     if (sync_meta) {
263       /* XXX: the set_sync() seems to be needed for resizing */
264       gst_gl_sync_meta_set_sync_point (sync_meta, priv->context);
265       gst_gl_sync_meta_wait (sync_meta, priv->other_context);
266     }
267
268     gst_video_frame_unmap (&gl_frame);
269
270     if (base_widget->buffer)
271       gst_buffer_unref (base_widget->buffer);
272
273     /* Keep the buffer to ensure current_tex stay valid */
274     base_widget->buffer = buffer;
275     base_widget->pending_buffer = NULL;
276   }
277
278   GST_DEBUG ("rendering buffer %p with gdk context %p",
279       base_widget->buffer, context);
280
281   _redraw_texture (GTK_GST_GL_WIDGET (widget), priv->current_tex);
282   gst_gl_overlay_compositor_draw_overlays (priv->overlay_compositor);
283
284   gst_gl_insert_debug_marker (priv->other_context, "texture %u redrawn",
285       priv->current_tex);
286
287 done:
288   if (priv->other_context)
289     gst_gl_context_activate (priv->other_context, FALSE);
290
291   GTK_GST_BASE_WIDGET_UNLOCK (widget);
292   return FALSE;
293 }
294
295 static void
296 _reset_gl (GtkGstGLWidget * gst_widget)
297 {
298   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
299   const GstGLFuncs *gl = priv->other_context->gl_vtable;
300
301   if (!priv->gdk_context)
302     priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
303
304   if (priv->gdk_context == NULL)
305     return;
306
307   gdk_gl_context_make_current (priv->gdk_context);
308   gst_gl_context_activate (priv->other_context, TRUE);
309
310   if (priv->vao) {
311     gl->DeleteVertexArrays (1, &priv->vao);
312     priv->vao = 0;
313   }
314
315   if (priv->vertex_buffer) {
316     gl->DeleteBuffers (1, &priv->vertex_buffer);
317     priv->vertex_buffer = 0;
318   }
319
320   if (priv->upload) {
321     gst_object_unref (priv->upload);
322     priv->upload = NULL;
323   }
324
325   if (priv->shader) {
326     gst_object_unref (priv->shader);
327     priv->shader = NULL;
328   }
329
330   if (priv->overlay_compositor)
331     gst_object_unref (priv->overlay_compositor);
332
333   gst_gl_context_activate (priv->other_context, FALSE);
334
335   gst_object_unref (priv->other_context);
336   priv->other_context = NULL;
337
338   gdk_gl_context_clear_current ();
339
340   g_object_unref (priv->gdk_context);
341   priv->gdk_context = NULL;
342 }
343
344 static void
345 gtk_gst_gl_widget_finalize (GObject * object)
346 {
347   GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (object)->priv;
348   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (object);
349
350   if (priv->other_context)
351     gst_gtk_invoke_on_main ((GThreadFunc) _reset_gl, base_widget);
352
353   if (priv->context)
354     gst_object_unref (priv->context);
355
356   if (priv->display)
357     gst_object_unref (priv->display);
358
359   gtk_gst_base_widget_finalize (object);
360   G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
361 }
362
363 static void
364 gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
365 {
366   GObjectClass *gobject_klass = (GObjectClass *) klass;
367   GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
368
369   g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
370   gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
371
372   gobject_klass->finalize = gtk_gst_gl_widget_finalize;
373   gl_widget_klass->render = gtk_gst_gl_widget_render;
374 }
375
376 static void
377 gtk_gst_gl_widget_init (GtkGstGLWidget * gst_widget)
378 {
379   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (gst_widget);
380   GdkDisplay *display;
381   GtkGstGLWidgetPrivate *priv;
382
383   gtk_gst_base_widget_init (base_widget);
384
385   gst_widget->priv = priv = GTK_GST_GL_WIDGET_GET_PRIVATE (gst_widget);
386
387   display = gdk_display_get_default ();
388
389 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (GDK_WINDOWING_X11)
390   if (GDK_IS_X11_DISPLAY (display))
391     priv->display = (GstGLDisplay *)
392         gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
393         (display));
394 #endif
395 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
396   if (GDK_IS_WAYLAND_DISPLAY (display)) {
397     struct wl_display *wayland_display =
398         gdk_wayland_display_get_wl_display (display);
399     priv->display = (GstGLDisplay *)
400         gst_gl_display_wayland_new_with_display (wayland_display);
401   }
402 #endif
403
404   (void) display;
405
406   if (!priv->display)
407     priv->display = gst_gl_display_new ();
408
409   gtk_gl_area_set_has_alpha (GTK_GL_AREA (gst_widget),
410       !base_widget->ignore_alpha);
411 }
412
413 static void
414 _get_gl_context (GtkGstGLWidget * gst_widget)
415 {
416   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
417   GstGLPlatform platform;
418   GstGLAPI gl_api;
419   guintptr gl_handle;
420
421   gtk_widget_realize (GTK_WIDGET (gst_widget));
422
423   if (priv->other_context)
424     gst_object_unref (priv->other_context);
425   priv->other_context = NULL;
426
427   if (priv->gdk_context)
428     g_object_unref (priv->gdk_context);
429
430   priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
431   if (priv->gdk_context == NULL) {
432     GError *error = gtk_gl_area_get_error (GTK_GL_AREA (gst_widget));
433
434     GST_ERROR_OBJECT (gst_widget, "Error creating GdkGLContext : %s",
435         error ? error->message : "No error set by Gdk");
436     g_clear_error (&error);
437     return;
438   }
439
440   g_object_ref (priv->gdk_context);
441
442   gdk_gl_context_make_current (priv->gdk_context);
443
444 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (GDK_WINDOWING_X11)
445   if (GST_IS_GL_DISPLAY_X11 (priv->display)) {
446     platform = GST_GL_PLATFORM_GLX;
447     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
448     gl_handle = gst_gl_context_get_current_gl_context (platform);
449     if (gl_handle)
450       priv->other_context =
451           gst_gl_context_new_wrapped (priv->display, gl_handle,
452           platform, gl_api);
453   }
454 #endif
455 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
456   if (GST_IS_GL_DISPLAY_WAYLAND (priv->display)) {
457     platform = GST_GL_PLATFORM_EGL;
458     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
459     gl_handle = gst_gl_context_get_current_gl_context (platform);
460     if (gl_handle)
461       priv->other_context =
462           gst_gl_context_new_wrapped (priv->display, gl_handle,
463           platform, gl_api);
464   }
465 #endif
466
467   (void) platform;
468   (void) gl_api;
469   (void) gl_handle;
470
471   if (priv->other_context) {
472     GError *error = NULL;
473
474     gst_gl_context_activate (priv->other_context, TRUE);
475     if (!gst_gl_context_fill_info (priv->other_context, &error)) {
476       GST_ERROR ("failed to retrieve gdk context info: %s", error->message);
477       g_clear_error (&error);
478       g_object_unref (priv->other_context);
479       priv->other_context = NULL;
480     } else {
481       gst_gl_context_activate (priv->other_context, FALSE);
482     }
483   }
484 }
485
486 GtkWidget *
487 gtk_gst_gl_widget_new (void)
488 {
489   return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
490 }
491
492 gboolean
493 gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * gst_widget)
494 {
495   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
496   GError *error = NULL;
497
498   g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (gst_widget), FALSE);
499
500   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
501
502   if (priv->display && priv->gdk_context && priv->other_context) {
503     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
504     return TRUE;
505   }
506
507   if (!priv->other_context) {
508     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
509     gst_gtk_invoke_on_main ((GThreadFunc) _get_gl_context, gst_widget);
510     GTK_GST_BASE_WIDGET_LOCK (gst_widget);
511   }
512
513   if (!GST_IS_GL_CONTEXT (priv->other_context)) {
514     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
515     return FALSE;
516   }
517
518   GST_OBJECT_LOCK (priv->display);
519   if (!gst_gl_display_create_context (priv->display, priv->other_context,
520           &priv->context, &error)) {
521     g_clear_error (&error);
522     GST_OBJECT_UNLOCK (priv->display);
523     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
524     return FALSE;
525   }
526   gst_gl_display_add_context (priv->display, priv->context);
527   GST_OBJECT_UNLOCK (priv->display);
528
529   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
530   return TRUE;
531 }
532
533 GstGLContext *
534 gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
535 {
536   if (!gst_widget->priv->other_context)
537     return NULL;
538
539   return gst_object_ref (gst_widget->priv->other_context);
540 }
541
542 GstGLContext *
543 gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
544 {
545   if (!gst_widget->priv->context)
546     return NULL;
547
548   return gst_object_ref (gst_widget->priv->context);
549 }
550
551 GstGLDisplay *
552 gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
553 {
554   if (!gst_widget->priv->display)
555     return NULL;
556
557   return gst_object_ref (gst_widget->priv->display);
558 }