aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/scenegraph/opengltextureinthread
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quick/scenegraph/opengltextureinthread')
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/CMakeLists.txt65
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/doc/images/opengltextureinthread-example.jpgbin0 -> 33269 bytes
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/doc/src/opengltextureinthread.qdoc36
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/error.qml59
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/main.cpp103
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/main.qml120
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/opengltextureinthread.pro24
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/textureinthread.qrc6
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/threadrenderer.cpp302
-rw-r--r--examples/quick/scenegraph/opengltextureinthread/threadrenderer.h78
10 files changed, 793 insertions, 0 deletions
diff --git a/examples/quick/scenegraph/opengltextureinthread/CMakeLists.txt b/examples/quick/scenegraph/opengltextureinthread/CMakeLists.txt
new file mode 100644
index 0000000000..daf2ef9e5f
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/CMakeLists.txt
@@ -0,0 +1,65 @@
+# Generated from opengltextureinthread.pro.
+
+cmake_minimum_required(VERSION 3.14)
+project(opengltextureinthread LANGUAGES CXX)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_AUTORCC ON)
+set(CMAKE_AUTOUIC ON)
+
+if(NOT DEFINED INSTALL_EXAMPLESDIR)
+ set(INSTALL_EXAMPLESDIR "examples")
+endif()
+
+set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/scenegraph/opengltextureinthread")
+
+find_package(Qt6 COMPONENTS Core)
+find_package(Qt6 COMPONENTS Gui)
+find_package(Qt6 COMPONENTS Quick)
+
+add_qt_gui_executable(opengltextureinthread
+ ../shared/logorenderer.cpp ../shared/logorenderer.h
+ main.cpp
+ threadrenderer.cpp threadrenderer.h
+)
+target_include_directories(opengltextureinthread PUBLIC
+ ../shared
+)
+
+target_link_libraries(opengltextureinthread PUBLIC
+ Qt::Core
+ Qt::CorePrivate
+ Qt::Gui
+ Qt::GuiPrivate
+ Qt::Quick
+)
+
+
+# Resources:
+set(textureinthread_resource_files
+ "error.qml"
+ "main.qml"
+)
+
+qt6_add_resources(opengltextureinthread "textureinthread"
+ PREFIX
+ "/scenegraph/textureinthread"
+ FILES
+ ${textureinthread_resource_files}
+)
+
+install(TARGETS opengltextureinthread
+ RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
+ LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+)
+
+set_target_properties(opengltextureinthread PROPERTIES
+ QT_QML_MODULE_INSTALL_QMLTYPES TRUE
+ QT_QML_MODULE_VERSION 1.0
+ QT_QML_MODULE_URI SceneGraphRendering
+)
+
+qt6_qml_type_registration(opengltextureinthread)
diff --git a/examples/quick/scenegraph/opengltextureinthread/doc/images/opengltextureinthread-example.jpg b/examples/quick/scenegraph/opengltextureinthread/doc/images/opengltextureinthread-example.jpg
new file mode 100644
index 0000000000..59134e07c4
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/doc/images/opengltextureinthread-example.jpg
Binary files differ
diff --git a/examples/quick/scenegraph/opengltextureinthread/doc/src/opengltextureinthread.qdoc b/examples/quick/scenegraph/opengltextureinthread/doc/src/opengltextureinthread.qdoc
new file mode 100644
index 0000000000..3c26138332
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/doc/src/opengltextureinthread.qdoc
@@ -0,0 +1,36 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:FDL$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Free Documentation License Usage
+** Alternatively, this file may be used under the terms of the GNU Free
+** Documentation License version 1.3 as published by the Free Software
+** Foundation and appearing in the file included in the packaging of
+** this file. Please review the following information to ensure
+** the GNU Free Documentation License version 1.3 requirements
+** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+/*!
+ \example scenegraph/opengltextureinthread
+ \title Scene Graph - Rendering FBOs in a thread
+ \ingroup qtquickexamples
+
+ \brief Shows how to use FramebufferObjects in a thread together with Qt Quick.
+
+ \image opengltextureinthread-example.jpg
+ */
diff --git a/examples/quick/scenegraph/opengltextureinthread/error.qml b/examples/quick/scenegraph/opengltextureinthread/error.qml
new file mode 100644
index 0000000000..6c300af808
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/error.qml
@@ -0,0 +1,59 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+
+Text {
+ width: 400
+ height: 100
+ text: 'Platform does not support threaded OpenGL needed by this example.'
+ verticalAlignment: Text.AlignVCenter
+ horizontalAlignment: Text.AlignHCenter
+}
diff --git a/examples/quick/scenegraph/opengltextureinthread/main.cpp b/examples/quick/scenegraph/opengltextureinthread/main.cpp
new file mode 100644
index 0000000000..08bf8ff66a
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/main.cpp
@@ -0,0 +1,103 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/QThread>
+
+#include <QGuiApplication>
+
+#include <QtGui/private/qguiapplication_p.h>
+#include <QtGui/qpa/qplatformintegration.h>
+
+#include <QtQuick/QQuickView>
+
+#include "threadrenderer.h"
+
+int main(int argc, char **argv)
+{
+ QGuiApplication app(argc, argv);
+
+ // this example is only compatible with OpenGL
+ QQuickWindow::setSceneGraphBackend(QSGRendererInterface::OpenGLRhi);
+
+ if (!QGuiApplicationPrivate::platform_integration->hasCapability(QPlatformIntegration::ThreadedOpenGL)) {
+ QQuickView view;
+ view.setSource(QUrl("qrc:///scenegraph/textureinthread/error.qml"));
+ view.show();
+ return app.exec();
+ }
+
+ int execReturn = 0;
+
+ {
+ QQuickView view;
+
+ // Rendering in a thread introduces a slightly more complicated cleanup
+ // so we ensure that no cleanup of graphics resources happen until the
+ // application is shutting down.
+ view.setPersistentOpenGLContext(true);
+ view.setPersistentSceneGraph(true);
+
+ view.setResizeMode(QQuickView::SizeRootObjectToView);
+ view.setSource(QUrl("qrc:///scenegraph/textureinthread/main.qml"));
+ view.show();
+
+ execReturn = app.exec();
+ }
+
+ // As the render threads make use of our QGuiApplication object
+ // to clean up gracefully, wait for them to finish before
+ // QGuiApp is taken off the heap.
+ for (QThread *t : qAsConst(ThreadRenderer::threads)) {
+ t->wait();
+ delete t;
+ }
+
+ return execReturn;
+}
diff --git a/examples/quick/scenegraph/opengltextureinthread/main.qml b/examples/quick/scenegraph/opengltextureinthread/main.qml
new file mode 100644
index 0000000000..51a3744f76
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/main.qml
@@ -0,0 +1,120 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+
+import SceneGraphRendering 1.0
+
+Rectangle {
+ width: 400
+ height: 400
+ color: "lightGray"
+
+ Renderer {
+ id: renderer
+ anchors.fill: parent
+ anchors.margins: 10
+
+ // The transform is just to show something interesting..
+ transform: [
+ Rotation { id: rotation; axis.x: 0; axis.z: 0; axis.y: 1; angle: 0; origin.x: renderer.width / 2; origin.y: renderer.height / 2; },
+ Translate { id: txOut; x: -renderer.width / 2; y: -renderer.height / 2 },
+ Scale { id: scale; },
+ Translate { id: txIn; x: renderer.width / 2; y: renderer.height / 2 }
+ ]
+
+ Behavior on opacity { NumberAnimation { duration: 500 } }
+ opacity: 0
+ Component.onCompleted: renderer.opacity = 1;
+ }
+
+ // Just to show something interesting
+ SequentialAnimation {
+ PauseAnimation { duration: 5000 }
+ ParallelAnimation {
+ NumberAnimation { target: scale; property: "xScale"; to: 0.6; duration: 1000; easing.type: Easing.InOutBack }
+ NumberAnimation { target: scale; property: "yScale"; to: 0.6; duration: 1000; easing.type: Easing.InOutBack }
+ }
+ NumberAnimation { target: rotation; property: "angle"; to: 80; duration: 1000; easing.type: Easing.InOutCubic }
+ NumberAnimation { target: rotation; property: "angle"; to: -80; duration: 1000; easing.type: Easing.InOutCubic }
+ NumberAnimation { target: rotation; property: "angle"; to: 0; duration: 1000; easing.type: Easing.InOutCubic }
+ NumberAnimation { target: renderer; property: "opacity"; to: 0.5; duration: 1000; easing.type: Easing.InOutCubic }
+ PauseAnimation { duration: 1000 }
+ NumberAnimation { target: renderer; property: "opacity"; to: 0.8; duration: 1000; easing.type: Easing.InOutCubic }
+ ParallelAnimation {
+ NumberAnimation { target: scale; property: "xScale"; to: 1; duration: 1000; easing.type: Easing.InOutBack }
+ NumberAnimation { target: scale; property: "yScale"; to: 1; duration: 1000; easing.type: Easing.InOutBack }
+ }
+ running: true
+ loops: Animation.Infinite
+ }
+
+ Rectangle {
+ id: labelFrame
+ anchors.margins: -10
+ radius: 5
+ color: "white"
+ border.color: "black"
+ opacity: 0.8
+ anchors.fill: label
+ }
+
+ Text {
+ id: label
+ anchors.bottom: renderer.bottom
+ anchors.left: renderer.left
+ anchors.right: renderer.right
+ anchors.margins: 20
+ wrapMode: Text.WordWrap
+ text: "The blue rectangle with the vintage 'Q' is an FBO, rendered by the application in a dedicated background thread. The background thread juggles two FBOs, one that is being rendered to and one for displaying. The texture to display is posted to the scene graph and displayed using a QSGSimpleTextureNode."
+ }
+
+
+}
diff --git a/examples/quick/scenegraph/opengltextureinthread/opengltextureinthread.pro b/examples/quick/scenegraph/opengltextureinthread/opengltextureinthread.pro
new file mode 100644
index 0000000000..d07149a2f9
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/opengltextureinthread.pro
@@ -0,0 +1,24 @@
+QT += quick
+
+# To make threaded gl check...
+QT += core-private gui-private
+
+CONFIG += qmltypes
+QML_IMPORT_NAME = SceneGraphRendering
+QML_IMPORT_MAJOR_VERSION = 1
+
+HEADERS += threadrenderer.h
+SOURCES += threadrenderer.cpp main.cpp
+
+INCLUDEPATH += ../shared
+HEADERS += ../shared/logorenderer.h
+SOURCES += ../shared/logorenderer.cpp
+
+RESOURCES += textureinthread.qrc
+
+target.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/opengltextureinthread
+INSTALLS += target
+
+OTHER_FILES += \
+ main.qml \
+ error.qml
diff --git a/examples/quick/scenegraph/opengltextureinthread/textureinthread.qrc b/examples/quick/scenegraph/opengltextureinthread/textureinthread.qrc
new file mode 100644
index 0000000000..66eb53b736
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/textureinthread.qrc
@@ -0,0 +1,6 @@
+<RCC>
+ <qresource prefix="/scenegraph/textureinthread">
+ <file>main.qml</file>
+ <file>error.qml</file>
+ </qresource>
+</RCC>
diff --git a/examples/quick/scenegraph/opengltextureinthread/threadrenderer.cpp b/examples/quick/scenegraph/opengltextureinthread/threadrenderer.cpp
new file mode 100644
index 0000000000..8c07785926
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/threadrenderer.cpp
@@ -0,0 +1,302 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "threadrenderer.h"
+#include "logorenderer.h"
+
+#include <QtCore/QMutex>
+#include <QtCore/QThread>
+
+#include <QOpenGLContext>
+#include <QOpenGLFramebufferObject>
+#include <QtGui/QGuiApplication>
+#include <QtGui/QOffscreenSurface>
+
+#include <QtQuick/QQuickWindow>
+#include <qsgsimpletexturenode.h>
+
+QList<QThread *> ThreadRenderer::threads;
+
+/*
+ * The render thread shares a context with the scene graph and will
+ * render into two separate FBOs, one to use for display and one
+ * to use for rendering
+ */
+class RenderThread : public QThread
+{
+ Q_OBJECT
+public:
+ RenderThread(const QSize &size)
+ : surface(nullptr)
+ , context(nullptr)
+ , m_renderFbo(nullptr)
+ , m_displayFbo(nullptr)
+ , m_logoRenderer(nullptr)
+ , m_size(size)
+ {
+ ThreadRenderer::threads << this;
+ }
+
+ QOffscreenSurface *surface;
+ QOpenGLContext *context;
+
+public slots:
+ void renderNext()
+ {
+ context->makeCurrent(surface);
+
+ if (!m_renderFbo) {
+ // Initialize the buffers and renderer
+ QOpenGLFramebufferObjectFormat format;
+ format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
+ m_renderFbo = new QOpenGLFramebufferObject(m_size, format);
+ m_displayFbo = new QOpenGLFramebufferObject(m_size, format);
+ m_logoRenderer = new LogoRenderer();
+ m_logoRenderer->initialize();
+ }
+
+ m_renderFbo->bind();
+ context->functions()->glViewport(0, 0, m_size.width(), m_size.height());
+
+ m_logoRenderer->render();
+
+ // We need to flush the contents to the FBO before posting
+ // the texture to the other thread, otherwise, we might
+ // get unexpected results.
+ context->functions()->glFlush();
+
+ m_renderFbo->bindDefault();
+ qSwap(m_renderFbo, m_displayFbo);
+
+ emit textureReady(m_displayFbo->texture(), m_size);
+ }
+
+ void shutDown()
+ {
+ context->makeCurrent(surface);
+ delete m_renderFbo;
+ delete m_displayFbo;
+ delete m_logoRenderer;
+ context->doneCurrent();
+ delete context;
+
+ // schedule this to be deleted only after we're done cleaning up
+ surface->deleteLater();
+
+ // Stop event processing, move the thread to GUI and make sure it is deleted.
+ exit();
+ moveToThread(QGuiApplication::instance()->thread());
+ }
+
+signals:
+ void textureReady(int id, const QSize &size);
+
+private:
+ QOpenGLFramebufferObject *m_renderFbo;
+ QOpenGLFramebufferObject *m_displayFbo;
+
+ LogoRenderer *m_logoRenderer;
+ QSize m_size;
+};
+
+
+
+class TextureNode : public QObject, public QSGSimpleTextureNode
+{
+ Q_OBJECT
+
+public:
+ TextureNode(QQuickWindow *window)
+ : m_id(0)
+ , m_size(0, 0)
+ , m_texture(nullptr)
+ , m_window(window)
+ {
+ // Our texture node must have a texture, so use the default 0 texture.
+ GLuint id = 0;
+ m_texture = m_window->createTextureFromNativeObject(QQuickWindow::NativeObjectTexture, &id, 0, QSize(1, 1));
+ setTexture(m_texture);
+ setFiltering(QSGTexture::Linear);
+ }
+
+ ~TextureNode() override
+ {
+ delete m_texture;
+ }
+
+signals:
+ void textureInUse();
+ void pendingNewTexture();
+
+public slots:
+
+ // This function gets called on the FBO rendering thread and will store the
+ // texture id and size and schedule an update on the window.
+ void newTexture(int id, const QSize &size) {
+ m_mutex.lock();
+ m_id = id;
+ m_size = size;
+ m_mutex.unlock();
+
+ // We cannot call QQuickWindow::update directly here, as this is only allowed
+ // from the rendering thread or GUI thread.
+ emit pendingNewTexture();
+ }
+
+
+ // Before the scene graph starts to render, we update to the pending texture
+ void prepareNode() {
+ m_mutex.lock();
+ int newId = m_id;
+ QSize size = m_size;
+ m_id = 0;
+ m_mutex.unlock();
+ if (newId) {
+ delete m_texture;
+ // note: include QQuickWindow::TextureHasAlphaChannel if the rendered content
+ // has alpha.
+ m_texture = m_window->createTextureFromNativeObject(QQuickWindow::NativeObjectTexture, &newId, 0, QSize(1, 1));
+ setTexture(m_texture);
+
+ markDirty(DirtyMaterial);
+
+ // This will notify the rendering thread that the texture is now being rendered
+ // and it can start rendering to the other one.
+ emit textureInUse();
+ }
+ }
+
+private:
+
+ int m_id;
+ QSize m_size;
+
+ QMutex m_mutex;
+
+ QSGTexture *m_texture;
+ QQuickWindow *m_window;
+};
+
+ThreadRenderer::ThreadRenderer()
+ : m_renderThread(nullptr)
+{
+ setFlag(ItemHasContents, true);
+ m_renderThread = new RenderThread(QSize(512, 512));
+}
+
+void ThreadRenderer::ready()
+{
+ m_renderThread->surface = new QOffscreenSurface();
+ m_renderThread->surface->setFormat(m_renderThread->context->format());
+ m_renderThread->surface->create();
+
+ m_renderThread->moveToThread(m_renderThread);
+
+ connect(window(), &QQuickWindow::sceneGraphInvalidated, m_renderThread, &RenderThread::shutDown, Qt::QueuedConnection);
+
+ m_renderThread->start();
+ update();
+}
+
+QSGNode *ThreadRenderer::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
+{
+ TextureNode *node = static_cast<TextureNode *>(oldNode);
+
+ if (!m_renderThread->context) {
+ QOpenGLContext *current = window()->openglContext();
+ // Some GL implementations requres that the currently bound context is
+ // made non-current before we set up sharing, so we doneCurrent here
+ // and makeCurrent down below while setting up our own context.
+ current->doneCurrent();
+
+ m_renderThread->context = new QOpenGLContext();
+ m_renderThread->context->setFormat(current->format());
+ m_renderThread->context->setShareContext(current);
+ m_renderThread->context->create();
+ m_renderThread->context->moveToThread(m_renderThread);
+
+ current->makeCurrent(window());
+
+ QMetaObject::invokeMethod(this, "ready");
+ return nullptr;
+ }
+
+ if (!node) {
+ node = new TextureNode(window());
+
+ /* Set up connections to get the production of FBO textures in sync with vsync on the
+ * rendering thread.
+ *
+ * When a new texture is ready on the rendering thread, we use a direct connection to
+ * the texture node to let it know a new texture can be used. The node will then
+ * emit pendingNewTexture which we bind to QQuickWindow::update to schedule a redraw.
+ *
+ * When the scene graph starts rendering the next frame, the prepareNode() function
+ * is used to update the node with the new texture. Once it completes, it emits
+ * textureInUse() which we connect to the FBO rendering thread's renderNext() to have
+ * it start producing content into its current "back buffer".
+ *
+ * This FBO rendering pipeline is throttled by vsync on the scene graph rendering thread.
+ */
+ connect(m_renderThread, &RenderThread::textureReady, node, &TextureNode::newTexture, Qt::DirectConnection);
+ connect(node, &TextureNode::pendingNewTexture, window(), &QQuickWindow::update, Qt::QueuedConnection);
+ connect(window(), &QQuickWindow::beforeRendering, node, &TextureNode::prepareNode, Qt::DirectConnection);
+ connect(node, &TextureNode::textureInUse, m_renderThread, &RenderThread::renderNext, Qt::QueuedConnection);
+
+ // Get the production of FBO textures started..
+ QMetaObject::invokeMethod(m_renderThread, "renderNext", Qt::QueuedConnection);
+ }
+
+ node->setRect(boundingRect());
+
+ return node;
+}
+
+#include "threadrenderer.moc"
diff --git a/examples/quick/scenegraph/opengltextureinthread/threadrenderer.h b/examples/quick/scenegraph/opengltextureinthread/threadrenderer.h
new file mode 100644
index 0000000000..96c00e7b2e
--- /dev/null
+++ b/examples/quick/scenegraph/opengltextureinthread/threadrenderer.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef THREADRENDERER_H
+#define THREADRENDERER_H
+
+#include <QQuickItem>
+
+class RenderThread;
+
+class ThreadRenderer : public QQuickItem
+{
+ Q_OBJECT
+ QML_NAMED_ELEMENT(Renderer)
+
+public:
+ ThreadRenderer();
+
+ static QList<QThread *> threads;
+
+public Q_SLOTS:
+ void ready();
+
+protected:
+ QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
+
+private:
+ RenderThread *m_renderThread;
+};
+
+#endif