all repos — mgba @ 190ace3003c48eb0baf298027feba8991348f5da

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
 17static const GLint _glVertices[] = {
 18	0, 0,
 19	256, 0,
 20	256, 256,
 21	0, 256
 22};
 23
 24static const GLint _glTexCoords[] = {
 25	0, 0,
 26	1, 0,
 27	1, 1,
 28	0, 1
 29};
 30
 31DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
 32	: Display(parent)
 33	, m_painter(new Painter(format, this))
 34	, m_started(false)
 35{
 36}
 37
 38void DisplayGL::startDrawing(const uint32_t* buffer, GBAThread* thread) {
 39	if (m_started) {
 40		return;
 41	}
 42	m_painter->setContext(thread);
 43	m_painter->setBacking(buffer);
 44	m_context = thread;
 45	m_painter->start();
 46	m_painter->resize(size());
 47	m_painter->move(0, 0);
 48	m_started = true;
 49
 50	lockAspectRatio(m_lockAspectRatio);
 51	filter(m_filter);
 52}
 53
 54void DisplayGL::stopDrawing() {
 55	if (m_started) {
 56		if (GBAThreadIsActive(m_context)) {
 57			GBAThreadInterrupt(m_context);
 58			GBASyncSuspendDrawing(&m_context->sync);
 59		}
 60		m_painter->stop();
 61		m_started = false;
 62		if (GBAThreadIsActive(m_context)) {
 63			GBASyncResumeDrawing(&m_context->sync);
 64			GBAThreadContinue(m_context);
 65		}
 66	}
 67}
 68
 69void DisplayGL::pauseDrawing() {
 70	if (m_started) {
 71		if (GBAThreadIsActive(m_context)) {
 72			GBAThreadInterrupt(m_context);
 73			GBASyncSuspendDrawing(&m_context->sync);
 74		}
 75		m_painter->pause();
 76		if (GBAThreadIsActive(m_context)) {
 77			GBASyncResumeDrawing(&m_context->sync);
 78			GBAThreadContinue(m_context);
 79		}
 80	}
 81}
 82
 83void DisplayGL::unpauseDrawing() {
 84	if (m_started) {
 85		if (GBAThreadIsActive(m_context)) {
 86			GBAThreadInterrupt(m_context);
 87			GBASyncSuspendDrawing(&m_context->sync);
 88		}
 89		m_painter->unpause();
 90		if (GBAThreadIsActive(m_context)) {
 91			GBASyncResumeDrawing(&m_context->sync);
 92			GBAThreadContinue(m_context);
 93		}
 94	}
 95}
 96
 97void DisplayGL::forceDraw() {
 98	if (m_started) {
 99		m_painter->forceDraw();
100	}
101}
102
103void DisplayGL::lockAspectRatio(bool lock) {
104	m_lockAspectRatio = lock;
105	if (m_started) {
106		m_painter->lockAspectRatio(lock);
107	}
108}
109
110void DisplayGL::filter(bool filter) {
111	m_filter = filter;
112	if (m_started) {
113		m_painter->filter(filter);
114	}
115}
116
117void DisplayGL::resizeEvent(QResizeEvent* event) {
118	m_painter->resize(event->size());
119}
120
121Painter::Painter(const QGLFormat& format, QWidget* parent)
122	: QGLWidget(format, parent)
123	, m_drawTimer(nullptr)
124	, m_lockAspectRatio(false)
125	, m_filter(false)
126{
127	setMinimumSize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
128	m_size = parent->size();
129	setAutoBufferSwap(false);
130}
131
132void Painter::setContext(GBAThread* context) {
133	m_context = context;
134}
135
136void Painter::setBacking(const uint32_t* backing) {
137	m_backing = backing;
138}
139
140void Painter::resize(const QSize& size) {
141	m_size = size;
142	QWidget::resize(size);
143	if (m_drawTimer) {
144		forceDraw();
145	}
146}
147
148void Painter::lockAspectRatio(bool lock) {
149	m_lockAspectRatio = lock;
150	if (m_drawTimer) {
151		forceDraw();
152	}
153}
154
155void Painter::filter(bool filter) {
156	m_filter = filter;
157	makeCurrent();
158	if (m_filter) {
159		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
160	} else {
161		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
162	}
163	doneCurrent();
164	if (m_drawTimer) {
165		forceDraw();
166	}
167}
168
169void Painter::start() {
170	makeCurrent();
171	glEnable(GL_TEXTURE_2D);
172	glGenTextures(1, &m_tex);
173	glBindTexture(GL_TEXTURE_2D, m_tex);
174	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
175	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176	if (m_filter) {
177		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
178	} else {
179		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
180	}
181	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
182	glEnableClientState(GL_VERTEX_ARRAY);
183	glVertexPointer(2, GL_INT, 0, _glVertices);
184	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
185	glMatrixMode(GL_PROJECTION);
186	glLoadIdentity();
187	glOrtho(0, 240, 160, 0, 0, 1);
188	glMatrixMode(GL_MODELVIEW);
189	glLoadIdentity();
190	doneCurrent();
191
192	m_drawTimer = new QTimer;
193	m_drawTimer->moveToThread(QThread::currentThread());
194	m_drawTimer->setInterval(0);
195	connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
196	m_drawTimer->start();
197}
198
199void Painter::draw() {
200	makeCurrent();
201	GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip);
202	performDraw();
203	GBASyncWaitFrameEnd(&m_context->sync);
204	swapBuffers();
205	doneCurrent();
206}
207
208void Painter::forceDraw() {
209	makeCurrent();
210	glViewport(0, 0, m_size.width() * devicePixelRatio(), m_size.height() * devicePixelRatio());
211	glClear(GL_COLOR_BUFFER_BIT);
212	performDraw();
213	swapBuffers();
214	doneCurrent();
215}
216
217void Painter::stop() {
218	m_drawTimer->stop();
219	delete m_drawTimer;
220	m_drawTimer = nullptr;
221	makeCurrent();
222	glDeleteTextures(1, &m_tex);
223	glClear(GL_COLOR_BUFFER_BIT);
224	swapBuffers();
225	doneCurrent();
226}
227
228void Painter::pause() {
229	m_drawTimer->stop();
230	// Make sure both buffers are filled
231	forceDraw();
232	forceDraw();
233}
234
235void Painter::unpause() {
236	m_drawTimer->start();
237}
238
239void Painter::initializeGL() {
240	glClearColor(0, 0, 0, 0);
241	glClear(GL_COLOR_BUFFER_BIT);
242	swapBuffers();
243}
244
245void Painter::performDraw() {
246	int w = m_size.width() * devicePixelRatio();
247	int h = m_size.height() * devicePixelRatio();
248#ifndef Q_OS_MAC
249	// TODO: This seems to cause framerates to drag down to 120 FPS on OS X,
250	// even if the emulator can go faster. Look into why.
251	glViewport(0, 0, m_size.width() * devicePixelRatio(), m_size.height() * devicePixelRatio());
252	glClear(GL_COLOR_BUFFER_BIT);
253#endif
254	int drawW = w;
255	int drawH = h;
256	if (m_lockAspectRatio) {
257		if (w * 2 > h * 3) {
258			drawW = h * 3 / 2;
259		} else if (w * 2 < h * 3) {
260			drawH = w * 2 / 3;
261		}
262	}
263	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
264#ifdef COLOR_16_BIT
265#ifdef COLOR_5_6_5
266	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, m_backing);
267#else
268	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, m_backing);
269#endif
270#else
271	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
272#endif
273	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
274	if (m_context->sync.videoFrameWait) {
275		glFlush();
276	}
277}