platform/windows: change dwmflush default, add autodetection

On each re/init, query the active monitor refresh rate via
DwmGetCompositionTimingInfo. If the client requested framerate exceeds
the host monitor refresh, automatically disable DwmFlush.

This avoids the problem by which DwmFlush would constrain the client
FPS if the host monitor runs at a lower refresh rate, thus allowing
the feature to be enabled by default.

If there are other issues caused by DwmFlush for certain systems,
it can still be disabled via configuration.
This commit is contained in:
Conn O'Griofa 2022-05-20 16:25:09 +01:00
parent 211b25848f
commit 2d969c2ccc
5 changed files with 29 additions and 11 deletions

View File

@ -339,11 +339,12 @@ dwmflush
Description
Invoke DwmFlush() to sync screen capture to the Windows presentation interval.
.. Caution:: Applies to Windows only. Alleviates visual stuttering during mouse movement, but causes the capture
rate to be limited to the host monitor's currently active refresh rate.
.. Caution:: Applies to Windows only. Alleviates visual stuttering during mouse movement.
If enabled, this feature will automatically deactivate if the client framerate exceeds
the host monitor's current refresh rate.
Default
``disabled``
``enabled``
Examples

View File

@ -410,8 +410,8 @@
<option value="enabled">Enabled</option>
</select>
<div class="form-text">
Improves capture latency during mouse movement.<br />
Enabling this may prevent the client's FPS from exceeding the host monitor's active refresh rate.
Improves capture latency/smoothness during mouse movement.<br />
Disable if you encounter any VSync-related issues.
</div>
</div>
<div class="mb-3" class="config-page" v-if="platform === 'linux'">
@ -843,7 +843,7 @@
this.config.key_rightalt_to_key_win || "disabled";
this.config.gamepad = this.config.gamepad || "x360";
this.config.upnp = this.config.upnp || "disabled";
this.config.dwmflush = this.config.dwmflush || "disabled";
this.config.dwmflush = this.config.dwmflush || "enabled";
this.config.min_log_level = this.config.min_log_level || 2;
this.config.origin_pin_allowed =
this.config.origin_pin_allowed || "pc";

View File

@ -226,10 +226,10 @@ video_t video {
1,
-1 }, // vt
{}, // encoder
{}, // adapter_name
{}, // output_name
false // dwmflush
{}, // encoder
{}, // adapter_name
{}, // output_name
true // dwmflush
};
audio_t audio {};

View File

@ -96,6 +96,7 @@ class duplication_t {
public:
dup_t dup;
bool has_frame {};
bool use_dwmflush {};
capture_e next_frame(DXGI_OUTDUPL_FRAME_INFO &frame_info, std::chrono::milliseconds timeout, resource_t::pointer *res_p);
capture_e reset(dup_t::pointer dup_p = dup_t::pointer());

View File

@ -2,6 +2,7 @@
// Created by loki on 1/12/20.
//
#include <cmath>
#include <codecvt>
#include "display.h"
@ -20,7 +21,7 @@ capture_e duplication_t::next_frame(DXGI_OUTDUPL_FRAME_INFO &frame_info, std::ch
return capture_status;
}
if(config::video.dwmflush) {
if(use_dwmflush) {
DwmFlush();
}
@ -217,6 +218,21 @@ int display_base_t::init(int framerate, const std::string &display_name) {
<< "Offset : "sv << offset_x << 'x' << offset_y << std::endl
<< "Virtual Desktop : "sv << env_width << 'x' << env_height;
// Enable DwmFlush() only if the current refresh rate can match the client framerate.
auto refresh_rate = framerate;
DWM_TIMING_INFO timing_info;
timing_info.cbSize = sizeof(timing_info);
status = DwmGetCompositionTimingInfo(NULL, &timing_info);
if(FAILED(status)) {
BOOST_LOG(warning) << "Failed to detect active refresh rate.";
}
else {
refresh_rate = std::round((double)timing_info.rateRefresh.uiNumerator / (double)timing_info.rateRefresh.uiDenominator);
}
dup.use_dwmflush = config::video.dwmflush && !(framerate > refresh_rate) ? true : false;
// Bump up thread priority
{
const DWORD flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;