summaryrefslogtreecommitdiffstats
path: root/cc/scheduler/scheduler_state_machine.cc
blob: 94100b88fc6e179c0d7aa655b83546fd947ec431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/scheduler/scheduler_state_machine.h"

#include "base/format_macros.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"

namespace cc {

SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings)
    : settings_(settings),
      commit_state_(COMMIT_STATE_IDLE),
      commit_count_(0),
      current_frame_number_(0),
      last_frame_number_where_begin_frame_sent_to_main_thread_(-1),
      last_frame_number_where_draw_was_called_(-1),
      last_frame_number_where_tree_activation_attempted_(-1),
      last_frame_number_where_update_visible_tiles_was_called_(-1),
      consecutive_failed_draws_(0),
      maximum_number_of_failed_draws_before_draw_is_forced_(3),
      needs_redraw_(false),
      swap_used_incomplete_tile_(false),
      needs_forced_redraw_(false),
      needs_forced_redraw_after_next_commit_(false),
      needs_redraw_after_next_commit_(false),
      needs_commit_(false),
      needs_forced_commit_(false),
      expect_immediate_begin_frame_for_main_thread_(false),
      main_thread_needs_layer_textures_(false),
      inside_begin_frame_(false),
      visible_(false),
      can_start_(false),
      can_draw_(false),
      has_pending_tree_(false),
      draw_if_possible_failed_(false),
      texture_state_(LAYER_TEXTURE_STATE_UNLOCKED),
      output_surface_state_(OUTPUT_SURFACE_LOST),
      did_create_and_initialize_first_output_surface_(false) {}

std::string SchedulerStateMachine::ToString() {
  std::string str;
  base::StringAppendF(&str,
                      "settings_.impl_side_painting = %d; ",
                      settings_.impl_side_painting);
  base::StringAppendF(&str, "commit_state_ = %d; ", commit_state_);
  base::StringAppendF(&str, "commit_count_ = %d; ", commit_count_);
  base::StringAppendF(
      &str, "current_frame_number_ = %d; ", current_frame_number_);
  base::StringAppendF(&str,
                      "last_frame_number_where_draw_was_called_ = %d; ",
                      last_frame_number_where_draw_was_called_);
  base::StringAppendF(
      &str,
      "last_frame_number_where_tree_activation_attempted_ = %d; ",
      last_frame_number_where_tree_activation_attempted_);
  base::StringAppendF(
      &str,
      "last_frame_number_where_update_visible_tiles_was_called_ = %d; ",
      last_frame_number_where_update_visible_tiles_was_called_);
  base::StringAppendF(
      &str, "consecutive_failed_draws_ = %d; ", consecutive_failed_draws_);
  base::StringAppendF(
      &str,
      "maximum_number_of_failed_draws_before_draw_is_forced_ = %d; ",
      maximum_number_of_failed_draws_before_draw_is_forced_);
  base::StringAppendF(&str, "needs_redraw_ = %d; ", needs_redraw_);
  base::StringAppendF(
      &str, "swap_used_incomplete_tile_ = %d; ", swap_used_incomplete_tile_);
  base::StringAppendF(
      &str, "needs_forced_redraw_ = %d; ", needs_forced_redraw_);
  base::StringAppendF(&str,
                      "needs_forced_redraw_after_next_commit_ = %d; ",
                      needs_forced_redraw_after_next_commit_);
  base::StringAppendF(&str, "needs_commit_ = %d; ", needs_commit_);
  base::StringAppendF(
      &str, "needs_forced_commit_ = %d; ", needs_forced_commit_);
  base::StringAppendF(&str,
                      "expect_immediate_begin_frame_for_main_thread_ = %d; ",
                      expect_immediate_begin_frame_for_main_thread_);
  base::StringAppendF(&str,
                      "main_thread_needs_layer_textures_ = %d; ",
                      main_thread_needs_layer_textures_);
  base::StringAppendF(&str, "inside_begin_frame_ = %d; ",
      inside_begin_frame_);
  base::StringAppendF(&str, "last_frame_time_ = %"PRId64"; ",
      (last_begin_frame_args_.frame_time - base::TimeTicks())
          .InMilliseconds());
  base::StringAppendF(&str, "last_deadline_ = %"PRId64"; ",
      (last_begin_frame_args_.deadline - base::TimeTicks()).InMilliseconds());
  base::StringAppendF(&str, "last_interval_ = %"PRId64"; ",
      last_begin_frame_args_.interval.InMilliseconds());
  base::StringAppendF(&str, "visible_ = %d; ", visible_);
  base::StringAppendF(&str, "can_start_ = %d; ", can_start_);
  base::StringAppendF(&str, "can_draw_ = %d; ", can_draw_);
  base::StringAppendF(
      &str, "draw_if_possible_failed_ = %d; ", draw_if_possible_failed_);
  base::StringAppendF(&str, "has_pending_tree_ = %d; ", has_pending_tree_);
  base::StringAppendF(&str, "texture_state_ = %d; ", texture_state_);
  base::StringAppendF(
      &str, "output_surface_state_ = %d; ", output_surface_state_);
  return str;
}

