Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: cc/scheduler/scheduler_state_machine.cc

Issue 19106007: cc: Allow the main thread to cancel commits (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add more comments Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_state_machine.h" 5 #include "cc/scheduler/scheduler_state_machine.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 10
11 namespace cc { 11 namespace cc {
12 12
13 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings) 13 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings)
14 : settings_(settings), 14 : settings_(settings),
15 commit_state_(COMMIT_STATE_IDLE), 15 commit_state_(COMMIT_STATE_IDLE),
16 commit_count_(0), 16 commit_count_(0),
17 current_frame_number_(0), 17 current_frame_number_(0),
18 last_frame_number_where_begin_frame_sent_to_main_thread_(-1),
18 last_frame_number_where_draw_was_called_(-1), 19 last_frame_number_where_draw_was_called_(-1),
19 last_frame_number_where_tree_activation_attempted_(-1), 20 last_frame_number_where_tree_activation_attempted_(-1),
20 last_frame_number_where_update_visible_tiles_was_called_(-1), 21 last_frame_number_where_update_visible_tiles_was_called_(-1),
21 consecutive_failed_draws_(0), 22 consecutive_failed_draws_(0),
22 maximum_number_of_failed_draws_before_draw_is_forced_(3), 23 maximum_number_of_failed_draws_before_draw_is_forced_(3),
23 needs_redraw_(false), 24 needs_redraw_(false),
24 swap_used_incomplete_tile_(false), 25 swap_used_incomplete_tile_(false),
25 needs_forced_redraw_(false), 26 needs_forced_redraw_(false),
26 needs_forced_redraw_after_next_commit_(false), 27 needs_forced_redraw_after_next_commit_(false),
28 needs_redraw_after_next_commit_(false),
27 needs_commit_(false), 29 needs_commit_(false),
28 needs_forced_commit_(false), 30 needs_forced_commit_(false),
29 expect_immediate_begin_frame_for_main_thread_(false), 31 expect_immediate_begin_frame_for_main_thread_(false),
30 main_thread_needs_layer_textures_(false), 32 main_thread_needs_layer_textures_(false),
31 inside_begin_frame_(false), 33 inside_begin_frame_(false),
32 visible_(false), 34 visible_(false),
33 can_start_(false), 35 can_start_(false),
34 can_draw_(false), 36 can_draw_(false),
35 has_pending_tree_(false), 37 has_pending_tree_(false),
36 draw_if_possible_failed_(false), 38 draw_if_possible_failed_(false),
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 bool SchedulerStateMachine::HasAttemptedTreeActivationThisFrame() const { 111 bool SchedulerStateMachine::HasAttemptedTreeActivationThisFrame() const {
110 return current_frame_number_ == 112 return current_frame_number_ ==
111 last_frame_number_where_tree_activation_attempted_; 113 last_frame_number_where_tree_activation_attempted_;
112 } 114 }
113 115
114 bool SchedulerStateMachine::HasUpdatedVisibleTilesThisFrame() const { 116 bool SchedulerStateMachine::HasUpdatedVisibleTilesThisFrame() const {
115 return current_frame_number_ == 117 return current_frame_number_ ==
116 last_frame_number_where_update_visible_tiles_was_called_; 118 last_frame_number_where_update_visible_tiles_was_called_;
117 } 119 }
118 120
121 void SchedulerStateMachine::SetPostCommitFlags() {
122 // This post-commit work is common to both completed and aborted commits.
123 if (needs_forced_redraw_after_next_commit_) {
124 needs_forced_redraw_after_next_commit_ = false;
125 needs_forced_redraw_ = true;
126 }
127 if (needs_redraw_after_next_commit_) {
128 needs_redraw_after_next_commit_ = false;
129 needs_redraw_ = true;
130 }
131 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD;
132 }
133
119 bool SchedulerStateMachine::DrawSuspendedUntilCommit() const { 134 bool SchedulerStateMachine::DrawSuspendedUntilCommit() const {
120 if (!can_draw_) 135 if (!can_draw_)
121 return true; 136 return true;
122 if (!visible_) 137 if (!visible_)
123 return true; 138 return true;
124 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD) 139 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD)
125 return true; 140 return true;
126 return false; 141 return false;
127 } 142 }
128 143
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if (!BeginFrameNeededToDrawByImplThread()) 192 if (!BeginFrameNeededToDrawByImplThread())
178 return true; 193 return true;
179 return false; 194 return false;
180 } 195 }
181 196
182 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const { 197 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
183 if (ShouldAcquireLayerTexturesForMainThread()) 198 if (ShouldAcquireLayerTexturesForMainThread())
184 return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD; 199 return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD;
185 200
186 switch (commit_state_) { 201 switch (commit_state_) {
187 case COMMIT_STATE_IDLE: 202 case COMMIT_STATE_IDLE: {
188 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && 203 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE &&
189 needs_forced_redraw_) 204 needs_forced_redraw_)
190 return ACTION_DRAW_FORCED; 205 return ACTION_DRAW_FORCED;
191 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && 206 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE &&
192 needs_forced_commit_) 207 needs_forced_commit_)
193 // TODO(enne): Should probably drop the active tree on force commit. 208 // TODO(enne): Should probably drop the active tree on force commit.
194 return has_pending_tree_ ? ACTION_NONE 209 return has_pending_tree_ ? ACTION_NONE
195 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; 210 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD;
196 if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_) 211 if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_)
197 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION; 212 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION;
198 if (output_surface_state_ == OUTPUT_SURFACE_CREATING) 213 if (output_surface_state_ == OUTPUT_SURFACE_CREATING)
199 return ACTION_NONE; 214 return ACTION_NONE;
200 if (ShouldUpdateVisibleTiles()) 215 if (ShouldUpdateVisibleTiles())
201 return ACTION_UPDATE_VISIBLE_TILES; 216 return ACTION_UPDATE_VISIBLE_TILES;
202 if (ShouldAttemptTreeActivation()) 217 if (ShouldAttemptTreeActivation())
203 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; 218 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
204 if (ShouldDraw()) { 219 if (ShouldDraw()) {
205 return needs_forced_redraw_ ? ACTION_DRAW_FORCED 220 return needs_forced_redraw_ ? ACTION_DRAW_FORCED
206 : ACTION_DRAW_IF_POSSIBLE; 221 : ACTION_DRAW_IF_POSSIBLE;
207 } 222 }
208 if (needs_commit_ && 223 bool can_commit_this_frame =
209 ((visible_ && output_surface_state_ == OUTPUT_SURFACE_ACTIVE) 224 visible_ &&
210 || needs_forced_commit_)) 225 current_frame_number_ >
226 last_frame_number_where_begin_frame_sent_to_main_thread_;
227 if (needs_commit_ && ((can_commit_this_frame &&
228 output_surface_state_ == OUTPUT_SURFACE_ACTIVE) ||
229 needs_forced_commit_))
211 // TODO(enne): Should probably drop the active tree on force commit. 230 // TODO(enne): Should probably drop the active tree on force commit.
212 return has_pending_tree_ ? ACTION_NONE 231 return has_pending_tree_ ? ACTION_NONE
213 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; 232 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD;
214 return ACTION_NONE; 233 return ACTION_NONE;
215 234 }
216 case COMMIT_STATE_FRAME_IN_PROGRESS: 235 case COMMIT_STATE_FRAME_IN_PROGRESS:
217 if (ShouldUpdateVisibleTiles()) 236 if (ShouldUpdateVisibleTiles())
218 return ACTION_UPDATE_VISIBLE_TILES; 237 return ACTION_UPDATE_VISIBLE_TILES;
219 if (ShouldAttemptTreeActivation()) 238 if (ShouldAttemptTreeActivation())
220 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; 239 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
221 if (ShouldDraw()) { 240 if (ShouldDraw()) {
222 return needs_forced_redraw_ ? ACTION_DRAW_FORCED 241 return needs_forced_redraw_ ? ACTION_DRAW_FORCED
223 : ACTION_DRAW_IF_POSSIBLE; 242 : ACTION_DRAW_IF_POSSIBLE;
224 } 243 }
225 return ACTION_NONE; 244 return ACTION_NONE;
226 245
227 case COMMIT_STATE_READY_TO_COMMIT: 246 case COMMIT_STATE_READY_TO_COMMIT:
228 return ACTION_COMMIT; 247 return ACTION_COMMIT;
229 248
230 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { 249 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: {
231 if (ShouldUpdateVisibleTiles()) 250 if (ShouldUpdateVisibleTiles())
232 return ACTION_UPDATE_VISIBLE_TILES; 251 return ACTION_UPDATE_VISIBLE_TILES;
233 if (ShouldAttemptTreeActivation()) 252 if (ShouldAttemptTreeActivation())
234 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; 253 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
235 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) { 254 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) {
236 return needs_forced_redraw_ ? ACTION_DRAW_FORCED 255 return needs_forced_redraw_ ? ACTION_DRAW_FORCED
237 : ACTION_DRAW_IF_POSSIBLE; 256 : ACTION_DRAW_IF_POSSIBLE;
238 } 257 }
239 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. If 258 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. If
240 // can_draw_ is false or textures are not available, proceed to the next 259 // can_draw_ is false or textures are not available, proceed to the next
241 // step (similar as in COMMIT_STATE_IDLE). 260 // step (similar as in COMMIT_STATE_IDLE).
242 bool can_commit = visible_ || needs_forced_commit_; 261 bool can_commit =
262 needs_forced_commit_ ||
263 (visible_ &&
264 current_frame_number_ >
265 last_frame_number_where_begin_frame_sent_to_main_thread_);
243 if (needs_commit_ && can_commit && DrawSuspendedUntilCommit()) 266 if (needs_commit_ && can_commit && DrawSuspendedUntilCommit())
244 return has_pending_tree_ ? ACTION_NONE 267 return has_pending_tree_ ? ACTION_NONE
245 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; 268 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD;
246 return ACTION_NONE; 269 return ACTION_NONE;
247 } 270 }
248 271
249 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: 272 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW:
250 if (ShouldUpdateVisibleTiles()) 273 if (ShouldUpdateVisibleTiles())
251 return ACTION_UPDATE_VISIBLE_TILES; 274 return ACTION_UPDATE_VISIBLE_TILES;
252 if (ShouldAttemptTreeActivation()) 275 if (ShouldAttemptTreeActivation())
(...skipping 16 matching lines...) Expand all
269 current_frame_number_; 292 current_frame_number_;
270 return; 293 return;
271 294
272 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: 295 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED:
273 last_frame_number_where_tree_activation_attempted_ = 296 last_frame_number_where_tree_activation_attempted_ =
274 current_frame_number_; 297 current_frame_number_;
275 return; 298 return;
276 299
277 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: 300 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD:
278 DCHECK(!has_pending_tree_); 301 DCHECK(!has_pending_tree_);
279 DCHECK(visible_ || needs_forced_commit_); 302 if (!needs_forced_commit_) {
303 DCHECK(visible_);
304 DCHECK_GT(current_frame_number_,
305 last_frame_number_where_begin_frame_sent_to_main_thread_);
306 }
280 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; 307 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS;
281 needs_commit_ = false; 308 needs_commit_ = false;
282 needs_forced_commit_ = false; 309 needs_forced_commit_ = false;
310 last_frame_number_where_begin_frame_sent_to_main_thread_ =
311 current_frame_number_;
283 return; 312 return;
284 313
285 case ACTION_COMMIT: 314 case ACTION_COMMIT:
286 commit_count_++; 315 commit_count_++;
287 if (expect_immediate_begin_frame_for_main_thread_) 316 if (expect_immediate_begin_frame_for_main_thread_)
288 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; 317 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW;
289 else 318 else
290 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; 319 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW;
291 // When impl-side painting, we draw on activation instead of on commit. 320 // When impl-side painting, we draw on activation instead of on commit.
292 if (!settings_.impl_side_painting) 321 if (!settings_.impl_side_painting)
293 needs_redraw_ = true; 322 needs_redraw_ = true;
294 if (draw_if_possible_failed_) 323 if (draw_if_possible_failed_)
295 last_frame_number_where_draw_was_called_ = -1; 324 last_frame_number_where_draw_was_called_ = -1;
296 325 SetPostCommitFlags();
297 if (needs_forced_redraw_after_next_commit_) {
298 needs_forced_redraw_after_next_commit_ = false;
299 needs_forced_redraw_ = true;
300 }
301
302 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD;
303 return; 326 return;
304 327
305 case ACTION_DRAW_FORCED: 328 case ACTION_DRAW_FORCED:
306 case ACTION_DRAW_IF_POSSIBLE: 329 case ACTION_DRAW_IF_POSSIBLE:
307 needs_redraw_ = false; 330 needs_redraw_ = false;
308 needs_forced_redraw_ = false; 331 needs_forced_redraw_ = false;
309 draw_if_possible_failed_ = false; 332 draw_if_possible_failed_ = false;
310 swap_used_incomplete_tile_ = false; 333 swap_used_incomplete_tile_ = false;
311 if (inside_begin_frame_) 334 if (inside_begin_frame_)
312 last_frame_number_where_draw_was_called_ = current_frame_number_; 335 last_frame_number_where_draw_was_called_ = current_frame_number_;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 if (!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE) 383 if (!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
361 return false; 384 return false;
362 385
363 // We should proactively request a BeginFrame if a commit or a tree activation 386 // We should proactively request a BeginFrame if a commit or a tree activation
364 // is pending. 387 // is pending.
365 return (needs_commit_ || needs_forced_commit_ || 388 return (needs_commit_ || needs_forced_commit_ ||
366 commit_state_ != COMMIT_STATE_IDLE || has_pending_tree_); 389 commit_state_ != COMMIT_STATE_IDLE || has_pending_tree_);
367 } 390 }
368 391
369 void SchedulerStateMachine::DidEnterBeginFrame(const BeginFrameArgs& args) { 392 void SchedulerStateMachine::DidEnterBeginFrame(const BeginFrameArgs& args) {
393 current_frame_number_++;
370 inside_begin_frame_ = true; 394 inside_begin_frame_ = true;
371 last_begin_frame_args_ = args; 395 last_begin_frame_args_ = args;
372 } 396 }
373 397
374 void SchedulerStateMachine::DidLeaveBeginFrame() { 398 void SchedulerStateMachine::DidLeaveBeginFrame() {
375 current_frame_number_++;
376 inside_begin_frame_ = false; 399 inside_begin_frame_ = false;
377 } 400 }
378 401
379 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; } 402 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; }
380 403
381 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } 404 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; }
382 405
383 void SchedulerStateMachine::DidSwapUseIncompleteTile() { 406 void SchedulerStateMachine::DidSwapUseIncompleteTile() {
384 swap_used_incomplete_tile_ = true; 407 swap_used_incomplete_tile_ = true;
385 } 408 }
(...skipping 29 matching lines...) Expand all
415 } 438 }
416 439
417 void SchedulerStateMachine::FinishCommit() { 440 void SchedulerStateMachine::FinishCommit() {
418 DCHECK(commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS || 441 DCHECK(commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS ||
419 (expect_immediate_begin_frame_for_main_thread_ && 442 (expect_immediate_begin_frame_for_main_thread_ &&
420 commit_state_ != COMMIT_STATE_IDLE)) 443 commit_state_ != COMMIT_STATE_IDLE))
421 << ToString(); 444 << ToString();
422 commit_state_ = COMMIT_STATE_READY_TO_COMMIT; 445 commit_state_ = COMMIT_STATE_READY_TO_COMMIT;
423 } 446 }
424 447
425 void SchedulerStateMachine::BeginFrameAbortedByMainThread() { 448 void SchedulerStateMachine::BeginFrameAbortedByMainThread(bool did_handle) {
426 DCHECK_EQ(commit_state_, COMMIT_STATE_FRAME_IN_PROGRESS); 449 DCHECK_EQ(commit_state_, COMMIT_STATE_FRAME_IN_PROGRESS);
427 if (expect_immediate_begin_frame_for_main_thread_) { 450 if (expect_immediate_begin_frame_for_main_thread_) {
428 expect_immediate_begin_frame_for_main_thread_ = false; 451 expect_immediate_begin_frame_for_main_thread_ = false;
452 } else if (did_handle) {
453 commit_state_ = COMMIT_STATE_IDLE;
454 SetPostCommitFlags();
429 } else { 455 } else {
430 commit_state_ = COMMIT_STATE_IDLE; 456 commit_state_ = COMMIT_STATE_IDLE;
431 SetNeedsCommit(); 457 SetNeedsCommit();
432 } 458 }
433 } 459 }
434 460
435 void SchedulerStateMachine::DidLoseOutputSurface() { 461 void SchedulerStateMachine::DidLoseOutputSurface() {
436 if (output_surface_state_ == OUTPUT_SURFACE_LOST || 462 if (output_surface_state_ == OUTPUT_SURFACE_LOST ||
437 output_surface_state_ == OUTPUT_SURFACE_CREATING) 463 output_surface_state_ == OUTPUT_SURFACE_CREATING)
438 return; 464 return;
(...skipping 12 matching lines...) Expand all
451 477
452 if (did_create_and_initialize_first_output_surface_) { 478 if (did_create_and_initialize_first_output_surface_) {
453 // TODO(boliu): See if we can remove this when impl-side painting is always 479 // TODO(boliu): See if we can remove this when impl-side painting is always
454 // on. Does anything on the main thread need to update after recreate? 480 // on. Does anything on the main thread need to update after recreate?
455 needs_commit_ = true; 481 needs_commit_ = true;
456 // If anything has requested a redraw, we don't want to actually draw 482 // If anything has requested a redraw, we don't want to actually draw
457 // when the output surface is restored until things have a chance to 483 // when the output surface is restored until things have a chance to
458 // sort themselves out with a commit. 484 // sort themselves out with a commit.
459 needs_redraw_ = false; 485 needs_redraw_ = false;
460 } 486 }
487 needs_redraw_after_next_commit_ = true;
461 did_create_and_initialize_first_output_surface_ = true; 488 did_create_and_initialize_first_output_surface_ = true;
462 } 489 }
463 490
464 bool SchedulerStateMachine::HasInitializedOutputSurface() const { 491 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
465 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; 492 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE;
466 } 493 }
467 494
468 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( 495 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced(
469 int num_draws) { 496 int num_draws) {
470 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; 497 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws;
471 } 498 }
472 499
473 } // namespace cc 500 } // namespace cc
OLDNEW
« no previous file with comments | « cc/scheduler/scheduler_state_machine.h ('k') | cc/scheduler/scheduler_state_machine_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698