From 099d01f96c1d5fcc4d2f8473d2ed5225301bc93e Mon Sep 17 00:00:00 2001 From: GPUCode Date: Mon, 6 Nov 2023 01:17:28 +0200 Subject: [PATCH] common: Move timer to common --- CMakeLists.txt | 4 +- src/Lib/Timer.h | 34 --------------- src/{Lib/Timer.cpp => common/timer.cpp} | 25 ++++++----- src/common/timer.h | 43 +++++++++++++++++++ src/core/hle/kernel/Objects/event_queue.cpp | 4 +- .../libraries/libkernel/time_management.cpp | 4 +- src/emuTimer.cpp | 23 +++++++--- src/emulator.cpp | 16 +++---- 8 files changed, 84 insertions(+), 69 deletions(-) delete mode 100644 src/Lib/Timer.h rename src/{Lib/Timer.cpp => common/timer.cpp} (83%) create mode 100644 src/common/timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 28e521b9..a4937618 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,8 @@ add_executable(shadps4 src/common/singleton.h src/common/string_util.cpp src/common/string_util.h + src/common/timer.cpp + src/common/timer.h src/common/types.h src/common/version.h ${LIBC_SOURCES} @@ -86,8 +88,6 @@ add_executable(shadps4 ${SYSTEMSERVICE_SOURCES} ${FILESYSTEM_SOURCES} ${HOST_SOURCES} - src/Lib/Timer.cpp - src/Lib/Timer.h src/main.cpp src/core/loader/elf.cpp src/core/loader/elf.h diff --git a/src/Lib/Timer.h b/src/Lib/Timer.h deleted file mode 100644 index 5ba2a09a..00000000 --- a/src/Lib/Timer.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "common/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 - [[nodiscard]] static u64 getQueryPerformanceCounter(); - 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 diff --git a/src/Lib/Timer.cpp b/src/common/timer.cpp similarity index 83% rename from src/Lib/Timer.cpp rename to src/common/timer.cpp index 74392fb5..fa3d172b 100644 --- a/src/Lib/Timer.cpp +++ b/src/common/timer.cpp @@ -1,10 +1,12 @@ -#include "Timer.h" +#include "common/timer.h" #ifdef _WIN64 #include #endif -Lib::Timer::Timer() { +namespace Common { + +Timer::Timer() { #ifdef _WIN64 LARGE_INTEGER f; QueryPerformanceFrequency(&f); @@ -14,7 +16,7 @@ Lib::Timer::Timer() { #endif } -void Lib::Timer::Start() { +void Timer::Start() { #ifdef _WIN64 LARGE_INTEGER c; QueryPerformanceCounter(&c); @@ -25,7 +27,7 @@ void Lib::Timer::Start() { m_is_timer_paused = false; } -void Lib::Timer::Pause() { +void Timer::Pause() { #ifdef _WIN64 LARGE_INTEGER c; QueryPerformanceCounter(&c); @@ -36,7 +38,7 @@ void Lib::Timer::Pause() { m_is_timer_paused = true; } -void Lib::Timer::Resume() { +void Timer::Resume() { u64 current_time = 0; #ifdef _WIN64 LARGE_INTEGER c; @@ -49,9 +51,7 @@ void Lib::Timer::Resume() { m_is_timer_paused = false; } -bool Lib::Timer::IsPaused() const { return m_is_timer_paused; } - -double Lib::Timer::GetTimeMsec() const { +double Timer::GetTimeMsec() const { if (m_is_timer_paused) { return 1000.0 * (static_cast(m_PauseTime - m_StartTime)) / static_cast(m_Frequency); } @@ -67,7 +67,7 @@ double Lib::Timer::GetTimeMsec() const { return 1000.0 * (static_cast(current_time - m_StartTime)) / static_cast(m_Frequency); } -double Lib::Timer::GetTimeSec() const { +double Timer::GetTimeSec() const { if (m_is_timer_paused) { return (static_cast(m_PauseTime - m_StartTime)) / static_cast(m_Frequency); } @@ -83,7 +83,7 @@ double Lib::Timer::GetTimeSec() const { return (static_cast(current_time - m_StartTime)) / static_cast(m_Frequency); } -u64 Lib::Timer::GetTicks() const { +u64 Timer::GetTicks() const { if (m_is_timer_paused) { return (m_PauseTime - m_StartTime); } @@ -99,11 +99,10 @@ u64 Lib::Timer::GetTicks() const { return (current_time - m_StartTime); } -u64 Lib::Timer::GetFrequency() const { return m_Frequency; } - -u64 Lib::Timer::getQueryPerformanceCounter() { +u64 Timer::getQueryPerformanceCounter() { LARGE_INTEGER c; QueryPerformanceCounter(&c); return c.QuadPart; } +} // namespace Common diff --git a/src/common/timer.h b/src/common/timer.h new file mode 100644 index 00000000..e390e7e6 --- /dev/null +++ b/src/common/timer.h @@ -0,0 +1,43 @@ +#pragma once + +#include "common/types.h" + +namespace Common { + +class Timer final { +public: + Timer(); + ~Timer() = default; + + void Start(); + void Pause(); + void Resume(); + + bool IsPaused() const { + return m_is_timer_paused; + } + + u64 GetFrequency() const { + return m_Frequency; + } + + double GetTimeMsec() const; + double GetTimeSec() const; + u64 GetTicks() const; + + [[nodiscard]] static u64 getQueryPerformanceCounter(); + +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{}; + u64 m_StartTime{}; + u64 m_PauseTime{}; +}; + +} // namespace Common diff --git a/src/core/hle/kernel/Objects/event_queue.cpp b/src/core/hle/kernel/Objects/event_queue.cpp index 1a84e06d..1ca2d6f1 100644 --- a/src/core/hle/kernel/Objects/event_queue.cpp +++ b/src/core/hle/kernel/Objects/event_queue.cpp @@ -1,6 +1,6 @@ #include "common/debug.h" +#include "common/timer.h" #include "core/hle/kernel/objects/event_queue.h" -#include "Lib/Timer.h" namespace Core::Kernel { @@ -26,7 +26,7 @@ int EqueueInternal::waitForEvents(SceKernelEvent* ev, int num, u32 micros) { std::unique_lock lock{m_mutex}; u32 timeElapsed = 0; - Lib::Timer t; + Common::Timer t; t.Start(); for (;;) { diff --git a/src/core/hle/libraries/libkernel/time_management.cpp b/src/core/hle/libraries/libkernel/time_management.cpp index bb062b71..a1b40ce8 100644 --- a/src/core/hle/libraries/libkernel/time_management.cpp +++ b/src/core/hle/libraries/libkernel/time_management.cpp @@ -1,6 +1,6 @@ +#include "common/timer.h" #include "core/hle/libraries/libkernel/time_management.h" #include "core/hle/libraries/libs.h" -#include "Lib/Timer.h" #include "emuTimer.h" namespace Core::Libraries::LibKernel { @@ -18,7 +18,7 @@ u64 PS4_SYSV_ABI sceKernelGetProcessTimeCounterFrequency() { } u64 PS4_SYSV_ABI sceKernelReadTsc() { - return Lib::Timer::getQueryPerformanceCounter(); + return Common::Timer::getQueryPerformanceCounter(); } void timeSymbolsRegister(Loader::SymbolsResolver* sym) { diff --git a/src/emuTimer.cpp b/src/emuTimer.cpp index c5d2700c..b05c001b 100644 --- a/src/emuTimer.cpp +++ b/src/emuTimer.cpp @@ -1,14 +1,23 @@ -#include "Lib/Timer.h" +#include "common/timer.h" namespace Emulator::emuTimer { -static Lib::Timer timer; -void start() { timer.Start(); } +static Common::Timer timer; -double getTimeMsec() { return timer.GetTimeMsec(); } +void start() { + timer.Start(); +} -u64 getTimeCounter() { return timer.GetTicks(); } +double getTimeMsec() { + return timer.GetTimeMsec(); +} -u64 getTimeFrequency() { return timer.GetFrequency(); } +u64 getTimeCounter() { + return timer.GetTicks(); +} -} // namespace Emulator::emuTimer \ No newline at end of file +u64 getTimeFrequency() { + return timer.GetFrequency(); +} + +} // namespace Emulator::emuTimer diff --git a/src/emulator.cpp b/src/emulator.cpp index b2ee632b..51f13be6 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -1,15 +1,13 @@ -#include "emulator.h" - -#include -#include -#include #include #include - -#include "core/PS4/HLE/Graphics/video_out.h" -#include "core/hle/libraries/libpad/pad.h" +#include "common/timer.h" #include "common/singleton.h" #include "common/version.h" +#include "emulator.h" +#include "core/PS4/HLE/Graphics/graphics_render.h" +#include "Emulator/Host/controller.h" +#include "core/PS4/HLE/Graphics/video_out.h" +#include "core/hle/libraries/libpad/pad.h" namespace Emu { @@ -92,7 +90,7 @@ static void calculateFps(double game_time_s) { } } void emuRun() { - Lib::Timer timer; + Common::Timer timer; timer.Start(); auto window_ctx = Common::Singleton::Instance(); {