all repos — mgba @ 53d9e6b43213d9eed2c7bc5b38d16f4fdad37bc3

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#if defined(BUILD_GL) || defined(BUILD_GLES2)
  9
 10#include "CoreController.h"
 11
 12#include <QApplication>
 13#include <QOpenGLContext>
 14#include <QOpenGLPaintDevice>
 15#include <QResizeEvent>
 16#include <QTimer>
 17#include <QWindow>
 18
 19#include <mgba/core/core.h>
 20#include <mgba-util/math.h>
 21#ifdef BUILD_GL
 22#include "platform/opengl/gl.h"
 23#endif
 24#ifdef BUILD_GLES2
 25#include "platform/opengl/gles2.h"
 26#ifdef _WIN32
 27#include <epoxy/wgl.h>
 28#endif
 29#endif
 30
 31using namespace QGBA;
 32
 33DisplayGL::DisplayGL(const QSurfaceFormat& format, QWidget* parent)
 34	: Display(parent)
 35	, m_gl(nullptr)
 36{
 37	setAttribute(Qt::WA_NativeWindow);
 38
 39	// This can spontaneously re-enter into this->resizeEvent before creation is done, so we
 40	// need to make sure it's initialized to nullptr before we assign the new object to it
 41	m_gl = new QOpenGLContext;
 42	m_gl->setFormat(format);
 43	m_gl->create();
 44
 45	m_gl->makeCurrent(windowHandle());
 46#if defined(_WIN32) && defined(USE_EPOXY)
 47	epoxy_handle_external_wglMakeCurrent();
 48#endif
 49	int majorVersion = m_gl->format().majorVersion();
 50	QStringList extensions = QString(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))).split(' ');
 51	m_gl->doneCurrent();
 52
 53	if (majorVersion == 2 && !extensions.contains("GL_ARB_framebuffer_object")) {
 54		QSurfaceFormat newFormat(format);
 55		newFormat.setVersion(1, 4);
 56		m_gl->setFormat(newFormat);
 57		m_gl->create();
 58	}
 59
 60	m_painter = new PainterGL(&m_videoProxy, windowHandle(), m_gl);
 61	setUpdatesEnabled(false); // Prevent paint events, which can cause race conditions
 62
 63	connect(&m_videoProxy, &VideoProxy::dataAvailable, &m_videoProxy, &VideoProxy::processData);
 64	connect(&m_videoProxy, &VideoProxy::eventPosted, &m_videoProxy, &VideoProxy::handleEvent);
 65}
 66
 67DisplayGL::~DisplayGL() {
 68	stopDrawing();
 69	delete m_painter;
 70	delete m_gl;
 71}
 72
 73bool DisplayGL::supportsShaders() const {
 74	return m_painter->supportsShaders();
 75}
 76
 77VideoShader* DisplayGL::shaders() {
 78	VideoShader* shaders = nullptr;
 79	if (m_drawThread) {
 80		QMetaObject::invokeMethod(m_painter, "shaders", Qt::BlockingQueuedConnection, Q_RETURN_ARG(VideoShader*, shaders));
 81	} else {
 82		shaders = m_painter->shaders();
 83	}
 84	return shaders;
 85}
 86
 87void DisplayGL::startDrawing(std::shared_ptr<CoreController> controller) {
 88	if (m_drawThread) {
 89		return;
 90	}
 91	m_isDrawing = true;
 92	m_painter->setContext(controller);
 93	m_painter->setMessagePainter(messagePainter());
 94	m_context = controller;
 95	m_painter->resize(size());
 96	m_drawThread = new QThread(this);
 97	m_drawThread->setObjectName("Painter Thread");
 98	m_gl->doneCurrent();
 99	m_gl->moveToThread(m_drawThread);
100	m_painter->moveToThread(m_drawThread);
101	m_videoProxy.moveToThread(m_drawThread);
102	connect(m_drawThread, &QThread::started, m_painter, &PainterGL::start);
103	m_drawThread->start();
104
105	lockAspectRatio(isAspectRatioLocked());
106	lockIntegerScaling(isIntegerScalingLocked());
107	filter(isFiltered());
108#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
109	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF());
110#else
111	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
112#endif
113	resizePainter();
114}
115
116void DisplayGL::stopDrawing() {
117	if (m_drawThread) {
118		m_isDrawing = false;
119		CoreController::Interrupter interrupter(m_context);
120		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
121		m_drawThread->exit();
122		m_drawThread = nullptr;
123
124		m_gl->makeCurrent(windowHandle());
125#if defined(_WIN32) && defined(USE_EPOXY)
126		epoxy_handle_external_wglMakeCurrent();
127#endif
128	}
129	m_context.reset();
130}
131
132void DisplayGL::pauseDrawing() {
133	if (m_drawThread) {
134		m_isDrawing = false;
135		CoreController::Interrupter interrupter(m_context);
136		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
137	}
138}
139
140void DisplayGL::unpauseDrawing() {
141	if (m_drawThread) {
142		m_isDrawing = true;
143		CoreController::Interrupter interrupter(m_context);
144		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
145	}
146}
147
148void DisplayGL::forceDraw() {
149	if (m_drawThread) {
150		QMetaObject::invokeMethod(m_painter, "forceDraw");
151	}
152}
153
154void DisplayGL::lockAspectRatio(bool lock) {
155	Display::lockAspectRatio(lock);
156	if (m_drawThread) {
157		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
158	}
159}
160
161void DisplayGL::lockIntegerScaling(bool lock) {
162	Display::lockIntegerScaling(lock);
163	if (m_drawThread) {
164		QMetaObject::invokeMethod(m_painter, "lockIntegerScaling", Q_ARG(bool, lock));
165	}
166}
167
168void DisplayGL::filter(bool filter) {
169	Display::filter(filter);
170	if (m_drawThread) {
171		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
172	}
173}
174
175void DisplayGL::framePosted() {
176	if (m_drawThread) {
177		m_painter->enqueue(m_context->drawContext());
178		QMetaObject::invokeMethod(m_painter, "draw");
179	}
180}
181
182void DisplayGL::setShaders(struct VDir* shaders) {
183	if (m_drawThread) {
184		QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
185	} else {
186		m_painter->setShaders(shaders);
187	}
188}
189
190void DisplayGL::clearShaders() {
191	QMetaObject::invokeMethod(m_painter, "clearShaders");
192}
193
194
195void DisplayGL::resizeContext() {
196	if (m_drawThread) {
197		m_isDrawing = false;
198		CoreController::Interrupter interrupter(m_context);
199		QMetaObject::invokeMethod(m_painter, "resizeContext", Qt::BlockingQueuedConnection);
200	}
201}
202
203void DisplayGL::resizeEvent(QResizeEvent* event) {
204	Display::resizeEvent(event);
205	resizePainter();
206}
207
208void DisplayGL::resizePainter() {
209	if (m_drawThread) {
210		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
211	}
212}
213
214VideoProxy* DisplayGL::videoProxy() {
215	if (supportsShaders()) {
216		return &m_videoProxy;
217	}
218	return nullptr;
219}
220
221int DisplayGL::framebufferHandle() {
222	return m_painter->glTex();
223}
224
225PainterGL::PainterGL(VideoProxy* proxy, QWindow* surface, QOpenGLContext* parent)
226	: m_gl(parent)
227	, m_surface(surface)
228	, m_videoProxy(proxy)
229{
230#ifdef BUILD_GL
231	mGLContext* glBackend;
232#endif
233#ifdef BUILD_GLES2
234	mGLES2Context* gl2Backend;
235#endif
236
237	m_gl->makeCurrent(m_surface);
238	m_window = new QOpenGLPaintDevice;
239#if defined(_WIN32) && defined(USE_EPOXY)
240	epoxy_handle_external_wglMakeCurrent();
241#endif
242	int majorVersion = m_gl->format().majorVersion();
243
244	QStringList extensions = QString(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))).split(' ');
245
246#ifdef BUILD_GLES2
247	if ((majorVersion == 2 && extensions.contains("GL_ARB_framebuffer_object")) || majorVersion > 2) {
248		gl2Backend = static_cast<mGLES2Context*>(malloc(sizeof(mGLES2Context)));
249		mGLES2ContextCreate(gl2Backend);
250		m_backend = &gl2Backend->d;
251		m_supportsShaders = true;
252	}
253#endif
254
255#ifdef BUILD_GL
256	 if (!m_backend) {
257		glBackend = static_cast<mGLContext*>(malloc(sizeof(mGLContext)));
258		mGLContextCreate(glBackend);
259		m_backend = &glBackend->d;
260		m_supportsShaders = false;
261	}
262#endif
263	m_backend->swap = [](VideoBackend* v) {
264		PainterGL* painter = static_cast<PainterGL*>(v->user);
265		if (!painter->m_gl->isValid()) {
266			return;
267		}
268		painter->m_gl->swapBuffers(painter->m_gl->surface());
269	};
270
271	m_backend->init(m_backend, 0);
272#ifdef BUILD_GLES2
273	if (m_supportsShaders) {
274		m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<mGLES2Context*>(m_backend)->initialShader);
275	}
276#endif
277	m_gl->doneCurrent();
278
279	m_backend->user = this;
280	m_backend->filter = false;
281	m_backend->lockAspectRatio = false;
282
283	for (int i = 0; i < 2; ++i) {
284		m_free.append(new uint32_t[1024 * 2048]);
285	}
286}
287
288PainterGL::~PainterGL() {
289	while (!m_queue.isEmpty()) {
290		delete[] m_queue.dequeue();
291	}
292	for (auto item : m_free) {
293		delete[] item;
294	}
295	m_gl->makeCurrent(m_surface);
296#if defined(_WIN32) && defined(USE_EPOXY)
297	epoxy_handle_external_wglMakeCurrent();
298#endif
299#ifdef BUILD_GLES2
300	if (m_shader.passes) {
301		mGLES2ShaderFree(&m_shader);
302	}
303#endif
304	m_backend->deinit(m_backend);
305	m_gl->doneCurrent();
306	free(m_backend);
307	m_backend = nullptr;
308	delete m_window;
309}
310
311void PainterGL::setContext(std::shared_ptr<CoreController> context) {
312	m_context = context;
313	resizeContext();
314}
315
316void PainterGL::resizeContext() {
317	if (!m_context) {
318		return;
319	}
320
321	if (!m_active) {
322		m_gl->makeCurrent(m_surface);
323#if defined(_WIN32) && defined(USE_EPOXY)
324		epoxy_handle_external_wglMakeCurrent();
325#endif
326	}
327
328	QSize size = m_context->screenDimensions();
329	m_backend->setDimensions(m_backend, size.width(), size.height());
330	if (!m_active) {
331		m_gl->doneCurrent();
332	}
333}
334
335void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
336	m_messagePainter = messagePainter;
337}
338
339void PainterGL::resize(const QSize& size) {
340	m_size = size;
341	if (m_started && !m_active) {
342		forceDraw();
343	}
344}
345
346void PainterGL::lockAspectRatio(bool lock) {
347	m_backend->lockAspectRatio = lock;
348	resize(m_size);
349}
350
351void PainterGL::lockIntegerScaling(bool lock) {
352	m_backend->lockIntegerScaling = lock;
353	resize(m_size);
354}
355
356void PainterGL::filter(bool filter) {
357	m_backend->filter = filter;
358	if (m_started && !m_active) {
359		forceDraw();
360	}
361}
362
363void PainterGL::start() {
364	m_gl->makeCurrent(m_surface);
365#if defined(_WIN32) && defined(USE_EPOXY)
366	epoxy_handle_external_wglMakeCurrent();
367#endif
368
369#ifdef BUILD_GLES2
370	if (m_supportsShaders && m_shader.passes) {
371		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
372	}
373#endif
374
375	m_active = true;
376	m_started = true;
377}
378
379void PainterGL::draw() {
380	if (m_queue.isEmpty()) {
381		return;
382	}
383
384	if (mCoreSyncWaitFrameStart(&m_context->thread()->impl->sync) || !m_queue.isEmpty()) {
385		dequeue();
386		mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
387		m_painter.begin(m_window);
388		performDraw();
389		m_painter.end();
390		m_backend->swap(m_backend);
391		if (!m_delayTimer.isValid()) {
392			m_delayTimer.start();
393		} else if (m_gl->format().swapInterval()) {
394			while (m_delayTimer.elapsed() < 15) {
395				QThread::usleep(100);
396			}
397			m_delayTimer.restart();
398		}
399	} else {
400		mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
401	}
402	if (!m_queue.isEmpty()) {
403		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
404	}
405}
406
407void PainterGL::forceDraw() {
408	m_painter.begin(m_window);
409	performDraw();
410	m_painter.end();
411	m_backend->swap(m_backend);
412}
413
414void PainterGL::stop() {
415	m_active = false;
416	m_started = false;
417	dequeueAll();
418	m_backend->clear(m_backend);
419	m_backend->swap(m_backend);
420	m_gl->doneCurrent();
421	m_gl->moveToThread(m_surface->thread());
422	m_context.reset();
423	moveToThread(m_gl->thread());
424	m_videoProxy->moveToThread(m_gl->thread());
425}
426
427void PainterGL::pause() {
428	m_active = false;
429}
430
431void PainterGL::unpause() {
432	m_active = true;
433}
434
435void PainterGL::performDraw() {
436	m_painter.beginNativePainting();
437	float r = m_surface->devicePixelRatio();
438	m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
439	m_backend->drawFrame(m_backend);
440	m_painter.endNativePainting();
441	if (m_messagePainter) {
442		m_messagePainter->paint(&m_painter);
443	}
444}
445
446void PainterGL::enqueue(const uint32_t* backing) {
447	m_mutex.lock();
448	uint32_t* buffer = nullptr;
449	if (backing) {
450		if (m_free.isEmpty()) {
451			buffer = m_queue.dequeue();
452		} else {
453			buffer = m_free.takeLast();
454		}
455		QSize size = m_context->screenDimensions();
456		memcpy(buffer, backing, size.width() * size.height() * BYTES_PER_PIXEL);
457	}
458	m_queue.enqueue(buffer);
459	m_mutex.unlock();
460}
461
462void PainterGL::dequeue() {
463	m_mutex.lock();
464	if (m_queue.isEmpty()) {
465		m_mutex.unlock();
466		return;
467	}
468	uint32_t* buffer = m_queue.dequeue();
469	if (buffer) {
470		m_backend->postFrame(m_backend, buffer);
471		m_free.append(buffer);
472	}
473	m_mutex.unlock();
474}
475
476void PainterGL::dequeueAll() {
477	uint32_t* buffer = 0;
478	m_mutex.lock();
479	while (!m_queue.isEmpty()) {
480		buffer = m_queue.dequeue();
481		if (buffer) {
482			m_free.append(buffer);
483		}
484	}
485	if (buffer) {
486		m_backend->postFrame(m_backend, buffer);
487	}
488	m_mutex.unlock();
489}
490
491void PainterGL::setShaders(struct VDir* dir) {
492	if (!supportsShaders()) {
493		return;
494	}
495#ifdef BUILD_GLES2
496	if (!m_active) {
497		m_gl->makeCurrent(m_surface);
498#if defined(_WIN32) && defined(USE_EPOXY)
499		epoxy_handle_external_wglMakeCurrent();
500#endif
501	}
502	if (m_shader.passes) {
503		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
504		mGLES2ShaderFree(&m_shader);
505	}
506	mGLES2ShaderLoad(&m_shader, dir);
507	if (m_started) {
508		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
509	}
510	if (!m_active) {
511		m_gl->doneCurrent();
512	}
513#endif
514}
515
516void PainterGL::clearShaders() {
517	if (!supportsShaders()) {
518		return;
519	}
520#ifdef BUILD_GLES2
521	if (!m_active) {
522		m_gl->makeCurrent(m_surface);
523#if defined(_WIN32) && defined(USE_EPOXY)
524		epoxy_handle_external_wglMakeCurrent();
525#endif
526	}
527	if (m_shader.passes) {
528		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
529		mGLES2ShaderFree(&m_shader);
530	}
531	if (!m_active) {
532		m_gl->doneCurrent();
533	}
534#endif
535}
536
537VideoShader* PainterGL::shaders() {
538	return &m_shader;
539}
540
541int PainterGL::glTex() {
542#ifdef BUILD_GLES2
543	if (supportsShaders()) {
544		mGLES2Context* gl2Backend = reinterpret_cast<mGLES2Context*>(m_backend);
545		return gl2Backend->tex;
546	}
547#endif
548#ifdef BUILD_GL
549	mGLContext* glBackend = reinterpret_cast<mGLContext*>(m_backend);
550	return glBackend->tex;
551#else
552	return -1;
553#endif
554}
555
556#endif