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(QWidget* parent)
27 : QGLWidget(QGLFormat(QGL::Rgba | QGL::SingleBuffer), parent)
28 , m_painter(nullptr)
29 , m_drawThread(nullptr)
30{
31 setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
32 setAutoBufferSwap(false);
33}
34
35void Display::startDrawing(const uint32_t* buffer, GBAThread* thread) {
36 if (m_drawThread) {
37 return;
38 }
39 m_drawThread = new QThread(this);
40 m_painter = new Painter(this);
41 m_painter->setContext(thread);
42 m_painter->setBacking(buffer);
43 m_painter->moveToThread(m_drawThread);
44 doneCurrent();
45 context()->moveToThread(m_drawThread);
46 connect(m_drawThread, SIGNAL(started()), m_painter, SLOT(start()));
47 m_drawThread->start();
48}
49
50void Display::stopDrawing() {
51 if (m_drawThread) {
52 QMetaObject::invokeMethod(m_painter, "stop", Qt::BlockingQueuedConnection);
53 m_drawThread->exit();
54 m_drawThread = nullptr;
55 }
56}
57
58void Display::initializeGL() {
59 glClearColor(0, 0, 0, 0);
60 glClear(GL_COLOR_BUFFER_BIT);
61 swapBuffers();
62}
63
64void Display::resizeEvent(QResizeEvent* event) {
65 if (m_painter) {
66 QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, event->size()));
67 }
68}
69
70Painter::Painter(Display* parent)
71 : m_gl(parent)
72{
73 m_size = parent->size();
74}
75
76void Painter::setContext(GBAThread* context) {
77 m_context = context;
78}
79
80void Painter::setBacking(const uint32_t* backing) {
81 m_backing = backing;
82}
83
84void Painter::resize(const QSize& size) {
85 m_size = size;
86 m_gl->makeCurrent();
87 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
88 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
89 m_gl->swapBuffers();
90 m_gl->doneCurrent();
91}
92
93void Painter::start() {
94 m_gl->makeCurrent();
95 glEnable(GL_TEXTURE_2D);
96 glGenTextures(1, &m_tex);
97 glBindTexture(GL_TEXTURE_2D, m_tex);
98 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
99 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
100 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
101 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
102 glEnableClientState(GL_VERTEX_ARRAY);
103 glVertexPointer(2, GL_INT, 0, _glVertices);
104 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
105 glMatrixMode(GL_PROJECTION);
106 glLoadIdentity();
107 glOrtho(0, 240, 160, 0, 0, 1);
108 glMatrixMode(GL_MODELVIEW);
109 glLoadIdentity();
110 m_gl->doneCurrent();
111
112 m_drawTimer = new QTimer;
113 m_drawTimer->moveToThread(QThread::currentThread());
114 m_drawTimer->setInterval(0);
115 connect(m_drawTimer, SIGNAL(timeout()), this, SLOT(draw()));
116 m_drawTimer->start();
117}
118
119void Painter::draw() {
120 m_gl->makeCurrent();
121 if (GBASyncWaitFrameStart(&m_context->sync, m_context->frameskip)) {
122 glViewport(0, 0, m_size.width() * m_gl->devicePixelRatio(), m_size.height() * m_gl->devicePixelRatio());
123 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_backing);
124 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
125 if (m_context->sync.videoFrameWait) {
126 glFlush();
127 }
128 }
129 GBASyncWaitFrameEnd(&m_context->sync);
130 m_gl->swapBuffers();
131 m_gl->doneCurrent();
132}
133
134void Painter::stop() {
135 m_drawTimer->stop();
136 delete m_drawTimer;
137 m_gl->makeCurrent();
138 glDeleteTextures(1, &m_tex);
139 glClear(GL_COLOR_BUFFER_BIT);
140 m_gl->swapBuffers();
141 m_gl->doneCurrent();
142 m_gl->context()->moveToThread(QApplication::instance()->thread());
143}