Use the new high performance counter instead of date time class

This commit is contained in:
zilmar 2016-09-17 07:37:27 +10:00
parent 8036bdcd73
commit 4bc957bb3b
12 changed files with 91 additions and 111 deletions

View File

@ -137,6 +137,10 @@
RelativePath=".\FileClass.cpp" RelativePath=".\FileClass.cpp"
> >
</File> </File>
<File
RelativePath=".\HighResTimeStamp.cpp"
>
</File>
<File <File
RelativePath=".\IniFileClass.cpp" RelativePath=".\IniFileClass.cpp"
> >
@ -222,6 +226,10 @@
RelativePath=".\FileClass.h" RelativePath=".\FileClass.h"
> >
</File> </File>
<File
RelativePath=".\HighResTimeStamp.h"
>
</File>
<File <File
RelativePath=".\IniFileClass.h" RelativePath=".\IniFileClass.h"
> >

View File

@ -33,7 +33,9 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ItemGroup> <ItemGroup>
<ClCompile Include="CriticalSection.cpp" /> <ClCompile Include="CriticalSection.cpp" />
<ClCompile Include="DateTimeClass.cpp" />
<ClCompile Include="FileClass.cpp" /> <ClCompile Include="FileClass.cpp" />
<ClCompile Include="HighResTimeStamp.cpp" />
<ClCompile Include="IniFileClass.cpp" /> <ClCompile Include="IniFileClass.cpp" />
<ClCompile Include="LogClass.cpp" /> <ClCompile Include="LogClass.cpp" />
<ClCompile Include="md5.cpp" /> <ClCompile Include="md5.cpp" />
@ -46,14 +48,15 @@
</ClCompile> </ClCompile>
<ClCompile Include="StdString.cpp" /> <ClCompile Include="StdString.cpp" />
<ClCompile Include="SyncEvent.cpp" /> <ClCompile Include="SyncEvent.cpp" />
<ClCompile Include="DateTimeClass.cpp" />
<ClCompile Include="Thread.cpp" /> <ClCompile Include="Thread.cpp" />
<ClCompile Include="Trace.cpp" /> <ClCompile Include="Trace.cpp" />
<ClCompile Include="Util.cpp" /> <ClCompile Include="Util.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="CriticalSection.h" /> <ClInclude Include="CriticalSection.h" />
<ClInclude Include="DateTimeClass.h" />
<ClInclude Include="FileClass.h" /> <ClInclude Include="FileClass.h" />
<ClInclude Include="HighResTimeStamp.h" />
<ClInclude Include="IniFileClass.h" /> <ClInclude Include="IniFileClass.h" />
<ClInclude Include="LogClass.h" /> <ClInclude Include="LogClass.h" />
<ClInclude Include="md5.h" /> <ClInclude Include="md5.h" />
@ -66,7 +69,6 @@
<ClInclude Include="StdString.h" /> <ClInclude Include="StdString.h" />
<ClInclude Include="stdtypes.h" /> <ClInclude Include="stdtypes.h" />
<ClInclude Include="SyncEvent.h" /> <ClInclude Include="SyncEvent.h" />
<ClInclude Include="DateTimeClass.h" />
<ClInclude Include="Thread.h" /> <ClInclude Include="Thread.h" />
<ClInclude Include="Trace.h" /> <ClInclude Include="Trace.h" />
<ClInclude Include="TraceModulesCommon.h" /> <ClInclude Include="TraceModulesCommon.h" />

View File

@ -53,10 +53,13 @@
<ClCompile Include="MemoryManagement.cpp"> <ClCompile Include="MemoryManagement.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="DateTimeClass.cpp"> <ClCompile Include="Thread.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Thread.cpp"> <ClCompile Include="HighResTimeStamp.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DateTimeClass.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -112,10 +115,13 @@
<ClInclude Include="MemoryManagement.h"> <ClInclude Include="MemoryManagement.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="DateTimeClass.h"> <ClInclude Include="Thread.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Thread.h"> <ClInclude Include="HighResTimeStamp.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DateTimeClass.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

View File

