| 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(size_t 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(); | |
| 46 m_timeStampHistory[1] = m_timeStampHistory[0]; | |
| 47 for (int i = 2; i < kTimeStampHistorySize; i++) | |
| 48 m_timeStampHistory[i] = base::TimeTicks(); | |
| 49 } | 33 } |
| 50 | 34 |
| 51 void FrameRateCounter::markBeginningOfFrame(base::TimeTicks timestamp) | 35 void FrameRateCounter::saveTimeStamp(base::TimeTicks timestamp) |
| 52 { | 36 { |
| 53 m_timeStampHistory[frameIndex(m_currentFrameNumber)] = timestamp; | 37 m_ringBuffer.SaveToBuffer(timestamp); |
| 54 base::TimeDelta frameIntervalSeconds = frameInterval(m_currentFrameNumber); | |
| 55 | 38 |
| 56 if (m_hasImplThread && m_currentFrameNumber > 0) { | 39 // Check if frame interval can be computed. |
| 40 if (m_ringBuffer.CurrentIndex() < 2) |
| 41 return; |
| 42 |
| 43 base::TimeDelta frameIntervalSeconds = recentFrameInterval(m_ringBuffer.Buff
erSize() - 1); |
| 44 |
| 45 if (m_hasImplThread && m_ringBuffer.CurrentIndex() > 0) |
| 57 HISTOGRAM_CUSTOM_COUNTS("Renderer4.CompositorThreadImplDrawDelay", frame
IntervalSeconds.InMilliseconds(), 1, 120, 60); | 46 HISTOGRAM_CUSTOM_COUNTS("Renderer4.CompositorThreadImplDrawDelay", frame
IntervalSeconds.InMilliseconds(), 1, 120, 60); |
| 58 } | |
| 59 | 47 |
| 60 if (!isBadFrameInterval(frameIntervalSeconds) && | 48 if (!isBadFrameInterval(frameIntervalSeconds) && |
| 61 frameIntervalSeconds.InSecondsF() > kDroppedFrameTime) | 49 frameIntervalSeconds.InSecondsF() > kDroppedFrameTime) |
| 62 ++m_droppedFrameCount; | 50 ++m_droppedFrameCount; |
| 63 } | 51 } |
| 64 | 52 |
| 65 void FrameRateCounter::markEndOfFrame() | |
| 66 { | |
| 67 m_currentFrameNumber += 1; | |
| 68 } | |
| 69 | |
| 70 bool FrameRateCounter::isBadFrameInterval(base::TimeDelta intervalBetweenConsecu
tiveFrames) const | 53 bool FrameRateCounter::isBadFrameInterval(base::TimeDelta intervalBetweenConsecu
tiveFrames) const |
| 71 { | 54 { |
| 72 double delta = intervalBetweenConsecutiveFrames.InSecondsF(); | 55 double delta = intervalBetweenConsecutiveFrames.InSecondsF(); |
| 73 bool schedulerAllowsDoubleFrames = !m_hasImplThread; | 56 bool schedulerAllowsDoubleFrames = !m_hasImplThread; |
| 74 bool intervalTooFast = schedulerAllowsDoubleFrames ? delta < kFrameTooFast :
delta <= 0.0; | 57 bool intervalTooFast = schedulerAllowsDoubleFrames ? delta < kFrameTooFast :
delta <= 0.0; |
| 75 bool intervalTooSlow = delta > kFrameTooSlow; | 58 bool intervalTooSlow = delta > kFrameTooSlow; |
| 76 return intervalTooFast || intervalTooSlow; | 59 return intervalTooFast || intervalTooSlow; |
| 77 } | 60 } |
| 78 | 61 |
| 79 bool FrameRateCounter::isBadFrame(int frameNumber) const | 62 void FrameRateCounter::getMinAndMaxFPS(double& minFPS, double& maxFPS) const |
| 80 { | 63 { |
| 81 return isBadFrameInterval(frameInterval(frameNumber)); | 64 minFPS = std::numeric_limits<double>::max(); |
| 65 maxFPS = 0; |
| 66 |
| 67 for (size_t i = m_ringBuffer.BufferSize() - 1; i > 0 && m_ringBuffer.IsFille
dIndex(i - 1); --i) { |
| 68 base::TimeDelta delta = recentFrameInterval(i); |
| 69 |
| 70 if (isBadFrameInterval(delta)) |
| 71 continue; |
| 72 |
| 73 DCHECK(delta.InSecondsF() > 0); |
| 74 double fps = 1.0 / delta.InSecondsF(); |
| 75 |
| 76 minFPS = std::min(fps, minFPS); |
| 77 maxFPS = std::max(fps, maxFPS); |
| 78 } |
| 79 |
| 80 if (minFPS > maxFPS) |
| 81 minFPS = maxFPS; |
| 82 } | 82 } |
| 83 | 83 |
| 84 double FrameRateCounter::getAverageFPS() const | 84 double FrameRateCounter::getAverageFPS() const |
| 85 { | 85 { |
| 86 int frameNumber = m_currentFrameNumber - 1; | |
| 87 int frameCount = 0; | 86 int frameCount = 0; |
| 88 double frameTimesTotal = 0; | 87 double frameTimesTotal = 0; |
| 89 double averageFPS = 0; | 88 double averageFPS = 0; |
| 90 | 89 |
| 91 // Walk backwards through the samples looking for a run of good frame | 90 // Walk backwards through the samples looking for a run of good frame |
| 92 // timings from which to compute the mean. | 91 // timings from which to compute the mean. |
| 93 // | 92 // |
| 94 // Slow frames occur just because the user is inactive, and should be | 93 // 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 | 94 // 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 | 95 // 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 | 96 // the first few swapbuffers happen instantly which skews the statistics |
| 98 // too much for short lived animations. | 97 // too much for short lived animations. |
| 99 // | 98 // |
| 100 // isBadFrameInterval encapsulates the frame too slow/frame too fast logic. | 99 // isBadFrameInterval encapsulates the frame too slow/frame too fast logic. |
| 101 | 100 |
| 102 while (frameIndex(frameNumber) != frameIndex(m_currentFrameNumber) && frameN
umber >= 0 && frameTimesTotal < 1.0) { | 101 for (size_t i = m_ringBuffer.BufferSize() - 1; i > 0 && m_ringBuffer.IsFille
dIndex(i - 1) && frameTimesTotal < 1.0; --i) { |
| 103 base::TimeDelta delta = frameInterval(frameNumber); | 102 base::TimeDelta delta = recentFrameInterval(i); |
| 104 | 103 |
| 105 if (!isBadFrameInterval(delta)) { | 104 if (!isBadFrameInterval(delta)) { |
| 106 frameCount++; | 105 frameCount++; |
| 107 frameTimesTotal += delta.InSecondsF(); | 106 frameTimesTotal += delta.InSecondsF(); |
| 108 } else if (frameCount) | 107 } else if (frameCount) |
| 109 break; | 108 break; |
| 110 | |
| 111 frameNumber--; | |
| 112 } | 109 } |
| 113 | 110 |
| 114 if (frameCount) | 111 if (frameCount) { |
| 112 DCHECK(frameTimesTotal > 0); |
| 115 averageFPS = frameCount / frameTimesTotal; | 113 averageFPS = frameCount / frameTimesTotal; |
| 114 } |
| 116 | 115 |
| 117 return averageFPS; | 116 return averageFPS; |
| 118 } | 117 } |
| 119 | 118 |
| 120 base::TimeTicks FrameRateCounter::timeStampOfRecentFrame(int n) const | 119 base::TimeTicks FrameRateCounter::timeStampOfRecentFrame(size_t n) const |
| 121 { | 120 { |
| 122 DCHECK(n >= 0); | 121 DCHECK(n < m_ringBuffer.BufferSize()); |
| 123 DCHECK(n < kTimeStampHistorySize); | 122 |
| 124 int desiredIndex = (frameIndex(m_currentFrameNumber) + n) % kTimeStampHistor
ySize; | 123 if (m_ringBuffer.IsFilledIndex(n)) |
| 125 return m_timeStampHistory[desiredIndex]; | 124 return m_ringBuffer.ReadBuffer(n); |
| 125 |
| 126 return base::TimeTicks(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 } // namespace cc | 129 } // namespace cc |
| OLD | NEW |