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