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#if defined(BUILD_GL) || defined(BUILD_GLES2)
9
10#include "CoreController.h"
11
12#include <QApplication>
13#include <QResizeEvent>
14#include <QTimer>
15
16#include <mgba/core/core.h>
17#include <mgba-util/math.h>
18#ifdef BUILD_GL
19#include "platform/opengl/gl.h"
20#endif
21#if !defined(_WIN32) || defined(USE_EPOXY)
22#include "platform/opengl/gles2.h"
23#ifdef _WIN32
24#include <epoxy/wgl.h>
25#endif
26#endif
27
28using namespace QGBA;
29
30DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
31 : Display(parent)
32 , m_gl(nullptr)
33{
34 // This can spontaneously re-enter into this->resizeEvent before creation is done, so we
35 // need to make sure it's initialized to nullptr before we assign the new object to it
36 m_gl = new EmptyGLWidget(format, this);
37 m_painter = new PainterGL(format.majorVersion() < 2 ? 1 : m_gl->format().majorVersion(), m_gl);
38 m_gl->setMouseTracking(true);
39 m_gl->setAttribute(Qt::WA_TransparentForMouseEvents); // This doesn't seem to work?
40 setUpdatesEnabled(false); // Prevent paint events, which can cause race conditions
41}
42
43DisplayGL::~DisplayGL() {
44 stopDrawing();
45 delete m_painter;
46}
47
48bool DisplayGL::supportsShaders() const {
49 return m_painter->supportsShaders();
50}
51
52VideoShader* DisplayGL::shaders() {
53 VideoShader* shaders = nullptr;
54 if (m_drawThread) {
55 QMetaObject::invokeMethod(m_painter, "shaders", Qt::BlockingQueuedConnection, Q_RETURN_ARG(VideoShader*, shaders));
56 } else {
57 shaders = m_painter->shaders();
58 }
59 return shaders;
60}
61
62void DisplayGL::startDrawing(std::shared_ptr<CoreController> controller) {
63 if (m_drawThread) {
64 return;
65 }
66 m_isDrawing = true;
67 m_painter->setContext(controller);
68 m_painter->setMessagePainter(messagePainter());
69 m_context = controller;
70 m_painter->resize(size());
71 m_gl->move(0, 0);
72 m_drawThread = new QThread(this);
73 m_drawThread->setObjectName("Painter Thread");
74 m_gl->context()->doneCurrent();
75 m_gl->context()->moveToThread(m_drawThread);
76 m_painter->moveToThread(m_drawThread);
77 connect(m_drawThread, &QThread::started, m_painter, &PainterGL::start);
78 m_drawThread->start();
79
80 lockAspectRatio(isAspectRatioLocked());
81 lockIntegerScaling(isIntegerScalingLocked());
82 filter(isFiltered());
83#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
84 messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF());
85#else
86 messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
87#endif
88 resizePainter();
89}
90
91void DisplayGL::stopDrawing() {
92 if (m_drawThread) {
93 m_isDrawing = false;
94 CoreController::Interrupter interrupter(m_context);
95 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
96 m_drawThread->exit();
97 m_drawThread = nullptr;
98 }
99 m_context.reset();
100}
101
102void DisplayGL::pauseDrawing() {
103 if (m_drawThread) {
104 m_isDrawing = false;
105 CoreController::Interrupter interrupter(m_context);
106 QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
107 }
108}
109
110void DisplayGL::unpauseDrawing() {
111 if (m_drawThread) {
112 m_isDrawing = true;
113 CoreController::Interrupter interrupter(m_context);
114 QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
115 }
116}
117
118void DisplayGL::forceDraw() {
119 if (m_drawThread) {
120 QMetaObject::invokeMethod(m_painter, "forceDraw");
121 }
122}
123
124void DisplayGL::lockAspectRatio(bool lock) {
125 Display::lockAspectRatio(lock);
126 if (m_drawThread) {
127 QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
128 }
129}
130
131void DisplayGL::lockIntegerScaling(bool lock) {
132 Display::lockIntegerScaling(lock);
133 if (m_drawThread) {
134 QMetaObject::invokeMethod(m_painter, "lockIntegerScaling", Q_ARG(bool, lock));
135 }
136}
137
138void DisplayGL::filter(bool filter) {
139 Display::filter(filter);
140 if (m_drawThread) {
141 QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
142 }
143}
144
145void DisplayGL::framePosted() {
146 if (m_drawThread) {
147 m_painter->enqueue(m_context->drawContext());
148 QMetaObject::invokeMethod(m_painter, "draw");
149 }
150}
151
152void DisplayGL::setShaders(struct VDir* shaders) {
153 if (m_drawThread) {
154 QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
155 } else {
156 m_painter->setShaders(shaders);
157 }
158}
159
160void DisplayGL::clearShaders() {
161 QMetaObject::invokeMethod(m_painter, "clearShaders");
162}
163
164
165void DisplayGL::resizeContext() {
166 if (m_drawThread) {
167 m_isDrawing = false;
168 CoreController::Interrupter interrupter(m_context);
169 QMetaObject::invokeMethod(m_painter, "resizeContext", Qt::BlockingQueuedConnection);
170 }
171}
172
173void DisplayGL::resizeEvent(QResizeEvent* event) {
174 Display::resizeEvent(event);
175 resizePainter();
176}
177
178void DisplayGL::resizePainter() {
179 if (m_gl) {
180 m_gl->resize(size());
181 }
182 if (m_drawThread) {
183 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
184 }
185}
186
187PainterGL::PainterGL(int majorVersion, QGLWidget* parent)
188 : m_gl(parent)
189{
190#ifdef BUILD_GL
191 mGLContext* glBackend;
192#endif
193#if !defined(_WIN32) || defined(USE_EPOXY)
194 mGLES2Context* gl2Backend;
195#endif
196
197 m_gl->makeCurrent();
198#if defined(_WIN32) && defined(USE_EPOXY)
199 epoxy_handle_external_wglMakeCurrent();
200#endif
201
202 QStringList extensions = QString(reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))).split(' ');
203
204#if !defined(_WIN32) || defined(USE_EPOXY)
205 if (extensions.contains("GL_ARB_framebuffer_object") && majorVersion >= 2) {
206 gl2Backend = static_cast<mGLES2Context*>(malloc(sizeof(mGLES2Context)));
207 mGLES2ContextCreate(gl2Backend);
208 m_backend = &gl2Backend->d;
209 m_supportsShaders = true;
210 }
211#endif
212
213#ifdef BUILD_GL
214 if (!m_backend) {
215 glBackend = static_cast<mGLContext*>(malloc(sizeof(mGLContext)));
216 mGLContextCreate(glBackend);
217 m_backend = &glBackend->d;
218 m_supportsShaders = false;
219 }
220#endif
221 m_backend->swap = [](VideoBackend* v) {
222 PainterGL* painter = static_cast<PainterGL*>(v->user);
223 if (!painter->m_gl->isVisible()) {
224 return;
225 }
226 painter->m_gl->swapBuffers();
227 };
228
229 m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
230#if !defined(_WIN32) || defined(USE_EPOXY)
231 if (m_supportsShaders) {
232 m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<mGLES2Context*>(m_backend)->initialShader);
233 }
234#endif
235 m_gl->doneCurrent();
236
237 m_backend->user = this;
238 m_backend->filter = false;
239 m_backend->lockAspectRatio = false;
240
241 for (int i = 0; i < 2; ++i) {
242 m_free.append(new uint32_t[256 * 256]);
243 }
244}
245
246PainterGL::~PainterGL() {
247 while (!m_queue.isEmpty()) {
248 delete[] m_queue.dequeue();
249 }
250 for (auto item : m_free) {
251 delete[] item;
252 }
253 m_gl->makeCurrent();
254#if defined(_WIN32) && defined(USE_EPOXY)
255 epoxy_handle_external_wglMakeCurrent();
256#endif
257#if !defined(_WIN32) || defined(USE_EPOXY)
258 if (m_shader.passes) {
259 mGLES2ShaderFree(&m_shader);
260 }
261#endif
262 m_backend->deinit(m_backend);
263 m_gl->doneCurrent();
264 free(m_backend);
265 m_backend = nullptr;
266}
267
268void PainterGL::setContext(std::shared_ptr<CoreController> context) {
269 m_context = context;
270 resizeContext();
271}
272
273void PainterGL::resizeContext() {
274 if (!m_context) {
275 return;
276 }
277
278 m_gl->makeCurrent();
279#if defined(_WIN32) && defined(USE_EPOXY)
280 epoxy_handle_external_wglMakeCurrent();
281#endif
282 QSize size = m_context->screenDimensions();
283 m_backend->setDimensions(m_backend, size.width(), size.height());
284 m_gl->doneCurrent();
285}
286
287void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
288 m_messagePainter = messagePainter;
289}
290
291void PainterGL::resize(const QSize& size) {
292 m_size = size;
293 if (m_started && !m_active) {
294 forceDraw();
295 }
296}
297
298void PainterGL::lockAspectRatio(bool lock) {
299 m_backend->lockAspectRatio = lock;
300 if (m_started && !m_active) {
301 forceDraw();
302 }
303}
304
305void PainterGL::lockIntegerScaling(bool lock) {
306 m_backend->lockIntegerScaling = lock;
307 if (m_started && !m_active) {
308 forceDraw();
309 }
310}
311
312void PainterGL::filter(bool filter) {
313 m_backend->filter = filter;
314 if (m_started && !m_active) {
315 forceDraw();
316 }
317}
318
319void PainterGL::start() {
320 m_gl->makeCurrent();
321#if defined(_WIN32) && defined(USE_EPOXY)
322 epoxy_handle_external_wglMakeCurrent();
323#endif
324
325#if !defined(_WIN32) || defined(USE_EPOXY)
326 if (m_supportsShaders && m_shader.passes) {
327 mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
328 }
329#endif
330
331 m_gl->doneCurrent();
332 m_active = true;
333 m_started = true;
334}
335
336void PainterGL::draw() {
337 if (m_queue.isEmpty()) {
338 return;
339 }
340
341 if (mCoreSyncWaitFrameStart(&m_context->thread()->impl->sync) || !m_queue.isEmpty()) {
342 dequeue();
343 mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
344 m_painter.begin(m_gl->context()->device());
345 performDraw();
346 m_painter.end();
347 m_backend->swap(m_backend);
348 if (!m_delayTimer.isValid()) {
349 m_delayTimer.start();
350 } else {
351 while (m_delayTimer.elapsed() < 15) {
352 QThread::usleep(100);
353 }
354 m_delayTimer.restart();
355 }
356 } else {
357 mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
358 }
359 if (!m_queue.isEmpty()) {
360 QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
361 }
362}
363
364void PainterGL::forceDraw() {
365 m_painter.begin(m_gl->context()->device());
366 performDraw();
367 m_painter.end();
368 m_backend->swap(m_backend);
369}
370
371void PainterGL::stop() {
372 m_active = false;
373 m_started = false;
374 m_gl->makeCurrent();
375#if defined(_WIN32) && defined(USE_EPOXY)
376 epoxy_handle_external_wglMakeCurrent();
377#endif
378 dequeueAll();
379 m_backend->clear(m_backend);
380 m_backend->swap(m_backend);
381 m_gl->doneCurrent();
382 m_gl->context()->moveToThread(m_gl->thread());
383 m_context.reset();
384 moveToThread(m_gl->thread());
385}
386
387void PainterGL::pause() {
388 m_active = false;
389}
390
391void PainterGL::unpause() {
392 m_active = true;
393}
394
395void PainterGL::performDraw() {
396 m_painter.beginNativePainting();
397#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
398 float r = m_gl->devicePixelRatioF();
399#else
400 float r = m_gl->devicePixelRatio();
401#endif
402 m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
403 m_backend->drawFrame(m_backend);
404 m_painter.endNativePainting();
405 if (m_messagePainter) {
406 m_messagePainter->paint(&m_painter);
407 }
408}
409
410void PainterGL::enqueue(const uint32_t* backing) {
411 m_mutex.lock();
412 uint32_t* buffer;
413 if (m_free.isEmpty()) {
414 buffer = m_queue.dequeue();
415 } else {
416 buffer = m_free.takeLast();
417 }
418 QSize size = m_context->screenDimensions();
419 memcpy(buffer, backing, size.width() * size.height() * BYTES_PER_PIXEL);
420 m_queue.enqueue(buffer);
421 m_mutex.unlock();
422}
423
424void PainterGL::dequeue() {
425 m_mutex.lock();
426 if (m_queue.isEmpty()) {
427 m_mutex.unlock();
428 return;
429 }
430 uint32_t* buffer = m_queue.dequeue();
431 m_backend->postFrame(m_backend, buffer);
432 m_free.append(buffer);
433 m_mutex.unlock();
434}
435
436void PainterGL::dequeueAll() {
437 uint32_t* buffer = 0;
438 m_mutex.lock();
439 while (!m_queue.isEmpty()) {
440 buffer = m_queue.dequeue();
441 m_free.append(buffer);
442 }
443 if (buffer) {
444 m_backend->postFrame(m_backend, buffer);
445 }
446 m_mutex.unlock();
447}
448
449void PainterGL::setShaders(struct VDir* dir) {
450 if (!supportsShaders()) {
451 return;
452 }
453#if !defined(_WIN32) || defined(USE_EPOXY)
454 m_gl->makeCurrent();
455#if defined(_WIN32) && defined(USE_EPOXY)
456 epoxy_handle_external_wglMakeCurrent();
457#endif
458 if (m_shader.passes) {
459 mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
460 mGLES2ShaderFree(&m_shader);
461 }
462 mGLES2ShaderLoad(&m_shader, dir);
463 if (m_started) {
464 mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
465 }
466 m_gl->doneCurrent();
467#endif
468}
469
470void PainterGL::clearShaders() {
471 if (!supportsShaders()) {
472 return;
473 }
474#if !defined(_WIN32) || defined(USE_EPOXY)
475 m_gl->makeCurrent();
476#if defined(_WIN32) && defined(USE_EPOXY)
477 epoxy_handle_external_wglMakeCurrent();
478#endif
479 if (m_shader.passes) {
480 mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
481 mGLES2ShaderFree(&m_shader);
482 }
483 m_gl->doneCurrent();
484#endif
485}
486
487VideoShader* PainterGL::shaders() {
488 return &m_shader;
489}
490
491#endif