From ce2c7a7d018d4bcd8496a764d6d3df695fb484f5 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Sun, 29 Oct 2023 23:46:18 +0200 Subject: [PATCH] restored previous timer class --- CMakeLists.txt | 2 + .../PS4/HLE/Kernel/Objects/event_queue.cpp | 13 ++- src/Lib/Timer.cpp | 103 ++++++++++++++++++ src/Lib/Timer.h | 34 ++++++ 4 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 src/Lib/Timer.cpp create mode 100644 src/Lib/Timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a81530a3..9b7465f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,8 @@ add_executable(shadps4 ${FILESYSTEM_SOURCES} ${HOST_SOURCES} ${UTIL_SOURCES} + src/Lib/Timer.cpp + src/Lib/Timer.h src/main.cpp src/types.h src/Core/FsFile.cpp diff --git a/src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp b/src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp index d1a68cc1..521cd432 100644 --- a/src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp +++ b/src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp @@ -1,7 +1,8 @@ #include "event_queue.h" -#include "debug.h" -#include +#include + +#include "debug.h" namespace HLE::Kernel::Objects { EqueueInternal::~EqueueInternal() {} @@ -25,8 +26,9 @@ int EqueueInternal::addEvent(const EqueueEvent& event) { int EqueueInternal::waitForEvents(SceKernelEvent* ev, int num, u32 micros) { std::unique_lock lock{m_mutex}; - u64 timeElapsed = 0; - const auto start = std::chrono::high_resolution_clock::now(); + u32 timeElapsed = 0; + Lib::Timer t; + t.Start(); for (;;) { int ret = getTriggeredEvents(ev, num); @@ -41,8 +43,7 @@ int EqueueInternal::waitForEvents(SceKernelEvent* ev, int num, u32 micros) { m_cond.wait_for(lock, std::chrono::microseconds(micros - timeElapsed)); } - const auto end = std::chrono::high_resolution_clock::now(); - timeElapsed = std::chrono::duration_cast(end - start).count(); + timeElapsed = static_cast(t.GetTimeSec() * 1000000.0); } return 0; diff --git a/src/Lib/Timer.cpp b/src/Lib/Timer.cpp new file mode 100644 index 00000000..3dd1d251 --- /dev/null +++ b/src/Lib/Timer.cpp @@ -0,0 +1,103 @@ +#include "Timer.h" + +#ifdef _WIN64 +#include +#endif + +Lib::Timer::Timer() { +#ifdef _WIN64 + LARGE_INTEGER f; + QueryPerformanceFrequency(&f); + m_Frequency = f.QuadPart; +#else +#error Unimplemented Timer constructor +#endif +} + +void Lib::Timer::Start() { +#ifdef _WIN64 + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + m_StartTime = c.QuadPart; +#else +#error Unimplemented Timer::Start() +#endif + m_is_timer_paused = false; +} + +void Lib::Timer::Pause() { +#ifdef _WIN64 + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + m_PauseTime = c.QuadPart; +#else +#error Unimplemented Timer::Pause() +#endif + m_is_timer_paused = true; +} + +void Lib::Timer::Resume() { + u64 current_time = 0; +#ifdef _WIN64 + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + current_time = c.QuadPart; +#else +#error Unimplemented Timer::Resume() +#endif + m_StartTime += current_time - m_PauseTime; + m_is_timer_paused = false; +} + +bool Lib::Timer::IsPaused() const { return m_is_timer_paused; } + +double Lib::Timer::GetTimeMsec() const { + if (m_is_timer_paused) { + return 1000.0 * (static_cast(m_PauseTime - m_StartTime)) / static_cast(m_Frequency); + } + + u64 current_time = 0; +#ifdef _WIN64 + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + current_time = c.QuadPart; +#else +#error Unimplemented Timer::GetTimeMsec() +#endif + return 1000.0 * (static_cast(current_time - m_StartTime)) / static_cast(m_Frequency); +} + +double Lib::Timer::GetTimeSec() const { + if (m_is_timer_paused) { + return (static_cast(m_PauseTime - m_StartTime)) / static_cast(m_Frequency); + } + + u64 current_time = 0; +#ifdef _WIN64 + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + current_time = c.QuadPart; +#else +#error Unimplemented Timer::GetTimeSec() +#endif + return (static_cast(current_time - m_StartTime)) / static_cast(m_Frequency); +} + +u64 Lib::Timer::GetTicks() const { + if (m_is_timer_paused) { + return (m_PauseTime - m_StartTime); + } + + u64 current_time = 0; +#ifdef _WIN64 + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + current_time = c.QuadPart; +#else +#error Unimplemented Timer::GetTicks() +#endif + return (current_time - m_StartTime); +} + +u64 Lib::Timer::GetFrequency() const { return m_Frequency; } + diff --git a/src/Lib/Timer.h b/src/Lib/Timer.h new file mode 100644 index 00000000..cdb0cc42 --- /dev/null +++ b/src/Lib/Timer.h @@ -0,0 +1,34 @@ +#pragma once + +#include "../types.h" + +namespace Lib { + class Timer final + { + public: + Timer(); + ~Timer() = default; + + void Start(); + void Pause(); + void Resume(); + bool IsPaused() const; + + double GetTimeMsec() const;// return time in milliseconds + double GetTimeSec() const;// return time in seconds + u64 GetTicks() const;// return time in ticks + u64 GetFrequency() const;// return ticks frequency + + public: + Timer(const Timer&) = delete; + Timer& operator=(const Timer&) = delete; + Timer(Timer&&) = delete; + Timer& operator=(Timer&&) = delete; + + private: + bool m_is_timer_paused = true; + u64 m_Frequency = 0; + u64 m_StartTime = 0; + u64 m_PauseTime = 0; + }; +} \ No newline at end of file