OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/frame_rate_counter.h" | 5 #include "cc/frame_rate_counter.h" |
6 | 6 |
7 #include <cmath> | 7 #include <limits> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "cc/proxy.h" | 10 #include "cc/proxy.h" |
11 | 11 |
12 namespace cc { | 12 namespace cc { |
13 | 13 |
14 const double FrameRateCounter::kFrameTooFast = 1.0 / 70.0; // measured in second s | 14 const double FrameRateCounter::kFrameTooFast = 1.0 / 70.0; // measured in second s |
15 const double FrameRateCounter::kFrameTooSlow = 1.0 / 4.0; | 15 const double FrameRateCounter::kFrameTooSlow = 1.0 / 4.0; |
16 const double FrameRateCounter::kDroppedFrameTime = 1.0 / 50.0; | 16 const double FrameRateCounter::kDroppedFrameTime = 1.0 / 50.0; |
17 | 17 |
18 // safeMod works on -1, returning m-1 in that case. | |
19 static inline int safeMod(int number, int modulus) | |
20 { | |
21 return (number + modulus) % modulus; | |
22 } | |
23 | |
24 // static | 18 // static |
25 scoped_ptr<FrameRateCounter> FrameRateCounter::create(bool hasImplThread) { | 19 scoped_ptr<FrameRateCounter> FrameRateCounter::create(bool hasImplThread) { |
26 return make_scoped_ptr(new FrameRateCounter(hasImplThread)); | 20 return make_scoped_ptr(new FrameRateCounter(hasImplThread)); |
27 } | 21 } |
28 | 22 |
29 inline base::TimeDelta FrameRateCounter::frameInterval(int frameNumber) const | 23 inline base::TimeDelta FrameRateCounter::recentFrameInterval(int n) const |
30 { | 24 { |
31 return m_timeStampHistory[frameIndex(frameNumber)] - | 25 DCHECK(n > 0); |
32 m_timeStampHistory[frameIndex(frameNumber - 1)]; | 26 return m_ringBuffer.ReadBuffer(n) - m_ringBuffer.ReadBuffer(n - 1); |
33 } | |
34 | |
35 inline int FrameRateCounter::frameIndex(int frameNumber) const | |
36 { | |
37 return safeMod(frameNumber, kTimeStampHistorySize); | |
38 } | 27 } |
39 | 28 |
40 FrameRateCounter::FrameRateCounter(bool hasImplThread) | 29 FrameRateCounter::FrameRateCounter(bool hasImplThread) |
41 : m_hasImplThread(hasImplThread) | 30 : m_hasImplThread(hasImplThread) |
42 , m_currentFrameNumber(1) | |
43 , m_droppedFrameCount(0) | 31 , m_droppedFrameCount(0) |
44 { | 32 { |
45 m_timeStampHistory[0] = base::TimeTicks::Now(); | 33 m_ringBuffer.SaveToBuffer(base::TimeTicks()); |
46 m_timeStampHistory[1] = m_timeStampHistory[0]; | |
47 for (int i = 2; i < kTimeStampHistorySize; i++) | |
48 m_timeStampHistory[i] = base::TimeTicks(); | |
49 } | 34 } |
50 | 35 |
51 void FrameRateCounter::markBeginningOfFrame(base::TimeTicks timestamp) | 36 void FrameRateCounter::saveTimeStamp(base::TimeTicks timestamp) |
52 { | 37 { |
53 m_timeStampHistory[frameIndex(m_currentFrameNumber)] = timestamp; | 38 m_ringBuffer.SaveToBuffer(timestamp); |
54 base::TimeDelta frameIntervalSeconds = frameInterval(m_currentFrameNumber); | 39 base::TimeDelta frameIntervalSeconds = recentFrameInterval(m_ringBuffer.Buff erSize() - 1); |
55 | 40 |
56 if (m_hasImplThread && m_currentFrameNumber > 0) { | 41 if (m_hasImplThread && m_ringBuffer.CurrentIndex() > 0) |
57 HISTOGRAM_CUSTOM_COUNTS("Renderer4.CompositorThreadImplDrawDelay", frame IntervalSeconds.InMilliseconds(), 1, 120, 60); | 42 HISTOGRAM_CUSTOM_COUNTS("Renderer4.CompositorThreadImplDrawDelay", frame IntervalSeconds.InMilliseconds(), 1, 120, 60); |
58 } | |
59 | 43 |
60 if (!isBadFrameInterval(frameIntervalSeconds) && | 44 if (!isBadFrameInterval(frameIntervalSeconds) && |
61 frameIntervalSeconds.InSecondsF() > kDroppedFrameTime) | 45 frameIntervalSeconds.InSecondsF() > kDroppedFrameTime) |
62 ++m_droppedFrameCount; | 46 ++m_droppedFrameCount; |
63 } | 47 } |
64 | 48 |
65 void FrameRateCounter::markEndOfFrame() | |
66 { | |
67 m_currentFrameNumber += 1; | |
68 } | |
69 | |
70 bool FrameRateCounter::isBadFrameInterval(base::TimeDelta intervalBetweenConsecu tiveFrames) const | 49 bool FrameRateCounter::isBadFrameInterval(base::TimeDelta intervalBetweenConsecu tiveFrames) const |
71 { | 50 { |
72 double delta = intervalBetweenConsecutiveFrames.InSecondsF(); | 51 double delta = intervalBetweenConsecutiveFrames.InSecondsF(); |
73 bool schedulerAllowsDoubleFrames = !m_hasImplThread; | 52 bool schedulerAllowsDoubleFrames = !m_hasImplThread; |
74 bool intervalTooFast = schedulerAllowsDoubleFrames ? delta < kFrameTooFast : delta <= 0.0; | 53 bool intervalTooFast = schedulerAllowsDoubleFrames ? delta < kFrameTooFast : delta <= 0.0; |
75 bool intervalTooSlow = delta > kFrameTooSlow; | 54 bool intervalTooSlow = delta > kFrameTooSlow; |
76 return intervalTooFast || intervalTooSlow; | 55 return intervalTooFast || intervalTooSlow; |
77 } | 56 } |
78 | 57 |
79 bool FrameRateCounter::isBadFrame(int frameNumber) const | 58 void FrameRateCounter::getMinAndMaxFPS(double& minFPS, double& maxFPS) const |
80 { | 59 { |
81 return isBadFrameInterval(frameInterval(frameNumber)); | 60 minFPS = std::numeric_limits<double>::max(); |
61 maxFPS = 0; | |
62 | |
63 for (int i = m_ringBuffer.BufferSize() - 1; i > 0 && m_ringBuffer.IsFilledIn dex(i - 1); --i) { | |
64 base::TimeDelta delta = recentFrameInterval(i); | |
65 | |
66 if (isBadFrameInterval(delta)) | |
67 continue; | |
68 | |
69 DCHECK(delta.InSecondsF() > 0); | |
egraether
2013/01/10 23:38:29
checking divide by 0
| |
70 double fps = 1.0 / delta.InSecondsF(); | |
71 | |
72 minFPS = std::min(fps, minFPS); | |
73 maxFPS = std::max(fps, maxFPS); | |
74 } | |
75 | |
76 if (minFPS > maxFPS) | |
77 minFPS = maxFPS; | |
82 } | 78 } |
83 | 79 |
84 double FrameRateCounter::getAverageFPS() const | 80 double FrameRateCounter::getAverageFPS() const |
85 { | 81 { |
86 int frameNumber = m_currentFrameNumber - 1; | |
87 int frameCount = 0; | 82 int frameCount = 0; |
88 double frameTimesTotal = 0; | 83 double frameTimesTotal = 0; |
89 double averageFPS = 0; | 84 double averageFPS = 0; |
90 | 85 |
91 // Walk backwards through the samples looking for a run of good frame | 86 // Walk backwards through the samples looking for a run of good frame |
92 // timings from which to compute the mean. | 87 // timings from which to compute the mean. |
93 // | 88 // |
94 // Slow frames occur just because the user is inactive, and should be | 89 // Slow frames occur just because the user is inactive, and should be |
95 // ignored. Fast frames are ignored if the scheduler is in single-thread | 90 // ignored. Fast frames are ignored if the scheduler is in single-thread |
96 // mode in order to represent the true frame rate in spite of the fact that | 91 // mode in order to represent the true frame rate in spite of the fact that |
97 // the first few swapbuffers happen instantly which skews the statistics | 92 // the first few swapbuffers happen instantly which skews the statistics |
98 // too much for short lived animations. | 93 // too much for short lived animations. |
99 // | 94 // |
100 // isBadFrameInterval encapsulates the frame too slow/frame too fast logic. | 95 // isBadFrameInterval encapsulates the frame too slow/frame too fast logic. |
101 | 96 |
102 while (frameIndex(frameNumber) != frameIndex(m_currentFrameNumber) && frameN umber >= 0 && frameTimesTotal < 1.0) { | 97 for (int i = m_ringBuffer.BufferSize() - 1; i > 0 && m_ringBuffer.IsFilledIn dex(i - 1) && frameTimesTotal < 1.0; --i) { |
103 base::TimeDelta delta = frameInterval(frameNumber); | 98 base::TimeDelta delta = recentFrameInterval(i); |
104 | 99 |
105 if (!isBadFrameInterval(delta)) { | 100 if (!isBadFrameInterval(delta)) { |
106 frameCount++; | 101 frameCount++; |
107 frameTimesTotal += delta.InSecondsF(); | 102 frameTimesTotal += delta.InSecondsF(); |
108 } else if (frameCount) | 103 } else if (frameCount) |
109 break; | 104 break; |
110 | |
111 frameNumber--; | |
112 } | 105 } |
113 | 106 |
114 if (frameCount) | 107 if (frameCount) { |
108 DCHECK(frameTimesTotal > 0); | |
egraether
2013/01/10 23:38:29
checking divide by 0
| |
115 averageFPS = frameCount / frameTimesTotal; | 109 averageFPS = frameCount / frameTimesTotal; |
110 } | |
116 | 111 |
117 return averageFPS; | 112 return averageFPS; |
118 } | 113 } |
119 | 114 |
120 base::TimeTicks FrameRateCounter::timeStampOfRecentFrame(int n) const | 115 base::TimeTicks FrameRateCounter::timeStampOfRecentFrame(int n) const |
121 { | 116 { |
122 DCHECK(n >= 0); | 117 DCHECK(n >= 0); |
123 DCHECK(n < kTimeStampHistorySize); | 118 DCHECK(n < m_ringBuffer.BufferSize()); |
124 int desiredIndex = (frameIndex(m_currentFrameNumber) + n) % kTimeStampHistor ySize; | 119 |
125 return m_timeStampHistory[desiredIndex]; | 120 if (m_ringBuffer.IsFilledIndex(n)) |
121 return m_ringBuffer.ReadBuffer(n); | |
122 | |
123 return base::TimeTicks(); | |
egraether
2013/01/10 23:38:29
Returns base::TimeTicks() for an invalid index. No
| |
126 } | 124 } |
127 | 125 |
128 } // namespace cc | 126 } // namespace cc |
OLD | NEW |