Make the hellowindow example multi-threaded to stress the GL backend.
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>
This commit is contained in:
parent
9db6c348df
commit
66d9b4d2f8
@ -6,39 +6,37 @@
|
|||||||
|
|
||||||
#include <qmath.h>
|
#include <qmath.h>
|
||||||
|
|
||||||
Renderer::Renderer()
|
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share)
|
||||||
: m_initialized(false)
|
: m_initialized(false)
|
||||||
|
, m_format(format)
|
||||||
{
|
{
|
||||||
m_format.setDepthBufferSize(16);
|
|
||||||
m_format.setSamples(4);
|
|
||||||
|
|
||||||
m_context = new QGuiGLContext;
|
m_context = new QGuiGLContext;
|
||||||
m_context->setFormat(m_format);
|
m_context->setFormat(format);
|
||||||
|
if (share)
|
||||||
|
m_context->setShareContext(share->m_context);
|
||||||
m_context->create();
|
m_context->create();
|
||||||
}
|
}
|
||||||
|
|
||||||
QSurfaceFormat Renderer::format() const
|
|
||||||
{
|
|
||||||
return m_format;
|
|
||||||
}
|
|
||||||
|
|
||||||
HelloWindow::HelloWindow(Renderer *renderer)
|
HelloWindow::HelloWindow(Renderer *renderer)
|
||||||
: m_colorIndex(0)
|
: m_colorIndex(0)
|
||||||
, m_renderer(renderer)
|
, m_renderer(renderer)
|
||||||
{
|
{
|
||||||
setSurfaceType(QWindow::OpenGLSurface);
|
setSurfaceType(QWindow::OpenGLSurface);
|
||||||
setWindowTitle(QLatin1String("Hello Window"));
|
setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
|
||||||
|
|
||||||
setFormat(renderer->format());
|
|
||||||
|
|
||||||
setGeometry(QRect(10, 10, 640, 480));
|
setGeometry(QRect(10, 10, 640, 480));
|
||||||
|
|
||||||
|
setFormat(renderer->format());
|
||||||
|
|
||||||
create();
|
create();
|
||||||
|
|
||||||
QTimer *timer = new QTimer(this);
|
QTimer *timer = new QTimer(this);
|
||||||
connect(timer, SIGNAL(timeout()), this, SLOT(render()));
|
connect(timer, SIGNAL(timeout()), this, SLOT(render()));
|
||||||
timer->start(10);
|
timer->start(10);
|
||||||
|
|
||||||
|
connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
|
||||||
|
renderer, SLOT(render(QSurface *, const QColor &, const QSize &)));
|
||||||
|
|
||||||
updateColor();
|
updateColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +45,11 @@ void HelloWindow::mousePressEvent(QMouseEvent *)
|
|||||||
updateColor();
|
updateColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HelloWindow::render()
|
||||||
|
{
|
||||||
|
emit needRender(this, m_color, size());
|
||||||
|
}
|
||||||
|
|
||||||
void HelloWindow::updateColor()
|
void HelloWindow::updateColor()
|
||||||
{
|
{
|
||||||
QColor colors[] =
|
QColor colors[] =
|
||||||
@ -62,11 +65,6 @@ void HelloWindow::updateColor()
|
|||||||
m_colorIndex = 0;
|
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)
|
void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
|
||||||
{
|
{
|
||||||
if (!m_context->makeCurrent(surface))
|
if (!m_context->makeCurrent(surface))
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <QtOpenGL/qgl.h>
|
#include <QtOpenGL/qgl.h>
|
||||||
#include <QtOpenGL/qglshaderprogram.h>
|
#include <QtOpenGL/qglshaderprogram.h>
|
||||||
|
#include <QtOpenGL/qglframebufferobject.h>
|
||||||
|
|
||||||
#include <QTime>
|
#include <QTime>
|
||||||
|
|
||||||
@ -9,11 +10,13 @@ class QGuiGLContext;
|
|||||||
|
|
||||||
class Renderer : public QObject
|
class Renderer : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
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);
|
void render(QSurface *surface, const QColor &color, const QSize &viewSize);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -45,14 +48,16 @@ class HelloWindow : public QWindow
|
|||||||
public:
|
public:
|
||||||
HelloWindow(Renderer *renderer);
|
HelloWindow(Renderer *renderer);
|
||||||
|
|
||||||
|
void updateColor();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void needRender(QSurface *surface, const QColor &color, const QSize &viewSize);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void render();
|
void render();
|
||||||
|
|
||||||
protected:
|
|
||||||
void mousePressEvent(QMouseEvent *);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateColor();
|
void mousePressEvent(QMouseEvent *);
|
||||||
|
|
||||||
int m_colorIndex;
|
int m_colorIndex;
|
||||||
QColor m_color;
|
QColor m_color;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
#include <QGuiApplication>
|
#include <QGuiApplication>
|
||||||
|
#include <QScreen>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
#include "hellowindow.h"
|
#include "hellowindow.h"
|
||||||
|
|
||||||
@ -6,13 +8,43 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
QGuiApplication app(argc, 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);
|
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);
|
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();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user