aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/scenegraph/textureinthread
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quick/scenegraph/textureinthread')
-rw-r--r--examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc2
-rw-r--r--examples/quick/scenegraph/textureinthread/error.qml49
-rw-r--r--examples/quick/scenegraph/textureinthread/main.cpp42
-rw-r--r--examples/quick/scenegraph/textureinthread/textureinthread.pro6
-rw-r--r--examples/quick/scenegraph/textureinthread/textureinthread.qrc3
-rw-r--r--examples/quick/scenegraph/textureinthread/threadrenderer.cpp69
-rw-r--r--examples/quick/scenegraph/textureinthread/threadrenderer.h6
7 files changed, 152 insertions, 25 deletions
diff --git a/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc b/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc
index 476605b268..6f4a2514f0 100644
--- a/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc
+++ b/examples/quick/scenegraph/textureinthread/doc/src/textureinthread.qdoc
@@ -26,7 +26,7 @@
****************************************************************************/
/*!
- \example quick/scenegraph/textureinthread
+ \example scenegraph/textureinthread
\title Scene Graph - Rendering FBOs in a thread
\ingroup qtquickexamples
diff --git a/examples/quick/scenegraph/textureinthread/error.qml b/examples/quick/scenegraph/textureinthread/error.qml
new file mode 100644
index 0000000000..1dbd576ef7
--- /dev/null
+++ b/examples/quick/scenegraph/textureinthread/error.qml
@@ -0,0 +1,49 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 Digia Plc and its Subsidiary(-ies) 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
index e415d254a1..801f1891b9 100644
--- a/examples/quick/scenegraph/textureinthread/main.cpp
+++ b/examples/quick/scenegraph/textureinthread/main.cpp
@@ -38,8 +38,13 @@
**
****************************************************************************/
+#include <QtCore/QThread>
+
#include <QGuiApplication>
+#include <QtGui/private/qguiapplication_p.h>
+#include <QtGui/qpa/qplatformintegration.h>
+
#include <QtQuick/QQuickView>
#include "threadrenderer.h"
@@ -48,12 +53,39 @@ 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();
+ }
+
qmlRegisterType<ThreadRenderer>("SceneGraphRendering", 1, 0, "Renderer");
+ 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();
+ }
- QQuickView view;
- view.setResizeMode(QQuickView::SizeRootObjectToView);
- view.setSource(QUrl("qrc:///scenegraph/textureinsgnode/main.qml"));
- view.show();
+ // 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.
+ foreach (QThread *t, ThreadRenderer::threads) {
+ t->wait();
+ delete t;
+ }
- return app.exec();
+ return execReturn;
}
diff --git a/examples/quick/scenegraph/textureinthread/textureinthread.pro b/examples/quick/scenegraph/textureinthread/textureinthread.pro
index b48c2a1863..7f5fbb76e9 100644
--- a/examples/quick/scenegraph/textureinthread/textureinthread.pro
+++ b/examples/quick/scenegraph/textureinthread/textureinthread.pro
@@ -1,5 +1,8 @@
QT += quick
+# To make threaded gl check...
+QT += core-private gui-private
+
HEADERS += threadrenderer.h
SOURCES += threadrenderer.cpp main.cpp
@@ -13,4 +16,5 @@ target.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/textureinthread
INSTALLS += target
OTHER_FILES += \
- main.qml
+ main.qml \
+ error.qml
diff --git a/examples/quick/scenegraph/textureinthread/textureinthread.qrc b/examples/quick/scenegraph/textureinthread/textureinthread.qrc
index 9ecf0ada1c..66eb53b736 100644
--- a/examples/quick/scenegraph/textureinthread/textureinthread.qrc
+++ b/examples/quick/scenegraph/textureinthread/textureinthread.qrc
@@ -1,5 +1,6 @@
<RCC>
- <qresource prefix="/scenegraph/textureinsgnode">
+ <qresource prefix="/scenegraph/textureinthread">
<file>main.qml</file>
+ <file>error.qml</file>
</qresource>
</RCC>
diff --git a/examples/quick/scenegraph/textureinthread/threadrenderer.cpp b/examples/quick/scenegraph/textureinthread/threadrenderer.cpp
index 90b6b49880..7de1d294ba 100644
--- a/examples/quick/scenegraph/textureinthread/threadrenderer.cpp
+++ b/examples/quick/scenegraph/textureinthread/threadrenderer.cpp
@@ -46,10 +46,13 @@
#include <QtGui/QOpenGLContext>
#include <QtGui/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
@@ -60,37 +63,39 @@ class RenderThread : public QThread
{
Q_OBJECT
public:
- RenderThread(const QSize &size)
+ RenderThread(const QSize &size, QOpenGLContext *context)
: m_renderFbo(0)
, m_displayFbo(0)
, m_logoRenderer(0)
+ , m_fakeSurface(0)
, m_size(size)
{
- // Since we're using queued connections, we need affinity to the rendering thread.
- moveToThread(this);
+ ThreadRenderer::threads << this;
// Set up the QOpenGLContext to use for rendering in this thread. It is sharing
// memory space with the GL context of the scene graph. This constructor is called
// during updatePaintNode, so we are currently on the scene graph thread with the
// scene graph's OpenGL context current.
- QOpenGLContext *current = QOpenGLContext::currentContext();
m_context = new QOpenGLContext();
- m_context->setShareContext(current);
- m_context->setFormat(current->format());
- m_context->create();
+ m_context->setShareContext(context);
+ m_context->setFormat(context->format());
m_context->moveToThread(this);
- // We need a non-visible surface to make current...
- m_fakeSurface = new QWindow();
- m_fakeSurface->setGeometry(0, 0, 64, 64);
- m_fakeSurface->setSurfaceType(QWindow::OpenGLSurface);
- m_fakeSurface->setFormat(current->format());
+ // We need a non-visible surface to make current in the other thread
+ // and QWindows must be created and managed on the GUI thread.
+ m_fakeSurface = new QOffscreenSurface();
+ m_fakeSurface->setFormat(context->format());
m_fakeSurface->create();
}
+ void setSurface(QOffscreenSurface *surface) { m_fakeSurface = surface; }
+
public slots:
void renderNext()
{
+ if (!m_context->isValid())
+ m_context->create();
+
m_context->makeCurrent(m_fakeSurface);
if (!m_renderFbo) {
@@ -119,6 +124,23 @@ public slots:
emit textureReady(m_displayFbo->texture(), m_size);
}
+ void shutDown()
+ {
+ m_context->makeCurrent(m_fakeSurface);
+ delete m_renderFbo;
+ delete m_displayFbo;
+ delete m_logoRenderer;
+ m_context->doneCurrent();
+ delete m_context;
+
+ // schedule this to be deleted only after we're done cleaning up
+ m_fakeSurface->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);
@@ -128,7 +150,7 @@ private:
LogoRenderer *m_logoRenderer;
- QWindow *m_fakeSurface;
+ QOffscreenSurface *m_fakeSurface;
QOpenGLContext *m_context;
QSize m_size;
};
@@ -209,19 +231,35 @@ private:
ThreadRenderer::ThreadRenderer()
+ : m_renderThread(0)
{
setFlag(ItemHasContents, true);
+ polish();
}
+void ThreadRenderer::updatePolish()
+{
+ if (!window() || !window()->openglContext())
+ return;
+ m_renderThread = new RenderThread(QSize(512, 512), window()->openglContext());
+ m_renderThread->moveToThread(m_renderThread);
+ m_renderThread->start();
+ connect(window(), SIGNAL(sceneGraphInvalidated()), m_renderThread, SLOT(shutDown()), Qt::QueuedConnection);
+}
QSGNode *ThreadRenderer::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
+ if (!m_renderThread) {
+ polish();
+ update();
+ return 0;
+ }
+
TextureNode *node = static_cast<TextureNode *>(oldNode);
if (!node) {
node = new TextureNode(window());
- m_renderThread = new RenderThread(QSize(512, 512));
/* Set up connections to get the production of FBO textures in sync with vsync on the
* rendering thread.
@@ -242,9 +280,6 @@ QSGNode *ThreadRenderer::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *
connect(window(), SIGNAL(beforeRendering()), node, SLOT(prepareNode()), Qt::DirectConnection);
connect(node, SIGNAL(textureInUse()), m_renderThread, SLOT(renderNext()), Qt::QueuedConnection);
- // Start the render thread and enter let it process events.
- m_renderThread->start();
-
// Get the production of FBO textures started..
QMetaObject::invokeMethod(m_renderThread, "renderNext", Qt::QueuedConnection);
}
diff --git a/examples/quick/scenegraph/textureinthread/threadrenderer.h b/examples/quick/scenegraph/textureinthread/threadrenderer.h
index f12e6404e5..8c68e27d92 100644
--- a/examples/quick/scenegraph/textureinthread/threadrenderer.h
+++ b/examples/quick/scenegraph/textureinthread/threadrenderer.h
@@ -52,9 +52,15 @@ class ThreadRenderer : public QQuickItem
public:
ThreadRenderer();
+ static QList<QThread *> threads;
+
+public slots:
+ void updatePolish();
+
protected:
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
+
private:
RenderThread *m_renderThread;
};