diff options
author | Yoann Lopes <yoann.lopes@qt.io> | 2016-05-04 15:41:32 +0200 |
---|---|---|
committer | Yoann Lopes <yoann.lopes@qt.io> | 2016-05-10 07:37:51 +0000 |
commit | ba8127639857232d8a37e953c5cda84203360d97 (patch) | |
tree | 441b0882a6846d45f7e98d0da6c37b15c5643bbd | |
parent | 37d91ff58d33a573f4d546282c0c5dfe0e5f4aa2 (diff) |
Android: improve texture rendering on API level >= 16.
Android API level 16 added SurfaceTexture::attachToGLContext(). This
allows to create the OpenGL texture when the first video frame is
available, rather than at initialization. This means we can do without
the ugly hack that makes the render thread call us back through some
custom property.
Additionally, it allows to recreate a new OpenGL texture every time
the SurfaceTexture is reset.
Task-number: QTBUG-51911
Change-Id: I17b04524d426c42ef8aa0288b0731597bc9eba62
Reviewed-by: Christian Stromme <christian.stromme@qt.io>
4 files changed, 57 insertions, 13 deletions
diff --git a/src/plugins/android/src/common/qandroidvideooutput.cpp b/src/plugins/android/src/common/qandroidvideooutput.cpp index 3a22c4e98..c0bd88d85 100644 --- a/src/plugins/android/src/common/qandroidvideooutput.cpp +++ b/src/plugins/android/src/common/qandroidvideooutput.cpp @@ -42,6 +42,7 @@ #include <qopenglfunctions.h> #include <qopenglshaderprogram.h> #include <qopenglframebufferobject.h> +#include <QtCore/private/qjnihelpers_p.h> QT_BEGIN_NAMESPACE @@ -159,6 +160,7 @@ QAndroidTextureVideoOutput::QAndroidTextureVideoOutput(QObject *parent) , m_fbo(0) , m_program(0) , m_glDeleter(0) + , m_surfaceTextureCanAttachToContext(QtAndroidPrivate::androidSdkVersion() >= 16) { } @@ -184,12 +186,14 @@ void QAndroidTextureVideoOutput::setSurface(QAbstractVideoSurface *surface) if (m_surface) { if (m_surface->isActive()) m_surface->stop(); - m_surface->setProperty("_q_GLThreadCallback", QVariant()); + + if (!m_surfaceTextureCanAttachToContext) + m_surface->setProperty("_q_GLThreadCallback", QVariant()); } m_surface = surface; - if (m_surface) { + if (m_surface && !m_surfaceTextureCanAttachToContext) { m_surface->setProperty("_q_GLThreadCallback", QVariant::fromValue<QObject*>(this)); } @@ -197,7 +201,7 @@ void QAndroidTextureVideoOutput::setSurface(QAbstractVideoSurface *surface) bool QAndroidTextureVideoOutput::isReady() { - return QOpenGLContext::currentContext() || m_externalTex; + return m_surfaceTextureCanAttachToContext || QOpenGLContext::currentContext() || m_externalTex; } bool QAndroidTextureVideoOutput::initSurfaceTexture() @@ -208,14 +212,16 @@ bool QAndroidTextureVideoOutput::initSurfaceTexture() if (!m_surface) return false; - // if we have an OpenGL context in the current thread, create a texture. Otherwise, wait - // for the GL render thread to call us back to do it. - if (QOpenGLContext::currentContext()) { - glGenTextures(1, &m_externalTex); - m_glDeleter = new OpenGLResourcesDeleter; - m_glDeleter->setTexture(m_externalTex); - } else if (!m_externalTex) { - return false; + if (!m_surfaceTextureCanAttachToContext) { + // if we have an OpenGL context in the current thread, create a texture. Otherwise, wait + // for the GL render thread to call us back to do it. + if (QOpenGLContext::currentContext()) { + glGenTextures(1, &m_externalTex); + m_glDeleter = new OpenGLResourcesDeleter; + m_glDeleter->setTexture(m_externalTex); + } else if (!m_externalTex) { + return false; + } } QMutexLocker locker(&m_mutex); @@ -227,7 +233,8 @@ bool QAndroidTextureVideoOutput::initSurfaceTexture() } else { delete m_surfaceTexture; m_surfaceTexture = 0; - m_glDeleter->deleteLater(); + if (m_glDeleter) + m_glDeleter->deleteLater(); m_externalTex = 0; m_glDeleter = 0; } @@ -242,6 +249,10 @@ void QAndroidTextureVideoOutput::clearSurfaceTexture() delete m_surfaceTexture; m_surfaceTexture = 0; } + + // Also reset the attached OpenGL texture + if (m_surfaceTextureCanAttachToContext) + m_externalTex = 0; } AndroidSurfaceTexture *QAndroidTextureVideoOutput::surfaceTexture() @@ -364,6 +375,18 @@ void QAndroidTextureVideoOutput::renderFrameToFbo() void QAndroidTextureVideoOutput::createGLResources() { + Q_ASSERT(QOpenGLContext::currentContext() != NULL); + + if (!m_glDeleter) + m_glDeleter = new OpenGLResourcesDeleter; + + if (m_surfaceTextureCanAttachToContext && !m_externalTex) { + m_surfaceTexture->detachFromGLContext(); + glGenTextures(1, &m_externalTex); + m_surfaceTexture->attachToGLContext(m_externalTex); + m_glDeleter->setTexture(m_externalTex); + } + if (!m_fbo || m_fbo->size() != m_nativeSize) { delete m_fbo; m_fbo = new QOpenGLFramebufferObject(m_nativeSize); @@ -407,7 +430,7 @@ void QAndroidTextureVideoOutput::customEvent(QEvent *e) { if (e->type() == QEvent::User) { // This is running in the render thread (OpenGL enabled) - if (!m_externalTex) { + if (!m_surfaceTextureCanAttachToContext && !m_externalTex) { glGenTextures(1, &m_externalTex); m_glDeleter = new OpenGLResourcesDeleter; // will cleanup GL resources in the correct thread m_glDeleter->setTexture(m_externalTex); diff --git a/src/plugins/android/src/common/qandroidvideooutput.h b/src/plugins/android/src/common/qandroidvideooutput.h index f4401fa1d..0068a5809 100644 --- a/src/plugins/android/src/common/qandroidvideooutput.h +++ b/src/plugins/android/src/common/qandroidvideooutput.h @@ -110,6 +110,8 @@ private: QOpenGLShaderProgram *m_program; OpenGLResourcesDeleter *m_glDeleter; + bool m_surfaceTextureCanAttachToContext; + friend class AndroidTextureVideoBuffer; }; diff --git a/src/plugins/android/src/wrappers/jni/androidsurfacetexture.cpp b/src/plugins/android/src/wrappers/jni/androidsurfacetexture.cpp index 9a25b7e28..2cea896e1 100644 --- a/src/plugins/android/src/wrappers/jni/androidsurfacetexture.cpp +++ b/src/plugins/android/src/wrappers/jni/androidsurfacetexture.cpp @@ -146,6 +146,22 @@ jobject AndroidSurfaceTexture::surfaceHolder() return m_surfaceHolder.object(); } +void AndroidSurfaceTexture::attachToGLContext(int texName) +{ + if (QtAndroidPrivate::androidSdkVersion() < 16 || !m_surfaceTexture.isValid()) + return; + + m_surfaceTexture.callMethod<void>("attachToGLContext", "(I)V", texName); +} + +void AndroidSurfaceTexture::detachFromGLContext() +{ + if (QtAndroidPrivate::androidSdkVersion() < 16 || !m_surfaceTexture.isValid()) + return; + + m_surfaceTexture.callMethod<void>("detachFromGLContext"); +} + bool AndroidSurfaceTexture::initJNI(JNIEnv *env) { // SurfaceTexture is available since API 11. diff --git a/src/plugins/android/src/wrappers/jni/androidsurfacetexture.h b/src/plugins/android/src/wrappers/jni/androidsurfacetexture.h index ac2af694e..a08483e5e 100644 --- a/src/plugins/android/src/wrappers/jni/androidsurfacetexture.h +++ b/src/plugins/android/src/wrappers/jni/androidsurfacetexture.h @@ -58,6 +58,9 @@ public: void release(); // API level 14 void updateTexImage(); + void attachToGLContext(int texName); // API level 16 + void detachFromGLContext(); // API level 16 + static bool initJNI(JNIEnv *env); Q_SIGNALS: |