tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / platform / graphics / chromium / cc / CCSchedulerStateMachine.cpp
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 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.
23  */
24
25 #include "config.h"
26
27 #include "cc/CCSchedulerStateMachine.h"
28
29 namespace WebCore {
30
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) { }
41
42 CCSchedulerStateMachine::Action CCSchedulerStateMachine::nextAction() const
43 {
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:
48         if (shouldDraw)
49             return ACTION_DRAW;
50         if (m_needsCommit && m_visible)
51             return ACTION_BEGIN_FRAME;
52         return ACTION_NONE;
53
54     case COMMIT_STATE_FRAME_IN_PROGRESS:
55         if (shouldDraw)
56             return ACTION_DRAW;
57         return ACTION_NONE;
58
59     case COMMIT_STATE_UPDATING_RESOURCES:
60         if (shouldDraw)
61             return ACTION_DRAW;
62         if (!m_updateMoreResourcesPending)
63             return ACTION_BEGIN_UPDATE_MORE_RESOURCES;
64         return ACTION_NONE;
65
66     case COMMIT_STATE_READY_TO_COMMIT:
67         return ACTION_COMMIT;
68
69     case COMMIT_STATE_WAITING_FOR_FIRST_DRAW:
70         if (shouldDraw)
71             return ACTION_DRAW;
72         return ACTION_NONE;
73     }
74     ASSERT_NOT_REACHED();
75     return ACTION_NONE;
76 }
77
78 void CCSchedulerStateMachine::updateState(Action action)
79 {
80     switch (action) {
81     case ACTION_NONE:
82         return;
83
84     case ACTION_BEGIN_FRAME:
85         ASSERT(m_visible);
86         m_commitState = COMMIT_STATE_FRAME_IN_PROGRESS;
87         m_needsCommit = false;
88         return;
89
90     case ACTION_BEGIN_UPDATE_MORE_RESOURCES:
91         ASSERT(m_commitState == COMMIT_STATE_UPDATING_RESOURCES);
92         m_updateMoreResourcesPending = true;
93         return;
94
95     case ACTION_COMMIT:
96         if (m_needsCommit || !m_visible)
97             m_commitState = COMMIT_STATE_WAITING_FOR_FIRST_DRAW;
98         else
99             m_commitState = COMMIT_STATE_IDLE;
100         m_needsRedraw = true;
101         return;
102
103     case ACTION_DRAW:
104         m_needsRedraw = false;
105         m_needsForcedRedraw = false;
106         if (m_insideVSync)
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;
111         }
112         return;
113     }
114 }
115
116 void CCSchedulerStateMachine::didEnterVSync()
117 {
118     m_insideVSync = true;
119 }
120
121 void CCSchedulerStateMachine::didLeaveVSync()
122 {
123     m_currentFrameNumber++;
124     m_insideVSync = false;
125 }
126
127 void CCSchedulerStateMachine::setVisible(bool visible)
128 {
129     m_visible = visible;
130 }
131
132 void CCSchedulerStateMachine::setNeedsRedraw()
133 {
134     m_needsRedraw = true;
135 }
136
137 void CCSchedulerStateMachine::setNeedsForcedRedraw()
138 {
139     m_needsForcedRedraw = true;
140 }
141
142 void CCSchedulerStateMachine::setNeedsCommit()
143 {
144     m_needsCommit = true;
145 }
146
147 void CCSchedulerStateMachine::beginFrameComplete()
148 {
149     ASSERT(m_commitState == COMMIT_STATE_FRAME_IN_PROGRESS);
150     m_commitState = COMMIT_STATE_UPDATING_RESOURCES;
151 }
152
153 void CCSchedulerStateMachine::beginUpdateMoreResourcesComplete(bool morePending)
154 {
155     ASSERT(m_commitState == COMMIT_STATE_UPDATING_RESOURCES);
156     ASSERT(m_updateMoreResourcesPending);
157     m_updateMoreResourcesPending = false;
158     if (!morePending)
159         m_commitState = COMMIT_STATE_READY_TO_COMMIT;
160 }
161
162 }