From 8997eda005d1e1d289aa07c16fddd1edb4d7b40d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 13 Jul 2022 19:27:38 -0700 Subject: [PATCH] Revert "Qt: Improve frame pacing, maybe" This reverts commit 76d6055bb0095caf57431f7250bc0e965aa49e5e. --- src/core/sync.c | 4 +-- src/platform/qt/DisplayGL.cpp | 48 ++++++++++++----------------------- src/platform/qt/DisplayGL.h | 4 +-- 3 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/core/sync.c b/src/core/sync.c index 78b04ea3e..b2a65493b 100644 --- a/src/core/sync.c +++ b/src/core/sync.c @@ -25,10 +25,10 @@ void mCoreSyncPostFrame(struct mCoreSync* sync) { MutexLock(&sync->videoFrameMutex); ++sync->videoFramePending; do { + ConditionWake(&sync->videoFrameAvailableCond); if (sync->videoFrameWait) { ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex); } - ConditionWake(&sync->videoFrameAvailableCond); } while (sync->videoFrameWait && sync->videoFramePending); MutexUnlock(&sync->videoFrameMutex); } @@ -54,7 +54,7 @@ bool mCoreSyncWaitFrameStart(struct mCoreSync* sync) { } if (sync->videoFrameWait) { ConditionWake(&sync->videoFrameRequiredCond); - if (ConditionWaitTimed(&sync->videoFrameAvailableCond, &sync->videoFrameMutex, 4)) { + if (ConditionWaitTimed(&sync->videoFrameAvailableCond, &sync->videoFrameMutex, 50)) { return false; } } diff --git a/src/platform/qt/DisplayGL.cpp b/src/platform/qt/DisplayGL.cpp index 4175505b5..740604b82 100644 --- a/src/platform/qt/DisplayGL.cpp +++ b/src/platform/qt/DisplayGL.cpp @@ -619,12 +619,11 @@ void PainterGL::start() { m_buffer = nullptr; m_active = true; m_started = true; - resetTiming(); emit started(); } void PainterGL::draw() { - if (!m_started) { + if (!m_started || m_queue.isEmpty()) { return; } @@ -637,16 +636,14 @@ void PainterGL::draw() { } mCoreSync* sync = &m_context->thread()->impl->sync; - - qint64 targetNsec = 1000000000 / sync->fpsTarget; - qint64 refreshNsec = 1000000000 / m_window->screen()->refreshRate(); - qint64 delay = m_timerResidue; - if (!mCoreSyncWaitFrameStart(sync)) { mCoreSyncWaitFrameEnd(sync); if (!sync->audioWait && !sync->videoFrameWait) { return; } + if (m_delayTimer.elapsed() >= 1000 / m_window->screen()->refreshRate()) { + return; + } if (!m_drawTimer.isActive()) { m_drawTimer.start(1); } @@ -654,30 +651,23 @@ void PainterGL::draw() { } dequeue(); bool forceRedraw = !m_videoProxy; - if (sync->audioWait || sync->videoFrameWait) { - while (delay + m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC < targetNsec) { - QThread::usleep(200); + if (!m_delayTimer.isValid()) { + m_delayTimer.start(); + } else { + if (sync->audioWait || sync->videoFrameWait) { + while (m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC < 1000000000 / sync->fpsTarget) { + QThread::usleep(500); + } + forceRedraw = sync->videoFrameWait; + } + if (!forceRedraw) { + forceRedraw = m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC >= 1000000000 / m_window->screen()->refreshRate(); } - forceRedraw = sync->videoFrameWait; - } - if (!forceRedraw) { - forceRedraw = delay + m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC >= refreshNsec; } mCoreSyncWaitFrameEnd(sync); if (forceRedraw) { - delay += m_delayTimer.nsecsElapsed(); m_delayTimer.restart(); - - delay -= targetNsec; - m_timerResidue = (m_timerResidue + delay) / 2; - - if (m_timerResidue > refreshNsec) { - if (!m_drawTimer.isActive()) { - m_drawTimer.start(1); - } - } - performDraw(); m_backend->swap(m_backend); } @@ -689,16 +679,11 @@ void PainterGL::forceDraw() { if (m_delayTimer.elapsed() < 1000 / m_window->screen()->refreshRate()) { return; } - resetTiming(); + m_delayTimer.restart(); } m_backend->swap(m_backend); } -void PainterGL::resetTiming() { - m_delayTimer.restart(); - m_timerResidue = 0; -} - void PainterGL::stop() { m_started = false; QMetaObject::invokeMethod(this, "doStop", Qt::BlockingQueuedConnection); @@ -736,7 +721,6 @@ void PainterGL::pause() { void PainterGL::unpause() { m_active = true; - resetTiming(); } void PainterGL::performDraw() { diff --git a/src/platform/qt/DisplayGL.h b/src/platform/qt/DisplayGL.h index 5b12feae0..47f94157d 100644 --- a/src/platform/qt/DisplayGL.h +++ b/src/platform/qt/DisplayGL.h @@ -176,9 +176,8 @@ private: void performDraw(); void dequeue(); void dequeueAll(bool keep = false); - void resetTiming(); - std::array, 8> m_buffers; + std::array, 3> m_buffers; QList m_free; QQueue m_queue; uint32_t* m_buffer = nullptr; @@ -195,7 +194,6 @@ private: bool m_active = false; bool m_started = false; QTimer m_drawTimer; - qint64 m_timerResidue; std::shared_ptr m_context; CoreController::Interrupter m_interrupter; bool m_supportsShaders;