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/scheduler.h" | 5 #include "cc/scheduler/scheduler.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
8 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "cc/debug/traced_value.h" | 11 #include "cc/debug/traced_value.h" |
11 | 12 |
12 namespace cc { | 13 namespace cc { |
13 | 14 |
14 Scheduler::Scheduler(SchedulerClient* client, | 15 Scheduler::Scheduler(SchedulerClient* client, |
15 const SchedulerSettings& scheduler_settings) | 16 const SchedulerSettings& scheduler_settings) |
16 : settings_(scheduler_settings), | 17 : settings_(scheduler_settings), |
17 client_(client), | 18 client_(client), |
18 weak_factory_(this), | 19 weak_factory_(this), |
19 last_set_needs_begin_frame_(false), | 20 last_set_needs_begin_frame_(false), |
20 has_pending_begin_frame_(false), | 21 has_pending_begin_frame_(false), |
21 safe_to_expect_begin_frame_(false), | 22 safe_to_expect_begin_frame_( |
23 !scheduler_settings.use_begin_frame_workaround_for_crbug_249806), | |
22 state_machine_(scheduler_settings), | 24 state_machine_(scheduler_settings), |
23 inside_process_scheduled_actions_(false) { | 25 inside_process_scheduled_actions_(false) { |
24 DCHECK(client_); | 26 DCHECK(client_); |
25 DCHECK(!state_machine_.BeginFrameNeededToDrawByImplThread()); | 27 DCHECK(!state_machine_.BeginFrameNeededByImplThread()); |
26 } | 28 } |
27 | 29 |
28 Scheduler::~Scheduler() { | 30 Scheduler::~Scheduler() {} |
29 client_->SetNeedsBeginFrameOnImplThread(false); | |
30 } | |
31 | 31 |
32 void Scheduler::SetCanStart() { | 32 void Scheduler::SetCanStart() { |
33 state_machine_.SetCanStart(); | 33 state_machine_.SetCanStart(); |
34 ProcessScheduledActions(); | 34 ProcessScheduledActions(); |
35 } | 35 } |
36 | 36 |
37 void Scheduler::SetVisible(bool visible) { | 37 void Scheduler::SetVisible(bool visible) { |
38 state_machine_.SetVisible(visible); | 38 state_machine_.SetVisible(visible); |
39 ProcessScheduledActions(); | 39 ProcessScheduledActions(); |
40 } | 40 } |
41 | 41 |
42 void Scheduler::SetCanDraw(bool can_draw) { | 42 void Scheduler::SetCanDraw(bool can_draw) { |
43 state_machine_.SetCanDraw(can_draw); | 43 state_machine_.SetCanDraw(can_draw); |
44 ProcessScheduledActions(); | 44 ProcessScheduledActions(); |
45 } | 45 } |
46 | 46 |
47 void Scheduler::NotifyReadyToActivate() { | 47 void Scheduler::NotifyReadyToActivate() { |
48 state_machine_.NotifyReadyToActivate(); | 48 state_machine_.NotifyReadyToActivate(); |
49 ProcessScheduledActions(); | 49 ProcessScheduledActions(); |
50 } | 50 } |
51 | 51 |
52 void Scheduler::ActivatePendingTree() { | |
53 client_->ScheduledActionActivatePendingTree(); | |
54 if (state_machine_.ShouldTriggerBeginFrameDeadlineEarly()) | |
55 PostBeginFrameDeadline(base::TimeTicks()); | |
56 } | |
57 | |
52 void Scheduler::SetNeedsCommit() { | 58 void Scheduler::SetNeedsCommit() { |
53 state_machine_.SetNeedsCommit(); | 59 state_machine_.SetNeedsCommit(); |
54 ProcessScheduledActions(); | 60 ProcessScheduledActions(); |
55 } | 61 } |
56 | 62 |
57 void Scheduler::SetNeedsForcedCommitForReadback() { | 63 void Scheduler::SetNeedsForcedCommitForReadback() { |
58 state_machine_.SetNeedsCommit(); | 64 state_machine_.SetNeedsCommit(); |
59 state_machine_.SetNeedsForcedCommitForReadback(); | 65 state_machine_.SetNeedsForcedCommitForReadback(); |
60 ProcessScheduledActions(); | 66 ProcessScheduledActions(); |
61 } | 67 } |
(...skipping 10 matching lines...) Expand all Loading... | |
72 | 78 |
73 void Scheduler::SetMainThreadNeedsLayerTextures() { | 79 void Scheduler::SetMainThreadNeedsLayerTextures() { |
74 state_machine_.SetMainThreadNeedsLayerTextures(); | 80 state_machine_.SetMainThreadNeedsLayerTextures(); |
75 ProcessScheduledActions(); | 81 ProcessScheduledActions(); |
76 } | 82 } |
77 | 83 |
78 void Scheduler::FinishCommit() { | 84 void Scheduler::FinishCommit() { |
79 TRACE_EVENT0("cc", "Scheduler::FinishCommit"); | 85 TRACE_EVENT0("cc", "Scheduler::FinishCommit"); |
80 state_machine_.FinishCommit(); | 86 state_machine_.FinishCommit(); |
81 ProcessScheduledActions(); | 87 ProcessScheduledActions(); |
88 | |
89 if (state_machine_.ShouldTriggerBeginFrameDeadlineEarly()) | |
90 PostBeginFrameDeadline(base::TimeTicks()); | |
82 } | 91 } |
83 | 92 |
84 void Scheduler::BeginFrameAbortedByMainThread(bool did_handle) { | 93 void Scheduler::BeginFrameAbortedByMainThread(bool did_handle) { |
85 TRACE_EVENT0("cc", "Scheduler::BeginFrameAbortedByMainThread"); | 94 TRACE_EVENT0("cc", "Scheduler::BeginFrameAbortedByMainThread"); |
86 state_machine_.BeginFrameAbortedByMainThread(did_handle); | 95 state_machine_.BeginFrameAbortedByMainThread(did_handle); |
87 ProcessScheduledActions(); | 96 ProcessScheduledActions(); |
88 } | 97 } |
89 | 98 |
90 void Scheduler::DidLoseOutputSurface() { | 99 void Scheduler::DidLoseOutputSurface() { |
91 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface"); | 100 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface"); |
101 begin_frame_deadline_closure_.Cancel(); | |
102 has_pending_begin_frame_ = false; | |
92 state_machine_.DidLoseOutputSurface(); | 103 state_machine_.DidLoseOutputSurface(); |
93 ProcessScheduledActions(); | 104 ProcessScheduledActions(); |
94 } | 105 } |
95 | 106 |
96 void Scheduler::DidCreateAndInitializeOutputSurface() { | 107 void Scheduler::DidCreateAndInitializeOutputSurface() { |
97 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface"); | 108 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface"); |
98 state_machine_.DidCreateAndInitializeOutputSurface(); | 109 state_machine_.DidCreateAndInitializeOutputSurface(); |
110 begin_frame_deadline_closure_.Cancel(); | |
99 has_pending_begin_frame_ = false; | 111 has_pending_begin_frame_ = false; |
100 last_set_needs_begin_frame_ = false; | 112 last_set_needs_begin_frame_ = false; |
101 safe_to_expect_begin_frame_ = false; | 113 safe_to_expect_begin_frame_ = |
114 !settings_.use_begin_frame_workaround_for_crbug_249806; | |
102 ProcessScheduledActions(); | 115 ProcessScheduledActions(); |
103 } | 116 } |
104 | 117 |
105 base::TimeTicks Scheduler::AnticipatedDrawTime() { | 118 base::TimeTicks Scheduler::AnticipatedDrawTime() { |
106 TRACE_EVENT0("cc", "Scheduler::AnticipatedDrawTime"); | 119 TRACE_EVENT0("cc", "Scheduler::AnticipatedDrawTime"); |
107 | 120 |
108 if (!last_set_needs_begin_frame_ || | 121 if (!last_set_needs_begin_frame_ || |
109 last_begin_frame_args_.interval <= base::TimeDelta()) | 122 last_begin_frame_args_.interval <= base::TimeDelta()) |
110 return base::TimeTicks(); | 123 return base::TimeTicks(); |
111 | 124 |
112 // TODO(brianderson): Express this in terms of the deadline. | |
113 base::TimeTicks now = base::TimeTicks::Now(); | 125 base::TimeTicks now = base::TimeTicks::Now(); |
114 int64 intervals = 1 + ((now - last_begin_frame_args_.frame_time) / | 126 base::TimeTicks timebase = std::max(last_begin_frame_args_.frame_time, |
115 last_begin_frame_args_.interval); | 127 last_begin_frame_args_.deadline); |
116 return last_begin_frame_args_.frame_time + | 128 int64 intervals = 1 + ((now - timebase) / last_begin_frame_args_.interval); |
117 (last_begin_frame_args_.interval * intervals); | 129 return timebase + (last_begin_frame_args_.interval * intervals); |
118 } | 130 } |
119 | 131 |
120 base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() { | 132 base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() { |
121 return last_begin_frame_args_.frame_time; | 133 return last_begin_frame_args_.frame_time; |
122 } | 134 } |
123 | 135 |
124 void Scheduler::SetupNextBeginFrameIfNeeded() { | 136 void Scheduler::SetupNextBeginFrameIfNeeded() { |
125 bool needs_begin_frame_to_draw = | 137 bool needs_begin_frame_to_draw = |
126 state_machine_.BeginFrameNeededToDrawByImplThread(); | 138 state_machine_.BeginFrameNeededToDrawByImplThread(); |
127 // We want to avoid proactive begin frames with the synchronous compositor | |
128 // because every SetNeedsBeginFrame will force a redraw. | |
129 bool proactive_begin_frame_wanted = | 139 bool proactive_begin_frame_wanted = |
130 state_machine_.ProactiveBeginFrameWantedByImplThread() && | 140 state_machine_.ProactiveBeginFrameWantedByImplThread(); |
131 !settings_.using_synchronous_renderer_compositor && | 141 |
132 settings_.throttle_frame_production; | 142 // We want to avoid proactive begin frames with the synchronous |
133 bool needs_begin_frame = needs_begin_frame_to_draw || | 143 // compositor because every SetNeedsBeginFrame will force a redraw. |
134 proactive_begin_frame_wanted; | 144 bool needs_begin_frame = |
135 bool immediate_disables_needed = | 145 needs_begin_frame_to_draw || proactive_begin_frame_wanted; |
136 settings_.using_synchronous_renderer_compositor; | 146 |
147 bool should_call_set_needs_begin_frame = | |
148 // The synchronous renderer compositor needs immediate enables/disables. | |
149 (settings_.using_synchronous_renderer_compositor && | |
150 needs_begin_frame != last_set_needs_begin_frame_) || | |
151 // Always request the BeginFrame immediately if it wasn't needed before. | |
152 (needs_begin_frame && !last_set_needs_begin_frame_) || | |
153 // Only disable the BeginFrame after a BeginFrame where we didn't swap. | |
154 (!needs_begin_frame && last_set_needs_begin_frame_ && | |
155 has_pending_begin_frame_ && !state_machine_.InsideBeginFrame()) || | |
enne (OOO)
2013/09/11 22:30:47
has_pending_begin_frame is a funny way of saying t
| |
156 // We did not draw and swap this BeginFrame, | |
157 // so we need to explicitly request another BeginFrame. | |
158 (needs_begin_frame && has_pending_begin_frame_ && | |
159 !state_machine_.InsideBeginFrame()); | |
137 | 160 |
138 if (needs_begin_frame_to_draw) | 161 if (needs_begin_frame_to_draw) |
139 safe_to_expect_begin_frame_ = true; | 162 safe_to_expect_begin_frame_ = true; |
140 | 163 |
141 // Determine if we need BeginFrame notifications. | 164 if (should_call_set_needs_begin_frame) { |
142 // If we do, always request the BeginFrame immediately. | |
143 // If not, only disable on the next BeginFrame to avoid unnecessary toggles. | |
144 // The synchronous renderer compositor requires immediate disables though. | |
145 if ((needs_begin_frame || | |
146 state_machine_.inside_begin_frame() || | |
147 immediate_disables_needed) && | |
148 (needs_begin_frame != last_set_needs_begin_frame_)) { | |
149 has_pending_begin_frame_ = false; | 165 has_pending_begin_frame_ = false; |
150 client_->SetNeedsBeginFrameOnImplThread(needs_begin_frame); | 166 client_->SetNeedsBeginFrameOnImplThread(needs_begin_frame); |
151 if (safe_to_expect_begin_frame_) | 167 if (safe_to_expect_begin_frame_) |
152 last_set_needs_begin_frame_ = needs_begin_frame; | 168 last_set_needs_begin_frame_ = needs_begin_frame; |
153 } | 169 } |
154 | |
155 // Request another BeginFrame if we haven't drawn for now until we have | |
156 // deadlines implemented. | |
157 if (state_machine_.inside_begin_frame() && has_pending_begin_frame_) { | |
158 has_pending_begin_frame_ = false; | |
159 client_->SetNeedsBeginFrameOnImplThread(true); | |
160 return; | |
161 } | |
162 } | 170 } |
163 | 171 |
164 void Scheduler::BeginFrame(const BeginFrameArgs& args) { | 172 void Scheduler::BeginFrame(const BeginFrameArgs& args) { |
165 TRACE_EVENT0("cc", "Scheduler::BeginFrame"); | 173 TRACE_EVENT0("cc", "Scheduler::BeginFrame"); |
166 DCHECK(!has_pending_begin_frame_); | 174 DCHECK(!has_pending_begin_frame_); |
167 has_pending_begin_frame_ = true; | 175 has_pending_begin_frame_ = true; |
168 safe_to_expect_begin_frame_ = true; | 176 safe_to_expect_begin_frame_ = true; |
169 last_begin_frame_args_ = args; | 177 last_begin_frame_args_ = args; |
170 state_machine_.DidEnterBeginFrame(args); | 178 last_begin_frame_args_.deadline -= client_->DrawDurationEstimate(); |
179 state_machine_.OnBeginFrame(last_begin_frame_args_); | |
171 ProcessScheduledActions(); | 180 ProcessScheduledActions(); |
172 state_machine_.DidLeaveBeginFrame(); | 181 state_machine_.OnBeginFrameDeadlinePending(); |
182 | |
183 if (settings_.using_synchronous_renderer_compositor) { | |
184 // The synchronous renderer compositor has to make its GL calls | |
185 // within this call to BeginFrame. | |
186 // TODO(brianderson): Have the OutputSurface initiate the deadline tasks | |
187 // so the sychronous renderer compoistor can take advantage of splitting | |
188 // up the BeginFrame and deadline as well. | |
189 OnBeginFrameDeadline(); | |
190 } else if (!settings_.deadline_scheduling_enabled) { | |
191 // We emulate the old non-deadline scheduler here by posting the | |
192 // deadline task without any delay. | |
193 PostBeginFrameDeadline(base::TimeTicks()); | |
194 } else if (state_machine_.ShouldTriggerBeginFrameDeadlineEarly()) { | |
195 // We are ready to draw a new active tree immediately. | |
196 PostBeginFrameDeadline(base::TimeTicks()); | |
197 } else if (state_machine_.needs_redraw()) { | |
198 // We have an animation or fast input path on the impl thread that wants | |
199 // to draw, so don't wait too long for a new active tree. | |
200 PostBeginFrameDeadline(last_begin_frame_args_.deadline); | |
201 } else { | |
202 // The impl thread doesn't have anything it wants to draw and we are just | |
203 // waiting for a new active tree, so post the deadline for the next | |
204 // expected BeginFrame start. | |
205 // TODO(brianderson): Handle long deadlines (that are past the next frame's | |
206 // frame time) properly instead of using this hack. | |
207 PostBeginFrameDeadline(last_begin_frame_args_.frame_time + | |
enne (OOO)
2013/09/11 22:30:47
This case almost seems like you shouldn't have a d
brianderson
2013/09/12 00:12:34
We may not have to draw yet, but if a new commit c
enne (OOO)
2013/09/12 00:26:45
Oh! Thanks for the explanation.
| |
208 last_begin_frame_args_.interval); | |
209 } | |
210 } | |
211 | |
212 void Scheduler::PostBeginFrameDeadline(base::TimeTicks deadline) { | |
213 begin_frame_deadline_closure_.Cancel(); | |
214 begin_frame_deadline_closure_.Reset( | |
215 base::Bind(&Scheduler::OnBeginFrameDeadline, weak_factory_.GetWeakPtr())); | |
216 client_->PostBeginFrameDeadline(begin_frame_deadline_closure_.callback(), | |
217 deadline); | |
218 } | |
219 | |
220 void Scheduler::OnBeginFrameDeadline() { | |
221 TRACE_EVENT0("cc", "Scheduler::OnBeginFrameDeadline"); | |
222 begin_frame_deadline_closure_.Cancel(); | |
223 state_machine_.OnBeginFrameDeadline(); | |
224 ProcessScheduledActions(); | |
225 // We only transition out of BEGIN_FRAME_STATE_INSIDE_DEADLINE when all | |
226 // actions that occur back-to-back in response to entering | |
227 // BEGIN_FRAME_STATE_INSIDE_DEADLINE have completed. This is important | |
228 // because sending the BeginFrame to the main thread will not occur if | |
229 // we transition to BEGIN_FRAME_STATE_IDLE too early. | |
230 state_machine_.OnBeginFrameIdle(); | |
231 client_->DidBeginFrameDeadlineOnImplThread(); | |
173 } | 232 } |
174 | 233 |
175 void Scheduler::DrawAndSwapIfPossible() { | 234 void Scheduler::DrawAndSwapIfPossible() { |
176 DrawSwapReadbackResult result = | 235 DrawSwapReadbackResult result = |
177 client_->ScheduledActionDrawAndSwapIfPossible(); | 236 client_->ScheduledActionDrawAndSwapIfPossible(); |
178 state_machine_.DidDrawIfPossibleCompleted(result.did_draw); | 237 state_machine_.DidDrawIfPossibleCompleted(result.did_draw); |
179 if (result.did_swap) | 238 if (result.did_swap) |
180 has_pending_begin_frame_ = false; | 239 has_pending_begin_frame_ = false; |
181 } | 240 } |
182 | 241 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
214 case SchedulerStateMachine::ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: | 273 case SchedulerStateMachine::ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: |
215 client_->ScheduledActionSendBeginFrameToMainThread(); | 274 client_->ScheduledActionSendBeginFrameToMainThread(); |
216 break; | 275 break; |
217 case SchedulerStateMachine::ACTION_COMMIT: | 276 case SchedulerStateMachine::ACTION_COMMIT: |
218 client_->ScheduledActionCommit(); | 277 client_->ScheduledActionCommit(); |
219 break; | 278 break; |
220 case SchedulerStateMachine::ACTION_UPDATE_VISIBLE_TILES: | 279 case SchedulerStateMachine::ACTION_UPDATE_VISIBLE_TILES: |
221 client_->ScheduledActionUpdateVisibleTiles(); | 280 client_->ScheduledActionUpdateVisibleTiles(); |
222 break; | 281 break; |
223 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE: | 282 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE: |
224 client_->ScheduledActionActivatePendingTree(); | 283 ActivatePendingTree(); |
225 break; | 284 break; |
226 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE: | 285 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE: |
227 DrawAndSwapIfPossible(); | 286 DrawAndSwapIfPossible(); |
228 break; | 287 break; |
229 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED: | 288 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED: |
230 DrawAndSwapForced(); | 289 DrawAndSwapForced(); |
231 break; | 290 break; |
232 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT: | 291 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT: |
233 // No action is actually performed, but this allows the state machine to | 292 // No action is actually performed, but this allows the state machine to |
234 // advance out of its waiting to draw state without actually drawing. | 293 // advance out of its waiting to draw state without actually drawing. |
(...skipping 12 matching lines...) Expand all Loading... | |
247 | 306 |
248 SetupNextBeginFrameIfNeeded(); | 307 SetupNextBeginFrameIfNeeded(); |
249 client_->DidAnticipatedDrawTimeChange(AnticipatedDrawTime()); | 308 client_->DidAnticipatedDrawTimeChange(AnticipatedDrawTime()); |
250 } | 309 } |
251 | 310 |
252 bool Scheduler::WillDrawIfNeeded() const { | 311 bool Scheduler::WillDrawIfNeeded() const { |
253 return !state_machine_.PendingDrawsShouldBeAborted(); | 312 return !state_machine_.PendingDrawsShouldBeAborted(); |
254 } | 313 } |
255 | 314 |
256 } // namespace cc | 315 } // namespace cc |
OLD | NEW |