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