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