RenderTimer.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef RENDER_TIMER_H
16 #define RENDER_TIMER_H
17 
18 #include <chrono>
19 
21 {
22 public:
23  enum Type {
26 
29 
34  };
35 
44  m_start(std::chrono::steady_clock::now()),
45  m_haveLimits(true),
46  m_minFraction(0.1),
47  m_softLimit(0.1),
48  m_hardLimit(0.2),
49  m_softLimitOverridden(false) {
50 
51  if (t == NoTimeout) {
52  m_haveLimits = false;
53  } else if (t == SlowRender) {
54  m_softLimit = 0.2;
55  m_hardLimit = 0.4;
56  }
57  }
58 
59 
67  bool outOfTime(double fractionComplete) {
68 
69  if (!m_haveLimits || fractionComplete < m_minFraction) {
70  return false;
71  }
72 
73  auto t = std::chrono::steady_clock::now();
74  double elapsed = std::chrono::duration<double>(t - m_start).count();
75 
76  if (elapsed > m_hardLimit) {
77  return true;
78  } else if (!m_softLimitOverridden && elapsed > m_softLimit) {
79  if (fractionComplete > 0.6) {
80  // If we're significantly more than half way by the
81  // time we reach the soft limit, ignore it (though
82  // always respect the hard limit, above). Otherwise
83  // respect the soft limit and report out of time now.
84  m_softLimitOverridden = true;
85  } else {
86  return true;
87  }
88  }
89 
90  return false;
91  }
92 
93  double secondsPerItem(int itemsRendered) const {
94 
95  if (itemsRendered == 0) return 0.0;
96 
97  auto t = std::chrono::steady_clock::now();
98  double elapsed = std::chrono::duration<double>(t - m_start).count();
99 
100  return elapsed / itemsRendered;
101  }
102 
103 private:
104  std::chrono::time_point<std::chrono::steady_clock> m_start;
106  double m_minFraction; // proportion, 0.0 -> 1.0
107  double m_softLimit; // seconds
108  double m_hardLimit; // seconds
110 };
111 
112 #endif
bool m_haveLimits
Definition: RenderTimer.h:105
An operation that the user might accept being slower.
Definition: RenderTimer.h:28
bool outOfTime(double fractionComplete)
Return true if we have run out of time and should suspend rendering and handle user events instead...
Definition: RenderTimer.h:67
std::chrono::time_point< std::chrono::steady_clock > m_start
Definition: RenderTimer.h:104
An operation that should always complete, i.e.
Definition: RenderTimer.h:33
RenderTimer(Type t)
Create a new RenderTimer and start timing.
Definition: RenderTimer.h:43
bool m_softLimitOverridden
Definition: RenderTimer.h:109
double m_hardLimit
Definition: RenderTimer.h:108
double secondsPerItem(int itemsRendered) const
Definition: RenderTimer.h:93
double m_minFraction
Definition: RenderTimer.h:106
double m_softLimit
Definition: RenderTimer.h:107
A normal rendering operation with normal responsiveness demands.
Definition: RenderTimer.h:25