bool SchedulerStateMachine::HasDrawnThisFrame() const {
  return current_frame_number_ == last_frame_number_where_draw_was_called_;
}

bool SchedulerStateMachine::HasAttemptedTreeActivationThisFrame() const {
  return current_frame_number_ ==
         last_frame_number_where_tree_activation_attempted_;
}

bool SchedulerStateMachine::HasUpdatedVisibleTilesThisFrame() const {
  return current_frame_number_ ==
         last_frame_number_where_update_visible_tiles_was_called_;
}

void SchedulerStateMachine::SetPostCommitFlags() {
  // This post-commit work is common to both completed and aborted commits.
  if (needs_forced_redraw_after_next_commit_) {
    needs_forced_redraw_after_next_commit_ = false;
    needs_forced_redraw_ = true;
  }
  if (needs_redraw_after_next_commit_) {
    needs_redraw_after_next_commit_ = false;
    needs_redraw_ = true;
  }
  texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD;
}

bool SchedulerStateMachine::DrawSuspendedUntilCommit() const {
  if (!can_draw_)
    return true;
  if (!visible_)
    return true;
  if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD)
    return true;
  return false;
}

bool SchedulerStateMachine::ScheduledToDraw() const {
  if (!needs_redraw_)
    return false;
  if (DrawSuspendedUntilCommit())
    return false;
  return true;
}

bool SchedulerStateMachine::ShouldDraw() const {
  if (needs_forced_redraw_)
    return true;

  if (!ScheduledToDraw())
    return false;
  if (!inside_begin_frame_)
    return false;
  if (HasDrawnThisFrame())
    return false;
  if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
    return false;
  return true;
}

bool SchedulerStateMachine::ShouldAttemptTreeActivation() const {
  return has_pending_tree_ && inside_begin_frame_ &&
         !HasAttemptedTreeActivationThisFrame();
}

bool SchedulerStateMachine::ShouldUpdateVisibleTiles() const {
  if (!settings_.impl_side_painting)
    return false;
  if (HasUpdatedVisibleTilesThisFrame())
    return false;

  return ShouldAttemptTreeActivation() || ShouldDraw() ||
         swap_used_incomplete_tile_;
}

bool SchedulerStateMachine::ShouldAcquireLayerTexturesForMainThread() const {
  if (!main_thread_needs_layer_textures_)
    return false;
  if (texture_state_ == LAYER_TEXTURE_STATE_UNLOCKED)
    return true;
  DCHECK_EQ(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD);
  // Transfer the lock from impl thread to main thread immediately if the
  // impl thread is not even scheduled to draw. Guards against deadlocking.
  if (!ScheduledToDraw())
    return true;
  if (!BeginFrameNeededToDrawByImplThread())
    return true;
  return false;
}

SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
  if (ShouldAcquireLayerTexturesForMainThread())
    return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD;

  switch (commit_state_) {
    case COMMIT_STATE_IDLE: {
      if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE &&
          needs_forced_redraw_)
        return ACTION_DRAW_FORCED;
      if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE &&
          needs_forced_commit_)
        // TODO(enne): Should probably drop the active tree on force commit.
        return has_pending_tree_ ? ACTION_NONE
                                 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD;
      if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_)
        return ACTION_BEGIN_OUTPUT_SURFACE_CREATION;
      if (output_surface_state_ == OUTPUT_SURFACE_CREATING)
        return ACTION_NONE;
      if (ShouldUpdateVisibleTiles())
        return ACTION_UPDATE_VISIBLE_TILES;
      if (ShouldAttemptTreeActivation())
        return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
      if (ShouldDraw()) {
        return needs_forced_redraw_ ? ACTION_DRAW_FORCED
                                    : ACTION_DRAW_IF_POSSIBLE;
      }
      bool can_commit_this_frame =
          visible_ &&
          current_frame_number_ >
              last_frame_number_where_begin_frame_sent_to_main_thread_;
      if (needs_commit_ && ((can_commit_this_frame &&
                             output_surface_state_ == OUTPUT_SURFACE_ACTIVE) ||
                            needs_forced_commit_))
        // TODO(enne): Should probably drop the active tree on force commit.
        return has_pending_tree_ ? ACTION_NONE
                                 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD;
      return ACTION_NONE;
    }
    case COMMIT_STATE_FRAME_IN_PROGRESS:
      if (ShouldUpdateVisibleTiles())
        return ACTION_UPDATE_VISIBLE_TILES;
      if (ShouldAttemptTreeActivation())
        return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
      if (ShouldDraw()) {
        return needs_forced_redraw_ ? ACTION_DRAW_FORCED
                                    : ACTION_DRAW_IF_POSSIBLE;
      }
      return ACTION_NONE;

    case COMMIT_STATE_READY_TO_COMMIT:
      return ACTION_COMMIT;

    case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: {
      if (ShouldUpdateVisibleTiles())
        return ACTION_UPDATE_VISIBLE_TILES;
      if (ShouldAttemptTreeActivation())
        return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
      if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) {
        return needs_forced_redraw_ ? ACTION_DRAW_FORCED
                                    : ACTION_DRAW_IF_POSSIBLE;
      }
      // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. If
      // can_draw_ is false or textures are not available, proceed to the next
      // step (similar as in COMMIT_STATE_IDLE).
      bool can_commit =
          needs_forced_commit_ ||
          (visible_ &&
           current_frame_number_ >
               last_frame_number_where_begin_frame_sent_to_main_thread_);
      if (needs_commit_ && can_commit && DrawSuspendedUntilCommit())
        return has_pending_tree_ ? ACTION_NONE
                                 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD;
      return ACTION_NONE;
    }

    case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW:
      if (ShouldUpdateVisibleTiles())
        return ACTION_UPDATE_VISIBLE_TILES;
      if (ShouldAttemptTreeActivation())
        return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED;
      if (needs_forced_redraw_)
        return ACTION_DRAW_FORCED;
      return ACTION_NONE;
  }
  NOTREACHED();
  return ACTION_NONE;
}

void SchedulerStateMachine::UpdateState(Action action) {
  switch (action) {
    case ACTION_NONE:
      return;

    case ACTION_UPDATE_VISIBLE_TILES:
      last_frame_number_where_update_visible_tiles_was_called_ =
          current_frame_number_;
      return;

    case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED:
      last_frame_number_where_tree_activation_attempted_ =
          current_frame_number_;
      return;

    case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD:
      DCHECK(!has_pending_tree_);
      if (!needs_forced_commit_) {
        DCHECK(visible_);
        DCHECK_GT(current_frame_number_,
                  last_frame_number_where_begin_frame_sent_to_main_thread_);
      }
      commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS;
      needs_commit_ = false;
      needs_forced_commit_ = false;
      last_frame_number_where_begin_frame_sent_to_main_thread_ =
          current_frame_number_;
      return;

    case ACTION_COMMIT:
      commit_count_++;
      if (expect_immediate_begin_frame_for_main_thread_)
        commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW;
      else
        commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW;
      // When impl-side painting, we draw on activation instead of on commit.
      if (!settings_.impl_side_painting)
        needs_redraw_ = true;
      if (draw_if_possible_failed_)
        last_frame_number_where_draw_was_called_ = -1;
      SetPostCommitFlags();
      return;

    case ACTION_DRAW_FORCED:
    case ACTION_DRAW_IF_POSSIBLE:
      needs_redraw_ = false;
      needs_forced_redraw_ = false;
      draw_if_possible_failed_ = false;
      swap_used_incomplete_tile_ = false;
      if (inside_begin_frame_)
        last_frame_number_where_draw_was_called_ = current_frame_number_;
      if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) {
        DCHECK(expect_immediate_begin_frame_for_main_thread_);
        commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS;
        expect_immediate_begin_frame_for_main_thread_ = false;
      } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) {
        commit_state_ = COMMIT_STATE_IDLE;
      }
      if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD)
        texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED;
      return;

    case ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
      DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE);
      DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST);
      output_surface_state_ = OUTPUT_SURFACE_CREATING;
      return;

    case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD:
      texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD;
      main_thread_needs_layer_textures_ = false;
      return;
  }
}

void SchedulerStateMachine::SetMainThreadNeedsLayerTextures() {
  DCHECK(!main_thread_needs_layer_textures_);
  DCHECK_NE(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD);
  main_thread_needs_layer_textures_ = true;
}

