Chromium Code Reviews| Index: chrome/browser/android/vr_shell/fps_meter.cc |
| diff --git a/chrome/browser/android/vr_shell/fps_meter.cc b/chrome/browser/android/vr_shell/fps_meter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0aae7e085cdf68fbd7e320109d6719b35e45d32c |
| --- /dev/null |
| +++ b/chrome/browser/android/vr_shell/fps_meter.cc |
| @@ -0,0 +1,58 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/vr_shell/fps_meter.h" |
| + |
| +#include <algorithm> |
| + |
| +namespace vr_shell { |
| + |
| +namespace { |
| + |
| +static constexpr size_t kNumFrameTimes = 200; |
| + |
| +} // namepsace |
| + |
| +FPSMeter::FPSMeter() { |
| + frame_times_.reserve(kNumFrameTimes); |
| +} |
| + |
| +FPSMeter::~FPSMeter() {} |
| + |
| +void FPSMeter::AddFrame(const base::TimeTicks& time_stamp) { |
| + if (last_time_stamp_.is_null()) { |
| + last_time_stamp_ = time_stamp; |
| + return; |
| + } |
| + |
| + base::TimeDelta delta = time_stamp - last_time_stamp_; |
| + last_time_stamp_ = time_stamp; |
| + |
| + if (frame_times_.size() + 1 < kNumFrameTimes) |
| + frame_times_.push_back(delta); |
| + else |
| + frame_times_[current_index_] = delta; |
| + |
| + current_index_++; |
| + if (current_index_ >= kNumFrameTimes) |
| + current_index_ = 0; |
| +} |
| + |
| +bool FPSMeter::CanComputeFPS() const { |
| + return !frame_times_.empty(); |
| +} |
| + |
| +// Simply takes the average time delta. |
| +double FPSMeter::GetFPS() const { |
| + if (!CanComputeFPS()) |
| + return 0.0; |
| + |
| + double total_duration = 0.0; |
| + for (const auto& delta : frame_times_) |
|
mthiesse
2017/04/11 19:14:36
Instead of recomputing the sum each time you read,
billorr
2017/04/12 00:40:03
floating point error will accumulate
|
| + total_duration += delta.InSecondsF(); |
| + |
| + return frame_times_.size() / total_duration; |
| +} |
| + |
| +} // namespace vr_shell |