gl: GL_ARRAY_BUFFER is not a part of VAO state
[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   GError *error = NULL;
122
123   gst_gl_insert_debug_marker (priv->other_context, "initializing redisplay");
124   if (!(priv->shader = gst_gl_shader_new_default (priv->context, &error))) {
125     GST_ERROR ("Failed to initialize shader: %s", error->message);
126     return;
127   }
128
129   priv->attr_position =
130       gst_gl_shader_get_attribute_location (priv->shader, "a_position");
131   priv->attr_texture =
132       gst_gl_shader_get_attribute_location (priv->shader, "a_texcoord");
133
134   if (gl->GenVertexArrays) {
135     gl->GenVertexArrays (1, &priv->vao);
136     gl->BindVertexArray (priv->vao);
137   }
138
139   gl->GenBuffers (1, &priv->vertex_buffer);
140   gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
141   gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
142       GL_STATIC_DRAW);
143
144   if (gl->GenVertexArrays) {
145     gtk_gst_gl_widget_bind_buffer (gst_widget);
146     gl->BindVertexArray (0);
147   }
148
149   gl->BindBuffer (GL_ARRAY_BUFFER, 0);
150
151   priv->overlay_compositor =
152       gst_gl_overlay_compositor_new (priv->other_context);
153
154   priv->initted = TRUE;
155 }
156
157 static void
158 _redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
159 {
160   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
161   const GstGLFuncs *gl = priv->context->gl_vtable;
162   const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
163
164   if (gst_widget->base.force_aspect_ratio) {
165     GstVideoRectangle src, dst, result;
166     gint widget_width, widget_height, widget_scale;
167
168     gl->ClearColor (0.0, 0.0, 0.0, 0.0);
169     gl->Clear (GL_COLOR_BUFFER_BIT);
170
171     widget_scale = gtk_widget_get_scale_factor ((GtkWidget *) gst_widget);
172     widget_width = gtk_widget_get_allocated_width ((GtkWidget *) gst_widget);
173     widget_height = gtk_widget_get_allocated_height ((GtkWidget *) gst_widget);
174
175     src.x = 0;
176     src.y = 0;
177     src.w = gst_widget->base.display_width;
178     src.h = gst_widget->base.display_height;
179
180     dst.x = 0;
181     dst.y = 0;
182     dst.w = widget_width * widget_scale;
183     dst.h = widget_height * widget_scale;
184
185     gst_video_sink_center_rect (src, dst, &result, TRUE);
186
187     gl->Viewport (result.x, result.y, result.w, result.h);
188   }
189
190   gst_gl_shader_use (priv->shader);
191
192   if (gl->BindVertexArray)
193     gl->BindVertexArray (priv->vao);
194   gtk_gst_gl_widget_bind_buffer (gst_widget);
195
196   gl->ActiveTexture (GL_TEXTURE0);
197   gl->BindTexture (GL_TEXTURE_2D, tex);
198   gst_gl_shader_set_uniform_1i (priv->shader, "tex", 0);
199
200   gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
201
202   if (gl->BindVertexArray)
203     gl->BindVertexArray (0);
204   gtk_gst_gl_widget_unbind_buffer (gst_widget);
205
206   gl->BindTexture (GL_TEXTURE_2D, 0);
207 }
208
209 static inline void
210 _draw_black (GstGLContext * context)
211 {
212   const GstGLFuncs *gl = context->gl_vtable;
213
214   gst_gl_insert_debug_marker (context, "no buffer.  rendering black");
215   gl->ClearColor (0.0, 0.0, 0.0, 0.0);
216   gl->Clear (GL_COLOR_BUFFER_BIT);
217 }
218
219 static gboolean
220 gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
221 {
222   GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (widget)->priv;
223   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (widget);
224
225   GTK_GST_BASE_WIDGET_LOCK (widget);
226
227   if (!priv->context || !priv->other_context)
228     goto done;
229
230   gst_gl_context_activate (priv->other_context, TRUE);
231
232   if (!priv->initted)
233     gtk_gst_gl_widget_init_redisplay (GTK_GST_GL_WIDGET (widget));
234
235   if (!priv->initted || !base_widget->negotiated) {
236     _draw_black (priv->other_context);
237     goto done;
238   }
239
240   /* Upload latest buffer */
241   if (base_widget->pending_buffer) {
242     GstBuffer *buffer = base_widget->pending_buffer;
243     GstVideoFrame gl_frame;
244     GstGLSyncMeta *sync_meta;
245
246     if (!gst_video_frame_map (&gl_frame, &base_widget->v_info, buffer,
247             GST_MAP_READ | GST_MAP_GL)) {
248       _draw_black (priv->other_context);
249       goto done;
250     }
251
252     priv->current_tex = *(guint *) gl_frame.data[0];
253     gst_gl_insert_debug_marker (priv->other_context, "redrawing texture %u",
254         priv->current_tex);
255
256     gst_gl_overlay_compositor_upload_overlays (priv->overlay_compositor,
257         buffer);
258
259     sync_meta = gst_buffer_get_gl_sync_meta (buffer);
260     if (sync_meta) {
261       /* XXX: the set_sync() seems to be needed for resizing */
262       gst_gl_sync_meta_set_sync_point (sync_meta, priv->context);
263       gst_gl_sync_meta_wait (sync_meta, priv->other_context);
264     }
265
266     gst_video_frame_unmap (&gl_frame);
267
268     if (base_widget->buffer)
269       gst_buffer_unref (base_widget->buffer);
270
271     /* Keep the buffer to ensure current_tex stay valid */
272     base_widget->buffer = buffer;
273     base_widget->pending_buffer = NULL;
274   }
275
276   GST_DEBUG ("rendering buffer %p with gdk context %p",
277       base_widget->buffer, context);
278
279   _redraw_texture (GTK_GST_GL_WIDGET (widget), priv->current_tex);
280   gst_gl_overlay_compositor_draw_overlays (priv->overlay_compositor);
281
282   gst_gl_insert_debug_marker (priv->other_context, "texture %u redrawn",
283       priv->current_tex);
284
285 done:
286   if (priv->other_context)
287     gst_gl_context_activate (priv->other_context, FALSE);
288
289   GTK_GST_BASE_WIDGET_UNLOCK (widget);
290   return FALSE;
291 }
292
293 static void
294 _reset_gl (GtkGstGLWidget * gst_widget)
295 {
296   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
297   const GstGLFuncs *gl = priv->other_context->gl_vtable;
298
299   if (!priv->gdk_context)
300     priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
301
302   if (priv->gdk_context == NULL)
303     return;
304
305   gdk_gl_context_make_current (priv->gdk_context);
306   gst_gl_context_activate (priv->other_context, TRUE);
307
308   if (priv->vao) {
309     gl->DeleteVertexArrays (1, &priv->vao);
310     priv->vao = 0;
311   }
312
313   if (priv->vertex_buffer) {
314     gl->DeleteBuffers (1, &priv->vertex_buffer);
315     priv->vertex_buffer = 0;
316   }
317
318   if (priv->upload) {
319     gst_object_unref (priv->upload);
320     priv->upload = NULL;
321   }
322
323   if (priv->shader) {
324     gst_object_unref (priv->shader);
325     priv->shader = NULL;
326   }
327
328   if (priv->overlay_compositor)
329     gst_object_unref (priv->overlay_compositor);
330
331   gst_gl_context_activate (priv->other_context, FALSE);
332
333   gst_object_unref (priv->other_context);
334   priv->other_context = NULL;
335
336   gdk_gl_context_clear_current ();
337
338   g_object_unref (priv->gdk_context);
339   priv->gdk_context = NULL;
340 }
341
342 static void
343 gtk_gst_gl_widget_finalize (GObject * object)
344 {
345   GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (object)->priv;
346   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (object);
347
348   if (priv->other_context)
349     gst_gtk_invoke_on_main ((GThreadFunc) _reset_gl, base_widget);
350
351   if (priv->context)
352     gst_object_unref (priv->context);
353
354   if (priv->display)
355     gst_object_unref (priv->display);
356
357   gtk_gst_base_widget_finalize (object);
358   G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
359 }
360
361 static void
362 gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
363 {
364   GObjectClass *gobject_klass = (GObjectClass *) klass;
365   GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
366
367   g_type_class_add_private (klass, sizeof (GtkGstGLWidgetPrivate));
368   gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
369
370   gobject_klass->finalize = gtk_gst_gl_widget_finalize;
371   gl_widget_klass->render = gtk_gst_gl_widget_render;
372 }
373
374 static void
375 gtk_gst_gl_widget_init (GtkGstGLWidget * gst_widget)
376 {
377   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (gst_widget);
378   GdkDisplay *display;
379   GtkGstGLWidgetPrivate *priv;
380
381   gtk_gst_base_widget_init (base_widget);
382
383   gst_widget->priv = priv = GTK_GST_GL_WIDGET_GET_PRIVATE (gst_widget);
384
385   display = gdk_display_get_default ();
386
387 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (GDK_WINDOWING_X11)
388   if (GDK_IS_X11_DISPLAY (display))
389     priv->display = (GstGLDisplay *)
390         gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
391         (display));
392 #endif
393 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
394   if (GDK_IS_WAYLAND_DISPLAY (display)) {
395     struct wl_display *wayland_display =
396         gdk_wayland_display_get_wl_display (display);
397     priv->display = (GstGLDisplay *)
398         gst_gl_display_wayland_new_with_display (wayland_display);
399   }
400 #endif
401
402   (void) display;
403
404   if (!priv->display)
405     priv->display = gst_gl_display_new ();
406
407   gtk_gl_area_set_has_alpha (GTK_GL_AREA (gst_widget),
408       !base_widget->ignore_alpha);
409 }
410
411 static void
412 _get_gl_context (GtkGstGLWidget * gst_widget)
413 {
414   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
415   GstGLPlatform platform;
416   GstGLAPI gl_api;
417   guintptr gl_handle;
418
419   gtk_widget_realize (GTK_WIDGET (gst_widget));
420
421   if (priv->other_context)
422     gst_object_unref (priv->other_context);
423   priv->other_context = NULL;
424
425   if (priv->gdk_context)
426     g_object_unref (priv->gdk_context);
427
428   priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
429   if (priv->gdk_context == NULL) {
430     GError *error = gtk_gl_area_get_error (GTK_GL_AREA (gst_widget));
431
432     GST_ERROR_OBJECT (gst_widget, "Error creating GdkGLContext : %s",
433         error ? error->message : "No error set by Gdk");
434     g_clear_error (&error);
435     return;
436   }
437
438   g_object_ref (priv->gdk_context);
439
440   gdk_gl_context_make_current (priv->gdk_context);
441
442 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (GDK_WINDOWING_X11)
443   if (GST_IS_GL_DISPLAY_X11 (priv->display)) {
444     platform = GST_GL_PLATFORM_GLX;
445     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
446     gl_handle = gst_gl_context_get_current_gl_context (platform);
447     if (gl_handle)
448       priv->other_context =
449           gst_gl_context_new_wrapped (priv->display, gl_handle,
450           platform, gl_api);
451   }
452 #endif
453 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
454   if (GST_IS_GL_DISPLAY_WAYLAND (priv->display)) {
455     platform = GST_GL_PLATFORM_EGL;
456     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
457     gl_handle = gst_gl_context_get_current_gl_context (platform);
458     if (gl_handle)
459       priv->other_context =
460           gst_gl_context_new_wrapped (priv->display, gl_handle,
461           platform, gl_api);
462   }
463 #endif
464
465   (void) platform;
466   (void) gl_api;
467   (void) gl_handle;
468
469   if (priv->other_context) {
470     GError *error = NULL;
471
472     gst_gl_context_activate (priv->other_context, TRUE);
473     if (!gst_gl_context_fill_info (priv->other_context, &error)) {
474       GST_ERROR ("failed to retrieve gdk context info: %s", error->message);
475       g_clear_error (&error);
476       g_object_unref (priv->other_context);
477       priv->other_context = NULL;
478     } else {
479       gst_gl_context_activate (priv->other_context, FALSE);
480     }
481   }
482 }
483
484 GtkWidget *
485 gtk_gst_gl_widget_new (void)
486 {
487   return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
488 }
489
490 gboolean
491 gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * gst_widget)
492 {
493   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
494   GError *error = NULL;
495
496   g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (gst_widget), FALSE);
497
498   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
499
500   if (priv->display && priv->gdk_context && priv->other_context) {
501     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
502     return TRUE;
503   }
504
505   if (!priv->other_context) {
506     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
507     gst_gtk_invoke_on_main ((GThreadFunc) _get_gl_context, gst_widget);
508     GTK_GST_BASE_WIDGET_LOCK (gst_widget);
509   }
510
511   if (!GST_IS_GL_CONTEXT (priv->other_context)) {
512     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
513     return FALSE;
514   }
515
516   if (!gst_gl_display_create_context (priv->display, priv->other_context,
517           &priv->context, &error)) {
518     g_clear_error (&error);
519     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
520     return FALSE;
521   }
522
523   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
524   return TRUE;
525 }
526
527 GstGLContext *
528 gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
529 {
530   if (!gst_widget->priv->other_context)
531     return NULL;
532
533   return gst_object_ref (gst_widget->priv->other_context);
534 }
535
536 GstGLContext *
537 gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
538 {
539   if (!gst_widget->priv->context)
540     return NULL;
541
542   return gst_object_ref (gst_widget->priv->context);
543 }
544
545 GstGLDisplay *
546 gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
547 {
548   if (!gst_widget->priv->display)
549     return NULL;
550
551   return gst_object_ref (gst_widget->priv->display);
552 }