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.
4
5#ifndef CC_DEBUG_FRAME_RATE_COUNTER_H_
6#define CC_DEBUG_FRAME_RATE_COUNTER_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/time/time.h"
11#include "cc/debug/ring_buffer.h"
12
13namespace cc {
14
15// This class maintains a history of timestamps, and provides functionality to
16// intelligently compute average frames per second.
17class FrameRateCounter {
18 public:
19  static scoped_ptr<FrameRateCounter> Create(bool has_impl_thread);
20
21  int current_frame_number() const { return ring_buffer_.CurrentIndex(); }
22  int dropped_frame_count() const { return dropped_frame_count_; }
23  size_t time_stamp_history_size() const { return ring_buffer_.BufferSize(); }
24
25  void SaveTimeStamp(base::TimeTicks timestamp, bool software);
26
27  // n = 0 returns the oldest frame interval retained in the history, while n =
28  // time_stamp_history_size() - 1 returns the most recent frame interval.
29  base::TimeDelta RecentFrameInterval(size_t n) const;
30
31  // This is a heuristic that can be used to ignore frames in a reasonable way.
32  // Returns true if the given frame interval is too fast or too slow, based on
33  // constant thresholds.
34  bool IsBadFrameInterval(
35      base::TimeDelta interval_between_consecutive_frames) const;
36
37  void GetMinAndMaxFPS(double* min_fps, double* max_fps) const;
38  double GetAverageFPS() const;
39
40  typedef RingBuffer<base::TimeTicks, 136> RingBufferType;
41  RingBufferType::Iterator begin() const { return ring_buffer_.Begin(); }
42  RingBufferType::Iterator end() const { return ring_buffer_.End(); }
43
44 private:
45  explicit FrameRateCounter(bool has_impl_thread);
46
47  RingBufferType ring_buffer_;
48
49  bool has_impl_thread_;
50  int dropped_frame_count_;
51
52  DISALLOW_COPY_AND_ASSIGN(FrameRateCounter);
53};
54
55}  // namespace cc
56
57#endif  // CC_DEBUG_FRAME_RATE_COUNTER_H_
58