From 6ef31dfb484321ec6f5a3ffe27fef98e7c1cd3b0 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Wed, 13 May 2020 11:22:37 +0200 Subject: Fix up the scenegraph/textureinthread example Force OpenGL, remove the ShaderEffect (it serves no purpose in this example) and remove deprecated API usage. Finally, rename the example to opengltextureinthread. Note that the example's performance is sub-par, but the same is true with direct OpenGL. Change-Id: I7eaa2349ffdb3d27bd9e8815d674e918bc709c03 Reviewed-by: Eirik Aavitsland --- examples/quick/scenegraph/.prev_CMakeLists.txt | 4 +- examples/quick/scenegraph/CMakeLists.txt | 2 +- .../opengltextureinthread/CMakeLists.txt | 65 +++++ .../doc/images/opengltextureinthread-example.jpg | Bin 0 -> 33269 bytes .../doc/src/opengltextureinthread.qdoc | 36 +++ .../scenegraph/opengltextureinthread/error.qml | 59 ++++ .../scenegraph/opengltextureinthread/main.cpp | 103 +++++++ .../scenegraph/opengltextureinthread/main.qml | 120 ++++++++ .../opengltextureinthread.pro | 24 ++ .../opengltextureinthread/textureinthread.qrc | 6 + .../opengltextureinthread/threadrenderer.cpp | 302 +++++++++++++++++++++ .../opengltextureinthread/threadrenderer.h | 78 ++++++ examples/quick/scenegraph/scenegraph.pro | 2 +- .../scenegraph/textureinthread/CMakeLists.txt | 61 ----- .../doc/images/textureinthread-example.jpg | Bin 33269 -> 0 bytes .../textureinthread/doc/src/textureinthread.qdoc | 36 --- .../quick/scenegraph/textureinthread/error.qml | 59 ---- examples/quick/scenegraph/textureinthread/main.cpp | 100 ------- examples/quick/scenegraph/textureinthread/main.qml | 146 ---------- .../scenegraph/textureinthread/textureinthread.pro | 24 -- .../scenegraph/textureinthread/textureinthread.qrc | 6 - .../scenegraph/textureinthread/threadrenderer.cpp | 301 -------------------- .../scenegraph/textureinthread/threadrenderer.h | 78 ------ 23 files changed, 796 insertions(+), 816 deletions(-) create mode 100644 examples/quick/scenegraph/opengltextureinthread/CMakeLists.txt create mode 100644 examples/quick/scenegraph/opengltextureinthread/doc/images/opengltextureinthread-example.jpg create mode 100644 examples/quick/scenegraph/opengltextureinthread/doc/src/opengltextureinthread.qdoc create mode 100644 examples/quick/scenegraph/opengltextureinthread/error.qml create mode 100644 examples/quick/scenegraph/opengltextureinthread/main.cpp create mode 100644 examples/quick/scenegraph/opengltextureinthread/main.qml create mode 100644 examples/quick/scenegraph/opengltextureinthread/opengltextureinthread.pro create mode 100644 examples/quick/scenegraph/opengltextureinthread/textureinthread.qrc create mode 100644 examples/quick/scenegraph/opengltextureinthread/threadrenderer.cpp create mode 100644 examples/quick/scenegraph/opengltextureinthread/threadrenderer.h delete mode 100644 examples/quick/scenegraph/textureinthread/CMakeLists.txt delete mode 100644 examples/quick/scenegraph/textureinthread/doc/images/textureinthread-example.jpg delete mode 100644 examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc delete mode 100644 examples/quick/scenegraph/textureinthread/error.qml delete mode 100644 examples/quick/scenegraph/textureinthread/main.cpp delete mode 100644 examples/quick/scenegraph/textureinthread/main.qml delete mode 100644 examples/quick/scenegraph/textureinthread/textureinthread.pro delete mode 100644 examples/quick/scenegraph/textureinthread/textureinthread.qrc delete mode 100644 examples/quick/scenegraph/textureinthread/threadrenderer.cpp delete mode 100644 examples/quick/scenegraph/textureinthread/threadrenderer.h (limited to 'examples/quick') diff --git a/examples/quick/scenegraph/.prev_CMakeLists.txt b/examples/quick/scenegraph/.prev_CMakeLists.txt index cf72c90b8d..4807706a43 100644 --- a/examples/quick/scenegraph/.prev_CMakeLists.txt +++ b/examples/quick/scenegraph/.prev_CMakeLists.txt @@ -5,11 +5,9 @@ add_subdirectory(rendernode) add_subdirectory(threadedanimation) if(QT_FEATURE_opengl OR QT_FEATURE_opengles2 OR QT_FEATURE_opengles3) add_subdirectory(graph) - add_subdirectory(simplematerial) - add_subdirectory(sgengine) add_subdirectory(fboitem) add_subdirectory(openglunderqml) - add_subdirectory(textureinthread) + add_subdirectory(opengltextureinthread) add_subdirectory(twotextureproviders) endif() if(IOS OR MACOS) diff --git a/examples/quick/scenegraph/CMakeLists.txt b/examples/quick/scenegraph/CMakeLists.txt index 2a5db7da54..f899f1d234 100644 --- a/examples/quick/scenegraph/CMakeLists.txt +++ b/examples/quick/scenegraph/CMakeLists.txt @@ -7,7 +7,7 @@ if(QT_FEATURE_opengl OR QT_FEATURE_opengles2 OR QT_FEATURE_opengles3) add_subdirectory(graph) add_subdirectory(fboitem) add_subdirectory(openglunderqml) - add_subdirectory(textureinthread) + add_subdirectory(opengltextureinthread) add_subdirectory(twotextureproviders) endif() if(MACOS) 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 Binary files /dev/null and b/examples/quick/scenegraph/opengltextureinthread/doc/images/opengltextureinthread-example.jpg 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 + +#include + +#include +#include + +#include + +#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 @@ + + + main.qml + error.qml + + 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 +#include + +#include +#include +#include +#include + +#include +#include + +QList 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(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 + +class RenderThread; + +class ThreadRenderer : public QQuickItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(Renderer) + +public: + ThreadRenderer(); + + static QList threads; + +public Q_SLOTS: + void ready(); + +protected: + QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *); + +private: + RenderThread *m_renderThread; +}; + +#endif diff --git a/examples/quick/scenegraph/scenegraph.pro b/examples/quick/scenegraph/scenegraph.pro index b9ce61e21d..147fa9a156 100644 --- a/examples/quick/scenegraph/scenegraph.pro +++ b/examples/quick/scenegraph/scenegraph.pro @@ -5,7 +5,7 @@ qtConfig(opengl(es1|es2)?) { graph \ fboitem \ openglunderqml \ - textureinthread \ + opengltextureinthread \ twotextureproviders } diff --git a/examples/quick/scenegraph/textureinthread/CMakeLists.txt b/examples/quick/scenegraph/textureinthread/CMakeLists.txt deleted file mode 100644 index d29a5c63f3..0000000000 --- a/examples/quick/scenegraph/textureinthread/CMakeLists.txt +++ /dev/null @@ -1,61 +0,0 @@ -# Generated from textureinthread.pro. - -cmake_minimum_required(VERSION 3.14) -project(textureinthread LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -set(INSTALL_EXAMPLEDIR "examples/quick/scenegraph/textureinthread") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Quick) - -add_qt_gui_executable(textureinthread - ../shared/logorenderer.cpp ../shared/logorenderer.h - main.cpp - threadrenderer.cpp threadrenderer.h -) -target_include_directories(textureinthread PUBLIC - ../shared -) - -target_link_libraries(textureinthread PUBLIC - Qt::Core - Qt::CorePrivate - Qt::Gui - Qt::GuiPrivate - Qt::Quick -) - - -# Resources: -set(textureinthread_resource_files - "error.qml" - "main.qml" -) - -qt6_add_resources(textureinthread "textureinthread" - PREFIX - "/scenegraph/textureinthread" - FILES - ${textureinthread_resource_files} -) - -install(TARGETS textureinthread - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) - -set_target_properties(textureinthread PROPERTIES - QT_QML_MODULE_INSTALL_QMLTYPES TRUE - QT_QML_MODULE_VERSION 1.0 - QT_QML_MODULE_URI SceneGraphRendering -) - -qt6_qml_type_registration(textureinthread) diff --git a/examples/quick/scenegraph/textureinthread/doc/images/textureinthread-example.jpg b/examples/quick/scenegraph/textureinthread/doc/images/textureinthread-example.jpg deleted file mode 100644 index 59134e07c4..0000000000 Binary files a/examples/quick/scenegraph/textureinthread/doc/images/textureinthread-example.jpg and /dev/null differ diff --git a/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc b/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc deleted file mode 100644 index b8446ec212..0000000000 --- a/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc +++ /dev/null @@ -1,36 +0,0 @@ -/**************************************************************************** -** -** 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/textureinthread - \title Scene Graph - Rendering FBOs in a thread - \ingroup qtquickexamples - - \brief Shows how to use FramebufferObjects in a thread together with Qt Quick. - - \image textureinthread-example.jpg - */ diff --git a/examples/quick/scenegraph/textureinthread/error.qml b/examples/quick/scenegraph/textureinthread/error.qml deleted file mode 100644 index 6c300af808..0000000000 --- a/examples/quick/scenegraph/textureinthread/error.qml +++ /dev/null @@ -1,59 +0,0 @@ -/**************************************************************************** -** -** 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/textureinthread/main.cpp b/examples/quick/scenegraph/textureinthread/main.cpp deleted file mode 100644 index cb00946bf7..0000000000 --- a/examples/quick/scenegraph/textureinthread/main.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** -** -** 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 - -#include - -#include -#include - -#include - -#include "threadrenderer.h" - -int main(int argc, char **argv) -{ - QGuiApplication app(argc, argv); - - 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/textureinthread/main.qml b/examples/quick/scenegraph/textureinthread/main.qml deleted file mode 100644 index eefee92f15..0000000000 --- a/examples/quick/scenegraph/textureinthread/main.qml +++ /dev/null @@ -1,146 +0,0 @@ -/**************************************************************************** -** -** 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 - -Item { - width: 400 - height: 400 - - // The checkers background - ShaderEffect { - id: tileBackground - anchors.fill: parent - - property real tileSize: 16 - property color color1: Qt.rgba(0.9, 0.9, 0.9, 1); - property color color2: Qt.rgba(0.85, 0.85, 0.85, 1); - - property size pixelSize: Qt.size(width / tileSize, height / tileSize); - - fragmentShader: - " - uniform lowp vec4 color1; - uniform lowp vec4 color2; - uniform highp vec2 pixelSize; - varying highp vec2 qt_TexCoord0; - void main() { - highp vec2 tc = sign(sin(3.14159265358979323846 * qt_TexCoord0 * pixelSize)); - if (tc.x != tc.y) - gl_FragColor = color1; - else - gl_FragColor = color2; - } - " - } - - 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/textureinthread/textureinthread.pro b/examples/quick/scenegraph/textureinthread/textureinthread.pro deleted file mode 100644 index 4d41daa7bf..0000000000 --- a/examples/quick/scenegraph/textureinthread/textureinthread.pro +++ /dev/null @@ -1,24 +0,0 @@ -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/textureinthread -INSTALLS += target - -OTHER_FILES += \ - main.qml \ - error.qml diff --git a/examples/quick/scenegraph/textureinthread/textureinthread.qrc b/examples/quick/scenegraph/textureinthread/textureinthread.qrc deleted file mode 100644 index 66eb53b736..0000000000 --- a/examples/quick/scenegraph/textureinthread/textureinthread.qrc +++ /dev/null @@ -1,6 +0,0 @@ - - - main.qml - error.qml - - diff --git a/examples/quick/scenegraph/textureinthread/threadrenderer.cpp b/examples/quick/scenegraph/textureinthread/threadrenderer.cpp deleted file mode 100644 index 5fd8037193..0000000000 --- a/examples/quick/scenegraph/textureinthread/threadrenderer.cpp +++ /dev/null @@ -1,301 +0,0 @@ -/**************************************************************************** -** -** 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 -#include - -#include -#include -#include -#include - -#include -#include - -QList 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. - m_texture = m_window->createTextureFromId(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->createTextureFromId(newId, size); - 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(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/textureinthread/threadrenderer.h b/examples/quick/scenegraph/textureinthread/threadrenderer.h deleted file mode 100644 index 96c00e7b2e..0000000000 --- a/examples/quick/scenegraph/textureinthread/threadrenderer.h +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************** -** -** 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 - -class RenderThread; - -class ThreadRenderer : public QQuickItem -{ - Q_OBJECT - QML_NAMED_ELEMENT(Renderer) - -public: - ThreadRenderer(); - - static QList threads; - -public Q_SLOTS: - void ready(); - -protected: - QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *); - -private: - RenderThread *m_renderThread; -}; - -#endif -- cgit v1.2.3