diff options
author | Antti Määttä <antti.maatta@qt.io> | 2016-10-21 11:59:45 +0300 |
---|---|---|
committer | Antti Määttä <antti.maatta@qt.io> | 2017-01-24 14:54:48 +0000 |
commit | e24179c3711eb93e7a2b8a205ab958bce53ef966 (patch) | |
tree | 7afff3e7be5bd04d6860b1d7a50ea5102b1d4c31 | |
parent | 3e31736ad632316797ba05517a283123d4b12be1 (diff) | |
download | qt3d-e24179c3711eb93e7a2b8a205ab958bce53ef966.tar.gz |
Add RenderQmlToTexture implementation
This contains the implementation from the old patch. It will be modified
in later commits.
Change-Id: I84afdeec684ea9f6b64353eda731484fdb354798
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r-- | src/render/framegraph/qrenderqmltotexture.cpp | 620 | ||||
-rw-r--r-- | src/render/framegraph/qrenderqmltotexture.h | 96 | ||||
-rw-r--r-- | src/render/framegraph/qrenderqmltotexture_p.h | 212 | ||||
-rw-r--r-- | src/render/framegraph/renderqmltotexture.cpp | 304 | ||||
-rw-r--r-- | src/render/framegraph/renderqmltotexture_p.h | 116 |
5 files changed, 1348 insertions, 0 deletions
diff --git a/src/render/framegraph/qrenderqmltotexture.cpp b/src/render/framegraph/qrenderqmltotexture.cpp new file mode 100644 index 000000000..d4fa41874 --- /dev/null +++ b/src/render/framegraph/qrenderqmltotexture.cpp @@ -0,0 +1,620 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qrenderqmltotexture.h" +#include "qrenderqmltotexture_p.h" + +#include <Qt3DCore/QPropertyUpdatedChange> + +#include <QtGui/QOpenGLContext> + +QT_BEGIN_NAMESPACE + +using namespace Qt3DCore; + +namespace Qt3DRender { + + +/*! + \class Qt3DRender::QRenderQmlToTexture + \inmodule Qt3DRender + + \brief This class enables rendering qml into a texture, which then can be + used as a part of 3D scene. + + The component uses QQuickRenderControl to render the given QML source into an + offscreen surface, which is attached to a texture provided by the user. This allows the + component to directly render into the texture without intermediate copy and the user to + freely specify how the texture is used in the 3D scene. + + \since 5.9 +*/ + +/*! + \qmltype RenderQmlToTexture + \inqmlmodule Qt3D.Render + \since 5.9 + \inherits + \instantiates Qt3DRender::QRenderQmlToTexture + \brief RenderQmlToTexture + */ + +/*! + \qmlproperty Qt3DRender::QAbstractTexture Qt3D.Render::RenderQmlToTexture::texture + Holds the texture being rendered to. + */ + +/*! + \qmlproperty QUrl Qt3D.Render::RenderQmlToTexture::source + Holds the qml source url. + */ + +/*! + \qmlproperty bool Qt3D.Render::RenderQmlToTexture::renderOnce + Holds whether the first rendered image to the texture is also the last, after which the + renderer releases resources needed for the rendering and the rendering is no longer possible + with this RenderQmlToTexture object. + */ + +/*! + \qmlproperty bool Qt3D.Render::RenderQmlToTexture::loaded + Hold whether the source has been loaded. + */ + +class RenderControl : public QQuickRenderControl +{ +public: + RenderControl(QWindow *w) : m_window(w) { } + QWindow *renderWindow(QPoint *offset) Q_DECL_OVERRIDE; + +private: + QWindow *m_window; +}; + +QWindow *RenderControl::renderWindow(QPoint *offset) +{ + if (offset) + *offset = QPoint(0, 0); + return m_window; +} + +/*! + \internal + Constructs object shared by the front-end and back-end to synchronize the rendering. + */ +RenderQmlToTextureSharedObject::RenderQmlToTextureSharedObject(RenderQmlToTextureManager *manager) + : m_quit(false) + , m_requestSync(false) + , m_prepared(false) + , m_initialized(false) + , m_renderControl(nullptr) + , m_quickWindow(nullptr) + , m_renderManager(manager) + , m_surface(nullptr) + , m_renderObject(nullptr) + , m_disallowed(false) +{ +} + +RenderQmlToTextureSharedObject::~RenderQmlToTextureSharedObject() +{ +} + +void RenderQmlToTextureSharedObject::cleanup() +{ + delete m_renderControl; + delete m_quickWindow; + delete m_surface; + m_renderControl = nullptr; + m_quickWindow = nullptr; + m_surface = nullptr; + m_initialized = false; +} + +bool RenderQmlToTextureSharedObject::canRender() const +{ + return m_initialized && m_prepared && !m_disallowed; +} + +bool RenderQmlToTextureSharedObject::isInitialized() const +{ + return m_initialized; +} + +void RenderQmlToTextureSharedObject::disallowRender() +{ + m_disallowed = true; +} + +void RenderQmlToTextureSharedObject::setInitialized() +{ + m_initialized = true; +} + +bool RenderQmlToTextureSharedObject::isPrepared() const +{ + return m_prepared; +} + +void RenderQmlToTextureSharedObject::setPrepared() +{ + m_prepared = true; +} + +// not protected, call only from main thread +bool RenderQmlToTextureSharedObject::isQuit() const +{ + Q_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread()); + return m_quit; +} + +// not protected, call only from main thread +void RenderQmlToTextureSharedObject::requestQuit() +{ + Q_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread()); + m_quit = true; + QCoreApplication::postEvent(m_renderObject, new QEvent(QUIT)); +} + +bool RenderQmlToTextureSharedObject::isSyncRequested() const +{ + return m_requestSync; +} + +void RenderQmlToTextureSharedObject::requestRender(bool sync) +{ + m_requestSync = sync; + QCoreApplication::postEvent(m_renderObject, new QEvent(RENDER)); +} + +void RenderQmlToTextureSharedObject::waitRender() +{ + m_cond.wait(&m_mutex); +} + +void RenderQmlToTextureSharedObject::wakeWaiting() +{ + m_cond.wakeOne(); +} + +void RenderQmlToTextureSharedObject::clearSyncRequest() +{ + m_requestSync = false; +} + +/*! + \internal + Constructs qml render manager. + */ +RenderQmlToTextureManager::RenderQmlToTextureManager(QRenderQmlToTexturePrivate *priv) + : m_priv(priv) + , m_qmlEngine(nullptr) + , m_qmlComponent(nullptr) + , m_rootItem(nullptr) + , m_source(nullptr) + , m_texture(nullptr) + , m_requested(false) + , m_initialized(false) + , m_renderSyncRequested(false) + , m_sharedObject(new RenderQmlToTextureSharedObject(this)) + , m_renderOnce(false) + , m_backendInitialized(false) +{ + setFormat(QSurfaceFormat::defaultFormat()); + + m_sharedObject->m_surface = new QOffscreenSurface; + m_sharedObject->m_surface->setFormat(QSurfaceFormat::defaultFormat()); + m_sharedObject->m_surface->create(); + + // Create render control + m_sharedObject->m_renderControl = new RenderControl(this); + + // Create window to render the QML with + m_sharedObject->m_quickWindow = new QQuickWindow(m_sharedObject->m_renderControl); + m_sharedObject->m_quickWindow->setClearBeforeRendering(true); + m_sharedObject->m_quickWindow->setDefaultAlphaBuffer(true); + + // Create a QML engine. + m_qmlEngine = new QQmlEngine; + if (!m_qmlEngine->incubationController()) + m_qmlEngine->setIncubationController(m_sharedObject->m_quickWindow->incubationController()); + + connect(m_sharedObject->m_renderControl, &QQuickRenderControl::renderRequested, + this, &RenderQmlToTextureManager::requestRender); + connect(m_sharedObject->m_renderControl, &QQuickRenderControl::sceneChanged, + this, &RenderQmlToTextureManager::requestRenderSync); +} + +RenderQmlToTextureManager::~RenderQmlToTextureManager() +{ + m_sharedObject = nullptr; +} + +void RenderQmlToTextureManager::requestRender() +{ + // Don't request render until the backend is initialized. + if (m_sharedObject->canRender()) { + if (!m_requested) { + m_requested = true; + QCoreApplication::postEvent(this, new QEvent(RENDER)); + } + } +} + +void RenderQmlToTextureManager::requestRenderSync() +{ + // Don't request render until the backed is initialized. + if (m_sharedObject->canRender()) { + if (!m_requested) { + m_requested = true; + QCoreApplication::postEvent(this, new QEvent(RENDERSYNC)); + } + } else { + m_renderSyncRequested = true; + } +} + +void RenderQmlToTextureManager::startIfInitialized() +{ + if (!m_initialized) { + if (m_backendInitialized && m_source.isValid()) { + m_qmlComponent = new QQmlComponent(m_qmlEngine, m_source); + if (m_qmlComponent->isLoading()) { + connect(m_qmlComponent, &QQmlComponent::statusChanged, + this, &RenderQmlToTextureManager::run); + } else { + run(); + } + } + } +} + +void RenderQmlToTextureManager::stopAndClean() +{ + if (m_sharedObject->isInitialized()) { + QMutexLocker lock(&m_sharedObject->m_mutex); + m_sharedObject->requestQuit(); + m_sharedObject->m_renderThread->wait(); + m_sharedObject->cleanup(); + delete m_qmlEngine; + delete m_qmlComponent; + m_qmlEngine = nullptr; + m_qmlComponent = nullptr; + } +} + +void RenderQmlToTextureManager::run() +{ + disconnect(m_qmlComponent, &QQmlComponent::statusChanged, this, &RenderQmlToTextureManager::run); + + if (m_qmlComponent->isError()) { + QList<QQmlError> errorList = m_qmlComponent->errors(); + for (const QQmlError &error: errorList) + qWarning() << error.url() << error.line() << error; + return; + } + + QObject *rootObject = m_qmlComponent->create(); + if (m_qmlComponent->isError()) { + QList<QQmlError> errorList = m_qmlComponent->errors(); + for (const QQmlError &error: errorList) + qWarning() << error.url() << error.line() << error; + return; + } + + m_rootItem = qobject_cast<QQuickItem *>(rootObject); + if (!m_rootItem) { + qWarning("QRenderQmlToTexture: Root item is not a QQuickItem."); + delete rootObject; + return; + } + + // The root item is ready. Associate it with the window. + m_rootItem->setParentItem(m_sharedObject->m_quickWindow->contentItem()); + + // Update window size. + updateSizes(); + + m_initialized = true; + m_sharedObject->setInitialized(); + + emit onLoadedChanged(); +} + +void RenderQmlToTextureManager::updateSizes() +{ + const int width = m_rootItem->width(); + const int height = m_rootItem->height(); + if (width == 0 || height == 0) { + qWarning() << "QRenderQmlToTexture: Root item size not set."; + return; + } + resize(width, height); + m_sharedObject->m_quickWindow->setGeometry(0, 0, width, height); +} + +void RenderQmlToTextureManager::setTexture(QAbstractTexture *texture) +{ + m_texture = texture; + startIfInitialized(); +} + +void RenderQmlToTextureManager::setSource(const QUrl &url) +{ + m_source = url; + startIfInitialized(); +} + +bool RenderQmlToTextureManager::event(QEvent *e) +{ + switch (e->type()) { + + case RENDER: { + // just render request, don't need to call sync in render thread + QMutexLocker lock(&m_sharedObject->m_mutex); + m_sharedObject->requestRender(false); + + Qt3DCore::QPropertyUpdatedChangePtr change(new Qt3DCore::QPropertyUpdatedChange(m_priv->m_id)); + change->setPropertyName("dirty"); + change->setValue(QVariant::fromValue(true)); + m_priv->notifyObservers(change); + + m_requested = false; + return true; + } + + case RENDERSYNC: { + // sync and render request, main and render threads must be synchronized + if (!m_sharedObject->isQuit()) + doRenderSync(); + m_requested = false; + return true; + } + + case PREPARE: { + m_sharedObject->m_renderControl->prepareThread(m_sharedObject->m_renderThread); + m_sharedObject->setPrepared(); + + if (m_renderSyncRequested) { + if (!m_requested) { + m_requested = true; + QCoreApplication::postEvent(this, new QEvent(RENDERSYNC)); + } + m_renderSyncRequested = false; + } + return true; + } + + case INITIALIZED: { + // backend is initialized, start the qml + m_backendInitialized = true; + startIfInitialized(); + return true; + } + + case RENDERED: { + // render is done, excellent, now clean anything not needed anymore. + stopAndClean(); + return true; + } + + default: + break; + } + return QWindow::event(e); +} + +void RenderQmlToTextureManager::doRenderSync() +{ + QMutexLocker lock(&m_sharedObject->m_mutex); + + m_sharedObject->requestRender(true); + m_sharedObject->m_renderControl->polishItems(); + + Qt3DCore::QPropertyUpdatedChangePtr change(new Qt3DCore::QPropertyUpdatedChange(m_priv->m_id)); + + change->setPropertyName("dirty"); + change->setValue(QVariant::fromValue(true)); + m_priv->notifyObservers(change); + + // begin waiting render thread + m_sharedObject->waitRender(); + m_requested = false; +} + +void RenderQmlToTextureManager::cleanup() +{ + stopAndClean(); +} + + +QRenderQmlToTexturePrivate::QRenderQmlToTexturePrivate() + : QFrameGraphNodePrivate() + , m_renderManager(new RenderQmlToTextureManager(this)) +{ +} + +QRenderQmlToTexturePrivate::~QRenderQmlToTexturePrivate() +{ + m_renderManager->cleanup(); + delete m_renderManager; +} + + +RenderQmlToTextureSharedObject *QRenderQmlToTexturePrivate::getSharedObject(QRenderQmlToTexture *rqtt) +{ + return rqtt->d_func()->m_renderManager->m_sharedObject.data(); +} + + +/*! + The constructor creates an instance with the specified \a parent. + */ +QRenderQmlToTexture::QRenderQmlToTexture(Qt3DCore::QNode *parent) + : QFrameGraphNode(*new QRenderQmlToTexturePrivate, parent) +{ + Q_D(QRenderQmlToTexture); + connect(d->m_renderManager, &RenderQmlToTextureManager::onLoadedChanged, + this, &QRenderQmlToTexture::sourceLoaded); +} + +/*! + Destructor. + */ +QRenderQmlToTexture::~QRenderQmlToTexture() +{ +} + +bool QRenderQmlToTexture::loaded() const +{ + Q_D(const QRenderQmlToTexture); + return d->m_renderManager->m_initialized; +} + +/*! + \property QRenderQmlToTexture::source + \brief Specifies the url for the qml. + * + This property specifies the url to the qml being rendered to the texture. + The source must specify QQuickItem as a root. The item must specify width + and height. The rendered qml is scaled to the texture size. + The property can not be changed after the rendering has been initialized. + */ +QUrl QRenderQmlToTexture::source() const +{ + Q_D(const QRenderQmlToTexture); + return d->m_renderManager->m_source; +} + +void QRenderQmlToTexture::setSource(const QUrl &url) +{ + Q_D(QRenderQmlToTexture); + if (d->m_renderManager->m_initialized) { + qWarning() << "Unable to set source after initialization."; + return; + } + d->m_renderManager->setSource(url); + emit sourceChanged(url); +} + +/*! + \property QRenderQmlToTexture::renderOnce + \brief Property to specify if the texture will be rendered only once. + * + This property specifies that the texture will be rendered only one time. + Once the rendering has been done, resources reserved for rendering will be + released and the QRenderQmlToTexture will become unusable. + If set to false, which is the default, the rendering is continuous. + */ +bool QRenderQmlToTexture::renderOnce() const +{ + Q_D(const QRenderQmlToTexture); + return d->m_renderManager->m_renderOnce; +} + +void QRenderQmlToTexture::setRenderOnce(bool once) +{ + Q_D(const QRenderQmlToTexture); + if (d->m_renderManager->m_renderOnce != once) { + d->m_renderManager->m_renderOnce = once; + emit renderOnceChanged(once); + } +} + +/*! + \property QRenderQmlToTexture::texture + \brief The texture being rendered to. + * + This property specifies the texture being rendered to. Once the texture has been + set and the rendering begins, the texture can not be changed anymore. + */ +QAbstractTexture *QRenderQmlToTexture::texture() const +{ + Q_D(const QRenderQmlToTexture); + return d->m_renderManager->m_texture; +} + +void QRenderQmlToTexture::setTexture(QAbstractTexture *texture) +{ + Q_D(QRenderQmlToTexture); + if (d->m_renderManager->m_initialized) { + qWarning() << "Unable to set texture after initialization."; + return; + } + if (d->m_renderManager->m_texture != texture) { + if (d->m_renderManager->m_texture) + QObject::disconnect(d->m_textureDestroyedConnection); + if (texture && !texture->parent()) + texture->setParent(this); + d->m_renderManager->setTexture(texture); + if (texture) + d->m_textureDestroyedConnection + = QObject::connect(texture, &QAbstractTexture::destroyed, + this, &QRenderQmlToTexture::textureDestroyed); + emit textureChanged(texture); + } +} + +void QRenderQmlToTexture::textureDestroyed(QObject *object) +{ + Q_D(QRenderQmlToTexture); + Q_UNUSED(object); + d->m_renderManager->setTexture(nullptr); +} + +Qt3DCore::QNodeCreatedChangeBasePtr QRenderQmlToTexture::createNodeCreationChange() const +{ + auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QRenderQmlToTextureData>::create(this); + auto &data = creationChange->data; + Q_D(const QRenderQmlToTexture); + data.renderOnce = d->m_renderManager->m_renderOnce; + data.textureId = d->m_renderManager->m_texture + ? d->m_renderManager->m_texture->id() : Qt3DCore::QNodeId(); + data.sharedObject = d->m_renderManager->m_sharedObject; + return creationChange; +} + +/*! + \internal + */ +void QRenderQmlToTexture::sourceLoaded() +{ + emit loadedChanged(true); +} + +} // namespace Qt3DRender + +QT_END_NAMESPACE diff --git a/src/render/framegraph/qrenderqmltotexture.h b/src/render/framegraph/qrenderqmltotexture.h new file mode 100644 index 000000000..044e4e721 --- /dev/null +++ b/src/render/framegraph/qrenderqmltotexture.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DRENDER_QRENDERQMLTOTEXTURE_H +#define QT3DRENDER_QRENDERQMLTOTEXTURE_H + +#include <QtCore/QUrl> +#include <QtCore/QEvent> + +#include <Qt3DRender/qframegraphnode.h> +#include <Qt3DRender/qabstracttexture.h> +#include <Qt3DRender/qt3drender_global.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +class QRenderQmlToTexturePrivate; + +class QT3DRENDERSHARED_EXPORT QRenderQmlToTexture : public Qt3DRender::QFrameGraphNode +{ + Q_OBJECT + + Q_PROPERTY(Qt3DRender::QAbstractTexture *texture READ texture WRITE setTexture NOTIFY textureChanged) + Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(bool renderOnce READ renderOnce WRITE setRenderOnce NOTIFY renderOnceChanged) + Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged) + +public: + explicit QRenderQmlToTexture(Qt3DCore::QNode *parent = nullptr); + ~QRenderQmlToTexture(); + + QUrl source() const; + QAbstractTexture *texture() const; + bool loaded() const; + bool renderOnce() const; + +public Q_SLOTS: + void setSource(const QUrl &url); + void setTexture(QAbstractTexture *texture); + void setRenderOnce(bool once); + +Q_SIGNALS: + void sourceChanged(const QUrl &url); + void textureChanged(QAbstractTexture *texture); + void loadedChanged(bool loaded); + void renderOnceChanged(bool once); + +protected: + Q_DECLARE_PRIVATE(QRenderQmlToTexture) + +private: + Qt3DCore::QNodeCreatedChangeBasePtr createNodeCreationChange() const Q_DECL_OVERRIDE; + void textureDestroyed(QObject *object); + + void sourceLoaded(); +}; + +} // namespace Qt3DRender + +QT_END_NAMESPACE + +#endif // QT3DRENDER_QRENDERQMLTOTEXTURE_H diff --git a/src/render/framegraph/qrenderqmltotexture_p.h b/src/render/framegraph/qrenderqmltotexture_p.h new file mode 100644 index 000000000..f9acf5368 --- /dev/null +++ b/src/render/framegraph/qrenderqmltotexture_p.h @@ -0,0 +1,212 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DRENDER_QRENDERQMLTOTEXTURE_P_H +#define QT3DRENDER_QRENDERQMLTOTEXTURE_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of other Qt classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <private/qobject_p.h> +#include <private/qframegraphnode_p.h> + +#include <Qt3DRender/qrenderqmltotexture.h> +#include <Qt3DRender/QAbstractTexture> + +#include <QtQml/QQmlEngine> +#include <QtQml/QQmlComponent> +#include <QtQuick/QQuickItem> +#include <QtQuick/QQuickWindow> +#include <QtQuick/QQuickRenderControl> +#include <QtGui/QOffscreenSurface> +#include <QtCore/QCoreApplication> +#include <QtCore/QWaitCondition> +#include <QtCore/QThread> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +class QRenderQmlToTexture; +class RenderQmlToTextureManager; + +// render thread -> render thread +static const QEvent::Type INITIALIZE = QEvent::Type(QEvent::User + 1); + +// main thread -> main thread, render thread +static const QEvent::Type RENDER = QEvent::Type(QEvent::User + 2); + +// main thread -> main thread +static const QEvent::Type RENDERSYNC = QEvent::Type(QEvent::User + 3); + +// render thread -> main thread +static const QEvent::Type PREPARE = QEvent::Type(QEvent::User + 4); +static const QEvent::Type INITIALIZED = QEvent::Type(QEvent::User + 5); +static const QEvent::Type RENDERED = QEvent::Type(QEvent::User + 6); + +// main thread -> render thread +static const QEvent::Type QUIT = QEvent::Type(QEvent::User + 7); + +class Q_AUTOTEST_EXPORT RenderQmlToTextureSharedObject +{ +public: + RenderQmlToTextureSharedObject(RenderQmlToTextureManager *manager); + ~RenderQmlToTextureSharedObject(); + + QQuickRenderControl *m_renderControl; + QQuickWindow *m_quickWindow; + RenderQmlToTextureManager *m_renderManager; + QOffscreenSurface *m_surface; + + QThread *m_renderThread; + QObject *m_renderObject; + + QWaitCondition m_cond; + QMutex m_mutex; + + bool isInitialized() const; + void setInitialized(); + + void requestQuit(); + bool isQuit() const; + + void requestRender(bool sync); + + bool isSyncRequested() const; + void clearSyncRequest(); + + void waitRender(); + void wakeWaiting(); + + bool isPrepared() const; + void setPrepared(); + + void disallowRender(); + bool canRender() const; + + void cleanup(); + +private: + + bool m_disallowed; + bool m_quit; + bool m_requestSync; + bool m_requestRender; + bool m_prepared; + bool m_initialized; +}; + +typedef QSharedPointer<RenderQmlToTextureSharedObject> RenderQmlToTextureSharedObjectPtr; + +class Q_AUTOTEST_EXPORT QRenderQmlToTexturePrivate : public QFrameGraphNodePrivate +{ +public: + Q_DECLARE_PUBLIC(QRenderQmlToTexture) + + QRenderQmlToTexturePrivate(); + ~QRenderQmlToTexturePrivate(); + + static RenderQmlToTextureSharedObject *getSharedObject(QRenderQmlToTexture *rqtt); + + RenderQmlToTextureManager *m_renderManager; + QMetaObject::Connection m_textureDestroyedConnection; +}; + +struct QRenderQmlToTextureData +{ + bool renderOnce; + Qt3DCore::QNodeId textureId; + RenderQmlToTextureSharedObjectPtr sharedObject; +}; + + +class RenderQmlToTextureManager : public QWindow +{ + Q_OBJECT +public: + RenderQmlToTextureManager(QRenderQmlToTexturePrivate *priv); + ~RenderQmlToTextureManager(); + + QQmlEngine *m_qmlEngine; + QQmlComponent *m_qmlComponent; + QQuickItem *m_rootItem; + QRenderQmlToTexturePrivate *m_priv; + QSharedPointer<RenderQmlToTextureSharedObject> m_sharedObject; + + QAbstractTexture *m_texture; + QUrl m_source; + Qt3DCore::QNodeId m_id; + + bool m_requested; + bool m_initialized; + bool m_renderSyncRequested; + bool m_renderOnce; + bool m_backendInitialized; + + void requestRender(); + void requestRenderSync(); + void doRenderSync(); + void startIfInitialized(); + void stopAndClean(); + void run(); + void updateSizes(); + + void setTexture(QAbstractTexture *texture); + void setSource(const QUrl &url); + + bool event(QEvent *e) Q_DECL_OVERRIDE; + + Q_SIGNAL void onLoadedChanged(); + + void cleanup(); +}; + + +} // namespace Qt3DRender + +QT_END_NAMESPACE + + +#endif // QT3DRENDER_QRENDERQMLTOTEXTURE_P_H diff --git a/src/render/framegraph/renderqmltotexture.cpp b/src/render/framegraph/renderqmltotexture.cpp new file mode 100644 index 000000000..91f8639c3 --- /dev/null +++ b/src/render/framegraph/renderqmltotexture.cpp @@ -0,0 +1,304 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <Qt3DRender/qrenderqmltotexture.h> +#include <Qt3DRender/private/qrenderqmltotexture_p.h> +#include <Qt3DRender/private/renderqmltotexture_p.h> +#include <Qt3DRender/private/graphicscontext_p.h> +#include <Qt3DRender/private/texture_p.h> +#include <Qt3DRender/private/nodemanagers_p.h> +#include <Qt3DCore/qpropertyupdatedchange.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +namespace Render { + +RenderQmlEventHandler::RenderQmlEventHandler(RenderQmlToTexture *node) + : QObject() + , m_node(node) +{ +} + +// Event handler for the RenderQmlToTexture::renderThread +bool RenderQmlEventHandler::event(QEvent *e) +{ + switch (e->type()) { + + case RENDER: { + m_node->render(); + return true; + } + + case INITIALIZE: { + m_node->initializeRender(); + return true; + } + + case QUIT: { + m_node->cleanup(); + return true; + } + + default: + break; + } + return QObject::event(e); +} + +RenderQmlToTexture::RenderQmlToTexture() + : FrameGraphNode(FrameGraphNode::InvalidNodeType) + , m_context(nullptr) + , m_sharedObject(nullptr) + , m_renderThread(nullptr) + , m_graphicsContext(nullptr) + , m_texture(nullptr) + , m_initialized(false) + , m_renderInitialized(false) + , m_renderOnce(false) +{ + +} + +RenderQmlToTexture::~RenderQmlToTexture() +{ + // this gets called from aspect thread. Wait for the render thread then delete it. + if (m_renderThread) { + m_renderThread->wait(1000); + delete m_renderThread; + } +} + +void RenderQmlToTexture::setTexture(Qt3DCore::QNodeId textureId) +{ + m_textureId = textureId; + attach(); + checkInitialized(); +} + +void RenderQmlToTexture::checkInitialized() +{ + if (!m_initialized && m_textureId != Qt3DCore::QNodeId()) { + + // Create render thread + m_renderThread = new QThread(); + m_renderThread->setObjectName(QStringLiteral("RenderQmlToTexture::renderThread")); + m_sharedObject->m_renderThread = m_renderThread; + + // Create event handler for the render thread + m_sharedObject->m_renderObject = new RenderQmlEventHandler(this); + m_sharedObject->m_renderObject->moveToThread(m_sharedObject->m_renderThread); + m_sharedObject->m_renderThread->start(); + + // Notify main thread we have been initialized + QCoreApplication::postEvent(m_sharedObject->m_renderManager, new QEvent(INITIALIZED)); + + // Initialize render thread + QCoreApplication::postEvent(m_sharedObject->m_renderObject, new QEvent(INITIALIZE)); + + m_initialized = true; + } +} + +void RenderQmlToTexture::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) +{ + const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QRenderQmlToTextureData>>(change); + const auto &data = typedChange->data; + m_renderOnce = m_renderOnce; + setSharedObject(data.sharedObject); + setTexture(data.textureId); +} + +void RenderQmlToTexture::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) +{ + if (e->type() == Qt3DCore::PropertyUpdated) { + Qt3DCore::QPropertyUpdatedChangePtr propertyChange + = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e); + if (propertyChange->propertyName() == QByteArrayLiteral("enabled")) + setEnabled(propertyChange->value().toBool()); + else if (propertyChange->propertyName() == QByteArrayLiteral("dirty")) { + // sent to trigger backend update when the texture gets rendered + // so do nothing here + } + else if (propertyChange->propertyName() == QByteArrayLiteral("renderOnce")) + m_renderOnce = propertyChange->value().toBool(); + else if (propertyChange->propertyName() == QByteArrayLiteral("texture")) { + Qt3DCore::QNodeId textureId = propertyChange->value().value<Qt3DCore::QNodeId>(); + setTexture(textureId); + } + markDirty(AbstractRenderer::AllDirty); + } + FrameGraphNode::sceneChangeEvent(e); +} + +void RenderQmlToTexture::setSharedObject(Qt3DRender::RenderQmlToTextureSharedObjectPtr sharedObject) +{ + m_sharedObject = sharedObject; +} + +void RenderQmlToTexture::initializeRender() +{ + if (!m_renderInitialized) { + Qt3DRender::Render::Renderer *renderer + = static_cast<Qt3DRender::Render::Renderer *>(this->renderer()); + if (!renderer) + return; + + QSurfaceFormat format; + format.setDepthBufferSize(24); + format.setStencilBufferSize(8); + + m_context = new QOpenGLContext(); + m_context->setFormat(format); + + m_context->setShareContext(renderer->shareContext()); + m_context->create(); + + m_graphicsContext = new GraphicsContext(); + m_graphicsContext->setOpenGLContext(m_context); + m_graphicsContext->setRenderer(renderer); + + m_graphicsContext->makeCurrent(m_sharedObject->m_surface); + m_sharedObject->m_renderControl->initialize(m_context); + m_graphicsContext->doneCurrent(); + + QCoreApplication::postEvent(m_sharedObject->m_renderManager, new QEvent(PREPARE)); + m_renderInitialized = true; + } +} + +void RenderQmlToTexture::attach() +{ + m_attachments = AttachmentPack(); + Attachment attach; + attach.m_mipLevel = 0; + attach.m_textureUuid = m_textureId; + attach.m_point = QRenderTargetOutput::AttachmentPoint::Color0; + +// m_attachments.addAttachment(attach); +} + +void RenderQmlToTexture::render() +{ + if (m_initialized && m_sharedObject && this->isEnabled()) { + + QMutexLocker lock(&m_sharedObject->m_mutex); + + // Lookup backend texture + if (m_texture == nullptr) { + m_texture = renderer()->nodeManagers()->textureManager()->lookupResource(m_textureId); + if (!m_texture) { + qCDebug(Render::Framegraph) << Q_FUNC_INFO << "Texture not set"; + return; + } + } + + m_graphicsContext->makeCurrent(m_sharedObject->m_surface); + + // Don't create the OpenGL texture in this thread. + const bool canUseTexture = !m_texture->isTextureReset(); + + if (canUseTexture) { + // Activate fbo for the texture + QOpenGLTexture *glTex = m_texture->getOrCreateGLTexture(); + const QSize textureSize = QSize(glTex->width(), glTex->height()); + + // TODO: create fbo from the texture. + GLuint fbo = 0;//m_graphicsContext->activateRenderTargetForQmlRender(this, m_attachments, 0); + + if (fbo != m_sharedObject->m_quickWindow->renderTargetId()) + m_sharedObject->m_quickWindow->setRenderTarget(fbo, textureSize); + + m_texture->textureLock()->lock(); + } + // Call disallow rendering while mutex is locked + if (canUseTexture && m_renderOnce) + m_sharedObject->disallowRender(); + + // Need to call sync even if the texture is not in use + if (m_sharedObject->isSyncRequested()) { + + m_sharedObject->clearSyncRequest(); + + m_sharedObject->m_renderControl->sync(); + + // gui thread can now continue + m_sharedObject->wakeWaiting(); + lock.unlock(); + } + + if (canUseTexture) { + + // Render + m_sharedObject->m_renderControl->render(); + + // Tell main thread we are done so it can begin cleanup + if (m_renderOnce) + QCoreApplication::postEvent(m_sharedObject->m_renderManager, new QEvent(RENDERED)); + + m_sharedObject->m_quickWindow->resetOpenGLState(); + m_context->functions()->glFlush(); + m_texture->textureLock()->unlock(); + } + m_graphicsContext->doneCurrent(); + } +} + +void RenderQmlToTexture::cleanup() +{ + if (m_renderInitialized && m_initialized) { + m_context->makeCurrent(m_sharedObject->m_surface); + m_sharedObject->m_renderControl->invalidate(); + m_context->doneCurrent(); + m_sharedObject->m_renderThread->quit(); + delete m_sharedObject->m_renderObject; + m_sharedObject->m_renderObject = nullptr; + delete m_context; + m_context = nullptr; + m_sharedObject = nullptr; + delete m_graphicsContext; + m_graphicsContext = nullptr; + m_renderInitialized = false; + m_initialized = false; + } +} + +} // namespace Render + +} // namespace Qt3DRender + +QT_END_NAMESPACE diff --git a/src/render/framegraph/renderqmltotexture_p.h b/src/render/framegraph/renderqmltotexture_p.h new file mode 100644 index 000000000..49913bcd9 --- /dev/null +++ b/src/render/framegraph/renderqmltotexture_p.h @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DRENDER_RENDER_RENDERQMLTOTEXTURE_P_H +#define QT3DRENDER_RENDER_RENDERQMLTOTEXTURE_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of other Qt classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <Qt3DCore/qnodeid.h> + +#include <Qt3DRender/private/framegraphnode_p.h> +#include <Qt3DRender/private/qrenderqmltotexture_p.h> + +#include <QtCore/QCoreApplication> +#include <QtCore/QSemaphore> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +namespace Render { + +class GraphicsContext; +class RenderQmlToTexture; + +class RenderQmlEventHandler : public QObject +{ + Q_OBJECT +public: + RenderQmlEventHandler(RenderQmlToTexture *node); + bool event(QEvent *e) Q_DECL_OVERRIDE; + +private: + RenderQmlToTexture *m_node; +}; + +class Q_AUTOTEST_EXPORT RenderQmlToTexture : public FrameGraphNode +{ +public: + RenderQmlToTexture(); + ~RenderQmlToTexture(); + + void attach(); + void render(); + void initializeRender(); + void setSharedObject(RenderQmlToTextureSharedObjectPtr sharedObject); + void cleanup(); + void setTexture(Qt3DCore::QNodeId textureId); + void checkInitialized(); + + void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) Q_DECL_OVERRIDE; + void initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) Q_DECL_FINAL; + + QOpenGLContext *m_context; + GraphicsContext *m_graphicsContext; + QThread *m_renderThread; + Qt3DCore::QNodeId m_textureId; + QSharedPointer<RenderQmlToTextureSharedObject> m_sharedObject; + AttachmentPack m_attachments; + Texture *m_texture; + + bool m_initialized; + bool m_renderInitialized; + bool m_renderOnce; +}; + +} // Render + +} // Qt3DRender + +QT_END_NAMESPACE + +#endif // QT3DRENDER_RENDER_RENDERQMLTOTEXTURE_P_H |