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); |
egraether
2013/01/10 02:32:09
Rewrote this method to make use of ReadBuffer(), b
| |
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::Now()); |
46 m_timeStampHistory[1] = m_timeStampHistory[0]; | |
47 for (int i = 2; i < kTimeStampHistorySize; i++) | |
48 m_timeStampHistory[i] = base::TimeTicks(); | |
egraether
2013/01/10 02:32:09
Removed this initialization, because it was unnece
shawnsingh
2013/01/10 21:13:01
Default constructor seems to initialize to 0...
| |
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 double fps = 1.0 / delta.InSecondsF(); | |
shawnsingh
2013/01/10 21:13:01
Let's add a DCHECK(delta.InSecondsF() > 0), to avo
| |
70 | |
71 minFPS = std::min(fps, minFPS); | |
72 maxFPS = std::max(fps, maxFPS); | |
73 } | |
74 | |
75 if (minFPS > maxFPS) | |
76 minFPS = maxFPS; | |
82 } | 77 } |
83 | 78 |
84 double FrameRateCounter::getAverageFPS() const | 79 double FrameRateCounter::getAverageFPS() const |
85 { | 80 { |
86 int frameNumber = m_currentFrameNumber - 1; | |
87 int frameCount = 0; | 81 int frameCount = 0; |
88 double frameTimesTotal = 0; | 82 double frameTimesTotal = 0; |
89 double averageFPS = 0; | 83 double averageFPS = 0; |
90 | 84 |
91 // Walk backwards through the samples looking for a run of good frame | 85 // Walk backwards through the samples looking for a run of good frame |
92 // timings from which to compute the mean. | 86 // timings from which to compute the mean. |
93 // | 87 // |
94 // Slow frames occur just because the user is inactive, and should be | 88 // 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 | 89 // 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 | 90 // 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 | 91 // the first few swapbuffers happen instantly which skews the statistics |
98 // too much for short lived animations. | 92 // too much for short lived animations. |
99 // | 93 // |
100 // isBadFrameInterval encapsulates the frame too slow/frame too fast logic. | 94 // isBadFrameInterval encapsulates the frame too slow/frame too fast logic. |
101 | 95 |
102 while (frameIndex(frameNumber) != frameIndex(m_currentFrameNumber) && frameN umber >= 0 && frameTimesTotal < 1.0) { | 96 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); | 97 base::TimeDelta delta = recentFrameInterval(i); |
104 | 98 |
105 if (!isBadFrameInterval(delta)) { | 99 if (!isBadFrameInterval(delta)) { |
106 frameCount++; | 100 frameCount++; |
107 frameTimesTotal += delta.InSecondsF(); | 101 frameTimesTotal += delta.InSecondsF(); |
108 } else if (frameCount) | 102 } else if (frameCount) |
109 break; | 103 break; |
110 | |
111 frameNumber--; | |
112 } | 104 } |
113 | 105 |
114 if (frameCount) | 106 if (frameCount) |
115 averageFPS = frameCount / frameTimesTotal; | 107 averageFPS = frameCount / frameTimesTotal; |
116 | 108 |
117 return averageFPS; | 109 return averageFPS; |
118 } | 110 } |
119 | 111 |
120 base::TimeTicks FrameRateCounter::timeStampOfRecentFrame(int n) const | 112 base::TimeTicks FrameRateCounter::timeStampOfRecentFrame(int n) const |
121 { | 113 { |
122 DCHECK(n >= 0); | 114 DCHECK(n >= 0); |
123 DCHECK(n < kTimeStampHistorySize); | 115 DCHECK(n < m_ringBuffer.BufferSize()); |
124 int desiredIndex = (frameIndex(m_currentFrameNumber) + n) % kTimeStampHistor ySize; | 116 return m_ringBuffer.ReadBuffer(n); |
125 return m_timeStampHistory[desiredIndex]; | |
126 } | 117 } |
127 | 118 |
128 } // namespace cc | 119 } // namespace cc |
OLD | NEW |