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)
|
||||
: 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()
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user