Roll Libjingle 8093:8097
[chromium-blink-merge.git] / cc / scheduler / delay_based_time_source.h
blob9f670d1f8ff05e77eaad4b9c54b4a6d55fc3cfcd
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_
8 #include <string>
10 #include "base/memory/weak_ptr.h"
11 #include "base/values.h"
12 #include "cc/base/cc_export.h"
14 namespace base {
15 namespace debug {
16 class TracedValue;
18 class SingleThreadTaskRunner;
21 namespace cc {
23 class CC_EXPORT TimeSourceClient {
24 public:
25 virtual void OnTimerTick() = 0;
27 protected:
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> {
36 public:
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
51 // inactive.
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::debug::TracedValue* dict) const;
60 protected:
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);
69 void OnTimerFired();
71 struct Parameters {
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_;
88 bool active_;
90 base::SingleThreadTaskRunner* task_runner_;
91 base::WeakPtrFactory<DelayBasedTimeSource> weak_factory_;
93 private:
94 friend class base::RefCounted<DelayBasedTimeSource>;
95 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource);
98 // DelayBasedTimeSource uses base::TimeTicks::HighResNow as its timebase.
99 class DelayBasedTimeSourceHighRes : public DelayBasedTimeSource {
100 public:
101 static scoped_refptr<DelayBasedTimeSourceHighRes> Create(
102 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner);
104 base::TimeTicks Now() const override;
106 protected:
107 DelayBasedTimeSourceHighRes(base::TimeDelta interval,
108 base::SingleThreadTaskRunner* task_runner);
109 ~DelayBasedTimeSourceHighRes() override;
111 std::string TypeString() const override;
113 private:
114 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSourceHighRes);
117 } // namespace cc
119 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_