cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / cc / scheduler / scheduler.cc
blobe383894a7836f9164ceb6cb14e647ce929837622
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"
10 #include "cc/debug/traced_value.h"
12 namespace cc {
14 Scheduler::Scheduler(SchedulerClient* client,
15 const SchedulerSettings& scheduler_settings)
16 : settings_(scheduler_settings),
17 client_(client),
18 weak_factory_(this),
19 last_set_needs_begin_frame_(false),
20 has_pending_begin_frame_(false),
21 safe_to_expect_begin_frame_(false),
22 state_machine_(scheduler_settings),
23 inside_process_scheduled_actions_(false) {
24 DCHECK(client_);
25 DCHECK(!state_machine_.BeginFrameNeededToDrawByImplThread());
28 Scheduler::~Scheduler() {
29 client_->SetNeedsBeginFrameOnImplThread(false);
32 void Scheduler::SetCanStart() {
33 state_machine_.SetCanStart();
34 ProcessScheduledActions();
37 void Scheduler::SetVisible(bool visible) {
38 state_machine_.SetVisible(visible);
39 ProcessScheduledActions();
42 void Scheduler::SetCanDraw(bool can_draw) {
43 state_machine_.SetCanDraw(can_draw);
44 ProcessScheduledActions();
47 void Scheduler::NotifyReadyToActivate() {
48 state_machine_.NotifyReadyToActivate();
49 ProcessScheduledActions();
52 void Scheduler::SetNeedsCommit() {
53 state_machine_.SetNeedsCommit();
54 ProcessScheduledActions();
57 void Scheduler::SetNeedsForcedCommit() {
58 state_machine_.SetNeedsCommit();
59 state_machine_.SetNeedsForcedCommit();
60 ProcessScheduledActions();
63 void Scheduler::SetNeedsRedraw() {
64 state_machine_.SetNeedsRedraw();
65 ProcessScheduledActions();
68 void Scheduler::DidSwapUseIncompleteTile() {
69 state_machine_.DidSwapUseIncompleteTile();
70 ProcessScheduledActions();
73 void Scheduler::SetNeedsForcedRedraw() {
74 state_machine_.SetNeedsForcedRedraw();
75 ProcessScheduledActions();
78 void Scheduler::SetMainThreadNeedsLayerTextures() {
79 state_machine_.SetMainThreadNeedsLayerTextures();
80 ProcessScheduledActions();
83 void Scheduler::FinishCommit() {
84 TRACE_EVENT0("cc", "Scheduler::FinishCommit");
85 state_machine_.FinishCommit();
86 ProcessScheduledActions();
89 void Scheduler::BeginFrameAbortedByMainThread(bool did_handle) {
90 TRACE_EVENT0("cc", "Scheduler::BeginFrameAbortedByMainThread");
91 state_machine_.BeginFrameAbortedByMainThread(did_handle);
92 ProcessScheduledActions();
95 void Scheduler::DidLoseOutputSurface() {
96 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
97 state_machine_.DidLoseOutputSurface();
98 ProcessScheduledActions();
101 void Scheduler::DidCreateAndInitializeOutputSurface() {
102 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
103 state_machine_.DidCreateAndInitializeOutputSurface();
104 has_pending_begin_frame_ = false;
105 last_set_needs_begin_frame_ = false;
106 safe_to_expect_begin_frame_ = false;
107 ProcessScheduledActions();
110 base::TimeTicks Scheduler::AnticipatedDrawTime() {
111 TRACE_EVENT0("cc", "Scheduler::AnticipatedDrawTime");
113 if (!last_set_needs_begin_frame_ ||
114 last_begin_frame_args_.interval <= base::TimeDelta())
115 return base::TimeTicks();
117 // TODO(brianderson): Express this in terms of the deadline.
118 base::TimeTicks now = base::TimeTicks::Now();
119 int64 intervals = 1 + ((now - last_begin_frame_args_.frame_time) /
120 last_begin_frame_args_.interval);
121 return last_begin_frame_args_.frame_time +
122 (last_begin_frame_args_.interval * intervals);
125 base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() {
126 return last_begin_frame_args_.frame_time;
129 void Scheduler::SetupNextBeginFrameIfNeeded() {
130 bool needs_begin_frame_to_draw =
131 state_machine_.BeginFrameNeededToDrawByImplThread();
132 // We want to avoid proactive begin frames with the synchronous compositor
133 // because every SetNeedsBeginFrame will force a redraw.
134 bool proactive_begin_frame_wanted =
135 state_machine_.ProactiveBeginFrameWantedByImplThread() &&
136 !settings_.using_synchronous_renderer_compositor &&
137 settings_.throttle_frame_production;
138 bool needs_begin_frame = needs_begin_frame_to_draw ||
139 proactive_begin_frame_wanted;
140 bool immediate_disables_needed =
141 settings_.using_synchronous_renderer_compositor;
143 if (needs_begin_frame_to_draw)
144 safe_to_expect_begin_frame_ = true;
146 // Determine if we need BeginFrame notifications.
147 // If we do, always request the BeginFrame immediately.
148 // If not, only disable on the next BeginFrame to avoid unnecessary toggles.
149 // The synchronous renderer compositor requires immediate disables though.
150 if ((needs_begin_frame ||
151 state_machine_.inside_begin_frame() ||
152 immediate_disables_needed) &&
153 (needs_begin_frame != last_set_needs_begin_frame_)) {
154 has_pending_begin_frame_ = false;
155 client_->SetNeedsBeginFrameOnImplThread(needs_begin_frame);
156 if (safe_to_expect_begin_frame_)
157 last_set_needs_begin_frame_ = needs_begin_frame;
160 // Request another BeginFrame if we haven't drawn for now until we have
161 // deadlines implemented.
162 if (state_machine_.inside_begin_frame() && has_pending_begin_frame_) {
163 has_pending_begin_frame_ = false;
164 client_->SetNeedsBeginFrameOnImplThread(true);
165 return;
169 void Scheduler::BeginFrame(const BeginFrameArgs& args) {
170 TRACE_EVENT0("cc", "Scheduler::BeginFrame");
171 DCHECK(!has_pending_begin_frame_);
172 has_pending_begin_frame_ = true;
173 safe_to_expect_begin_frame_ = true;
174 last_begin_frame_args_ = args;
175 state_machine_.DidEnterBeginFrame(args);
176 ProcessScheduledActions();
177 state_machine_.DidLeaveBeginFrame();
180 void Scheduler::DrawAndSwapIfPossible() {
181 ScheduledActionDrawAndSwapResult result =
182 client_->ScheduledActionDrawAndSwapIfPossible();
183 state_machine_.DidDrawIfPossibleCompleted(result.did_draw);
184 if (result.did_swap)
185 has_pending_begin_frame_ = false;
188 void Scheduler::DrawAndSwapForced() {
189 ScheduledActionDrawAndSwapResult result =
190 client_->ScheduledActionDrawAndSwapForced();
191 if (result.did_swap)
192 has_pending_begin_frame_ = false;
195 void Scheduler::ProcessScheduledActions() {
196 // We do not allow ProcessScheduledActions to be recursive.
197 // The top-level call will iteratively execute the next action for us anyway.
198 if (inside_process_scheduled_actions_)
199 return;
201 base::AutoReset<bool> mark_inside(&inside_process_scheduled_actions_, true);
203 SchedulerStateMachine::Action action;
204 do {
205 action = state_machine_.NextAction();
206 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
207 "SchedulerStateMachine",
208 "state",
209 TracedValue::FromValue(state_machine_.AsValue().release()));
210 state_machine_.UpdateState(action);
211 switch (action) {
212 case SchedulerStateMachine::ACTION_NONE:
213 break;
214 case SchedulerStateMachine::ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD:
215 client_->ScheduledActionSendBeginFrameToMainThread();
216 break;
217 case SchedulerStateMachine::ACTION_COMMIT:
218 client_->ScheduledActionCommit();
219 break;
220 case SchedulerStateMachine::ACTION_UPDATE_VISIBLE_TILES:
221 client_->ScheduledActionUpdateVisibleTiles();
222 break;
223 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE:
224 client_->ScheduledActionActivatePendingTree();
225 break;
226 case SchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE:
227 DrawAndSwapIfPossible();
228 break;
229 case SchedulerStateMachine::ACTION_DRAW_FORCED:
230 DrawAndSwapForced();
231 break;
232 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT:
233 // No action is actually performed, but this allows the state machine to
234 // advance out of its waiting to draw state without actually drawing.
235 break;
236 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
237 client_->ScheduledActionBeginOutputSurfaceCreation();
238 break;
239 case SchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD:
240 client_->ScheduledActionAcquireLayerTexturesForMainThread();
241 break;
243 } while (action != SchedulerStateMachine::ACTION_NONE);
245 SetupNextBeginFrameIfNeeded();
246 client_->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
249 bool Scheduler::WillDrawIfNeeded() const {
250 return !state_machine_.PendingDrawsShouldBeAborted();
253 } // namespace cc