all repos — mgba @ 5b040e236db0dd39e2e29ad7724b9712ac84c57a

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	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_gl->isValid()) {
265			return;
266		}
267		painter->m_gl->swapBuffers(painter->m_surface);
268		painter->m_gl->doneCurrent();
269		painter->m_gl->makeCurrent(painter->m_surface);
270#if defined(_WIN32) && defined(USE_EPOXY)
271		epoxy_handle_external_wglMakeCurrent();
272#endif
273	};
274
275	m_backend->init(m_backend, 0);
276#ifdef BUILD_GLES2
277	if (m_supportsShaders) {
278		m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<mGLES2Context*>(m_backend)->initialShader);
279	}
280#endif
281	m_gl->doneCurrent();
282
283	m_backend->user = this;
284	m_backend->filter = false;
285	m_backend->lockAspectRatio = false;
286
287	for (int i = 0; i < 2; ++i) {
288		m_free.append(new uint32_t[1024 * 2048]);
289	}
290}
291
292PainterGL::~PainterGL() {
293	while (!m_queue.isEmpty()) {
294		delete[] m_queue.dequeue();
295	}
296	for (auto item : m_free) {
297		delete[] item;
298	}
299	m_gl->makeCurrent(m_surface);
300#if defined(_WIN32) && defined(USE_EPOXY)
301	epoxy_handle_external_wglMakeCurrent();
302#endif
303#ifdef BUILD_GLES2
304	if (m_shader.passes) {
305		mGLES2ShaderFree(&m_shader);
306	}
307#endif
308	m_backend->deinit(m_backend);
309	m_gl->doneCurrent();
310	free(m_backend);
311	m_backend = nullptr;
312	delete m_window;
313}
314
315void PainterGL::setContext(std::shared_ptr<CoreController> context) {
316	m_context = context;
317	resizeContext();
318}
319
320void PainterGL::resizeContext() {
321	if (!m_context) {
322		return;
323	}
324
325	QSize size = m_context->screenDimensions();
326	m_backend->setDimensions(m_backend, size.width(), size.height());
327}
328
329void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
330	m_messagePainter = messagePainter;
331}
332
333void PainterGL::resize(const QSize& size) {
334	m_size = size;
335	if (m_started && !m_active) {
336		forceDraw();
337	}
338}
339
340void PainterGL::lockAspectRatio(bool lock) {
341	m_backend->lockAspectRatio = lock;
342	resize(m_size);
343}
344
345void PainterGL::lockIntegerScaling(bool lock) {
346	m_backend->lockIntegerScaling = lock;
347	resize(m_size);
348}
349
350void PainterGL::filter(bool filter) {
351	m_backend->filter = filter;
352	if (m_started && !m_active) {
353		forceDraw();
354	}
355}
356
357void PainterGL::start() {
358	m_gl->makeCurrent(m_surface);
359#if defined(_WIN32) && defined(USE_EPOXY)
360	epoxy_handle_external_wglMakeCurrent();
361#endif
362
363#ifdef BUILD_GLES2
364	if (m_supportsShaders && m_shader.passes) {
365		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
366	}
367#endif
368
369	m_active = true;
370	m_started = true;
371}
372
373void PainterGL::draw() {
374	if (m_queue.isEmpty()) {
375		return;
376	}
377
378	if (mCoreSyncWaitFrameStart(&m_context->thread()->impl->sync) || !m_queue.isEmpty()) {
379		dequeue();
380		mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
381		m_painter.begin(m_window);
382		performDraw();
383		m_painter.end();
384		m_backend->swap(m_backend);
385		if (!m_delayTimer.isValid()) {
386			m_delayTimer.start();
387		} else if (m_gl->format().swapInterval() && m_context->videoSync()) {
388			while (m_delayTimer.elapsed() < 15) {
389				QThread::usleep(100);
390			}
391			m_delayTimer.restart();
392		}
393	} else {
394		mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
395	}
396	if (!m_queue.isEmpty()) {
397		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
398	}
399}
400
401void PainterGL::forceDraw() {
402	m_painter.begin(m_window);
403	performDraw();
404	m_painter.end();
405	m_backend->swap(m_backend);
406}
407
408void PainterGL::stop() {
409	m_active = false;
410	m_started = false;
411	dequeueAll();
412	m_backend->clear(m_backend);
413	m_backend->swap(m_backend);
414	if (m_videoProxy) {
415		m_videoProxy->reset();
416	}
417	m_gl->doneCurrent();
418	m_gl->moveToThread(m_surface->thread());
419	m_context.reset();
420	moveToThread(m_gl->thread());
421	m_videoProxy->moveToThread(m_gl->thread());
422}
423
424void PainterGL::pause() {
425	m_active = false;
426}
427
428void PainterGL::unpause() {
429	m_active = true;
430}
431
432void PainterGL::performDraw() {
433	m_painter.beginNativePainting();
434	float r = m_surface->devicePixelRatio();
435	m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
436	m_backend->drawFrame(m_backend);
437	m_painter.endNativePainting();
438	if (m_messagePainter) {
439		m_messagePainter->paint(&m_painter);
440	}
441}
442
443void PainterGL::enqueue(const uint32_t* backing) {
444	m_mutex.lock();
445	uint32_t* buffer = nullptr;
446	if (backing) {
447		if (m_free.isEmpty()) {
448			buffer = m_queue.dequeue();
449		} else {
450			buffer = m_free.takeLast();
451		}
452		QSize size = m_context->screenDimensions();
453		memcpy(buffer, backing, size.width() * size.height() * BYTES_PER_PIXEL);
454	}
455	m_queue.enqueue(buffer);
456	m_mutex.unlock();
457}
458
459void PainterGL::dequeue() {
460	m_mutex.lock();
461	if (m_queue.isEmpty()) {
462		m_mutex.unlock();
463		return;
464	}
465	uint32_t* buffer = m_queue.dequeue();
466	if (buffer) {
467		m_backend->postFrame(m_backend, buffer);
468		m_free.append(buffer);
469	}
470	m_mutex.unlock();
471}
472
473void PainterGL::dequeueAll() {
474	uint32_t* buffer = 0;
475	m_mutex.lock();
476	while (!m_queue.isEmpty()) {
477		buffer = m_queue.dequeue();
478		if (buffer) {
479			m_free.append(buffer);
480		}
481	}
482	if (buffer) {
483		m_backend->postFrame(m_backend, buffer);
484	}
485	m_mutex.unlock();
486}
487
488void PainterGL::setShaders(struct VDir* dir) {
489	if (!supportsShaders()) {
490		return;
491	}
492#ifdef BUILD_GLES2
493	if (m_shader.passes) {
494		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
495		mGLES2ShaderFree(&m_shader);
496	}
497	mGLES2ShaderLoad(&m_shader, dir);
498	if (m_started) {
499		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
500	}
501#endif
502}
503
504void PainterGL::clearShaders() {
505	if (!supportsShaders()) {
506		return;
507	}
508#ifdef BUILD_GLES2
509	if (m_shader.passes) {
510		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
511		mGLES2ShaderFree(&m_shader);
512	}
513#endif
514}
515
516VideoShader* PainterGL::shaders() {
517	return &m_shader;
518}
519
520int PainterGL::glTex() {
521#ifdef BUILD_GLES2
522	if (supportsShaders()) {
523		mGLES2Context* gl2Backend = reinterpret_cast<mGLES2Context*>(m_backend);
524		return gl2Backend->tex;
525	}
526#endif
527#ifdef BUILD_GL
528	mGLContext* glBackend = reinterpret_cast<mGLContext*>(m_backend);
529	return glBackend->tex;
530#else
531	return -1;
532#endif
533}
534
535#endif