OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y |
| 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N |
| 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 */ |
| 24 |
| 25 #include "config.h" |
| 26 |
| 27 #include "cc/CCSchedulerStateMachine.h" |
| 28 |
| 29 #include <gtest/gtest.h> |
| 30 #include <wtf/text/CString.h> |
| 31 #include <wtf/text/WTFString.h> |
| 32 |
| 33 using namespace WTF; |
| 34 using namespace WebCore; |
| 35 |
| 36 namespace { |
| 37 |
| 38 const CCSchedulerStateMachine::CommitState allCommitStates[] = { |
| 39 CCSchedulerStateMachine::COMMIT_STATE_IDLE, |
| 40 CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, |
| 41 CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCES, |
| 42 CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, |
| 43 CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW |
| 44 }; |
| 45 |
| 46 // Exposes the protected state fields of the CCSchedulerStateMachine for testing |
| 47 class StateMachine : public CCSchedulerStateMachine { |
| 48 public: |
| 49 void setCommitState(CommitState cs) { m_commitState = cs; } |
| 50 CommitState commitState() const { return m_commitState; } |
| 51 |
| 52 void setNeedsCommit(bool b) { m_needsCommit = b; } |
| 53 bool needsCommit() const { return m_needsCommit; } |
| 54 |
| 55 void setNeedsForcedCommit(bool b) { m_needsForcedCommit = b; } |
| 56 bool needsForcedCommit() const { return m_needsForcedCommit; } |
| 57 |
| 58 void setNeedsRedraw(bool b) { m_needsRedraw = b; } |
| 59 bool needsRedraw() const { return m_needsRedraw; } |
| 60 |
| 61 void setNeedsForcedRedraw(bool b) { m_needsForcedRedraw = b; } |
| 62 bool needsForcedRedraw() const { return m_needsForcedRedraw; } |
| 63 |
| 64 bool canDraw() const { return m_canDraw; } |
| 65 bool insideVSync() const { return m_insideVSync; } |
| 66 bool visible() const { return m_visible; } |
| 67 |
| 68 void setUpdateMoreResourcesPending(bool b) { m_updateMoreResourcesPending =
b; } |
| 69 bool updateMoreResourcesPending() const { return m_updateMoreResourcesPendin
g; } |
| 70 }; |
| 71 |
| 72 TEST(CCSchedulerStateMachineTest, TestNextActionBeginsFrameIfNeeded) |
| 73 { |
| 74 // If no commit needed, do nothing |
| 75 { |
| 76 StateMachine state; |
| 77 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_IDLE); |
| 78 state.setCanBeginFrame(true); |
| 79 state.setNeedsRedraw(false); |
| 80 state.setNeedsCommit(false); |
| 81 state.setUpdateMoreResourcesPending(false); |
| 82 state.setVisible(true); |
| 83 |
| 84 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 85 |
| 86 state.didLeaveVSync(); |
| 87 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 88 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 89 state.didEnterVSync(); |
| 90 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 91 } |
| 92 |
| 93 // If commit requested but canBeginFrame is still false, do nothing. |
| 94 { |
| 95 StateMachine state; |
| 96 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_IDLE); |
| 97 state.setNeedsRedraw(false); |
| 98 state.setNeedsCommit(false); |
| 99 state.setUpdateMoreResourcesPending(false); |
| 100 state.setVisible(true); |
| 101 |
| 102 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 103 |
| 104 state.didLeaveVSync(); |
| 105 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 106 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 107 state.didEnterVSync(); |
| 108 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 109 } |
| 110 |
| 111 |
| 112 // If commit requested, begin a frame |
| 113 { |
| 114 StateMachine state; |
| 115 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_IDLE); |
| 116 state.setCanBeginFrame(true); |
| 117 state.setNeedsRedraw(false); |
| 118 state.setNeedsCommit(true); |
| 119 state.setUpdateMoreResourcesPending(false); |
| 120 state.setVisible(true); |
| 121 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 122 } |
| 123 |
| 124 // Begin the frame, make sure needsCommit and commitState update correctly. |
| 125 { |
| 126 StateMachine state; |
| 127 state.setCanBeginFrame(true); |
| 128 state.setVisible(true); |
| 129 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 130 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state
.commitState()); |
| 131 EXPECT_FALSE(state.needsCommit()); |
| 132 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 133 } |
| 134 } |
| 135 |
| 136 TEST(CCSchedulerStateMachineTest, TestSetForcedRedrawDoesNotSetsNormalRedraw) |
| 137 { |
| 138 CCSchedulerStateMachine state; |
| 139 state.setNeedsForcedRedraw(); |
| 140 EXPECT_FALSE(state.redrawPending()); |
| 141 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 142 } |
| 143 |
| 144 TEST(CCSchedulerStateMachineTest, TestFailedDrawSetsNeedsCommitAndDoesNotDrawAga
in) |
| 145 { |
| 146 CCSchedulerStateMachine state; |
| 147 state.setCanBeginFrame(true); |
| 148 state.setVisible(true); |
| 149 state.setNeedsRedraw(); |
| 150 EXPECT_TRUE(state.redrawPending()); |
| 151 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 152 state.didEnterVSync(); |
| 153 |
| 154 // We're drawing now. |
| 155 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 156 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 157 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 158 EXPECT_FALSE(state.redrawPending()); |
| 159 EXPECT_FALSE(state.commitPending()); |
| 160 |
| 161 // Failing the draw makes us require a commit. |
| 162 state.didDrawIfPossibleCompleted(false); |
| 163 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 164 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 165 EXPECT_TRUE(state.redrawPending()); |
| 166 EXPECT_TRUE(state.commitPending()); |
| 167 } |
| 168 |
| 169 TEST(CCSchedulerStateMachineTest, TestSetNeedsRedrawDuringFailedDrawDoesNotRemov
eNeedsRedraw) |
| 170 { |
| 171 CCSchedulerStateMachine state; |
| 172 state.setCanBeginFrame(true); |
| 173 state.setVisible(true); |
| 174 state.setNeedsRedraw(); |
| 175 EXPECT_TRUE(state.redrawPending()); |
| 176 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 177 state.didEnterVSync(); |
| 178 |
| 179 // We're drawing now. |
| 180 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 181 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 182 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 183 EXPECT_FALSE(state.redrawPending()); |
| 184 EXPECT_FALSE(state.commitPending()); |
| 185 |
| 186 // While still in the same vsync callback, set needs redraw again. |
| 187 // This should not redraw. |
| 188 state.setNeedsRedraw(); |
| 189 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 190 |
| 191 // Failing the draw makes us require a commit. |
| 192 state.didDrawIfPossibleCompleted(false); |
| 193 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 194 EXPECT_TRUE(state.redrawPending()); |
| 195 } |
| 196 |
| 197 TEST(CCSchedulerStateMachineTest, TestCommitAfterFailedDrawAllowsDrawInSameFrame
) |
| 198 { |
| 199 CCSchedulerStateMachine state; |
| 200 state.setCanBeginFrame(true); |
| 201 state.setVisible(true); |
| 202 |
| 203 // Start a commit. |
| 204 state.setNeedsCommit(); |
| 205 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 206 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 207 EXPECT_TRUE(state.commitPending()); |
| 208 |
| 209 // Then initiate a draw. |
| 210 state.setNeedsRedraw(); |
| 211 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 212 state.didEnterVSync(); |
| 213 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 214 EXPECT_TRUE(state.redrawPending()); |
| 215 |
| 216 // Fail the draw. |
| 217 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 218 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 219 state.didDrawIfPossibleCompleted(false); |
| 220 EXPECT_TRUE(state.redrawPending()); |
| 221 // But the commit is ongoing. |
| 222 EXPECT_TRUE(state.commitPending()); |
| 223 |
| 224 // Finish the commit. |
| 225 state.beginFrameComplete(); |
| 226 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 227 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 228 state.beginUpdateMoreResourcesComplete(false); |
| 229 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 230 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); |
| 231 EXPECT_TRUE(state.redrawPending()); |
| 232 |
| 233 // And we should be allowed to draw again. |
| 234 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 235 } |
| 236 |
| 237 TEST(CCSchedulerStateMachineTest, TestCommitAfterFailedAndSuccessfulDrawDoesNotA
llowDrawInSameFrame) |
| 238 { |
| 239 CCSchedulerStateMachine state; |
| 240 state.setCanBeginFrame(true); |
| 241 state.setVisible(true); |
| 242 |
| 243 // Start a commit. |
| 244 state.setNeedsCommit(); |
| 245 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 246 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 247 EXPECT_TRUE(state.commitPending()); |
| 248 |
| 249 // Then initiate a draw. |
| 250 state.setNeedsRedraw(); |
| 251 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 252 state.didEnterVSync(); |
| 253 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 254 EXPECT_TRUE(state.redrawPending()); |
| 255 |
| 256 // Fail the draw. |
| 257 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 258 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 259 state.didDrawIfPossibleCompleted(false); |
| 260 EXPECT_TRUE(state.redrawPending()); |
| 261 // But the commit is ongoing. |
| 262 EXPECT_TRUE(state.commitPending()); |
| 263 |
| 264 // Force a draw. |
| 265 state.setNeedsForcedRedraw(); |
| 266 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); |
| 267 |
| 268 // Do the forced draw. |
| 269 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_FORCED); |
| 270 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 271 EXPECT_FALSE(state.redrawPending()); |
| 272 // And the commit is still ongoing. |
| 273 EXPECT_TRUE(state.commitPending()); |
| 274 |
| 275 // Finish the commit. |
| 276 state.beginFrameComplete(); |
| 277 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 278 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 279 state.beginUpdateMoreResourcesComplete(false); |
| 280 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 281 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); |
| 282 EXPECT_TRUE(state.redrawPending()); |
| 283 |
| 284 // And we should not be allowed to draw again in the same frame.. |
| 285 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 286 } |
| 287 |
| 288 TEST(CCSchedulerStateMachineTest, TestFailedDrawsWillEventuallyForceADrawAfterTh
eNextCommit) |
| 289 { |
| 290 CCSchedulerStateMachine state; |
| 291 state.setCanBeginFrame(true); |
| 292 state.setVisible(true); |
| 293 state.setMaximumNumberOfFailedDrawsBeforeDrawIsForced(1); |
| 294 |
| 295 // Start a commit. |
| 296 state.setNeedsCommit(); |
| 297 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 298 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 299 EXPECT_TRUE(state.commitPending()); |
| 300 |
| 301 // Then initiate a draw. |
| 302 state.setNeedsRedraw(); |
| 303 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 304 state.didEnterVSync(); |
| 305 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 306 EXPECT_TRUE(state.redrawPending()); |
| 307 |
| 308 // Fail the draw. |
| 309 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 310 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 311 state.didDrawIfPossibleCompleted(false); |
| 312 EXPECT_TRUE(state.redrawPending()); |
| 313 // But the commit is ongoing. |
| 314 EXPECT_TRUE(state.commitPending()); |
| 315 |
| 316 // Finish the commit. Note, we should not yet be forcing a draw, but should |
| 317 // continue the commit as usual. |
| 318 state.beginFrameComplete(); |
| 319 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 320 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 321 state.beginUpdateMoreResourcesComplete(false); |
| 322 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 323 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); |
| 324 EXPECT_TRUE(state.redrawPending()); |
| 325 |
| 326 // The redraw should be forced in this case. |
| 327 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); |
| 328 } |
| 329 |
| 330 TEST(CCSchedulerStateMachineTest, TestFailedDrawIsRetriedNextVSync) |
| 331 { |
| 332 CCSchedulerStateMachine state; |
| 333 state.setCanBeginFrame(true); |
| 334 state.setVisible(true); |
| 335 |
| 336 // Start a draw. |
| 337 state.setNeedsRedraw(); |
| 338 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 339 state.didEnterVSync(); |
| 340 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 341 EXPECT_TRUE(state.redrawPending()); |
| 342 |
| 343 // Fail the draw. |
| 344 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 345 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 346 state.didDrawIfPossibleCompleted(false); |
| 347 EXPECT_TRUE(state.redrawPending()); |
| 348 |
| 349 // We should not be trying to draw again now, but we have a commit pending. |
| 350 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 351 |
| 352 state.didLeaveVSync(); |
| 353 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 354 state.didEnterVSync(); |
| 355 |
| 356 // We should try draw again in the next vsync. |
| 357 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 358 } |
| 359 |
| 360 TEST(CCSchedulerStateMachineTest, TestDoestDrawTwiceInSameFrame) |
| 361 { |
| 362 CCSchedulerStateMachine state; |
| 363 state.setVisible(true); |
| 364 state.setNeedsRedraw(); |
| 365 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 366 state.didEnterVSync(); |
| 367 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 368 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 369 |
| 370 // While still in the same vsync callback, set needs redraw again. |
| 371 // This should not redraw. |
| 372 state.setNeedsRedraw(); |
| 373 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 374 |
| 375 // Move to another frame. This should now draw. |
| 376 state.didDrawIfPossibleCompleted(true); |
| 377 state.didLeaveVSync(); |
| 378 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 379 state.didEnterVSync(); |
| 380 |
| 381 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 382 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 383 state.didDrawIfPossibleCompleted(true); |
| 384 EXPECT_FALSE(state.vsyncCallbackNeeded()); |
| 385 } |
| 386 |
| 387 TEST(CCSchedulerStateMachineTest, TestNextActionDrawsOnVSync) |
| 388 { |
| 389 // When not on vsync, or on vsync but not visible, don't draw. |
| 390 size_t numCommitStates = sizeof(allCommitStates) / sizeof(CCSchedulerStateMa
chine::CommitState); |
| 391 for (size_t i = 0; i < numCommitStates; ++i) { |
| 392 for (unsigned j = 0; j < 2; ++j) { |
| 393 StateMachine state; |
| 394 state.setCommitState(allCommitStates[i]); |
| 395 bool visible = j; |
| 396 if (!visible) { |
| 397 state.didEnterVSync(); |
| 398 state.setVisible(false); |
| 399 } else |
| 400 state.setVisible(true); |
| 401 |
| 402 // Case 1: needsCommit=false |
| 403 state.setNeedsCommit(false); |
| 404 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 405 |
| 406 // Case 2: needsCommit=true |
| 407 state.setNeedsCommit(true); |
| 408 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 409 } |
| 410 } |
| 411 |
| 412 // 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. |
| 413 for (size_t i = 0; i < numCommitStates; ++i) { |
| 414 for (unsigned j = 0; j < 2; ++j) { |
| 415 StateMachine state; |
| 416 state.setCommitState(allCommitStates[i]); |
| 417 bool forcedDraw = j; |
| 418 if (!forcedDraw) { |
| 419 state.didEnterVSync(); |
| 420 state.setNeedsRedraw(true); |
| 421 state.setVisible(true); |
| 422 } else |
| 423 state.setNeedsForcedRedraw(true); |
| 424 |
| 425 CCSchedulerStateMachine::Action expectedAction; |
| 426 if (allCommitStates[i] != CCSchedulerStateMachine::COMMIT_STATE_READ
Y_TO_COMMIT) |
| 427 expectedAction = forcedDraw ? CCSchedulerStateMachine::ACTION_DR
AW_FORCED : CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE; |
| 428 else |
| 429 expectedAction = CCSchedulerStateMachine::ACTION_COMMIT; |
| 430 |
| 431 // Case 1: needsCommit=false updateMoreResourcesPending=false. |
| 432 state.setNeedsCommit(false); |
| 433 state.setUpdateMoreResourcesPending(false); |
| 434 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 435 EXPECT_EQ(expectedAction, state.nextAction()); |
| 436 |
| 437 // Case 2: needsCommit=false updateMoreResourcesPending=true. |
| 438 state.setNeedsCommit(false); |
| 439 state.setUpdateMoreResourcesPending(true); |
| 440 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 441 EXPECT_EQ(expectedAction, state.nextAction()); |
| 442 |
| 443 // Case 3: needsCommit=true updateMoreResourcesPending=false. |
| 444 state.setNeedsCommit(true); |
| 445 state.setUpdateMoreResourcesPending(false); |
| 446 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 447 EXPECT_EQ(expectedAction, state.nextAction()); |
| 448 |
| 449 // Case 4: needsCommit=true updateMoreResourcesPending=true. |
| 450 state.setNeedsCommit(true); |
| 451 state.setUpdateMoreResourcesPending(true); |
| 452 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 453 EXPECT_EQ(expectedAction, state.nextAction()); |
| 454 } |
| 455 } |
| 456 } |
| 457 |
| 458 TEST(CCSchedulerStateMachineTest, TestNoCommitStatesRedrawWhenInvisible) |
| 459 { |
| 460 size_t numCommitStates = sizeof(allCommitStates) / sizeof(CCSchedulerStateMa
chine::CommitState); |
| 461 for (size_t i = 0; i < numCommitStates; ++i) { |
| 462 // There shouldn't be any drawing regardless of vsync. |
| 463 for (unsigned j = 0; j < 2; ++j) { |
| 464 StateMachine state; |
| 465 state.setCommitState(allCommitStates[i]); |
| 466 state.setVisible(false); |
| 467 state.setNeedsRedraw(true); |
| 468 state.setNeedsForcedRedraw(false); |
| 469 if (j == 1) |
| 470 state.didEnterVSync(); |
| 471 |
| 472 // Case 1: needsCommit=false updateMoreResourcesPending=false. |
| 473 state.setNeedsCommit(false); |
| 474 state.setUpdateMoreResourcesPending(false); |
| 475 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 476 |
| 477 // Case 2: needsCommit=false updateMoreResourcesPending=true. |
| 478 state.setNeedsCommit(false); |
| 479 state.setUpdateMoreResourcesPending(true); |
| 480 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 481 |
| 482 // Case 3: needsCommit=true updateMoreResourcesPending=false. |
| 483 state.setNeedsCommit(true); |
| 484 state.setUpdateMoreResourcesPending(false); |
| 485 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 486 |
| 487 // Case 4: needsCommit=true updateMoreResourcesPending=true. |
| 488 state.setNeedsCommit(true); |
| 489 state.setUpdateMoreResourcesPending(true); |
| 490 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 491 } |
| 492 } |
| 493 } |
| 494 |
| 495 TEST(CCSchedulerStateMachineTest, TestCanRedraw_StopsDraw) |
| 496 { |
| 497 size_t numCommitStates = sizeof(allCommitStates) / sizeof(CCSchedulerStateMa
chine::CommitState); |
| 498 for (size_t i = 0; i < numCommitStates; ++i) { |
| 499 // There shouldn't be any drawing regardless of vsync. |
| 500 for (unsigned j = 0; j < 2; ++j) { |
| 501 StateMachine state; |
| 502 state.setCommitState(allCommitStates[i]); |
| 503 state.setVisible(false); |
| 504 state.setNeedsRedraw(true); |
| 505 state.setNeedsForcedRedraw(false); |
| 506 if (j == 1) |
| 507 state.didEnterVSync(); |
| 508 |
| 509 state.setCanDraw(false); |
| 510 EXPECT_NE(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.ne
xtAction()); |
| 511 } |
| 512 } |
| 513 } |
| 514 |
| 515 TEST(CCSchedulerStateMachineTest, TestCanRedrawWithWaitingForFirstDrawMakesProgr
ess) |
| 516 { |
| 517 StateMachine state; |
| 518 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST
_DRAW); |
| 519 state.setCanBeginFrame(true); |
| 520 state.setNeedsCommit(true); |
| 521 state.setNeedsRedraw(true); |
| 522 state.setUpdateMoreResourcesPending(false); |
| 523 state.setVisible(true); |
| 524 state.setCanDraw(false); |
| 525 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 526 } |
| 527 |
| 528 TEST(CCSchedulerStateMachineTest, TestUpdates_NoRedraw_OneRoundOfUpdates) |
| 529 { |
| 530 StateMachine state; |
| 531 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCE
S); |
| 532 state.setNeedsRedraw(false); |
| 533 state.setUpdateMoreResourcesPending(false); |
| 534 state.setVisible(true); |
| 535 |
| 536 // Verify we begin update, both for vsync and not vsync. |
| 537 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 538 state.didEnterVSync(); |
| 539 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 540 |
| 541 // Begin an update. |
| 542 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 543 |
| 544 // Verify we don't do anything, both for vsync and not vsync. |
| 545 state.didLeaveVSync(); |
| 546 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 547 state.didEnterVSync(); |
| 548 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 549 |
| 550 // End update with no more updates pending. |
| 551 state.beginUpdateMoreResourcesComplete(false); |
| 552 state.didLeaveVSync(); |
| 553 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 554 } |
| 555 |
| 556 TEST(CCSchedulerStateMachineTest, TestUpdates_NoRedraw_TwoRoundsOfUpdates) |
| 557 { |
| 558 StateMachine state; |
| 559 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCE
S); |
| 560 state.setNeedsRedraw(false); |
| 561 state.setUpdateMoreResourcesPending(false); |
| 562 state.setVisible(true); |
| 563 |
| 564 // Verify the update begins, both for vsync and not vsync. |
| 565 state.didEnterVSync(); |
| 566 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 567 state.didLeaveVSync(); |
| 568 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 569 |
| 570 // Begin an update. |
| 571 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 572 |
| 573 // Verify we do nothing, both for vsync and not vsync. |
| 574 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 575 state.didEnterVSync(); |
| 576 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 577 |
| 578 // Ack the update with more pending. |
| 579 state.beginUpdateMoreResourcesComplete(true); |
| 580 |
| 581 // Verify we update more, both for vsync and not vsync. |
| 582 state.didLeaveVSync(); |
| 583 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 584 state.didEnterVSync(); |
| 585 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 586 |
| 587 // Begin another update, while inside vsync. And, it updating. |
| 588 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 589 state.beginUpdateMoreResourcesComplete(false); |
| 590 |
| 591 // Make sure we commit, independent of vsync. |
| 592 state.didLeaveVSync(); |
| 593 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 594 state.didEnterVSync(); |
| 595 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 596 } |
| 597 |
| 598 |
| 599 TEST(CCSchedulerStateMachineTest, TestVSyncNeededWhenUpdatesPendingButInvisible) |
| 600 { |
| 601 StateMachine state; |
| 602 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCE
S); |
| 603 state.setNeedsRedraw(false); |
| 604 state.setVisible(false); |
| 605 state.setUpdateMoreResourcesPending(true); |
| 606 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 607 |
| 608 state.setUpdateMoreResourcesPending(false); |
| 609 EXPECT_TRUE(state.vsyncCallbackNeeded()); |
| 610 } |
| 611 |
| 612 TEST(CCSchedulerStateMachineTest, TestUpdates_WithRedraw_OneRoundOfUpdates) |
| 613 { |
| 614 StateMachine state; |
| 615 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCE
S); |
| 616 state.setNeedsRedraw(true); |
| 617 state.setUpdateMoreResourcesPending(false); |
| 618 state.setVisible(true); |
| 619 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 620 |
| 621 // Begin an update. |
| 622 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 623 |
| 624 // Ensure we draw on the next vsync even though an update is in-progress. |
| 625 state.didEnterVSync(); |
| 626 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 627 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 628 state.didDrawIfPossibleCompleted(true); |
| 629 |
| 630 // Ensure that we once we have drawn, we dont do anything else. |
| 631 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 632 |
| 633 // Leave the vsync before we finish the update. |
| 634 state.didLeaveVSync(); |
| 635 |
| 636 // Finish update but leave more resources pending. |
| 637 state.beginUpdateMoreResourcesComplete(true); |
| 638 |
| 639 // Verify that regardless of vsync, we update some more. |
| 640 state.didLeaveVSync(); |
| 641 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 642 state.didEnterVSync(); |
| 643 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 644 state.didEnterVSync(); |
| 645 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 646 |
| 647 // Begin another update. Finish it immediately. Inside the vsync. |
| 648 state.didEnterVSync(); |
| 649 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 650 state.didLeaveVSync(); |
| 651 state.beginUpdateMoreResourcesComplete(false); |
| 652 |
| 653 // Verify we commit regardless of vsync state |
| 654 state.didLeaveVSync(); |
| 655 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 656 state.didEnterVSync(); |
| 657 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 658 } |
| 659 |
| 660 TEST(CCSchedulerStateMachineTest, TestSetNeedsCommitIsNotLost) |
| 661 { |
| 662 StateMachine state; |
| 663 state.setCanBeginFrame(true); |
| 664 state.setNeedsCommit(true); |
| 665 state.setVisible(true); |
| 666 |
| 667 // Begin the frame. |
| 668 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 669 state.updateState(state.nextAction()); |
| 670 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); |
| 671 |
| 672 // Now, while the frame is in progress, set another commit. |
| 673 state.setNeedsCommit(true); |
| 674 EXPECT_TRUE(state.needsCommit()); |
| 675 |
| 676 // Let the frame finish. |
| 677 state.beginFrameComplete(); |
| 678 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCES, state.co
mmitState()); |
| 679 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 680 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 681 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 682 state.beginUpdateMoreResourcesComplete(false); |
| 683 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, state.commi
tState()); |
| 684 |
| 685 // Expect to commit regardless of vsync state. |
| 686 state.didLeaveVSync(); |
| 687 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 688 state.didEnterVSync(); |
| 689 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 690 |
| 691 // Commit and make sure we draw on next vsync |
| 692 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); |
| 693 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 694 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); |
| 695 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 696 state.didDrawIfPossibleCompleted(true); |
| 697 |
| 698 // Verify that another commit will begin. |
| 699 state.didLeaveVSync(); |
| 700 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 701 } |
| 702 |
| 703 TEST(CCSchedulerStateMachineTest, TestFullCycle) |
| 704 { |
| 705 StateMachine state; |
| 706 state.setCanBeginFrame(true); |
| 707 state.setVisible(true); |
| 708 |
| 709 // Start clean and set commit. |
| 710 state.setNeedsCommit(true); |
| 711 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 712 |
| 713 // Begin the frame. |
| 714 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 715 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); |
| 716 EXPECT_FALSE(state.needsCommit()); |
| 717 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 718 |
| 719 // Tell the scheduler the frame finished. |
| 720 state.beginFrameComplete(); |
| 721 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCES, state.co
mmitState()); |
| 722 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 723 |
| 724 // Tell the scheduler the update began and finished |
| 725 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 726 state.beginUpdateMoreResourcesComplete(false); |
| 727 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, state.commi
tState()); |
| 728 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 729 |
| 730 // Commit. |
| 731 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); |
| 732 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); |
| 733 EXPECT_TRUE(state.needsRedraw()); |
| 734 |
| 735 // Expect to do nothing until vsync. |
| 736 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 737 |
| 738 // At vsync, draw. |
| 739 state.didEnterVSync(); |
| 740 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 741 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 742 state.didDrawIfPossibleCompleted(true); |
| 743 state.didLeaveVSync(); |
| 744 |
| 745 // Should be synchronized, no draw needed, no action needed. |
| 746 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); |
| 747 EXPECT_FALSE(state.needsRedraw()); |
| 748 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 749 } |
| 750 |
| 751 TEST(CCSchedulerStateMachineTest, TestFullCycleWithCommitRequestInbetween) |
| 752 { |
| 753 StateMachine state; |
| 754 state.setCanBeginFrame(true); |
| 755 state.setVisible(true); |
| 756 |
| 757 // Start clean and set commit. |
| 758 state.setNeedsCommit(true); |
| 759 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 760 |
| 761 // Begin the frame. |
| 762 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 763 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); |
| 764 EXPECT_FALSE(state.needsCommit()); |
| 765 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 766 |
| 767 // Request another commit while the commit is in flight. |
| 768 state.setNeedsCommit(true); |
| 769 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 770 |
| 771 // Tell the scheduler the frame finished. |
| 772 state.beginFrameComplete(); |
| 773 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_UPDATING_RESOURCES, state.co
mmitState()); |
| 774 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 775 |
| 776 // Tell the scheduler the update began and finished |
| 777 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCE
S); |
| 778 state.beginUpdateMoreResourcesComplete(false); |
| 779 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_READY_TO_COMMIT, state.commi
tState()); |
| 780 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 781 |
| 782 // Commit. |
| 783 state.updateState(CCSchedulerStateMachine::ACTION_COMMIT); |
| 784 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); |
| 785 EXPECT_TRUE(state.needsRedraw()); |
| 786 |
| 787 // Expect to do nothing until vsync. |
| 788 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 789 |
| 790 // At vsync, draw. |
| 791 state.didEnterVSync(); |
| 792 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 793 state.updateState(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE); |
| 794 state.didDrawIfPossibleCompleted(true); |
| 795 state.didLeaveVSync(); |
| 796 |
| 797 // Should be synchronized, no draw needed, no action needed. |
| 798 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); |
| 799 EXPECT_FALSE(state.needsRedraw()); |
| 800 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 801 } |
| 802 |
| 803 TEST(CCSchedulerStateMachineTest, TestRequestCommitInvisible) |
| 804 { |
| 805 StateMachine state; |
| 806 state.setNeedsCommit(true); |
| 807 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 808 } |
| 809 |
| 810 TEST(CCSchedulerStateMachineTest, TestGoesInvisibleBeforeBeginFrameCompletes) |
| 811 { |
| 812 StateMachine state; |
| 813 state.setCanBeginFrame(true); |
| 814 state.setVisible(true); |
| 815 |
| 816 // Start clean and set commit. |
| 817 state.setNeedsCommit(true); |
| 818 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 819 |
| 820 // Begin the frame while visible. |
| 821 state.updateState(CCSchedulerStateMachine::ACTION_BEGIN_FRAME); |
| 822 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS, state.com
mitState()); |
| 823 EXPECT_FALSE(state.needsCommit()); |
| 824 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 825 |
| 826 // Become invisible and abort the beginFrame. |
| 827 state.setVisible(false); |
| 828 state.beginFrameAborted(); |
| 829 |
| 830 // We should now be back in the idle state as if we didn't start a frame at
all. |
| 831 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); |
| 832 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 833 } |
| 834 |
| 835 TEST(CCSchedulerStateMachineTest, TestContextLostWhenCompletelyIdle) |
| 836 { |
| 837 StateMachine state; |
| 838 state.setCanBeginFrame(true); |
| 839 state.setVisible(true); |
| 840 |
| 841 state.didLoseContext(); |
| 842 |
| 843 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 844 state.updateState(state.nextAction()); |
| 845 |
| 846 // Once context recreation begins, nothing should happen. |
| 847 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 848 |
| 849 // Recreate the context |
| 850 state.didRecreateContext(); |
| 851 |
| 852 // When the context is recreated, we should begin a commit |
| 853 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 854 state.updateState(state.nextAction()); |
| 855 } |
| 856 |
| 857 TEST(CCSchedulerStateMachineTest, TestContextLostWhenIdleAndCommitRequestedWhile
Recreating) |
| 858 { |
| 859 StateMachine state; |
| 860 state.setCanBeginFrame(true); |
| 861 state.setVisible(true); |
| 862 |
| 863 state.didLoseContext(); |
| 864 |
| 865 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 866 state.updateState(state.nextAction()); |
| 867 |
| 868 // Once context recreation begins, nothing should happen. |
| 869 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 870 |
| 871 // While context is recreating, commits shouldn't begin. |
| 872 state.setNeedsCommit(true); |
| 873 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 874 |
| 875 // Recreate the context |
| 876 state.didRecreateContext(); |
| 877 |
| 878 // When the context is recreated, we should begin a commit |
| 879 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 880 state.updateState(state.nextAction()); |
| 881 |
| 882 // Once the context is recreated, whether we draw should be based on |
| 883 // setCanDraw. |
| 884 state.setNeedsRedraw(true); |
| 885 state.didEnterVSync(); |
| 886 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 887 state.setCanDraw(false); |
| 888 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 889 state.setCanDraw(true); |
| 890 state.didLeaveVSync(); |
| 891 } |
| 892 |
| 893 TEST(CCSchedulerStateMachineTest, TestContextLostWhileCommitInProgress) |
| 894 { |
| 895 StateMachine state; |
| 896 state.setCanBeginFrame(true); |
| 897 state.setVisible(true); |
| 898 |
| 899 // Get a commit in flight. |
| 900 state.setNeedsCommit(true); |
| 901 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 902 state.updateState(state.nextAction()); |
| 903 |
| 904 // Set damage and expect a draw. |
| 905 state.setNeedsRedraw(true); |
| 906 state.didEnterVSync(); |
| 907 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 908 state.updateState(state.nextAction()); |
| 909 state.didLeaveVSync(); |
| 910 |
| 911 // Cause a lost context while the begin frame is in flight. |
| 912 state.didLoseContext(); |
| 913 |
| 914 // Ask for another draw. Expect nothing happens. |
| 915 state.setNeedsRedraw(true); |
| 916 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 917 |
| 918 // Finish the frame, update resources, and commit. |
| 919 state.beginFrameComplete(); |
| 920 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 921 state.updateState(state.nextAction()); |
| 922 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 923 state.beginUpdateMoreResourcesComplete(false); |
| 924 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 925 state.updateState(state.nextAction()); |
| 926 |
| 927 // Expect to be told to begin context recreation, independent of vsync state |
| 928 state.didEnterVSync(); |
| 929 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 930 state.didLeaveVSync(); |
| 931 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 932 } |
| 933 |
| 934 TEST(CCSchedulerStateMachineTest, TestContextLostWhileCommitInProgressAndAnother
CommitRequested) |
| 935 { |
| 936 StateMachine state; |
| 937 state.setCanBeginFrame(true); |
| 938 state.setVisible(true); |
| 939 |
| 940 // Get a commit in flight. |
| 941 state.setNeedsCommit(true); |
| 942 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 943 state.updateState(state.nextAction()); |
| 944 |
| 945 // Set damage and expect a draw. |
| 946 state.setNeedsRedraw(true); |
| 947 state.didEnterVSync(); |
| 948 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 949 state.updateState(state.nextAction()); |
| 950 state.didLeaveVSync(); |
| 951 |
| 952 // Cause a lost context while the begin frame is in flight. |
| 953 state.didLoseContext(); |
| 954 |
| 955 // Ask for another draw and also set needs commit. Expect nothing happens. |
| 956 // Setting another commit will put us into |
| 957 // COMMIT_STATE_WAITING_FOR_FIRST_DRAW after we finish the frame on the main |
| 958 // thread. |
| 959 state.setNeedsRedraw(true); |
| 960 state.setNeedsCommit(true); |
| 961 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 962 |
| 963 // Finish the frame, update resources, and commit. |
| 964 state.beginFrameComplete(); |
| 965 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 966 state.updateState(state.nextAction()); |
| 967 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 968 state.beginUpdateMoreResourcesComplete(false); |
| 969 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 970 state.updateState(state.nextAction()); |
| 971 |
| 972 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_WAITING_FOR_FIRST_DRAW, stat
e.commitState()); |
| 973 |
| 974 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE, state.nextAction
()); |
| 975 state.updateState(state.nextAction()); |
| 976 |
| 977 // Expect to be told to begin context recreation, independent of vsync state |
| 978 state.didEnterVSync(); |
| 979 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 980 state.didLeaveVSync(); |
| 981 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 982 } |
| 983 |
| 984 |
| 985 TEST(CCSchedulerStateMachineTest, TestFinishAllRenderingWhileContextLost) |
| 986 { |
| 987 StateMachine state; |
| 988 state.setVisible(true); |
| 989 |
| 990 // Cause a lost context lost. |
| 991 state.didLoseContext(); |
| 992 |
| 993 // Ask a forced redraw and verify it ocurrs. |
| 994 state.setNeedsForcedRedraw(true); |
| 995 state.didEnterVSync(); |
| 996 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); |
| 997 state.didLeaveVSync(); |
| 998 |
| 999 // Clear the forced redraw bit. |
| 1000 state.setNeedsForcedRedraw(false); |
| 1001 |
| 1002 // Expect to be told to begin context recreation, independent of vsync state |
| 1003 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION, state.ne
xtAction()); |
| 1004 state.updateState(state.nextAction()); |
| 1005 |
| 1006 // Ask a forced redraw and verify it ocurrs. |
| 1007 state.setNeedsForcedRedraw(true); |
| 1008 state.didEnterVSync(); |
| 1009 EXPECT_EQ(CCSchedulerStateMachine::ACTION_DRAW_FORCED, state.nextAction()); |
| 1010 state.didLeaveVSync(); |
| 1011 } |
| 1012 |
| 1013 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenInvisibleAndForceCommit) |
| 1014 { |
| 1015 StateMachine state; |
| 1016 state.setCanBeginFrame(true); |
| 1017 state.setVisible(false); |
| 1018 state.setNeedsCommit(true); |
| 1019 state.setNeedsForcedCommit(true); |
| 1020 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 1021 } |
| 1022 |
| 1023 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenCanBeginFrameFalseAndForceCo
mmit) |
| 1024 { |
| 1025 StateMachine state; |
| 1026 state.setVisible(true); |
| 1027 state.setNeedsCommit(true); |
| 1028 state.setNeedsForcedCommit(true); |
| 1029 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 1030 } |
| 1031 |
| 1032 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenCommitInProgress) |
| 1033 { |
| 1034 StateMachine state; |
| 1035 state.setCanBeginFrame(true); |
| 1036 state.setVisible(false); |
| 1037 state.setCommitState(CCSchedulerStateMachine::COMMIT_STATE_FRAME_IN_PROGRESS
); |
| 1038 state.setNeedsCommit(true); |
| 1039 state.setNeedsForcedCommit(true); |
| 1040 |
| 1041 state.beginFrameComplete(); |
| 1042 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES, state
.nextAction()); |
| 1043 state.updateState(state.nextAction()); |
| 1044 EXPECT_EQ(CCSchedulerStateMachine::ACTION_NONE, state.nextAction()); |
| 1045 state.beginUpdateMoreResourcesComplete(false); |
| 1046 EXPECT_EQ(CCSchedulerStateMachine::ACTION_COMMIT, state.nextAction()); |
| 1047 state.updateState(state.nextAction()); |
| 1048 |
| 1049 EXPECT_EQ(CCSchedulerStateMachine::COMMIT_STATE_IDLE, state.commitState()); |
| 1050 |
| 1051 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 1052 } |
| 1053 |
| 1054 TEST(CCSchedulerStateMachineTest, TestBeginFrameWhenContextLost) |
| 1055 { |
| 1056 StateMachine state; |
| 1057 state.setCanBeginFrame(true); |
| 1058 state.setVisible(true); |
| 1059 state.setNeedsCommit(true); |
| 1060 state.setNeedsForcedCommit(true); |
| 1061 state.didLoseContext(); |
| 1062 EXPECT_EQ(CCSchedulerStateMachine::ACTION_BEGIN_FRAME, state.nextAction()); |
| 1063 } |
| 1064 |
| 1065 } |
OLD | NEW |