all repos — mgba @ dc60c28bf10aee056e61e5111cee4a857d2cc33b

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