bool SchedulerStateMachine::BeginFrameNeededToDrawByImplThread() const {
  // If we can't draw, don't tick until we are notified that we can draw again.
  if (!can_draw_)
    return false;

  if (needs_forced_redraw_)
    return true;

  if (visible_ && swap_used_incomplete_tile_)
    return true;

  return needs_redraw_ && visible_ &&
         output_surface_state_ == OUTPUT_SURFACE_ACTIVE;
}

bool SchedulerStateMachine::ProactiveBeginFrameWantedByImplThread() const {
  // Do not be proactive when invisible.
  if (!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
    return false;

  // We should proactively request a BeginFrame if a commit or a tree activation
  // is pending.
  return (needs_commit_ || needs_forced_commit_ ||
          commit_state_ != COMMIT_STATE_IDLE || has_pending_tree_);
}

void SchedulerStateMachine::DidEnterBeginFrame(const BeginFrameArgs& args) {
  current_frame_number_++;
  inside_begin_frame_ = true;
  last_begin_frame_args_ = args;
}

void SchedulerStateMachine::DidLeaveBeginFrame() {
  inside_begin_frame_ = false;
}

void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; }

void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; }

void SchedulerStateMachine::DidSwapUseIncompleteTile() {
  swap_used_incomplete_tile_ = true;
}

void SchedulerStateMachine::SetNeedsForcedRedraw() {
  needs_forced_redraw_ = true;
}

void SchedulerStateMachine::DidDrawIfPossibleCompleted(bool success) {
  draw_if_possible_failed_ = !success;
  if (draw_if_possible_failed_) {
    needs_redraw_ = true;
    needs_commit_ = true;
    consecutive_failed_draws_++;
    if (settings_.timeout_and_draw_when_animation_checkerboards &&
        consecutive_failed_draws_ >=
        maximum_number_of_failed_draws_before_draw_is_forced_) {
      consecutive_failed_draws_ = 0;
      // We need to force a draw, but it doesn't make sense to do this until
      // we've committed and have new textures.
      needs_forced_redraw_after_next_commit_ = true;
    }
  } else {
    consecutive_failed_draws_ = 0;
  }
}

void SchedulerStateMachine::SetNeedsCommit() { needs_commit_ = true; }

void SchedulerStateMachine::SetNeedsForcedCommit() {
  needs_forced_commit_ = true;
  expect_immediate_begin_frame_for_main_thread_ = true;
}

void SchedulerStateMachine::FinishCommit() {
  DCHECK(commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS ||
         (expect_immediate_begin_frame_for_main_thread_ &&
          commit_state_ != COMMIT_STATE_IDLE))
      << ToString();
  commit_state_ = COMMIT_STATE_READY_TO_COMMIT;
}

void SchedulerStateMachine::BeginFrameAbortedByMainThread(bool did_handle) {
  DCHECK_EQ(commit_state_, COMMIT_STATE_FRAME_IN_PROGRESS);
  if (expect_immediate_begin_frame_for_main_thread_) {
    expect_immediate_begin_frame_for_main_thread_ = false;
  } else if (did_handle) {
    commit_state_ = COMMIT_STATE_IDLE;
    SetPostCommitFlags();
  } else {
    commit_state_ = COMMIT_STATE_IDLE;
    SetNeedsCommit();
  }
}

void SchedulerStateMachine::DidLoseOutputSurface() {
  if (output_surface_state_ == OUTPUT_SURFACE_LOST ||
      output_surface_state_ == OUTPUT_SURFACE_CREATING)
    return;
  output_surface_state_ = OUTPUT_SURFACE_LOST;
}

void SchedulerStateMachine::SetHasPendingTree(bool has_pending_tree) {
  has_pending_tree_ = has_pending_tree;
}

void SchedulerStateMachine::SetCanDraw(bool can) { can_draw_ = can; }

void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
  DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING);
  output_surface_state_ = OUTPUT_SURFACE_ACTIVE;

  if (did_create_and_initialize_first_output_surface_) {
    // TODO(boliu): See if we can remove this when impl-side painting is always
    // on. Does anything on the main thread need to update after recreate?
    needs_commit_ = true;
    // If anything has requested a redraw, we don't want to actually draw
    // when the output surface is restored until things have a chance to
    // sort themselves out with a commit.
    needs_redraw_ = false;
  }
  needs_redraw_after_next_commit_ = true;
  did_create_and_initialize_first_output_surface_ = true;
}

bool SchedulerStateMachine::HasInitializedOutputSurface() const {
  return output_surface_state_ == OUTPUT_SURFACE_ACTIVE;
}

void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced(
    int num_draws) {
  maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws;
}

}  // namespace cc