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"
14 namespace base
{ class SingleThreadTaskRunner
; }
18 class CC_EXPORT TimeSourceClient
{
20 virtual void OnTimerTick() = 0;
23 virtual ~TimeSourceClient() {}
26 // This timer implements a time source that achieves the specified interval
27 // in face of millisecond-precision delayed callbacks and random queueing
28 // delays. DelayBasedTimeSource uses base::TimeTicks::Now as its timebase.
29 class CC_EXPORT DelayBasedTimeSource
30 : public base::RefCounted
<DelayBasedTimeSource
> {
32 static scoped_refptr
<DelayBasedTimeSource
> Create(
33 base::TimeDelta interval
, base::SingleThreadTaskRunner
* task_runner
);
35 virtual void SetClient(TimeSourceClient
* client
);
37 // TimeSource implementation
38 virtual void SetTimebaseAndInterval(base::TimeTicks timebase
,
39 base::TimeDelta interval
);
41 virtual base::TimeTicks
SetActive(bool active
);
42 virtual bool Active() const;
44 // Get the last and next tick times. NextTickTime() returns null when
46 virtual base::TimeTicks
LastTickTime() const;
47 virtual base::TimeTicks
NextTickTime() const;
49 // Virtual for testing.
50 virtual base::TimeTicks
Now() const;
52 virtual scoped_ptr
<base::Value
> AsValue() const;
55 DelayBasedTimeSource(base::TimeDelta interval
,
56 base::SingleThreadTaskRunner
* task_runner
);
57 virtual ~DelayBasedTimeSource();
59 virtual std::string
TypeString() const;
61 base::TimeTicks
NextTickTarget(base::TimeTicks now
);
62 void PostNextTickTask(base::TimeTicks now
);
66 Parameters(base::TimeDelta interval
, base::TimeTicks tick_target
)
67 : interval(interval
), tick_target(tick_target
) {}
68 base::TimeDelta interval
;
69 base::TimeTicks tick_target
;
72 TimeSourceClient
* client_
;
73 base::TimeTicks last_tick_time_
;
75 // current_parameters_ should only be written by PostNextTickTask.
76 // next_parameters_ will take effect on the next call to PostNextTickTask.
77 // Maintaining a pending set of parameters allows NextTickTime() to always
78 // reflect the actual time we expect OnTimerFired to be called.
79 Parameters current_parameters_
;
80 Parameters next_parameters_
;
84 base::SingleThreadTaskRunner
* task_runner_
;
85 base::WeakPtrFactory
<DelayBasedTimeSource
> weak_factory_
;
88 friend class base::RefCounted
<DelayBasedTimeSource
>;
89 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource
);
92 // DelayBasedTimeSource uses base::TimeTicks::HighResNow as its timebase.
93 class DelayBasedTimeSourceHighRes
: public DelayBasedTimeSource
{
95 static scoped_refptr
<DelayBasedTimeSourceHighRes
> Create(
96 base::TimeDelta interval
, base::SingleThreadTaskRunner
* task_runner
);
98 virtual base::TimeTicks
Now() const OVERRIDE
;
101 DelayBasedTimeSourceHighRes(base::TimeDelta interval
,
102 base::SingleThreadTaskRunner
* task_runner
);
103 virtual ~DelayBasedTimeSourceHighRes();
105 virtual std::string
TypeString() const OVERRIDE
;
108 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSourceHighRes
);
113 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_