all repos — mgba @ 6ebef8dc167f862eaf84cb706c961a2f23fa722a

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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 "DisplayGL.h"
  7
  8#include <QApplication>
  9#include <QResizeEvent>
 10
 11extern "C" {
 12#include "gba/supervisor/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
 31DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
 32	: Display(parent)
 33	, m_gl(new EmptyGLWidget(format, this))
 34	, m_painter(new PainterGL(m_gl))
 35	, m_drawThread(nullptr)
 36	, m_lockAspectRatio(false)
 37	, m_filter(false)
 38	, m_context(nullptr)
 39{
 40}
 41
 42DisplayGL::~DisplayGL() {
 43	delete m_painter;
 44}
 45
 46void DisplayGL::startDrawing(GBAThread* thread) {
 47	if (m_drawThread) {
 48		return;
 49	}
 50	m_painter->setContext(thread);
 51	m_context = thread;
 52	m_painter->resize(size());
 53	m_gl->move(0, 0);
 54	m_drawThread = new QThread(this);
 55	m_gl->context()->doneCurrent();
 56	m_gl->context()->moveToThread(m_drawThread);
 57	m_painter->moveToThread(m_drawThread);
 58	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 59	m_drawThread->start();
 60
 61	lockAspectRatio(m_lockAspectRatio);
 62	filter(m_filter);
 63	resizePainter();
 64}
 65
 66void DisplayGL::stopDrawing() {
 67	if (m_drawThread) {
 68		if (GBAThreadIsActive(m_context)) {
 69			GBAThreadInterrupt(m_context);
 70			GBASyncSuspendDrawing(&m_context->sync);
 71		}
 72		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 73		m_drawThread->exit();
 74		m_drawThread = nullptr;
 75		if (GBAThreadIsActive(m_context)) {
 76			GBASyncResumeDrawing(&m_context->sync);
 77			GBAThreadContinue(m_context);
 78		}
 79	}
 80}
 81
 82void DisplayGL::pauseDrawing() {
 83	if (m_drawThread) {
 84		if (GBAThreadIsActive(m_context)) {
 85			GBAThreadInterrupt(m_context);
 86			GBASyncSuspendDrawing(&m_context->sync);
 87		}
 88		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
 89		if (GBAThreadIsActive(m_context)) {
 90			GBASyncResumeDrawing(&m_context->sync);
 91			GBAThreadContinue(m_context);
 92		}
 93	}
 94}
 95
 96void DisplayGL::unpauseDrawing() {
 97	if (m_drawThread) {
 98		if (GBAThreadIsActive(m_context)) {
 99			GBAThreadInterrupt(m_context);
100			GBASyncSuspendDrawing(&m_context->sync);
101		}
102		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
103		if (GBAThreadIsActive(m_context)) {
104			GBASyncResumeDrawing(&m_context->sync);
105			GBAThreadContinue(m_context);
106		}
107	}
108}
109
110void DisplayGL::forceDraw() {
111	if (m_drawThread) {
112		QMetaObject::invokeMethod(m_painter, "forceDraw");
113	}
114}
115
116void DisplayGL::lockAspectRatio(bool lock) {
117	m_lockAspectRatio = lock;
118	if (m_drawThread) {
119		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
120	}
121}
122
123void DisplayGL::filter(bool filter) {
124	m_filter = filter;
125	if (m_drawThread) {
126		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
127	}
128}
129
130void DisplayGL::framePosted(const uint32_t* buffer) {
131	if (m_drawThread && buffer) {
132		QMetaObject::invokeMethod(m_painter, "setBacking", Q_ARG(const uint32_t*, buffer));
133	}
134}
135
136void DisplayGL::showMessage(const QString& message) {
137	if (m_drawThread) {
138		QMetaObject::invokeMethod(m_painter, "showMessage", Q_ARG(const QString&, message));
139	}
140}
141
142void DisplayGL::resizeEvent(QResizeEvent*) {
143	resizePainter();
144}
145
146void DisplayGL::resizePainter() {
147	m_gl->resize(size());
148	if (m_drawThread) {
149		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
150	}
151}
152
153PainterGL::PainterGL(QGLWidget* parent)
154	: m_gl(parent)
155	, m_drawTimer(nullptr)
156	, m_messageTimer(this)
157	, m_lockAspectRatio(false)
158	, m_filter(false)
159	, m_context(nullptr)
160{
161	m_messageFont.setFamily("Source Code Pro");
162	m_messageFont.setStyleHint(QFont::Monospace);
163	m_messageFont.setPixelSize(6);
164	connect(&m_messageTimer, SIGNAL(timeout()), this, SLOT(clearMessage()));
165	m_messageTimer.setSingleShot(true);
166	m_messageTimer.setInterval(5000);
167
168	clearMessage();
169}
170
171void PainterGL::setContext(GBAThread* context) {
172	m_context = context;
173}
174
175void PainterGL::setBacking(const uint32_t* backing) {
176	m_gl->makeCurrent();
177	glBindTexture(GL_TEXTURE_2D, m_tex);
178#ifdef COLOR_16_BIT
179#ifdef COLOR_5_6_5
180	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, backing);
181#else
182	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, backing);
183#endif
184#else
185	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, backing);
186#endif
187	m_gl->doneCurrent();
188}
189
190void PainterGL::resize(const QSize& size) {
191	m_size = size;
192	int w = m_size.width() * m_gl->devicePixelRatio();
193	int h = m_size.height() *m_gl->devicePixelRatio();
194	int drawW = w;
195	int drawH = h;
196	if (m_lockAspectRatio) {
197		if (w * 2 > h * 3) {
198			drawW = h * 3 / 2;
199		} else if (w * 2 < h * 3) {
200			drawH = w * 2 / 3;
201		}
202	}
203	m_viewport = QRect((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
204	m_painter.begin(m_gl->context()->device());
205	m_world = QTransform(
206		qreal(drawW) / VIDEO_HORIZONTAL_PIXELS, 0,
207		0, qreal(drawH) / VIDEO_VERTICAL_PIXELS,
208		m_viewport.x() / 2,
209		m_viewport.y() / 2);
210	m_painter.setWorldTransform(m_world);
211	m_painter.setFont(m_messageFont);
212	m_message.prepare(m_world, m_messageFont);
213	m_painter.end();
214	if (m_drawTimer) {
215		forceDraw();
216	}
217}
218
219void PainterGL::lockAspectRatio(bool lock) {
220	m_lockAspectRatio = lock;
221	if (m_drawTimer) {
222		forceDraw();
223	}
224}
225
226void PainterGL::filter(bool filter) {
227	m_filter = filter;
228	m_gl->makeCurrent();
229	if (m_filter) {
230		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
231	} else {
232		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233	}
234	m_gl->doneCurrent();
235	if (m_drawTimer) {
236		forceDraw();
237	}
238}
239
240void PainterGL::start() {
241	m_gl->makeCurrent();
242	glEnable(GL_TEXTURE_2D);
243	glGenTextures(1, &m_tex);
244	glBindTexture(GL_TEXTURE_2D, m_tex);
245	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
246	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
247	if (m_filter) {
248		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
249	} else {
250		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
251	}
252	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
253	glEnableClientState(GL_VERTEX_ARRAY);
254	glVertexPointer(2, GL_INT, 0, _glVertices);
255	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
256	glMatrixMode(GL_MODELVIEW);
257	glLoadIdentity();
258	glClearColor(0, 0, 0, 0);
259	glClear(GL_COLOR_BUFFER_BIT);
260	m_gl->doneCurrent();
261
262	m_drawTimer = new QTimer;
263	m_drawTimer->moveToThread(QThread::currentThread());
264	m_drawTimer->setInterval(0);
265	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
266	m_drawTimer->start();
267}
268
269void PainterGL::draw() {
270	GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip);
271	m_painter.begin(m_gl->context()->device());
272	m_painter.setWorldTransform(m_world);
273	performDraw();
274	m_painter.end();
275	GBASyncWaitFrameEnd(&m_context->sync);
276	m_gl->swapBuffers();
277}
278
279void PainterGL::forceDraw() {
280	m_painter.begin(m_gl->context()->device());
281	m_painter.setWorldTransform(m_world);
282	performDraw();
283	m_painter.end();
284	m_gl->swapBuffers();
285}
286
287void PainterGL::stop() {
288	m_drawTimer->stop();
289	delete m_drawTimer;
290	m_drawTimer = nullptr;
291	m_gl->makeCurrent();
292	glDeleteTextures(1, &m_tex);
293	glClearColor(0, 0, 0, 0);
294	glClear(GL_COLOR_BUFFER_BIT);
295	m_gl->swapBuffers();
296	m_gl->doneCurrent();
297	m_gl->context()->moveToThread(m_gl->thread());
298	moveToThread(m_gl->thread());
299}
300
301void PainterGL::pause() {
302	m_drawTimer->stop();
303	// Make sure both buffers are filled
304	forceDraw();
305	forceDraw();
306}
307
308void PainterGL::unpause() {
309	m_drawTimer->start();
310}
311
312void PainterGL::performDraw() {
313	m_painter.beginNativePainting();
314	glMatrixMode(GL_PROJECTION);
315	glLoadIdentity();
316	glOrtho(0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, 0, 1);
317	glMatrixMode(GL_MODELVIEW);
318	glLoadIdentity();
319	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
320	glClearColor(0, 0, 0, 0);
321	glClear(GL_COLOR_BUFFER_BIT);
322	glViewport(m_viewport.x(), m_viewport.y(), m_viewport.width(), m_viewport.height());
323	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
324	if (m_context->sync.videoFrameWait) {
325		glFlush();
326	}
327	m_painter.endNativePainting();
328	m_painter.setRenderHint(QPainter::Antialiasing);
329	m_painter.setFont(m_messageFont);
330	m_painter.setPen(Qt::black);
331	m_painter.translate(1, 72);
332	for (int i = 0; i < 16; ++i) {
333		m_painter.save();
334		m_painter.translate(cos(i * M_PI / 8.0) * 0.4, sin(i * M_PI / 8.0) * 0.4);
335		m_painter.drawStaticText(0, 0, m_message);
336		m_painter.restore();
337	}
338	m_painter.setPen(Qt::white);
339	m_painter.drawStaticText(0, 0, m_message);
340}
341
342void PainterGL::showMessage(const QString& message) {
343	m_message.setText(message);
344	m_message.prepare(m_world, m_messageFont);
345	m_messageTimer.stop();
346	m_messageTimer.start();
347}
348
349void PainterGL::clearMessage() {
350	m_message.setText(QString());
351}