all repos — mgba @ 05cb9d47ab034904c730be225b4d0e476f10f50f

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::Ignored, QSizePolicy::Ignored);
 32	setAutoBufferSwap(false);
 33}
 34
 35void Display::startDrawing(const uint32_t* buffer, GBAThread* thread) {
 36	if (m_drawThread) {
 37		return;
 38	}
 39	m_drawThread = new QThread(this);
 40	m_painter = new Painter(this);
 41	m_painter->setContext(thread);
 42	m_painter->setBacking(buffer);
 43	m_painter->moveToThread(m_drawThread);
 44	doneCurrent();
 45	context()->moveToThread(m_drawThread);
 46	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 47	m_drawThread->start(QThread::TimeCriticalPriority);
 48}
 49
 50void Display::stopDrawing() {
 51	if (m_drawThread) {
 52		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 53		m_drawThread->exit();
 54		m_drawThread = nullptr;
 55	}
 56}
 57
 58void Display::forceDraw() {
 59	if (m_drawThread) {
 60		QMetaObject::invokeMethod(m_painter, "forceDraw", Qt::BlockingQueuedConnection);
 61	}
 62}
 63
 64void Display::initializeGL() {
 65	glClearColor(0, 0, 0, 0);
 66	glClear(GL_COLOR_BUFFER_BIT);
 67	swapBuffers();
 68}
 69
 70void Display::resizeEvent(QResizeEvent* event) {
 71	if (m_painter) {
 72		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
 73	}
 74}
 75
 76Painter::Painter(Display* parent)
 77	: m_gl(parent)
 78{
 79	m_size = parent->size();
 80}
 81
 82void Painter::setContext(GBAThread* context) {
 83	m_context = context;
 84}
 85
 86void Painter::setBacking(const uint32_t* backing) {
 87	m_backing = backing;
 88}
 89
 90void Painter::resize(const QSize& size) {
 91	m_size = size;
 92	m_gl->makeCurrent();
 93	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
 94	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 95	m_gl->swapBuffers();
 96	m_gl->doneCurrent();
 97}
 98
 99void Painter::start() {
100	m_gl->makeCurrent();
101	glEnable(GL_TEXTURE_2D);
102	glGenTextures(1, &m_tex);
103	glBindTexture(GL_TEXTURE_2D, m_tex);
104	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
105	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
106	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
107	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
108	glEnableClientState(GL_VERTEX_ARRAY);
109	glVertexPointer(2, GL_INT, 0, _glVertices);
110	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
111	glMatrixMode(GL_PROJECTION);
112	glLoadIdentity();
113	glOrtho(0, 240, 160, 0, 0, 1);
114	glMatrixMode(GL_MODELVIEW);
115	glLoadIdentity();
116	m_gl->doneCurrent();
117
118	m_drawTimer = new QTimer;
119	m_drawTimer->moveToThread(QThread::currentThread());
120	m_drawTimer->setInterval(0);
121	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
122	m_drawTimer->start();
123}
124
125void Painter::draw() {
126	m_gl->makeCurrent();
127	if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
128		glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
129		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
130		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
131		if (m_context->sync.videoFrameWait) {
132			glFlush();
133		}
134	}
135	GBASyncWaitFrameEnd(&m_context->sync);
136	m_gl->swapBuffers();
137	m_gl->doneCurrent();
138}
139
140void Painter::forceDraw() {
141	m_gl->makeCurrent();
142	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
143	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
144	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
145	if (m_context->sync.videoFrameWait) {
146		glFlush();
147	}
148	m_gl->swapBuffers();
149	m_gl->doneCurrent();
150}
151
152void Painter::stop() {
153	m_drawTimer->stop();
154	delete m_drawTimer;
155	m_gl->makeCurrent();
156	glDeleteTextures(1, &m_tex);
157	glClear(GL_COLOR_BUFFER_BIT);
158	m_gl->swapBuffers();
159	m_gl->doneCurrent();
160	m_gl->context()->moveToThread(QApplication::instance()->thread());
161}