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