| 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_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 |
| 12 class DEVNULL {}; |
| 13 template <typename A> |
| 14 DEVNULL& operator <<(DEVNULL &d, A a) { return d; } |
| 15 //static DEVNULL gDevNull; |
| 16 //#undef VLOG |
| 17 //#define VLOG(x) gDevNull |
| 18 |
| 11 namespace cc { | 19 namespace cc { |
| 12 | 20 |
| 13 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings) | 21 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings) |
| 14 : settings_(settings), | 22 : settings_(settings), |
| 15 commit_state_(COMMIT_STATE_IDLE), | 23 commit_state_(COMMIT_STATE_IDLE), |
| 16 commit_count_(0), | 24 commit_count_(0), |
| 17 current_frame_number_(0), | 25 begin_frame_count_(0), |
| 18 last_frame_number_where_draw_was_called_(-1), | 26 begin_frame_deadline_count_(0), |
| 19 last_frame_number_where_tree_activation_attempted_(-1), | 27 draw_attempt_count_(0), |
| 20 last_frame_number_where_check_for_completed_tile_uploads_called_(-1), | 28 last_begin_frame_count_draw_was_called_(-1), |
| 29 last_begin_frame_count_begin_frame_sent_to_main_thread_(-1), |
| 30 last_draw_attempt_count_tree_activation_attempted_(-1), |
| 31 last_draw_attempt_count_completed_tile_uploads_checked_(-1), |
| 21 consecutive_failed_draws_(0), | 32 consecutive_failed_draws_(0), |
| 22 maximum_number_of_failed_draws_before_draw_is_forced_(3), | 33 maximum_number_of_failed_draws_before_draw_is_forced_(3), |
| 23 needs_redraw_(false), | 34 needs_redraw_(false), |
| 24 swap_used_incomplete_tile_(false), | 35 swap_used_incomplete_tile_(false), |
| 25 needs_forced_redraw_(false), | 36 readback_state_(READBACK_STATE_IDLE), |
| 26 needs_forced_redraw_after_next_commit_(false), | 37 forced_redraw_state_(FORCED_REDRAW_STATE_IDLE), |
| 27 needs_commit_(false), | 38 needs_commit_(false), |
| 28 needs_forced_commit_(false), | |
| 29 expect_immediate_begin_frame_for_main_thread_(false), | |
| 30 main_thread_needs_layer_textures_(false), | 39 main_thread_needs_layer_textures_(false), |
| 31 inside_begin_frame_(false), | 40 begin_frame_state_(BEGIN_FRAME_STATE_IDLE), |
| 41 commit_tree_has_been_drawn_(true), |
| 42 active_tree_has_been_drawn_(false), |
| 43 active_tree_is_null_(true), |
| 32 visible_(false), | 44 visible_(false), |
| 33 can_start_(false), | 45 can_start_(false), |
| 34 can_draw_(false), | 46 can_draw_(false), |
| 35 has_pending_tree_(false), | 47 has_pending_tree_(false), |
| 36 draw_if_possible_failed_(false), | 48 draw_if_possible_failed_(false), |
| 37 texture_state_(LAYER_TEXTURE_STATE_UNLOCKED), | 49 texture_state_(LAYER_TEXTURE_STATE_UNLOCKED), |
| 38 output_surface_state_(OUTPUT_SURFACE_LOST), | 50 output_surface_state_(OUTPUT_SURFACE_LOST), |
| 39 did_create_and_initialize_first_output_surface_(false) {} | 51 did_create_and_initialize_first_output_surface_(false) {} |
| 40 | 52 |
| 41 std::string SchedulerStateMachine::ToString() { | 53 std::string SchedulerStateMachine::ToString() { |
| 42 std::string str; | 54 std::string str; |
| 43 base::StringAppendF(&str, | 55 base::StringAppendF(&str, |
| 44 "settings_.impl_side_painting = %d; ", | 56 "settings_.impl_side_painting = %d; ", |
| 45 settings_.impl_side_painting); | 57 settings_.impl_side_painting); |
| 46 base::StringAppendF(&str, "commit_state_ = %d; ", commit_state_); | 58 base::StringAppendF(&str, "commit_state_ = %d; ", commit_state_); |
| 47 base::StringAppendF(&str, "commit_count_ = %d; ", commit_count_); | 59 base::StringAppendF(&str, "commit_count_ = %d; ", commit_count_); |
| 48 base::StringAppendF( | 60 base::StringAppendF(&str, "begin_frame_count_ = %d; ", |
| 49 &str, "current_frame_number_ = %d; ", current_frame_number_); | 61 begin_frame_count_); |
| 62 base::StringAppendF(&str, "draw_attempt_count_ = %d; ", |
| 63 draw_attempt_count_); |
| 50 base::StringAppendF(&str, | 64 base::StringAppendF(&str, |
| 51 "last_frame_number_where_draw_was_called_ = %d; ", | 65 "last_begin_frame_count_draw_was_called_ = %d; ", |
| 52 last_frame_number_where_draw_was_called_); | 66 last_begin_frame_count_draw_was_called_); |
| 53 base::StringAppendF( | 67 base::StringAppendF( |
| 54 &str, | 68 &str, |
| 55 "last_frame_number_where_tree_activation_attempted_ = %d; ", | 69 "last_draw_attempt_count_tree_activation_attempted_ = %d; ", |
| 56 last_frame_number_where_tree_activation_attempted_); | 70 last_draw_attempt_count_tree_activation_attempted_); |
| 57 base::StringAppendF( | 71 base::StringAppendF( |
| 58 &str, | 72 &str, |
| 59 "last_frame_number_where_check_for_completed_tile_uploads_called_ = %d; ", | 73 "last_draw_attempt_count_completed_tile_uploads_checked_ = %d; ", |
| 60 last_frame_number_where_check_for_completed_tile_uploads_called_); | 74 last_draw_attempt_count_completed_tile_uploads_checked_); |
| 61 base::StringAppendF( | 75 base::StringAppendF( |
| 62 &str, "consecutive_failed_draws_ = %d; ", consecutive_failed_draws_); | 76 &str, "consecutive_failed_draws_ = %d; ", consecutive_failed_draws_); |
| 63 base::StringAppendF( | 77 base::StringAppendF( |
| 64 &str, | 78 &str, |
| 65 "maximum_number_of_failed_draws_before_draw_is_forced_ = %d; ", | 79 "maximum_number_of_failed_draws_before_draw_is_forced_ = %d; ", |
| 66 maximum_number_of_failed_draws_before_draw_is_forced_); | 80 maximum_number_of_failed_draws_before_draw_is_forced_); |
| 67 base::StringAppendF(&str, "needs_redraw_ = %d; ", needs_redraw_); | 81 base::StringAppendF(&str, "needs_redraw_ = %d; ", needs_redraw_); |
| 68 base::StringAppendF( | 82 base::StringAppendF( |
| 69 &str, "swap_used_incomplete_tile_ = %d; ", swap_used_incomplete_tile_); | 83 &str, "swap_used_incomplete_tile_ = %d; ", swap_used_incomplete_tile_); |
| 70 base::StringAppendF( | 84 base::StringAppendF(&str, "readback_state_ = %d; ", readback_state_); |
| 71 &str, "needs_forced_redraw_ = %d; ", needs_forced_redraw_); | |
| 72 base::StringAppendF(&str, | 85 base::StringAppendF(&str, |
| 73 "needs_forced_redraw_after_next_commit_ = %d; ", | 86 "forced_redraw_state_ = %d; ", |
| 74 needs_forced_redraw_after_next_commit_); | 87 forced_redraw_state_); |
| 75 base::StringAppendF(&str, "needs_commit_ = %d; ", needs_commit_); | 88 base::StringAppendF(&str, "needs_commit_ = %d; ", needs_commit_); |
| 76 base::StringAppendF( | |
| 77 &str, "needs_forced_commit_ = %d; ", needs_forced_commit_); | |
| 78 base::StringAppendF(&str, | |
| 79 "expect_immediate_begin_frame_for_main_thread_ = %d; ", | |
| 80 expect_immediate_begin_frame_for_main_thread_); | |
| 81 base::StringAppendF(&str, | 89 base::StringAppendF(&str, |
| 82 "main_thread_needs_layer_textures_ = %d; ", | 90 "main_thread_needs_layer_textures_ = %d; ", |
| 83 main_thread_needs_layer_textures_); | 91 main_thread_needs_layer_textures_); |
| 84 base::StringAppendF(&str, "inside_begin_frame_ = %d; ", | 92 base::StringAppendF(&str, "begin_frame_state_ = %d; ", begin_frame_state_); |
| 85 inside_begin_frame_); | |
| 86 base::StringAppendF(&str, "last_frame_time_ = %"PRId64"; ", | 93 base::StringAppendF(&str, "last_frame_time_ = %"PRId64"; ", |
| 87 (last_begin_frame_args_.frame_time - base::TimeTicks()) | 94 (last_begin_frame_args_.frame_time - base::TimeTicks()) |
| 88 .InMilliseconds()); | 95 .InMilliseconds()); |
| 89 base::StringAppendF(&str, "last_deadline_ = %"PRId64"; ", | 96 base::StringAppendF(&str, "last_deadline_ = %"PRId64"; ", |
| 90 (last_begin_frame_args_.deadline - base::TimeTicks()).InMilliseconds()); | 97 (last_begin_frame_args_.deadline - base::TimeTicks()).InMilliseconds()); |
| 91 base::StringAppendF(&str, "last_interval_ = %"PRId64"; ", | 98 base::StringAppendF(&str, "last_interval_ = %"PRId64"; ", |
| 92 last_begin_frame_args_.interval.InMilliseconds()); | 99 last_begin_frame_args_.interval.InMilliseconds()); |
| 93 base::StringAppendF(&str, "visible_ = %d; ", visible_); | 100 base::StringAppendF(&str, "visible_ = %d; ", visible_); |
| 94 base::StringAppendF(&str, "can_start_ = %d; ", can_start_); | 101 base::StringAppendF(&str, "can_start_ = %d; ", can_start_); |
| 95 base::StringAppendF(&str, "can_draw_ = %d; ", can_draw_); | 102 base::StringAppendF(&str, "can_draw_ = %d; ", can_draw_); |
| 96 base::StringAppendF( | 103 base::StringAppendF( |
| 97 &str, "draw_if_possible_failed_ = %d; ", draw_if_possible_failed_); | 104 &str, "draw_if_possible_failed_ = %d; ", draw_if_possible_failed_); |
| 98 base::StringAppendF(&str, "has_pending_tree_ = %d; ", has_pending_tree_); | 105 base::StringAppendF(&str, "has_pending_tree_ = %d; ", has_pending_tree_); |
| 106 base::StringAppendF(&str, "commit_tree_has_been_drawn_ = %d; ", |
| 107 commit_tree_has_been_drawn_); |
| 108 base::StringAppendF(&str, "active_tree_has_been_drawn_ = %d; ", |
| 109 active_tree_has_been_drawn_); |
| 110 base::StringAppendF(&str, "active_tree_is_null_ = %d; ", |
| 111 active_tree_is_null_); |
| 99 base::StringAppendF(&str, "texture_state_ = %d; ", texture_state_); | 112 base::StringAppendF(&str, "texture_state_ = %d; ", texture_state_); |
| 100 base::StringAppendF( | 113 base::StringAppendF( |
| 101 &str, "output_surface_state_ = %d; ", output_surface_state_); | 114 &str, "output_surface_state_ = %d; ", output_surface_state_); |
| 102 return str; | 115 return str; |
| 103 } | 116 } |
| 104 | 117 |
| 105 bool SchedulerStateMachine::HasDrawnThisFrame() const { | 118 bool SchedulerStateMachine::HasDrawnThisFrame() const { |
| 106 return current_frame_number_ == last_frame_number_where_draw_was_called_; | 119 return begin_frame_count_ == last_begin_frame_count_draw_was_called_; |
| 107 } | 120 } |
| 108 | 121 |
| 109 bool SchedulerStateMachine::HasAttemptedTreeActivationThisFrame() const { | 122 bool SchedulerStateMachine::HasSentBeginFrameToMainThreadThisFrame() const { |
| 110 return current_frame_number_ == | 123 return begin_frame_count_ == |
| 111 last_frame_number_where_tree_activation_attempted_; | 124 last_begin_frame_count_begin_frame_sent_to_main_thread_; |
| 112 } | 125 } |
| 113 | 126 |
| 114 bool SchedulerStateMachine::HasCheckedForCompletedTileUploadsThisFrame() const { | 127 bool SchedulerStateMachine::HasAttemptedTreeActivationThisDrawAttempt() const { |
| 115 return current_frame_number_ == | 128 return draw_attempt_count_ == |
| 116 last_frame_number_where_check_for_completed_tile_uploads_called_; | 129 last_draw_attempt_count_tree_activation_attempted_; |
| 130 } |
| 131 |
| 132 bool SchedulerStateMachine:: |
| 133 HasCheckedForCompletedTileUploadsThisDrawAttempt() const { |
| 134 return draw_attempt_count_ == |
| 135 last_draw_attempt_count_completed_tile_uploads_checked_; |
| 117 } | 136 } |
| 118 | 137 |
| 119 bool SchedulerStateMachine::DrawSuspendedUntilCommit() const { | 138 bool SchedulerStateMachine::DrawSuspendedUntilCommit() const { |
| 120 if (!can_draw_) | 139 if (!can_draw_) |
| 121 return true; | 140 return true; |
| 122 if (!visible_) | 141 if (!visible_) |
| 123 return true; | 142 return true; |
| 124 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD) | 143 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD) |
| 125 return true; | 144 return true; |
| 126 return false; | 145 return false; |
| 127 } | 146 } |
| 128 | 147 |
| 129 bool SchedulerStateMachine::ScheduledToDraw() const { | 148 bool SchedulerStateMachine::ScheduledToDraw() const { |
| 130 if (!needs_redraw_) | 149 if (!needs_redraw_) |
| 131 return false; | 150 return false; |
| 132 if (DrawSuspendedUntilCommit()) | 151 if (DrawSuspendedUntilCommit()) |
| 133 return false; | 152 return false; |
| 134 return true; | 153 return true; |
| 135 } | 154 } |
| 136 | 155 |
| 137 bool SchedulerStateMachine::ShouldDraw() const { | 156 bool SchedulerStateMachine::ShouldDraw() const { |
| 138 if (needs_forced_redraw_) | 157 // After a readback, make sure not to draw again until we've replaced the |
| 139 return true; | 158 // readback commit with a real one. |
| 159 if (readback_state_ == READBACK_STATE_REPLACEMENT_COMMIT_PENDING || |
| 160 readback_state_ == READBACK_STATE_REPLACEMENT_COMMIT_ACTIVATING) |
| 161 return false; |
| 140 | 162 |
| 163 // Only draw outside of the BeginFrame deadline for readbacks or if we've |
| 164 // lost the outpust surface or are waiting for a forced draw. |
| 165 if (readback_state_ == READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK) |
| 166 return commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 167 if (output_surface_state_ == OUTPUT_SURFACE_LOST) |
| 168 return commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW || |
| 169 commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 170 |
| 171 // Never draw outside of the BeginFrame deadline (except for readbacks). |
| 172 if (begin_frame_state_ != BEGIN_FRAME_STATE_INSIDE_DEADLINE) |
| 173 return false; |
| 174 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW) |
| 175 return commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 176 if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) |
| 177 return false; |
| 141 if (!ScheduledToDraw()) | 178 if (!ScheduledToDraw()) |
| 142 return false; | 179 return false; |
| 143 if (!inside_begin_frame_) | |
| 144 return false; | |
| 145 if (HasDrawnThisFrame()) | 180 if (HasDrawnThisFrame()) |
| 146 return false; | 181 return false; |
| 147 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE) | 182 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE) |
| 148 return false; | 183 return false; |
| 149 return true; | 184 return true; |
| 150 } | 185 } |
| 151 | 186 |
| 152 bool SchedulerStateMachine::ShouldAttemptTreeActivation() const { | 187 bool SchedulerStateMachine::ShouldAttemptTreeActivation() const { |
| 153 return has_pending_tree_ && inside_begin_frame_ && | 188 return !HasAttemptedTreeActivationThisDrawAttempt() && |
| 154 !HasAttemptedTreeActivationThisFrame(); | 189 has_pending_tree_ && |
| 190 (!can_draw_ || !visible_ || |
| 191 output_surface_state_ != OUTPUT_SURFACE_ACTIVE || |
| 192 ((active_tree_has_been_drawn_ || active_tree_is_null_) && |
| 193 begin_frame_state_ != BEGIN_FRAME_STATE_IDLE)); |
| 155 } | 194 } |
| 156 | 195 |
| 157 bool SchedulerStateMachine::ShouldCheckForCompletedTileUploads() const { | 196 bool SchedulerStateMachine::ShouldCheckForCompletedTileUploads() const { |
| 158 if (!settings_.impl_side_painting) | 197 if (!settings_.impl_side_painting) |
| 159 return false; | 198 return false; |
| 160 if (HasCheckedForCompletedTileUploadsThisFrame()) | 199 if (HasCheckedForCompletedTileUploadsThisDrawAttempt()) |
| 161 return false; | 200 return false; |
| 162 | 201 |
| 163 return ShouldAttemptTreeActivation() || ShouldDraw() || | 202 return ShouldAttemptTreeActivation() || ShouldDraw() || |
| 164 swap_used_incomplete_tile_; | 203 swap_used_incomplete_tile_; |
| 165 } | 204 } |
| 166 | 205 |
| 167 bool SchedulerStateMachine::ShouldAcquireLayerTexturesForMainThread() const { | 206 bool SchedulerStateMachine::ShouldAcquireLayerTexturesForMainThread() const { |
| 168 if (!main_thread_needs_layer_textures_) | 207 if (!main_thread_needs_layer_textures_) |
| 169 return false; | 208 return false; |
| 170 if (texture_state_ == LAYER_TEXTURE_STATE_UNLOCKED) | 209 if (texture_state_ == LAYER_TEXTURE_STATE_UNLOCKED) |
| 171 return true; | 210 return true; |
| 172 DCHECK_EQ(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD); | 211 DCHECK_EQ(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD); |
| 173 // Transfer the lock from impl thread to main thread immediately if the | 212 // Transfer the lock from impl thread to main thread immediately if the |
| 174 // impl thread is not even scheduled to draw. Guards against deadlocking. | 213 // impl thread is not even scheduled to draw. Guards against deadlocking. |
| 175 if (!ScheduledToDraw()) | 214 if (!ScheduledToDraw()) |
| 176 return true; | 215 return true; |
| 177 if (!BeginFrameNeededToDrawByImplThread()) | 216 if (!BeginFrameNeededToDrawByImplThread()) |
| 178 return true; | 217 return true; |
| 179 return false; | 218 return false; |
| 180 } | 219 } |
| 181 | 220 |
| 221 bool SchedulerStateMachine::ShouldSendBeginFrameToMainThread() const { |
| 222 if (HasSentBeginFrameToMainThreadThisFrame()) |
| 223 return false; |
| 224 |
| 225 if (readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED) |
| 226 return true; |
| 227 |
| 228 // TODO(brianderson): Allow sending BeginFrame to main thread while idle when |
| 229 // the main thread isn't consuming user input. |
| 230 if (begin_frame_state_ == BEGIN_FRAME_STATE_IDLE && |
| 231 BeginFrameNeededByImplThread()) |
| 232 return false; |
| 233 |
| 234 // Do not send begin frame to main thread in the deadline until we have drawn. |
| 235 if (begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_DEADLINE && |
| 236 !HasDrawnThisFrame()) |
| 237 return false; |
| 238 |
| 239 // We can't accept a commit if we have a pending tree. |
| 240 if (has_pending_tree_) |
| 241 return false; |
| 242 |
| 243 bool can_commit = needs_commit_ && |
| 244 (visible_ || |
| 245 forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT); |
| 246 switch (commit_state_) { |
| 247 case COMMIT_STATE_IDLE: |
| 248 return can_commit && |
| 249 (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT || |
| 250 output_surface_state_ == OUTPUT_SURFACE_ACTIVE); |
| 251 |
| 252 // COMMIT_STATE_WAITING_FOR_ACTIVATION wants to enforce activation and |
| 253 // eventually a draw. |
| 254 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. |
| 255 // If can_draw_ is false or textures are not available, proceed to the |
| 256 // next step (similar as in COMMIT_STATE_IDLE). |
| 257 case COMMIT_STATE_WAITING_FOR_ACTIVATION: |
| 258 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: |
| 259 return can_commit && DrawSuspendedUntilCommit(); |
| 260 default: |
| 261 return false; |
| 262 } |
| 263 |
| 264 return false; |
| 265 } |
| 266 |
| 182 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const { | 267 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const { |
| 183 if (ShouldAcquireLayerTexturesForMainThread()) | 268 if (ShouldAcquireLayerTexturesForMainThread()) |
| 184 return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD; | 269 return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD; |
| 185 | 270 |
| 186 switch (commit_state_) { | 271 switch (commit_state_) { |
| 187 case COMMIT_STATE_IDLE: | 272 case COMMIT_STATE_IDLE: VLOG(0) << __LINE__ << "COMMIT_STATE_IDLE\n"; |
| 188 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && | 273 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && |
| 189 needs_forced_redraw_) | 274 readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED) |
| 190 return ACTION_DRAW_FORCED; | |
| 191 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && | |
| 192 needs_forced_commit_) | |
| 193 // TODO(enne): Should probably drop the active tree on force commit. | 275 // TODO(enne): Should probably drop the active tree on force commit. |
| 194 return has_pending_tree_ ? ACTION_NONE | 276 return has_pending_tree_ ? ACTION_NONE |
| 195 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; | 277 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; |
| 196 if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_) | 278 if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_) |
| 197 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION; | 279 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION; |
| 198 if (output_surface_state_ == OUTPUT_SURFACE_CREATING) | 280 if (output_surface_state_ == OUTPUT_SURFACE_CREATING) |
| 199 return ACTION_NONE; | 281 return ACTION_NONE; |
| 200 if (ShouldCheckForCompletedTileUploads()) | 282 if (ShouldCheckForCompletedTileUploads()) |
| 201 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 283 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 202 if (ShouldAttemptTreeActivation()) | 284 if (ShouldAttemptTreeActivation()) |
| 203 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 285 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 204 if (ShouldDraw()) { | 286 if (ShouldDraw()) { |
| 205 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 287 DCHECK_NE(forced_redraw_state_, FORCED_REDRAW_STATE_WAITING_FOR_DRAW); |
| 206 : ACTION_DRAW_IF_POSSIBLE; | 288 DCHECK_NE(readback_state_, READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK)
; |
| 289 return ACTION_DRAW_IF_POSSIBLE; |
| 207 } | 290 } |
| 208 if (needs_commit_ && | 291 if (ShouldSendBeginFrameToMainThread()) { |
| 209 ((visible_ && output_surface_state_ == OUTPUT_SURFACE_ACTIVE) | |
| 210 || needs_forced_commit_)) | |
| 211 // TODO(enne): Should probably drop the active tree on force commit. | 292 // TODO(enne): Should probably drop the active tree on force commit. |
| 212 return has_pending_tree_ ? ACTION_NONE | 293 return ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; |
| 213 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; | 294 } |
| 214 return ACTION_NONE; | 295 return ACTION_NONE; |
| 215 | 296 |
| 216 case COMMIT_STATE_FRAME_IN_PROGRESS: | 297 case COMMIT_STATE_FRAME_IN_PROGRESS:VLOG(0) << __LINE__ << "COMMIT_STATE_FRA
ME_IN_PROGRESS\n"; |
| 217 if (ShouldCheckForCompletedTileUploads()) | 298 if (ShouldCheckForCompletedTileUploads()) |
| 218 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 299 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 219 if (ShouldAttemptTreeActivation()) | 300 if (ShouldAttemptTreeActivation()) |
| 220 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 301 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 221 if (ShouldDraw()) { | 302 if (ShouldDraw()) { |
| 222 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 303 DCHECK_NE(forced_redraw_state_, FORCED_REDRAW_STATE_WAITING_FOR_DRAW); |
| 223 : ACTION_DRAW_IF_POSSIBLE; | 304 DCHECK_NE(readback_state_, READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK)
; |
| 305 return ACTION_DRAW_IF_POSSIBLE; |
| 224 } | 306 } |
| 225 return ACTION_NONE; | 307 return ACTION_NONE; |
| 226 | 308 |
| 227 case COMMIT_STATE_READY_TO_COMMIT: | 309 case COMMIT_STATE_READY_TO_COMMIT:VLOG(0) << __LINE__ << "COMMIT_STATE_READY
_TO_COMMIT\n"; |
| 228 return ACTION_COMMIT; | 310 return ACTION_COMMIT; |
| 229 | 311 |
| 230 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { | 312 case COMMIT_STATE_WAITING_FOR_ACTIVATION: { VLOG(0) << __LINE__ << "COMMIT_S
TATE_WAITING_FOR_ACTIVATION\n"; |
| 231 if (ShouldCheckForCompletedTileUploads()) | 313 if (ShouldCheckForCompletedTileUploads()) |
| 232 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 314 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 233 if (ShouldAttemptTreeActivation()) | 315 if (ShouldAttemptTreeActivation()) |
| 234 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 316 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 235 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) { | 317 if (ShouldDraw()) { |
| 236 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 318 DCHECK_NE(forced_redraw_state_, FORCED_REDRAW_STATE_WAITING_FOR_DRAW); |
| 237 : ACTION_DRAW_IF_POSSIBLE; | 319 DCHECK_NE(readback_state_, READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK)
; |
| 320 return ACTION_DRAW_IF_POSSIBLE; |
| 238 } | 321 } |
| 239 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. If | 322 if (ShouldSendBeginFrameToMainThread()) |
| 240 // can_draw_ is false or textures are not available, proceed to the next | 323 return ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; |
| 241 // step (similar as in COMMIT_STATE_IDLE). | |
| 242 bool can_commit = visible_ || needs_forced_commit_; | |
| 243 if (needs_commit_ && can_commit && DrawSuspendedUntilCommit()) | |
| 244 return has_pending_tree_ ? ACTION_NONE | |
| 245 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; | |
| 246 return ACTION_NONE; | 324 return ACTION_NONE; |
| 247 } | 325 } |
| 248 | 326 |
| 249 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: | 327 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { VLOG(0) << __LINE__ << "COMMIT_S
TATE_WAITING_FOR_FIRST_DRAW\n"; |
| 250 if (ShouldCheckForCompletedTileUploads()) | 328 if (ShouldCheckForCompletedTileUploads()) |
| 251 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 329 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 252 if (ShouldAttemptTreeActivation()) | 330 if (ShouldAttemptTreeActivation()) |
| 253 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 331 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 254 if (needs_forced_redraw_) | 332 if (ShouldDraw()) { |
| 255 return ACTION_DRAW_FORCED; | 333 DCHECK_NE(forced_redraw_state_, FORCED_REDRAW_STATE_WAITING_FOR_DRAW); |
| 334 DCHECK_NE(readback_state_, READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK)
; |
| 335 return ACTION_DRAW_IF_POSSIBLE; |
| 336 } |
| 337 if (ShouldSendBeginFrameToMainThread()) |
| 338 return ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; |
| 339 return ACTION_NONE; |
| 340 } |
| 341 |
| 342 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: VLOG(0) << __LINE__ << "COM
MIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW\n"; |
| 343 if (ShouldCheckForCompletedTileUploads()) |
| 344 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 345 if (ShouldAttemptTreeActivation()) |
| 346 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 347 if (ShouldDraw()) { |
| 348 if(forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW) |
| 349 return ACTION_DRAW_FORCED; |
| 350 else if (readback_state_ == READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK
) |
| 351 return ACTION_DRAW_AND_READBACK; |
| 352 NOTREACHED(); |
| 353 } |
| 256 return ACTION_NONE; | 354 return ACTION_NONE; |
| 257 } | 355 } |
| 258 NOTREACHED(); | 356 NOTREACHED(); |
| 259 return ACTION_NONE; | 357 return ACTION_NONE; |
| 260 } | 358 } |
| 261 | 359 |
| 262 void SchedulerStateMachine::UpdateState(Action action) { | 360 void SchedulerStateMachine::UpdateState(Action action) { |
| 263 switch (action) { | 361 switch (action) { |
| 264 case ACTION_NONE: | 362 case ACTION_NONE: |
| 265 return; | 363 return; |
| 266 | 364 |
| 267 case ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: | 365 case ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: |
| 268 last_frame_number_where_check_for_completed_tile_uploads_called_ = | 366 last_draw_attempt_count_completed_tile_uploads_checked_ = |
| 269 current_frame_number_; | 367 draw_attempt_count_; |
| 270 return; | 368 return; |
| 271 | 369 |
| 272 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: | 370 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: |
| 273 last_frame_number_where_tree_activation_attempted_ = | 371 last_draw_attempt_count_tree_activation_attempted_ = |
| 274 current_frame_number_; | 372 draw_attempt_count_; |
| 275 return; | 373 return; |
| 276 | 374 |
| 277 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: | 375 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: |
| 278 DCHECK(!has_pending_tree_); | 376 DCHECK(!has_pending_tree_); |
| 279 DCHECK(visible_ || needs_forced_commit_); | 377 DCHECK(visible_ || |
| 378 readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED || |
| 379 forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) <<
ToString(); |
| 280 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; | 380 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; |
| 281 needs_commit_ = false; | 381 needs_commit_ = false; |
| 282 needs_forced_commit_ = false; | 382 if (readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED) |
| 383 readback_state_ = READBACK_STATE_FORCED_COMMIT_PENDING; |
| 384 last_begin_frame_count_begin_frame_sent_to_main_thread_ = |
| 385 begin_frame_count_; |
| 283 return; | 386 return; |
| 284 | 387 |
| 285 case ACTION_COMMIT: | 388 case ACTION_COMMIT: |
| 286 commit_count_++; | 389 commit_count_++; |
| 287 if (expect_immediate_begin_frame_for_main_thread_) | |
| 288 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; | |
| 289 else | |
| 290 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; | |
| 291 // When impl-side painting, we draw on activation instead of on commit. | |
| 292 if (!settings_.impl_side_painting) | |
| 293 needs_redraw_ = true; | |
| 294 if (draw_if_possible_failed_) | |
| 295 last_frame_number_where_draw_was_called_ = -1; | |
| 296 | 390 |
| 297 if (needs_forced_redraw_after_next_commit_) { | 391 if (settings_.impl_side_painting) { |
| 298 needs_forced_redraw_after_next_commit_ = false; | 392 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) |
| 299 needs_forced_redraw_ = true; | 393 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION; |
| 394 else if (readback_state_ == READBACK_STATE_FORCED_COMMIT_PENDING) |
| 395 readback_state_ = READBACK_STATE_WAITING_FOR_ACTIVATION; |
| 396 else if (readback_state_ == READBACK_STATE_REPLACEMENT_COMMIT_PENDING) |
| 397 readback_state_ = READBACK_STATE_REPLACEMENT_COMMIT_ACTIVATING; |
| 398 commit_state_ = COMMIT_STATE_WAITING_FOR_ACTIVATION; |
| 399 } else { |
| 400 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) { |
| 401 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_DRAW; |
| 402 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 403 } else if (readback_state_ == READBACK_STATE_FORCED_COMMIT_PENDING || |
| 404 readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED) { |
| 405 readback_state_ = READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK; |
| 406 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 407 } else { |
| 408 if (readback_state_ == READBACK_STATE_REPLACEMENT_COMMIT_PENDING) |
| 409 readback_state_ = READBACK_STATE_IDLE; |
| 410 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; |
| 411 } |
| 300 } | 412 } |
| 301 | 413 |
| 414 // When impl-side painting, we draw on activation instead of on commit. |
| 415 if (!settings_.impl_side_painting) { |
| 416 needs_redraw_ = true; |
| 417 if (draw_if_possible_failed_) |
| 418 last_begin_frame_count_draw_was_called_ = -1; |
| 419 } |
| 420 commit_tree_has_been_drawn_ = false; |
| 302 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD; | 421 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD; |
| 303 return; | 422 return; |
| 304 | 423 |
| 424 case ACTION_DRAW_IF_POSSIBLE: |
| 305 case ACTION_DRAW_FORCED: | 425 case ACTION_DRAW_FORCED: |
| 306 case ACTION_DRAW_IF_POSSIBLE: | 426 case ACTION_DRAW_AND_READBACK: |
| 307 needs_redraw_ = false; | |
| 308 needs_forced_redraw_ = false; | |
| 309 draw_if_possible_failed_ = false; | |
| 310 swap_used_incomplete_tile_ = false; | |
| 311 if (inside_begin_frame_) | |
| 312 last_frame_number_where_draw_was_called_ = current_frame_number_; | |
| 313 if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) { | 427 if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) { |
| 314 DCHECK(expect_immediate_begin_frame_for_main_thread_); | 428 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW) { |
| 315 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; | 429 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE; |
| 316 expect_immediate_begin_frame_for_main_thread_ = false; | 430 } else if (readback_state_ == |
| 431 READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK) { |
| 432 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; |
| 433 readback_state_ = has_pending_tree_ ? |
| 434 READBACK_STATE_REPLACEMENT_COMMIT_ACTIVATING : |
| 435 READBACK_STATE_REPLACEMENT_COMMIT_PENDING; |
| 436 } |
| 317 } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) { | 437 } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) { |
| 318 commit_state_ = COMMIT_STATE_IDLE; | 438 commit_state_ = COMMIT_STATE_IDLE; |
| 319 } | 439 } |
| 440 needs_redraw_ = false; |
| 441 draw_if_possible_failed_ = false; |
| 442 swap_used_incomplete_tile_ = false; |
| 443 last_begin_frame_count_draw_was_called_ = begin_frame_count_; |
| 320 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD) | 444 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD) |
| 321 texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED; | 445 texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED; |
| 446 active_tree_has_been_drawn_ = true; |
| 447 commit_tree_has_been_drawn_ = true; |
| 322 return; | 448 return; |
| 323 | 449 |
| 324 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION: | 450 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION: |
| 325 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE); | 451 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE); |
| 326 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST); | 452 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST); |
| 327 output_surface_state_ = OUTPUT_SURFACE_CREATING; | 453 output_surface_state_ = OUTPUT_SURFACE_CREATING; |
| 328 return; | 454 return; |
| 329 | 455 |
| 330 case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: | 456 case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: |
| 331 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD; | 457 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD; |
| 332 main_thread_needs_layer_textures_ = false; | 458 main_thread_needs_layer_textures_ = false; |
| 333 if (commit_state_ != COMMIT_STATE_FRAME_IN_PROGRESS) | 459 if (commit_state_ != COMMIT_STATE_FRAME_IN_PROGRESS) |
| 334 needs_commit_ = true; | 460 needs_commit_ = true; |
| 335 return; | 461 return; |
| 336 } | 462 } |
| 337 } | 463 } |
| 338 | 464 |
| 465 void SchedulerStateMachine::AdvanceBeginFrameStateWhenNoActionsRemain() { |
| 466 switch (begin_frame_state_) { |
| 467 case BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME: |
| 468 begin_frame_state_ = BEGIN_FRAME_STATE_DEADLINE_PENDING; |
| 469 break; |
| 470 case BEGIN_FRAME_STATE_INSIDE_DEADLINE: |
| 471 begin_frame_state_ = BEGIN_FRAME_STATE_IDLE; |
| 472 break; |
| 473 case BEGIN_FRAME_STATE_IDLE: |
| 474 case BEGIN_FRAME_STATE_DEADLINE_PENDING: |
| 475 break; |
| 476 } |
| 477 } |
| 478 |
| 339 void SchedulerStateMachine::SetMainThreadNeedsLayerTextures() { | 479 void SchedulerStateMachine::SetMainThreadNeedsLayerTextures() { |
| 340 DCHECK(!main_thread_needs_layer_textures_); | 480 DCHECK(!main_thread_needs_layer_textures_); |
| 341 DCHECK_NE(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD); | 481 DCHECK_NE(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD); |
| 342 main_thread_needs_layer_textures_ = true; | 482 main_thread_needs_layer_textures_ = true; |
| 343 } | 483 } |
| 344 | 484 |
| 485 bool SchedulerStateMachine::BeginFrameNeededByImplThread() const { |
| 486 return BeginFrameNeededToDrawByImplThread() || |
| 487 BeginFrameProactivelyNeededByImplThread(); |
| 488 } |
| 489 |
| 345 bool SchedulerStateMachine::BeginFrameNeededToDrawByImplThread() const { | 490 bool SchedulerStateMachine::BeginFrameNeededToDrawByImplThread() const { |
| 346 // If we can't draw, don't tick until we are notified that we can draw again. | 491 // If we can't draw, don't tick until we are notified that we can draw again. |
| 347 if (!can_draw_) | 492 if (!can_draw_) |
| 348 return false; | 493 return false; |
| 349 | 494 |
| 350 if (needs_forced_redraw_) | 495 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW) |
| 351 return true; | 496 return true; |
| 352 | 497 |
| 353 if (visible_ && swap_used_incomplete_tile_) | 498 if (visible_ && swap_used_incomplete_tile_) |
| 354 return true; | 499 return true; |
| 355 | 500 |
| 356 return needs_redraw_ && visible_ && | 501 return needs_redraw_ && visible_ && |
| 357 output_surface_state_ == OUTPUT_SURFACE_ACTIVE; | 502 output_surface_state_ == OUTPUT_SURFACE_ACTIVE; |
| 358 } | 503 } |
| 359 | 504 |
| 360 bool SchedulerStateMachine::ProactiveBeginFrameWantedByImplThread() const { | 505 bool SchedulerStateMachine::BeginFrameProactivelyNeededByImplThread() const { |
| 361 // Do not be proactive when invisible. | 506 if (settings_.using_synchronous_renderer_compositor) |
| 507 return false; |
| 508 |
| 509 if (!settings_.throttle_frame_production) |
| 510 return false; |
| 511 |
| 362 if (!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE) | 512 if (!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE) |
| 363 return false; | 513 return false; |
| 364 | 514 |
| 365 // We should proactively request a BeginFrame if a commit or a tree activation | 515 // We should proactively request a BeginFrame if a commit or a tree activation |
| 366 // is pending. | 516 // is pending. |
| 367 return (needs_commit_ || needs_forced_commit_ || | 517 return (needs_commit_ || |
| 368 commit_state_ != COMMIT_STATE_IDLE || has_pending_tree_); | 518 commit_state_ != COMMIT_STATE_IDLE || |
| 519 has_pending_tree_); |
| 369 } | 520 } |
| 370 | 521 |
| 371 void SchedulerStateMachine::DidEnterBeginFrame(const BeginFrameArgs& args) { | 522 void SchedulerStateMachine::OnBeginFrame(const BeginFrameArgs& args) { |
| 372 inside_begin_frame_ = true; | 523 begin_frame_count_++; |
| 524 draw_attempt_count_++; |
| 373 last_begin_frame_args_ = args; | 525 last_begin_frame_args_ = args; |
| 526 begin_frame_state_ = BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME; |
| 374 } | 527 } |
| 375 | 528 |
| 376 void SchedulerStateMachine::DidLeaveBeginFrame() { | 529 bool SchedulerStateMachine::ShouldTriggerBeginFrameDeadlineEarly() const { |
| 377 current_frame_number_++; | 530 // TODO(brianderson): This should take into account multiple commit sources. |
| 378 inside_begin_frame_ = false; | 531 return begin_frame_state_ == BEGIN_FRAME_STATE_DEADLINE_PENDING && |
| 532 WillDrawNewTree(); |
| 379 } | 533 } |
| 380 | 534 |
| 381 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; } | 535 bool SchedulerStateMachine::InsideBeginFrame() const { |
| 536 return begin_frame_state_ != BEGIN_FRAME_STATE_IDLE; |
| 537 } |
| 538 |
| 539 bool SchedulerStateMachine::WillDrawNewTree() const { |
| 540 if (settings_.impl_side_painting) |
| 541 return !active_tree_has_been_drawn_; |
| 542 else |
| 543 return !commit_tree_has_been_drawn_; |
| 544 } |
| 545 |
| 546 void SchedulerStateMachine::OnBeginFrameDeadline() { |
| 547 DCHECK_EQ(begin_frame_state_, BEGIN_FRAME_STATE_DEADLINE_PENDING); |
| 548 begin_frame_deadline_count_++; |
| 549 draw_attempt_count_++; |
| 550 begin_frame_state_ = BEGIN_FRAME_STATE_INSIDE_DEADLINE; |
| 551 } |
| 552 |
| 553 void SchedulerStateMachine::SetVisible(bool visible) { |
| 554 visible_ = visible; |
| 555 } |
| 382 | 556 |
| 383 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } | 557 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } |
| 384 | 558 |
| 385 void SchedulerStateMachine::DidSwapUseIncompleteTile() { | 559 void SchedulerStateMachine::DidSwapUseIncompleteTile() { |
| 386 swap_used_incomplete_tile_ = true; | 560 swap_used_incomplete_tile_ = true; |
| 387 } | 561 } |
| 388 | 562 |
| 389 void SchedulerStateMachine::SetNeedsForcedRedraw() { | |
| 390 needs_forced_redraw_ = true; | |
| 391 } | |
| 392 | |
| 393 void SchedulerStateMachine::DidDrawIfPossibleCompleted(bool success) { | 563 void SchedulerStateMachine::DidDrawIfPossibleCompleted(bool success) { |
| 394 draw_if_possible_failed_ = !success; | 564 draw_if_possible_failed_ = !success; |
| 395 if (draw_if_possible_failed_) { | 565 if (draw_if_possible_failed_) { |
| 396 needs_redraw_ = true; | 566 needs_redraw_ = true; |
| 397 needs_commit_ = true; | 567 needs_commit_ = true; |
| 398 consecutive_failed_draws_++; | 568 consecutive_failed_draws_++; |
| 399 if (settings_.timeout_and_draw_when_animation_checkerboards && | 569 if (settings_.timeout_and_draw_when_animation_checkerboards && |
| 400 consecutive_failed_draws_ >= | 570 consecutive_failed_draws_ >= |
| 401 maximum_number_of_failed_draws_before_draw_is_forced_) { | 571 maximum_number_of_failed_draws_before_draw_is_forced_) { |
| 402 consecutive_failed_draws_ = 0; | 572 consecutive_failed_draws_ = 0; |
| 403 // We need to force a draw, but it doesn't make sense to do this until | 573 // We need to force a draw, but it doesn't make sense to do this until |
| 404 // we've committed and have new textures. | 574 // we've committed and have new textures. |
| 405 needs_forced_redraw_after_next_commit_ = true; | 575 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_COMMIT; |
| 406 } | 576 } |
| 407 } else { | 577 } else { |
| 408 consecutive_failed_draws_ = 0; | 578 consecutive_failed_draws_ = 0; |
| 409 } | 579 } |
| 410 } | 580 } |
| 411 | 581 |
| 412 void SchedulerStateMachine::SetNeedsCommit() { needs_commit_ = true; } | 582 void SchedulerStateMachine::SetNeedsCommit() { needs_commit_ = true; } |
| 413 | 583 |
| 414 void SchedulerStateMachine::SetNeedsForcedCommit() { | 584 void SchedulerStateMachine::SetNeedsForcedCommitForReadback() { |
| 415 needs_forced_commit_ = true; | 585 VLOG(0) << __LINE__ << "SetNeedsForcedCommitForReadback\n"; |
| 416 expect_immediate_begin_frame_for_main_thread_ = true; | 586 DCHECK_EQ(readback_state_, READBACK_STATE_IDLE); |
| 587 readback_state_ = READBACK_STATE_FORCED_COMMIT_REQUESTED; |
| 417 } | 588 } |
| 418 | 589 |
| 419 void SchedulerStateMachine::FinishCommit() { | 590 void SchedulerStateMachine::FinishCommit() { |
| 420 DCHECK(commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS || | 591 DCHECK(commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS || |
| 421 (expect_immediate_begin_frame_for_main_thread_ && | 592 (readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED && |
| 422 commit_state_ != COMMIT_STATE_IDLE)) | 593 commit_state_ != COMMIT_STATE_IDLE)) |
| 423 << ToString(); | 594 << ToString(); |
| 424 commit_state_ = COMMIT_STATE_READY_TO_COMMIT; | 595 commit_state_ = COMMIT_STATE_READY_TO_COMMIT; |
| 425 } | 596 } |
| 426 | 597 |
| 427 void SchedulerStateMachine::BeginFrameAbortedByMainThread() { | 598 void SchedulerStateMachine::BeginFrameAbortedByMainThread() { |
| 428 DCHECK_EQ(commit_state_, COMMIT_STATE_FRAME_IN_PROGRESS); | 599 DCHECK_EQ(commit_state_, COMMIT_STATE_FRAME_IN_PROGRESS) << ToString(); |
| 429 if (expect_immediate_begin_frame_for_main_thread_) { | 600 if (readback_state_ == READBACK_STATE_FORCED_COMMIT_REQUESTED) { |
| 430 expect_immediate_begin_frame_for_main_thread_ = false; | 601 readback_state_ = READBACK_STATE_IDLE; |
| 431 } else { | 602 } else { |
| 432 commit_state_ = COMMIT_STATE_IDLE; | 603 commit_state_ = COMMIT_STATE_IDLE; |
| 433 SetNeedsCommit(); | 604 SetNeedsCommit(); |
| 434 } | 605 } |
| 435 } | 606 } |
| 436 | 607 |
| 437 void SchedulerStateMachine::DidLoseOutputSurface() { | 608 void SchedulerStateMachine::DidLoseOutputSurface() { |
| 438 if (output_surface_state_ == OUTPUT_SURFACE_LOST || | 609 if (output_surface_state_ == OUTPUT_SURFACE_LOST || |
| 439 output_surface_state_ == OUTPUT_SURFACE_CREATING) | 610 output_surface_state_ == OUTPUT_SURFACE_CREATING) |
| 440 return; | 611 return; |
| 441 output_surface_state_ = OUTPUT_SURFACE_LOST; | 612 output_surface_state_ = OUTPUT_SURFACE_LOST; |
| 442 } | 613 } |
| 443 | 614 |
| 444 void SchedulerStateMachine::SetHasPendingTree(bool has_pending_tree) { | 615 void SchedulerStateMachine::SetHasTrees(bool has_pending_tree, |
| 616 bool active_tree_is_null) { |
| 617 if (has_pending_tree_ && !has_pending_tree) { |
| 618 // This is a new activation. |
| 619 VLOG(0) << __LINE__ << "ACTIVATED!\n"; |
| 620 draw_attempt_count_++; |
| 621 active_tree_has_been_drawn_ = false; |
| 622 |
| 623 if (draw_if_possible_failed_) |
| 624 last_begin_frame_count_draw_was_called_ = -1; |
| 625 |
| 626 if (COMMIT_STATE_WAITING_FOR_ACTIVATION == commit_state_) { |
| 627 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION) { |
| 628 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 629 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_DRAW; |
| 630 } |
| 631 else if(readback_state_ == READBACK_STATE_WAITING_FOR_ACTIVATION) { |
| 632 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 633 readback_state_ = READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK; |
| 634 } else { |
| 635 commit_state_ = COMMIT_STATE_IDLE; |
| 636 if (readback_state_ == READBACK_STATE_REPLACEMENT_COMMIT_ACTIVATING) |
| 637 readback_state_ = READBACK_STATE_IDLE; |
| 638 } |
| 639 } |
| 640 } |
| 641 |
| 445 has_pending_tree_ = has_pending_tree; | 642 has_pending_tree_ = has_pending_tree; |
| 643 active_tree_is_null_ = active_tree_is_null; |
| 446 } | 644 } |
| 447 | 645 |
| 448 void SchedulerStateMachine::SetCanDraw(bool can) { can_draw_ = can; } | 646 void SchedulerStateMachine::SetCanDraw(bool can_draw) { |
| 647 can_draw_ = can_draw; |
| 648 } |
| 449 | 649 |
| 450 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() { | 650 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() { |
| 451 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING); | 651 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING); |
| 452 output_surface_state_ = OUTPUT_SURFACE_ACTIVE; | 652 output_surface_state_ = OUTPUT_SURFACE_ACTIVE; |
| 453 | 653 |
| 454 if (did_create_and_initialize_first_output_surface_) { | 654 if (did_create_and_initialize_first_output_surface_) { |
| 455 // TODO(boliu): See if we can remove this when impl-side painting is always | 655 // TODO(boliu): See if we can remove this when impl-side painting is always |
| 456 // on. Does anything on the main thread need to update after recreate? | 656 // on. Does anything on the main thread need to update after recreate? |
| 457 needs_commit_ = true; | 657 needs_commit_ = true; |
| 458 // If anything has requested a redraw, we don't want to actually draw | 658 // If anything has requested a redraw, we don't want to actually draw |
| 459 // when the output surface is restored until things have a chance to | 659 // when the output surface is restored until things have a chance to |
| 460 // sort themselves out with a commit. | 660 // sort themselves out with a commit. |
| 461 needs_redraw_ = false; | 661 needs_redraw_ = false; |
| 462 } | 662 } |
| 463 did_create_and_initialize_first_output_surface_ = true; | 663 did_create_and_initialize_first_output_surface_ = true; |
| 464 } | 664 } |
| 465 | 665 |
| 466 bool SchedulerStateMachine::HasInitializedOutputSurface() const { | 666 bool SchedulerStateMachine::HasInitializedOutputSurface() const { |
| 467 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; | 667 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; |
| 468 } | 668 } |
| 469 | 669 |
| 470 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( | 670 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( |
| 471 int num_draws) { | 671 int num_draws) { |
| 472 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; | 672 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; |
| 473 } | 673 } |
| 474 | 674 |
| 475 } // namespace cc | 675 } // namespace cc |
| OLD | NEW |