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