2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
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 ANY
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 ON
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.
27 #include "cc/CCSchedulerStateMachine.h"
31 CCSchedulerStateMachine::CCSchedulerStateMachine()
32 : m_commitState(COMMIT_STATE_IDLE)
33 , m_currentFrameNumber(0)
34 , m_lastFrameNumberWhereDrawWasCalled(-1)
35 , m_needsRedraw(false)
36 , m_needsForcedRedraw(false)
37 , m_needsCommit(false)
38 , m_updateMoreResourcesPending(false)
39 , m_insideVSync(false)
40 , m_visible(false) { }
42 CCSchedulerStateMachine::Action CCSchedulerStateMachine::nextAction() const
44 bool canDraw = m_currentFrameNumber != m_lastFrameNumberWhereDrawWasCalled;
45 bool shouldDraw = (m_needsRedraw && m_insideVSync && m_visible && canDraw) || m_needsForcedRedraw;
46 switch (m_commitState) {
47 case COMMIT_STATE_IDLE:
50 if (m_needsCommit && m_visible)
51 return ACTION_BEGIN_FRAME;
54 case COMMIT_STATE_FRAME_IN_PROGRESS:
59 case COMMIT_STATE_UPDATING_RESOURCES:
62 if (!m_updateMoreResourcesPending)
63 return ACTION_BEGIN_UPDATE_MORE_RESOURCES;
66 case COMMIT_STATE_READY_TO_COMMIT:
69 case COMMIT_STATE_WAITING_FOR_FIRST_DRAW:
78 void CCSchedulerStateMachine::updateState(Action action)
84 case ACTION_BEGIN_FRAME:
86 m_commitState = COMMIT_STATE_FRAME_IN_PROGRESS;
87 m_needsCommit = false;
90 case ACTION_BEGIN_UPDATE_MORE_RESOURCES:
91 ASSERT(m_commitState == COMMIT_STATE_UPDATING_RESOURCES);
92 m_updateMoreResourcesPending = true;
96 if (m_needsCommit || !m_visible)
97 m_commitState = COMMIT_STATE_WAITING_FOR_FIRST_DRAW;
99 m_commitState = COMMIT_STATE_IDLE;
100 m_needsRedraw = true;
104 m_needsRedraw = false;
105 m_needsForcedRedraw = false;
107 m_lastFrameNumberWhereDrawWasCalled = m_currentFrameNumber;
108 if (m_commitState == COMMIT_STATE_WAITING_FOR_FIRST_DRAW) {
109 ASSERT(m_needsCommit);
110 m_commitState = COMMIT_STATE_IDLE;
116 void CCSchedulerStateMachine::didEnterVSync()
118 m_insideVSync = true;
121 void CCSchedulerStateMachine::didLeaveVSync()
123 m_currentFrameNumber++;
124 m_insideVSync = false;
127 void CCSchedulerStateMachine::setVisible(bool visible)
132 void CCSchedulerStateMachine::setNeedsRedraw()
134 m_needsRedraw = true;
137 void CCSchedulerStateMachine::setNeedsForcedRedraw()
139 m_needsForcedRedraw = true;
142 void CCSchedulerStateMachine::setNeedsCommit()
144 m_needsCommit = true;
147 void CCSchedulerStateMachine::beginFrameComplete()
149 ASSERT(m_commitState == COMMIT_STATE_FRAME_IN_PROGRESS);
150 m_commitState = COMMIT_STATE_UPDATING_RESOURCES;
153 void CCSchedulerStateMachine::beginUpdateMoreResourcesComplete(bool morePending)
155 ASSERT(m_commitState == COMMIT_STATE_UPDATING_RESOURCES);
156 ASSERT(m_updateMoreResourcesPending);
157 m_updateMoreResourcesPending = false;
159 m_commitState = COMMIT_STATE_READY_TO_COMMIT;