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}
133
134void Display::resizeEvent(QResizeEvent* event) {
135 if (m_started) {
136 GBAThreadInterrupt(m_context);
137 GBASyncSuspendDrawing(&m_context->sync);
138 m_painter->resize(event->size());
139 GBASyncResumeDrawing(&m_context->sync);
140 GBAThreadContinue(m_context);
141 }
142}
143
144Painter::Painter(Display* parent)
145 : m_gl(parent)
146 , m_lockAspectRatio(false)
147 , m_filter(false)
148{
149 m_size = parent->size();
150}
151
152void Painter::setContext(GBAThread* context) {
153 m_context = context;
154}
155
156void Painter::setBacking(const uint32_t* backing) {
157 m_backing = backing;
158}
159
160void Painter::resize(const QSize& size) {
161 m_size = size;
162 forceDraw();
163 forceDraw();
164}
165
166void Painter::lockAspectRatio(bool lock) {
167 m_lockAspectRatio = lock;
168 forceDraw();
169 forceDraw();
170}
171
172void Painter::filter(bool filter) {
173 m_filter = filter;
174 m_gl->makeCurrent();
175 if (m_filter) {
176 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177 } else {
178 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
179 }
180 m_gl->doneCurrent();
181 forceDraw();
182}
183
184void Painter::start() {
185 m_gl->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 m_gl->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 m_gl->makeCurrent();
216 GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip);
217 performDraw();
218 GBASyncWaitFrameEnd(&m_context->sync);
219 m_gl->swapBuffers();
220 m_gl->doneCurrent();
221}
222
223void Painter::forceDraw() {
224 m_gl->makeCurrent();
225 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
226 glClear(GL_COLOR_BUFFER_BIT);
227 performDraw();
228 m_gl->swapBuffers();
229 m_gl->doneCurrent();
230}
231
232void Painter::stop() {
233 m_drawTimer->stop();
234 delete m_drawTimer;
235 m_gl->makeCurrent();
236 glDeleteTextures(1, &m_tex);
237 glClear(GL_COLOR_BUFFER_BIT);
238 m_gl->swapBuffers();
239 m_gl->doneCurrent();
240 m_gl->context()->moveToThread(QApplication::instance()->thread());
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::performDraw() {
255 int w = m_size.width() * m_gl->devicePixelRatio();
256 int h = m_size.height() * m_gl->devicePixelRatio();
257#ifndef Q_OS_MAC
258 // TODO: This seems to cause framerates to drag down to 120 FPS on OS X,
259 // even if the emulator can go faster. Look into why.
260 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
261 glClear(GL_COLOR_BUFFER_BIT);
262#endif
263 int drawW = w;
264 int drawH = h;
265 if (m_lockAspectRatio) {
266 if (w * 2 > h * 3) {
267 drawW = h * 3 / 2;
268 } else if (w * 2 < h * 3) {
269 drawH = w * 2 / 3;
270 }
271 }
272 glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
273#ifdef COLOR_16_BIT
274#ifdef COLOR_5_6_5
275 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, m_backing);
276#else
277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, m_backing);
278#endif
279#else
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
281#endif
282 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
283 if (m_context->sync.videoFrameWait) {
284 glFlush();
285 }
286}