Mercurial > hg > svgui
comparison layer/RenderTimer.h @ 1324:13d9b422f7fe zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:31 +0100 |
parents | a34a2a25907c |
children |
comparison
equal
deleted
inserted
replaced
1183:57d192e26331 | 1324:13d9b422f7fe |
---|---|
19 | 19 |
20 class RenderTimer | 20 class RenderTimer |
21 { | 21 { |
22 public: | 22 public: |
23 enum Type { | 23 enum Type { |
24 /// A normal rendering operation with normal responsiveness demands | 24 /// A normal rendering operation with normal responsiveness demands |
25 FastRender, | 25 FastRender, |
26 | 26 |
27 /// An operation that the user might accept being slower | 27 /// An operation that the user might accept being slower |
28 SlowRender, | 28 SlowRender, |
29 | 29 |
30 /// An operation that should always complete, i.e. as if there | 30 /// An operation that should always complete, i.e. as if there |
31 /// were no RenderTimer in use, but without having to change | 31 /// were no RenderTimer in use, but without having to change |
32 /// client code structurally | 32 /// client code structurally |
33 NoTimeout | 33 NoTimeout |
34 }; | 34 }; |
35 | 35 |
36 /** | 36 /** |
37 * Create a new RenderTimer and start timing. Make one of these | 37 * Create a new RenderTimer and start timing. Make one of these |
38 * before rendering, and then call outOfTime() regularly during | 38 * before rendering, and then call outOfTime() regularly during |
39 * rendering. If outOfTime() returns true, abandon rendering! and | 39 * rendering. If outOfTime() returns true, abandon rendering! and |
40 * schedule the rest for after some user responsiveness has | 40 * schedule the rest for after some user responsiveness has |
41 * happened. | 41 * happened. |
42 */ | 42 */ |
43 RenderTimer(Type t) : | 43 RenderTimer(Type t) : |
44 m_start(std::chrono::steady_clock::now()), | 44 m_start(std::chrono::steady_clock::now()), |
45 m_haveLimits(true), | 45 m_haveLimits(true), |
46 m_minFraction(0.1), | 46 m_minFraction(0.1), |
47 m_softLimit(0.1), | 47 m_softLimit(0.1), |
48 m_hardLimit(0.2), | 48 m_hardLimit(0.2), |
49 m_softLimitOverridden(false) { | 49 m_softLimitOverridden(false) { |
50 | 50 |
51 if (t == NoTimeout) { | 51 if (t == NoTimeout) { |
52 m_haveLimits = false; | 52 m_haveLimits = false; |
53 } else if (t == SlowRender) { | 53 } else if (t == SlowRender) { |
54 m_softLimit = 0.2; | 54 m_softLimit = 0.2; |
55 m_hardLimit = 0.4; | 55 m_hardLimit = 0.4; |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 | 59 |
60 /** | 60 /** |
61 * Return true if we have run out of time and should suspend | 61 * Return true if we have run out of time and should suspend |
64 * of how much of the work has been done as of this call, as a | 64 * of how much of the work has been done as of this call, as a |
65 * number between 0.0 (none of it) and 1.0 (all of it). | 65 * number between 0.0 (none of it) and 1.0 (all of it). |
66 */ | 66 */ |
67 bool outOfTime(double fractionComplete) { | 67 bool outOfTime(double fractionComplete) { |
68 | 68 |
69 if (!m_haveLimits || fractionComplete < m_minFraction) { | 69 if (!m_haveLimits || fractionComplete < m_minFraction) { |
70 return false; | 70 return false; |
71 } | 71 } |
72 | 72 |
73 auto t = std::chrono::steady_clock::now(); | 73 auto t = std::chrono::steady_clock::now(); |
74 double elapsed = std::chrono::duration<double>(t - m_start).count(); | 74 double elapsed = std::chrono::duration<double>(t - m_start).count(); |
75 | 75 |
76 if (elapsed > m_hardLimit) { | 76 if (elapsed > m_hardLimit) { |
77 return true; | 77 return true; |
78 } else if (!m_softLimitOverridden && elapsed > m_softLimit) { | 78 } else if (!m_softLimitOverridden && elapsed > m_softLimit) { |
79 if (fractionComplete > 0.6) { | 79 if (fractionComplete > 0.6) { |
80 // If we're significantly more than half way by the | 80 // If we're significantly more than half way by the |
81 // time we reach the soft limit, ignore it (though | 81 // time we reach the soft limit, ignore it (though |
82 // always respect the hard limit, above). Otherwise | 82 // always respect the hard limit, above). Otherwise |
83 // respect the soft limit and report out of time now. | 83 // respect the soft limit and report out of time now. |
84 m_softLimitOverridden = true; | 84 m_softLimitOverridden = true; |
85 } else { | 85 } else { |
86 return true; | 86 return true; |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 return false; | 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; | |
91 } | 101 } |
92 | 102 |
93 private: | 103 private: |
94 std::chrono::time_point<std::chrono::steady_clock> m_start; | 104 std::chrono::time_point<std::chrono::steady_clock> m_start; |
95 bool m_haveLimits; | 105 bool m_haveLimits; |
96 double m_minFraction; | 106 double m_minFraction; // proportion, 0.0 -> 1.0 |
97 double m_softLimit; | 107 double m_softLimit; // seconds |
98 double m_hardLimit; | 108 double m_hardLimit; // seconds |
99 bool m_softLimitOverridden; | 109 bool m_softLimitOverridden; |
100 }; | 110 }; |
101 | 111 |
102 #endif | 112 #endif |