From f52c9653c98382bc5e03bb645baf63b91c33eece Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Fri, 19 Jun 2020 11:29:16 +0200 Subject: Port shared texture provider to RHI-on-OpenGL There is no actual need for the SharedTexture subclass, since the OpenGL context is guaranteed to be bound when the createTexture() call is made, and we don't support the buffer changing after it has been received. Task-number: QTBUG-78673 Change-Id: I73e0f755e0618c67dabeccb0085bb44560f2a214 Reviewed-by: Paul Olav Tvete --- .../extensions/qwltexturesharingextension.cpp | 80 +++------------------- .../texture-sharing/sharedtextureprovider.cpp | 63 ++++------------- .../texture-sharing/sharedtextureprovider.h | 21 ------ 3 files changed, 25 insertions(+), 139 deletions(-) diff --git a/src/compositor/extensions/qwltexturesharingextension.cpp b/src/compositor/extensions/qwltexturesharingextension.cpp index ef1d82fe2..c90fde1ec 100644 --- a/src/compositor/extensions/qwltexturesharingextension.cpp +++ b/src/compositor/extensions/qwltexturesharingextension.cpp @@ -50,74 +50,6 @@ QT_BEGIN_NAMESPACE -class SharedTexture : public QSGTexture -{ - Q_OBJECT -public: - SharedTexture(QtWayland::ServerBuffer *buffer); - - //TODO: QRhiTexture - - int textureId() const;//######### override; - qint64 comparisonKey() const override; - QSize textureSize() const override; - bool hasAlphaChannel() const override; - bool hasMipmaps() const override; - - void bind(); //###### override; - -private: - void updateGLTexture() const; - QtWayland::ServerBuffer *m_buffer = nullptr; - mutable QOpenGLTexture *m_tex = nullptr; -}; - -SharedTexture::SharedTexture(QtWayland::ServerBuffer *buffer) - : m_buffer(buffer), m_tex(nullptr) -{ -} - -int SharedTexture::textureId() const -{ - updateGLTexture(); - return m_tex ? m_tex->textureId() : 0; -} - -qint64 SharedTexture::comparisonKey() const -{ - return m_tex ? qint64(m_tex->textureId()) : qint64(this); -} - -QSize SharedTexture::textureSize() const -{ - updateGLTexture(); - return m_tex ? QSize(m_tex->width(), m_tex->height()) : QSize(); -} - -bool SharedTexture::hasAlphaChannel() const -{ - return true; -} - -bool SharedTexture::hasMipmaps() const -{ - updateGLTexture(); - return m_tex ? (m_tex->mipLevels() > 1) : false; -} - -void SharedTexture::bind() -{ - updateGLTexture(); - if (m_tex) - m_tex->bind(); -} - -inline void SharedTexture::updateGLTexture() const -{ - if (!m_tex && m_buffer) - m_tex = m_buffer->toOpenGlTexture(); -} - class SharedTextureFactory : public QQuickTextureFactory { public: @@ -142,9 +74,17 @@ public: return m_buffer ? (m_buffer->size().width() * m_buffer->size().height() * 4) : 0; } - QSGTexture *createTexture(QQuickWindow *) const override + QSGTexture *createTexture(QQuickWindow *window) const override { - return new SharedTexture(const_cast(m_buffer)); + if (m_buffer != nullptr) { + QOpenGLTexture *texture = const_cast(m_buffer)->toOpenGlTexture(); + return QNativeInterface::QSGOpenGLTexture::fromNative(texture->textureId(), + window, + m_buffer->size(), + QQuickWindow::TextureHasAlphaChannel); + } + + return nullptr; } private: diff --git a/src/imports/texture-sharing/sharedtextureprovider.cpp b/src/imports/texture-sharing/sharedtextureprovider.cpp index a7d61d519..45da33330 100644 --- a/src/imports/texture-sharing/sharedtextureprovider.cpp +++ b/src/imports/texture-sharing/sharedtextureprovider.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -60,52 +61,6 @@ QT_BEGIN_NAMESPACE -SharedTexture::SharedTexture(QtWaylandClient::QWaylandServerBuffer *buffer) - : m_buffer(buffer), m_tex(nullptr) -{ -} - -int SharedTexture::textureId() const -{ - updateGLTexture(); - return m_tex ? m_tex->textureId() : 0; -} - -qint64 SharedTexture::comparisonKey() const -{ - return m_tex ? qint64(m_tex->textureId()) : qint64(this); -} - -QSize SharedTexture::textureSize() const -{ - updateGLTexture(); - return m_tex ? QSize(m_tex->width(), m_tex->height()) : QSize(); -} - -bool SharedTexture::hasAlphaChannel() const -{ - return true; -} - -bool SharedTexture::hasMipmaps() const -{ - updateGLTexture(); - return m_tex ? (m_tex->mipLevels() > 1) : false; -} - -void SharedTexture::bind() -{ - updateGLTexture(); - if (m_tex) - m_tex->bind(); -} - -inline void SharedTexture::updateGLTexture() const -{ - if (!m_tex && m_buffer) - m_tex = m_buffer->toOpenGlTexture(); -} - class SharedTextureFactory : public QQuickTextureFactory { public: @@ -133,9 +88,16 @@ public: return m_buffer ? (m_buffer->size().width() * m_buffer->size().height() * 4) : 0; } - QSGTexture *createTexture(QQuickWindow *) const override + QSGTexture *createTexture(QQuickWindow *window) const override { - return new SharedTexture(const_cast(m_buffer)); + if (m_buffer != nullptr) { + QOpenGLTexture *texture = const_cast(m_buffer)->toOpenGlTexture(); + return QNativeInterface::QSGOpenGLTexture::fromNative(texture->textureId(), + window, + m_buffer->size(), + QQuickWindow::TextureHasAlphaChannel); + } + return nullptr; } private: @@ -189,6 +151,11 @@ void SharedTextureRegistry::handleExtensionActive() bool SharedTextureRegistry::preinitialize() { + if (QSGRhiSupport::instance()->rhiBackend() != QRhi::OpenGLES2) { + qWarning() << "The shared-texture extension is only supported on OpenGL. Use QQuickWindow::setSceneGraphBackend() to override the default."; + return false; + } + auto *serverBufferIntegration = QGuiApplicationPrivate::platformIntegration()->nativeInterface()->nativeResourceForIntegration("server_buffer_integration"); if (!serverBufferIntegration) { diff --git a/src/imports/texture-sharing/sharedtextureprovider.h b/src/imports/texture-sharing/sharedtextureprovider.h index d200315e0..7eaf9e0d5 100644 --- a/src/imports/texture-sharing/sharedtextureprovider.h +++ b/src/imports/texture-sharing/sharedtextureprovider.h @@ -93,27 +93,6 @@ private: bool m_sharingAvailable = false; }; -class SharedTexture : public QSGTexture -{ - Q_OBJECT -public: - SharedTexture(QtWaylandClient::QWaylandServerBuffer *buffer); - - int textureId() const; //###### override; - qint64 comparisonKey() const override; - QSize textureSize() const override; - bool hasAlphaChannel() const override; - bool hasMipmaps() const override; - - void bind(); //######## override; - -private: - void updateGLTexture() const; - QtWaylandClient::QWaylandServerBuffer *m_buffer = nullptr; - mutable QOpenGLTexture *m_tex = nullptr; -}; - - QT_END_NAMESPACE #endif // SHAREDTEXTUREPROVIDER_H -- cgit v1.2.3