all repos — mgba @ aacab52a840f8c086a8fb22160c52f25fbe49032

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