common: Move timer to common

This commit is contained in:
GPUCode 2023-11-06 01:17:28 +02:00
parent 369d92fa56
commit 099d01f96c
8 changed files with 84 additions and 69 deletions

View File

@ -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

View File

@ -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;
};
}

View File

@ -1,10 +1,12 @@
#include "Timer.h"
#include "common/timer.h"
#ifdef _WIN64
#include <windows.h>
#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<double>(m_PauseTime - m_StartTime)) / static_cast<double>(m_Frequency);
}
@ -67,7 +67,7 @@ double Lib::Timer::GetTimeMsec() const {
return 1000.0 * (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(m_Frequency);
}
double Lib::Timer::GetTimeSec() const {
double Timer::GetTimeSec() const {
if (m_is_timer_paused) {
return (static_cast<double>(m_PauseTime - m_StartTime)) / static_cast<double>(m_Frequency);
}
@ -83,7 +83,7 @@ double Lib::Timer::GetTimeSec() const {
return (static_cast<double>(current_time - m_StartTime)) / static_cast<double>(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

43
src/common/timer.h Normal file
View File

@ -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

View File

@ -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 (;;) {

View File

@ -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) {

View File

@ -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();
}
u64 getTimeFrequency() {
return timer.GetFrequency();
}
} // namespace Emulator::emuTimer

View File

@ -1,15 +1,13 @@
#include "emulator.h"
#include <core/PS4/HLE/Graphics/graphics_render.h>
#include <Emulator/Host/controller.h>
#include <Lib/Timer.h>
#include <fmt/core.h>
#include <vulkan_util.h>
#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<Emu::WindowCtx>::Instance();
{