Make the hellowindow example multi-threaded to stress the GL backend.
authorSamuel Rødal <samuel.rodal@nokia.com>
Thu, 18 Aug 2011 08:50:18 +0000 (10:50 +0200)
committerPaul Olav Tvete <paul.tvete@nokia.com>
Fri, 19 Aug 2011 11:57:30 +0000 (13:57 +0200)
Change-Id: I9e158c0889b050f9ed76ea21176102fc792eef83
Reviewed-on: http://codereview.qt.nokia.com/3150
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Paul Olav Tvete <paul.tvete@nokia.com>
examples/opengl/hellowindow/hellowindow.cpp
examples/opengl/hellowindow/hellowindow.h
examples/opengl/hellowindow/main.cpp

index 5a232e6..3c4f88b 100644 (file)
@@ -6,39 +6,37 @@
 
 #include <qmath.h>
 
-Renderer::Renderer()
+Renderer::Renderer(const QSurfaceFormat &format, Renderer *share)
     : m_initialized(false)
+    , m_format(format)
 {
-    m_format.setDepthBufferSize(16);
-    m_format.setSamples(4);
-
     m_context = new QGuiGLContext;
-    m_context->setFormat(m_format);
+    m_context->setFormat(format);
+    if (share)
+        m_context->setShareContext(share->m_context);
     m_context->create();
 }
 
-QSurfaceFormat Renderer::format() const
-{
-    return m_format;
-}
-
 HelloWindow::HelloWindow(Renderer *renderer)
     : m_colorIndex(0)
     , m_renderer(renderer)
 {
     setSurfaceType(QWindow::OpenGLSurface);
-    setWindowTitle(QLatin1String("Hello Window"));
-
-    setFormat(renderer->format());
+    setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
 
     setGeometry(QRect(10, 10, 640, 480));
 
+    setFormat(renderer->format());
+
     create();
 
     QTimer *timer = new QTimer(this);
     connect(timer, SIGNAL(timeout()), this, SLOT(render()));
     timer->start(10);
 
+    connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
+            renderer, SLOT(render(QSurface *, const QColor &, const QSize &)));
+
     updateColor();
 }
 
@@ -47,6 +45,11 @@ void HelloWindow::mousePressEvent(QMouseEvent *)
     updateColor();
 }
 
+void HelloWindow::render()
+{
+    emit needRender(this, m_color, size());
+}
+
 void HelloWindow::updateColor()
 {
     QColor colors[] =
@@ -62,11 +65,6 @@ void HelloWindow::updateColor()
         m_colorIndex = 0;
 }
 
-void HelloWindow::render()
-{
-    m_renderer->render(this, m_color, geometry().size());
-}
-
 void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
 {
     if (!m_context->makeCurrent(surface))
index 3c5388c..c114b09 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <QtOpenGL/qgl.h>
 #include <QtOpenGL/qglshaderprogram.h>
+#include <QtOpenGL/qglframebufferobject.h>
 
 #include <QTime>
 
@@ -9,11 +10,13 @@ class QGuiGLContext;
 
 class Renderer : public QObject
 {
+    Q_OBJECT
 public:
-    Renderer();
+    Renderer(const QSurfaceFormat &format, Renderer *share = 0);
 
-    QSurfaceFormat format() const;
+    QSurfaceFormat format() const { return m_format; }
 
+public slots:
     void render(QSurface *surface, const QColor &color, const QSize &viewSize);
 
 private:
@@ -45,14 +48,16 @@ class HelloWindow : public QWindow
 public:
     HelloWindow(Renderer *renderer);
 
+    void updateColor();
+
+signals:
+    void needRender(QSurface *surface, const QColor &color, const QSize &viewSize);
+
 private slots:
     void render();
 
-protected:
-    void mousePressEvent(QMouseEvent *);
-
 private:
-    void updateColor();
+    void mousePressEvent(QMouseEvent *);
 
     int m_colorIndex;
     QColor m_color;
index af5943a..b247807 100644 (file)
@@ -1,4 +1,6 @@
 #include <QGuiApplication>
+#include <QScreen>
+#include <QThread>
 
 #include "hellowindow.h"
 
@@ -6,13 +8,43 @@ int main(int argc, char **argv)
 {
     QGuiApplication app(argc, argv);
 
-    Renderer renderer;
+    QScreen *screen = QGuiApplication::primaryScreen();
 
-    HelloWindow windowA(&renderer);
+    QRect screenGeometry = screen->availableGeometry();
+
+    QSurfaceFormat format;
+    format.setDepthBufferSize(16);
+    format.setSamples(4);
+
+    QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80);
+    QSize windowSize(400, 320);
+    int delta = 40;
+
+    Renderer rendererA(format);
+    Renderer rendererB(format, &rendererA);
+
+    QThread renderThread;
+    rendererB.moveToThread(&renderThread);
+    renderThread.start();
+
+    QObject::connect(qGuiApp, SIGNAL(lastWindowClosed()), &renderThread, SLOT(quit()));
+
+    HelloWindow windowA(&rendererA);
+    windowA.setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0));
+    windowA.setWindowTitle(QLatin1String("Thread A - Context A"));
     windowA.setVisible(true);
 
-    HelloWindow windowB(&renderer);
+    HelloWindow windowB(&rendererA);
+    windowB.setGeometry(QRect(center, windowSize).translated(delta / 2, 0));
+    windowB.setWindowTitle(QLatin1String("Thread A - Context A"));
     windowB.setVisible(true);
 
-    return app.exec();
+    HelloWindow windowC(&rendererB);
+    windowC.setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta));
+    windowC.setWindowTitle(QLatin1String("Thread B - Context B"));
+    windowC.setVisible(true);
+
+    app.exec();
+
+    renderThread.wait();
 }