OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "config.h" | |
6 | |
7 #include "CCFrameRateController.h" | |
8 | |
9 #include "CCDelayBasedTimeSource.h" | |
10 #include "CCTimeSource.h" | |
11 #include "TraceEvent.h" | |
12 #include <wtf/CurrentTime.h> | |
13 | |
14 namespace { | |
15 | |
16 // This will be the maximum number of pending frames unless | |
17 // CCFrameRateController::setMaxFramesPending is called. | |
18 const int defaultMaxFramesPending = 2; | |
19 | |
20 } // namespace | |
21 | |
22 namespace cc { | |
23 | |
24 class CCFrameRateControllerTimeSourceAdapter : public CCTimeSourceClient { | |
25 public: | |
26 static scoped_ptr<CCFrameRateControllerTimeSourceAdapter> create(CCFrameRate
Controller* frameRateController) { | |
27 return make_scoped_ptr(new CCFrameRateControllerTimeSourceAdapter(frameR
ateController)); | |
28 } | |
29 virtual ~CCFrameRateControllerTimeSourceAdapter() {} | |
30 | |
31 virtual void onTimerTick() OVERRIDE { | |
32 m_frameRateController->onTimerTick(); | |
33 } | |
34 | |
35 private: | |
36 explicit CCFrameRateControllerTimeSourceAdapter(CCFrameRateController* frame
RateController) | |
37 : m_frameRateController(frameRateController) {} | |
38 | |
39 CCFrameRateController* m_frameRateController; | |
40 }; | |
41 | |
42 CCFrameRateController::CCFrameRateController(PassRefPtr<CCTimeSource> timer) | |
43 : m_client(0) | |
44 , m_numFramesPending(0) | |
45 , m_maxFramesPending(defaultMaxFramesPending) | |
46 , m_timeSource(timer) | |
47 , m_active(false) | |
48 , m_swapBuffersCompleteSupported(true) | |
49 , m_isTimeSourceThrottling(true) | |
50 { | |
51 m_timeSourceClientAdapter = CCFrameRateControllerTimeSourceAdapter::create(t
his); | |
52 m_timeSource->setClient(m_timeSourceClientAdapter.get()); | |
53 } | |
54 | |
55 CCFrameRateController::CCFrameRateController(CCThread* thread) | |
56 : m_client(0) | |
57 , m_numFramesPending(0) | |
58 , m_maxFramesPending(defaultMaxFramesPending) | |
59 , m_active(false) | |
60 , m_swapBuffersCompleteSupported(true) | |
61 , m_isTimeSourceThrottling(false) | |
62 , m_manualTicker(new CCTimer(thread, this)) | |
63 { | |
64 } | |
65 | |
66 CCFrameRateController::~CCFrameRateController() | |
67 { | |
68 if (m_isTimeSourceThrottling) | |
69 m_timeSource->setActive(false); | |
70 } | |
71 | |
72 void CCFrameRateController::setActive(bool active) | |
73 { | |
74 if (m_active == active) | |
75 return; | |
76 TRACE_EVENT1("cc", "CCFrameRateController::setActive", "active", active); | |
77 m_active = active; | |
78 | |
79 if (m_isTimeSourceThrottling) | |
80 m_timeSource->setActive(active); | |
81 else { | |
82 if (active) | |
83 postManualTick(); | |
84 else | |
85 m_manualTicker->stop(); | |
86 } | |
87 } | |
88 | |
89 void CCFrameRateController::setMaxFramesPending(int maxFramesPending) | |
90 { | |
91 ASSERT(maxFramesPending > 0); | |
92 m_maxFramesPending = maxFramesPending; | |
93 } | |
94 | |
95 void CCFrameRateController::setTimebaseAndInterval(base::TimeTicks timebase, bas
e::TimeDelta interval) | |
96 { | |
97 if (m_isTimeSourceThrottling) | |
98 m_timeSource->setTimebaseAndInterval(timebase, interval); | |
99 } | |
100 | |
101 void CCFrameRateController::setSwapBuffersCompleteSupported(bool supported) | |
102 { | |
103 m_swapBuffersCompleteSupported = supported; | |
104 } | |
105 | |
106 void CCFrameRateController::onTimerTick() | |
107 { | |
108 ASSERT(m_active); | |
109 | |
110 // Check if we have too many frames in flight. | |
111 bool throttled = m_numFramesPending >= m_maxFramesPending; | |
112 | |
113 if (m_client) | |
114 m_client->vsyncTick(throttled); | |
115 | |
116 if (m_swapBuffersCompleteSupported && !m_isTimeSourceThrottling && m_numFram
esPending < m_maxFramesPending) | |
117 postManualTick(); | |
118 } | |
119 | |
120 void CCFrameRateController::postManualTick() | |
121 { | |
122 if (m_active) | |
123 m_manualTicker->startOneShot(0); | |
124 } | |
125 | |
126 void CCFrameRateController::onTimerFired() | |
127 { | |
128 onTimerTick(); | |
129 } | |
130 | |
131 void CCFrameRateController::didBeginFrame() | |
132 { | |
133 if (m_swapBuffersCompleteSupported) | |
134 m_numFramesPending++; | |
135 else if (!m_isTimeSourceThrottling) | |
136 postManualTick(); | |
137 } | |
138 | |
139 void CCFrameRateController::didFinishFrame() | |
140 { | |
141 ASSERT(m_swapBuffersCompleteSupported); | |
142 | |
143 m_numFramesPending--; | |
144 if (!m_isTimeSourceThrottling) | |
145 postManualTick(); | |
146 } | |
147 | |
148 void CCFrameRateController::didAbortAllPendingFrames() | |
149 { | |
150 m_numFramesPending = 0; | |
151 } | |
152 | |
153 base::TimeTicks CCFrameRateController::nextTickTime() | |
154 { | |
155 if (m_isTimeSourceThrottling) | |
156 return m_timeSource->nextTickTime(); | |
157 | |
158 return base::TimeTicks(); | |
159 } | |
160 | |
161 } | |
OLD | NEW |