summaryrefslogtreecommitdiffstats
path: root/examples/opengl
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@digia.com>2013-04-08 08:32:43 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-04-25 02:35:19 +0200
commit5c6e2882ddcc6580192507dbe8d0769b1b4c1bc2 (patch)
treeea5b73321b027678ec656f35393b3193978dd565 /examples/opengl
parentb43609404cd645faabf88f0677d09c246f8f731c (diff)
Improved robustness of threaded rendering in hellowindow example.
Previously resizes along with the animation was implemented using queued signals and slots, potentially causing a huge lag between the size of the window and the rendered contents. Now the animation is always driven by the rendering thread and is triggered based on the window's isExposed() status. Change-Id: Ifd89a63c2a436671a7b15326ff56be9ec2a5362d Reviewed-by: Jørgen Lind <jorgen.lind@digia.com> Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'examples/opengl')
-rw-r--r--examples/opengl/hellowindow/hellowindow.cpp56
-rw-r--r--examples/opengl/hellowindow/hellowindow.h25
2 files changed, 55 insertions, 26 deletions
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp
index bcb28e9d96..b5ffbd63a2 100644
--- a/examples/opengl/hellowindow/hellowindow.cpp
+++ b/examples/opengl/hellowindow/hellowindow.cpp
@@ -46,6 +46,7 @@
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
: m_initialized(false)
, m_format(format)
+ , m_currentWindow(0)
{
m_context = new QOpenGLContext(this);
if (screen)
@@ -57,7 +58,7 @@ Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *scree
}
HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
- : m_colorIndex(0), m_renderer(renderer), m_timer(0)
+ : m_colorIndex(0), m_renderer(renderer)
{
setSurfaceType(QWindow::OpenGLSurface);
setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
@@ -68,21 +69,12 @@ HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
create();
- connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
- renderer.data(), SLOT(render(QSurface *, const QColor &, const QSize &)));
-
updateColor();
}
void HelloWindow::exposeEvent(QExposeEvent *)
{
- render();
-
- if (!m_timer) {
- m_timer = new QTimer(this);
- connect(m_timer, SIGNAL(timeout()), this, SLOT(render()));
- m_timer->start(10);
- }
+ m_renderer->setAnimating(this, isExposed());
}
void HelloWindow::mousePressEvent(QMouseEvent *)
@@ -90,14 +82,16 @@ void HelloWindow::mousePressEvent(QMouseEvent *)
updateColor();
}
-void HelloWindow::render()
+QColor HelloWindow::color() const
{
- if (isExposed())
- emit needRender(this, m_color, size());
+ QMutexLocker locker(&m_colorLock);
+ return m_color;
}
void HelloWindow::updateColor()
{
+ QMutexLocker locker(&m_colorLock);
+
QColor colors[] =
{
QColor(100, 255, 0),
@@ -108,11 +102,41 @@ void HelloWindow::updateColor()
m_colorIndex = 1 - m_colorIndex;
}
-void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
+void Renderer::setAnimating(HelloWindow *window, bool animating)
+{
+ QMutexLocker locker(&m_windowLock);
+ if (m_windows.contains(window) == animating)
+ return;
+
+ if (animating) {
+ m_windows << window;
+ if (m_windows.size() == 1)
+ QTimer::singleShot(0, this, SLOT(render()));
+ } else {
+ m_currentWindow = 0;
+ m_windows.removeOne(window);
+ }
+}
+
+void Renderer::render()
{
+ QMutexLocker locker(&m_windowLock);
+
+ if (m_windows.isEmpty())
+ return;
+
+ HelloWindow *surface = m_windows.at(m_currentWindow);
+ QColor color = surface->color();
+
+ m_currentWindow = (m_currentWindow + 1) % m_windows.size();
+
if (!m_context->makeCurrent(surface))
return;
+ QSize viewSize = surface->size();
+
+ locker.unlock();
+
if (!m_initialized) {
initialize();
m_initialized = true;
@@ -146,6 +170,8 @@ void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewS
m_context->swapBuffers(surface);
m_fAngle += 1.0f;
+
+ QTimer::singleShot(0, this, SLOT(render()));
}
void Renderer::paintQtLogo()
diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h
index 3d6ac80cfe..6d66f0204a 100644
--- a/examples/opengl/hellowindow/hellowindow.h
+++ b/examples/opengl/hellowindow/hellowindow.h
@@ -41,10 +41,13 @@
#include <QWindow>
#include <QColor>
+#include <QMutex>
#include <QOpenGLShaderProgram>
#include <QSharedPointer>
#include <QTimer>
+class HelloWindow;
+
class Renderer : public QObject
{
Q_OBJECT
@@ -54,8 +57,10 @@ public:
QSurfaceFormat format() const { return m_format; }
-public slots:
- void render(QSurface *surface, const QColor &color, const QSize &viewSize);
+ void setAnimating(HelloWindow *window, bool animating);
+
+private slots:
+ void render();
private:
void initialize();
@@ -78,30 +83,28 @@ private:
QSurfaceFormat m_format;
QOpenGLContext *m_context;
QOpenGLShaderProgram *m_program;
+
+ QList<HelloWindow *> m_windows;
+ int m_currentWindow;
+
+ QMutex m_windowLock;
};
class HelloWindow : public QWindow
{
- Q_OBJECT
-
public:
explicit HelloWindow(const QSharedPointer<Renderer> &renderer);
+ QColor color() const;
void updateColor();
void exposeEvent(QExposeEvent *event);
-signals:
- void needRender(QSurface *surface, const QColor &color, const QSize &viewSize);
-
-private slots:
- void render();
-
private:
void mousePressEvent(QMouseEvent *);
int m_colorIndex;
QColor m_color;
const QSharedPointer<Renderer> m_renderer;
- QTimer *m_timer;
+ mutable QMutex m_colorLock;
};