all repos — mgba @ ee8dedeea144be20640640a34162c1df7faa1eb7

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "Display.h"
  7
  8#include <QApplication>
  9#include <QResizeEvent>
 10
 11extern "C" {
 12#include "gba-thread.h"
 13}
 14
 15using namespace QGBA;
 16
 17static const GLint _glVertices[] = {
 18	0, 0,
 19	256, 0,
 20	256, 256,
 21	0, 256
 22};
 23
 24static const GLint _glTexCoords[] = {
 25	0, 0,
 26	1, 0,
 27	1, 1,
 28	0, 1
 29};
 30
 31Display::Display(QGLFormat format, QWidget* parent)
 32	: QGLWidget(format, parent)
 33	, m_painter(nullptr)
 34	, m_drawThread(nullptr)
 35{
 36	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
 37	setMinimumSize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
 38	setAutoBufferSwap(false);
 39	setCursor(Qt::BlankCursor);
 40}
 41
 42void Display::startDrawing(const uint32_t* buffer, GBAThread* thread) {
 43	if (m_drawThread) {
 44		return;
 45	}
 46	m_drawThread = new QThread(this);
 47	m_painter = new Painter(this);
 48	m_painter->setContext(thread);
 49	m_painter->setBacking(buffer);
 50	m_painter->moveToThread(m_drawThread);
 51	m_context = thread;
 52	doneCurrent();
 53	context()->moveToThread(m_drawThread);
 54	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 55	m_drawThread->start(QThread::TimeCriticalPriority);
 56
 57	lockAspectRatio(m_lockAspectRatio);
 58	filter(m_filter);
 59}
 60
 61void Display::stopDrawing() {
 62	if (m_drawThread) {
 63		if (GBAThreadIsActive(m_context)) {
 64			GBAThreadInterrupt(m_context);
 65			GBASyncSuspendDrawing(&m_context->sync);
 66		}
 67		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 68		m_drawThread->exit();
 69		m_drawThread = nullptr;
 70		if (GBAThreadIsActive(m_context)) {
 71			GBASyncResumeDrawing(&m_context->sync);
 72			GBAThreadContinue(m_context);
 73		}
 74	}
 75}
 76
 77void Display::pauseDrawing() {
 78	if (m_drawThread) {
 79		if (GBAThreadIsActive(m_context)) {
 80			GBAThreadInterrupt(m_context);
 81			GBASyncSuspendDrawing(&m_context->sync);
 82		}
 83		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
 84		if (GBAThreadIsActive(m_context)) {
 85			GBASyncResumeDrawing(&m_context->sync);
 86			GBAThreadContinue(m_context);
 87		}
 88	}
 89}
 90
 91void Display::unpauseDrawing() {
 92	if (m_drawThread) {
 93		if (GBAThreadIsActive(m_context)) {
 94			GBAThreadInterrupt(m_context);
 95			GBASyncSuspendDrawing(&m_context->sync);
 96		}
 97		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
 98		if (GBAThreadIsActive(m_context)) {
 99			GBASyncResumeDrawing(&m_context->sync);
100			GBAThreadContinue(m_context);
101		}
102	}
103}
104
105void Display::forceDraw() {
106	if (m_drawThread) {
107		QMetaObject::invokeMethod(m_painter, "forceDraw", Qt::QueuedConnection);
108	}
109}
110
111void Display::lockAspectRatio(bool lock) {
112	m_lockAspectRatio = lock;
113	if (m_drawThread) {
114		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Qt::QueuedConnection, Q_ARG(bool, lock));
115	}
116}
117
118void Display::filter(bool filter) {
119	m_filter = filter;
120	if (m_drawThread) {
121		QMetaObject::invokeMethod(m_painter, "filter", Qt::QueuedConnection, Q_ARG(bool, filter));
122	}
123}
124
125#ifdef USE_PNG
126void Display::screenshot() {
127	GBAThreadInterrupt(m_context);
128	GBAThreadTakeScreenshot(m_context);
129	GBAThreadContinue(m_context);
130}
131#endif
132
133void Display::initializeGL() {
134	glClearColor(0, 0, 0, 0);
135	glClear(GL_COLOR_BUFFER_BIT);
136	swapBuffers();
137}
138
139void Display::resizeEvent(QResizeEvent* event) {
140	if (m_drawThread) {
141		GBAThreadInterrupt(m_context);
142		GBASyncSuspendDrawing(&m_context->sync);
143		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
144		GBASyncResumeDrawing(&m_context->sync);
145		GBAThreadContinue(m_context);
146	}
147}
148
149Painter::Painter(Display* parent)
150	: m_gl(parent)
151	, m_lockAspectRatio(false)
152{
153	m_size = parent->size();
154}
155
156void Painter::setContext(GBAThread* context) {
157	m_context = context;
158}
159
160void Painter::setBacking(const uint32_t* backing) {
161	m_backing = backing;
162}
163
164void Painter::resize(const QSize& size) {
165	m_size = size;
166	forceDraw();
167}
168
169void Painter::lockAspectRatio(bool lock) {
170	m_lockAspectRatio = lock;
171	forceDraw();
172}
173
174void Painter::filter(bool filter) {
175	m_filter = filter;
176	m_gl->makeCurrent();
177	if (m_filter) {
178		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
179	} else {
180		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
181	}
182	m_gl->doneCurrent();
183	forceDraw();
184}
185
186void Painter::start() {
187	m_gl->makeCurrent();
188	glEnable(GL_TEXTURE_2D);
189	glGenTextures(1, &m_tex);
190	glBindTexture(GL_TEXTURE_2D, m_tex);
191	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
192	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
193	if (m_filter) {
194		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
195	} else {
196		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
197	}
198	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
199	glEnableClientState(GL_VERTEX_ARRAY);
200	glVertexPointer(2, GL_INT, 0, _glVertices);
201	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
202	glMatrixMode(GL_PROJECTION);
203	glLoadIdentity();
204	glOrtho(0, 240, 160, 0, 0, 1);
205	glMatrixMode(GL_MODELVIEW);
206	glLoadIdentity();
207	m_gl->doneCurrent();
208
209	m_drawTimer = new QTimer;
210	m_drawTimer->moveToThread(QThread::currentThread());
211	m_drawTimer->setInterval(0);
212	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
213	m_drawTimer->start();
214}
215
216void Painter::draw() {
217	m_gl->makeCurrent();
218	if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
219		performDraw();
220	}
221	GBASyncWaitFrameEnd(&m_context->sync);
222	m_gl->swapBuffers();
223	m_gl->doneCurrent();
224}
225
226void Painter::forceDraw() {
227	m_gl->makeCurrent();
228	performDraw();
229	m_gl->swapBuffers();
230	m_gl->doneCurrent();
231}
232
233void Painter::stop() {
234	m_drawTimer->stop();
235	delete m_drawTimer;
236	m_gl->makeCurrent();
237	glDeleteTextures(1, &m_tex);
238	glClear(GL_COLOR_BUFFER_BIT);
239	m_gl->swapBuffers();
240	m_gl->doneCurrent();
241	m_gl->context()->moveToThread(QApplication::instance()->thread());
242}
243
244void Painter::pause() {
245	m_drawTimer->stop();
246	// Make sure both buffers are filled
247	forceDraw();
248	forceDraw();
249}
250
251void Painter::unpause() {
252	m_drawTimer->start();
253}
254
255void Painter::performDraw() {
256	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
257	glClear(GL_COLOR_BUFFER_BIT);
258	int w = m_size.width() * m_gl->devicePixelRatio();
259	int h = m_size.height() * m_gl->devicePixelRatio();
260	int drawW = w;
261	int drawH = h;
262	if (m_lockAspectRatio) {
263		if (w * 2 > h * 3) {
264			drawW = h * 3 / 2;
265		} else if (w * 2 < h * 3) {
266			drawH = w * 2 / 3;
267		}
268	}
269	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
270	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
271	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
272	if (m_context->sync.videoFrameWait) {
273		glFlush();
274	}
275}