all repos — mgba @ 70bc5a0bdbbe7a6faab36609210e6e005f0426dc

mGBA Game Boy Advance Emulator

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

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