OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 | |
7 #include "CCSchedulerStateMachine.h" | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 using namespace cc; | |
12 using namespace WTF; | |
13 | |
14 namespace { | |
15 | |
16 const CCSchedulerStateMachine::CommitState allCommitStates[] = { | |
17 CCSchedulerStateMachine::COMMIT_STATE_IDLE, | |
18 CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, | |
19 CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, | |
20 CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW | |
21 }; | |
22 | |
23 // Exposes the protected state fields of the CCSchedulerStateMachine for testing | |
24 class StateMachine : public CCSchedulerStateMachine { | |
25 public: | |
26 void setCommitState(CommitState cs) { m_commitState = cs; } | |
27 CommitState commitState() const { return m_commitState; } | |
28 | |
29 void setNeedsCommit(bool b) { m_needsCommit = b; } | |
30 bool needsCommit() const { return m_needsCommit; } | |
31 | |
32 void setNeedsForcedCommit(bool b) { m_needsForcedCommit = b; } | |
33 bool needsForcedCommit() const { return m_needsForcedCommit; } | |
34 | |
35 void setNeedsRedraw(bool b) { m_needsRedraw = b; } | |
36 bool needsRedraw() const { return m_needsRedraw; } | |
37 | |
38 void setNeedsForcedRedraw(bool b) { m_needsForcedRedraw = b; } | |
39 bool needsForcedRedraw() const { return m_needsForcedRedraw; } | |
40 | |
41 bool canDraw() const { return m_canDraw; } | |
42 bool insideVSync() const { return m_insideVSync; } | |
43 bool visible() const { return m_visible; } | |
44 }; | |
45 | |
46 TEST(CCSchedulerStateMachineTest, TestNextActionBeginsFrameIfNeeded) | |
47 { | |
48 // If no commit needed, do nothing | |
49 { | |
50 StateMachine state; | |
51 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_IDLE); | |
52 state.setCanBeginFrame(true); | |
53 state.setNeedsRedraw(false); | |
54 state.setNeedsCommit(false); | |
55 state.setVisible(true); | |
56 | |
57 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
58 | |
59 state.didLeaveVSync(); | |
60 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
61 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
62 state.didEnterVSync(); | |
63 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
64 } | |
65 | |
66 // If commit requested but canBeginFrame is still false, do nothing. | |
67 { | |
68 StateMachine state; | |
69 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_IDLE); | |
70 state.setNeedsRedraw(false); | |
71 state.setNeedsCommit(false); | |
72 state.setVisible(true); | |
73 | |
74 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
75 | |
76 state.didLeaveVSync(); | |
77 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
78 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
79 state.didEnterVSync(); | |
80 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
81 } | |
82 | |
83 | |
84 // If commit requested, begin a frame | |
85 { | |
86 StateMachine state; | |
87 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_IDLE); | |
88 state.setCanBeginFrame(true); | |
89 state.setNeedsRedraw(false); | |
90 state.setNeedsCommit(true); | |
91 state.setVisible(true); | |
92 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
93 } | |
94 | |
95 // Begin the frame, make sure needsCommit and commitState update correctly. | |
96 { | |
97 StateMachine state; | |
98 state.setCanBeginFrame(true); | |
99 state.setVisible(true); | |
100 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
101 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state
.commitState()); | |
102 EXPECT_FALSE(state.needsCommit()); | |
103 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
104 } | |
105 } | |
106 | |
107 TEST(CCSchedulerStateMachineTest, TestSetForcedRedrawDoesNotSetsNormalRedraw) | |
108 { | |
109 CCSchedulerStateMachine state; | |
110 state.setCanDraw(true); | |
111 state.setNeedsForcedRedraw(); | |
112 EXPECT_FALSE(state.redrawPending()); | |
113 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
114 } | |
115 | |
116 TEST(CCSchedulerStateMachineTest, TestFailedDrawSetsNeedsCommitAndDoesNotDrawAga
in) | |
117 { | |
118 CCSchedulerStateMachine state; | |
119 state.setCanBeginFrame(true); | |
120 state.setVisible(true); | |
121 state.setCanDraw(true); | |
122 state.setNeedsRedraw(); | |
123 EXPECT_TRUE(state.redrawPending()); | |
124 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
125 state.didEnterVSync(); | |
126 | |
127 // We're drawing now. | |
128 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
129 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
130 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
131 EXPECT_FALSE(state.redrawPending()); | |
132 EXPECT_FALSE(state.commitPending()); | |
133 | |
134 // Failing the draw makes us require a commit. | |
135 state.didDrawIfPossibleCompleted(false); | |
136 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
137 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
138 EXPECT_TRUE(state.redrawPending()); | |
139 EXPECT_TRUE(state.commitPending()); | |
140 } | |
141 | |
142 TEST(CCSchedulerStateMachineTest, TestSetNeedsRedrawDuringFailedDrawDoesNotRemov
eNeedsRedraw) | |
143 { | |
144 CCSchedulerStateMachine state; | |
145 state.setCanBeginFrame(true); | |
146 state.setVisible(true); | |
147 state.setCanDraw(true); | |
148 state.setNeedsRedraw(); | |
149 EXPECT_TRUE(state.redrawPending()); | |
150 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
151 state.didEnterVSync(); | |
152 | |
153 // We're drawing now. | |
154 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
155 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
156 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
157 EXPECT_FALSE(state.redrawPending()); | |
158 EXPECT_FALSE(state.commitPending()); | |
159 | |
160 // While still in the same vsync callback, set needs redraw again. | |
161 // This should not redraw. | |
162 state.setNeedsRedraw(); | |
163 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
164 | |
165 // Failing the draw makes us require a commit. | |
166 state.didDrawIfPossibleCompleted(false); | |
167 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
168 EXPECT_TRUE(state.redrawPending()); | |
169 } | |
170 | |
171 TEST(CCSchedulerStateMachineTest, TestCommitAfterFailedDrawAllowsDrawInSameFrame
) | |
172 { | |
173 CCSchedulerStateMachine state; | |
174 state.setCanBeginFrame(true); | |
175 state.setVisible(true); | |
176 state.setCanDraw(true); | |
177 | |
178 // Start a commit. | |
179 state.setNeedsCommit(); | |
180 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
181 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
182 EXPECT_TRUE(state.commitPending()); | |
183 | |
184 // Then initiate a draw. | |
185 state.setNeedsRedraw(); | |
186 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
187 state.didEnterVSync(); | |
188 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
189 EXPECT_TRUE(state.redrawPending()); | |
190 | |
191 // Fail the draw. | |
192 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
193 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
194 state.didDrawIfPossibleCompleted(false); | |
195 EXPECT_TRUE(state.redrawPending()); | |
196 // But the commit is ongoing. | |
197 EXPECT_TRUE(state.commitPending()); | |
198 | |
199 // Finish the commit. | |
200 state.beginFrameComplete(); | |
201 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
202 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); | |
203 EXPECT_TRUE(state.redrawPending()); | |
204 | |
205 // And we should be allowed to draw again. | |
206 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
207 } | |
208 | |
209 TEST(CCSchedulerStateMachineTest, TestCommitAfterFailedAndSuccessfulDrawDoesNotA
llowDrawInSameFrame) | |
210 { | |
211 CCSchedulerStateMachine state; | |
212 state.setCanBeginFrame(true); | |
213 state.setVisible(true); | |
214 state.setCanDraw(true); | |
215 | |
216 // Start a commit. | |
217 state.setNeedsCommit(); | |
218 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
219 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
220 EXPECT_TRUE(state.commitPending()); | |
221 | |
222 // Then initiate a draw. | |
223 state.setNeedsRedraw(); | |
224 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
225 state.didEnterVSync(); | |
226 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
227 EXPECT_TRUE(state.redrawPending()); | |
228 | |
229 // Fail the draw. | |
230 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
231 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
232 state.didDrawIfPossibleCompleted(false); | |
233 EXPECT_TRUE(state.redrawPending()); | |
234 // But the commit is ongoing. | |
235 EXPECT_TRUE(state.commitPending()); | |
236 | |
237 // Force a draw. | |
238 state.setNeedsForcedRedraw(); | |
239 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); | |
240 | |
241 // Do the forced draw. | |
242 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_FORCED); | |
243 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
244 EXPECT_FALSE(state.redrawPending()); | |
245 // And the commit is still ongoing. | |
246 EXPECT_TRUE(state.commitPending()); | |
247 | |
248 // Finish the commit. | |
249 state.beginFrameComplete(); | |
250 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
251 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); | |
252 EXPECT_TRUE(state.redrawPending()); | |
253 | |
254 // And we should not be allowed to draw again in the same frame.. | |
255 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
256 } | |
257 | |
258 TEST(CCSchedulerStateMachineTest, TestFailedDrawsWillEventuallyForceADrawAfterTh
eNextCommit) | |
259 { | |
260 CCSchedulerStateMachine state; | |
261 state.setCanBeginFrame(true); | |
262 state.setVisible(true); | |
263 state.setCanDraw(true); | |
264 state.setMaximumNumberOfFailedDrawsBeforeDrawIsForced(1); | |
265 | |
266 // Start a commit. | |
267 state.setNeedsCommit(); | |
268 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
269 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
270 EXPECT_TRUE(state.commitPending()); | |
271 | |
272 // Then initiate a draw. | |
273 state.setNeedsRedraw(); | |
274 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
275 state.didEnterVSync(); | |
276 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
277 EXPECT_TRUE(state.redrawPending()); | |
278 | |
279 // Fail the draw. | |
280 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
281 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
282 state.didDrawIfPossibleCompleted(false); | |
283 EXPECT_TRUE(state.redrawPending()); | |
284 // But the commit is ongoing. | |
285 EXPECT_TRUE(state.commitPending()); | |
286 | |
287 // Finish the commit. Note, we should not yet be forcing a draw, but should | |
288 // continue the commit as usual. | |
289 state.beginFrameComplete(); | |
290 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
291 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); | |
292 EXPECT_TRUE(state.redrawPending()); | |
293 | |
294 // The redraw should be forced in this case. | |
295 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); | |
296 } | |
297 | |
298 TEST(CCSchedulerStateMachineTest, TestFailedDrawIsRetriedNextVSync) | |
299 { | |
300 CCSchedulerStateMachine state; | |
301 state.setCanBeginFrame(true); | |
302 state.setVisible(true); | |
303 state.setCanDraw(true); | |
304 | |
305 // Start a draw. | |
306 state.setNeedsRedraw(); | |
307 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
308 state.didEnterVSync(); | |
309 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
310 EXPECT_TRUE(state.redrawPending()); | |
311 | |
312 // Fail the draw. | |
313 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
314 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
315 state.didDrawIfPossibleCompleted(false); | |
316 EXPECT_TRUE(state.redrawPending()); | |
317 | |
318 // We should not be trying to draw again now, but we have a commit pending. | |
319 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
320 | |
321 state.didLeaveVSync(); | |
322 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
323 state.didEnterVSync(); | |
324 | |
325 // We should try draw again in the next vsync. | |
326 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
327 } | |
328 | |
329 TEST(CCSchedulerStateMachineTest, TestDoestDrawTwiceInSameFrame) | |
330 { | |
331 CCSchedulerStateMachine state; | |
332 state.setVisible(true); | |
333 state.setCanDraw(true); | |
334 state.setNeedsRedraw(); | |
335 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
336 state.didEnterVSync(); | |
337 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
338 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
339 | |
340 // While still in the same vsync callback, set needs redraw again. | |
341 // This should not redraw. | |
342 state.setNeedsRedraw(); | |
343 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
344 | |
345 // Move to another frame. This should now draw. | |
346 state.didDrawIfPossibleCompleted(true); | |
347 state.didLeaveVSync(); | |
348 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
349 state.didEnterVSync(); | |
350 | |
351 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
352 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
353 state.didDrawIfPossibleCompleted(true); | |
354 EXPECT_FALSE(state.vsyncCallbackNeeded()); | |
355 } | |
356 | |
357 TEST(CCSchedulerStateMachineTest, TestNextActionDrawsOnVSync) | |
358 { | |
359 // When not on vsync, or on vsync but not visible, don't draw. | |
360 size_t numCommitStates = sizeof(allCommitStates) / sizeof(CCSchedulerStateMa
chine::CommitState); | |
361 for (size_t i = 0; i < numCommitStates; ++i) { | |
362 for (unsigned j = 0; j < 2; ++j) { | |
363 StateMachine state; | |
364 state.setCommitState(allCommitStates[i]); | |
365 bool visible = j; | |
366 if (!visible) { | |
367 state.didEnterVSync(); | |
368 state.setVisible(false); | |
369 } else | |
370 state.setVisible(true); | |
371 | |
372 // Case 1: needsCommit=false | |
373 state.setNeedsCommit(false); | |
374 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); | |
375 | |
376 // Case 2: needsCommit=true | |
377 state.setNeedsCommit(true); | |
378 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); | |
379 } | |
380 } | |
381 | |
382 // When on vsync, or not on vsync but needsForcedRedraw set, should always d
raw except if you're ready to commit, in which case commit. | |
383 for (size_t i = 0; i < numCommitStates; ++i) { | |
384 for (unsigned j = 0; j < 2; ++j) { | |
385 StateMachine state; | |
386 state.setCanDraw(true); | |
387 state.setCommitState(allCommitStates[i]); | |
388 bool forcedDraw = j; | |
389 if (!forcedDraw) { | |
390 state.didEnterVSync(); | |
391 state.setNeedsRedraw(true); | |
392 state.setVisible(true); | |
393 } else | |
394 state.setNeedsForcedRedraw(true); | |
395 | |
396 CCSchedulerStateMachine::Action expectedAction; | |
397 if (allCommitStates[i] != CCSchedulerStateMachine::COMMIT_STATE_READ
Y_TO_COMMIT) | |
398 expectedAction = forcedDraw ? CCSchedulerStateMachine::ACTION_DR
AW_FORCED : CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE; | |
399 else | |
400 expectedAction = CCSchedulerStateMachine::ACTION_COMMIT; | |
401 | |
402 // Case 1: needsCommit=false. | |
403 state.setNeedsCommit(false); | |
404 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
405 EXPECT_EQ(expectedAction, state.nextAction()); | |
406 | |
407 // Case 2: needsCommit=true. | |
408 state.setNeedsCommit(true); | |
409 EXPECT_TRUE(state.vsyncCallbackNeeded()); | |
410 EXPECT_EQ(expectedAction, state.nextAction()); | |
411 } | |
412 } | |
413 } | |
414 | |
415 TEST(CCSchedulerStateMachineTest, TestNoCommitStatesRedrawWhenInvisible) | |
416 { | |
417 size_t numCommitStates = sizeof(allCommitStates) / sizeof(CCSchedulerStateMa
chine::CommitState); | |
418 for (size_t i = 0; i < numCommitStates; ++i) { | |
419 // There shouldn't be any drawing regardless of vsync. | |
420 for (unsigned j = 0; j < 2; ++j) { | |
421 StateMachine state; | |
422 state.setCommitState(allCommitStates[i]); | |
423 state.setVisible(false); | |
424 state.setNeedsRedraw(true); | |
425 state.setNeedsForcedRedraw(false); | |
426 if (j == 1) | |
427 state.didEnterVSync(); | |
428 | |
429 // Case 1: needsCommit=false. | |
430 state.setNeedsCommit(false); | |
431 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); | |
432 | |
433 // Case 2: needsCommit=true. | |
434 state.setNeedsCommit(true); | |
435 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); | |
436 } | |
437 } | |
438 } | |
439 | |
440 TEST(CCSchedulerStateMachineTest, TestCanRedraw_StopsDraw) | |
441 { | |
442 size_t numCommitStates = sizeof(allCommitStates) / sizeof(CCSchedulerStateMa
chine::CommitState); | |
443 for (size_t i = 0; i < numCommitStates; ++i) { | |
444 // There shouldn't be any drawing regardless of vsync. | |
445 for (unsigned j = 0; j < 2; ++j) { | |
446 StateMachine state; | |
447 state.setCommitState(allCommitStates[i]); | |
448 state.setVisible(false); | |
449 state.setNeedsRedraw(true); | |
450 state.setNeedsForcedRedraw(false); | |
451 if (j == 1) | |
452 state.didEnterVSync(); | |
453 | |
454 state.setCanDraw(false); | |
455 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); | |
456 } | |
457 } | |
458 } | |
459 | |
460 TEST(CCSchedulerStateMachineTest, TestCanRedrawWithWaitingForFirstDrawMakesProgr
ess) | |
461 { | |
462 StateMachine state; | |
463 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST
_DRAW); | |
464 state.setCanBeginFrame(true); | |
465 state.setNeedsCommit(true); | |
466 state.setNeedsRedraw(true); | |
467 state.setVisible(true); | |
468 state.setCanDraw(false); | |
469 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
470 } | |
471 | |
472 TEST(CCSchedulerStateMachineTest, TestSetNeedsCommitIsNotLost) | |
473 { | |
474 StateMachine state; | |
475 state.setCanBeginFrame(true); | |
476 state.setNeedsCommit(true); | |
477 state.setVisible(true); | |
478 state.setCanDraw(true); | |
479 | |
480 // Begin the frame. | |
481 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
482 state.updateState(state.nextAction()); | |
483 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); | |
484 | |
485 // Now, while the frame is in progress, set another commit. | |
486 state.setNeedsCommit(true); | |
487 EXPECT_TRUE(state.needsCommit()); | |
488 | |
489 // Let the frame finish. | |
490 state.beginFrameComplete(); | |
491 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, state.commi
tState()); | |
492 | |
493 // Expect to commit regardless of vsync state. | |
494 state.didLeaveVSync(); | |
495 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
496 state.didEnterVSync(); | |
497 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
498 | |
499 // Commit and make sure we draw on next vsync | |
500 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); | |
501 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
502 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); | |
503 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
504 state.didDrawIfPossibleCompleted(true); | |
505 | |
506 // Verify that another commit will begin. | |
507 state.didLeaveVSync(); | |
508 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
509 } | |
510 | |
511 TEST(CCSchedulerStateMachineTest, TestFullCycle) | |
512 { | |
513 StateMachine state; | |
514 state.setCanBeginFrame(true); | |
515 state.setVisible(true); | |
516 state.setCanDraw(true); | |
517 | |
518 // Start clean and set commit. | |
519 state.setNeedsCommit(true); | |
520 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
521 | |
522 // Begin the frame. | |
523 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
524 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); | |
525 EXPECT_FALSE(state.needsCommit()); | |
526 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
527 | |
528 // Tell the scheduler the frame finished. | |
529 state.beginFrameComplete(); | |
530 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, state.commi
tState()); | |
531 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
532 | |
533 // Commit. | |
534 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); | |
535 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); | |
536 EXPECT_TRUE(state.needsRedraw()); | |
537 | |
538 // Expect to do nothing until vsync. | |
539 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
540 | |
541 // At vsync, draw. | |
542 state.didEnterVSync(); | |
543 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
544 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
545 state.didDrawIfPossibleCompleted(true); | |
546 state.didLeaveVSync(); | |
547 | |
548 // Should be synchronized, no draw needed, no action needed. | |
549 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); | |
550 EXPECT_FALSE(state.needsRedraw()); | |
551 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
552 } | |
553 | |
554 TEST(CCSchedulerStateMachineTest, TestFullCycleWithCommitRequestInbetween) | |
555 { | |
556 StateMachine state; | |
557 state.setCanBeginFrame(true); | |
558 state.setVisible(true); | |
559 state.setCanDraw(true); | |
560 | |
561 // Start clean and set commit. | |
562 state.setNeedsCommit(true); | |
563 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
564 | |
565 // Begin the frame. | |
566 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
567 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); | |
568 EXPECT_FALSE(state.needsCommit()); | |
569 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
570 | |
571 // Request another commit while the commit is in flight. | |
572 state.setNeedsCommit(true); | |
573 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
574 | |
575 // Tell the scheduler the frame finished. | |
576 state.beginFrameComplete(); | |
577 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, state.commi
tState()); | |
578 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
579 | |
580 // Commit. | |
581 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); | |
582 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); | |
583 EXPECT_TRUE(state.needsRedraw()); | |
584 | |
585 // Expect to do nothing until vsync. | |
586 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
587 | |
588 // At vsync, draw. | |
589 state.didEnterVSync(); | |
590 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
591 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); | |
592 state.didDrawIfPossibleCompleted(true); | |
593 state.didLeaveVSync(); | |
594 | |
595 // Should be synchronized, no draw needed, no action needed. | |
596 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); | |
597 EXPECT_FALSE(state.needsRedraw()); | |
598 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
599 } | |
600 | |
601 TEST(CCSchedulerStateMachineTest, TestRequestCommitInvisible) | |
602 { | |
603 StateMachine state; | |
604 state.setNeedsCommit(true); | |
605 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
606 } | |
607 | |
608 TEST(CCSchedulerStateMachineTest, TestGoesInvisibleBeforeBeginFrameCompletes) | |
609 { | |
610 StateMachine state; | |
611 state.setCanBeginFrame(true); | |
612 state.setVisible(true); | |
613 state.setCanDraw(true); | |
614 | |
615 // Start clean and set commit. | |
616 state.setNeedsCommit(true); | |
617 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
618 | |
619 // Begin the frame while visible. | |
620 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); | |
621 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); | |
622 EXPECT_FALSE(state.needsCommit()); | |
623 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
624 | |
625 // Become invisible and abort the beginFrame. | |
626 state.setVisible(false); | |
627 state.beginFrameAborted(); | |
628 | |
629 // We should now be back in the idle state as if we didn't start a frame at
all. | |
630 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); | |
631 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
632 | |
633 // Become visible again | |
634 state.setVisible(true); | |
635 | |
636 // We should be beginning a frame now | |
637 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); | |
638 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
639 | |
640 // Begin the frame | |
641 state.updateState(state.nextAction()); | |
642 | |
643 // We should be starting the commit now | |
644 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); | |
645 } | |
646 | |
647 TEST(CCSchedulerStateMachineTest, TestContextLostWhenCompletelyIdle) | |
648 { | |
649 StateMachine state; | |
650 state.setCanBeginFrame(true); | |
651 state.setVisible(true); | |
652 state.setCanDraw(true); | |
653 | |
654 state.didLoseContext(); | |
655 | |
656 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
657 state.updateState(state.nextAction()); | |
658 | |
659 // Once context recreation begins, nothing should happen. | |
660 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
661 | |
662 // Recreate the context | |
663 state.didRecreateContext(); | |
664 | |
665 // When the context is recreated, we should begin a commit | |
666 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
667 state.updateState(state.nextAction()); | |
668 } | |
669 | |
670 TEST(CCSchedulerStateMachineTest, TestContextLostWhenIdleAndCommitRequestedWhile
Recreating) | |
671 { | |
672 StateMachine state; | |
673 state.setCanBeginFrame(true); | |
674 state.setVisible(true); | |
675 state.setCanDraw(true); | |
676 | |
677 state.didLoseContext(); | |
678 | |
679 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
680 state.updateState(state.nextAction()); | |
681 | |
682 // Once context recreation begins, nothing should happen. | |
683 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
684 | |
685 // While context is recreating, commits shouldn't begin. | |
686 state.setNeedsCommit(true); | |
687 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
688 | |
689 // Recreate the context | |
690 state.didRecreateContext(); | |
691 | |
692 // When the context is recreated, we should begin a commit | |
693 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
694 state.updateState(state.nextAction()); | |
695 | |
696 // Once the context is recreated, whether we draw should be based on | |
697 // setCanDraw. | |
698 state.setNeedsRedraw(true); | |
699 state.didEnterVSync(); | |
700 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
701 state.setCanDraw(false); | |
702 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
703 state.setCanDraw(true); | |
704 state.didLeaveVSync(); | |
705 } | |
706 | |
707 TEST(CCSchedulerStateMachineTest, TestContextLostWhileCommitInProgress) | |
708 { | |
709 StateMachine state; | |
710 state.setCanBeginFrame(true); | |
711 state.setVisible(true); | |
712 state.setCanDraw(true); | |
713 | |
714 // Get a commit in flight. | |
715 state.setNeedsCommit(true); | |
716 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
717 state.updateState(state.nextAction()); | |
718 | |
719 // Set damage and expect a draw. | |
720 state.setNeedsRedraw(true); | |
721 state.didEnterVSync(); | |
722 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
723 state.updateState(state.nextAction()); | |
724 state.didLeaveVSync(); | |
725 | |
726 // Cause a lost context while the begin frame is in flight. | |
727 state.didLoseContext(); | |
728 | |
729 // Ask for another draw. Expect nothing happens. | |
730 state.setNeedsRedraw(true); | |
731 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
732 | |
733 // Finish the frame, and commit. | |
734 state.beginFrameComplete(); | |
735 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
736 state.updateState(state.nextAction()); | |
737 | |
738 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); | |
739 | |
740 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
741 state.updateState(state.nextAction()); | |
742 | |
743 // Expect to be told to begin context recreation, independent of vsync state | |
744 state.didEnterVSync(); | |
745 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
746 state.didLeaveVSync(); | |
747 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
748 } | |
749 | |
750 TEST(CCSchedulerStateMachineTest, TestContextLostWhileCommitInProgressAndAnother
CommitRequested) | |
751 { | |
752 StateMachine state; | |
753 state.setCanBeginFrame(true); | |
754 state.setVisible(true); | |
755 state.setCanDraw(true); | |
756 | |
757 // Get a commit in flight. | |
758 state.setNeedsCommit(true); | |
759 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
760 state.updateState(state.nextAction()); | |
761 | |
762 // Set damage and expect a draw. | |
763 state.setNeedsRedraw(true); | |
764 state.didEnterVSync(); | |
765 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
766 state.updateState(state.nextAction()); | |
767 state.didLeaveVSync(); | |
768 | |
769 // Cause a lost context while the begin frame is in flight. | |
770 state.didLoseContext(); | |
771 | |
772 // Ask for another draw and also set needs commit. Expect nothing happens. | |
773 state.setNeedsRedraw(true); | |
774 state.setNeedsCommit(true); | |
775 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); | |
776 | |
777 // Finish the frame, and commit. | |
778 state.beginFrameComplete(); | |
779 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
780 state.updateState(state.nextAction()); | |
781 | |
782 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); | |
783 | |
784 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); | |
785 state.updateState(state.nextAction()); | |
786 | |
787 // Expect to be told to begin context recreation, independent of vsync state | |
788 state.didEnterVSync(); | |
789 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
790 state.didLeaveVSync(); | |
791 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
792 } | |
793 | |
794 | |
795 TEST(CCSchedulerStateMachineTest, TestFinishAllRenderingWhileContextLost) | |
796 { | |
797 StateMachine state; | |
798 state.setVisible(true); | |
799 state.setCanDraw(true); | |
800 | |
801 // Cause a lost context lost. | |
802 state.didLoseContext(); | |
803 | |
804 // Ask a forced redraw and verify it ocurrs. | |
805 state.setNeedsForcedRedraw(true); | |
806 state.didEnterVSync(); | |
807 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); | |
808 state.didLeaveVSync(); | |
809 | |
810 // Clear the forced redraw bit. | |
811 state.setNeedsForcedRedraw(false); | |
812 | |
813 // Expect to be told to begin context recreation, independent of vsync state | |
814 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); | |
815 state.updateState(state.nextAction()); | |
816 | |
817 // Ask a forced redraw and verify it ocurrs. | |
818 state.setNeedsForcedRedraw(true); | |
819 state.didEnterVSync(); | |
820 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); | |
821 state.didLeaveVSync(); | |
822 } | |
823 | |
824 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenInvisibleAndForceCommit) | |
825 { | |
826 StateMachine state; | |
827 state.setCanBeginFrame(true); | |
828 state.setVisible(false); | |
829 state.setNeedsCommit(true); | |
830 state.setNeedsForcedCommit(true); | |
831 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
832 } | |
833 | |
834 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenCanBeginFrameFalseAndForceCo
mmit) | |
835 { | |
836 StateMachine state; | |
837 state.setVisible(true); | |
838 state.setCanDraw(true); | |
839 state.setNeedsCommit(true); | |
840 state.setNeedsForcedCommit(true); | |
841 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
842 } | |
843 | |
844 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenCommitInProgress) | |
845 { | |
846 StateMachine state; | |
847 state.setCanBeginFrame(true); | |
848 state.setVisible(false); | |
849 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS
); | |
850 state.setNeedsCommit(true); | |
851 state.setNeedsForcedCommit(true); | |
852 | |
853 state.beginFrameComplete(); | |
854 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); | |
855 state.updateState(state.nextAction()); | |
856 | |
857 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); | |
858 | |
859 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
860 } | |
861 | |
862 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenContextLost) | |
863 { | |
864 StateMachine state; | |
865 state.setCanBeginFrame(true); | |
866 state.setVisible(true); | |
867 state.setCanDraw(true); | |
868 state.setNeedsCommit(true); | |
869 state.setNeedsForcedCommit(true); | |
870 state.didLoseContext(); | |
871 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); | |
872 } | |
873 | |
874 } | |
OLD | NEW |