From 74402c49a4c6e9411a06fd1b0a8e8de6725433a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A4=C3=A4tt=C3=A4=20Antti?= Date: Fri, 25 May 2018 14:44:09 +0300 Subject: Fix flashing and crashing of scene2d Prevent simultanious usage of the GLTexture being rendered to by scene2d. Task-number: QTBUG-68511 Task-number: QT3DS-1792 Change-Id: I88de12cba68ef7af2c7afb1f6e9d6143028efc86 Reviewed-by: Andy Nichols --- .../opengl/graphicshelpers/submissioncontext.cpp | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/render/renderers/opengl/graphicshelpers') diff --git a/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp b/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp index 4ec929b6e..ecaa12d8b 100644 --- a/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp +++ b/src/render/renderers/opengl/graphicshelpers/submissioncontext.cpp @@ -93,6 +93,40 @@ QT_BEGIN_NAMESPACE namespace Qt3DRender { namespace Render { + +class TextureExtRendererLocker +{ +public: + static void lock(GLTexture *tex) + { + if (!tex->isExternalRenderingEnabled()) + return; + if (s_lockHash.keys().contains(tex)) { + ++s_lockHash[tex]; + } else { + tex->externalRenderingLock()->lock(); + s_lockHash[tex] = 1; + } + } + static void unlock(GLTexture *tex) + { + if (!tex->isExternalRenderingEnabled()) + return; + if (!s_lockHash.keys().contains(tex)) + return; + + --s_lockHash[tex]; + if (s_lockHash[tex] == 0) { + s_lockHash.remove(tex); + tex->externalRenderingLock()->unlock(); + } + } +private: + static QHash s_lockHash; +}; + +QHash TextureExtRendererLocker::s_lockHash = QHash(); + static QHash static_contexts; namespace { @@ -475,6 +509,9 @@ void SubmissionContext::endDrawing(bool swapBuffers) if (m_ownCurrent) m_gl->doneCurrent(); decayTextureScores(); + for (int i = 0; i < m_activeTextures.size(); ++i) + if (m_activeTextures[i].texture) + TextureExtRendererLocker::unlock(m_activeTextures[i].texture); } void SubmissionContext::activateRenderTarget(Qt3DCore::QNodeId renderTargetNodeId, const AttachmentPack &attachments, GLuint defaultFboId) @@ -827,6 +864,8 @@ void SubmissionContext::bindFrameBufferAttachmentHelper(GLuint fboId, const Atta if (!m_glHelper->frameBufferNeedsRenderBuffer(attachment)) { QOpenGLTexture *glTex = rTex ? rTex->getOrCreateGLTexture() : nullptr; if (glTex != nullptr) { + // The texture can not be rendered simultaniously by another renderer + Q_ASSERT(!rTex->isExternalRenderingEnabled()); if (fboSize.isEmpty()) fboSize = QSize(glTex->width(), glTex->height()); else @@ -892,7 +931,10 @@ int SubmissionContext::activateTexture(TextureScope scope, GLTexture *tex, int o if (glTex == nullptr) return -1; glTex->bind(onUnit); + if (m_activeTextures[onUnit].texture) + TextureExtRendererLocker::unlock(m_activeTextures[onUnit].texture); m_activeTextures[onUnit].texture = tex; + TextureExtRendererLocker::lock(tex); } #if defined(QT3D_RENDER_ASPECT_OPENGL_DEBUG) -- cgit v1.2.3