Revert "Qt: Improve frame pacing, maybe"

This reverts commit 76d6055bb0.
This commit is contained in:
Vicki Pfau 2022-07-13 19:27:38 -07:00
parent ff95aab0b9
commit 8997eda005
3 changed files with 19 additions and 37 deletions

View File

@ -25,10 +25,10 @@ void mCoreSyncPostFrame(struct mCoreSync* sync) {
MutexLock(&sync->videoFrameMutex); MutexLock(&sync->videoFrameMutex);
++sync->videoFramePending; ++sync->videoFramePending;
do { do {
ConditionWake(&sync->videoFrameAvailableCond);
if (sync->videoFrameWait) { if (sync->videoFrameWait) {
ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex); ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
} }
ConditionWake(&sync->videoFrameAvailableCond);
} while (sync->videoFrameWait && sync->videoFramePending); } while (sync->videoFrameWait && sync->videoFramePending);
MutexUnlock(&sync->videoFrameMutex); MutexUnlock(&sync->videoFrameMutex);
} }
@ -54,7 +54,7 @@ bool mCoreSyncWaitFrameStart(struct mCoreSync* sync) {
} }
if (sync->videoFrameWait) { if (sync->videoFrameWait) {
ConditionWake(&sync->videoFrameRequiredCond); ConditionWake(&sync->videoFrameRequiredCond);
if (ConditionWaitTimed(&sync->videoFrameAvailableCond, &sync->videoFrameMutex, 4)) { if (ConditionWaitTimed(&sync->videoFrameAvailableCond, &sync->videoFrameMutex, 50)) {
return false; return false;
} }
} }

View File

@ -619,12 +619,11 @@ void PainterGL::start() {
m_buffer = nullptr; m_buffer = nullptr;
m_active = true; m_active = true;
m_started = true; m_started = true;
resetTiming();
emit started(); emit started();
} }
void PainterGL::draw() { void PainterGL::draw() {
if (!m_started) { if (!m_started || m_queue.isEmpty()) {
return; return;
} }
@ -637,16 +636,14 @@ void PainterGL::draw() {
} }
mCoreSync* sync = &m_context->thread()->impl->sync; 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)) { if (!mCoreSyncWaitFrameStart(sync)) {
mCoreSyncWaitFrameEnd(sync); mCoreSyncWaitFrameEnd(sync);
if (!sync->audioWait && !sync->videoFrameWait) { if (!sync->audioWait && !sync->videoFrameWait) {
return; return;
} }
if (m_delayTimer.elapsed() >= 1000 / m_window->screen()->refreshRate()) {
return;
}
if (!m_drawTimer.isActive()) { if (!m_drawTimer.isActive()) {
m_drawTimer.start(1); m_drawTimer.start(1);
} }
@ -654,30 +651,23 @@ void PainterGL::draw() {
} }
dequeue(); dequeue();
bool forceRedraw = !m_videoProxy; bool forceRedraw = !m_videoProxy;
if (!m_delayTimer.isValid()) {
m_delayTimer.start();
} else {
if (sync->audioWait || sync->videoFrameWait) { if (sync->audioWait || sync->videoFrameWait) {
while (delay + m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC < targetNsec) { while (m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC < 1000000000 / sync->fpsTarget) {
QThread::usleep(200); QThread::usleep(500);
} }
forceRedraw = sync->videoFrameWait; forceRedraw = sync->videoFrameWait;
} }
if (!forceRedraw) { if (!forceRedraw) {
forceRedraw = delay + m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC >= refreshNsec; forceRedraw = m_delayTimer.nsecsElapsed() + OVERHEAD_NSEC >= 1000000000 / m_window->screen()->refreshRate();
}
} }
mCoreSyncWaitFrameEnd(sync); mCoreSyncWaitFrameEnd(sync);
if (forceRedraw) { if (forceRedraw) {
delay += m_delayTimer.nsecsElapsed();
m_delayTimer.restart(); m_delayTimer.restart();
delay -= targetNsec;
m_timerResidue = (m_timerResidue + delay) / 2;
if (m_timerResidue > refreshNsec) {
if (!m_drawTimer.isActive()) {
m_drawTimer.start(1);
}
}
performDraw(); performDraw();
m_backend->swap(m_backend); m_backend->swap(m_backend);
} }
@ -689,16 +679,11 @@ void PainterGL::forceDraw() {
if (m_delayTimer.elapsed() < 1000 / m_window->screen()->refreshRate()) { if (m_delayTimer.elapsed() < 1000 / m_window->screen()->refreshRate()) {
return; return;
} }
resetTiming(); m_delayTimer.restart();
} }
m_backend->swap(m_backend); m_backend->swap(m_backend);
} }
void PainterGL::resetTiming() {
m_delayTimer.restart();
m_timerResidue = 0;
}
void PainterGL::stop() { void PainterGL::stop() {
m_started = false; m_started = false;
QMetaObject::invokeMethod(this, "doStop", Qt::BlockingQueuedConnection); QMetaObject::invokeMethod(this, "doStop", Qt::BlockingQueuedConnection);
@ -736,7 +721,6 @@ void PainterGL::pause() {
void PainterGL::unpause() { void PainterGL::unpause() {
m_active = true; m_active = true;
resetTiming();
} }
void PainterGL::performDraw() { void PainterGL::performDraw() {

View File

@ -176,9 +176,8 @@ private:
void performDraw(); void performDraw();
void dequeue(); void dequeue();
void dequeueAll(bool keep = false); void dequeueAll(bool keep = false);
void resetTiming();
std::array<std::array<uint32_t, 0x100000>, 8> m_buffers; std::array<std::array<uint32_t, 0x100000>, 3> m_buffers;
QList<uint32_t*> m_free; QList<uint32_t*> m_free;
QQueue<uint32_t*> m_queue; QQueue<uint32_t*> m_queue;
uint32_t* m_buffer = nullptr; uint32_t* m_buffer = nullptr;
@ -195,7 +194,6 @@ private:
bool m_active = false; bool m_active = false;
bool m_started = false; bool m_started = false;
QTimer m_drawTimer; QTimer m_drawTimer;
qint64 m_timerResidue;
std::shared_ptr<CoreController> m_context; std::shared_ptr<CoreController> m_context;
CoreController::Interrupter m_interrupter; CoreController::Interrupter m_interrupter;
bool m_supportsShaders; bool m_supportsShaders;