all repos — mgba @ eeee6fe44eb11e77774c7bb3f08c2016682d33cd

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
 64DisplayGL::~DisplayGL() {
 65	stopDrawing();
 66	delete m_painter;
 67	delete m_gl;
 68}
 69
 70bool DisplayGL::supportsShaders() const {
 71	return m_painter->supportsShaders();
 72}
 73
 74VideoShader* DisplayGL::shaders() {
 75	VideoShader* shaders = nullptr;
 76	if (m_drawThread) {
 77		QMetaObject::invokeMethod(m_painter, "shaders", Qt::BlockingQueuedConnection, Q_RETURN_ARG(VideoShader*, shaders));
 78	} else {
 79		shaders = m_painter->shaders();
 80	}
 81	return shaders;
 82}
 83
 84void DisplayGL::startDrawing(std::shared_ptr<CoreController> controller) {
 85	if (m_drawThread) {
 86		return;
 87	}
 88	m_isDrawing = true;
 89	m_painter->setContext(controller);
 90	m_painter->setMessagePainter(messagePainter());
 91	m_context = controller;
 92	m_painter->resize(size());
 93	m_drawThread = new QThread(this);
 94	m_drawThread->setObjectName("Painter Thread");
 95	m_gl->doneCurrent();
 96	m_gl->moveToThread(m_drawThread);
 97	m_painter->moveToThread(m_drawThread);
 98	m_videoProxy.moveToThread(m_drawThread);
 99	connect(m_drawThread, &QThread::started, m_painter, &PainterGL::start);
100	m_drawThread->start();
101
102	lockAspectRatio(isAspectRatioLocked());
103	lockIntegerScaling(isIntegerScalingLocked());
104	filter(isFiltered());
105#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
106	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF());
107#else
108	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
109#endif
110	resizePainter();
111}
112
113void DisplayGL::stopDrawing() {
114	if (m_drawThread) {
115		m_isDrawing = false;
116		CoreController::Interrupter interrupter(m_context);
117		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
118		m_drawThread->exit();
119		m_drawThread = nullptr;
120
121		m_gl->makeCurrent(windowHandle());
122#if defined(_WIN32) && defined(USE_EPOXY)
123		epoxy_handle_external_wglMakeCurrent();
124#endif
125	}
126	m_context.reset();
127}
128
129void DisplayGL::pauseDrawing() {
130	if (m_drawThread) {
131		m_isDrawing = false;
132		CoreController::Interrupter interrupter(m_context);
133		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
134	}
135}
136
137void DisplayGL::unpauseDrawing() {
138	if (m_drawThread) {
139		m_isDrawing = true;
140		CoreController::Interrupter interrupter(m_context);
141		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
142	}
143}
144
145void DisplayGL::forceDraw() {
146	if (m_drawThread) {
147		QMetaObject::invokeMethod(m_painter, "forceDraw");
148	}
149}
150
151void DisplayGL::lockAspectRatio(bool lock) {
152	Display::lockAspectRatio(lock);
153	if (m_drawThread) {
154		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
155	}
156}
157
158void DisplayGL::lockIntegerScaling(bool lock) {
159	Display::lockIntegerScaling(lock);
160	if (m_drawThread) {
161		QMetaObject::invokeMethod(m_painter, "lockIntegerScaling", Q_ARG(bool, lock));
162	}
163}
164
165void DisplayGL::filter(bool filter) {
166	Display::filter(filter);
167	if (m_drawThread) {
168		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
169	}
170}
171
172void DisplayGL::framePosted() {
173	if (m_drawThread) {
174		m_painter->enqueue(m_context->drawContext());
175		QMetaObject::invokeMethod(m_painter, "draw");
176	}
177}
178
179void DisplayGL::setShaders(struct VDir* shaders) {
180	if (m_drawThread) {
181		QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
182	} else {
183		m_painter->setShaders(shaders);
184	}
185}
186
187void DisplayGL::clearShaders() {
188	QMetaObject::invokeMethod(m_painter, "clearShaders");
189}
190
191
192void DisplayGL::resizeContext() {
193	if (m_drawThread) {
194		m_isDrawing = false;
195		CoreController::Interrupter interrupter(m_context);
196		QMetaObject::invokeMethod(m_painter, "resizeContext", Qt::BlockingQueuedConnection);
197	}
198}
199
200void DisplayGL::resizeEvent(QResizeEvent* event) {
201	Display::resizeEvent(event);
202	resizePainter();
203}
204
205void DisplayGL::resizePainter() {
206	if (m_drawThread) {
207		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
208	}
209}
210
211VideoProxy* DisplayGL::videoProxy() {
212	if (supportsShaders()) {
213		return &m_videoProxy;
214	}
215	return nullptr;
216}
217
218int DisplayGL::framebufferHandle() {
219	return m_painter->glTex();
220}
221
222PainterGL::PainterGL(VideoProxy* proxy, QWindow* surface, QOpenGLContext* parent)
223	: m_gl(parent)
224	, m_surface(surface)
225	, m_videoProxy(proxy)
226{
227#ifdef BUILD_GL
228	mGLContext* glBackend;
229#endif
230#ifdef BUILD_GLES2
231	mGLES2Context* gl2Backend;
232#endif
233
234	m_gl->makeCurrent(m_surface);
235	m_window = new QOpenGLPaintDevice;
236#if defined(_WIN32) && defined(USE_EPOXY)
237	epoxy_handle_external_wglMakeCurrent();
238#endif
239	int majorVersion = m_gl->format().majorVersion();
240
241	QStringList extensions = QString(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))).split(' ');
242
243#ifdef BUILD_GLES2
244	if ((majorVersion == 2 && extensions.contains("GL_ARB_framebuffer_object")) || majorVersion > 2) {
245		gl2Backend = static_cast<mGLES2Context*>(malloc(sizeof(mGLES2Context)));
246		mGLES2ContextCreate(gl2Backend);
247		m_backend = &gl2Backend->d;
248		m_supportsShaders = true;
249	}
250#endif
251
252#ifdef BUILD_GL
253	 if (!m_backend) {
254		glBackend = static_cast<mGLContext*>(malloc(sizeof(mGLContext)));
255		mGLContextCreate(glBackend);
256		m_backend = &glBackend->d;
257		m_supportsShaders = false;
258	}
259#endif
260	m_backend->swap = [](VideoBackend* v) {
261		PainterGL* painter = static_cast<PainterGL*>(v->user);
262		if (!painter->m_gl->isValid()) {
263			return;
264		}
265		painter->m_gl->swapBuffers(painter->m_gl->surface());
266	};
267
268	m_backend->init(m_backend, 0);
269#ifdef BUILD_GLES2
270	if (m_supportsShaders) {
271		m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<mGLES2Context*>(m_backend)->initialShader);
272	}
273#endif
274	m_gl->doneCurrent();
275
276	m_backend->user = this;
277	m_backend->filter = false;
278	m_backend->lockAspectRatio = false;
279
280	for (int i = 0; i < 2; ++i) {
281		m_free.append(new uint32_t[1024 * 2048]);
282	}
283}
284
285PainterGL::~PainterGL() {
286	while (!m_queue.isEmpty()) {
287		delete[] m_queue.dequeue();
288	}
289	for (auto item : m_free) {
290		delete[] item;
291	}
292	m_gl->makeCurrent(m_surface);
293#if defined(_WIN32) && defined(USE_EPOXY)
294	epoxy_handle_external_wglMakeCurrent();
295#endif
296#ifdef BUILD_GLES2
297	if (m_shader.passes) {
298		mGLES2ShaderFree(&m_shader);
299	}
300#endif
301	m_backend->deinit(m_backend);
302	m_gl->doneCurrent();
303	free(m_backend);
304	m_backend = nullptr;
305	delete m_window;
306}
307
308void PainterGL::setContext(std::shared_ptr<CoreController> context) {
309	m_context = context;
310	resizeContext();
311}
312
313void PainterGL::resizeContext() {
314	if (!m_context) {
315		return;
316	}
317
318	if (!m_active) {
319		m_gl->makeCurrent(m_surface);
320#if defined(_WIN32) && defined(USE_EPOXY)
321		epoxy_handle_external_wglMakeCurrent();
322#endif
323	}
324
325	QSize size = m_context->screenDimensions();
326	m_backend->setDimensions(m_backend, size.width(), size.height());
327	if (!m_active) {
328		m_gl->doneCurrent();
329	}
330}
331
332void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
333	m_messagePainter = messagePainter;
334}
335
336void PainterGL::resize(const QSize& size) {
337	m_size = size;
338	if (m_started && !m_active) {
339		forceDraw();
340	}
341}
342
343void PainterGL::lockAspectRatio(bool lock) {
344	m_backend->lockAspectRatio = lock;
345	resize(m_size);
346}
347
348void PainterGL::lockIntegerScaling(bool lock) {
349	m_backend->lockIntegerScaling = lock;
350	resize(m_size);
351}
352
353void PainterGL::filter(bool filter) {
354	m_backend->filter = filter;
355	if (m_started && !m_active) {
356		forceDraw();
357	}
358}
359
360void PainterGL::start() {
361	m_gl->makeCurrent(m_surface);
362#if defined(_WIN32) && defined(USE_EPOXY)
363	epoxy_handle_external_wglMakeCurrent();
364#endif
365
366#ifdef BUILD_GLES2
367	if (m_supportsShaders && m_shader.passes) {
368		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
369	}
370#endif
371
372	m_active = true;
373	m_started = true;
374}
375
376void PainterGL::draw() {
377	if (m_queue.isEmpty()) {
378		return;
379	}
380
381	if (mCoreSyncWaitFrameStart(&m_context->thread()->impl->sync) || !m_queue.isEmpty()) {
382		dequeue();
383		mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
384		m_painter.begin(m_window);
385		performDraw();
386		m_painter.end();
387		m_backend->swap(m_backend);
388		if (!m_delayTimer.isValid()) {
389			m_delayTimer.start();
390		} else if (m_gl->format().swapInterval()) {
391			while (m_delayTimer.elapsed() < 15) {
392				QThread::usleep(100);
393			}
394			m_delayTimer.restart();
395		}
396	} else {
397		mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
398	}
399	if (!m_queue.isEmpty()) {
400		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
401	}
402}
403
404void PainterGL::forceDraw() {
405	m_painter.begin(m_window);
406	performDraw();
407	m_painter.end();
408	m_backend->swap(m_backend);
409}
410
411void PainterGL::stop() {
412	m_active = false;
413	m_started = false;
414	dequeueAll();
415	m_backend->clear(m_backend);
416	m_backend->swap(m_backend);
417	if (m_videoProxy) {
418		m_videoProxy->reset();
419	}
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