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 (begin_frame_state_ == BEGIN_FRAME_STATE_IDLE && | |
210 can_expect_begin_frame) | |
211 return false; | |
212 | |
213 // Do not send begin frame to main thread in the deadline until we have drawn. | |
214 if (begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_DEADLINE && | |
215 !HasDrawnThisFrame()) | |
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: { | |
290 if (ShouldCheckForCompletedTileUploads()) | |
291 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | |
292 if (ShouldAttemptTreeActivation()) | |
293 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | |
294 // TODO(brianderson): How to handle OUTPUT_SURFACE_LOST. | |
brianderson
2013/07/09 01:50:48
I really need to understand how to handle OUTPUT_S
boliu
2013/07/09 23:33:27
ACTION_BEGIN_OUTPUT_SURFACE_CREATION post a task t
| |
295 if (ShouldDraw()) { | |
296 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | |
297 : ACTION_DRAW_IF_POSSIBLE; | |
298 } | |
299 if (ShouldSendBeginFrameToMainThread()) | |
300 return ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD; | |
301 return ACTION_NONE; | |
302 } | |
303 | |
230 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { | 304 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW: { |
231 if (ShouldCheckForCompletedTileUploads()) | 305 if (ShouldCheckForCompletedTileUploads()) |
232 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 306 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
233 if (ShouldAttemptTreeActivation()) | 307 if (ShouldAttemptTreeActivation()) |
234 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 308 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
235 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) { | 309 if (ShouldDraw() || output_surface_state_ == OUTPUT_SURFACE_LOST) { |
236 return needs_forced_redraw_ ? ACTION_DRAW_FORCED | 310 return needs_forced_redraw_ ? ACTION_DRAW_FORCED |
237 : ACTION_DRAW_IF_POSSIBLE; | 311 : ACTION_DRAW_IF_POSSIBLE; |
238 } | 312 } |
239 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW wants to enforce a draw. If | 313 if (ShouldSendBeginFrameToMainThread()) |
240 // can_draw_ is false or textures are not available, proceed to the next | 314 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; | 315 return ACTION_NONE; |
247 } | 316 } |
248 | 317 |
249 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: | 318 case COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW: |
250 if (ShouldCheckForCompletedTileUploads()) | 319 if (ShouldCheckForCompletedTileUploads()) |
251 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; | 320 return ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS; |
252 if (ShouldAttemptTreeActivation()) | 321 if (ShouldAttemptTreeActivation()) |
253 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; | 322 return ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED; |
254 if (needs_forced_redraw_) | 323 if (needs_forced_redraw_) |
255 return ACTION_DRAW_FORCED; | 324 return ACTION_DRAW_FORCED; |
256 return ACTION_NONE; | 325 return ACTION_NONE; |
257 } | 326 } |
258 NOTREACHED(); | 327 NOTREACHED(); |
259 return ACTION_NONE; | 328 return ACTION_NONE; |
260 } | 329 } |
261 | 330 |
262 void SchedulerStateMachine::UpdateState(Action action) { | 331 void SchedulerStateMachine::UpdateState(Action action) { |
263 switch (action) { | 332 switch (action) { |
264 case ACTION_NONE: | 333 case ACTION_NONE: |
334 if (begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME) | |
335 begin_frame_state_ = BEGIN_FRAME_STATE_DEADLINE_PENDING; | |
336 if (begin_frame_state_ == BEGIN_FRAME_STATE_INSIDE_DEADLINE) | |
337 begin_frame_state_ = BEGIN_FRAME_STATE_IDLE; | |
265 return; | 338 return; |
266 | 339 |
267 case ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: | 340 case ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: |
268 last_frame_number_where_check_for_completed_tile_uploads_called_ = | 341 last_draw_attempt_count_completed_tile_uploads_checked_ = |
269 current_frame_number_; | 342 draw_attempt_count_; |
270 return; | 343 return; |
271 | 344 |
272 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: | 345 case ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: |
273 last_frame_number_where_tree_activation_attempted_ = | 346 last_draw_attempt_count_tree_activation_attempted_ = |
274 current_frame_number_; | 347 draw_attempt_count_; |
275 return; | 348 return; |
276 | 349 |
277 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: | 350 case ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: |
278 DCHECK(!has_pending_tree_); | 351 DCHECK(!has_pending_tree_); |
279 DCHECK(visible_ || needs_forced_commit_); | 352 DCHECK(visible_ || needs_forced_commit_); |
280 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; | 353 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; |
281 needs_commit_ = false; | 354 needs_commit_ = false; |
282 needs_forced_commit_ = false; | 355 needs_forced_commit_ = false; |
356 last_begin_frame_count_begin_frame_sent_to_main_thread_ = | |
357 begin_frame_count_; | |
283 return; | 358 return; |
284 | 359 |
285 case ACTION_COMMIT: | 360 case ACTION_COMMIT: |
286 commit_count_++; | 361 commit_count_++; |
287 if (expect_immediate_begin_frame_for_main_thread_) | 362 if (expect_immediate_begin_frame_for_main_thread_) |
288 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; | 363 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW; |
364 else if (settings_.impl_side_painting) | |
365 commit_state_ = COMMIT_STATE_WAITING_FOR_ACTIVATION; | |
289 else | 366 else |
290 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; | 367 commit_state_ = COMMIT_STATE_WAITING_FOR_FIRST_DRAW; |
291 // When impl-side painting, we draw on activation instead of on commit. | 368 // When impl-side painting, we draw on activation instead of on commit. |
292 if (!settings_.impl_side_painting) | 369 if (!settings_.impl_side_painting) |
293 needs_redraw_ = true; | 370 needs_redraw_ = true; |
294 if (draw_if_possible_failed_) | 371 if (draw_if_possible_failed_) |
295 last_frame_number_where_draw_was_called_ = -1; | 372 last_begin_frame_count_draw_was_called_ = -1; |
296 | 373 |
297 if (needs_forced_redraw_after_next_commit_) { | 374 if (needs_forced_redraw_after_next_commit_) { |
298 needs_forced_redraw_after_next_commit_ = false; | 375 needs_forced_redraw_after_next_commit_ = false; |
299 needs_forced_redraw_ = true; | 376 needs_forced_redraw_ = true; |
300 } | 377 } |
301 | 378 commit_tree_has_been_drawn_ = false; |
302 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD; | 379 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD; |
303 return; | 380 return; |
304 | 381 |
305 case ACTION_DRAW_FORCED: | 382 case ACTION_DRAW_FORCED: |
306 case ACTION_DRAW_IF_POSSIBLE: | 383 case ACTION_DRAW_IF_POSSIBLE: |
307 needs_redraw_ = false; | 384 needs_redraw_ = false; |
308 needs_forced_redraw_ = false; | 385 needs_forced_redraw_ = false; |
309 draw_if_possible_failed_ = false; | 386 draw_if_possible_failed_ = false; |
310 swap_used_incomplete_tile_ = false; | 387 swap_used_incomplete_tile_ = false; |
311 if (inside_begin_frame_) | 388 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) { | 389 if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW) { |
314 DCHECK(expect_immediate_begin_frame_for_main_thread_); | 390 DCHECK(expect_immediate_begin_frame_for_main_thread_); |
315 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; | 391 commit_state_ = COMMIT_STATE_FRAME_IN_PROGRESS; |
316 expect_immediate_begin_frame_for_main_thread_ = false; | 392 expect_immediate_begin_frame_for_main_thread_ = false; |
317 } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) { | 393 } else if (commit_state_ == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) { |
318 commit_state_ = COMMIT_STATE_IDLE; | 394 commit_state_ = COMMIT_STATE_IDLE; |
319 } | 395 } |
320 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD) | 396 if (texture_state_ == LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD) |
321 texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED; | 397 texture_state_ = LAYER_TEXTURE_STATE_UNLOCKED; |
398 active_tree_has_been_drawn_ = true; | |
399 commit_tree_has_been_drawn_ = true; | |
322 return; | 400 return; |
323 | 401 |
324 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION: | 402 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION: |
325 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE); | 403 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE); |
326 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST); | 404 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST); |
327 output_surface_state_ = OUTPUT_SURFACE_CREATING; | 405 output_surface_state_ = OUTPUT_SURFACE_CREATING; |
328 return; | 406 return; |
329 | 407 |
330 case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: | 408 case ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: |
331 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD; | 409 texture_state_ = LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD; |
(...skipping 19 matching lines...) Expand all Loading... | |
351 return true; | 429 return true; |
352 | 430 |
353 if (visible_ && swap_used_incomplete_tile_) | 431 if (visible_ && swap_used_incomplete_tile_) |
354 return true; | 432 return true; |
355 | 433 |
356 return needs_redraw_ && visible_ && | 434 return needs_redraw_ && visible_ && |
357 output_surface_state_ == OUTPUT_SURFACE_ACTIVE; | 435 output_surface_state_ == OUTPUT_SURFACE_ACTIVE; |
358 } | 436 } |
359 | 437 |
360 bool SchedulerStateMachine::ProactiveBeginFrameWantedByImplThread() const { | 438 bool SchedulerStateMachine::ProactiveBeginFrameWantedByImplThread() const { |
361 // Do not be proactive when invisible. | 439 if (settings_.using_synchronous_renderer_compositor) |
362 if (!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE) | 440 return false; |
441 | |
442 if (!settings_.throttle_frame_production) | |
443 return false; | |
444 | |
445 if ((!visible_ || output_surface_state_ != OUTPUT_SURFACE_ACTIVE) && | |
446 !(needs_forced_commit_ || needs_forced_redraw_)) | |
363 return false; | 447 return false; |
364 | 448 |
365 // We should proactively request a BeginFrame if a commit or a tree activation | 449 // We should proactively request a BeginFrame if a commit or a tree activation |
366 // is pending. | 450 // is pending. |
367 return (needs_commit_ || needs_forced_commit_ || | 451 return (needs_commit_ || needs_forced_commit_ || |
368 commit_state_ != COMMIT_STATE_IDLE || has_pending_tree_); | 452 commit_state_ != COMMIT_STATE_IDLE || has_pending_tree_); |
369 } | 453 } |
370 | 454 |
371 void SchedulerStateMachine::DidEnterBeginFrame(const BeginFrameArgs& args) { | 455 void SchedulerStateMachine::OnBeginFrame(const BeginFrameArgs& args) { |
372 inside_begin_frame_ = true; | 456 begin_frame_count_++; |
457 draw_attempt_count_++; | |
373 last_begin_frame_args_ = args; | 458 last_begin_frame_args_ = args; |
459 begin_frame_state_ = BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME; | |
374 } | 460 } |
375 | 461 |
376 void SchedulerStateMachine::DidLeaveBeginFrame() { | 462 bool SchedulerStateMachine::ShouldTriggerBeginFrameDeadlineEarly() const { |
377 current_frame_number_++; | 463 // TODO(brianderson): This should take into account multiple commit sources. |
378 inside_begin_frame_ = false; | 464 return begin_frame_state_ == BEGIN_FRAME_STATE_DEADLINE_PENDING && |
465 WillDrawNewTree(); | |
379 } | 466 } |
380 | 467 |
381 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; } | 468 bool SchedulerStateMachine::InsideBeginFrame() const { |
469 return begin_frame_state_ != BEGIN_FRAME_STATE_IDLE; | |
470 } | |
471 | |
472 bool SchedulerStateMachine::WillDrawNewTree() const { | |
473 if (settings_.impl_side_painting) | |
474 return !active_tree_has_been_drawn_; | |
475 else | |
476 return !commit_tree_has_been_drawn_; | |
477 } | |
478 | |
479 void SchedulerStateMachine::OnBeginFrameDeadline() { | |
480 DCHECK_EQ(begin_frame_state_, BEGIN_FRAME_STATE_DEADLINE_PENDING); | |
481 begin_frame_deadline_count_++; | |
482 draw_attempt_count_++; | |
483 begin_frame_state_ = BEGIN_FRAME_STATE_INSIDE_DEADLINE; | |
484 } | |
485 | |
486 void SchedulerStateMachine::SetVisible(bool visible) { | |
487 visible_ = visible; | |
488 } | |
382 | 489 |
383 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } | 490 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; } |
384 | 491 |
385 void SchedulerStateMachine::DidSwapUseIncompleteTile() { | 492 void SchedulerStateMachine::DidSwapUseIncompleteTile() { |
386 swap_used_incomplete_tile_ = true; | 493 swap_used_incomplete_tile_ = true; |
387 } | 494 } |
388 | 495 |
389 void SchedulerStateMachine::SetNeedsForcedRedraw() { | 496 void SchedulerStateMachine::SetNeedsForcedRedraw() { |
390 needs_forced_redraw_ = true; | 497 needs_forced_redraw_ = true; |
391 } | 498 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
434 } | 541 } |
435 } | 542 } |
436 | 543 |
437 void SchedulerStateMachine::DidLoseOutputSurface() { | 544 void SchedulerStateMachine::DidLoseOutputSurface() { |
438 if (output_surface_state_ == OUTPUT_SURFACE_LOST || | 545 if (output_surface_state_ == OUTPUT_SURFACE_LOST || |
439 output_surface_state_ == OUTPUT_SURFACE_CREATING) | 546 output_surface_state_ == OUTPUT_SURFACE_CREATING) |
440 return; | 547 return; |
441 output_surface_state_ = OUTPUT_SURFACE_LOST; | 548 output_surface_state_ = OUTPUT_SURFACE_LOST; |
442 } | 549 } |
443 | 550 |
444 void SchedulerStateMachine::SetHasPendingTree(bool has_pending_tree) { | 551 void SchedulerStateMachine::SetHasTrees(bool has_pending_tree, |
552 bool active_tree_is_null) { | |
553 if (has_pending_tree_ && !has_pending_tree) { | |
554 // This is a new activation. | |
555 draw_attempt_count_++; | |
556 active_tree_has_been_drawn_ = false; | |
557 if (COMMIT_STATE_WAITING_FOR_ACTIVATION == commit_state_) | |
558 commit_state_ = COMMIT_STATE_IDLE; | |
559 } | |
560 | |
445 has_pending_tree_ = has_pending_tree; | 561 has_pending_tree_ = has_pending_tree; |
562 | |
563 if (active_tree_is_null) | |
564 active_tree_has_been_drawn_ = true; | |
446 } | 565 } |
447 | 566 |
448 void SchedulerStateMachine::SetCanDraw(bool can) { can_draw_ = can; } | 567 void SchedulerStateMachine::SetCanDraw(bool can_draw) { |
568 can_draw_ = can_draw; | |
569 } | |
449 | 570 |
450 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() { | 571 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() { |
451 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING); | 572 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING); |
452 output_surface_state_ = OUTPUT_SURFACE_ACTIVE; | 573 output_surface_state_ = OUTPUT_SURFACE_ACTIVE; |
453 | 574 |
454 if (did_create_and_initialize_first_output_surface_) { | 575 if (did_create_and_initialize_first_output_surface_) { |
455 // TODO(boliu): See if we can remove this when impl-side painting is always | 576 // 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? | 577 // on. Does anything on the main thread need to update after recreate? |
457 needs_commit_ = true; | 578 needs_commit_ = true; |
458 // If anything has requested a redraw, we don't want to actually draw | 579 // 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 | 580 // when the output surface is restored until things have a chance to |
460 // sort themselves out with a commit. | 581 // sort themselves out with a commit. |
461 needs_redraw_ = false; | 582 needs_redraw_ = false; |
462 } | 583 } |
463 did_create_and_initialize_first_output_surface_ = true; | 584 did_create_and_initialize_first_output_surface_ = true; |
464 } | 585 } |
465 | 586 |
466 bool SchedulerStateMachine::HasInitializedOutputSurface() const { | 587 bool SchedulerStateMachine::HasInitializedOutputSurface() const { |
467 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; | 588 return output_surface_state_ == OUTPUT_SURFACE_ACTIVE; |
468 } | 589 } |
469 | 590 |
470 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( | 591 void SchedulerStateMachine::SetMaximumNumberOfFailedDrawsBeforeDrawIsForced( |
471 int num_draws) { | 592 int num_draws) { |
472 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; | 593 maximum_number_of_failed_draws_before_draw_is_forced_ = num_draws; |
473 } | 594 } |
474 | 595 |
475 } // namespace cc | 596 } // namespace cc |
OLD | NEW |