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>
This commit is contained in:
parent
b43609404c
commit
5c6e2882dd
@ -46,6 +46,7 @@
|
|||||||
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
|
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
|
||||||
: m_initialized(false)
|
: m_initialized(false)
|
||||||
, m_format(format)
|
, m_format(format)
|
||||||
|
, m_currentWindow(0)
|
||||||
{
|
{
|
||||||
m_context = new QOpenGLContext(this);
|
m_context = new QOpenGLContext(this);
|
||||||
if (screen)
|
if (screen)
|
||||||
@ -57,7 +58,7 @@ Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *scree
|
|||||||
}
|
}
|
||||||
|
|
||||||
HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
|
HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
|
||||||
: m_colorIndex(0), m_renderer(renderer), m_timer(0)
|
: m_colorIndex(0), m_renderer(renderer)
|
||||||
{
|
{
|
||||||
setSurfaceType(QWindow::OpenGLSurface);
|
setSurfaceType(QWindow::OpenGLSurface);
|
||||||
setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
||||||
@ -68,21 +69,12 @@ HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
|
|||||||
|
|
||||||
create();
|
create();
|
||||||
|
|
||||||
connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
|
|
||||||
renderer.data(), SLOT(render(QSurface *, const QColor &, const QSize &)));
|
|
||||||
|
|
||||||
updateColor();
|
updateColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelloWindow::exposeEvent(QExposeEvent *)
|
void HelloWindow::exposeEvent(QExposeEvent *)
|
||||||
{
|
{
|
||||||
render();
|
m_renderer->setAnimating(this, isExposed());
|
||||||
|
|
||||||
if (!m_timer) {
|
|
||||||
m_timer = new QTimer(this);
|
|
||||||
connect(m_timer, SIGNAL(timeout()), this, SLOT(render()));
|
|
||||||
m_timer->start(10);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelloWindow::mousePressEvent(QMouseEvent *)
|
void HelloWindow::mousePressEvent(QMouseEvent *)
|
||||||
@ -90,14 +82,16 @@ void HelloWindow::mousePressEvent(QMouseEvent *)
|
|||||||
updateColor();
|
updateColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelloWindow::render()
|
QColor HelloWindow::color() const
|
||||||
{
|
{
|
||||||
if (isExposed())
|
QMutexLocker locker(&m_colorLock);
|
||||||
emit needRender(this, m_color, size());
|
return m_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelloWindow::updateColor()
|
void HelloWindow::updateColor()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&m_colorLock);
|
||||||
|
|
||||||
QColor colors[] =
|
QColor colors[] =
|
||||||
{
|
{
|
||||||
QColor(100, 255, 0),
|
QColor(100, 255, 0),
|
||||||
@ -108,11 +102,41 @@ void HelloWindow::updateColor()
|
|||||||
m_colorIndex = 1 - m_colorIndex;
|
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))
|
if (!m_context->makeCurrent(surface))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
QSize viewSize = surface->size();
|
||||||
|
|
||||||
|
locker.unlock();
|
||||||
|
|
||||||
if (!m_initialized) {
|
if (!m_initialized) {
|
||||||
initialize();
|
initialize();
|
||||||
m_initialized = true;
|
m_initialized = true;
|
||||||
@ -146,6 +170,8 @@ void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewS
|
|||||||
m_context->swapBuffers(surface);
|
m_context->swapBuffers(surface);
|
||||||
|
|
||||||
m_fAngle += 1.0f;
|
m_fAngle += 1.0f;
|
||||||
|
|
||||||
|
QTimer::singleShot(0, this, SLOT(render()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::paintQtLogo()
|
void Renderer::paintQtLogo()
|
||||||
|
@ -41,10 +41,13 @@
|
|||||||
#include <QWindow>
|
#include <QWindow>
|
||||||
|
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
|
#include <QMutex>
|
||||||
#include <QOpenGLShaderProgram>
|
#include <QOpenGLShaderProgram>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
class HelloWindow;
|
||||||
|
|
||||||
class Renderer : public QObject
|
class Renderer : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -54,8 +57,10 @@ public:
|
|||||||
|
|
||||||
QSurfaceFormat format() const { return m_format; }
|
QSurfaceFormat format() const { return m_format; }
|
||||||
|
|
||||||
public slots:
|
void setAnimating(HelloWindow *window, bool animating);
|
||||||
void render(QSurface *surface, const QColor &color, const QSize &viewSize);
|
|
||||||
|
private slots:
|
||||||
|
void render();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initialize();
|
void initialize();
|
||||||
@ -78,30 +83,28 @@ private:
|
|||||||
QSurfaceFormat m_format;
|
QSurfaceFormat m_format;
|
||||||
QOpenGLContext *m_context;
|
QOpenGLContext *m_context;
|
||||||
QOpenGLShaderProgram *m_program;
|
QOpenGLShaderProgram *m_program;
|
||||||
|
|
||||||
|
QList<HelloWindow *> m_windows;
|
||||||
|
int m_currentWindow;
|
||||||
|
|
||||||
|
QMutex m_windowLock;
|
||||||
};
|
};
|
||||||
|
|
||||||
class HelloWindow : public QWindow
|
class HelloWindow : public QWindow
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit HelloWindow(const QSharedPointer<Renderer> &renderer);
|
explicit HelloWindow(const QSharedPointer<Renderer> &renderer);
|
||||||
|
|
||||||
|
QColor color() const;
|
||||||
void updateColor();
|
void updateColor();
|
||||||
|
|
||||||
void exposeEvent(QExposeEvent *event);
|
void exposeEvent(QExposeEvent *event);
|
||||||
|
|
||||||
signals:
|
|
||||||
void needRender(QSurface *surface, const QColor &color, const QSize &viewSize);
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void render();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void mousePressEvent(QMouseEvent *);
|
void mousePressEvent(QMouseEvent *);
|
||||||
|
|
||||||
int m_colorIndex;
|
int m_colorIndex;
|
||||||
QColor m_color;
|
QColor m_color;
|
||||||
const QSharedPointer<Renderer> m_renderer;
|
const QSharedPointer<Renderer> m_renderer;
|
||||||
QTimer *m_timer;
|
mutable QMutex m_colorLock;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user