summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBartlomiej Moskal <bartlomiej.moskal@qt.io>2023-12-13 14:09:22 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-12-14 21:36:46 +0000
commit312c718a8b7c65e16a266a71d8d39efd8ff1a589 (patch)
tree00306b773b8a35b9ae712fc6806faf8ff53b04b8 /src
parent522b7d56bad00334ec250c6334833fa9bcfdc8ec (diff)
Android-backend: Use the same thread by TextureVideoOutput after reset
7d53ccf2623dfacd4cc8f400729fb78d886924b4 commit fixed the Memory leak related to not deleted QRhi. QRhiWithThreadGuard was added to keep m_rhi and m_thread until frame will be released. Currently there is a problem when frame is kept too long. After unlocking the screen and going back to app, QRhi is recreated. It is recreated from the new thread. If the old frame (from previous m_rhi and previous m_thread) was somehow still kept and it will be released after that, app will crash with: "Cannot make QOpenGLContext current in a different thread" After mentioned commit, QRhi is recreated from the new thread. The reason of re-creating AndroidTextureThread was to not handle old pending frames after reset. As this solution may cause the crash - it needs to be handled in other way. This commit stops re-creating texture thread during reset. Instead of this, there is an additional check if current surfaceTexture is the same that was used for connection. Pick-to: 6.6 6.5 Fixes: QTBUG-118839 Change-Id: I853c3c96b690778435fd592898973f1f478fcea3 Reviewed-by: Artem Dyomin <artem.dyomin@qt.io> (cherry picked from commit c07c261eb311ac4f3a22c03b27d4b0c65cf69917) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r--src/plugins/multimedia/android/common/qandroidvideooutput.cpp15
-rw-r--r--src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture.cpp3
-rw-r--r--src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture_p.h2
3 files changed, 11 insertions, 9 deletions
diff --git a/src/plugins/multimedia/android/common/qandroidvideooutput.cpp b/src/plugins/multimedia/android/common/qandroidvideooutput.cpp
index 5724a56ba..16bbc0d7a 100644
--- a/src/plugins/multimedia/android/common/qandroidvideooutput.cpp
+++ b/src/plugins/multimedia/android/common/qandroidvideooutput.cpp
@@ -298,10 +298,11 @@ public:
}
public slots:
- void onFrameAvailable()
+ void onFrameAvailable(quint64 index)
{
- // Check if 'm_surfaceTexture' is not reset because there can be pending frames in queue.
- if (m_surfaceTexture) {
+ // Check if 'm_surfaceTexture' is not reset and if the current index is the same that
+ // was used for creating connection because there can be pending frames in queue.
+ if (m_surfaceTexture && m_surfaceTexture->index() == index) {
m_surfaceTexture->updateTexImage();
auto matrix = extTransformMatrix(m_surfaceTexture.get());
auto tex = m_textureCopy->copyExternalTexture(m_size, matrix);
@@ -337,8 +338,9 @@ public slots:
m_texture->create();
m_surfaceTexture = std::make_unique<AndroidSurfaceTexture>(m_texture->nativeTexture().object);
if (m_surfaceTexture->surfaceTexture()) {
+ const quint64 index = m_surfaceTexture->index();
connect(m_surfaceTexture.get(), &AndroidSurfaceTexture::frameAvailable, this,
- &AndroidTextureThread::onFrameAvailable);
+ [this, index] () { this->onFrameAvailable(index); });
m_textureCopy = std::make_unique<TextureCopy>(m_rhi.get(), m_texture.get());
@@ -453,11 +455,6 @@ void QAndroidTextureVideoOutput::reset()
if (m_sink)
m_sink->platformVideoSink()->setVideoFrame({});
QMetaObject::invokeMethod(m_surfaceThread.get(), &AndroidTextureThread::clearSurfaceTexture);
- // We are not allowed to work on the same thread after the reset
- // as there may be pending frames and should not be handled.
- startNewSurfaceThread();
- QMetaObject::invokeMethod(m_surfaceThread.get(),
- [&](){ m_surfaceThread->setFrameSize(m_nativeSize); }, Qt::BlockingQueuedConnection);
}
void QAndroidTextureVideoOutput::newFrame(const QVideoFrame &frame)
diff --git a/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture.cpp b/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture.cpp
index 53da2ffd8..c5860b265 100644
--- a/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture.cpp
+++ b/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture.cpp
@@ -12,6 +12,8 @@ typedef QList<jlong> SurfaceTextures;
Q_GLOBAL_STATIC(SurfaceTextures, g_surfaceTextures);
Q_GLOBAL_STATIC(QMutex, g_textureMutex);
+static QAtomicInteger<quint64> indexCounter = 0u;
+
// native method for QtSurfaceTexture.java
static void notifyFrameAvailable(JNIEnv* , jobject, jlong id)
{
@@ -27,6 +29,7 @@ static void notifyFrameAvailable(JNIEnv* , jobject, jlong id)
AndroidSurfaceTexture::AndroidSurfaceTexture(quint32 texName)
: QObject()
+ , m_index(indexCounter.fetchAndAddRelaxed(1))
{
Q_STATIC_ASSERT(sizeof (jlong) >= sizeof (void *));
m_surfaceTexture = QJniObject("android/graphics/SurfaceTexture", "(I)V", jint(texName));
diff --git a/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture_p.h b/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture_p.h
index fce840e74..24581ca8d 100644
--- a/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture_p.h
+++ b/src/plugins/multimedia/android/wrappers/jni/androidsurfacetexture_p.h
@@ -43,6 +43,7 @@ public:
static bool registerNativeMethods();
+ quint64 index() const { return m_index; }
Q_SIGNALS:
void frameAvailable();
@@ -52,6 +53,7 @@ private:
QJniObject m_surfaceTexture;
QJniObject m_surface;
QJniObject m_surfaceHolder;
+ const quint64 m_index = 0;
};
QT_END_NAMESPACE