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)
18#include "platform/opengl/gles2.h"
19#endif
20}
21
22using namespace QGBA;
23
24DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent)
25 : Display(parent)
26 , m_isDrawing(false)
27 , m_gl(new EmptyGLWidget(format, this))
28 , m_drawThread(nullptr)
29 , m_context(nullptr)
30{
31 m_painter = new PainterGL(m_gl, QGLFormat::openGLVersionFlags());
32 m_gl->setMouseTracking(true);
33 m_gl->setAttribute(Qt::WA_TransparentForMouseEvents); // This doesn't seem to work?
34}
35
36DisplayGL::~DisplayGL() {
37 delete m_painter;
38}
39
40void DisplayGL::startDrawing(GBAThread* thread) {
41 if (m_drawThread) {
42 return;
43 }
44 m_isDrawing = true;
45 m_painter->setContext(thread);
46 m_painter->setMessagePainter(messagePainter());
47 m_context = thread;
48 m_painter->resize(size());
49 m_gl->move(0, 0);
50 m_drawThread = new QThread(this);
51 m_drawThread->setObjectName("Painter Thread");
52 m_gl->context()->doneCurrent();
53 m_gl->context()->moveToThread(m_drawThread);
54 m_painter->moveToThread(m_drawThread);
55 connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
56 m_drawThread->start();
57 GBASyncSetVideoSync(&m_context->sync, false);
58
59 lockAspectRatio(isAspectRatioLocked());
60 filter(isFiltered());
61 messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
62 resizePainter();
63}
64
65void DisplayGL::stopDrawing() {
66 if (m_drawThread) {
67 m_isDrawing = false;
68 if (GBAThreadIsActive(m_context)) {
69 GBAThreadInterrupt(m_context);
70 }
71 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
72 m_drawThread->exit();
73 m_drawThread = nullptr;
74 if (GBAThreadIsActive(m_context)) {
75 GBAThreadContinue(m_context);
76 }
77 }
78}
79
80void DisplayGL::pauseDrawing() {
81 if (m_drawThread) {
82 m_isDrawing = false;
83 if (GBAThreadIsActive(m_context)) {
84 GBAThreadInterrupt(m_context);
85 }
86 QMetaObject::invokeMethod(m_painter, "pause", Qt::BlockingQueuedConnection);
87 if (GBAThreadIsActive(m_context)) {
88 GBAThreadContinue(m_context);
89 }
90 }
91}
92
93void DisplayGL::unpauseDrawing() {
94 if (m_drawThread) {
95 m_isDrawing = true;
96 if (GBAThreadIsActive(m_context)) {
97 GBAThreadInterrupt(m_context);
98 }
99 QMetaObject::invokeMethod(m_painter, "unpause", Qt::BlockingQueuedConnection);
100 if (GBAThreadIsActive(m_context)) {
101 GBAThreadContinue(m_context);
102 }
103 }
104}
105
106void DisplayGL::forceDraw() {
107 if (m_drawThread) {
108 QMetaObject::invokeMethod(m_painter, "forceDraw");
109 }
110}
111
112void DisplayGL::lockAspectRatio(bool lock) {
113 Display::lockAspectRatio(lock);
114 if (m_drawThread) {
115 QMetaObject::invokeMethod(m_painter, "lockAspectRatio", Q_ARG(bool, lock));
116 }
117}
118
119void DisplayGL::filter(bool filter) {
120 Display::filter(filter);
121 if (m_drawThread) {
122 QMetaObject::invokeMethod(m_painter, "filter", Q_ARG(bool, filter));
123 }
124}
125
126void DisplayGL::framePosted(const uint32_t* buffer) {
127 if (m_drawThread && buffer) {
128 m_painter->enqueue(buffer);
129 QMetaObject::invokeMethod(m_painter, "draw");
130 }
131}
132
133void DisplayGL::resizeEvent(QResizeEvent* event) {
134 Display::resizeEvent(event);
135 resizePainter();
136}
137
138void DisplayGL::resizePainter() {
139 m_gl->resize(size());
140 if (m_drawThread) {
141 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size()));
142 }
143}
144
145PainterGL::PainterGL(QGLWidget* parent, QGLFormat::OpenGLVersionFlags glVersion)
146 : m_gl(parent)
147 , m_active(false)
148 , m_context(nullptr)
149 , m_messagePainter(nullptr)
150{
151#ifdef BUILD_GL
152 GBAGLContext* glBackend;
153#endif
154#ifndef _WIN32
155 GBAGLES2Context* gl2Backend;
156#endif
157
158#ifndef _WIN32
159 if (glVersion & QGLFormat::OpenGL_Version_3_0) {
160 gl2Backend = new GBAGLES2Context;
161 GBAGLES2ContextCreate(gl2Backend);
162 m_backend = &gl2Backend->d;
163 } else {
164#else
165 {
166#endif
167 glBackend = new GBAGLContext;
168 GBAGLContextCreate(glBackend);
169 m_backend = &glBackend->d;
170 }
171 m_backend->swap = [](VideoBackend* v) {
172 PainterGL* painter = static_cast<PainterGL*>(v->user);
173 painter->m_gl->swapBuffers();
174 };
175 m_backend->user = this;
176 m_backend->filter = false;
177 m_backend->lockAspectRatio = false;
178
179 for (int i = 0; i < 2; ++i) {
180 m_free.append(new uint32_t[256 * 256]);
181 }
182}
183
184PainterGL::~PainterGL() {
185 while (!m_queue.isEmpty()) {
186 delete[] m_queue.dequeue();
187 }
188 for (auto item : m_free) {
189 delete[] item;
190 }
191 delete m_backend;
192 m_backend = nullptr;
193}
194
195void PainterGL::setContext(GBAThread* context) {
196 m_context = context;
197}
198
199void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
200 m_messagePainter = messagePainter;
201}
202
203void PainterGL::resize(const QSize& size) {
204 m_size = size;
205 if (m_active) {
206 forceDraw();
207 }
208}
209
210void PainterGL::lockAspectRatio(bool lock) {
211 m_backend->lockAspectRatio = lock;
212 if (m_active) {
213 forceDraw();
214 }
215}
216
217void PainterGL::filter(bool filter) {
218 m_backend->filter = filter;
219 if (m_active) {
220 forceDraw();
221 }
222}
223
224void PainterGL::start() {
225 m_gl->makeCurrent();
226 m_backend->init(m_backend, reinterpret_cast<WHandle>(m_gl->winId()));
227 m_gl->doneCurrent();
228 m_active = true;
229}
230
231void PainterGL::draw() {
232 if (m_queue.isEmpty() || !GBAThreadIsActive(m_context)) {
233 return;
234 }
235 if (GBASyncWaitFrameStart(&m_context->sync) || !m_queue.isEmpty()) {
236 dequeue();
237 m_painter.begin(m_gl->context()->device());
238 performDraw();
239 m_painter.end();
240 GBASyncWaitFrameEnd(&m_context->sync);
241 m_backend->swap(m_backend);
242 } else {
243 GBASyncWaitFrameEnd(&m_context->sync);
244 }
245 if (!m_queue.isEmpty()) {
246 QMetaObject::invokeMethod(this, "draw", Qt::QueuedConnection);
247 }
248}
249
250void PainterGL::forceDraw() {
251 m_painter.begin(m_gl->context()->device());
252 performDraw();
253 m_painter.end();
254 m_backend->swap(m_backend);
255}
256
257void PainterGL::stop() {
258 m_active = false;
259 m_gl->makeCurrent();
260 dequeueAll();
261 m_backend->clear(m_backend);
262 m_backend->swap(m_backend);
263 m_backend->deinit(m_backend);
264 m_gl->doneCurrent();
265 m_gl->context()->moveToThread(m_gl->thread());
266 moveToThread(m_gl->thread());
267}
268
269void PainterGL::pause() {
270 m_active = false;
271}
272
273void PainterGL::unpause() {
274 m_active = true;
275}
276
277void PainterGL::performDraw() {
278 m_painter.beginNativePainting();
279 float r = m_gl->devicePixelRatio();
280 m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r);
281 m_backend->drawFrame(m_backend);
282 m_painter.endNativePainting();
283 if (m_messagePainter) {
284 m_messagePainter->paint(&m_painter);
285 }
286}
287
288void PainterGL::enqueue(const uint32_t* backing) {
289 m_mutex.lock();
290 uint32_t* buffer;
291 if (m_free.isEmpty()) {
292 buffer = m_queue.dequeue();
293 } else {
294 buffer = m_free.takeLast();
295 }
296 memcpy(buffer, backing, 256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
297 m_queue.enqueue(buffer);
298 m_mutex.unlock();
299}
300
301void PainterGL::dequeue() {
302 m_mutex.lock();
303 if (m_queue.isEmpty()) {
304 m_mutex.unlock();
305 return;
306 }
307 uint32_t* buffer = m_queue.dequeue();
308 m_backend->postFrame(m_backend, buffer);
309 m_free.append(buffer);
310 m_mutex.unlock();
311}
312
313void PainterGL::dequeueAll() {
314 uint32_t* buffer = 0;
315 m_mutex.lock();
316 while (!m_queue.isEmpty()) {
317 buffer = m_queue.dequeue();
318 m_free.append(buffer);
319 }
320 if (buffer) {
321 m_backend->postFrame(m_backend, buffer);
322 }
323 m_mutex.unlock();
324}