Commit 7a25fb5b authored by Matthew Waters's avatar Matthew Waters 🐨

qt: add a qml overlay filter element [part 2]

It takes a qml scene description and renders it using a possible input
stream.

Currently supported on GLX and WGL.

Follow up to (as that MR had an old version of the commit):
- gstreamer/gst-plugins-good!475
- 4778d716: qt: add a qml overlay filter element
parent 4778d716
......@@ -251,7 +251,7 @@ gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
gst_gl_context_activate(*wrap_glcontext, TRUE);
if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {
GST_ERROR ("failed to retrieve qt context info: %s", error->message);
g_object_unref (*wrap_glcontext);
gst_object_unref (*wrap_glcontext);
*wrap_glcontext = NULL;
return FALSE;
} else {
......@@ -282,9 +282,9 @@ gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
gst_object_unref (window);
if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) {
GST_ERROR ("failed to create shared GL context: %s", error->message);
g_object_unref (*context);
gst_object_unref (*context);
*context = NULL;
g_object_unref (*wrap_glcontext);
gst_object_unref (*wrap_glcontext);
*wrap_glcontext = NULL;
wglMakeCurrent (device, (HGLRC) gl_handle);
return FALSE;
......@@ -317,18 +317,26 @@ qt_opengl_native_context_from_gst_gl_context (GstGLContext * context)
#endif
#if GST_GL_HAVE_PLATFORM_EGL
if (platform == GST_GL_PLATFORM_EGL) {
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
EGLDisplay egl_display = EGL_DEFAULT_DISPLAY;
GstGLDisplay *display = gst_gl_context_get_display (context);
GstGLDisplayEGL *display_egl = gst_gl_display_egl_from_gl_display (display);
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
if (gst_gl_display_get_handle_type (display) == GST_GL_DISPLAY_TYPE_WAYLAND) {
GstGLDisplayEGL *display_egl = gst_gl_display_egl_from_gl_display (display);
if (display_egl) {
EGLDisplay egl_display = (EGLDisplay) gst_gl_display_get_handle ((GstGLDisplay *) display_egl);
gst_object_unref (display_egl);
gst_object_unref (display);
return QVariant::fromValue(QEGLNativeContext((EGLContext) handle, egl_display));
}
#if 1
g_warning ("Qt does not support wrapping native OpenGL contexts "
"on wayland. See https://bugreports.qt.io/browse/QTBUG-82528");
gst_object_unref (display_egl);
gst_object_unref (display);
return QVariant::fromValue(nullptr);
#else
if (display_egl)
egl_display = (EGLDisplay) gst_gl_display_get_handle ((GstGLDisplay *) display_egl);
#endif
}
#endif
gst_object_unref (display_egl);
gst_object_unref (display);
return QVariant::fromValue(QEGLNativeContext((EGLContext) handle, egl_display));
}
#endif
#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
......
......@@ -238,6 +238,9 @@ gst_qt_overlay_gl_stop (GstGLBaseFilter * bfilter)
{
GstQtOverlay *qt_overlay = GST_QT_OVERLAY (bfilter);
if (qt_overlay->widget)
qt_overlay->widget->setBuffer (NULL);
if (qt_overlay->renderer) {
qt_overlay->renderer->cleanup();
delete qt_overlay->renderer;
......@@ -272,6 +275,7 @@ gst_qt_overlay_prepare_output_buffer (GstBaseTransform * btrans,
GstGLFilter *filter = GST_GL_FILTER (btrans);
GstQtOverlay *qt_overlay = GST_QT_OVERLAY (btrans);
GstGLMemory *out_mem;
GstGLSyncMeta *sync_meta;
if (qt_overlay->widget) {
qt_overlay->widget->setCaps (bfilter->in_caps);
......@@ -292,6 +296,9 @@ gst_qt_overlay_prepare_output_buffer (GstBaseTransform * btrans,
GST_VIDEO_INFO_WIDTH (&filter->in_info),
GST_VIDEO_INFO_HEIGHT (&filter->out_info));
sync_meta = gst_buffer_add_gl_sync_meta (bfilter->context, *outbuf);
gst_gl_sync_meta_set_sync_point (sync_meta, bfilter->context);
bclass->copy_metadata (btrans, buffer, *outbuf);
return GST_FLOW_OK;
......
This diff is collapsed.
......@@ -80,8 +80,8 @@ private:
static void initialize_gst_gl_c (GstGLContext * context, GstQuickRenderer * self) { self->initializeGstGL (); }
void initializeGstGL ();
static void stop_c (GstGLContext * context, GstQuickRenderer * self) { self->stop (); }
void stop ();
static void stop_c (GstGLContext * context, GstQuickRenderer * self) { self->stopGL (); }
void stopGL ();
static void activate_context_c (GstGLContext * context, GstQuickRenderer * self) { self->activateContext (); }
void activateContext ();
......@@ -90,16 +90,12 @@ private:
void deactivateContext ();
GstGLContext *gl_context;
QOpenGLContext *m_context;
QThread *m_renderThread;
GstBackingSurface *m_surface;
QOpenGLFramebufferObject *m_fbo;
QQuickWindow *m_quickWindow;
QQuickRenderControl *m_renderControl;
QQmlEngine *m_qmlEngine;
QQmlComponent *m_qmlComponent;
QQuickItem *m_rootItem;
GstAnimationDriver *m_animationDriver;
GstGLBaseMemoryAllocator *gl_allocator;
GstGLAllocationParams *gl_params;
......@@ -107,6 +103,7 @@ private:
GstGLMemory *gl_mem;
QString m_errorString;
struct SharedRenderData *m_sharedRenderData;
};
#endif /* __QT_QUICK_RENDER_H__ */
......@@ -135,7 +135,7 @@ QtGLVideoItem::~QtGLVideoItem()
* no qmlglsink's will call in again, and that
* any ongoing calls are done by invalidating the proxy
* pointer */
GST_INFO ("Destroying QtGLVideoItem and invalidating the proxy");
GST_INFO ("%p Destroying QtGLVideoItem and invalidating the proxy %p", this, proxy.get());
proxy->invalidateRef();
proxy.clear();
......@@ -147,6 +147,8 @@ QtGLVideoItem::~QtGLVideoItem()
if (this->priv->display)
gst_object_unref(this->priv->display);
gst_buffer_replace (&this->priv->buffer, NULL);
gst_caps_replace (&this->priv->caps, NULL);
g_free (this->priv);
this->priv = NULL;
......@@ -261,11 +263,13 @@ QtGLVideoItemInterface::setBuffer (GstBuffer * buffer)
{
QMutexLocker locker(&lock);
if (qt_item == NULL)
if (qt_item == NULL) {
GST_WARNING ("%p actual item is NULL. setBuffer call ignored", this);
return;
}
if (!qt_item->priv->negotiated) {
GST_WARNING ("Got buffer on unnegotiated QtGLVideoItem. Dropping");
GST_WARNING ("%p Got buffer on unnegotiated QtGLVideoItem. Dropping", this);
return;
}
......@@ -281,7 +285,7 @@ QtGLVideoItemInterface::setBuffer (GstBuffer * buffer)
void
QtGLVideoItem::onSceneGraphInitialized ()
{
GST_DEBUG ("scene graph initialization with Qt GL context %p",
GST_DEBUG ("%p scene graph initialization with Qt GL context %p", this,
this->window()->openglContext ());
if (this->priv->qt_context == this->window()->openglContext ())
......@@ -308,6 +312,10 @@ QtGLVideoItem::onSceneGraphInvalidated ()
GST_FIXME ("%p scene graph invalidated", this);
}
/**
* Retrieve and populate the GL context information from the current
* OpenGL context.
*/
gboolean
QtGLVideoItemInterface::initWinSys ()
{
......@@ -408,27 +416,28 @@ _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
if (!ok)
return FALSE;
GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
GST_LOG ("%p PAR: %u/%u DAR:%u/%u", widget, par_n, par_d, display_par_n,
display_par_d);
if (height % display_ratio_den == 0) {
GST_DEBUG ("keeping video height");
GST_DEBUG ("%p keeping video height", widget);
widget->priv->display_width = (guint)
gst_util_uint64_scale_int (height, display_ratio_num,
display_ratio_den);
widget->priv->display_height = height;
} else if (width % display_ratio_num == 0) {
GST_DEBUG ("keeping video width");
GST_DEBUG ("%p keeping video width", widget);
widget->priv->display_width = width;
widget->priv->display_height = (guint)
gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
} else {
GST_DEBUG ("approximating while keeping video height");
GST_DEBUG ("%p approximating while keeping video height", widget);
widget->priv->display_width = (guint)
gst_util_uint64_scale_int (height, display_ratio_num,
display_ratio_den);
widget->priv->display_height = height;
}
GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
GST_DEBUG ("%p scaling to %dx%d", widget, widget->priv->display_width,
widget->priv->display_height);
return TRUE;
......
......@@ -60,12 +60,13 @@ int main(int argc, char *argv[])
/* the plugin must be loaded before loading the qml file to register the
* GstGLVideoItem qml item */
GstElement *overlay = gst_element_factory_make ("qmlgloverlay", NULL);
GstElement *overlay2 = gst_element_factory_make ("qmlgloverlay", NULL);
GstElement *sink = gst_element_factory_make ("qmlglsink", NULL);
g_assert (src && glupload && overlay && sink);
gst_bin_add_many (GST_BIN (pipeline), src, glupload, overlay, sink, NULL);
gst_element_link_many (src, glupload, overlay, sink, NULL);
gst_bin_add_many (GST_BIN (pipeline), src, glupload, overlay, overlay2, sink, NULL);
gst_element_link_many (src, glupload, overlay, overlay2, sink, NULL);
/* load qmlglsink output */
QQmlApplicationEngine engine;
......@@ -93,10 +94,21 @@ int main(int argc, char *argv[])
QByteArray overlay_scene = f.readAll();
qDebug() << overlay_scene;
QFile f2(":/overlay2.qml");
if(!f2.open(QIODevice::ReadOnly)) {
qWarning() << "error: " << f2.errorString();
return 1;
}
QByteArray overlay_scene2 = f2.readAll();
qDebug() << overlay_scene2;
/* load qmlgloverlay contents */
g_signal_connect (overlay, "qml-scene-initialized", G_CALLBACK (on_overlay_scene_initialized), NULL);
g_object_set (overlay, "qml-scene", overlay_scene.data(), NULL);
g_signal_connect (overlay2, "qml-scene-initialized", G_CALLBACK (on_overlay_scene_initialized), NULL);
g_object_set (overlay2, "qml-scene", overlay_scene2.data(), NULL);
rootObject->scheduleRenderJob (new SetPlaying (pipeline),
QQuickWindow::BeforeSynchronizingStage);
......
import QtQuick 2.4
import org.freedesktop.gstreamer.GLVideoItem 1.0
Item {
/* render upside down for GStreamer */
transform: Scale { origin.x : 0; origin.y : height / 2.; yScale : -1 }
GstGLVideoItem {
id: video
objectName: "inputVideoItem"
anchors.centerIn: parent
width: parent.width
height: parent.height
}
Text {
id: rotatingText
anchors.centerIn: parent
text: "Qt Quick\nrendered to\na texture"
font.pointSize: 20
color: "black"
style: Text.Outline
styleColor: "white"
RotationAnimator {
target: rotatingText;
from: 0;
to: 360;
duration: 10000
running: true
loops: Animation.Infinite
}
}
Text {
property int elapsedTime: 0
id: time
anchors.bottom: rotatingText.top
anchors.horizontalCenter: rotatingText.horizontalCenter
font.pointSize: 12
style: Text.Outline
styleColor: "red"
color: "black"
Timer {
interval: 1000
running: true
repeat: true
onTriggered: {
parent.elapsedTime += interval / 1000
parent.text = "overlay: " + parent.elapsedTime.toString() + " seconds"
}
}
}
}
......@@ -2,5 +2,6 @@
<qresource prefix="/">
<file>main.qml</file>
<file>overlay.qml</file>
<file>overlay2.qml</file>
</qresource>
</RCC>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment