Qt: Move GL drawing back into its own thread

This commit is contained in:
Jeffrey Pfau 2015-04-26 03:29:10 -07:00
parent 65543bdd69
commit f52d91c6c8
4 changed files with 100 additions and 75 deletions

View File

@ -44,6 +44,7 @@ Misc:
- Qt: Show multiplayer numbers in window title - Qt: Show multiplayer numbers in window title
- Qt: Handle saving input settings better - Qt: Handle saving input settings better
- Debugger: Free watchpoints in addition to breakpoints - Debugger: Free watchpoints in addition to breakpoints
- Qt: Move GL frame drawing back onto its own thread
0.2.0: (2015-04-03) 0.2.0: (2015-04-03)
Features: Features:

View File

@ -30,37 +30,48 @@ static const GLint _glTexCoords[] = {
DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent) DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
: Display(parent) : Display(parent)
, m_painter(new Painter(format, this)) , m_gl(new EmptyGLWidget(format, this))
, m_started(false) , m_painter(new PainterGL(m_gl))
, m_drawThread(nullptr)
, m_lockAspectRatio(false) , m_lockAspectRatio(false)
, m_filter(false) , m_filter(false)
, m_context(nullptr) , m_context(nullptr)
{ {
} }
DisplayGL::~DisplayGL() {
delete m_painter;
}
void DisplayGL::startDrawing(GBAThread* thread) { void DisplayGL::startDrawing(GBAThread* thread) {
if (m_started) { if (m_drawThread) {
return; return;
} }
m_painter->setContext(thread); m_painter->setContext(thread);
m_context = thread; m_context = thread;
m_painter->start();
m_painter->resize(size()); m_painter->resize(size());
m_painter->move(0, 0); m_gl->move(0, 0);
m_started = true; m_gl->resize(size());
m_drawThread = new QThread(this);
m_gl->context()->doneCurrent();
m_gl->context()->moveToThread(m_drawThread);
m_painter->moveToThread(m_drawThread);
connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
m_drawThread->start();
lockAspectRatio(m_lockAspectRatio); lockAspectRatio(m_lockAspectRatio);
filter(m_filter); filter(m_filter);
} }
void DisplayGL::stopDrawing() { void DisplayGL::stopDrawing() {
if (m_started) { if (m_drawThread) {
if (GBAThreadIsActive(m_context)) { if (GBAThreadIsActive(m_context)) {
GBAThreadInterrupt(m_context); GBAThreadInterrupt(m_context);
GBASyncSuspendDrawing(&m_context->sync); GBASyncSuspendDrawing(&m_context->sync);
} }
m_painter->stop(); QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
m_started = false; m_drawThread->exit();
m_drawThread = nullptr;
if (GBAThreadIsActive(m_context)) { if (GBAThreadIsActive(m_context)) {
GBASyncResumeDrawing(&m_context->sync); GBASyncResumeDrawing(&m_context->sync);
GBAThreadContinue(m_context); GBAThreadContinue(m_context);
@ -69,12 +80,12 @@ void DisplayGL::stopDrawing() {
} }
void DisplayGL::pauseDrawing() { void DisplayGL::pauseDrawing() {
if (m_started) { if (m_drawThread) {
if (GBAThreadIsActive(m_context)) { if (GBAThreadIsActive(m_context)) {
GBAThreadInterrupt(m_context); GBAThreadInterrupt(m_context);
GBASyncSuspendDrawing(&m_context->sync); GBASyncSuspendDrawing(&m_context->sync);
} }
m_painter->pause(); QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
if (GBAThreadIsActive(m_context)) { if (GBAThreadIsActive(m_context)) {
GBASyncResumeDrawing(&m_context->sync); GBASyncResumeDrawing(&m_context->sync);
GBAThreadContinue(m_context); GBAThreadContinue(m_context);
@ -83,12 +94,12 @@ void DisplayGL::pauseDrawing() {
} }
void DisplayGL::unpauseDrawing() { void DisplayGL::unpauseDrawing() {
if (m_started) { if (m_drawThread) {
if (GBAThreadIsActive(m_context)) { if (GBAThreadIsActive(m_context)) {
GBAThreadInterrupt(m_context); GBAThreadInterrupt(m_context);
GBASyncSuspendDrawing(&m_context->sync); GBASyncSuspendDrawing(&m_context->sync);
} }
m_painter->unpause(); QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
if (GBAThreadIsActive(m_context)) { if (GBAThreadIsActive(m_context)) {
GBASyncResumeDrawing(&m_context->sync); GBASyncResumeDrawing(&m_context->sync);
GBAThreadContinue(m_context); GBAThreadContinue(m_context);
@ -97,53 +108,53 @@ void DisplayGL::unpauseDrawing() {
} }
void DisplayGL::forceDraw() { void DisplayGL::forceDraw() {
if (m_started) { if (m_drawThread) {
m_painter->forceDraw(); QMetaObject::invokeMethod(m_painter, "forceDraw");
} }
} }
void DisplayGL::lockAspectRatio(bool lock) { void DisplayGL::lockAspectRatio(bool lock) {
m_lockAspectRatio = lock; m_lockAspectRatio = lock;
if (m_started) { if (m_drawThread) {
m_painter->lockAspectRatio(lock); QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
} }
} }
void DisplayGL::filter(bool filter) { void DisplayGL::filter(bool filter) {
m_filter = filter; m_filter = filter;
if (m_started) { if (m_drawThread) {
m_painter->filter(filter); QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
} }
} }
void DisplayGL::framePosted(const uint32_t* buffer) { void DisplayGL::framePosted(const uint32_t* buffer) {
if (m_started && buffer) { if (m_drawThread && buffer) {
m_painter->setBacking(buffer); QMetaObject::invokeMethod(m_painter, "setBacking", Q_ARG(const uint32_t*, buffer));
} }
} }
void DisplayGL::resizeEvent(QResizeEvent* event) { void DisplayGL::resizeEvent(QResizeEvent* event) {
m_painter->resize(event->size()); m_gl->resize(size());
if (m_drawThread) {
QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
}
} }
Painter::Painter(const QGLFormat& format, QWidget* parent) PainterGL::PainterGL(QGLWidget* parent)
: QGLWidget(format, parent) : m_gl(parent)
, m_drawTimer(nullptr) , m_drawTimer(nullptr)
, m_lockAspectRatio(false) , m_lockAspectRatio(false)
, m_filter(false) , m_filter(false)
, m_context(nullptr) , m_context(nullptr)
{ {
setMinimumSize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
m_size = parent->size();
setAutoBufferSwap(false);
} }
void Painter::setContext(GBAThread* context) { void PainterGL::setContext(GBAThread* context) {
m_context = context; m_context = context;
} }
void Painter::setBacking(const uint32_t* backing) { void PainterGL::setBacking(const uint32_t* backing) {
makeCurrent(); m_gl->makeCurrent();
#ifdef COLOR_16_BIT #ifdef COLOR_16_BIT
#ifdef COLOR_5_6_5 #ifdef COLOR_5_6_5
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, backing); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, backing);
@ -153,40 +164,41 @@ void Painter::setBacking(const uint32_t* backing) {
#else #else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, backing); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, backing);
#endif #endif
doneCurrent(); m_gl->doneCurrent();
} }
void Painter::resize(const QSize& size) { void PainterGL::resize(const QSize& size) {
m_size = size; m_size = size;
QWidget::resize(size);
if (m_drawTimer) { if (m_drawTimer) {
forceDraw(); forceDraw();
forceDraw();
} }
} }
void Painter::lockAspectRatio(bool lock) { void PainterGL::lockAspectRatio(bool lock) {
m_lockAspectRatio = lock; m_lockAspectRatio = lock;
if (m_drawTimer) { if (m_drawTimer) {
forceDraw(); forceDraw();
forceDraw();
} }
} }
void Painter::filter(bool filter) { void PainterGL::filter(bool filter) {
m_filter = filter; m_filter = filter;
makeCurrent(); m_gl->makeCurrent();
if (m_filter) { if (m_filter) {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else { } else {
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} }
doneCurrent(); m_gl->doneCurrent();
if (m_drawTimer) { if (m_drawTimer) {
forceDraw(); forceDraw();
} }
} }
void Painter::start() { void PainterGL::start() {
makeCurrent(); m_gl->makeCurrent();
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glGenTextures(1, &m_tex); glGenTextures(1, &m_tex);
glBindTexture(GL_TEXTURE_2D, m_tex); glBindTexture(GL_TEXTURE_2D, m_tex);
@ -206,7 +218,9 @@ void Painter::start() {
glOrtho(0, 240, 160, 0, 0, 1); glOrtho(0, 240, 160, 0, 0, 1);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
doneCurrent(); glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
m_gl->doneCurrent();
m_drawTimer = new QTimer; m_drawTimer = new QTimer;
m_drawTimer->moveToThread(QThread::currentThread()); m_drawTimer->moveToThread(QThread::currentThread());
@ -215,58 +229,55 @@ void Painter::start() {
m_drawTimer->start(); m_drawTimer->start();
} }
void Painter::draw() { void PainterGL::draw() {
makeCurrent(); m_gl->makeCurrent();
GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip); GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip);
performDraw(); performDraw();
GBASyncWaitFrameEnd(&m_context->sync); GBASyncWaitFrameEnd(&m_context->sync);
swapBuffers(); m_gl->swapBuffers();
doneCurrent(); m_gl->doneCurrent();
} }
void Painter::forceDraw() { void PainterGL::forceDraw() {
makeCurrent(); m_gl->makeCurrent();
glViewport(0, 0, m_size.width() * devicePixelRatio(), m_size.height() * devicePixelRatio()); glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
performDraw(); performDraw();
swapBuffers(); m_gl->swapBuffers();
doneCurrent(); m_gl->doneCurrent();
} }
void Painter::stop() { void PainterGL::stop() {
m_drawTimer->stop(); m_drawTimer->stop();
delete m_drawTimer; delete m_drawTimer;
m_drawTimer = nullptr; m_drawTimer = nullptr;
makeCurrent(); m_gl->makeCurrent();
glDeleteTextures(1, &m_tex); glDeleteTextures(1, &m_tex);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
swapBuffers(); m_gl->swapBuffers();
doneCurrent(); m_gl->doneCurrent();
m_gl->context()->moveToThread(m_gl->thread());
moveToThread(m_gl->thread());
} }
void Painter::pause() { void PainterGL::pause() {
m_drawTimer->stop(); m_drawTimer->stop();
// Make sure both buffers are filled // Make sure both buffers are filled
forceDraw(); forceDraw();
forceDraw(); forceDraw();
} }
void Painter::unpause() { void PainterGL::unpause() {
m_drawTimer->start(); m_drawTimer->start();
} }
void Painter::initializeGL() { void PainterGL::performDraw() {
glClearColor(0, 0, 0, 0); int w = m_size.width() * m_gl->devicePixelRatio();
glClear(GL_COLOR_BUFFER_BIT); int h = m_size.height() *m_gl->devicePixelRatio();
}
void Painter::performDraw() {
int w = m_size.width() * devicePixelRatio();
int h = m_size.height() * devicePixelRatio();
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
// TODO: This seems to cause framerates to drag down to 120 FPS on OS X, // TODO: This seems to cause framerates to drag down to 120 FPS on OS X,
// even if the emulator can go faster. Look into why. // even if the emulator can go faster. Look into why.
glViewport(0, 0, m_size.width() * devicePixelRatio(), m_size.height() * devicePixelRatio()); glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
#endif #endif
int drawW = w; int drawW = w;

View File

@ -16,12 +16,22 @@ struct GBAThread;
namespace QGBA { namespace QGBA {
class Painter; class EmptyGLWidget : public QGLWidget {
public:
EmptyGLWidget(const QGLFormat& format, QWidget* parent) : QGLWidget(format, parent) {}
protected:
void paintEvent(QPaintEvent*) override {}
void resizeEvent(QResizeEvent*) override {}
};
class PainterGL;
class DisplayGL : public Display { class DisplayGL : public Display {
Q_OBJECT Q_OBJECT
public: public:
DisplayGL(const QGLFormat& format, QWidget* parent = nullptr); DisplayGL(const QGLFormat& format, QWidget* parent = nullptr);
~DisplayGL();
public slots: public slots:
void startDrawing(GBAThread* context) override; void startDrawing(GBAThread* context) override;
@ -34,27 +44,31 @@ public slots:
void framePosted(const uint32_t*) override; void framePosted(const uint32_t*) override;
protected: protected:
virtual void paintEvent(QPaintEvent*) override {}; virtual void paintEvent(QPaintEvent*) override {
QPainter paint(this);
paint.fillRect(QRect(QPoint(), size()), Qt::black);
};
virtual void resizeEvent(QResizeEvent*) override; virtual void resizeEvent(QResizeEvent*) override;
private: private:
Painter* m_painter; QGLWidget* m_gl;
bool m_started; PainterGL* m_painter;
QThread* m_drawThread;
GBAThread* m_context; GBAThread* m_context;
bool m_lockAspectRatio; bool m_lockAspectRatio;
bool m_filter; bool m_filter;
}; };
class Painter : public QGLWidget { class PainterGL : public QObject {
Q_OBJECT Q_OBJECT
public: public:
Painter(const QGLFormat& format, QWidget* parent); PainterGL(QGLWidget* parent);
void setContext(GBAThread*); void setContext(GBAThread*);
void setBacking(const uint32_t*);
public slots: public slots:
void setBacking(const uint32_t*);
void forceDraw(); void forceDraw();
void draw(); void draw();
void start(); void start();
@ -65,13 +79,11 @@ public slots:
void lockAspectRatio(bool lock); void lockAspectRatio(bool lock);
void filter(bool filter); void filter(bool filter);
protected:
virtual void initializeGL() override;
virtual void paintEvent(QPaintEvent*) override {}
private: private:
void performDraw(); void performDraw();
QGLWidget* m_gl;
QThread* m_thread;
QTimer* m_drawTimer; QTimer* m_drawTimer;
GBAThread* m_context; GBAThread* m_context;
GLuint m_tex; GLuint m_tex;

View File

@ -26,6 +26,7 @@ GBAApp::GBAApp(int& argc, char* argv[])
#endif #endif
SocketSubsystemInit(); SocketSubsystemInit();
qRegisterMetaType<const uint32_t*>("const uint32_t*");
QApplication::setApplicationName(PROJECT_NAME); QApplication::setApplicationName(PROJECT_NAME);
QApplication::setApplicationVersion(PROJECT_VERSION); QApplication::setApplicationVersion(PROJECT_VERSION);