summaryrefslogtreecommitdiffstats
path: root/cc/delay_based_time_source.h
blob: a9229c3f0cc968de83d3ff7916721ded6e0be0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CCDelayBasedTimeSource_h
#define CCDelayBasedTimeSource_h

#include "base/memory/weak_ptr.h"
#include "cc/time_source.h"

namespace cc {

// This timer implements a time source that achieves the specified interval
// in face of millisecond-precision delayed callbacks and random queueing delays.
class DelayBasedTimeSource : public TimeSource {
public:
    static scoped_refptr<DelayBasedTimeSource> create(base::TimeDelta interval, Thread* thread);

    virtual void setClient(TimeSourceClient* client) OVERRIDE;

    // TimeSource implementation
    virtual void setTimebaseAndInterval(base::TimeTicks timebase, base::TimeDelta interval) OVERRIDE;

    virtual void setActive(bool) OVERRIDE;
    virtual bool active() const OVERRIDE;

    // Get the last and next tick times. nextTimeTime() returns null when
    // inactive.
    virtual base::TimeTicks lastTickTime() OVERRIDE;
    virtual base::TimeTicks nextTickTime() OVERRIDE;


    // Virtual for testing.
    virtual base::TimeTicks now() const;

protected:
    DelayBasedTimeSource(base::TimeDelta interval, Thread* thread);
    virtual ~DelayBasedTimeSource();

    base::TimeTicks nextTickTarget(base::TimeTicks now);
    void postNextTickTask(base::TimeTicks now);
    void onTimerFired();

    enum State {
        STATE_INACTIVE,
        STATE_STARTING,
        STATE_ACTIVE,
    };

    struct Parameters {
        Parameters(base::TimeDelta interval, base::TimeTicks tickTarget)
            : interval(interval), tickTarget(tickTarget)
        { }
        base::TimeDelta interval;
        base::TimeTicks tickTarget;
    };

    TimeSourceClient* m_client;
    bool m_hasTickTarget;
    base::TimeTicks m_lastTickTime;

    // m_currentParameters should only be written by postNextTickTask.
    // m_nextParameters will take effect on the next call to postNextTickTask.
    // Maintaining a pending set of parameters allows nextTickTime() to always
    // reflect the actual time we expect onTimerFired to be called.
    Parameters m_currentParameters;
    Parameters m_nextParameters;

    State m_state;

    Thread* m_thread;
    base::WeakPtrFactory<DelayBasedTimeSource> m_weakFactory;
    DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource);
};

}  // namespace cc

#endif // CCDelayBasedTimeSource_h