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/memory/weak_ptr.h"
11 #include "base/values.h"
12 #include "cc/base/cc_export.h"
15 namespace trace_event
{
18 class SingleThreadTaskRunner
;
23 class CC_EXPORT TimeSourceClient
{
25 virtual void OnTimerTick() = 0;
28 virtual ~TimeSourceClient() {}
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 virtual void SetClient(TimeSourceClient
* client
);
46 // TimeSource implementation
47 virtual void SetTimebaseAndInterval(base::TimeTicks timebase
,
48 base::TimeDelta interval
);
49 base::TimeDelta
Interval() const { return next_parameters_
.interval
; }
51 virtual base::TimeTicks
SetActive(bool active
);
52 virtual bool Active() const;
54 // Get the last and next tick times. NextTickTime() returns null when
56 virtual base::TimeTicks
LastTickTime() const;
57 virtual base::TimeTicks
NextTickTime() const;
59 // Virtual for testing.
60 virtual base::TimeTicks
Now() const;
62 virtual void AsValueInto(base::trace_event::TracedValue
* dict
) const;
65 DelayBasedTimeSource(base::TimeDelta interval
,
66 base::SingleThreadTaskRunner
* task_runner
);
68 virtual std::string
TypeString() const;
70 base::TimeTicks
NextTickTarget(base::TimeTicks now
);
71 void PostNextTickTask(base::TimeTicks now
);
75 Parameters(base::TimeDelta interval
, base::TimeTicks tick_target
)
76 : interval(interval
), tick_target(tick_target
) {}
77 base::TimeDelta interval
;
78 base::TimeTicks tick_target
;
81 TimeSourceClient
* client_
;
82 base::TimeTicks last_tick_time_
;
84 // current_parameters_ should only be written by PostNextTickTask.
85 // next_parameters_ will take effect on the next call to PostNextTickTask.
86 // Maintaining a pending set of parameters allows NextTickTime() to always
87 // reflect the actual time we expect OnTimerFired to be called.
88 Parameters current_parameters_
;
89 Parameters next_parameters_
;
93 base::SingleThreadTaskRunner
* task_runner_
;
94 base::WeakPtrFactory
<DelayBasedTimeSource
> weak_factory_
;
97 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource
);
102 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_