2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2012 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "VideoCommon/FPSCounter.h"
|
|
|
|
|
2014-07-03 00:19:08 +00:00
|
|
|
#include <fstream>
|
2017-06-30 07:25:32 +00:00
|
|
|
#include <iomanip>
|
2014-07-03 00:19:08 +00:00
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Timer.h"
|
2020-12-31 12:03:20 +00:00
|
|
|
#include "Core/Core.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2022-06-18 02:11:53 +00:00
|
|
|
static constexpr u64 FPS_REFRESH_INTERVAL_US = 250000;
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2014-07-13 11:04:25 +00:00
|
|
|
FPSCounter::FPSCounter()
|
2012-10-04 03:41:02 +00:00
|
|
|
{
|
2022-07-18 03:43:47 +00:00
|
|
|
m_last_time = Common::Timer::NowUs();
|
2020-12-31 12:03:20 +00:00
|
|
|
|
|
|
|
m_on_state_changed_handle = Core::AddOnStateChangedCallback([this](Core::State state) {
|
|
|
|
if (state == Core::State::Paused)
|
|
|
|
SetPaused(true);
|
|
|
|
else if (state == Core::State::Running)
|
|
|
|
SetPaused(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
FPSCounter::~FPSCounter()
|
|
|
|
{
|
|
|
|
Core::RemoveOnStateChangedCallback(&m_on_state_changed_handle);
|
2012-10-04 03:41:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 11:04:25 +00:00
|
|
|
void FPSCounter::LogRenderTimeToFile(u64 val)
|
2012-10-04 03:41:02 +00:00
|
|
|
{
|
2014-07-13 11:04:25 +00:00
|
|
|
if (!m_bench_file.is_open())
|
2017-12-05 20:23:35 +00:00
|
|
|
{
|
|
|
|
File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + "render_time.txt",
|
|
|
|
std::ios_base::out);
|
|
|
|
}
|
2012-10-04 03:41:02 +00:00
|
|
|
|
2017-06-30 07:25:32 +00:00
|
|
|
m_bench_file << std::fixed << std::setprecision(8) << (val / 1000.0) << std::endl;
|
2012-10-04 03:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:57:54 +00:00
|
|
|
void FPSCounter::Update()
|
2012-10-04 03:41:02 +00:00
|
|
|
{
|
2022-07-18 03:43:47 +00:00
|
|
|
const u64 time = Common::Timer::NowUs();
|
2020-12-31 12:03:20 +00:00
|
|
|
const u64 diff = time - m_last_time;
|
|
|
|
m_time_diff_secs = static_cast<double>(diff / 1000000.0);
|
2014-07-09 15:03:17 +00:00
|
|
|
if (g_ActiveConfig.bLogRenderTimeToFile)
|
2017-06-30 07:25:32 +00:00
|
|
|
LogRenderTimeToFile(diff);
|
|
|
|
|
|
|
|
m_frame_counter++;
|
|
|
|
m_time_since_update += diff;
|
|
|
|
m_last_time = time;
|
|
|
|
|
2022-06-18 02:11:53 +00:00
|
|
|
if (m_time_since_update >= FPS_REFRESH_INTERVAL_US)
|
2014-07-09 15:03:17 +00:00
|
|
|
{
|
2017-06-30 07:25:32 +00:00
|
|
|
m_fps = m_frame_counter / (m_time_since_update / 1000000.0);
|
|
|
|
m_frame_counter = 0;
|
|
|
|
m_time_since_update = 0;
|
2014-07-09 15:03:17 +00:00
|
|
|
}
|
2014-07-03 00:19:08 +00:00
|
|
|
}
|
2020-12-31 12:03:20 +00:00
|
|
|
|
|
|
|
void FPSCounter::SetPaused(bool paused)
|
|
|
|
{
|
|
|
|
if (paused)
|
|
|
|
{
|
2022-07-18 03:43:47 +00:00
|
|
|
m_last_time_pause = Common::Timer::NowUs();
|
2020-12-31 12:03:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-18 03:43:47 +00:00
|
|
|
const u64 time = Common::Timer::NowUs();
|
2020-12-31 12:03:20 +00:00
|
|
|
const u64 diff = time - m_last_time_pause;
|
|
|
|
m_last_time += diff;
|
|
|
|
}
|
|
|
|
}
|