all repos — mgba @ 4a38f9b97998fb977f04d2b980830d3b697a6bd4

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#include <QApplication>
  9#include <QResizeEvent>
 10#include <QTimer>
 11
 12#include <mgba/core/core.h>
 13#include <mgba/core/thread.h>
 14#ifdef BUILD_GL
 15#include "platform/opengl/gl.h"
 16#endif
 17#if !defined(_WIN32) || defined(USE_EPOXY)
 18#include "platform/opengl/gles2.h"
 19#ifdef _WIN32
 20#include <epoxy/wgl.h>
 21#endif
 22#endif
 23
 24using namespace QGBA;
 25
 26DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
 27	: Display(parent)
 28	, m_isDrawing(false)
 29	, m_gl(new EmptyGLWidget(format, this))
 30	, m_drawThread(nullptr)
 31	, m_context(nullptr)
 32{
 33	m_painter = new PainterGL(format.majorVersion() < 2 ? 1 : m_gl->format().majorVersion(), m_gl);
 34	m_gl->setMouseTracking(true);
 35	m_gl->setAttribute(Qt::WA_TransparentForMouseEvents); // This doesn't seem to work?
 36}
 37
 38DisplayGL::~DisplayGL() {
 39	delete m_painter;
 40}
 41
 42bool DisplayGL::supportsShaders() const {
 43	return m_painter->supportsShaders();
 44}
 45
 46VideoShader* DisplayGL::shaders() {
 47	VideoShader* shaders = nullptr;
 48	if (m_drawThread) {
 49		QMetaObject::invokeMethod(m_painter, "shaders", Qt::BlockingQueuedConnection, Q_RETURN_ARG(VideoShader*, shaders));
 50	} else {
 51		shaders = m_painter->shaders();
 52	}
 53	return shaders;
 54}
 55
 56void DisplayGL::startDrawing(mCoreThread* thread) {
 57	if (m_drawThread) {
 58		return;
 59	}
 60	m_isDrawing = true;
 61	m_painter->setContext(thread);
 62	m_painter->setMessagePainter(messagePainter());
 63	m_context = thread;
 64	m_painter->resize(size());
 65	m_gl->move(0, 0);
 66	m_drawThread = new QThread(this);
 67	m_drawThread->setObjectName("Painter Thread");
 68	m_gl->context()->doneCurrent();
 69	m_gl->context()->moveToThread(m_drawThread);
 70	m_painter->moveToThread(m_drawThread);
 71	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 72	m_drawThread->start();
 73	mCoreSyncSetVideoSync(&m_context->sync, false);
 74
 75	lockAspectRatio(isAspectRatioLocked());
 76	filter(isFiltered());
 77#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
 78	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF());
 79#else
 80	messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
 81#endif
 82	resizePainter();
 83}
 84
 85void DisplayGL::stopDrawing() {
 86	if (m_drawThread) {
 87		m_isDrawing = false;
 88		if (mCoreThreadIsActive(m_context)) {
 89			mCoreThreadInterrupt(m_context);
 90		}
 91		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 92		m_drawThread->exit();
 93		m_drawThread = nullptr;
 94		if (mCoreThreadIsActive(m_context)) {
 95			mCoreThreadContinue(m_context);
 96		}
 97	}
 98}
 99
