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 if (m_drawThread) {
152 QMetaObject::invokeMethod(m_painter, "setShaders", Qt::BlockingQueuedConnection, Q_ARG(struct VDir*, shaders));
153 } else {
154 m_painter->setShaders(shaders);
155 }
156}
157
158void DisplayGL::clearShaders() {
159 QMetaObject::invokeMethod(m_painter, "clearShaders");
160}
161
162void DisplayGL::resizeEvent(QResizeEvent* event) {
163 Display::resizeEvent(event);
164 resizePainter();
165}
166
167void DisplayGL::resizePainter() {
168 m_gl->resize(size());
169 if (m_drawThread) {
170 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
171 }
172}
173
174PainterGL::PainterGL(QGLWidget* parent, QGLFormat::OpenGLVersionFlags glVersion)
175 : m_gl(parent)
176 , m_active(false)
177 , m_started(false)
178 , m_context(nullptr)
179 , m_shader{}
180 , m_backend(nullptr)
181 , m_messagePainter(nullptr)
182{
183#ifdef BUILD_GL
184 GBAGLContext* glBackend;
185#endif
186#if !defined(_WIN32) || defined(USE_EPOXY)
187 GBAGLES2Context* gl2Backend;
188#endif
189
190#if !defined(_WIN32) || defined(USE_EPOXY)
191 if (glVersion & QGLFormat::OpenGL_Version_3_0) {
192 gl2Backend = new GBAGLES2Context;
193 GBAGLES2ContextCreate(gl2Backend);
194 m_backend = &gl2Backend->d;
195 m_supportsShaders = true;
196 }
197#endif
198
199#ifdef BUILD_GL
200 if (!m_backend) {
201 glBackend = new GBAGLContext;
202 GBAGLContextCreate(glBackend);
203 m_backend = &glBackend->d;
204 m_supportsShaders = false;
205 }
206#endif
207 m_backend->swap = [](VideoBackend* v) {
208 PainterGL* painter = static_cast<PainterGL*>(v->user);
209 painter->m_gl->swapBuffers();
210 };
211
212 m_gl->makeCurrent();
213#if defined(_WIN32) && defined(USE_EPOXY)
214 epoxy_handle_external_wglMakeCurrent();
215#endif
216 m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
217#if !defined(_WIN32) || defined(USE_EPOXY)
218 if (m_supportsShaders) {
219 m_shader.preprocessShader = static_cast<void*>(&reinterpret_cast<GBAGLES2Context*>(m_backend)->initialShader);
220 }
221#endif
222 m_gl->doneCurrent();
223
224 m_backend->user = this;
225 m_backend->filter = false;
226 m_backend->lockAspectRatio = false;
227
228 for (int i = 0; i < 2; ++i) {
229 m_free.append(new uint32_t[256 * 256]);
230 }
231}
232
233PainterGL::~PainterGL() {
234 while (!m_queue.isEmpty()) {
235 delete[] m_queue.dequeue();
236 }
237 for (auto item : m_free) {
238 delete[] item;
239 }
240#if !defined(_WIN32) || defined(USE_EPOXY)
241 if (m_shader.passes) {
242 GBAGLES2ShaderFree(&m_shader);
243 }
244#endif
245 m_backend->deinit(m_backend);
246 delete m_backend;
247 m_backend = nullptr;
248}
249
250void PainterGL::setContext(GBAThread* context) {
251 m_context = context;
252}
253
254void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
255 m_messagePainter = messagePainter;
256}
257
258void PainterGL::resize(const QSize& size) {
259 m_size = size;
260 if (m_started && !m_active) {
261 forceDraw();
262 }
263}
264
265void PainterGL::lockAspectRatio(bool lock) {
266 m_backend->lockAspectRatio = lock;
267 if (m_started && !m_active) {
268 forceDraw();
269 }
270}
271
272void PainterGL::filter(bool filter) {
273 m_backend->filter = filter;
274 if (m_started && !m_active) {
275 forceDraw();
276 }
277}
278
279void PainterGL::start() {
280 m_gl->makeCurrent();
281#if defined(_WIN32) && defined(USE_EPOXY)
282 epoxy_handle_external_wglMakeCurrent();
283#endif
284
285#if !defined(_WIN32) || defined(USE_EPOXY)
286 if (m_supportsShaders && m_shader.passes) {
287 GBAGLES2ShaderAttach(reinterpret_cast<GBAGLES2Context*>(m_backend), static_cast<GBAGLES2Shader*>(m_shader.passes), m_shader.nPasses);
288 }
289#endif
290
291 m_gl->doneCurrent();
292 m_active = true;
293 m_started = true;
294}
295
296void PainterGL::draw() {
297 if (m_queue.isEmpty() || !GBAThreadIsActive(m_context)) {
298 return;
299 }
300 if (GBASyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
301 dequeue();
302 GBASyncWaitFrameEnd(&m_context->sync);
303 m_painter.begin(m_gl->context()->device());
304 performDraw();
305 m_painter.end();
306 m_backend->swap(m_backend);
307 } else {
308 GBASyncWaitFrameEnd(&m_context->sync);
309 }
310 if (!m_queue.isEmpty()) {
311 QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
312 }
313}
314
315void PainterGL::forceDraw() {
316 m_painter.begin(m_gl->context()->device());
317 performDraw();
318 m_painter.end();
319 m_backend->swap(m_backend);
320}
321
322void PainterGL::stop() {
323 m_active = false;
324 m_started = false;
325 m_gl->makeCurrent();
326#if defined(_WIN32) && defined(USE_EPOXY)
327 epoxy_handle_external_wglMakeCurrent();
328#endif
329 dequeueAll();
330 m_backend->clear(m_backend);
331 m_backend->swap(m_backend);
332 m_gl->doneCurrent();
333 m_gl->context()->moveToThread(m_gl->thread());
334 moveToThread(m_gl->thread());
335}
336
337void PainterGL::pause() {
338 m_active = false;
339}
340
341void PainterGL::unpause() {
342 m_active = true;
343}
344
345void PainterGL::performDraw() {
346 m_painter.beginNativePainting();
347 float r = m_gl->devicePixelRatio();
348 m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
349 m_backend->drawFrame(m_backend);
350 m_painter.endNativePainting();
351 if (m_messagePainter) {
352 m_messagePainter->paint(&m_painter);
353 }
354}
355
356void PainterGL::enqueue(const uint32_t* backing) {
357 m_mutex.lock();
358 uint32_t* buffer;
359 if (m_free.isEmpty()) {
360 buffer = m_queue.dequeue();
361 } else {
362 buffer = m_free.takeLast();
363 }
364 memcpy(buffer, backing, 256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
365 m_queue.enqueue(buffer);
366 m_mutex.unlock();
367}
368
369void PainterGL::dequeue() {
370 m_mutex.lock();
371 if (m_queue.isEmpty()) {
372 m_mutex.unlock();
373 return;
374 }
375 uint32_t* buffer = m_queue.dequeue();
376 m_backend->postFrame(m_backend, buffer);
377 m_free.append(buffer);
378 m_mutex.unlock();
379}
380
381void PainterGL::dequeueAll() {
382 uint32_t* buffer = 0;
383 m_mutex.lock();
384 while (!m_queue.isEmpty()) {
385 buffer = m_queue.dequeue();
386 m_free.append(buffer);
387 }
388 if (buffer) {
389 m_backend->postFrame(m_backend, buffer);
390 }
391 m_mutex.unlock();
392}
393
394void PainterGL::setShaders(struct VDir* dir) {
395 if (!supportsShaders()) {
396 return;
397 }
398#if !defined(_WIN32) || defined(USE_EPOXY)
399 m_gl->makeCurrent();
400#if defined(_WIN32) && defined(USE_EPOXY)
401 epoxy_handle_external_wglMakeCurrent();
402#endif
403 if (m_shader.passes) {
404 GBAGLES2ShaderDetach(reinterpret_cast<GBAGLES2Context*>(m_backend));
405 GBAGLES2ShaderFree(&m_shader);
406 }
407 GBAGLES2ShaderLoad(&m_shader, dir);
408 if (m_started) {
409 GBAGLES2ShaderAttach(reinterpret_cast<GBAGLES2Context*>(m_backend), static_cast<GBAGLES2Shader*>(m_shader.passes), m_shader.nPasses);
410 }
411 m_gl->doneCurrent();
412#endif
413}
414
415void PainterGL::clearShaders() {
416 if (!supportsShaders()) {
417 return;
418 }
419#if !defined(_WIN32) || defined(USE_EPOXY)
420 m_gl->makeCurrent();
421#if defined(_WIN32) && defined(USE_EPOXY)
422 epoxy_handle_external_wglMakeCurrent();
423#endif
424 if (m_shader.passes) {
425 GBAGLES2ShaderDetach(reinterpret_cast<GBAGLES2Context*>(m_backend));
426 GBAGLES2ShaderFree(&m_shader);
427 }
428 m_gl->doneCurrent();
429#endif
430}
431
432VideoShader* PainterGL::shaders() {
433 return &m_shader;
434}