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.
7 #include "CCScheduler.h"
9 #include "TraceEvent.h"
13 CCScheduler::CCScheduler(CCSchedulerClient
* client
, PassOwnPtr
<CCFrameRateController
> frameRateController
)
15 , m_frameRateController(frameRateController
)
16 , m_updateMoreResourcesPending(false)
19 m_frameRateController
->setClient(this);
20 m_frameRateController
->setActive(m_stateMachine
.vsyncCallbackNeeded());
23 CCScheduler::~CCScheduler()
25 m_frameRateController
->setActive(false);
28 void CCScheduler::setCanBeginFrame(bool can
)
30 m_stateMachine
.setCanBeginFrame(can
);
31 processScheduledActions();
34 void CCScheduler::setVisible(bool visible
)
36 m_stateMachine
.setVisible(visible
);
37 processScheduledActions();
40 void CCScheduler::setCanDraw(bool canDraw
)
42 m_stateMachine
.setCanDraw(canDraw
);
44 // Defer processScheduleActions so we don't recurse and commit/draw
45 // multiple frames. We can call processScheduledActions directly
46 // once it is no longer re-entrant.
47 m_frameRateController
->setActive(m_stateMachine
.vsyncCallbackNeeded());
50 void CCScheduler::setNeedsCommit()
52 m_stateMachine
.setNeedsCommit();
53 processScheduledActions();
56 void CCScheduler::setNeedsForcedCommit()
58 m_stateMachine
.setNeedsForcedCommit();
59 processScheduledActions();
62 void CCScheduler::setNeedsRedraw()
64 m_stateMachine
.setNeedsRedraw();
65 processScheduledActions();
68 void CCScheduler::setNeedsForcedRedraw()
70 m_stateMachine
.setNeedsForcedRedraw();
71 processScheduledActions();
74 void CCScheduler::setMainThreadNeedsLayerTextures()
76 m_stateMachine
.setMainThreadNeedsLayerTextures();
77 processScheduledActions();
80 void CCScheduler::beginFrameComplete()
82 TRACE_EVENT0("cc", "CCScheduler::beginFrameComplete");
83 m_stateMachine
.beginFrameComplete();
84 processScheduledActions();
87 void CCScheduler::beginFrameAborted()
89 TRACE_EVENT0("cc", "CCScheduler::beginFrameAborted");
90 m_stateMachine
.beginFrameAborted();
91 processScheduledActions();
94 void CCScheduler::setMaxFramesPending(int maxFramesPending
)
96 m_frameRateController
->setMaxFramesPending(maxFramesPending
);
99 void CCScheduler::setSwapBuffersCompleteSupported(bool supported
)
101 m_frameRateController
->setSwapBuffersCompleteSupported(supported
);
104 void CCScheduler::didSwapBuffersComplete()
106 TRACE_EVENT0("cc", "CCScheduler::didSwapBuffersComplete");
107 m_frameRateController
->didFinishFrame();
110 void CCScheduler::didLoseContext()
112 TRACE_EVENT0("cc", "CCScheduler::didLoseContext");
113 m_frameRateController
->didAbortAllPendingFrames();
114 m_stateMachine
.didLoseContext();
115 processScheduledActions();
118 void CCScheduler::didRecreateContext()
120 TRACE_EVENT0("cc", "CCScheduler::didRecreateContext");
121 m_stateMachine
.didRecreateContext();
122 processScheduledActions();
125 void CCScheduler::setTimebaseAndInterval(double timebase
, double intervalSeconds
)
127 m_frameRateController
->setTimebaseAndInterval(timebase
, intervalSeconds
);
130 void CCScheduler::vsyncTick()
132 if (m_updateMoreResourcesPending
) {
133 m_updateMoreResourcesPending
= false;
134 m_stateMachine
.beginUpdateMoreResourcesComplete(m_client
->hasMoreResourceUpdates());
136 TRACE_EVENT0("cc", "CCScheduler::vsyncTick");
138 m_stateMachine
.didEnterVSync();
139 processScheduledActions();
140 m_stateMachine
.didLeaveVSync();
143 void CCScheduler::processScheduledActions()
145 // Early out so we don't spam TRACE_EVENTS with useless processScheduledActions.
146 if (m_stateMachine
.nextAction() == CCSchedulerStateMachine::ACTION_NONE
) {
147 m_frameRateController
->setActive(m_stateMachine
.vsyncCallbackNeeded());
151 // This function can re-enter itself. For example, draw may call
152 // setNeedsCommit. Proceeed with caution.
153 CCSchedulerStateMachine::Action action
;
155 action
= m_stateMachine
.nextAction();
156 m_stateMachine
.updateState(action
);
157 TRACE_EVENT1("cc", "CCScheduler::processScheduledActions()", "action", action
);
160 case CCSchedulerStateMachine::ACTION_NONE
:
162 case CCSchedulerStateMachine::ACTION_BEGIN_FRAME
:
163 m_client
->scheduledActionBeginFrame();
165 case CCSchedulerStateMachine::ACTION_BEGIN_UPDATE_MORE_RESOURCES
:
166 if (m_client
->hasMoreResourceUpdates()) {
167 m_client
->scheduledActionUpdateMoreResources(m_frameRateController
->nextTickTimeIfActivated());
168 m_updateMoreResourcesPending
= true;
170 m_stateMachine
.beginUpdateMoreResourcesComplete(false);
172 case CCSchedulerStateMachine::ACTION_COMMIT
:
173 m_client
->scheduledActionCommit();
175 case CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE
: {
176 CCScheduledActionDrawAndSwapResult result
= m_client
->scheduledActionDrawAndSwapIfPossible();
177 m_stateMachine
.didDrawIfPossibleCompleted(result
.didDraw
);
179 m_frameRateController
->didBeginFrame();
182 case CCSchedulerStateMachine::ACTION_DRAW_FORCED
: {
183 CCScheduledActionDrawAndSwapResult result
= m_client
->scheduledActionDrawAndSwapForced();
185 m_frameRateController
->didBeginFrame();
187 } case CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION
:
188 m_client
->scheduledActionBeginContextRecreation();
190 case CCSchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
:
191 m_client
->scheduledActionAcquireLayerTexturesForMainThread();
194 } while (action
!= CCSchedulerStateMachine::ACTION_NONE
);
196 // Activate or deactivate the frame rate controller.
197 m_frameRateController
->setActive(m_stateMachine
.vsyncCallbackNeeded());