Skip to content

Commit

Permalink
Qt: Fix frames getting backlogged (fixes #2122)
Browse files Browse the repository at this point in the history
  • Loading branch information
endrift committed Apr 19, 2021
1 parent fcdf6c5 commit fdbb442
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -16,6 +16,7 @@ Other fixes:
- Qt: Fix OpenGL renderer lagging behind when fast-forwarding (fixes mgba.io/i/2094)
- Qt: Fix smudged window icon on Windows
- Qt: Fix saving settings enabling camera when camera name changes (fixes mgba.io/i/2125)
- Qt: Fix frames getting backlogged (fixes mgba.io/i/2122)
Misc:
- Core: Truncate preloading ROMs that slightly exceed max size (fixes mgba.io/i/2093)
- GBA: Default-enable VBA bug compat for Ruby and Emerald ROM hacks
Expand Down
26 changes: 18 additions & 8 deletions src/platform/qt/DisplayGL.cpp
Expand Up @@ -371,7 +371,7 @@ void PainterGL::resizeContext() {
if (m_dims == size) {
return;
}
dequeueAll();
dequeueAll(false);
m_backend->setDimensions(m_backend, size.width(), size.height());
}

Expand Down Expand Up @@ -429,7 +429,7 @@ void PainterGL::start() {
}

void PainterGL::draw() {
if (!m_active || m_queue.isEmpty()) {
if (!m_started || m_queue.isEmpty()) {
return;
}
mCoreSync* sync = &m_context->thread()->impl->sync;
Expand Down Expand Up @@ -461,6 +461,11 @@ void PainterGL::draw() {
performDraw();
m_backend->swap(m_backend);
}

QMutexLocker locker(&m_mutex);
if (!m_queue.isEmpty()) {
QTimer::singleShot(1, this, &PainterGL::draw);
}
}

void PainterGL::forceDraw() {
Expand All @@ -477,7 +482,7 @@ void PainterGL::forceDraw() {
void PainterGL::stop() {
m_active = false;
m_started = false;
dequeueAll();
dequeueAll(false);
if (m_context) {
if (m_videoProxy) {
m_videoProxy->detach(m_context.get());
Expand All @@ -499,6 +504,7 @@ void PainterGL::stop() {

void PainterGL::pause() {
m_active = false;
dequeueAll(true);
}

void PainterGL::unpause() {
Expand Down Expand Up @@ -551,20 +557,24 @@ void PainterGL::dequeue() {
m_buffer = buffer;
}

void PainterGL::dequeueAll() {
void PainterGL::dequeueAll(bool keep) {
QMutexLocker locker(&m_mutex);
uint32_t* buffer = 0;
m_mutex.lock();
while (!m_queue.isEmpty()) {
buffer = m_queue.dequeue();
if (buffer) {
if (keep) {
if (m_buffer && buffer) {
m_free.append(m_buffer);
m_buffer = buffer;
}
} else if (buffer) {
m_free.append(buffer);
}
}
if (m_buffer) {
if (m_buffer && !keep) {
m_free.append(m_buffer);
m_buffer = nullptr;
}
m_mutex.unlock();
}

void PainterGL::setVideoProxy(std::shared_ptr<VideoProxy> proxy) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/qt/DisplayGL.h
Expand Up @@ -136,7 +136,7 @@ public slots:
void makeCurrent();
void performDraw();
void dequeue();
void dequeueAll();
void dequeueAll(bool keep = false);

std::array<std::array<uint32_t, 0x100000>, 3> m_buffers;
QList<uint32_t*> m_free;
Expand Down

0 comments on commit fdbb442

Please sign in to comment.