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"
10 #include <base/auto_reset.h>
14 CCScheduler::CCScheduler(CCSchedulerClient
* client
, PassOwnPtr
<CCFrameRateController
> frameRateController
)
16 , m_frameRateController(frameRateController
)
17 , m_insideProcessScheduledActions(false)
20 m_frameRateController
->setClient(this);
21 ASSERT(!m_stateMachine
.vsyncCallbackNeeded());
24 CCScheduler::~CCScheduler()
26 m_frameRateController
->setActive(false);
29 void CCScheduler::setCanBeginFrame(bool can
)
31 m_stateMachine
.setCanBeginFrame(can
);
32 processScheduledActions();
35 void CCScheduler::setVisible(bool visible
)
37 m_stateMachine
.setVisible(visible
);
38 processScheduledActions();
41 void CCScheduler::setCanDraw(bool canDraw
)
43 m_stateMachine
.setCanDraw(canDraw
);
44 processScheduledActions();
47 void CCScheduler::setNeedsCommit()
49 m_stateMachine
.setNeedsCommit();
50 processScheduledActions();
53 void CCScheduler::setNeedsForcedCommit()
55 m_stateMachine
.setNeedsForcedCommit();
56 processScheduledActions();
59 void CCScheduler::setNeedsRedraw()
61 m_stateMachine
.setNeedsRedraw();
62 processScheduledActions();
65 void CCScheduler::setNeedsForcedRedraw()
67 m_stateMachine
.setNeedsForcedRedraw();
68 processScheduledActions();
71 void CCScheduler::setMainThreadNeedsLayerTextures()
73 m_stateMachine
.setMainThreadNeedsLayerTextures();
74 processScheduledActions();
77 void CCScheduler::beginFrameComplete()
79 TRACE_EVENT0("cc", "CCScheduler::beginFrameComplete");
80 m_stateMachine
.beginFrameComplete();
81 processScheduledActions();
84 void CCScheduler::beginFrameAborted()
86 TRACE_EVENT0("cc", "CCScheduler::beginFrameAborted");
87 m_stateMachine
.beginFrameAborted();
88 processScheduledActions();
91 void CCScheduler::setMaxFramesPending(int maxFramesPending
)
93 m_frameRateController
->setMaxFramesPending(maxFramesPending
);
96 void CCScheduler::setSwapBuffersCompleteSupported(bool supported
)
98 m_frameRateController
->setSwapBuffersCompleteSupported(supported
);
101 void CCScheduler::didSwapBuffersComplete()
103 TRACE_EVENT0("cc", "CCScheduler::didSwapBuffersComplete");
104 m_frameRateController
->didFinishFrame();
107 void CCScheduler::didLoseContext()
109 TRACE_EVENT0("cc", "CCScheduler::didLoseContext");
110 m_frameRateController
->didAbortAllPendingFrames();
111 m_stateMachine
.didLoseContext();
112 processScheduledActions();
115 void CCScheduler::didRecreateContext()
117 TRACE_EVENT0("cc", "CCScheduler::didRecreateContext");
118 m_stateMachine
.didRecreateContext();
119 processScheduledActions();
122 void CCScheduler::setTimebaseAndInterval(base::TimeTicks timebase
, base::TimeDelta interval
)
124 m_frameRateController
->setTimebaseAndInterval(timebase
, interval
);
127 base::TimeTicks
CCScheduler::anticipatedDrawTime()
129 return m_frameRateController
->nextTickTime();
132 void CCScheduler::vsyncTick(bool throttled
)
134 TRACE_EVENT1("cc", "CCScheduler::vsyncTick", "throttled", throttled
);
136 m_stateMachine
.didEnterVSync();
137 processScheduledActions();
139 m_stateMachine
.didLeaveVSync();
142 void CCScheduler::processScheduledActions()
144 // We do not allow processScheduledActions to be recursive.
145 // The top-level call will iteratively execute the next action for us anyway.
146 if (m_insideProcessScheduledActions
)
149 AutoReset
<bool> markInside(&m_insideProcessScheduledActions
, true);
151 CCSchedulerStateMachine::Action action
= m_stateMachine
.nextAction();
152 while (action
!= CCSchedulerStateMachine::ACTION_NONE
) {
153 m_stateMachine
.updateState(action
);
154 TRACE_EVENT1("cc", "CCScheduler::processScheduledActions()", "action", action
);
157 case CCSchedulerStateMachine::ACTION_NONE
:
159 case CCSchedulerStateMachine::ACTION_BEGIN_FRAME
:
160 m_client
->scheduledActionBeginFrame();
162 case CCSchedulerStateMachine::ACTION_COMMIT
:
163 m_client
->scheduledActionCommit();
165 case CCSchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE
: {
166 CCScheduledActionDrawAndSwapResult result
= m_client
->scheduledActionDrawAndSwapIfPossible();
167 m_stateMachine
.didDrawIfPossibleCompleted(result
.didDraw
);
169 m_frameRateController
->didBeginFrame();
172 case CCSchedulerStateMachine::ACTION_DRAW_FORCED
: {
173 CCScheduledActionDrawAndSwapResult result
= m_client
->scheduledActionDrawAndSwapForced();
175 m_frameRateController
->didBeginFrame();
177 } case CCSchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION
:
178 m_client
->scheduledActionBeginContextRecreation();
180 case CCSchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
:
181 m_client
->scheduledActionAcquireLayerTexturesForMainThread();
184 action
= m_stateMachine
.nextAction();
187 // Activate or deactivate the frame rate controller.
188 m_frameRateController
->setActive(m_stateMachine
.vsyncCallbackNeeded());
189 m_client
->didAnticipatedDrawTimeChange(m_frameRateController
->nextTickTime());