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