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