src/platform/qt/Display.cpp (view raw)
1/* Copyright (c) 2013-2014 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-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_drawThread(nullptr)
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_drawThread) {
44 return;
45 }
46 m_drawThread = new QThread(this);
47 m_painter = new Painter(this);
48 m_painter->setContext(thread);
49 m_painter->setBacking(buffer);
50 m_painter->moveToThread(m_drawThread);
51 m_context = thread;
52 doneCurrent();
53 context()->moveToThread(m_drawThread);
54 connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
55 m_drawThread->start(QThread::TimeCriticalPriority);
56
57 lockAspectRatio(m_lockAspectRatio);
58 filter(m_filter);
59}
60
61void Display::stopDrawing() {
62 if (m_drawThread) {
63 if (GBAThreadIsActive(m_context)) {
64 GBAThreadInterrupt(m_context);
65 GBASyncSuspendDrawing(&m_context->sync);
66 }
67 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
68 m_drawThread->exit();
69 m_drawThread = nullptr;
70 if (GBAThreadIsActive(m_context)) {
71 GBASyncResumeDrawing(&m_context->sync);
72 GBAThreadContinue(m_context);
73 }
74 }
75}
76
77void Display::pauseDrawing() {
78 if (m_drawThread) {
79 if (GBAThreadIsActive(m_context)) {
80 GBAThreadInterrupt(m_context);
81 GBASyncSuspendDrawing(&m_context->sync);
82 }
83 QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
84 if (GBAThreadIsActive(m_context)) {
85 GBASyncResumeDrawing(&m_context->sync);
86 GBAThreadContinue(m_context);
87 }
88 }
89}
90
91void Display::unpauseDrawing() {
92 if (m_drawThread) {
93 if (GBAThreadIsActive(m_context)) {
94 GBAThreadInterrupt(m_context);
95 GBASyncSuspendDrawing(&m_context->sync);
96 }
97 QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
98 if (GBAThreadIsActive(m_context)) {
99 GBASyncResumeDrawing(&m_context->sync);
100 GBAThreadContinue(m_context);
101 }
102 }
103}
104
105void Display::forceDraw() {
106 if (m_drawThread) {
107 QMetaObject::invokeMethod(m_painter, "forceDraw", Qt::QueuedConnection);
108 }
109}
110
111void Display::lockAspectRatio(bool lock) {
112 m_lockAspectRatio = lock;
113 if (m_drawThread) {
114 QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Qt::QueuedConnection, Q_ARG(bool, lock));
115 }
116}
117
118void Display::filter(bool filter) {
119 m_filter = filter;
120 if (m_drawThread) {
121 QMetaObject::invokeMethod(m_painter, "filter", Qt::QueuedConnection, Q_ARG(bool, filter));
122 }
123}
124
125#ifdef USE_PNG
126void Display::screenshot() {
127 GBAThreadInterrupt(m_context);
128 GBAThreadTakeScreenshot(m_context);
129 GBAThreadContinue(m_context);
130}
131#endif
132
133void Display::initializeGL() {
134 glClearColor(0, 0, 0, 0);
135 glClear(GL_COLOR_BUFFER_BIT);
136 swapBuffers();
137}
138
139void Display::resizeEvent(QResizeEvent* event) {
140 if (m_drawThread) {
141 GBAThreadInterrupt(m_context);
142 GBASyncSuspendDrawing(&m_context->sync);
143 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
144 GBASyncResumeDrawing(&m_context->sync);
145 GBAThreadContinue(m_context);
146 }
147}
148
149Painter::Painter(Display* parent)
150 : m_gl(parent)
151 , m_lockAspectRatio(false)
152{
153 m_size = parent->size();
154}
155
156void Painter::setContext(GBAThread* context) {
157 m_context = context;
158}
159
160void Painter::setBacking(const uint32_t* backing) {
161 m_backing = backing;
162}
163
164void Painter::resize(const QSize& size) {
165 m_size = size;
166 forceDraw();
167 forceDraw();
168}
169
170void Painter::lockAspectRatio(bool lock) {
171 m_lockAspectRatio = lock;
172 forceDraw();
173 forceDraw();
174}
175
176void Painter::filter(bool filter) {
177 m_filter = filter;
178 m_gl->makeCurrent();
179 if (m_filter) {
180 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
181 } else {
182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
183 }
184 m_gl->doneCurrent();
185 forceDraw();
186}
187
188void Painter::start() {
189 m_gl->makeCurrent();
190 glEnable(GL_TEXTURE_2D);
191 glGenTextures(1, &m_tex);
192 glBindTexture(GL_TEXTURE_2D, m_tex);
193 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
194 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
195 if (m_filter) {
196 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
197 } else {
198 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
199 }
200 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
201 glEnableClientState(GL_VERTEX_ARRAY);
202 glVertexPointer(2, GL_INT, 0, _glVertices);
203 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
204 glMatrixMode(GL_PROJECTION);
205 glLoadIdentity();
206 glOrtho(0, 240, 160, 0, 0, 1);
207 glMatrixMode(GL_MODELVIEW);
208 glLoadIdentity();
209 m_gl->doneCurrent();
210
211 m_drawTimer = new QTimer;
212 m_drawTimer->moveToThread(QThread::currentThread());
213 m_drawTimer->setInterval(0);
214 connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
215 m_drawTimer->start();
216}
217
218void Painter::draw() {
219 m_gl->makeCurrent();
220 if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
221 performDraw();
222 }
223 GBASyncWaitFrameEnd(&m_context->sync);
224 m_gl->swapBuffers();
225 m_gl->doneCurrent();
226}
227
228void Painter::forceDraw() {
229 m_gl->makeCurrent();
230 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
231 glClear(GL_COLOR_BUFFER_BIT);
232 performDraw();
233 m_gl->swapBuffers();
234 m_gl->doneCurrent();
235}
236
237void Painter::stop() {
238 m_drawTimer->stop();
239 delete m_drawTimer;
240 m_gl->makeCurrent();
241 glDeleteTextures(1, &m_tex);
242 glClear(GL_COLOR_BUFFER_BIT);
243 m_gl->swapBuffers();
244 m_gl->doneCurrent();
245 m_gl->context()->moveToThread(QApplication::instance()->thread());
246}
247
248void Painter::pause() {
249 m_drawTimer->stop();
250 // Make sure both buffers are filled
251 forceDraw();
252 forceDraw();
253}
254
255void Painter::unpause() {
256 m_drawTimer->start();
257}
258
259void Painter::performDraw() {
260 int w = m_size.width() * m_gl->devicePixelRatio();
261 int h = m_size.height() * m_gl->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() * m_gl->devicePixelRatio(), m_size.height() * m_gl->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 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
279 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
280 if (m_context->sync.videoFrameWait) {
281 glFlush();
282 }
283}