src/platform/qt/Display.cpp (view raw)
1#include "Display.h"
2
3#include <QApplication>
4#include <QResizeEvent>
5
6extern "C" {
7#include "gba-thread.h"
8}
9
10using namespace QGBA;
11
12static const GLint _glVertices[] = {
13 0, 0,
14 256, 0,
15 256, 256,
16 0, 256
17};
18
19static const GLint _glTexCoords[] = {
20 0, 0,
21 1, 0,
22 1, 1,
23 0, 1
24};
25
26Display::Display(QGLFormat format, QWidget* parent)
27 : QGLWidget(format, parent)
28 , m_painter(nullptr)
29 , m_drawThread(nullptr)
30{
31 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
32 setMinimumSize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
33 setAutoBufferSwap(false);
34 setCursor(Qt::BlankCursor);
35}
36
37void Display::startDrawing(const uint32_t* buffer, GBAThread* thread) {
38 if (m_drawThread) {
39 return;
40 }
41 m_drawThread = new QThread(this);
42 m_painter = new Painter(this);
43 m_painter->setContext(thread);
44 m_painter->setBacking(buffer);
45 m_painter->moveToThread(m_drawThread);
46 m_context = thread;
47 doneCurrent();
48 context()->moveToThread(m_drawThread);
49 connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
50 m_drawThread->start(QThread::TimeCriticalPriority);
51}
52
53void Display::stopDrawing() {
54 if (m_drawThread) {
55 GBAThreadInterrupt(m_context);
56 GBASyncSuspendDrawing(&m_context->sync);
57 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
58 m_drawThread->exit();
59 m_drawThread = nullptr;
60 GBASyncResumeDrawing(&m_context->sync);
61 GBAThreadContinue(m_context);
62 }
63}
64
65void Display::forceDraw() {
66 if (m_drawThread) {
67 QMetaObject::invokeMethod(m_painter, "forceDraw", Qt::QueuedConnection);
68 }
69}
70
71#ifdef USE_PNG
72void Display::screenshot() {
73 GBAThreadInterrupt(m_context);
74 GBAThreadTakeScreenshot(m_context);
75 GBAThreadContinue(m_context);
76}
77#endif
78
79void Display::initializeGL() {
80 glClearColor(0, 0, 0, 0);
81 glClear(GL_COLOR_BUFFER_BIT);
82 swapBuffers();
83}
84
85void Display::resizeEvent(QResizeEvent* event) {
86 if (m_drawThread) {
87 GBAThreadInterrupt(m_context);
88 GBASyncSuspendDrawing(&m_context->sync);
89 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
90 GBASyncResumeDrawing(&m_context->sync);
91 GBAThreadContinue(m_context);
92 }
93}
94
95Painter::Painter(Display* parent)
96 : m_gl(parent)
97{
98 m_size = parent->size();
99}
100
101void Painter::setContext(GBAThread* context) {
102 m_context = context;
103}
104
105void Painter::setBacking(const uint32_t* backing) {
106 m_backing = backing;
107}
108
109void Painter::resize(const QSize& size) {
110 m_size = size;
111 m_gl->makeCurrent();
112 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
113 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
114 m_gl->swapBuffers();
115 m_gl->doneCurrent();
116}
117
118void Painter::start() {
119 m_gl->makeCurrent();
120 glEnable(GL_TEXTURE_2D);
121 glGenTextures(1, &m_tex);
122 glBindTexture(GL_TEXTURE_2D, m_tex);
123 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
124 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
126 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
127 glEnableClientState(GL_VERTEX_ARRAY);
128 glVertexPointer(2, GL_INT, 0, _glVertices);
129 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
130 glMatrixMode(GL_PROJECTION);
131 glLoadIdentity();
132 glOrtho(0, 240, 160, 0, 0, 1);
133 glMatrixMode(GL_MODELVIEW);
134 glLoadIdentity();
135 m_gl->doneCurrent();
136
137 m_drawTimer = new QTimer;
138 m_drawTimer->moveToThread(QThread::currentThread());
139 m_drawTimer->setInterval(0);
140 connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
141 m_drawTimer->start();
142}
143
144void Painter::draw() {
145 m_gl->makeCurrent();
146 if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
147 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
149 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
150 if (m_context->sync.videoFrameWait) {
151 glFlush();
152 }
153 }
154 GBASyncWaitFrameEnd(&m_context->sync);
155 m_gl->swapBuffers();
156 m_gl->doneCurrent();
157}
158
159void Painter::forceDraw() {
160 m_gl->makeCurrent();
161 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
163 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
164 if (m_context->sync.videoFrameWait) {
165 glFlush();
166 }
167 m_gl->swapBuffers();
168 m_gl->doneCurrent();
169}
170
171void Painter::stop() {
172 m_drawTimer->stop();
173 delete m_drawTimer;
174 m_gl->makeCurrent();
175 glDeleteTextures(1, &m_tex);
176 glClear(GL_COLOR_BUFFER_BIT);
177 m_gl->swapBuffers();
178 m_gl->doneCurrent();
179 m_gl->context()->moveToThread(QApplication::instance()->thread());
180}