100void DisplayGL::pauseDrawing() {
101	if (m_drawThread) {
102		m_isDrawing = false;
103		if (mCoreThreadIsActive(m_context)) {
104			mCoreThreadInterrupt(m_context);
105		}
106		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
107		if (mCoreThreadIsActive(m_context)) {
108			mCoreThreadContinue(m_context);
109		}
110	}
111}
112
113void DisplayGL::unpauseDrawing() {
114	if (m_drawThread) {
115		m_isDrawing = true;
116		if (mCoreThreadIsActive(m_context)) {
117			mCoreThreadInterrupt(m_context);
118		}
119		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
120		if (mCoreThreadIsActive(m_context)) {
121			mCoreThreadContinue(m_context);
122		}
123	}
124}
125
126void DisplayGL::forceDraw() {
127	if (m_drawThread) {
128		QMetaObject::invokeMethod(m_painter, "forceDraw");
129	}
130}
131
132void DisplayGL::lockAspectRatio(bool lock) {
133	Display::lockAspectRatio(lock);
134	if (m_drawThread) {
135		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
136	}
137}
138
139void DisplayGL::filter(bool filter) {
140	Display::filter(filter);
141	if (m_drawThread) {
142		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
143	}
144}
145
146void DisplayGL::framePosted(const uint32_t* buffer) {
147	if (m_drawThread && buffer) {
148		m_painter->enqueue(buffer);
149		QMetaObject::invokeMethod(m_painter, "draw");
150	}
151}
152
153void DisplayGL::setShaders(struct VDir* shaders) {
154	if (m_drawThread) {
155		QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
156	} else {
157		m_painter->setShaders(shaders);
158	}
159}
160
161void DisplayGL::clearShaders() {
162	QMetaObject::invokeMethod(m_painter, "clearShaders");
163}
164
165void DisplayGL::resizeEvent(QResizeEvent* event) {
166	Display::resizeEvent(event);
167	resizePainter();
168}
169
170void DisplayGL::resizePainter() {
171	m_gl->resize(size());
172	if (m_drawThread) {
173		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
174	}
175}
176
177PainterGL::PainterGL(int majorVersion, QGLWidget* parent)
178	: m_gl(parent)
179	, m_active(false)
180	, m_started(false)
181	, m_context(nullptr)
182	, m_shader{}
183	, m_backend(nullptr)
184	, m_messagePainter(nullptr)
185{
186#ifdef BUILD_GL
187	mGLContext* glBackend;
188#endif
189#if !defined(_WIN32) || defined(USE_EPOXY)
190	mGLES2Context* gl2Backend;
191#endif
192
193#if !defined(_WIN32) || defined(USE_EPOXY)
194	if (majorVersion >= 2) {
195		gl2Backend = new mGLES2Context;
196		mGLES2ContextCreate(gl2Backend);
197		m_backend = &gl2Backend->d;
198		m_supportsShaders = true;
199	}
200#endif
201
202#ifdef BUILD_GL
203	 if (!m_backend) {
204		glBackend = new mGLContext;
205		mGLContextCreate(glBackend);
206		m_backend = &glBackend->d;
207		m_supportsShaders = false;
208	}
209#endif
210	m_backend->swap = [](VideoBackend* v) {
211		PainterGL* painter = static_cast<PainterGL*>(v->user);
212		painter->m_gl->swapBuffers();
213	};
214
215	m_gl->makeCurrent();
216#if defined(_WIN32) && defined(USE_EPOXY)
217	epoxy_handle_external_wglMakeCurrent();
218#endif
219	m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
220#if !defined(_WIN32) || defined(USE_EPOXY)
221	if (m_supportsShaders) {
222		m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<mGLES2Context*>(m_backend)->initialShader);
223	}
224#endif
225	m_gl->doneCurrent();
226
227	m_backend->user = this;
228	m_backend->filter = false;
229	m_backend->lockAspectRatio = false;
230
231	for (int i = 0; i < 2; ++i) {
232		m_free.append(new uint32_t[256 * 256]);
233	}
234}
235
236PainterGL::~PainterGL() {
237	while (!m_queue.isEmpty()) {
238		delete[] m_queue.dequeue();
239	}
240	for (auto item : m_free) {
241		delete[] item;
242	}
243	m_gl->makeCurrent();
244#if defined(_WIN32) && defined(USE_EPOXY)
245	epoxy_handle_external_wglMakeCurrent();
246#endif
247#if !defined(_WIN32) || defined(USE_EPOXY)
248	if (m_shader.passes) {
249		mGLES2ShaderFree(&m_shader);
250	}
251#endif
252	m_backend->deinit(m_backend);
253	m_gl->doneCurrent();
254	delete m_backend;
255	m_backend = nullptr;
256}
257
258void PainterGL::setContext(mCoreThread* context) {
259	m_context = context;
260
261	if (!context) {
262		return;
263	}
264
265	m_gl->makeCurrent();
266#if defined(_WIN32) && defined(USE_EPOXY)
267	epoxy_handle_external_wglMakeCurrent();
268#endif
269	unsigned width, height;
270	context->core->desiredVideoDimensions(context->core, &width, &height);
271	m_backend->setDimensions(m_backend, width, height);
272	m_gl->doneCurrent();
273}
274
275void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
276	m_messagePainter = messagePainter;
277}
278
279void PainterGL::resize(const QSize& size) {
280	m_size = size;
281	if (m_started && !m_active) {
282		forceDraw();
283	}
284}
285
286void PainterGL::lockAspectRatio(bool lock) {
287	m_backend->lockAspectRatio = lock;
288	if (m_started && !m_active) {
289		forceDraw();
290	}
291}
292
293void PainterGL::filter(bool filter) {
294	m_backend->filter = filter;
295	if (m_started && !m_active) {
296		forceDraw();
297	}
298}
299
300void PainterGL::start() {
301	m_gl->makeCurrent();
302#if defined(_WIN32) && defined(USE_EPOXY)
303	epoxy_handle_external_wglMakeCurrent();
304#endif
305
306#if !defined(_WIN32) || defined(USE_EPOXY)
307	if (m_supportsShaders && m_shader.passes) {
308		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
309	}
310#endif
311
312	m_gl->doneCurrent();
313	m_active = true;
314	m_started = true;
315}
316
317void PainterGL::draw() {
318	if (m_queue.isEmpty() || !mCoreThreadIsActive(m_context)) {
319		return;
320	}
321
322	if (mCoreSyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
323		dequeue();
324		mCoreSyncWaitFrameEnd(&m_context->sync);
325		m_painter.begin(m_gl->context()->device());
326		performDraw();
327		m_painter.end();
328		m_backend->swap(m_backend);
329		if (!m_delayTimer.isValid()) {
330			m_delayTimer.start();
331		} else {
332			while (m_delayTimer.elapsed() < 15) {
333				QThread::usleep(100);
334			}
335			m_delayTimer.restart();
336		}
337	} else {
338		mCoreSyncWaitFrameEnd(&m_context->sync);
339	}
340	if (!m_queue.isEmpty()) {
341		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
342	}
343}
344
345void PainterGL::forceDraw() {
346	m_painter.begin(m_gl->context()->device());
347	performDraw();
348	m_painter.end();
349	m_backend->swap(m_backend);
350}
351
352void PainterGL::stop() {
353	m_active = false;
354	m_started = false;
355	m_gl->makeCurrent();
356#if defined(_WIN32) && defined(USE_EPOXY)
357	epoxy_handle_external_wglMakeCurrent();
358#endif
359	dequeueAll();
360	m_backend->clear(m_backend);
361	m_backend->swap(m_backend);
362	m_gl->doneCurrent();
363	m_gl->context()->moveToThread(m_gl->thread());
364	moveToThread(m_gl->thread());
365}
366
367void PainterGL::pause() {
368	m_active = false;
369}
370
371void PainterGL::unpause() {
372	m_active = true;
373}
374
375void PainterGL::performDraw() {
376	m_painter.beginNativePainting();
377#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
378	float r = m_gl->devicePixelRatioF();
379#else
380	float r = m_gl->devicePixelRatio();
381#endif
382	m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
383	m_backend->drawFrame(m_backend);
384	m_painter.endNativePainting();
385	if (m_messagePainter) {
386		m_messagePainter->paint(&m_painter);
387	}
388}
389
390void PainterGL::enqueue(const uint32_t* backing) {
391	m_mutex.lock();
392	uint32_t* buffer;
393	if (m_free.isEmpty()) {
394		buffer = m_queue.dequeue();
395	} else {
396		buffer = m_free.takeLast();
397	}
398	unsigned width, height;
399	m_context->core->desiredVideoDimensions(m_context->core, &width, &height);
400	memcpy(buffer, backing, width * height * BYTES_PER_PIXEL);
401	m_queue.enqueue(buffer);
402	m_mutex.unlock();
403}
404
405void PainterGL::dequeue() {
406	m_mutex.lock();
407	if (m_queue.isEmpty()) {
408		m_mutex.unlock();
409		return;
410	}
411	uint32_t* buffer = m_queue.dequeue();
412	m_backend->postFrame(m_backend, buffer);
413	m_free.append(buffer);
414	m_mutex.unlock();
415}
416
417void PainterGL::dequeueAll() {
418	uint32_t* buffer = 0;
419	m_mutex.lock();
420	while (!m_queue.isEmpty()) {
421		buffer = m_queue.dequeue();
422		m_free.append(buffer);
423	}
424	if (buffer) {
425		m_backend->postFrame(m_backend, buffer);
426	}
427	m_mutex.unlock();
428}
429
430void PainterGL::setShaders(struct VDir* dir) {
431	if (!supportsShaders()) {
432		return;
433	}
434#if !defined(_WIN32) || defined(USE_EPOXY)
435	m_gl->makeCurrent();
436#if defined(_WIN32) && defined(USE_EPOXY)
437	epoxy_handle_external_wglMakeCurrent();
438#endif
439	if (m_shader.passes) {
440		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
441		mGLES2ShaderFree(&m_shader);
442	}
443	mGLES2ShaderLoad(&m_shader, dir);
444	if (m_started) {
445		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
446	}
447	m_gl->doneCurrent();
448#endif
449}
450
451void PainterGL::clearShaders() {
452	if (!supportsShaders()) {
453		return;
454	}
455#if !defined(_WIN32) || defined(USE_EPOXY)
456	m_gl->makeCurrent();
457#if defined(_WIN32) && defined(USE_EPOXY)
458	epoxy_handle_external_wglMakeCurrent();
459#endif
460	if (m_shader.passes) {
461		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
462		mGLES2ShaderFree(&m_shader);
463	}
464	m_gl->doneCurrent();
465#endif
466}
467
468VideoShader* PainterGL::shaders() {
469	return &m_shader;
470}