all repos — mgba @ 096f5ca27c52d1d0aa14b5641791c9b24ac88f3a

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#ifdef BUILD_GL
 15#include "platform/opengl/gl.h"
 16#endif
 17#if !defined(_WIN32) || defined(USE_EPOXY)
 18#include "platform/opengl/gles2.h"
 19#ifdef _WIN32 
 20#include <epoxy/wgl.h>
 21#endif
 22#endif
 23}
 24
 25using namespace QGBA;
 26
 27DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
 28	: Display(parent)
 29	, m_isDrawing(false)
 30	, m_gl(new EmptyGLWidget(format, this))
 31	, m_drawThread(nullptr)
 32	, m_context(nullptr)
 33{
 34	m_painter = new PainterGL(m_gl, QGLFormat::openGLVersionFlags());
 35	m_gl->setMouseTracking(true);
 36	m_gl->setAttribute(Qt::WA_TransparentForMouseEvents); // This doesn't seem to work?
 37}
 38
 39DisplayGL::~DisplayGL() {
 40	delete m_painter;
 41}
 42
 43bool DisplayGL::supportsShaders() const {
 44	return m_painter->supportsShaders();
 45}
 46
 47void DisplayGL::startDrawing(GBAThread* thread) {
 48	if (m_drawThread) {
 49		return;
 50	}
 51	m_isDrawing = true;
 52	m_painter->setContext(thread);
 53	m_painter->setMessagePainter(messagePainter());
 54	m_context = thread;
 55	m_painter->resize(size());
 56	m_gl->move(0, 0);
 57	m_drawThread = new QThread(this);
 58	m_drawThread->setObjectName("Painter Thread");
 59	m_gl->context()->doneCurrent();
 60	m_gl->context()->moveToThread(m_drawThread);
 61	m_painter->moveToThread(m_drawThread);
 62	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 63	m_drawThread->start();
 64	GBASyncSetVideoSync(&m_context->sync, false);
 65
 66	lockAspectRatio(isAspectRatioLocked());
 67	filter(isFiltered());
 68	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
 69	resizePainter();
 70}
 71
 72void DisplayGL::stopDrawing() {
 73	if (m_drawThread) {
 74		m_isDrawing = false;
 75		if (GBAThreadIsActive(m_context)) {
 76			GBAThreadInterrupt(m_context);
 77		}
 78		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 79		m_drawThread->exit();
 80		m_drawThread = nullptr;
 81		if (GBAThreadIsActive(m_context)) {
 82			GBAThreadContinue(m_context);
 83		}
 84	}
 85}
 86
 87void DisplayGL::pauseDrawing() {
 88	if (m_drawThread) {
 89		m_isDrawing = false;
 90		if (GBAThreadIsActive(m_context)) {
 91			GBAThreadInterrupt(m_context);
 92		}
 93		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
 94		if (GBAThreadIsActive(m_context)) {
 95			GBAThreadContinue(m_context);
 96		}
 97	}
 98}
 99
