all repos — mgba @ d83520c5a246b8096ceb02ce001f4ee7291bba9f

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		if (GBAThreadIsActive(m_context)) {
 56			GBAThreadInterrupt(m_context);
 57			GBASyncSuspendDrawing(&m_context->sync);
 58		}
 59		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 60		m_drawThread->exit();
 61		m_drawThread = nullptr;
 62		if (GBAThreadIsActive(m_context)) {
 63			GBASyncResumeDrawing(&m_context->sync);
 64			GBAThreadContinue(m_context);
 65		}
 66	}
 67}
 68
 69void Display::forceDraw() {
 70	if (m_drawThread) {
 71		QMetaObject::invokeMethod(m_painter, "forceDraw", Qt::QueuedConnection);
 72	}
 73}
 74
 75#ifdef USE_PNG
 76void Display::screenshot() {
 77	GBAThreadInterrupt(m_context);
 78	GBAThreadTakeScreenshot(m_context);
 79	GBAThreadContinue(m_context);
 80}
 81#endif
 82
 83void Display::initializeGL() {
 84	glClearColor(0, 0, 0, 0);
 85	glClear(GL_COLOR_BUFFER_BIT);
 86	swapBuffers();
 87}
 88
 89void Display::resizeEvent(QResizeEvent* event) {
 90	if (m_drawThread) {
 91		GBAThreadInterrupt(m_context);
 92		GBASyncSuspendDrawing(&m_context->sync);
 93		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
 94		GBASyncResumeDrawing(&m_context->sync);
 95		GBAThreadContinue(m_context);
 96	}
 97}
 98
 99Painter::Painter(Display* parent)
100	: m_gl(parent)
101{
102	m_size = parent->size();
103}
104
105void Painter::setContext(GBAThread* context) {
106	m_context = context;
107}
108
109void Painter::setBacking(const uint32_t* backing) {
110	m_backing = backing;
111}
112
113void Painter::resize(const QSize& size) {
114	m_size = size;
115	m_gl->makeCurrent();
116	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
117	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
118	m_gl->swapBuffers();
119	m_gl->doneCurrent();
120}
121
122void Painter::start() {
123	m_gl->makeCurrent();
124	glEnable(GL_TEXTURE_2D);
125	glGenTextures(1, &m_tex);
126	glBindTexture(GL_TEXTURE_2D, m_tex);
127	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
128	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
129	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
131	glEnableClientState(GL_VERTEX_ARRAY);
132	glVertexPointer(2, GL_INT, 0, _glVertices);
133	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
134	glMatrixMode(GL_PROJECTION);
135	glLoadIdentity();
136	glOrtho(0, 240, 160, 0, 0, 1);
137	glMatrixMode(GL_MODELVIEW);
138	glLoadIdentity();
139	m_gl->doneCurrent();
140
141	m_drawTimer = new QTimer;
142	m_drawTimer->moveToThread(QThread::currentThread());
143	m_drawTimer->setInterval(0);
144	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
145	m_drawTimer->start();
146}
147
148void Painter::draw() {
149	m_gl->makeCurrent();
150	if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
151		glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
152		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
153		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
154		if (m_context->sync.videoFrameWait) {
155			glFlush();
156		}
157	}
158	GBASyncWaitFrameEnd(&m_context->sync);
159	m_gl->swapBuffers();
160	m_gl->doneCurrent();
161}
162
163void Painter::forceDraw() {
164	m_gl->makeCurrent();
165	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
166	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
167	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
168	if (m_context->sync.videoFrameWait) {
169		glFlush();
170	}
171	m_gl->swapBuffers();
172	m_gl->doneCurrent();
173}
174
175void Painter::stop() {
176	m_drawTimer->stop();
177	delete m_drawTimer;
178	m_gl->makeCurrent();
179	glDeleteTextures(1, &m_tex);
180	glClear(GL_COLOR_BUFFER_BIT);
181	m_gl->swapBuffers();
182	m_gl->doneCurrent();
183	m_gl->context()->moveToThread(QApplication::instance()->thread());
184}