all repos — mgba @ e9a2b2a57c4f478fb3600fe9d87e1b97804d73de

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	, m_filter(false)
153{
154	m_size = parent->size();
155}
156
157void Painter::setContext(GBAThread* context) {
158	m_context = context;
159}
160
161void Painter::setBacking(const uint32_t* backing) {
162	m_backing = backing;
163}
164
165void Painter::resize(const QSize& size) {
166	m_size = size;
167	forceDraw();
168	forceDraw();
169}
170
171void Painter::lockAspectRatio(bool lock) {
172	m_lockAspectRatio = lock;
173	forceDraw();
174	forceDraw();
175}
176
177void Painter::filter(bool filter) {
178	m_filter = filter;
179	m_gl->makeCurrent();
180	if (m_filter) {
181		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
182	} else {
183		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
184	}
185	m_gl->doneCurrent();
186	forceDraw();
187}
188
189void Painter::start() {
190	m_gl->makeCurrent();
191	glEnable(GL_TEXTURE_2D);
192	glGenTextures(1, &m_tex);
193	glBindTexture(GL_TEXTURE_2D, m_tex);
194	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
195	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
196	if (m_filter) {
197		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
198	} else {
199		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
200	}
201	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
202	glEnableClientState(GL_VERTEX_ARRAY);
203	glVertexPointer(2, GL_INT, 0, _glVertices);
204	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
205	glMatrixMode(GL_PROJECTION);
206	glLoadIdentity();
207	glOrtho(0, 240, 160, 0, 0, 1);
208	glMatrixMode(GL_MODELVIEW);
209	glLoadIdentity();
210	m_gl->doneCurrent();
211
212	m_drawTimer = new QTimer;
213	m_drawTimer->moveToThread(QThread::currentThread());
214	m_drawTimer->setInterval(0);
215	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
216	m_drawTimer->start();
217}
218
219void Painter::draw() {
220	m_gl->makeCurrent();
221	if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
222		performDraw();
223	}
224	GBASyncWaitFrameEnd(&m_context->sync);
225	m_gl->swapBuffers();
226	m_gl->doneCurrent();
227}
228
229void Painter::forceDraw() {
230	m_gl->makeCurrent();
231	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
232	glClear(GL_COLOR_BUFFER_BIT);
233	performDraw();
234	m_gl->swapBuffers();
235	m_gl->doneCurrent();
236}
237
238void Painter::stop() {
239	m_drawTimer->stop();
240	delete m_drawTimer;
241	m_gl->makeCurrent();
242	glDeleteTextures(1, &m_tex);
243	glClear(GL_COLOR_BUFFER_BIT);
244	m_gl->swapBuffers();
245	m_gl->doneCurrent();
246	m_gl->context()->moveToThread(QApplication::instance()->thread());
247}
248
249void Painter::pause() {
250	m_drawTimer->stop();
251	// Make sure both buffers are filled
252	forceDraw();
253	forceDraw();
254}
255
256void Painter::unpause() {
257	m_drawTimer->start();
258}
259
260void Painter::performDraw() {
261	int w = m_size.width() * m_gl->devicePixelRatio();
262	int h = m_size.height() * m_gl->devicePixelRatio();
263#ifndef Q_OS_MAC
264	// TODO: This seems to cause framerates to drag down to 120 FPS on OS X,
265	// even if the emulator can go faster. Look into why.
266	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
267	glClear(GL_COLOR_BUFFER_BIT);
268#endif
269	int drawW = w;
270	int drawH = h;
271	if (m_lockAspectRatio) {
272		if (w * 2 > h * 3) {
273			drawW = h * 3 / 2;
274		} else if (w * 2 < h * 3) {
275			drawH = w * 2 / 3;
276		}
277	}
278	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
279	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
280	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
281	if (m_context->sync.videoFrameWait) {
282		glFlush();
283	}
284}