all repos — mgba @ e2812b30b219bf9c77d2ea8e882f333fc8752ffd

mGBA Game Boy Advance Emulator

src/platform/qt/Display.cpp (view raw)

  1#include "Display.h"
  2
  3#include <QApplication>
  4#include <QResizeEvent>
  5
  6extern "C" {
  7#include "gba-thread.h"
  8}
  9
 10using namespace QGBA;
 11
 12static const GLint _glVertices[] = {
 13	0, 0,
 14	256, 0,
 15	256, 256,
 16	0, 256
 17};
 18
 19static const GLint _glTexCoords[] = {
 20	0, 0,
 21	1, 0,
 22	1, 1,
 23	0, 1
 24};
 25
 26Display::Display(QGLFormat format, QWidget* parent)
 27	: QGLWidget(format, parent)
 28	, m_painter(nullptr)
 29	, m_drawThread(nullptr)
 30{
 31	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
 32	setMinimumSize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
 33	setAutoBufferSwap(false);
 34	setCursor(Qt::BlankCursor);
 35}
 36
 37void Display::startDrawing(const uint32_t* buffer, GBAThread* thread) {
 38	if (m_drawThread) {
 39		return;
 40	}
 41	m_drawThread = new QThread(this);
 42	m_painter = new Painter(this);
 43	m_painter->setContext(thread);
 44	m_painter->setBacking(buffer);
 45	m_painter->moveToThread(m_drawThread);
 46	m_context = thread;
 47	doneCurrent();
 48	context()->moveToThread(m_drawThread);
 49	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 50	m_drawThread->start(QThread::TimeCriticalPriority);
 51}
 52
 53void Display::stopDrawing() {
 54	if (m_drawThread) {
 55		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 56		m_drawThread->exit();
 57		m_drawThread = nullptr;
 58	}
 59}
 60
 61void Display::forceDraw() {
 62	if (m_drawThread) {
 63		QMetaObject::invokeMethod(m_painter, "forceDraw", Qt::QueuedConnection);
 64	}
 65}
 66
 67void Display::initializeGL() {
 68	glClearColor(0, 0, 0, 0);
 69	glClear(GL_COLOR_BUFFER_BIT);
 70	swapBuffers();
 71}
 72
 73void Display::resizeEvent(QResizeEvent* event) {
 74	if (m_drawThread) {
 75		GBAThreadInterrupt(m_context);
 76		GBASyncSuspendDrawing(&m_context->sync);
 77		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
 78		GBASyncResumeDrawing(&m_context->sync);
 79		GBAThreadContinue(m_context);
 80	}
 81}
 82
 83Painter::Painter(Display* parent)
 84	: m_gl(parent)
 85{
 86	m_size = parent->size();
 87}
 88
 89void Painter::setContext(GBAThread* context) {
 90	m_context = context;
 91}
 92
 93void Painter::setBacking(const uint32_t* backing) {
 94	m_backing = backing;
 95}
 96
 97void Painter::resize(const QSize& size) {
 98	m_size = size;
 99	m_gl->makeCurrent();
100	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
101	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
102	m_gl->swapBuffers();
103	m_gl->doneCurrent();
104}
105
106void Painter::start() {
107	m_gl->makeCurrent();
108	glEnable(GL_TEXTURE_2D);
109	glGenTextures(1, &m_tex);
110	glBindTexture(GL_TEXTURE_2D, m_tex);
111	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
112	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
114	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
115	glEnableClientState(GL_VERTEX_ARRAY);
116	glVertexPointer(2, GL_INT, 0, _glVertices);
117	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
118	glMatrixMode(GL_PROJECTION);
119	glLoadIdentity();
120	glOrtho(0, 240, 160, 0, 0, 1);
121	glMatrixMode(GL_MODELVIEW);
122	glLoadIdentity();
123	m_gl->doneCurrent();
124
125	m_drawTimer = new QTimer;
126	m_drawTimer->moveToThread(QThread::currentThread());
127	m_drawTimer->setInterval(0);
128	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
129	m_drawTimer->start();
130}
131
132void Painter::draw() {
133	m_gl->makeCurrent();
134	if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
135		glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
136		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
137		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
138		if (m_context->sync.videoFrameWait) {
139			glFlush();
140		}
141	}
142	GBASyncWaitFrameEnd(&m_context->sync);
143	m_gl->swapBuffers();
144	m_gl->doneCurrent();
145}
146
147void Painter::forceDraw() {
148	m_gl->makeCurrent();
149	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
150	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
151	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
152	if (m_context->sync.videoFrameWait) {
153		glFlush();
154	}
155	m_gl->swapBuffers();
156	m_gl->doneCurrent();
157}
158
159void Painter::stop() {
160	m_drawTimer->stop();
161	delete m_drawTimer;
162	m_gl->makeCurrent();
163	glDeleteTextures(1, &m_tex);
164	glClear(GL_COLOR_BUFFER_BIT);
165	m_gl->swapBuffers();
166	m_gl->doneCurrent();
167	m_gl->context()->moveToThread(QApplication::instance()->thread());
168}