all repos — mgba @ daaa2fa5236df5e8976e7e0ba7ac39e0d9233422

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