all repos — mgba @ 35b5626053dea8a4c53c63b6da23831fea6bb649

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	if (!m_delayTimer.isValid()) {
322		m_delayTimer.start();
323	} else if (m_delayTimer.elapsed() < 16) {
324		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
325		QThread::usleep(500);
326		return;
327	} else {
328		m_delayTimer.restart();
329	}
330
331	if (mCoreSyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
332		dequeue();
333		mCoreSyncWaitFrameEnd(&m_context->sync);
334		m_painter.begin(m_gl->context()->device());
335		performDraw();
336		m_painter.end();
337		m_backend->swap(m_backend);
338	} else {
339		mCoreSyncWaitFrameEnd(&m_context->sync);
340	}
341	if (!m_queue.isEmpty()) {
342		QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
343	}
344}
345
346void PainterGL::forceDraw() {
347	m_painter.begin(m_gl->context()->device());
348	performDraw();
349	m_painter.end();
350	m_backend->swap(m_backend);
351}
352
353void PainterGL::stop() {
354	m_active = false;
355	m_started = false;
356	m_gl->makeCurrent();
357#if defined(_WIN32) && defined(USE_EPOXY)
358	epoxy_handle_external_wglMakeCurrent();
359#endif
360	dequeueAll();
361	m_backend->clear(m_backend);
362	m_backend->swap(m_backend);
363	m_gl->doneCurrent();
364	m_gl->context()->moveToThread(m_gl->thread());
365	moveToThread(m_gl->thread());
366}
367
368void PainterGL::pause() {
369	m_active = false;
370}
371
372void PainterGL::unpause() {
373	m_active = true;
374}
375
376void PainterGL::performDraw() {
377	m_painter.beginNativePainting();
378#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
379	float r = m_gl->devicePixelRatioF();
380#else
381	float r = m_gl->devicePixelRatio();
382#endif
383	m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
384	m_backend->drawFrame(m_backend);
385	m_painter.endNativePainting();
386	if (m_messagePainter) {
387		m_messagePainter->paint(&m_painter);
388	}
389}
390
391void PainterGL::enqueue(const uint32_t* backing) {
392	m_mutex.lock();
393	uint32_t* buffer;
394	if (m_free.isEmpty()) {
395		buffer = m_queue.dequeue();
396	} else {
397		buffer = m_free.takeLast();
398	}
399	unsigned width, height;
400	m_context->core->desiredVideoDimensions(m_context->core, &width, &height);
401	memcpy(buffer, backing, width * height * BYTES_PER_PIXEL);
402	m_queue.enqueue(buffer);
403	m_mutex.unlock();
404}
405
406void PainterGL::dequeue() {
407	m_mutex.lock();
408	if (m_queue.isEmpty()) {
409		m_mutex.unlock();
410		return;
411	}
412	uint32_t* buffer = m_queue.dequeue();
413	m_backend->postFrame(m_backend, buffer);
414	m_free.append(buffer);
415	m_mutex.unlock();
416}
417
418void PainterGL::dequeueAll() {
419	uint32_t* buffer = 0;
420	m_mutex.lock();
421	while (!m_queue.isEmpty()) {
422		buffer = m_queue.dequeue();
423		m_free.append(buffer);
424	}
425	if (buffer) {
426		m_backend->postFrame(m_backend, buffer);
427	}
428	m_mutex.unlock();
429}
430
431void PainterGL::setShaders(struct VDir* dir) {
432	if (!supportsShaders()) {
433		return;
434	}
435#if !defined(_WIN32) || defined(USE_EPOXY)
436	m_gl->makeCurrent();
437#if defined(_WIN32) && defined(USE_EPOXY)
438	epoxy_handle_external_wglMakeCurrent();
439#endif
440	if (m_shader.passes) {
441		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
442		mGLES2ShaderFree(&m_shader);
443	}
444	mGLES2ShaderLoad(&m_shader, dir);
445	if (m_started) {
446		mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
447	}
448	m_gl->doneCurrent();
449#endif
450}
451
452void PainterGL::clearShaders() {
453	if (!supportsShaders()) {
454		return;
455	}
456#if !defined(_WIN32) || defined(USE_EPOXY)
457	m_gl->makeCurrent();
458#if defined(_WIN32) && defined(USE_EPOXY)
459	epoxy_handle_external_wglMakeCurrent();
460#endif
461	if (m_shader.passes) {
462		mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
463		mGLES2ShaderFree(&m_shader);
464	}
465	m_gl->doneCurrent();
466#endif
467}
468
469VideoShader* PainterGL::shaders() {
470	return &m_shader;
471}