Commit 6c75e6c5 authored by Tim-Philipp Müller's avatar Tim-Philipp Müller 🐠
parents 16a70b58 93cf3ef9
plugin_LTLIBRARIES = libgstqmlgl.la
noinst_HEADERS = \
gstqsgtexture.h \
gstqtglutility.h \
gstqtsink.h \
gstqtsrc.h \
qtitem.h \
qtwindow.h
moc_generated = \
moc_qtitem.cc \
moc_qtwindow.cc \
moc_gstqsgtexture.cc
#anything generated by the Qt tools...
BUILT_SOURCES = $(moc_generated)
CLEANFILES = $(moc_generated)
nodist_libgstqmlgl_la_SOURCES = $(BUILT_SOURCES)
libgstqmlgl_la_SOURCES = \
gstqsgtexture.cc \
gstqtglutility.cc \
qtitem.cc \
qtwindow.cc \
gstqtsink.cc \
gstqtsink.h \
gstqtsrc.cc \
gstqtsrc.h \
gstqtgl.h \
gstplugin.cc
libgstqmlgl_la_CXXFLAGS = \
$(GST_PLUGINS_BAD_CFLAGS) \
$(GST_PLUGINS_BASE_CFLAGS) \
$(GST_GL_CFLAGS) \
$(GST_BASE_CFLAGS) \
$(GST_CXXFLAGS) \
$(QT_CFLAGS) \
$(GL_CFLAGS) -std=c++11
libgstqmlgl_la_LIBADD = \
$(GST_PLUGINS_BASE_LIBS) \
$(GST_GL_LIBS) \
-lgstvideo-$(GST_API_VERSION) \
$(GST_BASE_LIBS) \
$(QT_LIBS)
libgstqmlgl_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
$(moc_generated): moc_%.cc: %.h
@MOC@ -o $@ $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(MOC_CPPFLAGS) $<
ui-%.h: %.ui
@UIC@ -o $@ $<
qrc-%.cc: %.qrc
@RCC@ -o $@ $<
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstqtsink.h"
#include "gstqtsrc.h"
#include <QtQml/QQmlApplicationEngine>
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "qmlglsink",
GST_RANK_NONE, GST_TYPE_QT_SINK)) {
return FALSE;
}
if (!gst_element_register (plugin, "qmlglsrc",
GST_RANK_NONE, GST_TYPE_QT_SRC)) {
return FALSE;
}
/* this means the plugin must be loaded before the qml engine is loaded */
qmlRegisterType<QtGLVideoItem> ("org.freedesktop.gstreamer.GLVideoItem", 1, 0, "GstGLVideoItem");
return TRUE;
}
#ifndef GST_PACKAGE_NAME
#define GST_PACKAGE_NAME "GStreamer Bad Plug-ins (qmake)"
#define GST_PACKAGE_ORIGIN "Unknown package origin"
#define GST_LICENSE "LGPL"
#define PACKAGE "gst-plugins-bad (qmake)"
#define PACKAGE_VERSION "1.13.0.1"
#endif
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
qmlgl,
"Qt gl plugin",
plugin_init, PACKAGE_VERSION, GST_LICENSE, GST_PACKAGE_NAME,
GST_PACKAGE_ORIGIN)
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <vector>
#include <stdio.h>
#include <gst/video/video.h>
#include <gst/gl/gl.h>
#include <gst/gl/gstglfuncs.h>
#include "gstqsgtexture.h"
#define GST_CAT_DEFAULT gst_qsg_texture_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
GstQSGTexture::GstQSGTexture ()
{
static volatile gsize _debug;
initializeOpenGLFunctions();
if (g_once_init_enter (&_debug)) {
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtqsgtexture", 0,
"Qt Scenegraph Texture");
g_once_init_leave (&_debug, 1);
}
gst_video_info_init (&this->v_info);
this->buffer_ = NULL;
this->qt_context_ = NULL;
this->sync_buffer_ = gst_buffer_new ();
this->dummy_tex_id_ = 0;
}
GstQSGTexture::~GstQSGTexture ()
{
gst_buffer_replace (&this->buffer_, NULL);
gst_buffer_replace (&this->sync_buffer_, NULL);
if (this->dummy_tex_id_ && QOpenGLContext::currentContext ()) {
QOpenGLContext::currentContext ()->functions ()->glDeleteTextures (1,
&this->dummy_tex_id_);
}
}
/* only called from the streaming thread with scene graph thread blocked */
void
GstQSGTexture::setCaps (GstCaps * caps)
{
GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
gst_video_info_from_caps (&this->v_info, caps);
}
/* only called from the streaming thread with scene graph thread blocked */
gboolean
GstQSGTexture::setBuffer (GstBuffer * buffer)
{
GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
/* FIXME: update more state here */
if (!gst_buffer_replace (&this->buffer_, buffer))
return FALSE;
this->qt_context_ = gst_gl_context_get_current ();
return TRUE;
}
/* only called from qt's scene graph render thread */
void
GstQSGTexture::bind ()
{
const GstGLFuncs *gl;
GstGLContext *context;
GstGLSyncMeta *sync_meta;
GstMemory *mem;
guint tex_id;
gboolean use_dummy_tex = TRUE;
if (!this->qt_context_)
return;
gst_gl_context_activate (this->qt_context_, TRUE);
if (!this->buffer_)
goto out;
if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
goto out;
this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
if (!this->mem_)
goto out;
g_assert (this->qt_context_);
gl = this->qt_context_->gl_vtable;
/* FIXME: should really lock the memory to prevent write access */
if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
(GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
g_assert_not_reached ();
goto out;
}
mem = gst_buffer_peek_memory (this->buffer_, 0);
g_assert (gst_is_gl_memory (mem));
context = ((GstGLBaseMemory *)mem)->context;
sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
if (!sync_meta)
sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
gst_gl_sync_meta_set_sync_point (sync_meta, context);
gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
tex_id = *(guint *) this->v_frame.data[0];
GST_LOG ("%p binding Qt texture %u", this, tex_id);
gl->BindTexture (GL_TEXTURE_2D, tex_id);
gst_video_frame_unmap (&this->v_frame);
/* Texture was successfully bound, so we do not need
* to use the dummy texture */
use_dummy_tex = FALSE;
out:
if (G_UNLIKELY (use_dummy_tex)) {
QOpenGLContext *qglcontext = QOpenGLContext::currentContext ();
QOpenGLFunctions *funcs = qglcontext->functions ();
/* Create dummy texture if not already present.
* Use the Qt OpenGL functions instead of the GstGL ones,
* since we are using the Qt OpenGL context here, and we must
* be able to delete the texture in the destructor. */
if (this->dummy_tex_id_ == 0) {
/* Make this a black 64x64 pixel RGBA texture.
* This size and format is supported pretty much everywhere, so these
* are a safe pick. (64 pixel sidelength must be supported according
* to the GLES2 spec, table 6.18.)
* Set min/mag filters to GL_LINEAR to make sure no mipmapping is used. */
const int tex_sidelength = 64;
std::vector < guint8 > dummy_data (tex_sidelength * tex_sidelength * 4, 0);
funcs->glGenTextures (1, &this->dummy_tex_id_);
funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
funcs->glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_sidelength,
tex_sidelength, 0, GL_RGBA, GL_UNSIGNED_BYTE, &dummy_data[0]);
}
g_assert (this->dummy_tex_id_ != 0);
funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
}
gst_gl_context_activate (this->qt_context_, FALSE);
}
/* can be called from any thread */
int
GstQSGTexture::textureId () const
{
int tex_id = 0;
if (this->buffer_) {
GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
tex_id = ((GstGLMemory *) mem)->tex_id;
}
GST_LOG ("%p get texture id %u", this, tex_id);
return tex_id;
}
/* can be called from any thread */
QSize
GstQSGTexture::textureSize () const
{
if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
return QSize (0, 0);
GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
this->v_info.height);
return QSize (this->v_info.width, this->v_info.height);
}
/* can be called from any thread */
bool
GstQSGTexture::hasAlphaChannel () const
{
/* FIXME: support RGB textures */
return true;
}
/* can be called from any thread */
bool
GstQSGTexture::hasMipmaps () const
{
return false;
}
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_QSG_TEXTURE_H__
#define __GST_QSG_TEXTURE_H__
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/gl/gl.h>
#include "gstqtgl.h"
#include <QtQuick/QSGTexture>
#include <QtGui/QOpenGLFunctions>
class GstQSGTexture : public QSGTexture, protected QOpenGLFunctions
{
Q_OBJECT
public:
GstQSGTexture ();
~GstQSGTexture ();
void setCaps (GstCaps * caps);
gboolean setBuffer (GstBuffer * buffer);
/* QSGTexture */
void bind ();
int textureId () const;
QSize textureSize () const;
bool hasAlphaChannel () const;
bool hasMipmaps () const;
private:
GstBuffer * buffer_;
GstBuffer * sync_buffer_;
GstGLContext * qt_context_;
GstMemory * mem_;
GLuint dummy_tex_id_;
GstVideoInfo v_info;
GstVideoFrame v_frame;
};
#endif /* __GST_QSG_TEXTURE_H__ */
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/* qt uses the same trick as us to typedef GLsync on gles2 but to a different
* type which confuses the preprocessor. As it's never actually used by qt
* public headers, define it to something else to avoid redefinition
* warnings/errors */
#include <gst/gl/gstglconfig.h>
#include <QtCore/qglobal.h>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
#include <QtGui/qtgui-config.h>
#endif
#if defined(QT_OPENGL_ES_2) && GST_GL_HAVE_WINDOW_ANDROID
#define GLsync gst_qt_GLsync
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#undef GLsync
#endif /* defined(QT_OPENGL_ES_2) */
/*
* GStreamer
* Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstqtglutility.h"
#include <QtGui/QGuiApplication>
#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
#include <QX11Info>
#include <gst/gl/x11/gstgldisplay_x11.h>
#endif
#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
#include <qpa/qplatformnativeinterface.h>
#include <gst/gl/wayland/gstgldisplay_wayland.h>
#endif
#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
#if GST_GL_HAVE_WINDOW_VIV_FB
#include <qpa/qplatformnativeinterface.h>
#include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
#else
#include <gst/gl/egl/gstegl.h>
#include <gst/gl/egl/gstgldisplay_egl.h>
#endif
#endif
#define GST_CAT_DEFAULT qt_gl_utils_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
GstGLDisplay *
gst_qt_get_gl_display ()
{
GstGLDisplay *display = NULL;
QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
static volatile gsize _debug;
g_assert (app != NULL);
if (g_once_init_enter (&_debug)) {
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
"Qt gl utility functions");
g_once_init_leave (&_debug, 1);
}
GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
if (QString::fromUtf8 ("xcb") == app->platformName())
display = (GstGLDisplay *)
gst_gl_display_x11_new_with_display (QX11Info::display ());
#endif
#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
if (QString::fromUtf8 ("wayland") == app->platformName()
|| QString::fromUtf8 ("wayland-egl") == app->platformName()){
struct wl_display * wayland_display;
QPlatformNativeInterface *native =
QGuiApplication::platformNativeInterface();
wayland_display = (struct wl_display *)
native->nativeResourceForWindow("display", NULL);
display = (GstGLDisplay *)
gst_gl_display_wayland_new_with_display (wayland_display);
}
#endif
#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
if (QString::fromUtf8 ("android") == app->platformName())
display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (eglGetDisplay(EGL_DEFAULT_DISPLAY));
#elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
if (QString::fromUtf8("eglfs") == app->platformName()) {
#if GST_GL_HAVE_WINDOW_VIV_FB
/* FIXME: Could get the display directly from Qt like this
QPlatformNativeInterface *native =
QGuiApplication::platformNativeInterface();
EGLDisplay egl_display = (EGLDisplay)
native->nativeResourceForWindow("egldisplay", NULL);
However we seem to have no way for getting the EGLNativeDisplayType, aka
native_display, via public API. As such we have to assume that display 0
is always used. Only way around that is parsing the index the same way as
Qt does in QEGLDeviceIntegration::fbDeviceName(), so let's do that.
*/
const gchar *fb_dev;
gint disp_idx = 0;
fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
if (fb_dev) {
if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
disp_idx = 0;
}
display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx);
#else
display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (eglGetDisplay(EGL_DEFAULT_DISPLAY));
#endif
}
#endif
#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
if (QString::fromUtf8 ("cocoa") == app->platformName())
display = (GstGLDisplay *) gst_gl_display_new ();
#endif
#if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
if (QString::fromUtf8 ("ios") == app->platformName())
display = gst_gl_display_new ();
#endif
#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
if (QString::fromUtf8 ("windows") == app->platformName())
display = gst_gl_display_new ();
#endif
if (!display)
display = gst_gl_display_new ();
return display;
}
gboolean
gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
GstGLContext **wrap_glcontext, GstGLContext **context)
{
GstGLPlatform platform = (GstGLPlatform) 0;
GstGLAPI gl_api;
guintptr gl_handle;
GError *error = NULL;
g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
if (GST_IS_GL_DISPLAY_X11 (display)) {
#if GST_GL_HAVE_PLATFORM_GLX
platform = GST_GL_PLATFORM_GLX;
#elif GST_GL_HAVE_PLATFORM_EGL
platform = GST_GL_PLATFORM_EGL;
#endif
}
#endif
#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
if (GST_IS_GL_DISPLAY_WAYLAND (display)) {
platform = GST_GL_PLATFORM_EGL;
}
#endif
#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
#if GST_GL_HAVE_WINDOW_VIV_FB
if (GST_IS_GL_DISPLAY_VIV_FB (display)) {
#else
if (GST_IS_GL_DISPLAY_EGL (display)) {
#endif
platform = GST_GL_PLATFORM_EGL;
}
#endif
if (platform == 0) {
#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
platform = GST_GL_PLATFORM_CGL;
#elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
platform = GST_GL_PLATFORM_EAGL;
#elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
platform = GST_GL_PLATFORM_WGL;
#else
GST_ERROR ("Unknown platform");
return FALSE;
#endif
}
gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
gl_handle = gst_gl_context_get_current_gl_context (platform);
if (gl_handle)
*wrap_glcontext =
gst_gl_context_new_wrapped (display, gl_handle,
platform, gl_api);
if (!*wrap_glcontext) {
GST_ERROR ("cannot wrap qt OpenGL context");
return FALSE;
}
(void) platform;
(void) gl_api;
(void) gl_handle;
gst_gl_context_activate (*wrap_glcontext, TRUE);
if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {