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.
5 #include "cc/scheduler.h"
7 #include "base/auto_reset.h"
8 #include "base/debug/trace_event.h"
9 #include "base/logging.h"
13 Scheduler::Scheduler(SchedulerClient
* client
, scoped_ptr
<FrameRateController
> frameRateController
)
15 , m_frameRateController(frameRateController
.Pass())
16 , m_insideProcessScheduledActions(false)
19 m_frameRateController
->setClient(this);
20 DCHECK(!m_stateMachine
.vsyncCallbackNeeded());
23 Scheduler::~Scheduler()
25 m_frameRateController
->setActive(false);
28 void Scheduler::setCanBeginFrame(bool can
)
30 m_stateMachine
.setCanBeginFrame(can
);
31 processScheduledActions();
34 void Scheduler::setVisible(bool visible
)
36 m_stateMachine
.setVisible(visible
);
37 processScheduledActions();
40 void Scheduler::setCanDraw(bool canDraw
)
42 m_stateMachine
.setCanDraw(canDraw
);
43 processScheduledActions();
46 void Scheduler::setNeedsCommit()
48 m_stateMachine
.setNeedsCommit();
49 processScheduledActions();
52 void Scheduler::setNeedsForcedCommit()
54 m_stateMachine
.setNeedsCommit();
55 m_stateMachine
.setNeedsForcedCommit();
56 processScheduledActions();
59 void Scheduler::setNeedsRedraw()
61 m_stateMachine
.setNeedsRedraw();
62 processScheduledActions();
65 void Scheduler::setNeedsForcedRedraw()
67 m_stateMachine
.setNeedsForcedRedraw();
68 processScheduledActions();
71 void Scheduler::setMainThreadNeedsLayerTextures()
73 m_stateMachine
.setMainThreadNeedsLayerTextures();
74 processScheduledActions();
77 void Scheduler::beginFrameComplete()
79 TRACE_EVENT0("cc", "Scheduler::beginFrameComplete");
80 m_stateMachine
.beginFrameComplete();
81 processScheduledActions();
84 void Scheduler::beginFrameAborted()
86 TRACE_EVENT0("cc", "Scheduler::beginFrameAborted");
87 m_stateMachine
.beginFrameAborted();
88 processScheduledActions();
91 void Scheduler::setMaxFramesPending(int maxFramesPending
)
93 m_frameRateController
->setMaxFramesPending(maxFramesPending
);
96 void Scheduler::setSwapBuffersCompleteSupported(bool supported
)
98 m_frameRateController
->setSwapBuffersCompleteSupported(supported
);
101 void Scheduler::didSwapBuffersComplete()
103 TRACE_EVENT0("cc", "Scheduler::didSwapBuffersComplete");
104 m_frameRateController
->didFinishFrame();
107 void Scheduler::didLoseContext()
109 TRACE_EVENT0("cc", "Scheduler::didLoseContext");
110 m_frameRateController
->didAbortAllPendingFrames();
111 m_stateMachine
.didLoseContext();
112 processScheduledActions();
115 void Scheduler::didRecreateContext()
117 TRACE_EVENT0("cc", "Scheduler::didRecreateContext");
118 m_stateMachine
.didRecreateContext();
119 processScheduledActions();
122 void Scheduler::setTimebaseAndInterval(base::TimeTicks timebase
, base::TimeDelta interval
)
124 m_frameRateController
->setTimebaseAndInterval(timebase
, interval
);
127 base::TimeTicks
Scheduler::anticipatedDrawTime()
129 return m_frameRateController
->nextTickTime();
132 void Scheduler::vsyncTick(bool throttled
)
134 TRACE_EVENT1("cc", "Scheduler::vsyncTick", "throttled", throttled
);
136 m_stateMachine
.didEnterVSync();
137 processScheduledActions();
139 m_stateMachine
.didLeaveVSync();
142 void Scheduler::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 base::AutoReset
<bool> markInside(&m_insideProcessScheduledActions
, true);
151 SchedulerStateMachine::Action action
= m_stateMachine
.nextAction();
152 while (action
!= SchedulerStateMachine::ACTION_NONE
) {
153 m_stateMachine
.updateState(action
);
154 TRACE_EVENT1("cc", "Scheduler::processScheduledActions()", "action", action
);
157 case SchedulerStateMachine::ACTION_NONE
:
159 case SchedulerStateMachine::ACTION_BEGIN_FRAME
:
160 m_client
->scheduledActionBeginFrame();
162 case SchedulerStateMachine::ACTION_COMMIT
:
163 m_client
->scheduledActionCommit();
165 case SchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE
: {
166 ScheduledActionDrawAndSwapResult result
= m_client
->scheduledActionDrawAndSwapIfPossible();
167 m_stateMachine
.didDrawIfPossibleCompleted(result
.didDraw
);
169 m_frameRateController
->didBeginFrame();
172 case SchedulerStateMachine::ACTION_DRAW_FORCED
: {
173 ScheduledActionDrawAndSwapResult result
= m_client
->scheduledActionDrawAndSwapForced();
175 m_frameRateController
->didBeginFrame();
177 } case SchedulerStateMachine::ACTION_BEGIN_CONTEXT_RECREATION
:
178 m_client
->scheduledActionBeginContextRecreation();
180 case SchedulerStateMachine::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());