qml: Mark material dirty when texture buffer is updated
[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     texNode->setTexture (new GstQSGTexture ());
209   }
210
211   tex = static_cast<GstQSGTexture *> (texNode->texture());
212   tex->setCaps (this->priv->caps);
213   tex->setBuffer (this->priv->buffer);
214   texNode->markDirty(QSGNode::DirtyMaterial);
215
216   if (this->priv->force_aspect_ratio) {
217     src.w = this->priv->display_width;
218     src.h = this->priv->display_height;
219
220     dst.x = boundingRect().x();
221     dst.y = boundingRect().y();
222     dst.w = boundingRect().width();
223     dst.h = boundingRect().height();
224
225     gst_video_sink_center_rect (src, dst, &result, TRUE);
226   } else {
227     result.x = boundingRect().x();
228     result.y = boundingRect().y();
229     result.w = boundingRect().width();
230     result.h = boundingRect().height();
231   }
232
233   texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
234
235   gst_gl_context_activate (this->priv->other_context, FALSE);
236   g_mutex_unlock (&this->priv->lock);
237
238   return texNode;
239 }
240
241 static void
242 _reset (QtGLVideoItem * qt_item)
243 {
244   gst_buffer_replace (&qt_item->priv->buffer, NULL);
245
246   gst_caps_replace (&qt_item->priv->caps, NULL);
247
248   qt_item->priv->negotiated = FALSE;
249   qt_item->priv->initted = FALSE;
250 }
251
252 void
253 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
254 {
255   g_return_if_fail (widget != NULL);
256   g_return_if_fail (widget->priv->negotiated);
257
258   g_mutex_lock (&widget->priv->lock);
259
260   gst_buffer_replace (&widget->priv->buffer, buffer);
261
262   QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
263
264   g_mutex_unlock (&widget->priv->lock);
265 }
266
267 void
268 QtGLVideoItem::onSceneGraphInitialized ()
269 {
270   GstGLPlatform platform;
271   GstGLAPI gl_api;
272   guintptr gl_handle;
273
274   GST_DEBUG ("scene graph initialization with Qt GL context %p",
275       this->window()->openglContext ());
276
277   if (this->priv->qt_context == this->window()->openglContext ())
278     return;
279
280   this->priv->qt_context = this->window()->openglContext ();
281   if (this->priv->qt_context == NULL) {
282     g_assert_not_reached ();
283     return;
284   }
285
286 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
287   if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
288     platform = GST_GL_PLATFORM_GLX;
289     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
290     gl_handle = gst_gl_context_get_current_gl_context (platform);
291     if (gl_handle)
292       this->priv->other_context =
293           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
294           platform, gl_api);
295   }
296 #endif
297 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
298   if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
299     platform = GST_GL_PLATFORM_EGL;
300     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
301     gl_handle = gst_gl_context_get_current_gl_context (platform);
302     if (gl_handle)
303       this->priv->other_context =
304           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
305           platform, gl_api);
306   }
307 #endif
308 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
309   if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
310     platform = GST_GL_PLATFORM_EGL;
311     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
312     gl_handle = gst_gl_context_get_current_gl_context (platform);
313     if (gl_handle)
314       this->priv->other_context =
315           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
316           platform, gl_api);
317   }
318 #endif
319 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
320   if (this->priv->display) {
321     platform = GST_GL_PLATFORM_CGL;
322     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
323     gl_handle = gst_gl_context_get_current_gl_context (platform);
324     if (gl_handle)
325       this->priv->other_context =
326           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
327           platform, gl_api);
328   }
329 #endif
330 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
331   if (this->priv->display) {
332     platform = GST_GL_PLATFORM_EAGL;
333     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
334     gl_handle = gst_gl_context_get_current_gl_context (platform);
335     if (gl_handle)
336       this->priv->other_context =
337           gst_gl_context_new_wrapped (this->priv->display, gl_handle,
338           platform, gl_api);
339   }
340 #endif
341
342   (void) platform;
343   (void) gl_api;
344   (void) gl_handle;
345
346   if (this->priv->other_context) {
347     GError *error = NULL;
348
349     gst_gl_context_activate (this->priv->other_context, TRUE);
350     if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
351       GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
352       g_object_unref (this->priv->other_context);
353       this->priv->other_context = NULL;
354     } else {
355       gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
356       gst_gl_context_activate (this->priv->other_context, FALSE);
357     }
358   }
359
360   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
361       this->priv->other_context);
362 }
363
364 void
365 QtGLVideoItem::onSceneGraphInvalidated ()
366 {
367   GST_FIXME ("%p scene graph invalidated", this);
368 }
369
370 gboolean
371 qt_item_init_winsys (QtGLVideoItem * widget)
372 {
373   g_return_val_if_fail (widget != NULL, FALSE);
374
375   g_mutex_lock (&widget->priv->lock);
376
377   if (widget->priv->display && widget->priv->qt_context
378       && widget->priv->other_context && widget->priv->context) {
379     /* already have the necessary state */
380     g_mutex_unlock (&widget->priv->lock);
381     return TRUE;
382   }
383
384   if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
385     GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
386         widget, widget->priv->display);
387     g_mutex_unlock (&widget->priv->lock);
388     return FALSE;
389   }
390
391   if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
392     GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
393         widget->priv->other_context);
394     g_mutex_unlock (&widget->priv->lock);
395     return FALSE;
396   }
397
398   widget->priv->context = gst_gl_context_new (widget->priv->display);
399
400   if (!widget->priv->context) {
401     g_mutex_unlock (&widget->priv->lock);
402     return FALSE;
403   }
404
405   gst_gl_context_create (widget->priv->context, widget->priv->other_context,
406       NULL);
407
408   g_mutex_unlock (&widget->priv->lock);
409   return TRUE;
410 }
411
412 void
413 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
414 {
415   if (win) {
416     if (win->isSceneGraphInitialized())
417       onSceneGraphInitialized();
418     else
419           connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
420
421     connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
422   } else {
423     this->priv->qt_context = NULL;
424   }
425 }
426
427 static gboolean
428 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
429 {
430   gboolean ok;
431   gint width, height;
432   gint par_n, par_d;
433   gint display_par_n, display_par_d;
434   guint display_ratio_num, display_ratio_den;
435
436   width = GST_VIDEO_INFO_WIDTH (info);
437   height = GST_VIDEO_INFO_HEIGHT (info);
438
439   par_n = GST_VIDEO_INFO_PAR_N (info);
440   par_d = GST_VIDEO_INFO_PAR_D (info);
441
442   if (!par_n)
443     par_n = 1;
444
445   /* get display's PAR */
446   if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
447     display_par_n = widget->priv->par_n;
448     display_par_d = widget->priv->par_d;
449   } else {
450     display_par_n = 1;
451     display_par_d = 1;
452   }
453
454   ok = gst_video_calculate_display_ratio (&display_ratio_num,
455       &display_ratio_den, width, height, par_n, par_d, display_par_n,
456       display_par_d);
457
458   if (!ok)
459     return FALSE;
460
461   GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
462
463   if (height % display_ratio_den == 0) {
464     GST_DEBUG ("keeping video height");
465     widget->priv->display_width = (guint)
466         gst_util_uint64_scale_int (height, display_ratio_num,
467         display_ratio_den);
468     widget->priv->display_height = height;
469   } else if (width % display_ratio_num == 0) {
470     GST_DEBUG ("keeping video width");
471     widget->priv->display_width = width;
472     widget->priv->display_height = (guint)
473         gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
474   } else {
475     GST_DEBUG ("approximating while keeping video height");
476     widget->priv->display_width = (guint)
477         gst_util_uint64_scale_int (height, display_ratio_num,
478         display_ratio_den);
479     widget->priv->display_height = height;
480   }
481   GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
482       widget->priv->display_height);
483
484   return TRUE;
485 }
486
487 gboolean
488 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
489 {
490   GstVideoInfo v_info;
491
492   g_return_val_if_fail (widget != NULL, FALSE);
493   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
494   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
495
496   if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
497     return TRUE;
498
499   if (!gst_video_info_from_caps (&v_info, caps))
500     return FALSE;
501
502   g_mutex_lock (&widget->priv->lock);
503
504   _reset (widget);
505
506   gst_caps_replace (&widget->priv->caps, caps);
507
508   if (!_calculate_par (widget, &v_info)) {
509     g_mutex_unlock (&widget->priv->lock);
510     return FALSE;
511   }
512
513   widget->priv->v_info = v_info;
514   widget->priv->negotiated = TRUE;
515
516   g_mutex_unlock (&widget->priv->lock);
517
518   return TRUE;
519 }
520
521 GstGLContext *
522 qt_item_get_qt_context (QtGLVideoItem * qt_item)
523 {
524   g_return_val_if_fail (qt_item != NULL, NULL);
525
526   if (!qt_item->priv->other_context)
527     return NULL;
528
529   return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
530 }
531
532 GstGLContext *
533 qt_item_get_context (QtGLVideoItem * qt_item)
534 {
535   g_return_val_if_fail (qt_item != NULL, NULL);
536
537   if (!qt_item->priv->context)
538     return NULL;
539
540   return (GstGLContext *) gst_object_ref (qt_item->priv->context);
541 }
542
543 GstGLDisplay *
544 qt_item_get_display (QtGLVideoItem * qt_item)
545 {
546   g_return_val_if_fail (qt_item != NULL, NULL);
547
548   if (!qt_item->priv->display)
549     return NULL;
550
551   return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);
552 }