all repos — mgba @ 7cf067ac144ec77c45d7707bc78b289632ce1c1a

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
 47VideoShader* DisplayGL::shaders() {
 48	VideoShader* shaders = nullptr;
 49	if (m_drawThread) {
 50		QMetaObject::invokeMethod(m_painter, "shaders", Qt::BlockingQueuedConnection, Q_RETURN_ARG(VideoShader*, shaders));
 51	} else {
 52		shaders = m_painter->shaders();
 53	}
 54	return shaders;
 55}
 56
 57void DisplayGL::startDrawing(GBAThread* thread) {
 58	if (m_drawThread) {
 59		return;
 60	}
 61	m_isDrawing = true;
 62	m_painter->setContext(thread);
 63	m_painter->setMessagePainter(messagePainter());
 64	m_context = thread;
 65	m_painter->resize(size());
 66	m_gl->move(0, 0);
 67	m_drawThread = new QThread(this);
 68	m_drawThread->setObjectName("Painter Thread");
 69	m_gl->context()->doneCurrent();
 70	m_gl->context()->moveToThread(m_drawThread);
 71	m_painter->moveToThread(m_drawThread);
 72	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 73	m_drawThread->start();
 74	GBASyncSetVideoSync(&m_context->sync, false);
 75
 76	lockAspectRatio(isAspectRatioLocked());
 77	filter(isFiltered());
 78	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
 79	resizePainter();
 80}
 81
 82void DisplayGL::stopDrawing() {
 83	if (m_drawThread) {
 84		m_isDrawing = false;
 85		if (GBAThreadIsActive(m_context)) {
 86			GBAThreadInterrupt(m_context);
 87		}
 88		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 89		m_drawThread->exit();
 90		m_drawThread = nullptr;
 91		if (GBAThreadIsActive(m_context)) {
 92			GBAThreadContinue(m_context);
 93		}
 94	}
 95}
 96
 97void DisplayGL::pauseDrawing() {
 98	if (m_drawThread) {
 99		m_isDrawing = false;
100		if (GBAThreadIsActive(m_context)) {
101			GBAThreadInterrupt(m_context);
102		}
103		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
104		if (GBAThreadIsActive(m_context)) {
105			GBAThreadContinue(m_context);
106		}
107	}
108}
109
110void DisplayGL::unpauseDrawing() {
111	if (m_drawThread) {
112		m_isDrawing = true;
113		if (GBAThreadIsActive(m_context)) {
114			GBAThreadInterrupt(m_context);
115		}
116		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
117		if (GBAThreadIsActive(m_context)) {
118			GBAThreadContinue(m_context);
119		}
120	}
121}
122
123void DisplayGL::forceDraw() {
124	if (m_drawThread) {
125		QMetaObject::invokeMethod(m_painter, "forceDraw");
126	}
127}
128
129void DisplayGL::lockAspectRatio(bool lock) {
130	Display::lockAspectRatio(lock);
131	if (m_drawThread) {
132		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
133	}
134}
135
136void DisplayGL::filter(bool filter) {
137	Display::filter(filter);
138	if (m_drawThread) {
139		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
140	}
141}
142
143void DisplayGL::framePosted(const uint32_t* buffer) {
144	if (m_drawThread && buffer) {
145		m_painter->enqueue(buffer);
146		QMetaObject::invokeMethod(m_painter, "draw");
147	}
148}
149
150void DisplayGL::setShaders(struct VDir* shaders) {
151	if (m_drawThread) {
152		QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
153	} else {
154		m_painter->setShaders(shaders);
155	}
156}
157
158void DisplayGL::clearShaders() {
159	QMetaObject::invokeMethod(m_painter, "clearShaders");
160}
161
162void DisplayGL::resizeEvent(QResizeEvent* event) {
163	Display::resizeEvent(event);
164	resizePainter();
165}
166
167void DisplayGL::resizePainter() {
168	m_gl->resize(size());
169	if (m_drawThread) {
170		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
171	}
172}
173
174PainterGL::PainterGL(QGLWidget* parent, QGLFormat::OpenGLVersionFlags glVersion)
175	: m_gl(parent)
176	, m_active(false)
177	, m_started(false)
178	, m_context(nullptr)
179	, m_shader{}
180	, m_backend(nullptr)
181	, m_messagePainter(nullptr)
182{
183#ifdef BUILD_GL
184	GBAGLContext* glBackend;
185#endif
186#if !defined(_WIN32) || defined(USE_EPOXY)
187	GBAGLES2Context* gl2Backend;
188#endif
189
190#if !defined(_WIN32) || defined(USE_EPOXY)
191	if (glVersion & QGLFormat::OpenGL_Version_3_0) {
192		gl2Backend = new GBAGLES2Context;
193		GBAGLES2ContextCreate(gl2Backend);
194		m_backend = &gl2Backend->d;
195		m_supportsShaders = true;
196	}
197#endif
198
199#ifdef BUILD_GL
200	 if (!m_backend) {
201		glBackend = new GBAGLContext;
202		GBAGLContextCreate(glBackend);
203		m_backend = &glBackend->d;
204		m_supportsShaders = false;
205	}
206#endif
207	m_backend->swap = [](VideoBackend* v) {
208		PainterGL* painter = static_cast<PainterGL*>(v->user);
209		painter->m_gl->swapBuffers();
210	};
211
212	m_gl->makeCurrent();
213#if defined(_WIN32) && defined(USE_EPOXY)
214	epoxy_handle_external_wglMakeCurrent();
215#endif
216	m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
217#if !defined(_WIN32) || defined(USE_EPOXY)
218	if (m_supportsShaders) {
219		m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<GBAGLES2Context*>(m_backend)->initialShader);
220	}
221#endif
222	m_gl->doneCurrent();
223
224	m_backend->user = this;
225	m_backend->filter = false;
226	m_backend->lockAspectRatio = false;
227
228	for (int i = 0; i < 2; ++i) {
229		m_free.append(new uint32_t[256 * 256]);
230	}
231}
232
233PainterGL::~PainterGL() {
234	while (!m_queue.isEmpty()) {
235		delete[] m_queue.dequeue();
236	}
237	for (auto item : m_free) {
238		delete[] item;
239	}
240	m_gl->makeCurrent();
241#if defined(_WIN32) && defined(USE_EPOXY)
242	epoxy_handle_external_wglMakeCurrent();
243#endif
244#if !defined(_WIN32) || defined(USE_EPOXY)
245	if (m_shader.passes) {
246		GBAGLES2ShaderFree(&m_shader);
247	}
248#endif
249	m_backend->deinit(m_backend);
250	m_gl->doneCurrent();
251	delete m_backend;
252	m_backend = nullptr;
253}
254
255void PainterGL::setContext(GBAThread* context) {
256	m_context = context;
257}
258
259void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
260	m_messagePainter = messagePainter;
261}
262
263void PainterGL::resize(const QSize& size) {
264	m_size = size;
265	if (m_started && !m_active) {
266		forceDraw();
267	}
268}
269
270void PainterGL::lockAspectRatio(bool lock) {
271	m_backend->lockAspectRatio = lock;
272	if (m_started && !m_active) {
273		forceDraw();
274	}
275}
276
277void PainterGL::filter(bool filter) {
278	m_backend->filter = filter;
279	if (m_started && !m_active) {
280		forceDraw();
281	}
282}
283
284void PainterGL::start() {
285	m_gl->makeCurrent();
286#if defined(_WIN32) && defined(USE_EPOXY)
287	epoxy_handle_external_wglMakeCurrent();
288#endif
289
290#if !defined(_WIN32) || defined(USE_EPOXY)
291	if (m_supportsShaders && m_shader.passes) {
292		GBAGLES2ShaderAttach(reinterpret_cast<GBAGLES2Context*>(m_backend), static_cast<GBAGLES2Shader*>(m_shader.passes), m_shader.nPasses);
293	}
294#endif
295
296	m_gl->doneCurrent();
297	m_active = true;
298	m_started = true;
299}
300
301void PainterGL::draw() {
302	if (m_queue.isEmpty() || !GBAThreadIsActive(m_context)) {
303		return;
304	}
305	if (GBASyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
306		dequeue();
307		GBASyncWaitFrameEnd(&m_context->sync);
308		m_painter.begin(m_gl->context()->device());
309		performDraw();
310		m_painter.end();
311		m_backend->swap(m_backend);
312	} else {
313		GBASyncWaitFrameEnd(&m_context->sync);
314	}
315	if (!m_queue.isEmpty()) {
316		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
317	}
318}
319
320void PainterGL::forceDraw() {
321	m_painter.begin(m_gl->context()->device());
322	performDraw();
323	m_painter.end();
324	m_backend->swap(m_backend);
325}
326
327void PainterGL::stop() {
328	m_active = false;
329	m_started = false;
330	m_gl->makeCurrent();
331#if defined(_WIN32) && defined(USE_EPOXY)
332	epoxy_handle_external_wglMakeCurrent();
333#endif
334	dequeueAll();
335	m_backend->clear(m_backend);
336	m_backend->swap(m_backend);
337	m_gl->doneCurrent();
338	m_gl->context()->moveToThread(m_gl->thread());
339	moveToThread(m_gl->thread());
340}
341
342void PainterGL::pause() {
343	m_active = false;
344}
345
346void PainterGL::unpause() {
347	m_active = true;
348}
349
350void PainterGL::performDraw() {
351	m_painter.beginNativePainting();
352	float r = m_gl->devicePixelRatio();
353	m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
354	m_backend->drawFrame(m_backend);
355	m_painter.endNativePainting();
356	if (m_messagePainter) {
357		m_messagePainter->paint(&m_painter);
358	}
359}
360
361void PainterGL::enqueue(const uint32_t* backing) {
362	m_mutex.lock();
363	uint32_t* buffer;
364	if (m_free.isEmpty()) {
365		buffer = m_queue.dequeue();
366	} else {
367		buffer = m_free.takeLast();
368	}
369	memcpy(buffer, backing, 256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
370	m_queue.enqueue(buffer);
371	m_mutex.unlock();
372}
373
374void PainterGL::dequeue() {
375	m_mutex.lock();
376	if (m_queue.isEmpty()) {
377		m_mutex.unlock();
378		return;
379	}
380	uint32_t* buffer = m_queue.dequeue();
381	m_backend->postFrame(m_backend, buffer);
382	m_free.append(buffer);
383	m_mutex.unlock();
384}
385
386void PainterGL::dequeueAll() {
387	uint32_t* buffer = 0;
388	m_mutex.lock();
389	while (!m_queue.isEmpty()) {
390		buffer = m_queue.dequeue();
391		m_free.append(buffer);
392	}
393	if (buffer) {
394		m_backend->postFrame(m_backend, buffer);
395	}
396	m_mutex.unlock();
397}
398
399void PainterGL::setShaders(struct VDir* dir) {
400	if (!supportsShaders()) {
401		return;
402	}
403#if !defined(_WIN32) || defined(USE_EPOXY)
404	m_gl->makeCurrent();
405#if defined(_WIN32) && defined(USE_EPOXY)
406	epoxy_handle_external_wglMakeCurrent();
407#endif
408	if (m_shader.passes) {
409		GBAGLES2ShaderDetach(reinterpret_cast<GBAGLES2Context*>(m_backend));
410		GBAGLES2ShaderFree(&m_shader);
411	}
412	GBAGLES2ShaderLoad(&m_shader, dir);
413	if (m_started) {
414		GBAGLES2ShaderAttach(reinterpret_cast<GBAGLES2Context*>(m_backend), static_cast<GBAGLES2Shader*>(m_shader.passes), m_shader.nPasses);
415	}
416	m_gl->doneCurrent();
417#endif
418}
419
420void PainterGL::clearShaders() {
421	if (!supportsShaders()) {
422		return;
423	}
424#if !defined(_WIN32) || defined(USE_EPOXY)
425	m_gl->makeCurrent();
426#if defined(_WIN32) && defined(USE_EPOXY)
427	epoxy_handle_external_wglMakeCurrent();
428#endif
429	if (m_shader.passes) {
430		GBAGLES2ShaderDetach(reinterpret_cast<GBAGLES2Context*>(m_backend));
431		GBAGLES2ShaderFree(&m_shader);
432	}
433	m_gl->doneCurrent();
434#endif
435}
436
437VideoShader* PainterGL::shaders() {
438	return &m_shader;
439}