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
3 // found in the LICENSE file.
5 #ifndef CC_FRAME_RATE_COUNTER_H_
6 #define CC_FRAME_RATE_COUNTER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h"
14 // This class maintains a history of timestamps, and provides functionality to
15 // intelligently compute average frames per second.
16 class FrameRateCounter
{
18 static scoped_ptr
<FrameRateCounter
> create(bool hasImplThread
);
20 void markBeginningOfFrame(base::TimeTicks timestamp
);
21 void markEndOfFrame();
22 int currentFrameNumber() const { return m_currentFrameNumber
; }
23 double getAverageFPS() const;
24 int timeStampHistorySize() const { return kTimeStampHistorySize
; }
26 // n = 0 returns the oldest frame retained in the history,
27 // while n = timeStampHistorySize() - 1 returns the timestamp most recent frame.
28 // FIXME: Returns most recent timestamp for n = 0 when called between markBeginningOfFrame and markEndOfFrame calls.
29 base::TimeTicks
timeStampOfRecentFrame(int n
) const;
31 // This is a heuristic that can be used to ignore frames in a reasonable way. Returns
32 // true if the given frame interval is too fast or too slow, based on constant thresholds.
33 bool isBadFrameInterval(base::TimeDelta intervalBetweenConsecutiveFrames
) const;
35 int droppedFrameCount() const { return m_droppedFrameCount
; }
38 explicit FrameRateCounter(bool hasImplThread
);
40 base::TimeDelta
frameInterval(int frameNumber
) const;
41 int frameIndex(int frameNumber
) const;
42 bool isBadFrame(int frameNumber
) const;
44 // Two thresholds (measured in seconds) that describe what is considered to be a "no-op frame" that should not be counted.
45 // - if the frame is too fast, then given our compositor implementation, the frame probably was a no-op and did not draw.
46 // - if the frame is too slow, then there is probably not animating content, so we should not pollute the average.
47 static const double kFrameTooFast
;
48 static const double kFrameTooSlow
;
50 // If a frame takes longer than this threshold (measured in seconds) then we
51 // (naively) assume that it missed a screen refresh; that is, we dropped a frame.
52 // FIXME: Determine this threshold based on monitor refresh rate, crbug.com/138642.
53 static const double kDroppedFrameTime
;
55 static const int kTimeStampHistorySize
= 170;
59 int m_currentFrameNumber
;
60 base::TimeTicks m_timeStampHistory
[kTimeStampHistorySize
];
62 int m_droppedFrameCount
;
64 DISALLOW_COPY_AND_ASSIGN(FrameRateCounter
);
69 #endif // CC_FRAME_RATE_COUNTER_H_