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() ? base::TimeTicks::ThreadNow()
15 : base::TimeTicks::Now();
20 LapTimer::LapTimer(int warmup_laps
,
21 base::TimeDelta time_limit
,
23 : warmup_laps_(warmup_laps
),
24 remaining_warmups_(0),
25 remaining_no_check_laps_(0),
26 time_limit_(time_limit
),
27 check_interval_(check_interval
) {
28 DCHECK_GT(check_interval
, 0);
32 void LapTimer::Reset() {
33 accumulator_
= base::TimeDelta();
35 remaining_warmups_
= warmup_laps_
;
36 remaining_no_check_laps_
= check_interval_
;
40 void LapTimer::Start() {
44 bool LapTimer::IsWarmedUp() { return remaining_warmups_
<= 0; }
46 void LapTimer::NextLap() {
55 --remaining_no_check_laps_
;
56 if (!remaining_no_check_laps_
) {
57 base::TimeTicks now
= Now();
58 accumulator_
+= now
- start_time_
;
60 remaining_no_check_laps_
= check_interval_
;
64 bool LapTimer::HasTimeLimitExpired() { return accumulator_
>= time_limit_
; }
66 bool LapTimer::HasTimedAllLaps() { return !(num_laps_
% check_interval_
); }
68 float LapTimer::MsPerLap() {
69 DCHECK(HasTimedAllLaps());
70 return accumulator_
.InMillisecondsF() / num_laps_
;
73 float LapTimer::LapsPerSecond() {
74 DCHECK(HasTimedAllLaps());
75 return num_laps_
/ accumulator_
.InSecondsF();
78 int LapTimer::NumLaps() { return num_laps_
; }