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