blob: bf183f7aa7313e2ffd2131829f77e6a7d83b773a [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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]9c88e562012-09-14 22:21:3012namespace cc {
[email protected]94f206c12012-08-25 00:09:1413
14class 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.
18class CCDelayBasedTimeSource : public CCTimeSource, CCTimerClient {
19public:
[email protected]4481ddb622012-09-20 16:33:4720 static PassRefPtr<CCDelayBasedTimeSource> create(base::TimeDelta interval, CCThread*);
[email protected]94f206c12012-08-25 00:09:1421
[email protected]76481592012-09-21 16:47:0622 virtual ~CCDelayBasedTimeSource();
[email protected]94f206c12012-08-25 00:09:1423
[email protected]76481592012-09-21 16:47:0624 virtual void setClient(CCTimeSourceClient* client) OVERRIDE;
[email protected]94f206c12012-08-25 00:09:1425
26 // CCTimeSource implementation
[email protected]4481ddb622012-09-20 16:33:4727 virtual void setTimebaseAndInterval(base::TimeTicks timebase, base::TimeDelta interval) OVERRIDE;
[email protected]94f206c12012-08-25 00:09:1428
29 virtual void setActive(bool) OVERRIDE;
[email protected]76481592012-09-21 16:47:0630 virtual bool active() const OVERRIDE;
[email protected]94f206c12012-08-25 00:09:1431
[email protected]e8e410d2012-09-28 01:47:0132 // Get the last and next tick times. nextTimeTime() returns null when
33 // inactive.
[email protected]4481ddb622012-09-20 16:33:4734 virtual base::TimeTicks lastTickTime() OVERRIDE;
[email protected]e8e410d2012-09-28 01:47:0135 virtual base::TimeTicks nextTickTime() OVERRIDE;
[email protected]94f206c12012-08-25 00:09:1436
37 // CCTimerClient implementation.
38 virtual void onTimerFired() OVERRIDE;
39
40 // Virtual for testing.
[email protected]4481ddb622012-09-20 16:33:4741 virtual base::TimeTicks now() const;
[email protected]94f206c12012-08-25 00:09:1442
43protected:
[email protected]4481ddb622012-09-20 16:33:4744 CCDelayBasedTimeSource(base::TimeDelta interval, CCThread*);
45 base::TimeTicks nextTickTarget(base::TimeTicks now);
46 void postNextTickTask(base::TimeTicks now);
[email protected]94f206c12012-08-25 00:09:1447
48 enum State {
49 STATE_INACTIVE,
50 STATE_STARTING,
51 STATE_ACTIVE,
52 };
53
54 struct Parameters {
[email protected]4481ddb622012-09-20 16:33:4755 Parameters(base::TimeDelta interval, base::TimeTicks tickTarget)
[email protected]94f206c12012-08-25 00:09:1456 : interval(interval), tickTarget(tickTarget)
57 { }
[email protected]4481ddb622012-09-20 16:33:4758 base::TimeDelta interval;
59 base::TimeTicks tickTarget;
[email protected]94f206c12012-08-25 00:09:1460 };
61
62 CCTimeSourceClient* m_client;
63 bool m_hasTickTarget;
[email protected]4481ddb622012-09-20 16:33:4764 base::TimeTicks m_lastTickTime;
[email protected]94f206c12012-08-25 00:09:1465
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