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
35 : public base::RefCounted
<DelayBasedTimeSource
> {
37 static scoped_refptr
<DelayBasedTimeSource
> Create(
38 base::TimeDelta interval
, base::SingleThreadTaskRunner
* task_runner
);
40 virtual void SetClient(TimeSourceClient
* client
);
42 // TimeSource implementation
43 virtual void SetTimebaseAndInterval(base::TimeTicks timebase
,
44 base::TimeDelta interval
);
45 base::TimeDelta
Interval() const { return next_parameters_
.interval
; }
47 virtual base::TimeTicks
SetActive(bool active
);
48 virtual bool Active() const;
50 // Get the last and next tick times. NextTickTime() returns null when
52 virtual base::TimeTicks
LastTickTime() const;
53 virtual base::TimeTicks
NextTickTime() const;
55 // Virtual for testing.
56 virtual base::TimeTicks
Now() const;
58 virtual void AsValueInto(base::trace_event::TracedValue
* dict
) const;
61 DelayBasedTimeSource(base::TimeDelta interval
,
62 base::SingleThreadTaskRunner
* task_runner
);
63 virtual ~DelayBasedTimeSource();
65 virtual std::string
TypeString() const;
67 base::TimeTicks
NextTickTarget(base::TimeTicks now
);
68 void PostNextTickTask(base::TimeTicks now
);
72 Parameters(base::TimeDelta interval
, base::TimeTicks tick_target
)
73 : interval(interval
), tick_target(tick_target
) {}
74 base::TimeDelta interval
;
75 base::TimeTicks tick_target
;
78 TimeSourceClient
* client_
;
79 base::TimeTicks last_tick_time_
;
81 // current_parameters_ should only be written by PostNextTickTask.
82 // next_parameters_ will take effect on the next call to PostNextTickTask.
83 // Maintaining a pending set of parameters allows NextTickTime() to always
84 // reflect the actual time we expect OnTimerFired to be called.
85 Parameters current_parameters_
;
86 Parameters next_parameters_
;
90 base::SingleThreadTaskRunner
* task_runner_
;
91 base::WeakPtrFactory
<DelayBasedTimeSource
> weak_factory_
;
94 friend class base::RefCounted
<DelayBasedTimeSource
>;
95 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource
);
98 // DelayBasedTimeSource that once used base::TimeTicks::HighResNow as its time
99 // source, but is now a no-op.
100 // TODO(brianderson): Remove along with gfx::/FrameTime.http://crbug.com/447329
101 class DelayBasedTimeSourceHighRes
: public DelayBasedTimeSource
{
103 static scoped_refptr
<DelayBasedTimeSourceHighRes
> Create(
104 base::TimeDelta interval
, base::SingleThreadTaskRunner
* task_runner
);
106 base::TimeTicks
Now() const override
;
109 DelayBasedTimeSourceHighRes(base::TimeDelta interval
,
110 base::SingleThreadTaskRunner
* task_runner
);
111 ~DelayBasedTimeSourceHighRes() override
;
113 std::string
TypeString() const override
;
116 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSourceHighRes
);
121 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_