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