Timer: Improve SleepUntil()

This commit is contained in:
Connor McLaughlin 2022-12-04 15:40:46 +10:00
parent 13f7672a78
commit f8cb480779
2 changed files with 13 additions and 8 deletions

View File

@ -1,4 +1,5 @@
#include "timer.h"
#include "types.h"
#include <cstdio>
#include <cstdlib>
@ -109,20 +110,19 @@ void Timer::SleepUntil(Value value, bool exact)
}
else
{
const std::int64_t diff = static_cast<std::int64_t>(value - GetCurrentValue());
const s64 diff = static_cast<s64>(value - GetCurrentValue());
if (diff <= 0)
return;
HANDLE timer = GetSleepTimer();
if (timer)
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
const u64 one_hundred_nanos_diff = static_cast<u64>(ConvertValueToNanoseconds(diff) / 100.0);
if (one_hundred_nanos_diff == 0)
return;
LARGE_INTEGER fti;
fti.LowPart = ft.dwLowDateTime;
fti.HighPart = ft.dwHighDateTime;
fti.QuadPart += diff;
fti.QuadPart = -static_cast<s64>(one_hundred_nanos_diff);
if (SetWaitableTimer(timer, &fti, 0, nullptr, nullptr, FALSE))
{
@ -132,7 +132,7 @@ void Timer::SleepUntil(Value value, bool exact)
}
// falling back to sleep... bad.
Sleep(static_cast<DWORD>(static_cast<std::uint64_t>(diff) / 1000000));
Sleep(static_cast<DWORD>(static_cast<u64>(diff) / 1000000));
}
}

View File

@ -124,12 +124,17 @@ void HostDisplay::ThrottlePresentation()
const u64 sleep_period = Common::Timer::ConvertNanosecondsToValue(1e+9f / static_cast<double>(throttle_rate));
const u64 current_ts = Common::Timer::GetCurrentValue();
if (current_ts >= m_last_frame_displayed_time)
// Allow it to fall behind/run ahead up to 2*period. Sleep isn't that precise, plus we need to
// allow time for the actual rendering.
const u64 max_variance = sleep_period * 2;
if (static_cast<u64>(std::abs(static_cast<s64>(current_ts - m_last_frame_displayed_time))) > max_variance)
m_last_frame_displayed_time = current_ts + sleep_period;
else
m_last_frame_displayed_time += sleep_period;
Common::Timer tt;
Common::Timer::SleepUntil(m_last_frame_displayed_time, false);
Log_WarningPrintf("sleep time %.2f ms", tt.GetTimeMilliseconds());
}
bool HostDisplay::GetHostRefreshRate(float* refresh_rate)