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/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
,
14 scoped_ptr
<FrameRateController
> frame_rate_controller
,
15 const SchedulerSettings
& scheduler_settings
)
16 : settings_(scheduler_settings
),
18 frame_rate_controller_(frame_rate_controller
.Pass()),
19 state_machine_(scheduler_settings
),
20 inside_process_scheduled_actions_(false) {
22 frame_rate_controller_
->SetClient(this);
23 DCHECK(!state_machine_
.VSyncCallbackNeeded());
26 Scheduler::~Scheduler() { frame_rate_controller_
->SetActive(false); }
28 void Scheduler::SetCanBeginFrame(bool can
) {
29 state_machine_
.SetCanBeginFrame(can
);
30 ProcessScheduledActions();
33 void Scheduler::SetVisible(bool visible
) {
34 state_machine_
.SetVisible(visible
);
35 ProcessScheduledActions();
38 void Scheduler::SetCanDraw(bool can_draw
) {
39 state_machine_
.SetCanDraw(can_draw
);
40 ProcessScheduledActions();
43 void Scheduler::SetHasPendingTree(bool has_pending_tree
) {
44 state_machine_
.SetHasPendingTree(has_pending_tree
);
45 ProcessScheduledActions();
48 void Scheduler::SetNeedsCommit() {
49 state_machine_
.SetNeedsCommit();
50 ProcessScheduledActions();
53 void Scheduler::SetNeedsForcedCommit() {
54 state_machine_
.SetNeedsCommit();
55 state_machine_
.SetNeedsForcedCommit();
56 ProcessScheduledActions();
59 void Scheduler::SetNeedsRedraw() {
60 state_machine_
.SetNeedsRedraw();
61 ProcessScheduledActions();
64 void Scheduler::DidSwapUseIncompleteTile() {
65 state_machine_
.DidSwapUseIncompleteTile();
66 ProcessScheduledActions();
69 void Scheduler::SetNeedsForcedRedraw() {
70 state_machine_
.SetNeedsForcedRedraw();
71 ProcessScheduledActions();
74 void Scheduler::SetMainThreadNeedsLayerTextures() {
75 state_machine_
.SetMainThreadNeedsLayerTextures();
76 ProcessScheduledActions();
79 void Scheduler::BeginFrameComplete() {
80 TRACE_EVENT0("cc", "Scheduler::beginFrameComplete");
81 state_machine_
.BeginFrameComplete();
82 ProcessScheduledActions();
85 void Scheduler::BeginFrameAborted() {
86 TRACE_EVENT0("cc", "Scheduler::beginFrameAborted");
87 state_machine_
.BeginFrameAborted();
88 ProcessScheduledActions();
91 void Scheduler::SetMaxFramesPending(int max_frames_pending
) {
92 frame_rate_controller_
->SetMaxFramesPending(max_frames_pending
);
95 int Scheduler::MaxFramesPending() const {
96 return frame_rate_controller_
->MaxFramesPending();
99 void Scheduler::SetSwapBuffersCompleteSupported(bool supported
) {
100 frame_rate_controller_
->SetSwapBuffersCompleteSupported(supported
);
103 void Scheduler::DidSwapBuffersComplete() {
104 TRACE_EVENT0("cc", "Scheduler::didSwapBuffersComplete");
105 frame_rate_controller_
->DidFinishFrame();
108 void Scheduler::DidLoseOutputSurface() {
109 TRACE_EVENT0("cc", "Scheduler::didLoseOutputSurface");
110 state_machine_
.DidLoseOutputSurface();
111 ProcessScheduledActions();
114 void Scheduler::DidRecreateOutputSurface() {
115 TRACE_EVENT0("cc", "Scheduler::didRecreateOutputSurface");
116 frame_rate_controller_
->DidAbortAllPendingFrames();
117 state_machine_
.DidRecreateOutputSurface();
118 ProcessScheduledActions();
121 void Scheduler::SetTimebaseAndInterval(base::TimeTicks timebase
,
122 base::TimeDelta interval
) {
123 frame_rate_controller_
->SetTimebaseAndInterval(timebase
, interval
);
126 base::TimeTicks
Scheduler::AnticipatedDrawTime() {
127 return frame_rate_controller_
->NextTickTime();
130 base::TimeTicks
Scheduler::LastVSyncTime() {
131 return frame_rate_controller_
->LastTickTime();
134 void Scheduler::VSyncTick(bool throttled
) {
135 TRACE_EVENT1("cc", "Scheduler::VSyncTick", "throttled", throttled
);
137 state_machine_
.DidEnterVSync();
138 ProcessScheduledActions();
140 state_machine_
.DidLeaveVSync();
143 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 (inside_process_scheduled_actions_
)
149 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
151 SchedulerStateMachine::Action action
= state_machine_
.NextAction();
152 while (action
!= SchedulerStateMachine::ACTION_NONE
) {
153 state_machine_
.UpdateState(action
);
155 "cc", "Scheduler::ProcessScheduledActions()", "action", action
);
158 case SchedulerStateMachine::ACTION_NONE
:
160 case SchedulerStateMachine::ACTION_BEGIN_FRAME
:
161 client_
->ScheduledActionBeginFrame();
163 case SchedulerStateMachine::ACTION_COMMIT
:
164 client_
->ScheduledActionCommit();
166 case SchedulerStateMachine::ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS
:
167 client_
->ScheduledActionCheckForCompletedTileUploads();
169 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED
:
170 client_
->ScheduledActionActivatePendingTreeIfNeeded();
172 case SchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE
: {
173 ScheduledActionDrawAndSwapResult result
=
174 client_
->ScheduledActionDrawAndSwapIfPossible();
175 state_machine_
.DidDrawIfPossibleCompleted(result
.did_draw
);
177 frame_rate_controller_
->DidBeginFrame();
180 case SchedulerStateMachine::ACTION_DRAW_FORCED
: {
181 ScheduledActionDrawAndSwapResult result
=
182 client_
->ScheduledActionDrawAndSwapForced();
184 frame_rate_controller_
->DidBeginFrame();
187 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_RECREATION
:
188 client_
->ScheduledActionBeginContextRecreation();
190 case SchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
:
191 client_
->ScheduledActionAcquireLayerTexturesForMainThread();
194 action
= state_machine_
.NextAction();
197 // Activate or deactivate the frame rate controller.
198 frame_rate_controller_
->SetActive(state_machine_
.VSyncCallbackNeeded());
199 client_
->DidAnticipatedDrawTimeChange(frame_rate_controller_
->NextTickTime());