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