OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/android/vr_shell/fps_meter.h" | 5 #include "chrome/browser/android/vr_shell/fps_meter.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 namespace vr_shell { | 9 namespace vr_shell { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 static constexpr size_t kNumFrameTimes = 10; | 13 static constexpr size_t kDefaultNumFrameTimes = 10; |
14 | 14 |
15 } // namepsace | 15 } // namespace |
16 | 16 |
17 FPSMeter::FPSMeter() : total_time_us_(0) { | 17 SampleQueue::SampleQueue(size_t window_size) : window_size_(window_size) { |
18 frame_times_.reserve(kNumFrameTimes); | 18 samples_.reserve(window_size); |
19 } | 19 } |
20 | 20 |
21 FPSMeter::~FPSMeter() {} | 21 SampleQueue::~SampleQueue() = default; |
| 22 |
| 23 void SampleQueue::AddSample(int64_t value) { |
| 24 sum_ += value; |
| 25 |
| 26 if (samples_.size() < window_size_) { |
| 27 samples_.push_back(value); |
| 28 } else { |
| 29 sum_ -= samples_[current_index_]; |
| 30 samples_[current_index_] = value; |
| 31 } |
| 32 |
| 33 ++current_index_; |
| 34 if (current_index_ >= window_size_) { |
| 35 current_index_ = 0; |
| 36 } |
| 37 } |
| 38 |
| 39 FPSMeter::FPSMeter() : frame_times_(kDefaultNumFrameTimes) {} |
| 40 |
| 41 FPSMeter::FPSMeter(size_t window_size) : frame_times_(window_size) {} |
| 42 |
| 43 FPSMeter::~FPSMeter() = default; |
22 | 44 |
23 size_t FPSMeter::GetNumFrameTimes() { | 45 size_t FPSMeter::GetNumFrameTimes() { |
24 return kNumFrameTimes; | 46 return frame_times_.GetWindowSize(); |
25 } | 47 } |
26 | 48 |
27 void FPSMeter::AddFrame(const base::TimeTicks& time_stamp) { | 49 void FPSMeter::AddFrame(const base::TimeTicks& time_stamp) { |
28 if (last_time_stamp_.is_null()) { | 50 if (last_time_stamp_.is_null()) { |
29 last_time_stamp_ = time_stamp; | 51 last_time_stamp_ = time_stamp; |
30 return; | 52 return; |
31 } | 53 } |
32 | 54 |
33 base::TimeDelta delta = time_stamp - last_time_stamp_; | 55 int64_t delta = (time_stamp - last_time_stamp_).InMicroseconds(); |
34 last_time_stamp_ = time_stamp; | 56 last_time_stamp_ = time_stamp; |
35 | 57 |
36 total_time_us_ += delta.InMicroseconds(); | 58 frame_times_.AddSample(delta); |
37 | |
38 if (frame_times_.size() < kNumFrameTimes) { | |
39 frame_times_.push_back(delta); | |
40 } else { | |
41 total_time_us_ -= frame_times_[current_index_].InMicroseconds(); | |
42 frame_times_[current_index_] = delta; | |
43 } | |
44 | |
45 current_index_++; | |
46 if (current_index_ >= kNumFrameTimes) | |
47 current_index_ = 0; | |
48 } | 59 } |
49 | 60 |
50 bool FPSMeter::CanComputeFPS() const { | 61 bool FPSMeter::CanComputeFPS() const { |
51 return !frame_times_.empty(); | 62 return frame_times_.GetCount() > 0; |
52 } | 63 } |
53 | 64 |
54 // Simply takes the average time delta. | 65 // Simply takes the average time delta. |
55 double FPSMeter::GetFPS() const { | 66 double FPSMeter::GetFPS() const { |
56 if (!CanComputeFPS()) | 67 if (!CanComputeFPS()) |
57 return 0.0; | 68 return 0.0; |
58 | 69 |
59 return (frame_times_.size() * 1.0e6) / total_time_us_; | 70 return (frame_times_.GetCount() * 1.0e6) / frame_times_.GetSum(); |
| 71 } |
| 72 |
| 73 SlidingAverage::SlidingAverage(size_t window_size) : values_(window_size) {} |
| 74 |
| 75 SlidingAverage::~SlidingAverage() = default; |
| 76 |
| 77 void SlidingAverage::AddSample(int64_t value) { |
| 78 values_.AddSample(value); |
| 79 } |
| 80 |
| 81 int64_t SlidingAverage::GetAverageOrDefault(int64_t default_value) const { |
| 82 if (values_.GetCount() == 0) |
| 83 return default_value; |
| 84 return values_.GetSum() / values_.GetCount(); |
60 } | 85 } |
61 | 86 |
62 } // namespace vr_shell | 87 } // namespace vr_shell |
OLD | NEW |