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 "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(new EmptyGLWidget(format, this))
33{
34 m_painter = new PainterGL(format.majorVersion() < 2 ? 1 : m_gl->format().majorVersion(), m_gl);
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(std::shared_ptr<CoreController> controller) {
58 if (m_drawThread) {
59 return;
60 }
61 m_isDrawing = true;
62 m_painter->setContext(controller);
63 m_painter->setMessagePainter(messagePainter());
64 m_context = controller;
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, &QThread::started, m_painter, &PainterGL::start);
73 m_drawThread->start();
74
75 lockAspectRatio(isAspectRatioLocked());
76 lockIntegerScaling(isIntegerScalingLocked());
77 filter(isFiltered());
78#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
79 messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF());
80#else
81 messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
82#endif
83 resizePainter();
84}
85
86void DisplayGL::stopDrawing() {
87 if (m_drawThread) {
88 m_isDrawing = false;
89 CoreController::Interrupter interrupter(m_context);
90 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
91 m_drawThread->exit();
92 m_drawThread = nullptr;
93 }
94 m_context.reset();
95}
96
97void DisplayGL::pauseDrawing() {
98 if (m_drawThread) {
99 m_isDrawing = false;
100 CoreController::Interrupter interrupter(m_context);
101 QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
102 }
103}
104
105void DisplayGL::unpauseDrawing() {
106 if (m_drawThread) {
107 m_isDrawing = true;
108 CoreController::Interrupter interrupter(m_context);
109 QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
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::lockIntegerScaling(bool lock) {
127 Display::lockIntegerScaling(lock);
128 if (m_drawThread) {
129 QMetaObject::invokeMethod(m_painter, "lockIntegerScaling", Q_ARG(bool, lock));
130 }
131}
132
133void DisplayGL::filter(bool filter) {
134 Display::filter(filter);
135 if (m_drawThread) {
136 QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
137 }
138}
139
140void DisplayGL::framePosted() {
141 if (m_drawThread) {
142 m_painter->enqueue(m_context->drawContext());
143 QMetaObject::invokeMethod(m_painter, "draw");
144 }
145}
146
147void DisplayGL::setShaders(struct VDir* shaders) {
148 if (m_drawThread) {
149 QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
150 } else {
151 m_painter->setShaders(shaders);
152 }
153}
154
155void DisplayGL::clearShaders() {
156 QMetaObject::invokeMethod(m_painter, "clearShaders");
157}
158
159void DisplayGL::resizeEvent(QResizeEvent* event) {
160 Display::resizeEvent(event);
161 resizePainter();
162}
163
164void DisplayGL::resizePainter() {
165 m_gl->resize(size());
166 if (m_drawThread) {
167 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
168 }
169}
170
171PainterGL::PainterGL(int majorVersion, QGLWidget* parent)
172 : m_gl(parent)
173{
174#ifdef BUILD_GL
175 mGLContext* glBackend;
176#endif
177#if !defined(_WIN32) || defined(USE_EPOXY)
178 mGLES2Context* gl2Backend;
179#endif
180
181#if !defined(_WIN32) || defined(USE_EPOXY)
182 if (majorVersion >= 2) {
183 gl2Backend = new mGLES2Context;
184 mGLES2ContextCreate(gl2Backend);
185 m_backend = &gl2Backend->d;
186 m_supportsShaders = true;
187 }
188#endif
189
190#ifdef BUILD_GL
191 if (!m_backend) {
192 glBackend = new mGLContext;
193 mGLContextCreate(glBackend);
194 m_backend = &glBackend->d;
195 m_supportsShaders = false;
196 }
197#endif
198 m_backend->swap = [](VideoBackend* v) {
199 PainterGL* painter = static_cast<PainterGL*>(v->user);
200 painter->m_gl->swapBuffers();
201 };
202
203 m_gl->makeCurrent();
204#if defined(_WIN32) && defined(USE_EPOXY)
205 epoxy_handle_external_wglMakeCurrent();
206#endif
207 m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
208#if !defined(_WIN32) || defined(USE_EPOXY)
209 if (m_supportsShaders) {
210 m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<mGLES2Context*>(m_backend)->initialShader);
211 }
212#endif
213 m_gl->doneCurrent();
214
215 m_backend->user = this;
216 m_backend->filter = false;
217 m_backend->lockAspectRatio = false;
218
219 for (int i = 0; i < 2; ++i) {
220 m_free.append(new uint32_t[256 * 256]);
221 }
222}
223
224PainterGL::~PainterGL() {
225 while (!m_queue.isEmpty()) {
226 delete[] m_queue.dequeue();
227 }
228 for (auto item : m_free) {
229 delete[] item;
230 }
231 m_gl->makeCurrent();
232#if defined(_WIN32) && defined(USE_EPOXY)
233 epoxy_handle_external_wglMakeCurrent();
234#endif
235#if !defined(_WIN32) || defined(USE_EPOXY)
236 if (m_shader.passes) {
237 mGLES2ShaderFree(&m_shader);
238 }
239#endif
240 m_backend->deinit(m_backend);
241 m_gl->doneCurrent();
242 delete m_backend;
243 m_backend = nullptr;
244}
245
246void PainterGL::setContext(std::shared_ptr<CoreController> context) {
247 m_context = context;
248
249 if (!context) {
250 return;
251 }
252
253 m_gl->makeCurrent();
254#if defined(_WIN32) && defined(USE_EPOXY)
255 epoxy_handle_external_wglMakeCurrent();
256#endif
257 QSize size = m_context->screenDimensions();
258 m_backend->setDimensions(m_backend, size.width(), size.height());
259 m_gl->doneCurrent();
260}
261
262void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
263 m_messagePainter = messagePainter;
264}
265
266void PainterGL::resize(const QSize& size) {
267 m_size = size;
268 if (m_started && !m_active) {
269 forceDraw();
270 }
271}
272
273void PainterGL::lockAspectRatio(bool lock) {
274 m_backend->lockAspectRatio = lock;
275 if (m_started && !m_active) {
276 forceDraw();
277 }
278}
279
280void PainterGL::lockIntegerScaling(bool lock) {
281 m_backend->lockIntegerScaling = lock;
282 if (m_started && !m_active) {
283 forceDraw();
284 }
285}
286
287void PainterGL::filter(bool filter) {
288 m_backend->filter = filter;
289 if (m_started && !m_active) {
290 forceDraw();
291 }
292}
293
294void PainterGL::start() {
295 m_gl->makeCurrent();
296#if defined(_WIN32) && defined(USE_EPOXY)
297 epoxy_handle_external_wglMakeCurrent();
298#endif
299
300#if !defined(_WIN32) || defined(USE_EPOXY)
301 if (m_supportsShaders && m_shader.passes) {
302 mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
303 }
304#endif
305
306 m_gl->doneCurrent();
307 m_active = true;
308 m_started = true;
309}
310
311void PainterGL::draw() {
312 if (m_queue.isEmpty()) {
313 return;
314 }
315
316 if (mCoreSyncWaitFrameStart(&m_context->thread()->impl->sync) || !m_queue.isEmpty()) {
317 dequeue();
318 mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
319 m_painter.begin(m_gl->context()->device());
320 performDraw();
321 m_painter.end();
322 m_backend->swap(m_backend);
323 if (!m_delayTimer.isValid()) {
324 m_delayTimer.start();
325 } else {
326 while (m_delayTimer.elapsed() < 15) {
327 QThread::usleep(100);
328 }
329 m_delayTimer.restart();
330 }
331 } else {
332 mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync);
333 }
334 if (!m_queue.isEmpty()) {
335 QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
336 }
337}
338
339void PainterGL::forceDraw() {
340 m_painter.begin(m_gl->context()->device());
341 performDraw();
342 m_painter.end();
343 m_backend->swap(m_backend);
344}
345
346void PainterGL::stop() {
347 m_active = false;
348 m_started = false;
349 m_gl->makeCurrent();
350#if defined(_WIN32) && defined(USE_EPOXY)
351 epoxy_handle_external_wglMakeCurrent();
352#endif
353 dequeueAll();
354 m_backend->clear(m_backend);
355 m_backend->swap(m_backend);
356 m_gl->doneCurrent();
357 m_gl->context()->moveToThread(m_gl->thread());
358 m_context.reset();
359 moveToThread(m_gl->thread());
360}
361
362void PainterGL::pause() {
363 m_active = false;
364}
365
366void PainterGL::unpause() {
367 m_active = true;
368}
369
370void PainterGL::performDraw() {
371 m_painter.beginNativePainting();
372#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
373 float r = m_gl->devicePixelRatioF();
374#else
375 float r = m_gl->devicePixelRatio();
376#endif
377 m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
378 m_backend->drawFrame(m_backend);
379 m_painter.endNativePainting();
380 if (m_messagePainter) {
381 m_messagePainter->paint(&m_painter);
382 }
383}
384
385void PainterGL::enqueue(const uint32_t* backing) {
386 m_mutex.lock();
387 uint32_t* buffer;
388 if (m_free.isEmpty()) {
389 buffer = m_queue.dequeue();
390 } else {
391 buffer = m_free.takeLast();
392 }
393 QSize size = m_context->screenDimensions();
394 memcpy(buffer, backing, size.width() * size.height() * BYTES_PER_PIXEL);
395 m_queue.enqueue(buffer);
396 m_mutex.unlock();
397}
398
399void PainterGL::dequeue() {
400 m_mutex.lock();
401 if (m_queue.isEmpty()) {
402 m_mutex.unlock();
403 return;
404 }
405 uint32_t* buffer = m_queue.dequeue();
406 m_backend->postFrame(m_backend, buffer);
407 m_free.append(buffer);
408 m_mutex.unlock();
409}
410
411void PainterGL::dequeueAll() {
412 uint32_t* buffer = 0;
413 m_mutex.lock();
414 while (!m_queue.isEmpty()) {
415 buffer = m_queue.dequeue();
416 m_free.append(buffer);
417 }
418 if (buffer) {
419 m_backend->postFrame(m_backend, buffer);
420 }
421 m_mutex.unlock();
422}
423
424void PainterGL::setShaders(struct VDir* dir) {
425 if (!supportsShaders()) {
426 return;
427 }
428#if !defined(_WIN32) || defined(USE_EPOXY)
429 m_gl->makeCurrent();
430#if defined(_WIN32) && defined(USE_EPOXY)
431 epoxy_handle_external_wglMakeCurrent();
432#endif
433 if (m_shader.passes) {
434 mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
435 mGLES2ShaderFree(&m_shader);
436 }
437 mGLES2ShaderLoad(&m_shader, dir);
438 if (m_started) {
439 mGLES2ShaderAttach(reinterpret_cast<mGLES2Context*>(m_backend), static_cast<mGLES2Shader*>(m_shader.passes), m_shader.nPasses);
440 }
441 m_gl->doneCurrent();
442#endif
443}
444
445void PainterGL::clearShaders() {
446 if (!supportsShaders()) {
447 return;
448 }
449#if !defined(_WIN32) || defined(USE_EPOXY)
450 m_gl->makeCurrent();
451#if defined(_WIN32) && defined(USE_EPOXY)
452 epoxy_handle_external_wglMakeCurrent();
453#endif
454 if (m_shader.passes) {
455 mGLES2ShaderDetach(reinterpret_cast<mGLES2Context*>(m_backend));
456 mGLES2ShaderFree(&m_shader);
457 }
458 m_gl->doneCurrent();
459#endif
460}
461
462VideoShader* PainterGL::shaders() {
463 return &m_shader;
464}
465
466#endif