summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/FPSCounter.cpp
blob: 4d71a7c462ef37348a7829bde1b49076f73c3e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2012 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "VideoCommon/FPSCounter.h"

#include <fstream>
#include <iomanip>

#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/Timer.h"
#include "Core/Core.h"
#include "VideoCommon/VideoConfig.h"

static constexpr u64 FPS_REFRESH_INTERVAL = 250000;

FPSCounter::FPSCounter()
{
  m_last_time = Common::Timer::GetTimeUs();

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

void FPSCounter::LogRenderTimeToFile(u64 val)
{
  if (!m_bench_file.is_open())
  {
    File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + "render_time.txt",
                      std::ios_base::out);
  }

  m_bench_file << std::fixed << std::setprecision(8) << (val / 1000.0) << std::endl;
}

void FPSCounter::Update()
{
  const u64 time = Common::Timer::GetTimeUs();
  const u64 diff = time - m_last_time;
  m_time_diff_secs = static_cast<double>(diff / 1000000.0);
  if (g_ActiveConfig.bLogRenderTimeToFile)
    LogRenderTimeToFile(diff);

  m_frame_counter++;
  m_time_since_update += diff;
  m_last_time = time;

  if (m_time_since_update >= FPS_REFRESH_INTERVAL)
  {
    m_fps = m_frame_counter / (m_time_since_update / 1000000.0);
    m_frame_counter = 0;
    m_time_since_update = 0;
  }
}

void FPSCounter::SetPaused(bool paused)
{
  if (paused)
  {
    m_last_time_pause = Common::Timer::GetTimeUs();
  }
  else
  {
    const u64 time = Common::Timer::GetTimeUs();
    const u64 diff = time - m_last_time_pause;
    m_last_time += diff;
  }
}