Telemetry: More elegant histogram printing.
[chromium-blink-merge.git] / cc / frame_rate_controller.cc
blob436e80dd52bb346006eb9609848079b5411e92e4
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.
5 #include "cc/frame_rate_controller.h"
7 #include "base/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "cc/delay_based_time_source.h"
10 #include "cc/time_source.h"
11 #include "cc/thread.h"
13 namespace {
15 // This will be the maximum number of pending frames unless
16 // FrameRateController::setMaxFramesPending is called.
17 const int defaultMaxFramesPending = 2;
19 } // namespace
21 namespace cc {
23 class FrameRateControllerTimeSourceAdapter : public TimeSourceClient {
24 public:
25 static scoped_ptr<FrameRateControllerTimeSourceAdapter> create(FrameRateController* frameRateController) {
26 return make_scoped_ptr(new FrameRateControllerTimeSourceAdapter(frameRateController));
28 virtual ~FrameRateControllerTimeSourceAdapter() {}
30 virtual void onTimerTick() OVERRIDE {
31 m_frameRateController->onTimerTick();
34 private:
35 explicit FrameRateControllerTimeSourceAdapter(FrameRateController* frameRateController)
36 : m_frameRateController(frameRateController) {}
38 FrameRateController* m_frameRateController;
41 FrameRateController::FrameRateController(scoped_refptr<TimeSource> timer)
42 : m_client(0)
43 , m_numFramesPending(0)
44 , m_maxFramesPending(defaultMaxFramesPending)
45 , m_timeSource(timer)
46 , m_active(false)
47 , m_swapBuffersCompleteSupported(true)
48 , m_isTimeSourceThrottling(true)
49 , m_thread(0)
50 , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this))
52 m_timeSourceClientAdapter = FrameRateControllerTimeSourceAdapter::create(this);
53 m_timeSource->setClient(m_timeSourceClientAdapter.get());
56 FrameRateController::FrameRateController(Thread* thread)
57 : m_client(0)
58 , m_numFramesPending(0)
59 , m_maxFramesPending(defaultMaxFramesPending)
60 , m_active(false)
61 , m_swapBuffersCompleteSupported(true)
62 , m_isTimeSourceThrottling(false)
63 , m_thread(thread)
64 , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this))
68 FrameRateController::~FrameRateController()
70 if (m_isTimeSourceThrottling)
71 m_timeSource->setActive(false);
74 void FrameRateController::setActive(bool active)
76 if (m_active == active)
77 return;
78 TRACE_EVENT1("cc", "FrameRateController::setActive", "active", active);
79 m_active = active;
81 if (m_isTimeSourceThrottling)
82 m_timeSource->setActive(active);
83 else {
84 if (active)
85 postManualTick();
86 else
87 m_weakFactory.InvalidateWeakPtrs();
91 void FrameRateController::setMaxFramesPending(int maxFramesPending)
93 DCHECK(maxFramesPending > 0);
94 m_maxFramesPending = maxFramesPending;
97 void FrameRateController::setTimebaseAndInterval(base::TimeTicks timebase, base::TimeDelta interval)
99 if (m_isTimeSourceThrottling)
100 m_timeSource->setTimebaseAndInterval(timebase, interval);
103 void FrameRateController::setSwapBuffersCompleteSupported(bool supported)
105 m_swapBuffersCompleteSupported = supported;
108 void FrameRateController::onTimerTick()
110 DCHECK(m_active);
112 // Check if we have too many frames in flight.
113 bool throttled = m_numFramesPending >= m_maxFramesPending;
114 TRACE_COUNTER_ID1("cc", "ThrottledVSyncInterval", m_thread, throttled);
116 if (m_client)
117 m_client->vsyncTick(throttled);
119 if (m_swapBuffersCompleteSupported && !m_isTimeSourceThrottling && m_numFramesPending < m_maxFramesPending)
120 postManualTick();
123 void FrameRateController::postManualTick()
125 if (m_active)
126 m_thread->postTask(base::Bind(&FrameRateController::manualTick, m_weakFactory.GetWeakPtr()));
129 void FrameRateController::manualTick()
131 onTimerTick();
134 void FrameRateController::didBeginFrame()
136 if (m_swapBuffersCompleteSupported)
137 m_numFramesPending++;
138 else if (!m_isTimeSourceThrottling)
139 postManualTick();
142 void FrameRateController::didFinishFrame()
144 DCHECK(m_swapBuffersCompleteSupported);
146 m_numFramesPending--;
147 if (!m_isTimeSourceThrottling)
148 postManualTick();
151 void FrameRateController::didAbortAllPendingFrames()
153 m_numFramesPending = 0;
156 base::TimeTicks FrameRateController::nextTickTime()
158 if (m_isTimeSourceThrottling)
159 return m_timeSource->nextTickTime();
161 return base::TimeTicks();
164 } // namespace cc