[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
[email protected] | 0fb2500 | 2012-10-12 07:20:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 4 | |
[email protected] | 8fcbaa37 | 2012-11-05 04:12:41 | [diff] [blame] | 5 | #ifndef CC_DELAY_BASED_TIME_SOURCE_H_ |
6 | #define CC_DELAY_BASED_TIME_SOURCE_H_ | ||||
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 7 | |
[email protected] | 4a048891 | 2012-10-30 23:29:50 | [diff] [blame] | 8 | #include "base/memory/weak_ptr.h" |
[email protected] | 52347c84 | 2012-11-02 21:06:20 | [diff] [blame] | 9 | #include "cc/cc_export.h" |
[email protected] | da2c912 | 2012-10-20 23:13:06 | [diff] [blame] | 10 | #include "cc/time_source.h" |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 11 | |
12 | namespace cc { | ||||
13 | |||||
[email protected] | a8e69ca | 2012-11-16 22:10:19 | [diff] [blame] | 14 | class Thread; |
15 | |||||
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 16 | // This timer implements a time source that achieves the specified interval |
17 | // in face of millisecond-precision delayed callbacks and random queueing delays. | ||||
[email protected] | 52347c84 | 2012-11-02 21:06:20 | [diff] [blame] | 18 | class CC_EXPORT DelayBasedTimeSource : public TimeSource { |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 19 | public: |
[email protected] | 4a048891 | 2012-10-30 23:29:50 | [diff] [blame] | 20 | static scoped_refptr<DelayBasedTimeSource> create(base::TimeDelta interval, Thread* thread); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 21 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 22 | virtual void setClient(TimeSourceClient* client) OVERRIDE; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 23 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 24 | // TimeSource implementation |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 25 | virtual void setTimebaseAndInterval(base::TimeTicks timebase, base::TimeDelta interval) OVERRIDE; |
26 | |||||
27 | virtual void setActive(bool) OVERRIDE; | ||||
28 | virtual bool active() const OVERRIDE; | ||||
29 | |||||
30 | // Get the last and next tick times. nextTimeTime() returns null when | ||||
31 | // inactive. | ||||
32 | virtual base::TimeTicks lastTickTime() OVERRIDE; | ||||
33 | virtual base::TimeTicks nextTickTime() OVERRIDE; | ||||
34 | |||||
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 35 | |
36 | // Virtual for testing. | ||||
37 | virtual base::TimeTicks now() const; | ||||
38 | |||||
39 | protected: | ||||
[email protected] | 4a048891 | 2012-10-30 23:29:50 | [diff] [blame] | 40 | DelayBasedTimeSource(base::TimeDelta interval, Thread* thread); |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 41 | virtual ~DelayBasedTimeSource(); |
[email protected] | 357b517 | 2012-10-18 17:39:33 | [diff] [blame] | 42 | |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 43 | base::TimeTicks nextTickTarget(base::TimeTicks now); |
44 | void postNextTickTask(base::TimeTicks now); | ||||
[email protected] | 4a048891 | 2012-10-30 23:29:50 | [diff] [blame] | 45 | void onTimerFired(); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 46 | |
47 | enum State { | ||||
48 | STATE_INACTIVE, | ||||
49 | STATE_STARTING, | ||||
50 | STATE_ACTIVE, | ||||
51 | }; | ||||
52 | |||||
53 | struct Parameters { | ||||
54 | Parameters(base::TimeDelta interval, base::TimeTicks tickTarget) | ||||
55 | : interval(interval), tickTarget(tickTarget) | ||||
56 | { } | ||||
57 | base::TimeDelta interval; | ||||
58 | base::TimeTicks tickTarget; | ||||
59 | }; | ||||
60 | |||||
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 61 | TimeSourceClient* m_client; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 62 | bool m_hasTickTarget; |
63 | base::TimeTicks m_lastTickTime; | ||||
64 | |||||
65 | // m_currentParameters should only be written by postNextTickTask. | ||||
66 | // m_nextParameters will take effect on the next call to postNextTickTask. | ||||
67 | // Maintaining a pending set of parameters allows nextTickTime() to always | ||||
68 | // reflect the actual time we expect onTimerFired to be called. | ||||
69 | Parameters m_currentParameters; | ||||
70 | Parameters m_nextParameters; | ||||
71 | |||||
72 | State m_state; | ||||
[email protected] | 4a048891 | 2012-10-30 23:29:50 | [diff] [blame] | 73 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 74 | Thread* m_thread; |
[email protected] | 4a048891 | 2012-10-30 23:29:50 | [diff] [blame] | 75 | base::WeakPtrFactory<DelayBasedTimeSource> m_weakFactory; |
76 | DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource); | ||||
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 77 | }; |
78 | |||||
[email protected] | 2d86b92 | 2012-10-13 16:57:47 | [diff] [blame] | 79 | } // namespace cc |
80 | |||||
[email protected] | 8fcbaa37 | 2012-11-05 04:12:41 | [diff] [blame] | 81 | #endif // CC_DELAY_BASED_TIME_SOURCE_H_ |