[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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. | ||||
4 | |||||
5 | #ifndef CCDelayBasedTimeSource_h | ||||
6 | #define CCDelayBasedTimeSource_h | ||||
7 | |||||
8 | #include "CCTimeSource.h" | ||||
9 | #include "CCTimer.h" | ||||
10 | #include <wtf/PassRefPtr.h> | ||||
11 | |||||
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 12 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 13 | |
14 | class CCThread; | ||||
15 | |||||
16 | // This timer implements a time source that achieves the specified interval | ||||
17 | // in face of millisecond-precision delayed callbacks and random queueing delays. | ||||
18 | class CCDelayBasedTimeSource : public CCTimeSource, CCTimerClient { | ||||
19 | public: | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 20 | static PassRefPtr<CCDelayBasedTimeSource> create(base::TimeDelta interval, CCThread*); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 21 | |
[email protected] | 7648159 | 2012-09-21 16:47:06 | [diff] [blame] | 22 | virtual ~CCDelayBasedTimeSource(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 23 | |
[email protected] | 7648159 | 2012-09-21 16:47:06 | [diff] [blame] | 24 | virtual void setClient(CCTimeSourceClient* client) OVERRIDE; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 25 | |
26 | // CCTimeSource implementation | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 27 | virtual void setTimebaseAndInterval(base::TimeTicks timebase, base::TimeDelta interval) OVERRIDE; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 28 | |
29 | virtual void setActive(bool) OVERRIDE; | ||||
[email protected] | 7648159 | 2012-09-21 16:47:06 | [diff] [blame] | 30 | virtual bool active() const OVERRIDE; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 31 | |
[email protected] | e8e410d | 2012-09-28 01:47:01 | [diff] [blame] | 32 | // Get the last and next tick times. nextTimeTime() returns null when |
33 | // inactive. | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 34 | virtual base::TimeTicks lastTickTime() OVERRIDE; |
[email protected] | e8e410d | 2012-09-28 01:47:01 | [diff] [blame] | 35 | virtual base::TimeTicks nextTickTime() OVERRIDE; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 36 | |
37 | // CCTimerClient implementation. | ||||
38 | virtual void onTimerFired() OVERRIDE; | ||||
39 | |||||
40 | // Virtual for testing. | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 41 | virtual base::TimeTicks now() const; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 42 | |
43 | protected: | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 44 | CCDelayBasedTimeSource(base::TimeDelta interval, CCThread*); |
45 | base::TimeTicks nextTickTarget(base::TimeTicks now); | ||||
46 | void postNextTickTask(base::TimeTicks now); | ||||
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 47 | |
48 | enum State { | ||||
49 | STATE_INACTIVE, | ||||
50 | STATE_STARTING, | ||||
51 | STATE_ACTIVE, | ||||
52 | }; | ||||
53 | |||||
54 | struct Parameters { | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 55 | Parameters(base::TimeDelta interval, base::TimeTicks tickTarget) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 56 | : interval(interval), tickTarget(tickTarget) |
57 | { } | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 58 | base::TimeDelta interval; |
59 | base::TimeTicks tickTarget; | ||||
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 60 | }; |
61 | |||||
62 | CCTimeSourceClient* m_client; | ||||
63 | bool m_hasTickTarget; | ||||
[email protected] | 4481ddb62 | 2012-09-20 16:33:47 | [diff] [blame] | 64 | base::TimeTicks m_lastTickTime; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 65 | |
66 | // m_currentParameters should only be written by postNextTickTask. | ||||
67 | // m_nextParameters will take effect on the next call to postNextTickTask. | ||||
68 | // Maintaining a pending set of parameters allows nextTickTime() to always | ||||
69 | // reflect the actual time we expect onTimerFired to be called. | ||||
70 | Parameters m_currentParameters; | ||||
71 | Parameters m_nextParameters; | ||||
72 | |||||
73 | State m_state; | ||||
74 | CCThread* m_thread; | ||||
75 | CCTimer m_timer; | ||||
76 | }; | ||||
77 | |||||
78 | } | ||||
79 | #endif // CCDelayBasedTimeSource_h |