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