all repos — mgba @ 7395d47a5a17fd8d82eeeb4031d4b92d0b679282

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 "gba/supervisor/thread.h"
 13}
 14
 15using namespace QGBA;
 16
 17DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
 18	: Display(parent)
 19	, m_gl(new EmptyGLWidget(format, this))
 20	, m_painter(new PainterGL(m_gl))
 21	, m_drawThread(nullptr)
 22	, m_context(nullptr)
 23{
 24}
 25
 26DisplayGL::~DisplayGL() {
 27	delete m_painter;
 28}
 29
 30void DisplayGL::startDrawing(GBAThread* thread) {
 31	if (m_drawThread) {
 32		return;
 33	}
 34	m_painter->setContext(thread);
 35	m_painter->setMessagePainter(messagePainter());
 36	m_context = thread;
 37	m_painter->resize(size());
 38	m_gl->move(0, 0);
 39	m_drawThread = new QThread(this);
 40	m_drawThread->setObjectName("Painter Thread");
 41	m_gl->context()->doneCurrent();
 42	m_gl->context()->moveToThread(m_drawThread);
 43	m_painter->moveToThread(m_drawThread);
 44	connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
 45	m_drawThread->start();
 46	GBASyncSetVideoSync(&m_context->sync, false);
 47
 48	lockAspectRatio(isAspectRatioLocked());
 49	filter(isFiltered());
 50	resizePainter();
 51}
 52
 53void DisplayGL::stopDrawing() {
 54	if (m_drawThread) {
 55		if (GBAThreadIsActive(m_context)) {
 56			GBAThreadInterrupt(m_context);
 57		}
 58		QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
 59		m_drawThread->exit();
 60		m_drawThread = nullptr;
 61		if (GBAThreadIsActive(m_context)) {
 62			GBAThreadContinue(m_context);
 63		}
 64	}
 65}
 66
 67void DisplayGL::pauseDrawing() {
 68	if (m_drawThread) {
 69		if (GBAThreadIsActive(m_context)) {
 70			GBAThreadInterrupt(m_context);
 71		}
 72		QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
 73		if (GBAThreadIsActive(m_context)) {
 74			GBAThreadContinue(m_context);
 75		}
 76	}
 77}
 78
 79void DisplayGL::unpauseDrawing() {
 80	if (m_drawThread) {
 81		if (GBAThreadIsActive(m_context)) {
 82			GBAThreadInterrupt(m_context);
 83		}
 84		QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
 85		if (GBAThreadIsActive(m_context)) {
 86			GBAThreadContinue(m_context);
 87		}
 88	}
 89}
 90
 91void DisplayGL::forceDraw() {
 92	if (m_drawThread) {
 93		QMetaObject::invokeMethod(m_painter, "forceDraw");
 94	}
 95}
 96
 97void DisplayGL::lockAspectRatio(bool lock) {
 98	Display::lockAspectRatio(lock);
 99	if (m_drawThread) {
100		QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
101	}
102}
103
104void DisplayGL::filter(bool filter) {
105	Display::filter(filter);
106	if (m_drawThread) {
107		QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
108	}
109}
110
111void DisplayGL::framePosted(const uint32_t* buffer) {
112	if (m_drawThread && buffer) {
113		QMetaObject::invokeMethod(m_painter, "setBacking", Q_ARG(const uint32_t*, buffer));
114	}
115}
116
117void DisplayGL::resizeEvent(QResizeEvent* event) {
118	Display::resizeEvent(event);
119	resizePainter();
120}
121
122void DisplayGL::resizePainter() {
123	m_gl->resize(size());
124	if (m_drawThread) {
125		QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
126	}
127}
128
129PainterGL::PainterGL(QGLWidget* parent)
130	: m_gl(parent)
131	, m_active(false)
132	, m_context(nullptr)
133	, m_messagePainter(nullptr)
134{
135	GBAGLContextCreate(&m_backend);
136	m_backend.d.swap = [](VideoBackend* v) {
137		PainterGL* painter = static_cast<PainterGL*>(v->user);
138		painter->m_gl->swapBuffers();
139	};
140	m_backend.d.user = this;
141	m_backend.d.filter = false;
142	m_backend.d.lockAspectRatio = false;
143}
144
145void PainterGL::setContext(GBAThread* context) {
146	m_context = context;
147}
148
149void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
150	m_messagePainter = messagePainter;
151}
152
153void PainterGL::setBacking(const uint32_t* backing) {
154	m_gl->makeCurrent();
155	m_backend.d.postFrame(&m_backend.d, backing);
156	if (m_active) {
157		draw();
158	}
159	m_gl->doneCurrent();
160}
161
162void PainterGL::resize(const QSize& size) {
163	m_size = size;
164	if (m_active) {
165		forceDraw();
166	}
167}
168
169void PainterGL::lockAspectRatio(bool lock) {
170	m_backend.d.lockAspectRatio = lock;
171	if (m_active) {
172		forceDraw();
173	}
174}
175
176void PainterGL::filter(bool filter) {
177	m_backend.d.filter = filter;
178	if (m_active) {
179		forceDraw();
180	}
181}
182
183void PainterGL::start() {
184	m_gl->makeCurrent();
185	m_backend.d.init(&m_backend.d, reinterpret_cast<WHandle>(m_gl->winId()));
186	m_gl->doneCurrent();
187	m_active = true;
188}
189
190void PainterGL::draw() {
191	if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
192		m_painter.begin(m_gl->context()->device());
193		performDraw();
194		m_painter.end();
195		GBASyncWaitFrameEnd(&m_context->sync);
196		m_backend.d.swap(&m_backend.d);
197	} else {
198		GBASyncWaitFrameEnd(&m_context->sync);
199	}
200}
201
202void PainterGL::forceDraw() {
203	m_painter.begin(m_gl->context()->device());
204	performDraw();
205	m_painter.end();
206	m_backend.d.swap(&m_backend.d);
207}
208
209void PainterGL::stop() {
210	m_active = false;
211	m_gl->makeCurrent();
212	m_backend.d.clear(&m_backend.d);
213	m_backend.d.swap(&m_backend.d);
214	m_backend.d.deinit(&m_backend.d);
215	m_gl->doneCurrent();
216	m_gl->context()->moveToThread(m_gl->thread());
217	moveToThread(m_gl->thread());
218}
219
220void PainterGL::pause() {
221	m_active = false;
222	// Make sure both buffers are filled
223	forceDraw();
224	forceDraw();
225}
226
227void PainterGL::unpause() {
228	m_active = true;
229}
230
231void PainterGL::performDraw() {
232	m_painter.beginNativePainting();
233	float r = m_gl->devicePixelRatio();
234	m_backend.d.resized(&m_backend.d, m_size.width() * r, m_size.height() * r);
235	m_backend.d.drawFrame(&m_backend.d);
236	m_painter.endNativePainting();
237	if (m_messagePainter) {
238		m_messagePainter->paint(&m_painter);
239	}
240}