| 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 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 begin_frame_count_(0), |
| 18 last_frame_number_where_draw_was_called_(-1), | 18 begin_frame_deadline_count_(0), |
| 19 last_frame_number_where_tree_activation_attempted_(-1), | 19 draw_attempt_count_(0), |
| 20 last_frame_number_where_check_for_completed_tile_uploads_called_(-1), | 20 last_begin_frame_count_draw_was_called_(-1), |
| 21 last_begin_frame_count_begin_frame_sent_to_main_thread_(-1), |
| 22 last_draw_attempt_count_tree_activation_attempted_(-1), |
| 23 last_draw_attempt_count_completed_tile_uploads_checked_(-1), |
| 21 consecutive_failed_draws_(0), | 24 consecutive_failed_draws_(0), |
| 22 maximum_number_of_failed_draws_before_draw_is_forced_(3), | 25 maximum_number_of_failed_draws_before_draw_is_forced_(3), |
| 23 needs_redraw_(false), | 26 needs_redraw_(false), |
| 24 swap_used_incomplete_tile_(false), | 27 swap_used_incomplete_tile_(false), |
| 25 needs_forced_redraw_(false), | 28 needs_forced_redraw_(false), |
| 26 needs_forced_redraw_after_next_commit_(false), | 29 needs_forced_redraw_after_next_commit_(false), |
| 27 needs_commit_(false), | 30 needs_commit_(false), |
| 28 needs_forced_commit_(false), | 31 needs_forced_commit_(false), |
| 29 expect_immediate_begin_frame_for_main_thread_(false), | 32 expect_immediate_begin_frame_for_main_thread_(false), |
| 30 main_thread_needs_layer_textures_(false), | 33 main_thread_needs_layer_textures_(false), |
| 31 inside_begin_frame_(false), | 34 begin_frame_state_(BEGIN_FRAME_STATE_IDLE), |
| 35 commit_tree_has_been_drawn_(true), |
| 36 active_tree_has_been_drawn_(false), |
| 32 visible_(false), | 37 visible_(false), |
| 33 can_start_(false), | 38 can_start_(false), |
| 34 can_draw_(false), | 39 can_draw_(false), |
| 35 has_pending_tree_(false), | 40 has_pending_tree_(false), |
| 36 draw_if_possible_failed_(false), | 41 draw_if_possible_failed_(false), |
| 37 texture_state_(LAYER_TEXTURE_STATE_UNLOCKED), | 42 texture_state_(LAYER_TEXTURE_STATE_UNLOCKED), |
| 38 output_surface_state_(OUTPUT_SURFACE_LOST), | 43 output_surface_state_(OUTPUT_SURFACE_LOST), |
| 39 did_create_and_initialize_first_output_surface_(false) {} | 44 did_create_and_initialize_first_output_surface_(false) {} |
| 40 | 45 |
| 41 std::string SchedulerStateMachine::ToString() { | 46 std::string SchedulerStateMachine::ToString() { |
| 42 std::string str; | 47 std::string str; |
| 43 base::StringAppendF(&str, | 48 base::StringAppendF(&str, |
| 44 "settings_.impl_side_painting = %d; ", | 49 "settings_.impl_side_painting = %d; ", |
| 45 settings_.impl_side_painting); | 50 settings_.impl_side_painting); |
| 46 base::StringAppendF(&str, "commit_state_ = %d; ", commit_state_); | 51 base::StringAppendF(&str, "commit_state_ = %d; ", commit_state_); |
| 47 base::StringAppendF(&str, "commit_count_ = %d; ", commit_count_); | 52 base::StringAppendF(&str, "commit_count_ = %d; ", commit_count_); |
| 48 base::StringAppendF( | 53 base::StringAppendF(&str, "begin_frame_count_ = %d; ", |
| 49 &str, "current_frame_number_ = %d; ", current_frame_number_); | 54 begin_frame_count_); |
| 55 base::StringAppendF(&str, "draw_attempt_count_ = %d; ", |
| 56 draw_attempt_count_); |
| 50 base::StringAppendF(&str, | 57 base::StringAppendF(&str, |
| 51 "last_frame_number_where_draw_was_called_ = %d; ", | 58 "last_begin_frame_count_draw_was_called_ = %d; ", |
| 52 last_frame_number_where_draw_was_called_); | 59 last_begin_frame_count_draw_was_called_); |
| 53 base::StringAppendF( | 60 base::StringAppendF( |
| 54 &str, | 61 &str, |
| 55 "last_frame_number_where_tree_activation_attempted_ = %d; ", | 62 "last_draw_attempt_count_tree_activation_attempted_ = %d; ", |
| 56 last_frame_number_where_tree_activation_attempted_); | 63 last_draw_attempt_count_tree_activation_attempted_); |
| 57 base::StringAppendF( | 64 base::StringAppendF( |
| 58 &str, | 65 &str, |
| 59 "last_frame_number_where_check_for_completed_tile_uploads_called_ = %d; ", | 66 "last_draw_attempt_count_completed_tile_uploads_checked_ = %d; ", |
| 60 last_frame_number_where_check_for_completed_tile_uploads_called_); | 67 last_draw_attempt_count_completed_tile_uploads_checked_); |
| 61 base::StringAppendF( | 68 base::StringAppendF( |
| 62 &str, "consecutive_failed_draws_ = %d; ", consecutive_failed_draws_); | 69 &str, "consecutive_failed_draws_ = %d; ", consecutive_failed_draws_); |
| 63 base::StringAppendF( | 70 base::StringAppendF( |
| 64 &str, | 71 &str, |
| 65 "maximum_number_of_failed_draws_before_draw_is_forced_ = %d; ", | 72 "maximum_number_of_failed_draws_before_draw_is_forced_ = %d; ", |
| 66 maximum_number_of_failed_draws_before_draw_is_forced_); | 73 maximum_number_of_failed_draws_before_draw_is_forced_); |
| 67 base::StringAppendF(&str, "needs_redraw_ = %d; ", needs_redraw_); | 74 base::StringAppendF(&str, "needs_redraw_ = %d; ", needs_redraw_); |
| 68 base::StringAppendF( | 75 base::StringAppendF( |
| 69 &str, "swap_used_incomplete_tile_ = %d; ", swap_used_incomplete_tile_); | 76 &str, "swap_used_incomplete_tile_ = %d; ", swap_used_incomplete_tile_); |
| 70 base::StringAppendF( | 77 base::StringAppendF( |
| 71 &str, "needs_forced_redraw_ = %d; ", needs_forced_redraw_); | 78 &str, "needs_forced_redraw_ = %d; ", needs_forced_redraw_); |
| 72 base::StringAppendF(&str, | 79 base::StringAppendF(&str, |
| 73 "needs_forced_redraw_after_next_commit_ = %d; ", | 80 "needs_forced_redraw_after_next_commit_ = %d; ", |
| 74 needs_forced_redraw_after_next_commit_); | 81 needs_forced_redraw_after_next_commit_); |
| 75 base::StringAppendF(&str, "needs_commit_ = %d; ", needs_commit_); | 82 base::StringAppendF(&str, "needs_commit_ = %d; ", needs_commit_); |
| 76 base::StringAppendF( | 83 base::StringAppendF( |
| 77 &str, "needs_forced_commit_ = %d; ", needs_forced_commit_); | 84 &str, "needs_forced_commit_ = %d; ", needs_forced_commit_); |
| 78 base::StringAppendF(&str, | 85 base::StringAppendF(&str, |
| 79 "expect_immediate_begin_frame_for_main_thread_ = %d; ", | 86 "expect_immediate_begin_frame_for_main_thread_ = %d; ", |
| 80 expect_immediate_begin_frame_for_main_thread_); | 87 expect_immediate_begin_frame_for_main_thread_); |
| 81 base::StringAppendF(&str, | 88 base::StringAppendF(&str, |
| 82 "main_thread_needs_layer_textures_ = %d; ", | 89 "main_thread_needs_layer_textures_ = %d; ", |
| 83 main_thread_needs_layer_textures_); | 90 main_thread_needs_layer_textures_); |
| 84 base::StringAppendF(&str, "inside_begin_frame_ = %d; ", | 91 base::StringAppendF(&str, "begin_frame_state_ = %d; ", begin_frame_state_); |
| 85 inside_begin_frame_); | |
| 86 base::StringAppendF(&str, "last_frame_time_ = %"PRId64"; ", | 92 base::StringAppendF(&str, "last_frame_time_ = %"PRId64"; ", |
| 87 (last_begin_frame_args_.frame_time - base::TimeTicks()) | 93 (last_begin_frame_args_.frame_time - base::TimeTicks()) |
| 88 .InMilliseconds()); | 94 .InMilliseconds()); |
| 89 base::StringAppendF(&str, "last_deadline_ = %"PRId64"; ", | 95 base::StringAppendF(&str, "last_deadline_ = %"PRId64"; ", |
| 90 (last_begin_frame_args_.deadline - base::TimeTicks()).InMilliseconds()); | 96 (last_begin_frame_args_.deadline - base::TimeTicks()).InMilliseconds()); |
| 91 base::StringAppendF(&str, "last_interval_ = %"PRId64"; ", | 97 base::StringAppendF(&str, "last_interval_ = %"PRId64"; ", |
| 92 last_begin_frame_args_.interval.InMilliseconds()); | 98 last_begin_frame_args_.interval.InMilliseconds()); |
| 93 base::StringAppendF(&str, "visible_ = %d; ", visible_); | 99 base::StringAppendF(&str, "visible_ = %d; ", visible_); |
| 94 base::StringAppendF(&str, "can_start_ = %d; ", can_start_); | 100 base::StringAppendF(&str, "can_start_ = %d; ", can_start_); |
| 95 base::StringAppendF(&str, "can_draw_ = %d; ", can_draw_); | 101 base::StringAppendF(&str, "can_draw_ = %d; ", can_draw_); |
| 96 base::StringAppendF( | 102 base::StringAppendF( |
| 97 &str, "draw_if_possible_failed_ = %d; ", draw_if_possible_failed_); | 103 &str, "draw_if_possible_failed_ = %d; ", draw_if_possible_failed_); |
| 98 base::StringAppendF(&str, "has_pending_tree_ = %d; ", has_pending_tree_); | 104 base::StringAppendF(&str, "has_pending_tree_ = %d; ", has_pending_tree_); |
| 105 base::StringAppendF(&str, "commit_tree_has_been_drawn_ = %d; ", |
| 106 commit_tree_has_been_drawn_); |
| 107 base::StringAppendF(&str, "active_tree_has_been_drawn_ = %d; ", |
| 108 active_tree_has_been_drawn_); |
| 99 base::StringAppendF(&str, "texture_state_ = %d; ", texture_state_); | 109 base::StringAppendF(&str, "texture_state_ = %d; ", texture_state_); |
| 100 base::StringAppendF( | 110 base::StringAppendF( |
| 101 &str, "output_surface_state_ = %d; ", output_surface_state_); | 111 &str, "output_surface_state_ = %d; ", output_surface_state_); |
| 102 return str; | 112 return str; |
| 103 } | 113 } |
| 104 | 114 |
| 105 bool SchedulerStateMachine::HasDrawnThisFrame() const { | 115 bool SchedulerStateMachine::HasDrawnThisFrame() const { |
| 106 return current_frame_number_ == last_frame_number_where_draw_was_called_; | 116 return begin_frame_count_ == last_begin_frame_count_draw_was_called_; |
| 107 } | 117 } |
| 108 | 118 |
| 109 bool SchedulerStateMachine::HasAttemptedTreeActivationThisFrame() const { | 119 bool SchedulerStateMachine::HasSentBeginFrameToMainThreadThisFrame() const { |
| 110 return current_frame_number_ == | 120 return begin_frame_count_ == |
| 111 last_frame_number_where_tree_activation_attempted_; | 121 last_begin_frame_count_begin_frame_sent_to_main_thread_; |
| 112 } | 122 } |
| 113 | 123 |
| 114 bool SchedulerStateMachine::HasCheckedForCompletedTileUploadsThisFrame() const { | 124 bool SchedulerStateMachine::HasAttemptedTreeActivationThisDrawAttempt() const { |
| 115 return current_frame_number_ == | 125 return draw_attempt_count_ == |
| 116 last_frame_number_where_check_for_completed_tile_uploads_called_; | 126 last_draw_attempt_count_tree_activation_attempted_; |
| 127 } |
| 128 |
| 129 bool SchedulerStateMachine:: |
| 130 HasCheckedForCompletedTileUploadsThisDrawAttempt() const { |
| 131 return draw_attempt_count_ == |
| 132 last_draw_attempt_count_completed_tile_uploads_checked_; |
| 117 } | 133 } |
| 118 | 134 |
| 119 bool SchedulerStateMachine::DrawSuspendedUntilCommit() const { | 135 bool SchedulerStateMachine::DrawSuspendedUntilCommit() const { |
| 120 if (!can_draw_) | 136 if (!can_draw_) |
| 121 return true; | 137 return true; |
| 122 if (!visible_) | 138 if (!visible_) |
| 123 return true; | 139 return true; |
| 124 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD) | 140 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD) |
| 125 return true; | 141 return true; |
| 126 return false; | 142 return false; |
| 127 } | 143 } |
| 128 | 144 |
| 129 bool SchedulerStateMachine::ScheduledToDraw() const { | 145 bool SchedulerStateMachine::ScheduledToDraw() const { |
| 130 if (!needs_redraw_) | 146 if (!needs_redraw_) |
| 131 return false; | 147 return false; |
| 132 if (DrawSuspendedUntilCommit()) | 148 if (DrawSuspendedUntilCommit()) |
| 133 return false; | 149 return false; |
| 134 return true; | 150 return true; |
| 135 } | 151 } |
| 136 | 152 |
| 137 bool SchedulerStateMachine::ShouldDraw() const { | 153 bool SchedulerStateMachine::ShouldDraw() const { |
| 138 if (needs_forced_redraw_) | 154 if (needs_forced_redraw_) |
| 139 return true; | 155 return true; |
| 140 | 156 |
| 141 if (!ScheduledToDraw()) | 157 if (!ScheduledToDraw()) |
| 142 return false; | 158 return false; |
| 143 if (!inside_begin_frame_) | 159 if (begin_frame_state_ != BEGIN_FRAME_STATE_INSIDE_DEADLINE) |
| 144 return false; | 160 return false; |
| 145 if (HasDrawnThisFrame()) | 161 if (HasDrawnThisFrame()) |
| 146 return false; | 162 return false; |
| 147 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE) | 163 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE) |
| 148 return false; | 164 return false; |
| 149 return true; | 165 return true; |
| 150 } | 166 } |
| 151 | 167 |
| 152 bool SchedulerStateMachine::ShouldAttemptTreeActivation() const { | 168 bool SchedulerStateMachine::ShouldAttemptTreeActivation() const { |
| 153 return has_pending_tree_ && inside_begin_frame_ && | 169 return !HasAttemptedTreeActivationThisDrawAttempt() && |
| 154 !HasAttemptedTreeActivationThisFrame(); | 170 has_pending_tree_ && |
| 171 (!can_draw_ || !visible_ || |
| 172 (active_tree_has_been_drawn_ && |
| 173 begin_frame_state_ != BEGIN_FRAME_STATE_IDLE)); |
| 155 } | 174 } |
| 156 | 175 |
| 157 bool SchedulerStateMachine::ShouldCheckForCompletedTileUploads() const { | 176 bool SchedulerStateMachine::ShouldCheckForCompletedTileUploads() const { |
| 158 if (!settings_.impl_side_painting) | 177 if (!settings_.impl_side_painting) |
| 159 return false; | 178 return false; |
| 160 if (HasCheckedForCompletedTileUploadsThisFrame()) | 179 if (HasCheckedForCompletedTileUploadsThisDrawAttempt()) |
| 161 return false; | 180 return false; |
| 162 | 181 |
| 163 return ShouldAttemptTreeActivation() || ShouldDraw() || | 182 return ShouldAttemptTreeActivation() || ShouldDraw() || |
| 164 swap_used_incomplete_tile_; | 183 swap_used_incomplete_tile_; |
| 165 } | 184 } |
| 166 | 185 |
| 167 bool SchedulerStateMachine::ShouldAcquireLayerTexturesForMainThread() const { | 186 bool SchedulerStateMachine::ShouldAcquireLayerTexturesForMainThread() const { |
| 168 if (!main_thread_needs_layer_textures_) | 187 if (!main_thread_needs_layer_textures_) |
| 169 return false; | 188 return false; |
| 170 if (texture_state_ == LAYER_TEXTURE_STATE_UNLOCKED) | 189 if (texture_state_ == LAYER_TEXTURE_STATE_UNLOCKED) |
| 171 return true; | 190 return true; |
| 172 DCHECK_EQ(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD); | 191 DCHECK_EQ(texture_state_, LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD); |
| 173 // Transfer the lock from impl thread to main thread immediately if the | 192 // Transfer the lock from impl thread to main thread immediately if the |
| 174 // impl thread is not even scheduled to draw. Guards against deadlocking. | 193 // impl thread is not even scheduled to draw. Guards against deadlocking. |
| 175 if (!ScheduledToDraw()) | 194 if (!ScheduledToDraw()) |
| 176 return true; | 195 return true; |
| 177 if (!BeginFrameNeededToDrawByImplThread()) | 196 if (!BeginFrameNeededToDrawByImplThread()) |
| 178 return true; | 197 return true; |
| 179 return false; | 198 return false; |
| 180 } | 199 } |
| 181 | 200 |
| 201 bool SchedulerStateMachine::ShouldSendBeginFrameToMainThread() const { |
| 202 if (HasSentBeginFrameToMainThreadThisFrame()) |
| 203 return false; |
| 204 |
| 205 // TODO(brianderson): Allow sending BeginFrame to main thread while idle when |
| 206 // the main thread isn't consuming user input? |
| 207 bool can_expect_begin_frame = BeginFrameNeededToDrawByImplThread() || |
| 208 ProactiveBeginFrameWantedByImplThread(); |
| 209 if (can_expect_begin_frame && |
| 210 begin_frame_state_ == BEGIN_FRAME_STATE_IDLE) |
| 211 return false; |
| 212 |
| 213 // Do not send begin frame to main thread in the deadline until we have drawn. |
| 214 if ((can_expect_begin_frame || WillDrawNewTree()) && |
| 215 begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_DEADLINE) |
| 216 return false; |
| 217 |
| 218 // We can't accept a commit if we have a pending tree. |
| 219 if (has_pending_tree_) |
| 220 return false; |
| 221 |
| 222 bool can_commit = needs_commit_ && (visible_ || needs_forced_commit_); |
| 223 switch (commit_state_) { |
| 224 case COMMIT_STATE_IDLE: |
| 225 return can_commit && (needs_forced_commit_ || |
| 226 output_surface_state_ == OUTPUT_SURFACE_ACTIVE); |
| 227 |
| 228 // COMMIT_STATE_WAITING_FOR_ACTIVATION wants to enforce activation and |
| 229 // eventually a draw. |
| 230 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. |
| 231 // If can_draw_ is false or textures are not available, proceed to the |
| 232 // next step (similar as in COMMIT_STATE_IDLE). |
| 233 case COMMIT_STATE_WAITING_FOR_ACTIVATION: |
| 234 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: |
| 235 return can_commit && DrawSuspendedUntilCommit(); |
| 236 default: |
| 237 return false; |
| 238 } |
| 239 |
| 240 return false; |
| 241 } |
| 242 |
| 182 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const { | 243 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const { |
| 183 if (ShouldAcquireLayerTexturesForMainThread()) | 244 if (ShouldAcquireLayerTexturesForMainThread()) |
| 184 return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD; | 245 return ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD; |
| 185 | 246 |
| 186 switch (commit_state_) { | 247 switch (commit_state_) { |
| 187 case COMMIT_STATE_IDLE: | 248 case COMMIT_STATE_IDLE: |
| 188 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && | 249 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && |
| 189 needs_forced_redraw_) | 250 needs_forced_redraw_) |
| 190 return ACTION_DRAW_FORCED; | 251 return ACTION_DRAW_FORCED; |
| 191 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && | 252 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE && |
| 192 needs_forced_commit_) | 253 needs_forced_commit_) |
| 193 // TODO(enne): Should probably drop the active tree on force commit. | 254 // TODO(enne): Should probably drop the active tree on force commit. |
| 194 return has_pending_tree_ ? ACTION_NONE | 255 return has_pending_tree_ ? ACTION_NONE |
| 195 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; | 256 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; |
| 196 if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_) | 257 if (output_surface_state_ == OUTPUT_SURFACE_LOST && can_start_) |
| 197 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION; | 258 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION; |
| 198 if (output_surface_state_ == OUTPUT_SURFACE_CREATING) | 259 if (output_surface_state_ == OUTPUT_SURFACE_CREATING) |
| 199 return ACTION_NONE; | 260 return ACTION_NONE; |
| 200 if (ShouldCheckForCompletedTileUploads()) | 261 if (ShouldCheckForCompletedTileUploads()) |
| 201 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 262 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 202 if (ShouldAttemptTreeActivation()) | 263 if (ShouldAttemptTreeActivation()) |
| 203 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 264 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 204 if (ShouldDraw()) { | 265 if (ShouldDraw()) { |
| 205 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 266 return needs_forced_redraw_ ? ACTION_DRAW_FORCED |
| 206 : ACTION_DRAW_IF_POSSIBLE; | 267 : ACTION_DRAW_IF_POSSIBLE; |
| 207 } | 268 } |
| 208 if (needs_commit_ && | 269 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. | 270 // TODO(enne): Should probably drop the active tree on force commit. |
| 212 return has_pending_tree_ ? ACTION_NONE | 271 return ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; |
| 213 : ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; | 272 } |
| 214 return ACTION_NONE; | 273 return ACTION_NONE; |
| 215 | 274 |
| 216 case COMMIT_STATE_FRAME_IN_PROGRESS: | 275 case COMMIT_STATE_FRAME_IN_PROGRESS: |
| 217 if (ShouldCheckForCompletedTileUploads()) | 276 if (ShouldCheckForCompletedTileUploads()) |
| 218 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 277 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 219 if (ShouldAttemptTreeActivation()) | 278 if (ShouldAttemptTreeActivation()) |
| 220 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 279 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 221 if (ShouldDraw()) { | 280 if (ShouldDraw()) { |
| 222 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 281 return needs_forced_redraw_ ? ACTION_DRAW_FORCED |
| 223 : ACTION_DRAW_IF_POSSIBLE; | 282 : ACTION_DRAW_IF_POSSIBLE; |
| 224 } | 283 } |
| 225 return ACTION_NONE; | 284 return ACTION_NONE; |
| 226 | 285 |
| 227 case COMMIT_STATE_READY_TO_COMMIT: | 286 case COMMIT_STATE_READY_TO_COMMIT: |
| 228 return ACTION_COMMIT; | 287 return ACTION_COMMIT; |
| 229 | 288 |
| 289 case COMMIT_STATE_WAITING_FOR_ACTIVATION: |
| 230 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { | 290 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { |
| 231 if (ShouldCheckForCompletedTileUploads()) | 291 if (ShouldCheckForCompletedTileUploads()) |
| 232 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 292 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 233 if (ShouldAttemptTreeActivation()) | 293 if (ShouldAttemptTreeActivation()) |
| 234 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 294 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 235 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) { | 295 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) { |
| 236 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 296 return needs_forced_redraw_ ? ACTION_DRAW_FORCED |
| 237 : ACTION_DRAW_IF_POSSIBLE; | 297 : ACTION_DRAW_IF_POSSIBLE; |
| 238 } | 298 } |
| 239 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. If | 299 if (ShouldSendBeginFrameToMainThread()) |
| 240 // can_draw_ is false or textures are not available, proceed to the next | 300 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; | 301 return ACTION_NONE; |
| 247 } | 302 } |
| 248 | 303 |
| 249 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: | 304 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: |
| 250 if (ShouldCheckForCompletedTileUploads()) | 305 if (ShouldCheckForCompletedTileUploads()) |
| 251 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 306 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
| 252 if (ShouldAttemptTreeActivation()) | 307 if (ShouldAttemptTreeActivation()) |
| 253 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 308 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
| 254 if (needs_forced_redraw_) | 309 if (needs_forced_redraw_) |
| 255 return ACTION_DRAW_FORCED; | 310 return ACTION_DRAW_FORCED; |
| 256 return ACTION_NONE; | 311 return ACTION_NONE; |
| 257 } | 312 } |
| 258 NOTREACHED(); | 313 NOTREACHED(); |
| 259 return ACTION_NONE; | 314 return ACTION_NONE; |
| 260 } | 315 } |
| 261 | 316 |
| 262 void SchedulerStateMachine::UpdateState(Action action) { | 317 void SchedulerStateMachine::UpdateState(Action action) { |
| 263 switch (action) { | 318 switch (action) { |
| 264 case ACTION_NONE: | 319 case ACTION_NONE: |
| 320 if (begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME) |
| 321 begin_frame_state_ = BEGIN_FRAME_STATE_DEADLINE_PENDING; |
| 322 if (begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_DEADLINE) |
| 323 begin_frame_state_ = BEGIN_FRAME_STATE_IDLE; |
| 265 return; | 324 return; |
| 266 | 325 |
| 267 case ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: | 326 case ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: |
| 268 last_frame_number_where_check_for_completed_tile_uploads_called_ = | 327 last_draw_attempt_count_completed_tile_uploads_checked_ = |
| 269 current_frame_number_; | 328 draw_attempt_count_; |
| 270 return; | 329 return; |
| 271 | 330 |
| 272 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: | 331 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: |
| 273 last_frame_number_where_tree_activation_attempted_ = | 332 last_draw_attempt_count_tree_activation_attempted_ = |
| 274 current_frame_number_; | 333 draw_attempt_count_; |
| 275 return; | 334 return; |
| 276 | 335 |
| 277 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: | 336 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: |
| 278 DCHECK(!has_pending_tree_); | 337 DCHECK(!has_pending_tree_); |
| 279 DCHECK(visible_ || needs_forced_commit_); | 338 DCHECK(visible_ || needs_forced_commit_); |
| 280 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; | 339 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; |
| 281 needs_commit_ = false; | 340 needs_commit_ = false; |
| 282 needs_forced_commit_ = false; | 341 needs_forced_commit_ = false; |
| 342 last_begin_frame_count_begin_frame_sent_to_main_thread_ = |
| 343 begin_frame_count_; |
| 283 return; | 344 return; |
| 284 | 345 |
| 285 case ACTION_COMMIT: | 346 case ACTION_COMMIT: |
| 286 commit_count_++; | 347 commit_count_++; |
| 287 if (expect_immediate_begin_frame_for_main_thread_) | 348 if (expect_immediate_begin_frame_for_main_thread_) |
| 288 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; | 349 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
| 350 else if (settings_.impl_side_painting) |
| 351 commit_state_ = COMMIT_STATE_WAITING_FOR_ACTIVATION; |
| 289 else | 352 else |
| 290 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; | 353 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; |
| 291 // When impl-side painting, we draw on activation instead of on commit. | 354 // When impl-side painting, we draw on activation instead of on commit. |
| 292 if (!settings_.impl_side_painting) | 355 if (!settings_.impl_side_painting) |
| 293 needs_redraw_ = true; | 356 needs_redraw_ = true; |
| 294 if (draw_if_possible_failed_) | 357 if (draw_if_possible_failed_) |
| 295 last_frame_number_where_draw_was_called_ = -1; | 358 last_begin_frame_count_draw_was_called_ = -1; |
| 296 | 359 |
| 297 if (needs_forced_redraw_after_next_commit_) { | 360 if (needs_forced_redraw_after_next_commit_) { |
| 298 needs_forced_redraw_after_next_commit_ = false; | 361 needs_forced_redraw_after_next_commit_ = false; |
| 299 needs_forced_redraw_ = true; | 362 needs_forced_redraw_ = true; |
| 300 } | 363 } |
| 301 | 364 commit_tree_has_been_drawn_ = false; |
| 302 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD; | 365 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD; |
| 303 return; | 366 return; |
| 304 | 367 |
| 305 case ACTION_DRAW_FORCED: | 368 case ACTION_DRAW_FORCED: |
| 306 case ACTION_DRAW_IF_POSSIBLE: | 369 case ACTION_DRAW_IF_POSSIBLE: |
| 307 needs_redraw_ = false; | 370 needs_redraw_ = false; |
| 308 needs_forced_redraw_ = false; | 371 needs_forced_redraw_ = false; |
| 309 draw_if_possible_failed_ = false; | 372 draw_if_possible_failed_ = false; |
| 310 swap_used_incomplete_tile_ = false; | 373 swap_used_incomplete_tile_ = false; |
| 311 if (inside_begin_frame_) | 374 last_begin_frame_count_draw_was_called_ = begin_frame_count_; |
| 312 last_frame_number_where_draw_was_called_ = current_frame_number_; | |
| 313 if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) { | 375 if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) { |
| 314 DCHECK(expect_immediate_begin_frame_for_main_thread_); | 376 DCHECK(expect_immediate_begin_frame_for_main_thread_); |
| 315 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; | 377 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; |
| 316 expect_immediate_begin_frame_for_main_thread_ = false; | 378 expect_immediate_begin_frame_for_main_thread_ = false; |
| 317 } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) { | 379 } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) { |
| 318 commit_state_ = COMMIT_STATE_IDLE; | 380 commit_state_ = COMMIT_STATE_IDLE; |
| 319 } | 381 } |
| 320 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD) | 382 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD) |
| 321 texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED; | 383 texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED; |
| 384 active_tree_has_been_drawn_ = true; |
| 385 commit_tree_has_been_drawn_ = true; |
| 322 return; | 386 return; |
| 323 | 387 |
| 324 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION: | 388 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION: |
| 325 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE); | 389 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE); |
| 326 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST); | 390 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST); |
| 327 output_surface_state_ = OUTPUT_SURFACE_CREATING; | 391 output_surface_state_ = OUTPUT_SURFACE_CREATING; |
| 328 return; | 392 return; |
| 329 | 393 |
| 330 case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: | 394 case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: |
| 331 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD; | 395 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 357 return true; | 421 return true; |
| 358 | 422 |
| 359 if (visible_ && swap_used_incomplete_tile_) | 423 if (visible_ && swap_used_incomplete_tile_) |
| 360 return true; | 424 return true; |
| 361 | 425 |
| 362 return needs_redraw_ && visible_ && | 426 return needs_redraw_ && visible_ && |
| 363 output_surface_state_ == OUTPUT_SURFACE_ACTIVE; | 427 output_surface_state_ == OUTPUT_SURFACE_ACTIVE; |
| 364 } | 428 } |
| 365 | 429 |
| 366 bool SchedulerStateMachine::ProactiveBeginFrameWantedByImplThread() const { | 430 bool SchedulerStateMachine::ProactiveBeginFrameWantedByImplThread() const { |
| 431 if (settings_.using_synchronous_renderer_compositor) |
| 432 return false; |
| 433 |
| 434 if ((!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE) && |
| 435 !(needs_forced_commit_ || needs_forced_redraw_)) |
| 436 return false; |
| 437 |
| 367 // We should proactively request a BeginFrame if a commit is pending. | 438 // We should proactively request a BeginFrame if a commit is pending. |
| 368 if (needs_commit_ || needs_forced_commit_ || | 439 if (needs_commit_ || needs_forced_commit_ || |
| 369 commit_state_ != COMMIT_STATE_IDLE) | 440 commit_state_ != COMMIT_STATE_IDLE) |
| 370 return true; | 441 return true; |
| 371 | 442 |
| 372 return false; | 443 return false; |
| 373 } | 444 } |
| 374 | 445 |
| 375 void SchedulerStateMachine::DidEnterBeginFrame(const BeginFrameArgs& args) { | 446 void SchedulerStateMachine::OnBeginFrame(const BeginFrameArgs& args) { |
| 376 inside_begin_frame_ = true; | 447 begin_frame_count_++; |
| 448 draw_attempt_count_++; |
| 377 last_begin_frame_args_ = args; | 449 last_begin_frame_args_ = args; |
| 450 begin_frame_state_ = BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME; |
| 378 } | 451 } |
| 379 | 452 |
| 380 void SchedulerStateMachine::DidLeaveBeginFrame() { | 453 bool SchedulerStateMachine::ShouldTriggerBeginFrameDeadlineEarly() const { |
| 381 current_frame_number_++; | 454 // TODO(brianderson): This should take into account multiple commit sources. |
| 382 inside_begin_frame_ = false; | 455 return begin_frame_state_ == BEGIN_FRAME_STATE_DEADLINE_PENDING && |
| 456 WillDrawNewTree(); |
| 383 } | 457 } |
| 384 | 458 |
| 385 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; } | 459 bool SchedulerStateMachine::InsideBeginFrame() const { |
| 460 return begin_frame_state_ != BEGIN_FRAME_STATE_IDLE; |
| 461 } |
| 462 |
| 463 bool SchedulerStateMachine::WillDrawNewTree() const { |
| 464 if (settings_.impl_side_painting) |
| 465 return !active_tree_has_been_drawn_; |
| 466 else |
| 467 return !commit_tree_has_been_drawn_; |
| 468 } |
| 469 |
| 470 void SchedulerStateMachine::OnBeginFrameDeadline() { |
| 471 begin_frame_deadline_count_++; |
| 472 draw_attempt_count_++; |
| 473 if (begin_frame_state_ == BEGIN_FRAME_STATE_DEADLINE_PENDING) |
| 474 begin_frame_state_ = BEGIN_FRAME_STATE_INSIDE_DEADLINE; |
| 475 } |
| 476 |
| 477 void SchedulerStateMachine::SetVisible(bool visible) { |
| 478 visible_ = visible; |
| 479 } |
| 386 | 480 |
| 387 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } | 481 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } |
| 388 | 482 |
| 389 void SchedulerStateMachine::DidSwapUseIncompleteTile() { | 483 void SchedulerStateMachine::DidSwapUseIncompleteTile() { |
| 390 swap_used_incomplete_tile_ = true; | 484 swap_used_incomplete_tile_ = true; |
| 391 } | 485 } |
| 392 | 486 |
| 393 void SchedulerStateMachine::SetNeedsForcedRedraw() { | 487 void SchedulerStateMachine::SetNeedsForcedRedraw() { |
| 394 needs_forced_redraw_ = true; | 488 needs_forced_redraw_ = true; |
| 395 } | 489 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 } | 532 } |
| 439 } | 533 } |
| 440 | 534 |
| 441 void SchedulerStateMachine::DidLoseOutputSurface() { | 535 void SchedulerStateMachine::DidLoseOutputSurface() { |
| 442 if (output_surface_state_ == OUTPUT_SURFACE_LOST || | 536 if (output_surface_state_ == OUTPUT_SURFACE_LOST || |
| 443 output_surface_state_ == OUTPUT_SURFACE_CREATING) | 537 output_surface_state_ == OUTPUT_SURFACE_CREATING) |
| 444 return; | 538 return; |
| 445 output_surface_state_ = OUTPUT_SURFACE_LOST; | 539 output_surface_state_ = OUTPUT_SURFACE_LOST; |
| 446 } | 540 } |
| 447 | 541 |
| 448 void SchedulerStateMachine::SetHasPendingTree(bool has_pending_tree) { | 542 void SchedulerStateMachine::SetHasTrees(bool has_pending_tree, |
| 543 bool active_tree_is_null) { |
| 544 if (has_pending_tree_ && !has_pending_tree) { |
| 545 // This is a new activation. |
| 546 draw_attempt_count_++; |
| 547 active_tree_has_been_drawn_ = false; |
| 548 if (COMMIT_STATE_WAITING_FOR_ACTIVATION == commit_state_) |
| 549 commit_state_ = COMMIT_STATE_IDLE; |
| 550 } |
| 551 |
| 449 has_pending_tree_ = has_pending_tree; | 552 has_pending_tree_ = has_pending_tree; |
| 553 |
| 554 if (active_tree_is_null) |
| 555 active_tree_has_been_drawn_ = true; |
| 450 } | 556 } |
| 451 | 557 |
| 452 void SchedulerStateMachine::SetCanDraw(bool can) { can_draw_ = can; } | 558 void SchedulerStateMachine::SetCanDraw(bool can_draw) { |
| 559 can_draw_ = can_draw; |
| 560 } |
| 453 | 561 |
| 454 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() { | 562 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() { |
| 455 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING); | 563 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING); |
| 456 output_surface_state_ = OUTPUT_SURFACE_ACTIVE; | 564 output_surface_state_ = OUTPUT_SURFACE_ACTIVE; |
| 457 | 565 |
| 458 if (did_create_and_initialize_first_output_surface_) { | 566 if (did_create_and_initialize_first_output_surface_) { |
| 459 // TODO(boliu): See if we can remove this when impl-side painting is always | 567 // TODO(boliu): See if we can remove this when impl-side painting is always |
| 460 // on. Does anything on the main thread need to update after recreate? | 568 // on. Does anything on the main thread need to update after recreate? |
| 461 needs_commit_ = true; | 569 needs_commit_ = true; |
| 462 // If anything has requested a redraw, we don't want to actually draw | 570 // If anything has requested a redraw, we don't want to actually draw |
| 463 // when the output surface is restored until things have a chance to | 571 // when the output surface is restored until things have a chance to |
| 464 // sort themselves out with a commit. | 572 // sort themselves out with a commit. |
| 465 needs_redraw_ = false; | 573 needs_redraw_ = false; |
| 466 } | 574 } |
| 467 did_create_and_initialize_first_output_surface_ = true; | 575 did_create_and_initialize_first_output_surface_ = true; |
| 468 } | 576 } |
| 469 | 577 |
| 470 bool SchedulerStateMachine::HasInitializedOutputSurface() const { | 578 bool SchedulerStateMachine::HasInitializedOutputSurface() const { |
| 471 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; | 579 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; |
| 472 } | 580 } |
| 473 | 581 |
| 474 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( | 582 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( |
| 475 int num_draws) { | 583 int num_draws) { |
| 476 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; | 584 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; |
| 477 } | 585 } |
| 478 | 586 |
| 479 } // namespace cc | 587 } // namespace cc |
| OLD | NEW |