qmlglsink: Schedule onSceneGrpahInitialized to execute on render thread
[platform/upstream/gst-plugins-good.git] / ext / qt / qtitem.cc
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 <gst/video/video.h>
28 #include "qtitem.h"
29 #include "gstqsgtexture.h"
30
31 #include <QtCore/QRunnable>
32 #include <QtGui/QGuiApplication>
33 #include <QtQuick/QQuickWindow>
34 #include <QtQuick/QSGSimpleTextureNode>
35
36 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (HAVE_QT_X11)
37 #include <QX11Info>
38 #include <gst/gl/x11/gstgldisplay_x11.h>
39 #include <gst/gl/x11/gstglcontext_glx.h>
40 #endif
41
42 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
43 #include <gst/gl/wayland/gstgldisplay_wayland.h>
44 #endif
45
46 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
47 #include <gst/gl/egl/gstgldisplay_egl.h>
48 #include <gst/gl/egl/gstglcontext_egl.h>
49 #endif
50
51 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
52 #include <gst/gl/coaoa/gstgldisplay_cocoa.h>
53 #endif
54
55 /**
56  * SECTION:gtkgstglwidget
57  * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
58  * @see_also: #GtkGLArea, #GstBuffer
59  *
60  * #QtGLVideoItem is an #GtkWidget that renders GStreamer video buffers.
61  */
62
63 #define GST_CAT_DEFAULT qt_item_debug
64 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
65
66 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
67     GTK_TYPE_GST_GL_WIDGET, QtGLVideoItemPrivate))
68
69 #define DEFAULT_FORCE_ASPECT_RATIO  TRUE
70 #define DEFAULT_PAR_N               0
71 #define DEFAULT_PAR_D               1
72
73 enum
74 {
75   PROP_0,
76   PROP_FORCE_ASPECT_RATIO,
77   PROP_PIXEL_ASPECT_RATIO,
78 };
79
80 struct _QtGLVideoItemPrivate
81 {
82   GMutex lock;
83
84   /* properties */
85   gboolean force_aspect_ratio;
86   gint par_n, par_d;
87
88   gint display_width;
89   gint display_height;
90
91   gboolean negotiated;
92   GstBuffer *buffer;
93   GstCaps *caps;
94   GstVideoInfo v_info;
95
96   gboolean initted;
97   GstGLDisplay *display;
98   QOpenGLContext *qt_context;
99   GstGLContext *other_context;
100   GstGLContext *context;
101 };
102
103 class InitializeSceneGraph : public QRunnable
104 {
105 public:
106   InitializeSceneGraph(QtGLVideoItem *item);
107   void run();
108
109 private:
110   QtGLVideoItem *item_;
111 };
112
113 InitializeSceneGraph::InitializeSceneGraph(QtGLVideoItem *item) :
114   item_(item)
115 {
116 }
117
118 void InitializeSceneGraph::run()
119 {
120   item_->onSceneGraphInitialized();
121 }
122
123 QtGLVideoItem::QtGLVideoItem()
124 {
125   QGuiApplication *app = dynamic_cast<QGuiApplication *> (QCoreApplication::instance ());
126   static volatile gsize _debug;
127
128   g_assert (app != NULL);
129
130   if (g_once_init_enter (&_debug)) {
131     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
132     g_once_init_leave (&_debug, 1);
133   }
134   this->m_openGlContextInitialized = false;
135   this->setFlag (QQuickItem::ItemHasContents, true);
136
137   this->priv = g_new0 (QtGLVideoItemPrivate, 1);
138
139   this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
140   this->priv->par_n = DEFAULT_PAR_N;
141   this->priv->par_d = DEFAULT_PAR_D;
142
143   g_mutex_init (&this->priv->lock);
144
145 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
146   if (QString::fromUtf8 ("xcb") == app->platformName())
147     this->priv->display = (GstGLDisplay *)
148         gst_gl_display_x11_new_with_display (QX11Info::display ());
149 #endif
150 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
151   if (QString::fromUtf8 ("android") == app->platformName())
152     this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
153 #endif
154 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
155   if (QString::fromUtf8 ("cocoa") == app->platformName())
156     this->priv->display = (GstGLDisplay *) gst_gl_display_cocoa_new ();
157 #endif
158 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
159   if (QString::fromUtf8 ("ios") == app->platformName())
160     this->priv->display = gst_gl_display_new ();
161 #endif
162
163   if (!this->priv->display)
164     this->priv->display = gst_gl_display_new ();
165
166   connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
167           SLOT(handleWindowChanged(QQuickWindow*)));
168
169   GST_DEBUG ("%p init Qt Video Item", this);
170 }
171
172 QtGLVideoItem::~QtGLVideoItem()
173 {
174   g_mutex_clear (&this->priv->lock);
175
176   g_free (this->priv);
177   this->priv = NULL;
178 }
179
180 void
181 QtGLVideoItem::setDAR(gint num, gint den)
182 {
183   this->priv->par_n = num;
184   this->priv->par_d = den;
185 }
186
187 void
188 QtGLVideoItem::getDAR(gint * num, gint * den)
189 {
190   if (num)
191     *num = this->priv->par_n;
192   if (den)
193     *den = this->priv->par_d;
194 }
195
196 void
197 QtGLVideoItem::setForceAspectRatio(bool far)
198 {
199   this->priv->force_aspect_ratio = far;
200 }
201
202 bool
203 QtGLVideoItem::getForceAspectRatio()
204 {
205   return this->priv->force_aspect_ratio;
206 }
207
208 QSGNode *
209 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
210     UpdatePaintNodeData * updatePaintNodeData)
211 {
212   if (!m_openGlContextInitialized) {
213     return oldNode;
214   }
215
216   QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
217   GstVideoRectangle src, dst, result;
218   GstQSGTexture *tex;
219
220   g_mutex_lock (&this->priv->lock);
221   gst_gl_context_activate (this->priv->other_context, TRUE);
222
223   GST_TRACE ("%p updatePaintNode", this);
224
225   if (!this->priv->caps) {
226     g_mutex_unlock (&this->priv->lock);
227     return NULL;
228   }
229
230   if (!texNode) {
231     texNode = new QSGSimpleTextureNode ();
232     texNode->setOwnsTexture (true);
233     texNode->setTexture (new GstQSGTexture ());
234   }
235
236   tex = static_cast<GstQSGTexture *> (texNode->texture());
237   tex->setCaps (this->priv->caps);
238   tex->setBuffer (this->priv->buffer);
239   texNode->markDirty(QSGNode::DirtyMaterial);
240
241   if (this->priv->force_aspect_ratio) {
242     src.w = this->priv->display_width;
243     src.h = this->priv->display_height;
244
245     dst.x = boundingRect().x();
246     dst.y = boundingRect().y();
247     dst.w = boundingRect().width();
248     dst.h = boundingRect().height();
249
250     gst_video_sink_center_rect (src, dst, &result, TRUE);
251   } else {
252     result.x = boundingRect().x();
253     result.y = boundingRect().y();
254     result.w = boundingRect().width();
255     result.h = boundingRect().height();
256   }
257
258   texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
259
260   gst_gl_context_activate (this->priv->other_context, FALSE);
261   g_mutex_unlock (&this->priv->lock);
262
263   return texNode;
264 }
265
266 static void
267 _reset (QtGLVideoItem * qt_item)
268 {
269   gst_buffer_replace (&qt_item->priv->buffer, NULL);
270
271   gst_caps_replace (&qt_item->priv->caps, NULL);
272
273   qt_item->priv->negotiated = FALSE;
274   qt_item->priv->initted = FALSE;
275 }
276
277 void
278 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
279 {
280   g_return_if_fail (widget != NULL);
281   g_return_if_fail (widget->priv->negotiated);
282
283   g_mutex_lock (&widget->priv->lock);
284
285   gst_buffer_replace (&widget->priv->buffer, buffer);
286
287   QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
288
289   g_mutex_unlock (&widget->priv->lock);
290 }
291
292 void
293 QtGLVideoItem::onSceneGraphInitialized ()
294 {
295   GstGLPlatform platform;
296   GstGLAPI gl_api;
297   guintptr gl_handle;
298
299   GST_DEBUG ("scene graph initialization with Qt GL context %p",
300       this->window()->openglContext ());
301
302   if (this->priv->qt_context == this->window()->openglContext ())
303     return;
304
305   this->priv->qt_context = this->window()->openglContext ();
306   if (this->priv->qt_context == NULL) {
307     g_assert_not_reached ();
308     return;
309   }
310
311 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
312   if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
313     platform = GST_GL_PLATFORM_GLX;
314     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
315     gl_handle = gst_gl_context_get_current_gl_context (platform);
316     if (gl_handle)
317       this->priv->other_context =
318           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
319           platform, gl_api);
320   }
321 #endif
322 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
323   if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
324     platform = GST_GL_PLATFORM_EGL;
325     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
326     gl_handle = gst_gl_context_get_current_gl_context (platform);
327     if (gl_handle)
328       this->priv->other_context =
329           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
330           platform, gl_api);
331   }
332 #endif
333 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
334   if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
335     platform = GST_GL_PLATFORM_EGL;
336     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
337     gl_handle = gst_gl_context_get_current_gl_context (platform);
338     if (gl_handle)
339       this->priv->other_context =
340           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
341           platform, gl_api);
342   }
343 #endif
344 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
345   if (this->priv->display) {
346     platform = GST_GL_PLATFORM_CGL;
347     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
348     gl_handle = gst_gl_context_get_current_gl_context (platform);
349     if (gl_handle)
350       this->priv->other_context =
351           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
352           platform, gl_api);
353   }
354 #endif
355 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
356   if (this->priv->display) {
357     platform = GST_GL_PLATFORM_EAGL;
358     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
359     gl_handle = gst_gl_context_get_current_gl_context (platform);
360     if (gl_handle)
361       this->priv->other_context =
362           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
363           platform, gl_api);
364   }
365 #endif
366
367   (void) platform;
368   (void) gl_api;
369   (void) gl_handle;
370
371   if (this->priv->other_context) {
372     GError *error = NULL;
373
374     gst_gl_context_activate (this->priv->other_context, TRUE);
375     if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
376       GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
377       g_object_unref (this->priv->other_context);
378       this->priv->other_context = NULL;
379     } else {
380       gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
381       gst_gl_context_activate (this->priv->other_context, FALSE);
382       m_openGlContextInitialized = true;
383     }
384   }
385
386   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
387       this->priv->other_context);
388 }
389
390 void
391 QtGLVideoItem::onSceneGraphInvalidated ()
392 {
393   GST_FIXME ("%p scene graph invalidated", this);
394 }
395
396 gboolean
397 qt_item_init_winsys (QtGLVideoItem * widget)
398 {
399   g_return_val_if_fail (widget != NULL, FALSE);
400
401   g_mutex_lock (&widget->priv->lock);
402
403   if (widget->priv->display && widget->priv->qt_context
404       && widget->priv->other_context && widget->priv->context) {
405     /* already have the necessary state */
406     g_mutex_unlock (&widget->priv->lock);
407     return TRUE;
408   }
409
410   if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
411     GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
412         widget, widget->priv->display);
413     g_mutex_unlock (&widget->priv->lock);
414     return FALSE;
415   }
416
417   if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
418     GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
419         widget->priv->other_context);
420     g_mutex_unlock (&widget->priv->lock);
421     return FALSE;
422   }
423
424   widget->priv->context = gst_gl_context_new (widget->priv->display);
425
426   if (!widget->priv->context) {
427     g_mutex_unlock (&widget->priv->lock);
428     return FALSE;
429   }
430
431   gst_gl_context_create (widget->priv->context, widget->priv->other_context,
432       NULL);
433
434   g_mutex_unlock (&widget->priv->lock);
435   return TRUE;
436 }
437
438 void
439 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
440 {
441   if (win) {
442     if (win->isSceneGraphInitialized())
443       win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage);
444     else
445       connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
446
447     connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
448   } else {
449     this->priv->qt_context = NULL;
450   }
451 }
452
453 static gboolean
454 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
455 {
456   gboolean ok;
457   gint width, height;
458   gint par_n, par_d;
459   gint display_par_n, display_par_d;
460   guint display_ratio_num, display_ratio_den;
461
462   width = GST_VIDEO_INFO_WIDTH (info);
463   height = GST_VIDEO_INFO_HEIGHT (info);
464
465   par_n = GST_VIDEO_INFO_PAR_N (info);
466   par_d = GST_VIDEO_INFO_PAR_D (info);
467
468   if (!par_n)
469     par_n = 1;
470
471   /* get display's PAR */
472   if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
473     display_par_n = widget->priv->par_n;
474     display_par_d = widget->priv->par_d;
475   } else {
476     display_par_n = 1;
477     display_par_d = 1;
478   }
479
480   ok = gst_video_calculate_display_ratio (&display_ratio_num,
481       &display_ratio_den, width, height, par_n, par_d, display_par_n,
482       display_par_d);
483
484   if (!ok)
485     return FALSE;
486
487   GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
488
489   if (height % display_ratio_den == 0) {
490     GST_DEBUG ("keeping video height");
491     widget->priv->display_width = (guint)
492         gst_util_uint64_scale_int (height, display_ratio_num,
493         display_ratio_den);
494     widget->priv->display_height = height;
495   } else if (width % display_ratio_num == 0) {
496     GST_DEBUG ("keeping video width");
497     widget->priv->display_width = width;
498     widget->priv->display_height = (guint)
499         gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
500   } else {
501     GST_DEBUG ("approximating while keeping video height");
502     widget->priv->display_width = (guint)
503         gst_util_uint64_scale_int (height, display_ratio_num,
504         display_ratio_den);
505     widget->priv->display_height = height;
506   }
507   GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
508       widget->priv->display_height);
509
510   return TRUE;
511 }
512
513 gboolean
514 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
515 {
516   GstVideoInfo v_info;
517
518   g_return_val_if_fail (widget != NULL, FALSE);
519   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
520   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
521
522   if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
523     return TRUE;
524
525   if (!gst_video_info_from_caps (&v_info, caps))
526     return FALSE;
527
528   g_mutex_lock (&widget->priv->lock);
529
530   _reset (widget);
531
532   gst_caps_replace (&widget->priv->caps, caps);
533
534   if (!_calculate_par (widget, &v_info)) {
535     g_mutex_unlock (&widget->priv->lock);
536     return FALSE;
537   }
538
539   widget->priv->v_info = v_info;
540   widget->priv->negotiated = TRUE;
541
542   g_mutex_unlock (&widget->priv->lock);
543
544   return TRUE;
545 }
546
547 GstGLContext *
548 qt_item_get_qt_context (QtGLVideoItem * qt_item)
549 {
550   g_return_val_if_fail (qt_item != NULL, NULL);
551
552   if (!qt_item->priv->other_context)
553     return NULL;
554
555   return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
556 }
557
558 GstGLContext *
559 qt_item_get_context (QtGLVideoItem * qt_item)
560 {
561   g_return_val_if_fail (qt_item != NULL, NULL);
562
563   if (!qt_item->priv->context)
564     return NULL;
565
566   return (GstGLContext *) gst_object_ref (qt_item->priv->context);
567 }
568
569 GstGLDisplay *
570 qt_item_get_display (QtGLVideoItem * qt_item)
571 {
572   g_return_val_if_fail (qt_item != NULL, NULL);
573
574   if (!qt_item->priv->display)
575     return NULL;
576
577   return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);
578 }