blob: 516fde9c56a26e3a9e3360a31bc2786d761e98df [file] [log] [blame]
[email protected]cd57cc5a2012-10-12 22:43:411// Copyright 2011 The Chromium Authors. All rights reserved.
[email protected]0fb25002012-10-12 07:20:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]cd57cc5a2012-10-12 22:43:414
[email protected]8fcbaa372012-11-05 04:12:415#ifndef CC_DELAY_BASED_TIME_SOURCE_H_
6#define CC_DELAY_BASED_TIME_SOURCE_H_
[email protected]cd57cc5a2012-10-12 22:43:417
[email protected]4a0488912012-10-30 23:29:508#include "base/memory/weak_ptr.h"
[email protected]52347c842012-11-02 21:06:209#include "cc/cc_export.h"
[email protected]da2c9122012-10-20 23:13:0610#include "cc/time_source.h"
[email protected]cd57cc5a2012-10-12 22:43:4111
12namespace cc {
13
[email protected]a8e69ca2012-11-16 22:10:1914class Thread;
15
[email protected]cd57cc5a2012-10-12 22:43:4116// 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]52347c842012-11-02 21:06:2018class CC_EXPORT DelayBasedTimeSource : public TimeSource {
[email protected]cd57cc5a2012-10-12 22:43:4119public:
[email protected]4a0488912012-10-30 23:29:5020 static scoped_refptr<DelayBasedTimeSource> create(base::TimeDelta interval, Thread* thread);
[email protected]cd57cc5a2012-10-12 22:43:4121
[email protected]96baf3e2012-10-22 23:09:5522 virtual void setClient(TimeSourceClient* client) OVERRIDE;
[email protected]cd57cc5a2012-10-12 22:43:4123
[email protected]96baf3e2012-10-22 23:09:5524 // TimeSource implementation
[email protected]cd57cc5a2012-10-12 22:43:4125 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]cd57cc5a2012-10-12 22:43:4135
36 // Virtual for testing.
37 virtual base::TimeTicks now() const;
38
39protected:
[email protected]4a0488912012-10-30 23:29:5040 DelayBasedTimeSource(base::TimeDelta interval, Thread* thread);
[email protected]96baf3e2012-10-22 23:09:5541 virtual ~DelayBasedTimeSource();
[email protected]357b5172012-10-18 17:39:3342
[email protected]cd57cc5a2012-10-12 22:43:4143 base::TimeTicks nextTickTarget(base::TimeTicks now);
44 void postNextTickTask(base::TimeTicks now);
[email protected]4a0488912012-10-30 23:29:5045 void onTimerFired();
[email protected]cd57cc5a2012-10-12 22:43:4146
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]96baf3e2012-10-22 23:09:5561 TimeSourceClient* m_client;
[email protected]cd57cc5a2012-10-12 22:43:4162 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]4a0488912012-10-30 23:29:5073
[email protected]96baf3e2012-10-22 23:09:5574 Thread* m_thread;
[email protected]4a0488912012-10-30 23:29:5075 base::WeakPtrFactory<DelayBasedTimeSource> m_weakFactory;
76 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource);
[email protected]cd57cc5a2012-10-12 22:43:4177};
78
[email protected]2d86b922012-10-13 16:57:4779} // namespace cc
80
[email protected]8fcbaa372012-11-05 04:12:4181#endif // CC_DELAY_BASED_TIME_SOURCE_H_