summaryrefslogtreecommitdiffstats
path: root/cc/frame_rate_controller.cc
blob: 46000fc19552067364546c0a06994e8962407235 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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.

#include "config.h"

#include "cc/frame_rate_controller.h"

#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "cc/delay_based_time_source.h"
#include "cc/time_source.h"

namespace {

// This will be the maximum number of pending frames unless
// FrameRateController::setMaxFramesPending is called.
const int defaultMaxFramesPending = 2;

}  // namespace

namespace cc {

class FrameRateControllerTimeSourceAdapter : public TimeSourceClient {
public:
    static scoped_ptr<FrameRateControllerTimeSourceAdapter> create(FrameRateController* frameRateController) {
        return make_scoped_ptr(new FrameRateControllerTimeSourceAdapter(frameRateController));
    }
    virtual ~FrameRateControllerTimeSourceAdapter() {}

    virtual void onTimerTick() OVERRIDE {
      m_frameRateController->onTimerTick();
    }

private:
    explicit FrameRateControllerTimeSourceAdapter(FrameRateController* frameRateController)
        : m_frameRateController(frameRateController) {}

    FrameRateController* m_frameRateController;
};

FrameRateController::FrameRateController(scoped_refptr<TimeSource> timer)
    : m_client(0)
    , m_numFramesPending(0)
    , m_maxFramesPending(defaultMaxFramesPending)
    , m_timeSource(timer)
    , m_active(false)
    , m_swapBuffersCompleteSupported(true)
    , m_isTimeSourceThrottling(true)
{
    m_timeSourceClientAdapter = FrameRateControllerTimeSourceAdapter::create(this);
    m_timeSource->setClient(m_timeSourceClientAdapter.get());
}

FrameRateController::FrameRateController(Thread* thread)
    : m_client(0)
    , m_numFramesPending(0)
    , m_maxFramesPending(defaultMaxFramesPending)
    , m_active(false)
    , m_swapBuffersCompleteSupported(true)
    , m_isTimeSourceThrottling(false)
    , m_manualTicker(new Timer(thread, this))
{
}

FrameRateController::~FrameRateController()
{
    if (m_isTimeSourceThrottling)
        m_timeSource->setActive(false);
}

void FrameRateController::setActive(bool active)
{
    if (m_active == active)
        return;
    TRACE_EVENT1("cc", "FrameRateController::setActive", "active", active);
    m_active = active;

    if (m_isTimeSourceThrottling)
        m_timeSource->setActive(active);
    else {
        if (active)
            postManualTick();
        else
            m_manualTicker->stop();
    }
}

void FrameRateController::setMaxFramesPending(int maxFramesPending)
{
    DCHECK(maxFramesPending > 0);
    m_maxFramesPending = maxFramesPending;
}

void FrameRateController::setTimebaseAndInterval(base::TimeTicks timebase, base::TimeDelta interval)
{
    if (m_isTimeSourceThrottling)
        m_timeSource->setTimebaseAndInterval(timebase, interval);
}

void FrameRateController::setSwapBuffersCompleteSupported(bool supported)
{
    m_swapBuffersCompleteSupported = supported;
}

void FrameRateController::onTimerTick()
{
    DCHECK(m_active);

    // Check if we have too many frames in flight.
    bool throttled = m_numFramesPending >= m_maxFramesPending;

    if (m_client)
        m_client->vsyncTick(throttled);

    if (m_swapBuffersCompleteSupported && !m_isTimeSourceThrottling && m_numFramesPending < m_maxFramesPending)
        postManualTick();
}

void FrameRateController::postManualTick()
{
    if (m_active)
        m_manualTicker->startOneShot(0);
}

void FrameRateController::onTimerFired()
{
    onTimerTick();
}

void FrameRateController::didBeginFrame()
{
    if (m_swapBuffersCompleteSupported)
        m_numFramesPending++;
    else if (!m_isTimeSourceThrottling)
        postManualTick();
}

void FrameRateController::didFinishFrame()
{
    DCHECK(m_swapBuffersCompleteSupported);

    m_numFramesPending--;
    if (!m_isTimeSourceThrottling)
        postManualTick();
}

void FrameRateController::didAbortAllPendingFrames()
{
    m_numFramesPending = 0;
}

base::TimeTicks FrameRateController::nextTickTime()
{
    if (m_isTimeSourceThrottling)
        return m_timeSource->nextTickTime();

    return base::TimeTicks();
}

}