The download arrow is not vertically centered and needs to be shifted down by 2 pt.
[chromium-blink-merge.git] / cc / scheduler / scheduler.cc
blob839389761a790f143e0c60b3c9c8db3014290d72
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"
11 namespace cc {
13 Scheduler::Scheduler(SchedulerClient* client,
14 scoped_ptr<FrameRateController> frame_rate_controller,
15 const SchedulerSettings& scheduler_settings)
16 : settings_(scheduler_settings),
17 client_(client),
18 frame_rate_controller_(frame_rate_controller.Pass()),
19 state_machine_(scheduler_settings),
20 inside_process_scheduled_actions_(false) {
21 DCHECK(client_);
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);
136 if (!throttled)
137 state_machine_.DidEnterVSync();
138 ProcessScheduledActions();
139 if (!throttled)
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_)
147 return;
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);
154 TRACE_EVENT1(
155 "cc", "Scheduler::ProcessScheduledActions()", "action", action);
157 switch (action) {
158 case SchedulerStateMachine::ACTION_NONE:
159 break;
160 case SchedulerStateMachine::ACTION_BEGIN_FRAME:
161 client_->ScheduledActionBeginFrame();
162 break;
163 case SchedulerStateMachine::ACTION_COMMIT:
164 client_->ScheduledActionCommit();
165 break;
166 case SchedulerStateMachine::ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS:
167 client_->ScheduledActionCheckForCompletedTileUploads();
168 break;
169 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED:
170 client_->ScheduledActionActivatePendingTreeIfNeeded();
171 break;
172 case SchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE: {
173 ScheduledActionDrawAndSwapResult result =
174 client_->ScheduledActionDrawAndSwapIfPossible();
175 state_machine_.DidDrawIfPossibleCompleted(result.did_draw);
176 if (result.did_swap)
177 frame_rate_controller_->DidBeginFrame();
178 break;
180 case SchedulerStateMachine::ACTION_DRAW_FORCED: {
181 ScheduledActionDrawAndSwapResult result =
182 client_->ScheduledActionDrawAndSwapForced();
183 if (result.did_swap)
184 frame_rate_controller_->DidBeginFrame();
185 break;
187 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_RECREATION:
188 client_->ScheduledActionBeginContextRecreation();
189 break;
190 case SchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD:
191 client_->ScheduledActionAcquireLayerTexturesForMainThread();
192 break;
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());
202 } // namespace cc