all repos — mgba @ 12505766e1ab83954ab216a5828cd064834056e8

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(13);
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();
193	int h = m_size.height();
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.reset();
206	m_world.translate(m_viewport.x(), m_viewport.y());
207	m_world.scale(qreal(drawW) / VIDEO_HORIZONTAL_PIXELS, qreal(drawH) / VIDEO_VERTICAL_PIXELS);
208	m_painter.setWorldTransform(m_world);
209	m_painter.setFont(m_messageFont);
210	m_message.prepare(m_world, m_messageFont);
211	m_painter.end();
212	if (m_drawTimer) {
213		forceDraw();
214	}
215}
216
217void PainterGL::lockAspectRatio(bool lock) {
218	m_lockAspectRatio = lock;
219	if (m_drawTimer) {
220		forceDraw();
221	}
222}
223
224void PainterGL::filter(bool filter) {
225	m_filter = filter;
226	m_gl->makeCurrent();
227	if (m_filter) {
228		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
229	} else {
230		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
231	}
232	m_gl->doneCurrent();
233	if (m_drawTimer) {
234		forceDraw();
235	}
236}
237
238void PainterGL::start() {
239	m_gl->makeCurrent();
240	glEnable(GL_TEXTURE_2D);
241	glGenTextures(1, &m_tex);
242	glBindTexture(GL_TEXTURE_2D, m_tex);
243	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
244	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
245	if (m_filter) {
246		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
247	} else {
248		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
249	}
250	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
251	glEnableClientState(GL_VERTEX_ARRAY);
252	glVertexPointer(2, GL_INT, 0, _glVertices);
253	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
254	glMatrixMode(GL_MODELVIEW);
255	glLoadIdentity();
256	glClearColor(0, 0, 0, 0);
257	glClear(GL_COLOR_BUFFER_BIT);
258	m_gl->doneCurrent();
259
260	m_drawTimer = new QTimer;
261	m_drawTimer->moveToThread(QThread::currentThread());
262	m_drawTimer->setInterval(0);
263	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
264	m_drawTimer->start();
265}
266
267void PainterGL::draw() {
268	GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip);
269	m_painter.begin(m_gl->context()->device());
270	m_painter.setWorldTransform(m_world);
271	performDraw();
272	m_painter.end();
273	GBASyncWaitFrameEnd(&m_context->sync);
274	m_gl->swapBuffers();
275}
276
277void PainterGL::forceDraw() {
278	m_painter.begin(m_gl->context()->device());
279	m_painter.setWorldTransform(m_world);
280	performDraw();
281	m_painter.end();
282	m_gl->swapBuffers();
283}
284
285void PainterGL::stop() {
286	m_drawTimer->stop();
287	delete m_drawTimer;
288	m_drawTimer = nullptr;
289	m_gl->makeCurrent();
290	glDeleteTextures(1, &m_tex);
291	glClearColor(0, 0, 0, 0);
292	glClear(GL_COLOR_BUFFER_BIT);
293	m_gl->swapBuffers();
294	m_gl->doneCurrent();
295	m_gl->context()->moveToThread(m_gl->thread());
296	moveToThread(m_gl->thread());
297}
298
299void PainterGL::pause() {
300	m_drawTimer->stop();
301	// Make sure both buffers are filled
302	forceDraw();
303	forceDraw();
304}
305
306void PainterGL::unpause() {
307	m_drawTimer->start();
308}
309
310void PainterGL::performDraw() {
311	m_painter.beginNativePainting();
312	glMatrixMode(GL_PROJECTION);
313	glLoadIdentity();
314	glOrtho(0, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0, 0, 1);
315	glMatrixMode(GL_MODELVIEW);
316	glLoadIdentity();
317	glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
318	glClearColor(0, 0, 0, 0);
319	glClear(GL_COLOR_BUFFER_BIT);
320	QRect viewport(m_viewport.topLeft() * m_gl->devicePixelRatio(), m_viewport.size() * m_gl->devicePixelRatio());
321	glViewport(viewport.x(), viewport.y(), viewport.width(), viewport.height());
322	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
323	if (m_context->sync.videoFrameWait) {
324		glFlush();
325	}
326	m_painter.endNativePainting();
327	m_painter.setRenderHint(QPainter::Antialiasing);
328	m_painter.setFont(m_messageFont);
329	m_painter.setPen(Qt::black);
330	m_painter.translate(1, VIDEO_VERTICAL_PIXELS - m_messageFont.pixelSize() - 1);
331	for (int i = 0; i < 16; ++i) {
332		m_painter.save();
333		m_painter.translate(cos(i * M_PI / 8.0) * 0.8, sin(i * M_PI / 8.0) * 0.8);
334		m_painter.drawStaticText(0, 0, m_message);
335		m_painter.restore();
336	}
337	m_painter.setPen(Qt::white);
338	m_painter.drawStaticText(0, 0, m_message);
339}
340
341void PainterGL::showMessage(const QString& message) {
342	m_message.setText(message);
343	m_message.prepare(m_world, m_messageFont);
344	m_messageTimer.stop();
345	m_messageTimer.start();
346}
347
348void PainterGL::clearMessage() {
349	m_message.setText(QString());
350}