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