all repos — mgba @ 8a66ee0d5673ff5f701fd3b3bf73876da2aeefcd

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