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#ifdef BUILD_GL
15#include "platform/opengl/gl.h"
16#endif
17#if !defined(_WIN32)
18#include "platform/opengl/gles2.h"
19#endif
20}
21
22using namespace QGBA;
23
24DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
25 : Display(parent)
26 , m_isDrawing(false)
27 , m_gl(new EmptyGLWidget(format, this))
28 , m_drawThread(nullptr)
29 , m_context(nullptr)
30{
31 QGLFormat::OpenGLVersionFlag glVersion = QGLFormat::OpenGL_Version_1_4;
32 QGLFormat::OpenGLVersionFlags supported = QGLFormat::openGLVersionFlags();
33 if (supported & QGLFormat::OpenGL_Version_2_1) {
34 glVersion = QGLFormat::OpenGL_Version_2_1;
35 }
36 m_painter = new PainterGL(m_gl, glVersion);
37 m_gl->setMouseTracking(true);
38 m_gl->setAttribute(Qt::WA_TransparentForMouseEvents); // This doesn't seem to work?
39}
40
41DisplayGL::~DisplayGL() {
42 delete m_painter;
43}
44
45void DisplayGL::startDrawing(GBAThread* thread) {
46 if (m_drawThread) {
47 return;
48 }
49 m_isDrawing = true;
50 m_painter->setContext(thread);
51 m_painter->setMessagePainter(messagePainter());
52 m_context = thread;
53 m_painter->resize(size());
54 m_gl->move(0, 0);
55 m_drawThread = new QThread(this);
56 m_drawThread->setObjectName("Painter Thread");
57 m_gl->context()->doneCurrent();
58 m_gl->context()->moveToThread(m_drawThread);
59 m_painter->moveToThread(m_drawThread);
60 connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
61 m_drawThread->start();
62 GBASyncSetVideoSync(&m_context->sync, false);
63
64 lockAspectRatio(isAspectRatioLocked());
65 filter(isFiltered());
66 messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
67 resizePainter();
68}
69
70void DisplayGL::stopDrawing() {
71 if (m_drawThread) {
72 m_isDrawing = false;
73 if (GBAThreadIsActive(m_context)) {
74 GBAThreadInterrupt(m_context);
75 }
76 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
77 m_drawThread->exit();
78 m_drawThread = nullptr;
79 if (GBAThreadIsActive(m_context)) {
80 GBAThreadContinue(m_context);
81 }
82 }
83}
84
85void DisplayGL::pauseDrawing() {
86 if (m_drawThread) {
87 m_isDrawing = false;
88 if (GBAThreadIsActive(m_context)) {
89 GBAThreadInterrupt(m_context);
90 }
91 QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
92 if (GBAThreadIsActive(m_context)) {
93 GBAThreadContinue(m_context);
94 }
95 }
96}
97
98void DisplayGL::unpauseDrawing() {
99 if (m_drawThread) {
100 m_isDrawing = true;
101 if (GBAThreadIsActive(m_context)) {
102 GBAThreadInterrupt(m_context);
103 }
104 QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
105 if (GBAThreadIsActive(m_context)) {
106 GBAThreadContinue(m_context);
107 }
108 }
109}
110
111void DisplayGL::forceDraw() {
112 if (m_drawThread) {
113 QMetaObject::invokeMethod(m_painter, "forceDraw");
114 }
115}
116
117void DisplayGL::lockAspectRatio(bool lock) {
118 Display::lockAspectRatio(lock);
119 if (m_drawThread) {
120 QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
121 }
122}
123
124void DisplayGL::filter(bool filter) {
125 Display::filter(filter);
126 if (m_drawThread) {
127 QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
128 }
129}
130
131void DisplayGL::framePosted(const uint32_t* buffer) {
132 if (m_drawThread && buffer) {
133 m_painter->enqueue(buffer);
134 QMetaObject::invokeMethod(m_painter, "draw");
135 }
136}
137
138void DisplayGL::resizeEvent(QResizeEvent* event) {
139 Display::resizeEvent(event);
140 resizePainter();
141}
142
143void DisplayGL::resizePainter() {
144 m_gl->resize(size());
145 if (m_drawThread) {
146 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
147 }
148}
149
150PainterGL::PainterGL(QGLWidget* parent, QGLFormat::OpenGLVersionFlag glVersion)
151 : m_gl(parent)
152 , m_active(false)
153 , m_context(nullptr)
154 , m_messagePainter(nullptr)
155{
156#ifdef BUILD_GL
157 GBAGLContext* glBackend;
158#endif
159#if !defined(_WIN32)
160 GBAGLES2Context* gl2Backend;
161#endif
162
163 switch (glVersion) {
164 default:
165#if !defined(_WIN32)
166 gl2Backend = new GBAGLES2Context;
167 GBAGLES2ContextCreate(gl2Backend);
168 m_backend = &gl2Backend->d;
169 break;
170#endif
171 case QGLFormat::OpenGL_Version_1_1:
172 case QGLFormat::OpenGL_Version_1_2:
173 case QGLFormat::OpenGL_Version_1_3:
174 case QGLFormat::OpenGL_Version_1_4:
175 case QGLFormat::OpenGL_Version_1_5:
176 glBackend = new GBAGLContext;
177 GBAGLContextCreate(glBackend);
178 m_backend = &glBackend->d;
179 break;
180 }
181 m_backend->swap = [](VideoBackend* v) {
182 PainterGL* painter = static_cast<PainterGL*>(v->user);
183 painter->m_gl->swapBuffers();
184 };
185 m_backend->user = this;
186 m_backend->filter = false;
187 m_backend->lockAspectRatio = false;
188
189 for (int i = 0; i < 2; ++i) {
190 m_free.append(new uint32_t[256 * 256]);
191 }
192}
193
194PainterGL::~PainterGL() {
195 while (!m_queue.isEmpty()) {
196 delete[] m_queue.dequeue();
197 }
198 for (auto item : m_free) {
199 delete[] item;
200 }
201 delete m_backend;
202}
203
204void PainterGL::setContext(GBAThread* context) {
205 m_context = context;
206}
207
208void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
209 m_messagePainter = messagePainter;
210}
211
212void PainterGL::resize(const QSize& size) {
213 m_size = size;
214 if (m_active) {
215 forceDraw();
216 }
217}
218
219void PainterGL::lockAspectRatio(bool lock) {
220 m_backend->lockAspectRatio = lock;
221 if (m_active) {
222 forceDraw();
223 }
224}
225
226void PainterGL::filter(bool filter) {
227 m_backend->filter = filter;
228 if (m_active) {
229 forceDraw();
230 }
231}
232
233void PainterGL::start() {
234 m_gl->makeCurrent();
235 m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
236 m_gl->doneCurrent();
237 m_active = true;
238}
239
240void PainterGL::draw() {
241 if (m_queue.isEmpty() || !GBAThreadIsActive(m_context)) {
242 return;
243 }
244 if (GBASyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
245 dequeue();
246 m_painter.begin(m_gl->context()->device());
247 performDraw();
248 m_painter.end();
249 GBASyncWaitFrameEnd(&m_context->sync);
250 m_backend->swap(m_backend);
251 } else {
252 GBASyncWaitFrameEnd(&m_context->sync);
253 }
254 if (!m_queue.isEmpty()) {
255 QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
256 }
257}
258
259void PainterGL::forceDraw() {
260 m_painter.begin(m_gl->context()->device());
261 performDraw();
262 m_painter.end();
263 m_backend->swap(m_backend);
264}
265
266void PainterGL::stop() {
267 m_active = false;
268 m_gl->makeCurrent();
269 dequeueAll();
270 m_backend->clear(m_backend);
271 m_backend->swap(m_backend);
272 m_backend->deinit(m_backend);
273 m_gl->doneCurrent();
274 m_gl->context()->moveToThread(m_gl->thread());
275 moveToThread(m_gl->thread());
276}
277
278void PainterGL::pause() {
279 m_active = false;
280}
281
282void PainterGL::unpause() {
283 m_active = true;
284}
285
286void PainterGL::performDraw() {
287 m_painter.beginNativePainting();
288 float r = m_gl->devicePixelRatio();
289 m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
290 m_backend->drawFrame(m_backend);
291 m_painter.endNativePainting();
292 if (m_messagePainter) {
293 m_messagePainter->paint(&m_painter);
294 }
295}
296
297void PainterGL::enqueue(const uint32_t* backing) {
298 m_mutex.lock();
299 uint32_t* buffer;
300 if (m_free.isEmpty()) {
301 buffer = m_queue.dequeue();
302 } else {
303 buffer = m_free.takeLast();
304 }
305 memcpy(buffer, backing, 256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
306 m_queue.enqueue(buffer);
307 m_mutex.unlock();
308}
309
310void PainterGL::dequeue() {
311 m_mutex.lock();
312 if (m_queue.isEmpty()) {
313 m_mutex.unlock();
314 return;
315 }
316 uint32_t* buffer = m_queue.dequeue();
317 m_backend->postFrame(m_backend, buffer);
318 m_free.append(buffer);
319 m_mutex.unlock();
320}
321
322void PainterGL::dequeueAll() {
323 uint32_t* buffer = 0;
324 m_mutex.lock();
325 while (!m_queue.isEmpty()) {
326 buffer = m_queue.dequeue();
327 m_free.append(buffer);
328 }
329 if (buffer) {
330 m_backend->postFrame(m_backend, buffer);
331 }
332 m_mutex.unlock();
333}