From 730fce4b1ce494f28449e84f7783837f44b21b6b Mon Sep 17 00:00:00 2001 From: ns6089 <61738816+ns6089@users.noreply.github.com> Date: Fri, 12 May 2023 17:07:22 +0300 Subject: [PATCH] Move client frame interval to local variable --- src/platform/windows/display.h | 6 ++++-- src/platform/windows/display_base.cpp | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/platform/windows/display.h b/src/platform/windows/display.h index 238a87c2..5c77aa0d 100644 --- a/src/platform/windows/display.h +++ b/src/platform/windows/display.h @@ -132,14 +132,16 @@ namespace platf::dxgi { capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) override; - std::chrono::nanoseconds delay; - factory1_t factory; adapter_t adapter; output_t output; device_t device; device_ctx_t device_ctx; duplication_t dup; + DXGI_RATIONAL display_refresh_rate; + int display_refresh_rate_rounded; + + int client_frame_rate; DXGI_FORMAT capture_format; D3D_FEATURE_LEVEL feature_level; diff --git a/src/platform/windows/display_base.cpp b/src/platform/windows/display_base.cpp index c016514a..4382ef0a 100644 --- a/src/platform/windows/display_base.cpp +++ b/src/platform/windows/display_base.cpp @@ -119,6 +119,7 @@ namespace platf::dxgi { capture_e display_base_t::capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor) { auto next_frame = std::chrono::steady_clock::now(); + const auto client_frame_interval = std::chrono::nanoseconds { 1s } / client_frame_rate; // Keep the display awake during capture. If the display goes to sleep during // capture, best case is that capture stops until it powers back on. However, @@ -142,14 +143,14 @@ namespace platf::dxgi { auto wait_time = next_frame - std::chrono::steady_clock::now(); if (wait_time > 0s && wait_time < 1s) { high_precision_sleep(wait_time); - next_frame += delay; + next_frame += client_frame_interval; } else { // If the wait time is negative (meaning the frame is past due) or the // computed wait time is beyond a second (meaning possible clock issues), // just capture the frame now and resynchronize the frame interval with // the current time. - next_frame = std::chrono::steady_clock::now() + delay; + next_frame = std::chrono::steady_clock::now() + client_frame_interval; } std::shared_ptr img_out; @@ -334,8 +335,6 @@ namespace platf::dxgi { // Ensure we can duplicate the current display syncThreadDesktop(); - delay = std::chrono::nanoseconds { 1s } / config.framerate; - // Get rectangle of full desktop for absolute mouse coordinates env_width = GetSystemMetrics(SM_CXVIRTUALSCREEN); env_height = GetSystemMetrics(SM_CYVIRTUALSCREEN); @@ -595,6 +594,13 @@ namespace platf::dxgi { BOOST_LOG(info) << "Desktop resolution ["sv << dup_desc.ModeDesc.Width << 'x' << dup_desc.ModeDesc.Height << ']'; BOOST_LOG(info) << "Desktop format ["sv << dxgi_format_to_string(dup_desc.ModeDesc.Format) << ']'; + display_refresh_rate = dup_desc.ModeDesc.RefreshRate; + display_refresh_rate_rounded = lround((double) display_refresh_rate.Numerator / display_refresh_rate.Denominator); + BOOST_LOG(info) << "Display refresh rate [" << display_refresh_rate_rounded << "Hz]"; + + client_frame_rate = config.framerate; + BOOST_LOG(info) << "Requested frame rate [" << client_frame_rate << "fps]"; + dxgi::output6_t output6 {}; status = output->QueryInterface(IID_IDXGIOutput6, (void **) &output6); if (SUCCEEDED(status)) {