From f9a576826c0d770aaae8aadbedaf83e1c0b72e0a Mon Sep 17 00:00:00 2001 From: Val Doroshchuk Date: Thu, 26 Jul 2018 15:08:28 +0200 Subject: Android: Fix fetching video frame data from the GUI thread MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If QAbstractVideoSurface is not used on rendering thread added a way to pass shared opengl context via GLContext surface property. Camera and media players that are created in Qt Quick will usually have their rendering done on the render thread, but the callback for custom video surface(s) (or probes) will usually be handle on the GUI thread, so to make it convenient to for the user to access the frame data, which is rendered on request, we now set-up context sharing and render to a fbo on the calling thread, if needed. Task-number: QTBUG-69333 Change-Id: I4cc5c1f741c82376f1402a047b946b59281c9a4c Reviewed-by: Christian Strømme --- .../android/src/common/qandroidvideooutput.cpp | 33 ++++++++++++++++++++++ .../android/src/common/qandroidvideooutput.h | 5 ++++ 2 files changed, 38 insertions(+) (limited to 'src/plugins/android/src/common') diff --git a/src/plugins/android/src/common/qandroidvideooutput.cpp b/src/plugins/android/src/common/qandroidvideooutput.cpp index 083ceff24..a4f4dbd5d 100644 --- a/src/plugins/android/src/common/qandroidvideooutput.cpp +++ b/src/plugins/android/src/common/qandroidvideooutput.cpp @@ -49,6 +49,8 @@ #include #include #include +#include +#include QT_BEGIN_NAMESPACE @@ -182,6 +184,8 @@ QAndroidTextureVideoOutput::QAndroidTextureVideoOutput(QObject *parent) QAndroidTextureVideoOutput::~QAndroidTextureVideoOutput() { + delete m_offscreenSurface; + delete m_glContext; clearSurfaceTexture(); if (m_glDeleter) { // Make sure all of these are deleted on the render thread. @@ -345,6 +349,35 @@ bool QAndroidTextureVideoOutput::renderFrameToFbo() if (!m_nativeSize.isValid() || !m_surfaceTexture) return false; + // Make sure we have an OpenGL context to make current. + if (!QOpenGLContext::currentContext() && !m_glContext) { + // Create Hidden QWindow surface to create context in this thread. + m_offscreenSurface = new QWindow(); + m_offscreenSurface->setSurfaceType(QWindow::OpenGLSurface); + // Needs geometry to be a valid surface, but size is not important. + m_offscreenSurface->setGeometry(0, 0, 1, 1); + m_offscreenSurface->create(); + + // Create OpenGL context and set share context from surface. + m_glContext = new QOpenGLContext(); + m_glContext->setFormat(m_offscreenSurface->requestedFormat()); + + auto surface = qobject_cast(m_surface->property("videoSurface").value()); + if (!surface) + surface = m_surface; + auto shareContext = qobject_cast(surface->property("GLContext").value()); + if (shareContext) + m_glContext->setShareContext(shareContext); + + if (!m_glContext->create()) { + qWarning("Failed to create QOpenGLContext"); + return false; + } + } + + if (m_glContext) + m_glContext->makeCurrent(m_offscreenSurface); + createGLResources(); m_surfaceTexture->updateTexImage(); diff --git a/src/plugins/android/src/common/qandroidvideooutput.h b/src/plugins/android/src/common/qandroidvideooutput.h index 2a35247e9..456fe8e22 100644 --- a/src/plugins/android/src/common/qandroidvideooutput.h +++ b/src/plugins/android/src/common/qandroidvideooutput.h @@ -51,6 +51,8 @@ class AndroidSurfaceHolder; class QOpenGLFramebufferObject; class QOpenGLShaderProgram; class QAbstractVideoSurface; +class QWindow; +class QOpenGLContext; class QAndroidVideoOutput : public QObject { @@ -132,6 +134,9 @@ private: bool m_surfaceTextureCanAttachToContext; + QWindow *m_offscreenSurface = nullptr; + QOpenGLContext *m_glContext = nullptr; + friend class AndroidTextureVideoBuffer; }; -- cgit v1.2.3