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 m_backing = backing;
143 makeCurrent();
144#ifdef COLOR_16_BIT
145#ifdef COLOR_5_6_5
146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, m_backing);
147#else
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, m_backing);
149#endif
150#else
151 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
152#endif
153 doneCurrent();
154}
155
156void Painter::resize(const QSize& size) {
157 m_size = size;
158 QWidget::resize(size);
159 if (m_drawTimer) {
160 forceDraw();
161 }
162}
163
164void Painter::lockAspectRatio(bool lock) {
165 m_lockAspectRatio = lock;
166 if (m_drawTimer) {
167 forceDraw();
168 }
169}
170
171void Painter::filter(bool filter) {
172 m_filter = filter;
173 makeCurrent();
174 if (m_filter) {
175 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
176 } else {
177 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
178 }
179 doneCurrent();
180 if (m_drawTimer) {
181 forceDraw();
182 }
183}
184
185void Painter::start() {
186 makeCurrent();
187 glEnable(GL_TEXTURE_2D);
188 glGenTextures(1, &m_tex);
189 glBindTexture(GL_TEXTURE_2D, m_tex);
190 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
191 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
192 if (m_filter) {
193 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
194 } else {
195 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
196 }
197 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
198 glEnableClientState(GL_VERTEX_ARRAY);
199 glVertexPointer(2, GL_INT, 0, _glVertices);
200 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
201 glMatrixMode(GL_PROJECTION);
202 glLoadIdentity();
203 glOrtho(0, 240, 160, 0, 0, 1);
204 glMatrixMode(GL_MODELVIEW);
205 glLoadIdentity();
206 doneCurrent();
207
208 m_drawTimer = new QTimer;
209 m_drawTimer->moveToThread(QThread::currentThread());
210 m_drawTimer->setInterval(0);
211 connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
212 m_drawTimer->start();
213}
214
215void Painter::draw() {
216 makeCurrent();
217 GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip);
218 performDraw();
219 GBASyncWaitFrameEnd(&m_context->sync);
220 swapBuffers();
221 doneCurrent();
222}
223
224void Painter::forceDraw() {
225 makeCurrent();
226 glViewport(0, 0, m_size.width() * devicePixelRatio(), m_size.height() * devicePixelRatio());
227 glClear(GL_COLOR_BUFFER_BIT);
228 performDraw();
229 swapBuffers();
230 doneCurrent();
231}
232
233void Painter::stop() {
234 m_drawTimer->stop();
235 delete m_drawTimer;
236 m_drawTimer = nullptr;
237 makeCurrent();
238 glDeleteTextures(1, &m_tex);
239 glClear(GL_COLOR_BUFFER_BIT);
240 swapBuffers();
241 doneCurrent();
242}
243
244void Painter::pause() {
245 m_drawTimer->stop();
246 // Make sure both buffers are filled
247 forceDraw();
248 forceDraw();
249}
250
251void Painter::unpause() {
252 m_drawTimer->start();
253}
254
255void Painter::initializeGL() {
256 glClearColor(0, 0, 0, 0);
257 glClear(GL_COLOR_BUFFER_BIT);
258}
259
260void Painter::performDraw() {
261 int w = m_size.width() * devicePixelRatio();
262 int h = m_size.height() * devicePixelRatio();
263#ifndef Q_OS_MAC
264 // TODO: This seems to cause framerates to drag down to 120 FPS on OS X,
265 // even if the emulator can go faster. Look into why.
266 glViewport(0, 0, m_size.width() * devicePixelRatio(), m_size.height() * devicePixelRatio());
267 glClear(GL_COLOR_BUFFER_BIT);
268#endif
269 int drawW = w;
270 int drawH = h;
271 if (m_lockAspectRatio) {
272 if (w * 2 > h * 3) {
273 drawW = h * 3 / 2;
274 } else if (w * 2 < h * 3) {
275 drawH = w * 2 / 3;
276 }
277 }
278 glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
279 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
280 if (m_context->sync.videoFrameWait) {
281 glFlush();
282 }
283}