qmlglsink: add win32 support
[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_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
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_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
166   if (QString::fromUtf8 ("android") == app->platformName())
167     this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
168 #elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
169   if (QString::fromUtf8("eglfs") == app->platformName())
170     this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
171 #endif
172
173 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
174   if (QString::fromUtf8 ("cocoa") == app->platformName())
175     this->priv->display = (GstGLDisplay *) gst_gl_display_cocoa_new ();
176 #endif
177 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
178   if (QString::fromUtf8 ("ios") == app->platformName())
179     this->priv->display = gst_gl_display_new ();
180 #endif
181 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
182   if (QString::fromUtf8 ("windows") == app->platformName())
183     this->priv->display = gst_gl_display_new ();
184 #endif
185
186   if (!this->priv->display)
187     this->priv->display = gst_gl_display_new ();
188
189   connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
190           SLOT(handleWindowChanged(QQuickWindow*)));
191
192   GST_DEBUG ("%p init Qt Video Item", this);
193 }
194
195 QtGLVideoItem::~QtGLVideoItem()
196 {
197   g_mutex_clear (&this->priv->lock);
198   if (this->priv->context)
199     gst_object_unref(this->priv->context);
200   if (this->priv->other_context)
201     gst_object_unref(this->priv->other_context);
202   g_free (this->priv);
203   this->priv = NULL;
204 }
205
206 void
207 QtGLVideoItem::setDAR(gint num, gint den)
208 {
209   this->priv->par_n = num;
210   this->priv->par_d = den;
211 }
212
213 void
214 QtGLVideoItem::getDAR(gint * num, gint * den)
215 {
216   if (num)
217     *num = this->priv->par_n;
218   if (den)
219     *den = this->priv->par_d;
220 }
221
222 void
223 QtGLVideoItem::setForceAspectRatio(bool force_aspect_ratio)
224 {
225   this->priv->force_aspect_ratio = !!force_aspect_ratio;
226 }
227
228 bool
229 QtGLVideoItem::getForceAspectRatio()
230 {
231   return this->priv->force_aspect_ratio;
232 }
233
234 QSGNode *
235 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
236     UpdatePaintNodeData * updatePaintNodeData)
237 {
238   if (!m_openGlContextInitialized) {
239     return oldNode;
240   }
241
242   QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
243   GstVideoRectangle src, dst, result;
244   GstQSGTexture *tex;
245
246   g_mutex_lock (&this->priv->lock);
247   gst_gl_context_activate (this->priv->other_context, TRUE);
248
249   GST_TRACE ("%p updatePaintNode", this);
250
251   if (!this->priv->caps) {
252     g_mutex_unlock (&this->priv->lock);
253     return NULL;
254   }
255
256   if (!texNode) {
257     texNode = new QSGSimpleTextureNode ();
258     texNode->setOwnsTexture (true);
259     texNode->setTexture (new GstQSGTexture ());
260   }
261
262   tex = static_cast<GstQSGTexture *> (texNode->texture());
263   tex->setCaps (this->priv->caps);
264   tex->setBuffer (this->priv->buffer);
265   texNode->markDirty(QSGNode::DirtyMaterial);
266
267   if (this->priv->force_aspect_ratio) {
268     src.w = this->priv->display_width;
269     src.h = this->priv->display_height;
270
271     dst.x = boundingRect().x();
272     dst.y = boundingRect().y();
273     dst.w = boundingRect().width();
274     dst.h = boundingRect().height();
275
276     gst_video_sink_center_rect (src, dst, &result, TRUE);
277   } else {
278     result.x = boundingRect().x();
279     result.y = boundingRect().y();
280     result.w = boundingRect().width();
281     result.h = boundingRect().height();
282   }
283
284   texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
285
286   gst_gl_context_activate (this->priv->other_context, FALSE);
287   g_mutex_unlock (&this->priv->lock);
288
289   return texNode;
290 }
291
292 static void
293 _reset (QtGLVideoItem * qt_item)
294 {
295   gst_buffer_replace (&qt_item->priv->buffer, NULL);
296
297   gst_caps_replace (&qt_item->priv->caps, NULL);
298
299   qt_item->priv->negotiated = FALSE;
300   qt_item->priv->initted = FALSE;
301 }
302
303 void
304 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
305 {
306   g_return_if_fail (widget != NULL);
307   g_return_if_fail (widget->priv->negotiated);
308
309   g_mutex_lock (&widget->priv->lock);
310
311   gst_buffer_replace (&widget->priv->buffer, buffer);
312
313   QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
314
315   g_mutex_unlock (&widget->priv->lock);
316 }
317
318 void
319 QtGLVideoItem::onSceneGraphInitialized ()
320 {
321   GstGLPlatform platform = (GstGLPlatform) 0;
322   GstGLAPI gl_api;
323   guintptr gl_handle;
324
325   GST_DEBUG ("scene graph initialization with Qt GL context %p",
326       this->window()->openglContext ());
327
328   if (this->priv->qt_context == this->window()->openglContext ())
329     return;
330
331   this->priv->qt_context = this->window()->openglContext ();
332   if (this->priv->qt_context == NULL) {
333     g_assert_not_reached ();
334     return;
335   }
336
337 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
338   if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
339     platform = GST_GL_PLATFORM_GLX;
340   }
341 #endif
342 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
343   if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
344     platform = GST_GL_PLATFORM_EGL;
345   }
346 #endif
347 #if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
348   if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
349     platform = GST_GL_PLATFORM_EGL;
350   }
351 #endif
352   if (platform == 0 && this->priv->display) {
353 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
354     platform = GST_GL_PLATFORM_CGL;
355 #elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
356     platform = GST_GL_PLATFORM_EAGL;
357 #elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
358     platform = GST_GL_PLATFORM_WGL;
359 #else
360     GST_ERROR ("Unknown platform");
361     return;
362 #endif
363   }
364
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   (void) platform;
373   (void) gl_api;
374   (void) gl_handle;
375
376   if (this->priv->other_context) {
377     GError *error = NULL;
378
379     gst_gl_context_activate (this->priv->other_context, TRUE);
380     if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
381       GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
382       g_object_unref (this->priv->other_context);
383       this->priv->other_context = NULL;
384     } else {
385       gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
386 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
387       if (!wglGetProcAddress ("wglCreateContextAttribsARB")) {
388         GstGLWindow *window;
389         HDC device;
390
391         /* If there's no wglCreateContextAttribsARB() support, then we would fallback to
392          * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL
393          * contexts are current in any other thread.
394          *
395          * The workaround here is to temporarily disable Qt's GL context while we
396          * set up our own.
397          */
398         this->priv->context = gst_gl_context_new (this->priv->display);
399         window = gst_gl_context_get_window (this->priv->context);
400         device = (HDC) gst_gl_window_get_display (window);
401
402         wglMakeCurrent (device, 0);
403         gst_object_unref (window);
404         if (!gst_gl_context_create (this->priv->context, this->priv->other_context, &error)) {
405           GST_ERROR ("%p failed to create shared GL context: %s", this, error->message);
406           g_object_unref (this->priv->context);
407           this->priv->context = NULL;
408           g_object_unref (this->priv->other_context);
409           this->priv->other_context = NULL;
410           wglMakeCurrent (device, (HGLRC) gl_handle);
411           return;
412         }
413         wglMakeCurrent (device, (HGLRC) gl_handle);
414       }
415 #endif
416       gst_gl_context_activate (this->priv->other_context, FALSE);
417       m_openGlContextInitialized = true;
418     }
419   }
420
421   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
422       this->priv->other_context);
423 }
424
425 void
426 QtGLVideoItem::onSceneGraphInvalidated ()
427 {
428   GST_FIXME ("%p scene graph invalidated", this);
429 }
430
431 gboolean
432 qt_item_init_winsys (QtGLVideoItem * widget)
433 {
434   GError *error = NULL;
435
436   g_return_val_if_fail (widget != NULL, FALSE);
437
438   g_mutex_lock (&widget->priv->lock);
439
440   if (widget->priv->display && widget->priv->qt_context
441       && widget->priv->other_context && widget->priv->context) {
442     /* already have the necessary state */
443     g_mutex_unlock (&widget->priv->lock);
444     return TRUE;
445   }
446
447   if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
448     GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
449         widget, widget->priv->display);
450     g_mutex_unlock (&widget->priv->lock);
451     return FALSE;
452   }
453
454   if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
455     GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
456         widget->priv->other_context);
457     g_mutex_unlock (&widget->priv->lock);
458     return FALSE;
459   }
460
461   widget->priv->context = gst_gl_context_new (widget->priv->display);
462
463   if (!widget->priv->context) {
464     g_mutex_unlock (&widget->priv->lock);
465     return FALSE;
466   }
467
468   if (!gst_gl_context_create (widget->priv->context, widget->priv->other_context,
469         &error)) {
470     GST_ERROR ("%s", error->message);
471     g_mutex_unlock (&widget->priv->lock);
472     return FALSE;
473   }
474
475   g_mutex_unlock (&widget->priv->lock);
476   return TRUE;
477 }
478
479 void
480 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
481 {
482   if (win) {
483     if (win->isSceneGraphInitialized())
484       win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage);
485     else
486       connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
487
488     connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
489   } else {
490     this->priv->qt_context = NULL;
491   }
492 }
493
494 static gboolean
495 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
496 {
497   gboolean ok;
498   gint width, height;
499   gint par_n, par_d;
500   gint display_par_n, display_par_d;
501   guint display_ratio_num, display_ratio_den;
502
503   width = GST_VIDEO_INFO_WIDTH (info);
504   height = GST_VIDEO_INFO_HEIGHT (info);
505
506   par_n = GST_VIDEO_INFO_PAR_N (info);
507   par_d = GST_VIDEO_INFO_PAR_D (info);
508
509   if (!par_n)
510     par_n = 1;
511
512   /* get display's PAR */
513   if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
514     display_par_n = widget->priv->par_n;
515     display_par_d = widget->priv->par_d;
516   } else {
517     display_par_n = 1;
518     display_par_d = 1;
519   }
520
521   ok = gst_video_calculate_display_ratio (&display_ratio_num,
522       &display_ratio_den, width, height, par_n, par_d, display_par_n,
523       display_par_d);
524
525   if (!ok)
526     return FALSE;
527
528   GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
529
530   if (height % display_ratio_den == 0) {
531     GST_DEBUG ("keeping video height");
532     widget->priv->display_width = (guint)
533         gst_util_uint64_scale_int (height, display_ratio_num,
534         display_ratio_den);
535     widget->priv->display_height = height;
536   } else if (width % display_ratio_num == 0) {
537     GST_DEBUG ("keeping video width");
538     widget->priv->display_width = width;
539     widget->priv->display_height = (guint)
540         gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
541   } else {
542     GST_DEBUG ("approximating while keeping video height");
543     widget->priv->display_width = (guint)
544         gst_util_uint64_scale_int (height, display_ratio_num,
545         display_ratio_den);
546     widget->priv->display_height = height;
547   }
548   GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
549       widget->priv->display_height);
550
551   return TRUE;
552 }
553
554 gboolean
555 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
556 {
557   GstVideoInfo v_info;
558
559   g_return_val_if_fail (widget != NULL, FALSE);
560   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
561   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
562
563   if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
564     return TRUE;
565
566   if (!gst_video_info_from_caps (&v_info, caps))
567     return FALSE;
568
569   g_mutex_lock (&widget->priv->lock);
570
571   _reset (widget);
572
573   gst_caps_replace (&widget->priv->caps, caps);
574
575   if (!_calculate_par (widget, &v_info)) {
576     g_mutex_unlock (&widget->priv->lock);
577     return FALSE;
578   }
579
580   widget->priv->v_info = v_info;
581   widget->priv->negotiated = TRUE;
582
583   g_mutex_unlock (&widget->priv->lock);
584
585   return TRUE;
586 }
587
588 GstGLContext *
589 qt_item_get_qt_context (QtGLVideoItem * qt_item)
590 {
591   g_return_val_if_fail (qt_item != NULL, NULL);
592
593   if (!qt_item->priv->other_context)
594     return NULL;
595
596   return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
597 }
598
599 GstGLContext *
600 qt_item_get_context (QtGLVideoItem * qt_item)
601 {
602   g_return_val_if_fail (qt_item != NULL, NULL);
603
604   if (!qt_item->priv->context)
605     return NULL;
606
607   return (GstGLContext *) gst_object_ref (qt_item->priv->context);
608 }
609
610 GstGLDisplay *
611 qt_item_get_display (QtGLVideoItem * qt_item)
612 {
613   g_return_val_if_fail (qt_item != NULL, NULL);
614
615   if (!qt_item->priv->display)
616     return NULL;
617
618   return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);
619 }