all repos — mgba @ ca8559549c2b77b5adb958557c3a6c9d6ebc5d47

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