all repos — mgba @ 9dc49df0bca23925cfcc8193a1770463fd9916cf

mGBA Game Boy Advance Emulator

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