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