Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / debug / lap_timer.cc
blob27aaf3499a9b2d6b156e1be9f2c28c845197c9d6
1 // Copyright 2014 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 #include "cc/debug/lap_timer.h"
7 #include "base/logging.h"
9 namespace cc {
11 namespace {
13 base::TimeTicks Now() {
14 return base::TimeTicks::IsThreadNowSupported()
15 ? base::TimeTicks::ThreadNow()
16 : base::TimeTicks::HighResNow();
19 } // namespace
21 LapTimer::LapTimer(int warmup_laps,
22 base::TimeDelta time_limit,
23 int check_interval)
24 : warmup_laps_(warmup_laps),
25 remaining_warmups_(0),
26 remaining_no_check_laps_(0),
27 time_limit_(time_limit),
28 check_interval_(check_interval) {
29 DCHECK_GT(check_interval, 0);
30 Reset();
33 void LapTimer::Reset() {
34 accumulator_ = base::TimeDelta();
35 num_laps_ = 0;
36 remaining_warmups_ = warmup_laps_;
37 remaining_no_check_laps_ = check_interval_;
38 Start();
41 void LapTimer::Start() {
42 start_time_ = Now();
45 bool LapTimer::IsWarmedUp() { return remaining_warmups_ <= 0; }
47 void LapTimer::NextLap() {
48 if (!IsWarmedUp()) {
49 --remaining_warmups_;
50 if (IsWarmedUp()) {
51 Start();
53 return;
55 ++num_laps_;
56 --remaining_no_check_laps_;
57 if (!remaining_no_check_laps_) {
58 base::TimeTicks now = Now();
59 accumulator_ += now - start_time_;
60 start_time_ = now;
61 remaining_no_check_laps_ = check_interval_;
65 bool LapTimer::HasTimeLimitExpired() { return accumulator_ >= time_limit_; }
67 bool LapTimer::HasTimedAllLaps() { return !(num_laps_ % check_interval_); }
69 float LapTimer::MsPerLap() {
70 DCHECK(HasTimedAllLaps());
71 return accumulator_.InMillisecondsF() / num_laps_;
74 float LapTimer::LapsPerSecond() {
75 DCHECK(HasTimedAllLaps());
76 return num_laps_ / accumulator_.InSecondsF();
79 int LapTimer::NumLaps() { return num_laps_; }
81 } // namespace cc