1 // Copyright 2011 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 #ifndef CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_
6 #define CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_
10 #include "base/cancelable_callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/values.h"
13 #include "cc/base/cc_export.h"
16 namespace trace_event
{
19 class SingleThreadTaskRunner
;
23 class CC_EXPORT DelayBasedTimeSourceClient
{
25 virtual void OnTimerTick() = 0;
28 virtual ~DelayBasedTimeSourceClient() {}
31 // This timer implements a time source that achieves the specified interval
32 // in face of millisecond-precision delayed callbacks and random queueing
33 // delays. DelayBasedTimeSource uses base::TimeTicks::Now as its timebase.
34 class CC_EXPORT DelayBasedTimeSource
{
36 static scoped_ptr
<DelayBasedTimeSource
> Create(
37 base::TimeDelta interval
,
38 base::SingleThreadTaskRunner
* task_runner
) {
39 return make_scoped_ptr(new DelayBasedTimeSource(interval
, task_runner
));
42 virtual ~DelayBasedTimeSource();
44 void SetClient(DelayBasedTimeSourceClient
* client
);
46 void SetTimebaseAndInterval(base::TimeTicks timebase
,
47 base::TimeDelta interval
);
49 base::TimeDelta
Interval() const;
51 // Returns the time for the last missed tick.
52 base::TimeTicks
SetActive(bool active
);
55 // Get the last and next tick times. NextTickTime() returns null when
57 base::TimeTicks
LastTickTime() const;
58 base::TimeTicks
NextTickTime() const;
60 virtual void AsValueInto(base::trace_event::TracedValue
* dict
) const;
63 DelayBasedTimeSource(base::TimeDelta interval
,
64 base::SingleThreadTaskRunner
* task_runner
);
66 // Virtual for testing.
67 virtual base::TimeTicks
Now() const;
68 virtual std::string
TypeString() const;
71 base::TimeTicks
NextTickTarget(base::TimeTicks now
) const;
73 void PostNextTickTask(base::TimeTicks now
);
74 void ResetTickTask(base::TimeTicks now
);
78 DelayBasedTimeSourceClient
* client_
;
82 base::TimeTicks timebase_
;
83 base::TimeDelta interval_
;
85 base::TimeTicks last_tick_time_
;
86 base::TimeTicks next_tick_time_
;
88 base::CancelableClosure tick_closure_
;
90 base::SingleThreadTaskRunner
* task_runner_
;
92 base::WeakPtrFactory
<DelayBasedTimeSource
> weak_factory_
;
94 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource
);
99 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_