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"
13 base::TimeTicks
Now() {
14 return base::TimeTicks::IsThreadNowSupported()
15 ? base::TimeTicks::ThreadNow()
16 : base::TimeTicks::HighResNow();
21 LapTimer::LapTimer(int warmup_laps
,
22 base::TimeDelta time_limit
,
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);
33 void LapTimer::Reset() {
34 accumulator_
= base::TimeDelta();
36 remaining_warmups_
= warmup_laps_
;
37 remaining_no_check_laps_
= check_interval_
;
41 void LapTimer::Start() {
45 bool LapTimer::IsWarmedUp() { return remaining_warmups_
<= 0; }
47 void LapTimer::NextLap() {
56 --remaining_no_check_laps_
;
57 if (!remaining_no_check_laps_
) {
58 base::TimeTicks now
= Now();
59 accumulator_
+= now
- start_time_
;
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_
; }