OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ | 5 #ifndef CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ |
6 #define CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ | 6 #define CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/cancelable_callback.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "cc/base/cc_export.h" | 13 #include "cc/base/cc_export.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace trace_event { | 16 namespace trace_event { |
16 class TracedValue; | 17 class TracedValue; |
17 } | 18 } |
18 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
19 } | 20 } |
20 | 21 |
21 namespace cc { | 22 namespace cc { |
22 | 23 class CC_EXPORT DelayBasedTimeSourceClient { |
23 class CC_EXPORT TimeSourceClient { | |
24 public: | 24 public: |
25 virtual void OnTimerTick() = 0; | 25 virtual void OnTimerTick() = 0; |
26 | 26 |
27 protected: | 27 protected: |
28 virtual ~TimeSourceClient() {} | 28 virtual ~DelayBasedTimeSourceClient() {} |
29 }; | 29 }; |
30 | 30 |
31 // This timer implements a time source that achieves the specified interval | 31 // This timer implements a time source that achieves the specified interval |
32 // in face of millisecond-precision delayed callbacks and random queueing | 32 // in face of millisecond-precision delayed callbacks and random queueing |
33 // delays. DelayBasedTimeSource uses base::TimeTicks::Now as its timebase. | 33 // delays. DelayBasedTimeSource uses base::TimeTicks::Now as its timebase. |
34 class CC_EXPORT DelayBasedTimeSource { | 34 class CC_EXPORT DelayBasedTimeSource { |
35 public: | 35 public: |
36 static scoped_ptr<DelayBasedTimeSource> Create( | 36 static scoped_ptr<DelayBasedTimeSource> Create( |
37 base::TimeDelta interval, | 37 base::TimeDelta interval, |
38 base::SingleThreadTaskRunner* task_runner) { | 38 base::SingleThreadTaskRunner* task_runner) { |
39 return make_scoped_ptr(new DelayBasedTimeSource(interval, task_runner)); | 39 return make_scoped_ptr(new DelayBasedTimeSource(interval, task_runner)); |
40 } | 40 } |
41 | 41 |
42 virtual ~DelayBasedTimeSource(); | 42 virtual ~DelayBasedTimeSource(); |
43 | 43 |
44 virtual void SetClient(TimeSourceClient* client); | 44 void SetClient(DelayBasedTimeSourceClient* client); |
45 | 45 |
46 // TimeSource implementation | 46 void SetTimebaseAndInterval(base::TimeTicks timebase, |
47 virtual void SetTimebaseAndInterval(base::TimeTicks timebase, | 47 base::TimeDelta interval); |
48 base::TimeDelta interval); | |
49 base::TimeDelta Interval() const { return next_parameters_.interval; } | |
50 | 48 |
51 virtual base::TimeTicks SetActive(bool active); | 49 base::TimeDelta Interval() const; |
52 virtual bool Active() const; | 50 |
| 51 // Returns the time for the last missed tick. |
| 52 base::TimeTicks SetActive(bool active); |
| 53 bool Active() const; |
53 | 54 |
54 // Get the last and next tick times. NextTickTime() returns null when | 55 // Get the last and next tick times. NextTickTime() returns null when |
55 // inactive. | 56 // inactive. |
56 virtual base::TimeTicks LastTickTime() const; | 57 base::TimeTicks LastTickTime() const; |
57 virtual base::TimeTicks NextTickTime() const; | 58 base::TimeTicks NextTickTime() const; |
58 | |
59 // Virtual for testing. | |
60 virtual base::TimeTicks Now() const; | |
61 | 59 |
62 virtual void AsValueInto(base::trace_event::TracedValue* dict) const; | 60 virtual void AsValueInto(base::trace_event::TracedValue* dict) const; |
63 | 61 |
64 protected: | 62 protected: |
65 DelayBasedTimeSource(base::TimeDelta interval, | 63 DelayBasedTimeSource(base::TimeDelta interval, |
66 base::SingleThreadTaskRunner* task_runner); | 64 base::SingleThreadTaskRunner* task_runner); |
67 | 65 |
| 66 // Virtual for testing. |
| 67 virtual base::TimeTicks Now() const; |
68 virtual std::string TypeString() const; | 68 virtual std::string TypeString() const; |
69 | 69 |
70 base::TimeTicks NextTickTarget(base::TimeTicks now); | 70 private: |
| 71 base::TimeTicks NextTickTarget(base::TimeTicks now) const; |
| 72 |
71 void PostNextTickTask(base::TimeTicks now); | 73 void PostNextTickTask(base::TimeTicks now); |
72 void OnTimerFired(); | 74 void ResetTickTask(base::TimeTicks now); |
73 | 75 |
74 struct Parameters { | 76 void OnTimerTick(); |
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; | |
79 }; | |
80 | 77 |
81 TimeSourceClient* client_; | 78 DelayBasedTimeSourceClient* client_; |
82 base::TimeTicks last_tick_time_; | |
83 | |
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_; | |
90 | 79 |
91 bool active_; | 80 bool active_; |
92 | 81 |
| 82 base::TimeTicks timebase_; |
| 83 base::TimeDelta interval_; |
| 84 |
| 85 base::TimeTicks last_tick_time_; |
| 86 base::TimeTicks next_tick_time_; |
| 87 |
| 88 base::CancelableClosure tick_closure_; |
| 89 |
93 base::SingleThreadTaskRunner* task_runner_; | 90 base::SingleThreadTaskRunner* task_runner_; |
| 91 |
94 base::WeakPtrFactory<DelayBasedTimeSource> weak_factory_; | 92 base::WeakPtrFactory<DelayBasedTimeSource> weak_factory_; |
95 | 93 |
96 private: | |
97 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource); | 94 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource); |
98 }; | 95 }; |
99 | 96 |
100 } // namespace cc | 97 } // namespace cc |
101 | 98 |
102 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ | 99 #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ |
OLD | NEW |