[chromium] LayerRendererChromium is not getting visibility messages in single threade...
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / cc / CCScheduler.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/CCScheduler.h"
28 #include "TraceEvent.h"
29
30 namespace WebCore {
31
32 CCScheduler::CCScheduler(CCSchedulerClient* client, PassOwnPtr<CCFrameRateController> frameRateController)
33     : m_client(client)
34     , m_frameRateController(frameRateController)
35     , m_updateMoreResourcesPending(false)
36 {
37     ASSERT(m_client);
38     m_frameRateController->setClient(this);
39     m_frameRateController->setActive(m_stateMachine.vsyncCallbackNeeded());
40 }
41
42 CCScheduler::~CCScheduler()
43 {
44     m_frameRateController->setActive(false);
45 }
46
47 void CCScheduler::setCanBeginFrame(bool can)
48 {
49     m_stateMachine.setCanBeginFrame(can);
50     processScheduledActions();
51 }
52
53 void CCScheduler::setVisible(bool visible)
54 {
55     m_stateMachine.setVisible(visible);
56     processScheduledActions();
57 }
58
59 void CCScheduler::setNeedsCommit()
60 {
61     m_stateMachine.setNeedsCommit();
62     processScheduledActions();
63 }
64
65 void CCScheduler::setNeedsForcedCommit()
66 {
67     m_stateMachine.setNeedsForcedCommit();
68     processScheduledActions();
69 }
70
71 void CCScheduler::setNeedsRedraw()
72 {
73     m_stateMachine.setNeedsRedraw();
74     processScheduledActions();
75 }
76
77 void CCScheduler::setNeedsForcedRedraw()
78 {
79     m_stateMachine.setNeedsForcedRedraw();
80     processScheduledActions();
81 }
82
83 void CCScheduler::setMainThreadNeedsLayerTextures()
84 {
85     m_stateMachine.setMainThreadNeedsLayerTextures();
86     processScheduledActions();
87 }
88
89 void CCScheduler::beginFrameComplete()
90 {
91     TRACE_EVENT0("cc", "CCScheduler::beginFrameComplete");
92     m_stateMachine.beginFrameComplete();
93     processScheduledActions();
94 }
95
96 void CCScheduler::beginFrameAborted()
97 {
98     TRACE_EVENT0("cc", "CCScheduler::beginFrameAborted");
99     m_stateMachine.beginFrameAborted();
100     processScheduledActions();
101 }
102
103 void CCScheduler::setMaxFramesPending(int maxFramesPending)
104 {
105     m_frameRateController->setMaxFramesPending(maxFramesPending);
106 }
107
108 void CCScheduler::didSwapBuffersComplete()
109 {
110     TRACE_EVENT0("cc", "CCScheduler::didSwapBuffersComplete");
111     m_frameRateController->didFinishFrame();
112 }
113
114 void CCScheduler::didLoseContext()
115 {
116     TRACE_EVENT0("cc", "CCScheduler::didLoseContext");
117     m_frameRateController->didAbortAllPendingFrames();
118     m_stateMachine.didLoseContext();
119     processScheduledActions();
120 }
121
122 void CCScheduler::didRecreateContext()
123 {
124     TRACE_EVENT0("cc", "CCScheduler::didRecreateContext");
125     m_stateMachine.didRecreateContext();
126     processScheduledActions();
127 }
128
129 void CCScheduler::vsyncTick()
130 {
131     if (m_updateMoreResourcesPending) {
132         m_updateMoreResourcesPending = false;
133         m_stateMachine.beginUpdateMoreResourcesComplete(m_client->hasMoreResourceUpdates());
134     }
135     TRACE_EVENT0("cc", "CCScheduler::vsyncTick");
136
137     m_stateMachine.didEnterVSync();
138     processScheduledActions();
139     m_stateMachine.didLeaveVSync();
140 }
141
142 CCSchedulerStateMachine::Action CCScheduler::nextAction()
143 {
144     m_stateMachine.setCanDraw(m_client->canDraw());
145     return m_stateMachine.nextAction();
146 }
147
148 void CCScheduler::processScheduledActions()
149 {
150     // Early out so we don't spam TRACE_EVENTS with useless processScheduledActions.
151     if (nextAction() == CCSchedulerStateMachine::ACTION_NONE) {
152         m_frameRateController->setActive(m_stateMachine.vsyncCallbackNeeded());
153         return;
154     }
155
156     // This function can re-enter itself. For example, draw may call
157     // setNeedsCommit. Proceeed with caution.
158     CCSchedulerStateMachine::Action action;
159     do {
160         action = nextAction();
161         m_stateMachine.updateState(action);
162
163         switch (action) {
164         case CCSchedulerStateMachine::ACTION_NONE:
165             break;
166         case CCSchedulerStateMachine::ACTION_BEGIN_FRAME:
167             m_client->scheduledActionBeginFrame();
168             break;
169         case CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES:
170             m_client->scheduledActionUpdateMoreResources();
171             if (!m_client->hasMoreResourceUpdates()) {
172                 // If we were just told to update resources, but there are no
173                 // more pending, then tell the state machine that the
174                 // beginUpdateMoreResources completed. If more are pending,
175                 // then we will ack the update at the next draw.
176                 m_updateMoreResourcesPending = false;
177                 m_stateMachine.beginUpdateMoreResourcesComplete(false);
178             } else
179                 m_updateMoreResourcesPending = true;
180             break;
181         case CCSchedulerStateMachine::ACTION_COMMIT:
182             m_client->scheduledActionCommit();
183             break;
184         case CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE: {
185             CCScheduledActionDrawAndSwapResult result = m_client->scheduledActionDrawAndSwapIfPossible();
186             m_stateMachine.didDrawIfPossibleCompleted(result.didDraw);
187             if (result.didSwap)
188                 m_frameRateController->didBeginFrame();
189             break;
190         }
191         case CCSchedulerStateMachine::ACTION_DRAW_FORCED: {
192             CCScheduledActionDrawAndSwapResult result = m_client->scheduledActionDrawAndSwapForced();
193             if (result.didSwap)
194                 m_frameRateController->didBeginFrame();
195             break;
196         } case CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION:
197             m_client->scheduledActionBeginContextRecreation();
198             break;
199         case CCSchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD:
200             m_client->scheduledActionAcquireLayerTexturesForMainThread();
201             break;
202         }
203     } while (action != CCSchedulerStateMachine::ACTION_NONE);
204
205     // Activate or deactivate the frame rate controller.
206     m_frameRateController->setActive(m_stateMachine.vsyncCallbackNeeded());
207 }
208
209 }