| 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 = 200; | 13 static constexpr size_t kNumFrameTimes = 10; |
| 14 | 14 |
| 15 } // namepsace | 15 } // namepsace |
| 16 | 16 |
| 17 FPSMeter::FPSMeter() : total_time_us_(0) { | 17 FPSMeter::FPSMeter() : total_time_us_(0) { |
| 18 frame_times_.reserve(kNumFrameTimes); | 18 frame_times_.reserve(kNumFrameTimes); |
| 19 } | 19 } |
| 20 | 20 |
| 21 FPSMeter::~FPSMeter() {} | 21 FPSMeter::~FPSMeter() {} |
| 22 | 22 |
| 23 size_t FPSMeter::GetNumFrameTimes() { |
| 24 return kNumFrameTimes; |
| 25 } |
| 26 |
| 23 void FPSMeter::AddFrame(const base::TimeTicks& time_stamp) { | 27 void FPSMeter::AddFrame(const base::TimeTicks& time_stamp) { |
| 24 if (last_time_stamp_.is_null()) { | 28 if (last_time_stamp_.is_null()) { |
| 25 last_time_stamp_ = time_stamp; | 29 last_time_stamp_ = time_stamp; |
| 26 return; | 30 return; |
| 27 } | 31 } |
| 28 | 32 |
| 29 base::TimeDelta delta = time_stamp - last_time_stamp_; | 33 base::TimeDelta delta = time_stamp - last_time_stamp_; |
| 30 last_time_stamp_ = time_stamp; | 34 last_time_stamp_ = time_stamp; |
| 31 | 35 |
| 32 total_time_us_ += delta.InMicroseconds(); | 36 total_time_us_ += delta.InMicroseconds(); |
| 33 | 37 |
| 34 if (frame_times_.size() + 1 < kNumFrameTimes) { | 38 if (frame_times_.size() < kNumFrameTimes) { |
| 35 frame_times_.push_back(delta); | 39 frame_times_.push_back(delta); |
| 36 } else { | 40 } else { |
| 37 total_time_us_ -= frame_times_[current_index_].InMicroseconds(); | 41 total_time_us_ -= frame_times_[current_index_].InMicroseconds(); |
| 38 frame_times_[current_index_] = delta; | 42 frame_times_[current_index_] = delta; |
| 39 } | 43 } |
| 40 | 44 |
| 41 current_index_++; | 45 current_index_++; |
| 42 if (current_index_ >= kNumFrameTimes) | 46 if (current_index_ >= kNumFrameTimes) |
| 43 current_index_ = 0; | 47 current_index_ = 0; |
| 44 } | 48 } |
| 45 | 49 |
| 46 bool FPSMeter::CanComputeFPS() const { | 50 bool FPSMeter::CanComputeFPS() const { |
| 47 return !frame_times_.empty(); | 51 return !frame_times_.empty(); |
| 48 } | 52 } |
| 49 | 53 |
| 50 // Simply takes the average time delta. | 54 // Simply takes the average time delta. |
| 51 double FPSMeter::GetFPS() const { | 55 double FPSMeter::GetFPS() const { |
| 52 if (!CanComputeFPS()) | 56 if (!CanComputeFPS()) |
| 53 return 0.0; | 57 return 0.0; |
| 54 | 58 |
| 55 return (frame_times_.size() * 1.0e6) / total_time_us_; | 59 return (frame_times_.size() * 1.0e6) / total_time_us_; |
| 56 } | 60 } |
| 57 | 61 |
| 58 } // namespace vr_shell | 62 } // namespace vr_shell |
| OLD | NEW |