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
71void Display::initializeGL() {
72 glClearColor(0, 0, 0, 0);
73 glClear(GL_COLOR_BUFFER_BIT);
74 swapBuffers();
75}
76
77void Display::resizeEvent(QResizeEvent* event) {
78 if (m_drawThread) {
79 GBAThreadInterrupt(m_context);
80 GBASyncSuspendDrawing(&m_context->sync);
81 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
82 GBASyncResumeDrawing(&m_context->sync);
83 GBAThreadContinue(m_context);
84 }
85}
86
87Painter::Painter(Display* parent)
88 : m_gl(parent)
89{
90 m_size = parent->size();
91}
92
93void Painter::setContext(GBAThread* context) {
94 m_context = context;
95}
96
97void Painter::setBacking(const uint32_t* backing) {
98 m_backing = backing;
99}
100
101void Painter::resize(const QSize& size) {
102 m_size = size;
103 m_gl->makeCurrent();
104 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
105 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
106 m_gl->swapBuffers();
107 m_gl->doneCurrent();
108}
109
110void Painter::start() {
111 m_gl->makeCurrent();
112 glEnable(GL_TEXTURE_2D);
113 glGenTextures(1, &m_tex);
114 glBindTexture(GL_TEXTURE_2D, m_tex);
115 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
116 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
117 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
118 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
119 glEnableClientState(GL_VERTEX_ARRAY);
120 glVertexPointer(2, GL_INT, 0, _glVertices);
121 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
122 glMatrixMode(GL_PROJECTION);
123 glLoadIdentity();
124 glOrtho(0, 240, 160, 0, 0, 1);
125 glMatrixMode(GL_MODELVIEW);
126 glLoadIdentity();
127 m_gl->doneCurrent();
128
129 m_drawTimer = new QTimer;
130 m_drawTimer->moveToThread(QThread::currentThread());
131 m_drawTimer->setInterval(0);
132 connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
133 m_drawTimer->start();
134}
135
136void Painter::draw() {
137 m_gl->makeCurrent();
138 if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
139 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
141 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
142 if (m_context->sync.videoFrameWait) {
143 glFlush();
144 }
145 }
146 GBASyncWaitFrameEnd(&m_context->sync);
147 m_gl->swapBuffers();
148 m_gl->doneCurrent();
149}
150
151void Painter::forceDraw() {
152 m_gl->makeCurrent();
153 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
154 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
155 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
156 if (m_context->sync.videoFrameWait) {
157 glFlush();
158 }
159 m_gl->swapBuffers();
160 m_gl->doneCurrent();
161}
162
163void Painter::stop() {
164 m_drawTimer->stop();
165 delete m_drawTimer;
166 m_gl->makeCurrent();
167 glDeleteTextures(1, &m_tex);
168 glClear(GL_COLOR_BUFFER_BIT);
169 m_gl->swapBuffers();
170 m_gl->doneCurrent();
171 m_gl->context()->moveToThread(QApplication::instance()->thread());
172}