OLD | NEW |
---|---|
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/scheduler/frame_rate_controller.h" | 5 #include "cc/scheduler/frame_rate_controller.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "cc/base/thread.h" | 9 #include "cc/base/thread.h" |
10 #include "cc/scheduler/delay_based_time_source.h" | 10 #include "cc/scheduler/delay_based_time_source.h" |
11 #include "cc/scheduler/time_source.h" | 11 #include "cc/scheduler/time_source.h" |
12 | 12 |
13 namespace cc { | 13 namespace cc { |
14 | 14 |
15 class FrameRateControllerTimeSourceAdapter : public TimeSourceClient { | 15 class FrameRateControllerTimeSourceAdapter : public TimeSourceClient { |
16 public: | 16 public: |
17 static scoped_ptr<FrameRateControllerTimeSourceAdapter> Create(FrameRateCont roller* frameRateController) { | 17 static scoped_ptr<FrameRateControllerTimeSourceAdapter> Create( |
18 return make_scoped_ptr(new FrameRateControllerTimeSourceAdapter(frameRat eController)); | 18 FrameRateController* frame_rate_controller) { |
19 } | 19 return make_scoped_ptr( |
20 virtual ~FrameRateControllerTimeSourceAdapter() {} | 20 new FrameRateControllerTimeSourceAdapter(frame_rate_controller)); |
21 } | |
22 virtual ~FrameRateControllerTimeSourceAdapter() {} | |
21 | 23 |
22 virtual void onTimerTick() OVERRIDE { | 24 virtual void onTimerTick() OVERRIDE { frame_rate_controller_->OnTimerTick(); } |
23 m_frameRateController->onTimerTick(); | |
24 } | |
25 | 25 |
26 private: | 26 private: |
27 explicit FrameRateControllerTimeSourceAdapter(FrameRateController* frameRate Controller) | 27 explicit FrameRateControllerTimeSourceAdapter( |
28 : m_frameRateController(frameRateController) {} | 28 FrameRateController* frame_rate_controller) |
29 : frame_rate_controller_(frame_rate_controller) {} | |
29 | 30 |
30 FrameRateController* m_frameRateController; | 31 FrameRateController* frame_rate_controller_; |
31 }; | 32 }; |
32 | 33 |
33 FrameRateController::FrameRateController(scoped_refptr<TimeSource> timer) | 34 FrameRateController::FrameRateController(scoped_refptr<TimeSource> timer) |
34 : m_client(0) | 35 : client_(0), |
enne (OOO)
2013/03/19 04:14:57
NULL
| |
35 , m_numFramesPending(0) | 36 num_frames_pending_(0), |
36 , m_maxFramesPending(0) | 37 max_frames_pending_(0), |
37 , m_timeSource(timer) | 38 time_source_(timer), |
38 , m_active(false) | 39 active_(false), |
39 , m_swapBuffersCompleteSupported(true) | 40 swap_buffers_complete_supported_(true), |
40 , m_isTimeSourceThrottling(true) | 41 is_time_source_throttling_(true), |
41 , m_thread(0) | 42 thread_(0), |
enne (OOO)
2013/03/19 04:14:57
NULL
| |
42 , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) | 43 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
43 { | 44 time_source_client_adapter_ = |
44 m_timeSourceClientAdapter = FrameRateControllerTimeSourceAdapter::Create(thi s); | 45 FrameRateControllerTimeSourceAdapter::Create(this); |
45 m_timeSource->setClient(m_timeSourceClientAdapter.get()); | 46 time_source_->setClient(time_source_client_adapter_.get()); |
46 } | 47 } |
47 | 48 |
48 FrameRateController::FrameRateController(Thread* thread) | 49 FrameRateController::FrameRateController(Thread* thread) |
49 : m_client(0) | 50 : client_(0), |
enne (OOO)
2013/03/19 04:14:57
NULL
| |
50 , m_numFramesPending(0) | 51 num_frames_pending_(0), |
51 , m_maxFramesPending(0) | 52 max_frames_pending_(0), |
52 , m_active(false) | 53 active_(false), |
53 , m_swapBuffersCompleteSupported(true) | 54 swap_buffers_complete_supported_(true), |
54 , m_isTimeSourceThrottling(false) | 55 is_time_source_throttling_(false), |
55 , m_thread(thread) | 56 thread_(thread), |
56 , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) | 57 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
57 { | 58 |
59 FrameRateController::~FrameRateController() { | |
60 if (is_time_source_throttling_) | |
61 time_source_->setActive(false); | |
58 } | 62 } |
59 | 63 |
60 FrameRateController::~FrameRateController() | 64 void FrameRateController::SetActive(bool active) { |
61 { | 65 if (active_ == active) |
62 if (m_isTimeSourceThrottling) | 66 return; |
63 m_timeSource->setActive(false); | 67 TRACE_EVENT1("cc", "FrameRateController::SetActive", "active", active); |
68 active_ = active; | |
69 | |
70 if (is_time_source_throttling_) | |
enne (OOO)
2013/03/19 04:14:57
This if needs curly braces because the else has th
| |
71 time_source_->setActive(active); | |
72 else { | |
73 if (active) | |
74 PostManualTick(); | |
75 else | |
76 weak_factory_.InvalidateWeakPtrs(); | |
77 } | |
64 } | 78 } |
65 | 79 |
66 void FrameRateController::setActive(bool active) | 80 void FrameRateController::SetMaxFramesPending(int max_frames_pending) { |
67 { | 81 DCHECK_GE(max_frames_pending, 0); |
68 if (m_active == active) | 82 max_frames_pending_ = max_frames_pending; |
69 return; | |
70 TRACE_EVENT1("cc", "FrameRateController::setActive", "active", active); | |
71 m_active = active; | |
72 | |
73 if (m_isTimeSourceThrottling) | |
74 m_timeSource->setActive(active); | |
75 else { | |
76 if (active) | |
77 postManualTick(); | |
78 else | |
79 m_weakFactory.InvalidateWeakPtrs(); | |
80 } | |
81 } | 83 } |
82 | 84 |
83 void FrameRateController::setMaxFramesPending(int maxFramesPending) | 85 void FrameRateController::SetTimebaseAndInterval(base::TimeTicks timebase, |
84 { | 86 base::TimeDelta interval) { |
85 DCHECK_GE(maxFramesPending, 0); | 87 if (is_time_source_throttling_) |
86 m_maxFramesPending = maxFramesPending; | 88 time_source_->setTimebaseAndInterval(timebase, interval); |
87 } | 89 } |
88 | 90 |
89 void FrameRateController::setTimebaseAndInterval(base::TimeTicks timebase, base: :TimeDelta interval) | 91 void FrameRateController::SetSwapBuffersCompleteSupported(bool supported) { |
90 { | 92 swap_buffers_complete_supported_ = supported; |
91 if (m_isTimeSourceThrottling) | |
92 m_timeSource->setTimebaseAndInterval(timebase, interval); | |
93 } | 93 } |
94 | 94 |
95 void FrameRateController::setSwapBuffersCompleteSupported(bool supported) | 95 void FrameRateController::OnTimerTick() { |
96 { | 96 DCHECK(active_); |
97 m_swapBuffersCompleteSupported = supported; | 97 |
98 // Check if we have too many frames in flight. | |
99 bool throttled = | |
100 max_frames_pending_ && num_frames_pending_ >= max_frames_pending_; | |
101 TRACE_COUNTER_ID1("cc", "ThrottledVSyncInterval", thread_, throttled); | |
102 | |
103 if (client_) | |
104 client_->VsyncTick(throttled); | |
105 | |
106 if (swap_buffers_complete_supported_ && !is_time_source_throttling_ && | |
107 !throttled) | |
108 PostManualTick(); | |
98 } | 109 } |
99 | 110 |
100 void FrameRateController::onTimerTick() | 111 void FrameRateController::PostManualTick() { |
101 { | 112 if (active_) |
enne (OOO)
2013/03/19 04:14:57
Needs curly braces because it's a multi-line if st
| |
102 DCHECK(m_active); | 113 thread_->PostTask(base::Bind(&FrameRateController::ManualTick, |
103 | 114 weak_factory_.GetWeakPtr())); |
104 // Check if we have too many frames in flight. | |
105 bool throttled = m_maxFramesPending && m_numFramesPending >= m_maxFramesPend ing; | |
106 TRACE_COUNTER_ID1("cc", "ThrottledVSyncInterval", m_thread, throttled); | |
107 | |
108 if (m_client) | |
109 m_client->vsyncTick(throttled); | |
110 | |
111 if (m_swapBuffersCompleteSupported && !m_isTimeSourceThrottling && !throttle d) | |
112 postManualTick(); | |
113 } | 115 } |
114 | 116 |
115 void FrameRateController::postManualTick() | 117 void FrameRateController::ManualTick() { OnTimerTick(); } |
116 { | 118 |
117 if (m_active) | 119 void FrameRateController::DidBeginFrame() { |
118 m_thread->PostTask(base::Bind(&FrameRateController::manualTick, m_weakFa ctory.GetWeakPtr())); | 120 if (swap_buffers_complete_supported_) |
121 num_frames_pending_++; | |
122 else if (!is_time_source_throttling_) | |
123 PostManualTick(); | |
119 } | 124 } |
120 | 125 |
121 void FrameRateController::manualTick() | 126 void FrameRateController::DidFinishFrame() { |
122 { | 127 DCHECK(swap_buffers_complete_supported_); |
123 onTimerTick(); | 128 |
129 num_frames_pending_--; | |
130 if (!is_time_source_throttling_) | |
131 PostManualTick(); | |
124 } | 132 } |
125 | 133 |
126 void FrameRateController::didBeginFrame() | 134 void FrameRateController::DidAbortAllPendingFrames() { |
127 { | 135 num_frames_pending_ = 0; |
128 if (m_swapBuffersCompleteSupported) | |
129 m_numFramesPending++; | |
130 else if (!m_isTimeSourceThrottling) | |
131 postManualTick(); | |
132 } | 136 } |
133 | 137 |
134 void FrameRateController::didFinishFrame() | 138 base::TimeTicks FrameRateController::NextTickTime() { |
135 { | 139 if (is_time_source_throttling_) |
136 DCHECK(m_swapBuffersCompleteSupported); | 140 return time_source_->nextTickTime(); |
137 | 141 |
138 m_numFramesPending--; | 142 return base::TimeTicks(); |
139 if (!m_isTimeSourceThrottling) | |
140 postManualTick(); | |
141 } | 143 } |
142 | 144 |
143 void FrameRateController::didAbortAllPendingFrames() | 145 base::TimeTicks FrameRateController::LastTickTime() { |
144 { | 146 if (is_time_source_throttling_) |
145 m_numFramesPending = 0; | 147 return time_source_->lastTickTime(); |
146 } | |
147 | 148 |
148 base::TimeTicks FrameRateController::nextTickTime() | 149 return base::TimeTicks::Now(); |
149 { | |
150 if (m_isTimeSourceThrottling) | |
151 return m_timeSource->nextTickTime(); | |
152 | |
153 return base::TimeTicks(); | |
154 } | |
155 | |
156 base::TimeTicks FrameRateController::lastTickTime() | |
157 { | |
158 if (m_isTimeSourceThrottling) | |
159 return m_timeSource->lastTickTime(); | |
160 | |
161 return base::TimeTicks::Now(); | |
162 } | 150 } |
163 | 151 |
164 } // namespace cc | 152 } // namespace cc |
OLD | NEW |