From fb670d209b3a80b3a3d61d1edce0e2fd5ae89b2c Mon Sep 17 00:00:00 2001 From: attila Date: Mon, 22 Jul 2024 17:18:30 +0200 Subject: [PATCH] Windows: Throttle VBlankThread when the display is off When the display goes to sleep IDXGIOutput::WaitForVBlank returns S_OK immediately causing the VBlankThread to consume a core entirely with the VBlank related messaging. To limit this problem, we use the same technique as Chromium presently does, and we sleep for 1 ms, if the time between the last two VBlank events was less than a ms. This limits the VBlankThread messaging rate of about 50.000/s on an Intel 13600 to 1000/s. --- modules/juce_gui_basics/native/juce_VBlank_windows.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/juce_gui_basics/native/juce_VBlank_windows.cpp b/modules/juce_gui_basics/native/juce_VBlank_windows.cpp index b5af4c0415..1e21b16d86 100644 --- a/modules/juce_gui_basics/native/juce_VBlank_windows.cpp +++ b/modules/juce_gui_basics/native/juce_VBlank_windows.cpp @@ -121,6 +121,12 @@ private: { if (output->WaitForVBlank() == S_OK) { + if (const auto now = Time::getMillisecondCounterHiRes(); + now - std::exchange (lastVBlankEvent, now) < 1.0) + { + Thread::sleep (1); + } + std::unique_lock lock { mutex }; condvar.wait (lock, [this] { return threadState != ThreadState::sleep; }); @@ -164,6 +170,7 @@ private: exit, }; + double lastVBlankEvent = 0.0; ThreadState threadState = ThreadState::paint; std::condition_variable condvar; std::mutex mutex;