@ -1,51 +1,15 @@
#include "stdafx.h" #include "stdafx.h"
#include "DateTimeClass.h" #include "DateTimeClass.h"
#ifdef ANDROID
#include <math.h>
#else
#include <sys/timeb.h>
#endif
#include <time.h> #include <time.h>
CDateTime::CDateTime() CDateTime::CDateTime()
{ {
m_time = 0; m_time = time(NULL);
}
CDateTime & CDateTime::SetToNow(void)
{
#ifdef ANDROID
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
m_time = (now.tv_sec * 1000l) + round(now.tv_nsec / 1.0e6);
#else
struct timeb now;
(void)::ftime(&now);
m_time = (now.time * 1000l) + now.millitm;
#endif
return *this;
} }
std::string CDateTime::Format(const char * format) std::string CDateTime::Format(const char * format)
{ {
char buffer[100]; char buffer[100];
time_t TimeValue = m_time / 1000l; strftime(buffer, sizeof(buffer), format, localtime(&m_time));
strftime(buffer, sizeof(buffer), format, localtime(&TimeValue));
return std::string(buffer); return std::string(buffer);
} }
double CDateTime::DiffernceMilliseconds(const CDateTime & compare)
{
double diff = (double)(m_time - compare.m_time);
return diff / 1000.0;
}
uint64_t CDateTime::Value(void)
{
return m_time;
}
void CDateTime::SetValue(uint64_t value)
{
m_time = value;
}

View File

@ -7,10 +7,7 @@ public:
CDateTime(); CDateTime();
CDateTime & SetToNow (void); CDateTime & SetToNow (void);
std::string Format (const char * format); std::string Format (const char * format);
double DiffernceMilliseconds (const CDateTime & compare);
uint64_t Value(void);
void SetValue(uint64_t value);
private: private:
uint64_t m_time; time_t m_time;
}; };

View File