100void DisplayGL::unpauseDrawing() {
101	if (m_drawThread) {
102		m_isDrawing = true;
103		if (GBAThreadIsActive(m_context)) {
104			GBAThreadInterrupt(m_context);
105		}
106		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
107		if (GBAThreadIsActive(m_context)) {
108			GBAThreadContinue(m_context);
109		}
110	}
111}
112
113void DisplayGL::forceDraw() {
114	if (m_drawThread) {
115		QMetaObject::invokeMethod(m_painter, "forceDraw");
116	}
117}
118
119void DisplayGL::lockAspectRatio(bool lock) {
120	Display::lockAspectRatio(lock);
121	if (m_drawThread) {
122		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
123	}
124}
125
126void DisplayGL::filter(bool filter) {
127	Display::filter(filter);
128	if (m_drawThread) {
129		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
130	}
131}
132
133void DisplayGL::framePosted(const uint32_t* buffer) {
134	if (m_drawThread && buffer) {
135		m_painter->enqueue(buffer);
136		QMetaObject::invokeMethod(m_painter, "draw");
137	}
138}
139
140void DisplayGL::setShaders(struct VDir* shaders) {
141	QMetaObject::invokeMethod(m_painter, "setShaders", Q_ARG(struct VDir*, shaders));
142}
143
144void DisplayGL::resizeEvent(QResizeEvent* event) {
145	Display::resizeEvent(event);
146	resizePainter();
147}
148
149void DisplayGL::resizePainter() {
150	m_gl->resize(size());
151	if (m_drawThread) {
152		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
153	}
154}
155
156PainterGL::PainterGL(QGLWidget* parent, QGLFormat::OpenGLVersionFlags glVersion)
157	: m_gl(parent)
158	, m_active(false)
159	, m_started(false)
160	, m_context(nullptr)
161	, m_shaders(nullptr)
162	, m_nShaders(0)
163	, m_messagePainter(nullptr)
164{
165#ifdef BUILD_GL
166	GBAGLContext* glBackend;
167#endif
168#if !defined(_WIN32) || defined(USE_EPOXY)
169	GBAGLES2Context* gl2Backend;
170#endif
171
172#if !defined(_WIN32) || defined(USE_EPOXY)
173	if (glVersion & QGLFormat::OpenGL_Version_3_0) {
174		gl2Backend = new GBAGLES2Context;
175		GBAGLES2ContextCreate(gl2Backend);
176		m_backend = &gl2Backend->d;
177		m_supportsShaders = true;
178	} else {
179#else
180	{
181#endif
182		glBackend = new GBAGLContext;
183		GBAGLContextCreate(glBackend);
184		m_backend = &glBackend->d;
185		m_supportsShaders = false;
186	}
187	m_backend->swap = [](VideoBackend* v) {
188		PainterGL* painter = static_cast<PainterGL*>(v->user);
189		painter->m_gl->swapBuffers();
190	};
191	m_backend->user = this;
192	m_backend->filter = false;
193	m_backend->lockAspectRatio = false;
194
195	for (int i = 0; i < 2; ++i) {
196		m_free.append(new uint32_t[256 * 256]);
197	}
198}
199
200PainterGL::~PainterGL() {
201	while (!m_queue.isEmpty()) {
202		delete[] m_queue.dequeue();
203	}
204	for (auto item : m_free) {
205		delete[] item;
206	}
207	delete m_backend;
208	m_backend = nullptr;
209}
210
211void PainterGL::setContext(GBAThread* context) {
212	m_context = context;
213}
214
215void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
216	m_messagePainter = messagePainter;
217}
218
219void PainterGL::resize(const QSize& size) {
220	m_size = size;
221	if (m_started && !m_active) {
222		forceDraw();
223	}
224}
225
226void PainterGL::lockAspectRatio(bool lock) {
227	m_backend->lockAspectRatio = lock;
228	if (m_started && !m_active) {
229		forceDraw();
230	}
231}
232
233void PainterGL::filter(bool filter) {
234	m_backend->filter = filter;
235	if (m_started && !m_active) {
236		forceDraw();
237	}
238}
239
240void PainterGL::start() {
241	m_gl->makeCurrent();
242#if defined(_WIN32) && defined(USE_EPOXY)
243	epoxy_handle_external_wglMakeCurrent();
244#endif
245	m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
246
247#if !defined(_WIN32) || defined(USE_EPOXY)
248	if (m_shaders) {
249		GBAGLES2ShaderAttach(reinterpret_cast<GBAGLES2Context*>(m_backend), m_shaders, m_nShaders);
250	}
251#endif
252
253	m_gl->doneCurrent();
254	m_active = true;
255	m_started = true;
256}
257
258void PainterGL::draw() {
259	if (m_queue.isEmpty() || !GBAThreadIsActive(m_context)) {
260		return;
261	}
262	if (GBASyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
263		dequeue();
264		m_painter.begin(m_gl->context()->device());
265		performDraw();
266		m_painter.end();
267		GBASyncWaitFrameEnd(&m_context->sync);
268		m_backend->swap(m_backend);
269	} else {
270		GBASyncWaitFrameEnd(&m_context->sync);
271	}
272	if (!m_queue.isEmpty()) {
273		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
274	}
275}
276
277void PainterGL::forceDraw() {
278	m_painter.begin(m_gl->context()->device());
279	performDraw();
280	m_painter.end();
281	m_backend->swap(m_backend);
282}
283
284void PainterGL::stop() {
285	m_active = false;
286	m_started = false;
287	m_gl->makeCurrent();
288#if defined(_WIN32) && defined(USE_EPOXY)
289	epoxy_handle_external_wglMakeCurrent();
290#endif
291	dequeueAll();
292	m_backend->clear(m_backend);
293	m_backend->swap(m_backend);
294	m_backend->deinit(m_backend);
295	m_gl->doneCurrent();
296	m_gl->context()->moveToThread(m_gl->thread());
297	moveToThread(m_gl->thread());
298}
299
300void PainterGL::pause() {
301	m_active = false;
302}
303
304void PainterGL::unpause() {
305	m_active = true;
306}
307
308void PainterGL::performDraw() {
309	m_painter.beginNativePainting();
310	float r = m_gl->devicePixelRatio();
311	m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
312	m_backend->drawFrame(m_backend);
313	m_painter.endNativePainting();
314	if (m_messagePainter) {
315		m_messagePainter->paint(&m_painter);
316	}
317}
318
319void PainterGL::enqueue(const uint32_t* backing) {
320	m_mutex.lock();
321	uint32_t* buffer;
322	if (m_free.isEmpty()) {
323		buffer = m_queue.dequeue();
324	} else {
325		buffer = m_free.takeLast();
326	}
327	memcpy(buffer, backing, 256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
328	m_queue.enqueue(buffer);
329	m_mutex.unlock();
330}
331
332void PainterGL::dequeue() {
333	m_mutex.lock();
334	if (m_queue.isEmpty()) {
335		m_mutex.unlock();
336		return;
337	}
338	uint32_t* buffer = m_queue.dequeue();
339	m_backend->postFrame(m_backend, buffer);
340	m_free.append(buffer);
341	m_mutex.unlock();
342}
343
344void PainterGL::dequeueAll() {
345	uint32_t* buffer = 0;
346	m_mutex.lock();
347	while (!m_queue.isEmpty()) {
348		buffer = m_queue.dequeue();
349		m_free.append(buffer);
350	}
351	if (buffer) {
352		m_backend->postFrame(m_backend, buffer);
353	}
354	m_mutex.unlock();
355}
356
357void PainterGL::setShaders(struct VDir* dir) {
358	if (!supportsShaders()) {
359		return;
360	}
361#if !defined(_WIN32) || defined(USE_EPOXY)
362	m_gl->makeCurrent();
363#if defined(_WIN32) && defined(USE_EPOXY)
364	epoxy_handle_external_wglMakeCurrent();
365#endif
366	if (m_shaders) {
367		GBAGLES2ShaderDetach(reinterpret_cast<GBAGLES2Context*>(m_backend));
368	}
369	GBAGLES2ShaderLoad(&m_shaders, &m_nShaders, nullptr, dir);
370	if (m_started) {
371		GBAGLES2ShaderAttach(reinterpret_cast<GBAGLES2Context*>(m_backend), m_shaders, m_nShaders);
372	}
373	m_gl->doneCurrent();
374#endif
375}