all repos — mgba @ 2781a2a9f951b39ff89e47d0d13106918e1316c3

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