@ -70,7 +70,7 @@ the plugin
#include <stddef.h> // offsetof #include <stddef.h> // offsetof
#include <glide.h> #include <glide.h>
#include <Common/MemTest.h> #include <Common/MemTest.h>
#include <Common/DateTimeClass.h> #include <Common/HighResTimeStamp.h>
#include <Settings/Settings.h> #include <Settings/Settings.h>
#include "GlideExtensions.h" #include "GlideExtensions.h"
#include "rdp.h" #include "rdp.h"
@ -150,8 +150,8 @@ extern "C" {
#define COLORED_DEBUGGER // ;) pretty colors #define COLORED_DEBUGGER // ;) pretty colors
#ifdef FPS #ifdef FPS
extern CDateTime fps_last; extern HighResTimeStamp fps_last;
extern CDateTime fps_next; extern HighResTimeStamp fps_next;
extern float fps; extern float fps;
extern uint32_t fps_count; extern uint32_t fps_count;
#endif #endif

View File

@ -43,6 +43,7 @@
#include "Version.h" #include "Version.h"
#include <Settings/Settings.h> #include <Settings/Settings.h>
#include <Common/CriticalSection.h> #include <Common/CriticalSection.h>
#include <Common/DateTimeClass.h>
#include <Common/path.h> #include <Common/path.h>
#include <png/png.h> #include <png/png.h>
#include <memory> #include <memory>
@ -91,8 +92,8 @@ int64 perf_next;
#endif #endif
#ifdef FPS #ifdef FPS
CDateTime fps_last; HighResTimeStamp fps_last;
CDateTime fps_next; HighResTimeStamp fps_next;
float fps = 0.0f; float fps = 0.0f;
uint32_t fps_count = 0; uint32_t fps_count = 0;
@ -1796,7 +1797,7 @@ void CALL UpdateScreen(void)
// Check frames per second // Check frames per second
fps_next.SetToNow(); fps_next.SetToNow();
double diff_secs = fps_next.DiffernceMilliseconds(fps_last); double diff_secs = (double)(fps_next.GetMicroSeconds() - fps_last.GetMicroSeconds()) / 1000000;
if (diff_secs > 0.5f) if (diff_secs > 0.5f)
{ {
fps = (float)(fps_count / diff_secs); fps = (float)(fps_count / diff_secs);
@ -2018,11 +2019,11 @@ void newSwapBuffers()
{ {
if (g_settings->clock_24_hr) if (g_settings->clock_24_hr)
{ {
output(956.0f, 0, 1, CDateTime().SetToNow().Format("%H:%M:%S").c_str(), 0); output(956.0f, 0, 1, CDateTime().Format("%H:%M:%S").c_str(), 0);
} }
else else
{ {
output(930.0f, 0, 1, CDateTime().SetToNow().Format("%I:%M:%S %p").c_str(), 0); output(930.0f, 0, 1, CDateTime().Format("%I:%M:%S %p").c_str(), 0);
} }
} }
//hotkeys //hotkeys

View File

@ -11,16 +11,14 @@
#include "stdafx.h" #include "stdafx.h"
#include "FramePerSecondClass.h" #include "FramePerSecondClass.h"
#include <Project64-core/N64System/N64Types.h> #include <Project64-core/N64System/N64Types.h>
#ifdef _WIN32
#include <Windows.h>
#endif
CFramePerSecond::CFramePerSecond() : CFramePerSecond::CFramePerSecond() :
m_CurrentViFrame(0), m_CurrentViFrame(0),
m_CurrentDlistFrame(0), m_CurrentDlistFrame(0),
m_iFrameRateType(g_Settings->LoadDword(UserInterface_FrameDisplayType)), m_iFrameRateType(g_Settings->LoadDword(UserInterface_FrameDisplayType)),
m_ScreenHertz(g_Settings->LoadDword(GameRunning_ScreenHertz)), m_ScreenHertz(g_Settings->LoadDword(GameRunning_ScreenHertz)),
m_ViFrameRate(0) m_ViFrameRateWhole(0),
m_ViFrameRateFraction(0)
{ {
g_Settings->RegisterChangeCB(UserInterface_FrameDisplayType, this, (CSettings::SettingChangedFunc)FrameRateTypeChanged); g_Settings->RegisterChangeCB(UserInterface_FrameDisplayType, this, (CSettings::SettingChangedFunc)FrameRateTypeChanged);
g_Settings->RegisterChangeCB(GameRunning_ScreenHertz, this, (CSettings::SettingChangedFunc)ScreenHertzChanged); g_Settings->RegisterChangeCB(GameRunning_ScreenHertz, this, (CSettings::SettingChangedFunc)ScreenHertzChanged);
@ -42,7 +40,7 @@ void CFramePerSecond::Reset(bool ClearDisplay)
{ {
m_CurrentDlistFrame = 0; m_CurrentDlistFrame = 0;
m_CurrentViFrame = 0; m_CurrentViFrame = 0;
m_LastViFrame.SetValue(0); m_LastViFrame.SetMicroSeconds(0);
for (int count = 0; count < NoOfFrames; count++) for (int count = 0; count < NoOfFrames; count++)
{ {
@ -57,7 +55,7 @@ void CFramePerSecond::Reset(bool ClearDisplay)
if (m_iFrameRateType == FR_VIs) if (m_iFrameRateType == FR_VIs)
{ {
DisplayViCounter(0); DisplayViCounter(-1, 0);
} }
} }
@ -71,11 +69,13 @@ void CFramePerSecond::UpdateViCounter(void)
} }
if ((m_CurrentViFrame & 7) == 0) if ((m_CurrentViFrame & 7) == 0)
{ {
CDateTime Time; HighResTimeStamp Time;
Time.SetToNow(); Time.SetToNow();
m_ViFrames[(m_CurrentViFrame >> 3) % NoOfFrames] = Time.Value() - m_LastViFrame.Value();
uint64_t time_diff = Time.GetMicroSeconds() - m_LastViFrame.GetMicroSeconds();
m_ViFrames[(m_CurrentViFrame >> 3) % NoOfFrames] = time_diff;
m_LastViFrame = Time; m_LastViFrame = Time;
DisplayViCounter(0); DisplayViCounter(-1, 0);
} }
m_CurrentViFrame += 1; m_CurrentViFrame += 1;
} }
@ -85,11 +85,11 @@ void CFramePerSecond::UpdateDisplay(void)
std::string DisplayString; std::string DisplayString;
if (m_iFrameRateType == FR_VIs || m_iFrameRateType == FR_VIs_DLs) if (m_iFrameRateType == FR_VIs || m_iFrameRateType == FR_VIs_DLs)
{ {
DisplayString = stdstr_f(m_ViFrameRate >= 0 ? "VI/s: %.2f" : "VI/s: -.--", m_ViFrameRate); DisplayString = stdstr_f(m_ViFrameRateWhole >= 0 ? "VI/s: %d.%d" : "VI/s: -.--", m_ViFrameRateWhole, m_ViFrameRateFraction);
} }
if (m_iFrameRateType == FR_PERCENT && m_ViFrameRate > 0) if (m_iFrameRateType == FR_PERCENT && m_ViFrameRateWhole > 0)
{ {
float Percent = ((float)m_ViFrameRate) / m_ScreenHertz; float Percent = ((float)m_ViFrameRateWhole + ((float)m_ViFrameRateFraction / 100)) / m_ScreenHertz;
DisplayString = stdstr_f("%.1f %%", Percent * 100).c_str(); DisplayString = stdstr_f("%.1f %%", Percent * 100).c_str();
} }
if (m_iFrameRateType == FR_DLs || m_iFrameRateType == FR_VIs_DLs) if (m_iFrameRateType == FR_DLs || m_iFrameRateType == FR_VIs_DLs)
@ -100,15 +100,16 @@ void CFramePerSecond::UpdateDisplay(void)
g_Notify->DisplayMessage2(DisplayString.c_str()); g_Notify->DisplayMessage2(DisplayString.c_str());
} }
void CFramePerSecond::DisplayViCounter(uint32_t FrameRate) void CFramePerSecond::DisplayViCounter(int32_t FrameRateWhole, uint32_t FrameRateFraction)
{ {
if (m_iFrameRateType != FR_VIs && m_iFrameRateType != FR_VIs_DLs && m_iFrameRateType != FR_PERCENT) if (m_iFrameRateType != FR_VIs && m_iFrameRateType != FR_VIs_DLs && m_iFrameRateType != FR_PERCENT)
{ {
return; return;
} }
if (FrameRate != 0) if (FrameRateWhole >= 0)
{ {
m_ViFrameRate = (float)FrameRate; m_ViFrameRateWhole = FrameRateWhole;
m_ViFrameRateFraction = FrameRateFraction;
} }
else else
{ {
@ -121,11 +122,14 @@ void CFramePerSecond::DisplayViCounter(uint32_t FrameRate)
{ {
Total += m_ViFrames[count]; Total += m_ViFrames[count];
} }
m_ViFrameRate = ((NoOfFrames << 3) / ((float)Total / 1000)); int baseFPS = (uint32_t)(((uint64_t)NoOfFrames << 3) * 100000000 / Total);
m_ViFrameRateWhole = baseFPS / 100;
m_ViFrameRateFraction = baseFPS % 100;
} }
else else
{ {
m_ViFrameRate = -1.0; m_ViFrameRateWhole = -1;
m_ViFrameRateFraction = 0;
} }
} }
UpdateDisplay(); UpdateDisplay();
@ -151,10 +155,10 @@ void CFramePerSecond::UpdateDlCounter(void)
} }
if ((m_CurrentDlistFrame & 3) == 0) if ((m_CurrentDlistFrame & 3) == 0)
{ {
CDateTime Time; HighResTimeStamp Now;
Time.SetToNow(); Now.SetToNow();
m_FramesDlist[(m_CurrentDlistFrame >> 2) % NoOfFrames] = Time.Value() - m_LastDlistFrame.Value(); m_FramesDlist[(m_CurrentDlistFrame >> 2) % NoOfFrames] = Now.GetMicroSeconds() - m_LastDlistFrame.GetMicroSeconds();
m_LastDlistFrame = Time; m_LastDlistFrame = Now;
if (m_CurrentDlistFrame > (NoOfFrames << 2)) if (m_CurrentDlistFrame > (NoOfFrames << 2))
{ {
int64_t Total; int64_t Total;
@ -164,7 +168,7 @@ void CFramePerSecond::UpdateDlCounter(void)
{ {
Total += m_FramesDlist[count]; Total += m_FramesDlist[count];
} }
m_DlistFrameRate = ((NoOfFrames << 2) / ((float)Total / 1000)); m_DlistFrameRate = ((NoOfFrames << 2) / ((float)Total / 1000000));
} }
else else
{ {

View File

@ -9,7 +9,7 @@
* * * *
****************************************************************************/ ****************************************************************************/
#pragma once #pragma once
#include <Common/DateTimeClass.h> #include <Common/HighResTimeStamp.h>
class CFramePerSecond class CFramePerSecond
{ {
@ -21,7 +21,7 @@ public:
void UpdateDlCounter(void); void UpdateDlCounter(void);
void UpdateViCounter(void); void UpdateViCounter(void);
void DisplayViCounter(uint32_t FrameRate); void DisplayViCounter(int32_t FrameRateWhole, uint32_t FrameRateFraction);
private: private:
CFramePerSecond(const CFramePerSecond&); // Disable copy constructor CFramePerSecond(const CFramePerSecond&); // Disable copy constructor
@ -35,14 +35,15 @@ private:
enum { NoOfFrames = 7 }; enum { NoOfFrames = 7 };
CDateTime m_LastViFrame; HighResTimeStamp m_LastViFrame;
int64_t m_ViFrames[NoOfFrames]; uint64_t m_ViFrames[NoOfFrames];
int32_t m_CurrentViFrame; uint32_t m_CurrentViFrame;
float m_ViFrameRate; int32_t m_ViFrameRateWhole;
uint32_t m_ViFrameRateFraction;
//Dlist //Dlist
CDateTime m_LastDlistFrame; HighResTimeStamp m_LastDlistFrame;
int64_t m_FramesDlist[NoOfFrames]; uint64_t m_FramesDlist[NoOfFrames];
int32_t m_CurrentDlistFrame; uint32_t m_CurrentDlistFrame;
float m_DlistFrameRate; float m_DlistFrameRate;
}; };

View File

@ -2089,7 +2089,7 @@ void CN64System::RefreshScreen()
uint32_t FrameRate; uint32_t FrameRate;
if (m_Limiter.Timer_Process(&FrameRate) && bDisplayFrameRate()) if (m_Limiter.Timer_Process(&FrameRate) && bDisplayFrameRate())
{ {
m_FPS.DisplayViCounter(FrameRate); m_FPS.DisplayViCounter(FrameRate, 0);
m_bCleanFrameBox = true; m_bCleanFrameBox = true;
} }
} }

View File

@ -11,17 +11,11 @@
#include "stdafx.h" #include "stdafx.h"
#include "SpeedLimiterClass.h" #include "SpeedLimiterClass.h"
#include <Common/Util.h> #include <Common/Util.h>
#ifdef _WIN32
#include <Windows.h>
#include <Mmsystem.h>
#pragma comment(lib, "winmm.lib")
#endif
CSpeedLimiter::CSpeedLimiter() : CSpeedLimiter::CSpeedLimiter() :
m_Frames(0), m_Frames(0),
m_Speed(60), m_Speed(60),
m_BaseSpeed(60), m_BaseSpeed(60)
m_Ratio(1000.0F / (float)m_Speed)
{ {
} }
@ -38,31 +32,37 @@ void CSpeedLimiter::SetHertz(uint32_t Hertz)
void CSpeedLimiter::FixSpeedRatio() void CSpeedLimiter::FixSpeedRatio()
{ {
m_Ratio = 1000.0f / static_cast<double>(m_Speed); m_MicroSecondsPerFrame = 1000000 / m_Speed;
m_Frames = 0; m_Frames = 0;
} }
bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate) bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate)
{ {
m_Frames += 1; m_Frames += 1;
CDateTime CurrentTime; HighResTimeStamp CurrentTime;
CurrentTime.SetToNow(); CurrentTime.SetToNow();
/* Calculate time that should of elapsed for this frame */ /* Calculate time that should of elapsed for this frame */
uint64_t CalculatedTime = (m_LastTime.Value()) + (m_Ratio * (double)m_Frames); uint64_t LastTime = m_LastTime.GetMicroSeconds(), CurrentTimeValue = CurrentTime.GetMicroSeconds();
if (CurrentTime.Value() < CalculatedTime) if (LastTime == 0)
{ {
int32_t time = (int)(CalculatedTime - CurrentTime.Value()); m_Frames = 0;
m_LastTime = CurrentTime;
return true;
}
uint64_t CalculatedTime = LastTime + (m_MicroSecondsPerFrame * m_Frames);
if (CurrentTimeValue < CalculatedTime)
{
int32_t time = (int)(CalculatedTime - CurrentTimeValue);
if (time > 0) if (time > 0)
{ {
#ifndef ANDROID pjutil::Sleep((time / 1000) + 1);
pjutil::Sleep(time);
#endif
} }
/* Refresh current time */ /* Refresh current time */
CurrentTime.SetToNow(); CurrentTime.SetToNow();
CurrentTimeValue = CurrentTime.GetMicroSeconds();
} }
if (CurrentTime.Value() - m_LastTime.Value() >= 1000) if (CurrentTimeValue - LastTime >= 1000000)
{ {
/* Output FPS */ /* Output FPS */
if (FrameRate != NULL) { *FrameRate = m_Frames; } if (FrameRate != NULL) { *FrameRate = m_Frames; }
@ -70,10 +70,7 @@ bool CSpeedLimiter::Timer_Process(uint32_t * FrameRate)
m_LastTime = CurrentTime; m_LastTime = CurrentTime;
return true; return true;
} }
else return false;
{
return false;
}
} }
void CSpeedLimiter::IncreaseSpeed() void CSpeedLimiter::IncreaseSpeed()

View File

@ -11,7 +11,7 @@
#pragma once #pragma once
#include <Project64-core/Settings/GameSettings.h> #include <Project64-core/Settings/GameSettings.h>
#include <Common/DateTimeClass.h> #include <Common/HighResTimeStamp.h>
class CSpeedLimiter : class CSpeedLimiter :
private CGameSettings private CGameSettings
@ -32,6 +32,6 @@ private:
void FixSpeedRatio(); void FixSpeedRatio();
uint32_t m_Speed, m_BaseSpeed, m_Frames; uint32_t m_Speed, m_BaseSpeed, m_Frames;
CDateTime m_LastTime; HighResTimeStamp m_LastTime;
double m_Ratio; uint32_t m_MicroSecondsPerFrame;
}; };