From c67a4181bbf21623a9d70b79695c9a1d1f293e9e Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 23 Dec 2021 21:44:55 -0500 Subject: [PATCH] Tweak framebuffer render-skipping algorithm to be slightly more aggressive to render after frame deadlines. --- include/window/Window.hpp | 1 + src/widget/FramebufferWidget.cpp | 11 ++++++++--- src/window/Window.cpp | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/window/Window.hpp b/include/window/Window.hpp index f22d9dc1..4f2f93ba 100644 --- a/include/window/Window.hpp +++ b/include/window/Window.hpp @@ -103,6 +103,7 @@ struct Window { } PRIVATE bool& fbDirtyOnSubpixelChange(); + PRIVATE int& fbCount(); }; diff --git a/src/widget/FramebufferWidget.cpp b/src/widget/FramebufferWidget.cpp index 7f996d2c..7f093e34 100644 --- a/src/widget/FramebufferWidget.cpp +++ b/src/widget/FramebufferWidget.cpp @@ -120,9 +120,14 @@ void FramebufferWidget::draw(const DrawArgs& args) { setDirty(); } - // It's more important to not lag the frame than to draw the framebuffer - if (dirty && APP->window->getFrameDurationRemaining() > 0.0) { - render(scale, offsetF, args.clipBox); + if (dirty) { + // Render only if there is frame time remaining (to avoid lagging frames significantly), or if it's one of the first framebuffers this frame (to avoid framebuffers from never rendering). + int count = ++APP->window->fbCount(); + const int maxCount = 4; + const float minRemaining = -1 / 60.0; + if (count <= maxCount || APP->window->getFrameDurationRemaining() > minRemaining) { + render(scale, offsetF, args.clipBox); + } } if (!internal->fb) diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 1c8b185d..30d0162e 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -100,6 +100,7 @@ struct Window::Internal { std::map> imageCache; bool fbDirtyOnSubpixelChange = true; + int fbCount = 0; }; @@ -415,6 +416,7 @@ void Window::step() { double lastFrameTime = internal->frameTime; internal->frameTime = frameTime; internal->lastFrameDuration = frameTime - lastFrameTime; + internal->fbCount = 0; // DEBUG("%.2lf Hz", 1.0 / internal->lastFrameDuration); // double t1 = 0.0, t2 = 0.0, t3 = 0.0, t4 = 0.0, t5 = 0.0; @@ -759,6 +761,11 @@ bool& Window::fbDirtyOnSubpixelChange() { } +int& Window::fbCount() { + return internal->fbCount; +} + + void init() { int err;