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"
9 #include "base/auto_reset.h"
10 #include "base/debug/trace_event.h"
11 #include "base/debug/trace_event_argument.h"
12 #include "base/logging.h"
13 #include "base/single_thread_task_runner.h"
14 #include "cc/debug/devtools_instrumentation.h"
15 #include "cc/debug/traced_value.h"
16 #include "cc/scheduler/delay_based_time_source.h"
17 #include "ui/gfx/frame_time.h"
21 BeginFrameSource
* SchedulerFrameSourcesConstructor::ConstructPrimaryFrameSource(
22 Scheduler
* scheduler
) {
23 if (!scheduler
->settings_
.throttle_frame_production
) {
25 "Scheduler::Scheduler()",
27 "BackToBackBeginFrameSource");
28 DCHECK(!scheduler
->primary_frame_source_internal_
);
29 scheduler
->primary_frame_source_internal_
=
30 BackToBackBeginFrameSource::Create(scheduler
->task_runner_
.get());
31 return scheduler
->primary_frame_source_internal_
.get();
32 } else if (scheduler
->settings_
.use_external_begin_frame_source
) {
34 "Scheduler::Scheduler()",
36 "ExternalBeginFrameSource");
37 DCHECK(scheduler
->primary_frame_source_internal_
)
38 << "Need external BeginFrameSource";
39 return scheduler
->primary_frame_source_internal_
.get();
42 "Scheduler::Scheduler()",
44 "SyntheticBeginFrameSource");
45 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_source
=
46 SyntheticBeginFrameSource::Create(scheduler
->task_runner_
.get(),
48 BeginFrameArgs::DefaultInterval());
50 DCHECK(!scheduler
->vsync_observer_
);
51 scheduler
->vsync_observer_
= synthetic_source
.get();
53 DCHECK(!scheduler
->primary_frame_source_internal_
);
54 scheduler
->primary_frame_source_internal_
= synthetic_source
.Pass();
55 return scheduler
->primary_frame_source_internal_
.get();
60 SchedulerFrameSourcesConstructor::ConstructBackgroundFrameSource(
61 Scheduler
* scheduler
) {
63 "Scheduler::Scheduler()",
64 "BackgroundFrameSource",
65 "SyntheticBeginFrameSource");
66 DCHECK(!(scheduler
->background_frame_source_internal_
));
67 scheduler
->background_frame_source_internal_
=
68 SyntheticBeginFrameSource::Create(
69 scheduler
->task_runner_
.get(), scheduler
->Now(),
70 scheduler
->settings_
.background_frame_interval
);
71 return scheduler
->background_frame_source_internal_
.get();
75 SchedulerClient
* client
,
76 const SchedulerSettings
& scheduler_settings
,
77 int layer_tree_host_id
,
78 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
79 base::PowerMonitor
* power_monitor
,
80 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
81 SchedulerFrameSourcesConstructor
* frame_sources_constructor
)
83 primary_frame_source_(NULL
),
84 background_frame_source_(NULL
),
85 primary_frame_source_internal_(external_begin_frame_source
.Pass()),
86 background_frame_source_internal_(),
87 vsync_observer_(NULL
),
88 settings_(scheduler_settings
),
90 layer_tree_host_id_(layer_tree_host_id
),
91 task_runner_(task_runner
),
92 power_monitor_(power_monitor
),
93 begin_retro_frame_posted_(false),
94 state_machine_(scheduler_settings
),
95 inside_process_scheduled_actions_(false),
96 inside_action_(SchedulerStateMachine::ACTION_NONE
),
98 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
99 "Scheduler::Scheduler",
101 settings_
.AsValue());
103 DCHECK(!state_machine_
.BeginFrameNeeded());
105 begin_retro_frame_closure_
=
106 base::Bind(&Scheduler::BeginRetroFrame
, weak_factory_
.GetWeakPtr());
107 begin_impl_frame_deadline_closure_
= base::Bind(
108 &Scheduler::OnBeginImplFrameDeadline
, weak_factory_
.GetWeakPtr());
109 poll_for_draw_triggers_closure_
= base::Bind(
110 &Scheduler::PollForAnticipatedDrawTriggers
, weak_factory_
.GetWeakPtr());
111 advance_commit_state_closure_
= base::Bind(
112 &Scheduler::PollToAdvanceCommitState
, weak_factory_
.GetWeakPtr());
114 frame_source_
= BeginFrameSourceMultiplexer::Create();
115 frame_source_
->AddObserver(this);
117 // Primary frame source
118 primary_frame_source_
=
119 frame_sources_constructor
->ConstructPrimaryFrameSource(this);
120 frame_source_
->AddSource(primary_frame_source_
);
121 primary_frame_source_
->SetClientReady();
123 // Background ticking frame source
124 background_frame_source_
=
125 frame_sources_constructor
->ConstructBackgroundFrameSource(this);
126 frame_source_
->AddSource(background_frame_source_
);
128 SetupPowerMonitoring();
131 Scheduler::~Scheduler() {
132 TeardownPowerMonitoring();
133 if (frame_source_
->NeedsBeginFrames())
134 frame_source_
->SetNeedsBeginFrames(false);
137 base::TimeTicks
Scheduler::Now() const {
138 base::TimeTicks now
= gfx::FrameTime::Now();
139 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"),
146 void Scheduler::SetupPowerMonitoring() {
147 if (settings_
.disable_hi_res_timer_tasks_on_battery
) {
148 DCHECK(power_monitor_
);
149 power_monitor_
->AddObserver(this);
150 state_machine_
.SetImplLatencyTakesPriorityOnBattery(
151 power_monitor_
->IsOnBatteryPower());
155 void Scheduler::TeardownPowerMonitoring() {
156 if (settings_
.disable_hi_res_timer_tasks_on_battery
) {
157 DCHECK(power_monitor_
);
158 power_monitor_
->RemoveObserver(this);
162 void Scheduler::OnPowerStateChange(bool on_battery_power
) {
163 DCHECK(settings_
.disable_hi_res_timer_tasks_on_battery
);
164 state_machine_
.SetImplLatencyTakesPriorityOnBattery(on_battery_power
);
167 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase
,
168 base::TimeDelta interval
) {
169 // TODO(brianderson): We should not be receiving 0 intervals.
170 if (interval
== base::TimeDelta())
171 interval
= BeginFrameArgs::DefaultInterval();
174 vsync_observer_
->OnUpdateVSyncParameters(timebase
, interval
);
177 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time
) {
178 DCHECK_GE(draw_time
.ToInternalValue(), 0);
179 estimated_parent_draw_time_
= draw_time
;
182 void Scheduler::SetCanStart() {
183 state_machine_
.SetCanStart();
184 ProcessScheduledActions();
187 void Scheduler::SetVisible(bool visible
) {
188 state_machine_
.SetVisible(visible
);
190 frame_source_
->SetActiveSource(primary_frame_source_
);
192 frame_source_
->SetActiveSource(background_frame_source_
);
194 ProcessScheduledActions();
197 void Scheduler::SetCanDraw(bool can_draw
) {
198 state_machine_
.SetCanDraw(can_draw
);
199 ProcessScheduledActions();
202 void Scheduler::NotifyReadyToActivate() {
203 state_machine_
.NotifyReadyToActivate();
204 ProcessScheduledActions();
207 void Scheduler::NotifyReadyToDraw() {
208 // Empty for now, until we take action based on the notification as part of
209 // crbugs 352894, 383157, 421923.
212 void Scheduler::SetNeedsCommit() {
213 state_machine_
.SetNeedsCommit();
214 ProcessScheduledActions();
217 void Scheduler::SetNeedsRedraw() {
218 state_machine_
.SetNeedsRedraw();
219 ProcessScheduledActions();
222 void Scheduler::SetNeedsAnimate() {
223 state_machine_
.SetNeedsAnimate();
224 ProcessScheduledActions();
227 void Scheduler::SetNeedsManageTiles() {
228 DCHECK(!IsInsideAction(SchedulerStateMachine::ACTION_MANAGE_TILES
));
229 state_machine_
.SetNeedsManageTiles();
230 ProcessScheduledActions();
233 void Scheduler::SetMaxSwapsPending(int max
) {
234 state_machine_
.SetMaxSwapsPending(max
);
237 void Scheduler::DidSwapBuffers() {
238 state_machine_
.DidSwapBuffers();
240 // There is no need to call ProcessScheduledActions here because
241 // swapping should not trigger any new actions.
242 if (!inside_process_scheduled_actions_
) {
243 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
247 void Scheduler::DidSwapBuffersComplete() {
248 state_machine_
.DidSwapBuffersComplete();
249 ProcessScheduledActions();
252 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
253 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
254 ProcessScheduledActions();
257 void Scheduler::NotifyReadyToCommit() {
258 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
259 state_machine_
.NotifyReadyToCommit();
260 ProcessScheduledActions();
263 void Scheduler::BeginMainFrameAborted(bool did_handle
) {
264 TRACE_EVENT0("cc", "Scheduler::BeginMainFrameAborted");
265 state_machine_
.BeginMainFrameAborted(did_handle
);
266 ProcessScheduledActions();
269 void Scheduler::DidManageTiles() {
270 state_machine_
.DidManageTiles();
273 void Scheduler::DidLoseOutputSurface() {
274 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
275 state_machine_
.DidLoseOutputSurface();
276 if (frame_source_
->NeedsBeginFrames())
277 frame_source_
->SetNeedsBeginFrames(false);
278 begin_retro_frame_args_
.clear();
279 ProcessScheduledActions();
282 void Scheduler::DidCreateAndInitializeOutputSurface() {
283 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
284 DCHECK(!frame_source_
->NeedsBeginFrames());
285 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
286 state_machine_
.DidCreateAndInitializeOutputSurface();
287 ProcessScheduledActions();
290 void Scheduler::NotifyBeginMainFrameStarted() {
291 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
292 state_machine_
.NotifyBeginMainFrameStarted();
295 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
296 if (!frame_source_
->NeedsBeginFrames() ||
297 begin_impl_frame_args_
.interval
<= base::TimeDelta())
298 return base::TimeTicks();
300 base::TimeTicks now
= Now();
301 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
302 begin_impl_frame_args_
.deadline
);
303 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
304 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
307 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
308 return begin_impl_frame_args_
.frame_time
;
311 void Scheduler::SetupNextBeginFrameIfNeeded() {
312 if (!task_runner_
.get())
315 bool needs_begin_frame
= state_machine_
.BeginFrameNeeded();
317 bool at_end_of_deadline
=
318 (state_machine_
.begin_impl_frame_state() ==
319 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
);
321 bool should_call_set_needs_begin_frame
=
322 // Always request the BeginFrame immediately if it wasn't needed before.
323 (needs_begin_frame
&& !frame_source_
->NeedsBeginFrames()) ||
324 // Only stop requesting BeginFrames after a deadline.
325 (!needs_begin_frame
&& frame_source_
->NeedsBeginFrames() &&
328 if (should_call_set_needs_begin_frame
) {
329 frame_source_
->SetNeedsBeginFrames(needs_begin_frame
);
332 if (at_end_of_deadline
) {
333 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
336 PostBeginRetroFrameIfNeeded();
337 SetupPollingMechanisms(needs_begin_frame
);
340 // We may need to poll when we can't rely on BeginFrame to advance certain
341 // state or to avoid deadlock.
342 void Scheduler::SetupPollingMechanisms(bool needs_begin_frame
) {
343 bool needs_advance_commit_state_timer
= false;
344 // Setup PollForAnticipatedDrawTriggers if we need to monitor state but
345 // aren't expecting any more BeginFrames. This should only be needed by
346 // the synchronous compositor when BeginFrameNeeded is false.
347 if (state_machine_
.ShouldPollForAnticipatedDrawTriggers()) {
348 DCHECK(!state_machine_
.SupportsProactiveBeginFrame());
349 DCHECK(!needs_begin_frame
);
350 if (poll_for_draw_triggers_task_
.IsCancelled()) {
351 poll_for_draw_triggers_task_
.Reset(poll_for_draw_triggers_closure_
);
352 base::TimeDelta delay
= begin_impl_frame_args_
.IsValid()
353 ? begin_impl_frame_args_
.interval
354 : BeginFrameArgs::DefaultInterval();
355 task_runner_
->PostDelayedTask(
356 FROM_HERE
, poll_for_draw_triggers_task_
.callback(), delay
);
359 poll_for_draw_triggers_task_
.Cancel();
361 // At this point we'd prefer to advance through the commit flow by
362 // drawing a frame, however it's possible that the frame rate controller
363 // will not give us a BeginFrame until the commit completes. See
364 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
365 // we set a repeating timer to poll on ProcessScheduledActions until we
366 // successfully reach BeginFrame. Synchronous compositor does not use
367 // frame rate controller or have the circular wait in the bug.
368 if (IsBeginMainFrameSentOrStarted() &&
369 !settings_
.using_synchronous_renderer_compositor
) {
370 needs_advance_commit_state_timer
= true;
374 if (needs_advance_commit_state_timer
) {
375 if (advance_commit_state_task_
.IsCancelled() &&
376 begin_impl_frame_args_
.IsValid()) {
377 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
378 // set the interval to twice the interval from the previous frame.
379 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
380 task_runner_
->PostDelayedTask(FROM_HERE
,
381 advance_commit_state_task_
.callback(),
382 begin_impl_frame_args_
.interval
* 2);
385 advance_commit_state_task_
.Cancel();
389 // BeginFrame is the mechanism that tells us that now is a good time to start
390 // making a frame. Usually this means that user input for the frame is complete.
391 // If the scheduler is busy, we queue the BeginFrame to be handled later as
392 // a BeginRetroFrame.
393 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
394 TRACE_EVENT1("cc", "Scheduler::BeginFrame", "args", args
.AsValue());
396 // Deliver BeginFrames to children.
397 if (settings_
.forward_begin_frames_to_children
&&
398 state_machine_
.children_need_begin_frames()) {
399 BeginFrameArgs
adjusted_args_for_children(args
);
400 // Adjust a deadline for child schedulers.
401 // TODO(simonhong): Once we have commitless update, we can get rid of
402 // BeginMainFrameToCommitDurationEstimate() +
403 // CommitToActivateDurationEstimate().
404 adjusted_args_for_children
.deadline
-=
405 (client_
->BeginMainFrameToCommitDurationEstimate() +
406 client_
->CommitToActivateDurationEstimate() +
407 client_
->DrawDurationEstimate() + EstimatedParentDrawTime());
408 client_
->SendBeginFramesToChildren(adjusted_args_for_children
);
411 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
412 // sent us the last BeginFrame we have missed. As we might not be able to
413 // actually make rendering for this call, handle it like a "retro frame".
414 // TODO(brainderson): Add a test for this functionality ASAP!
415 if (args
.type
== BeginFrameArgs::MISSED
) {
416 begin_retro_frame_args_
.push_back(args
);
417 PostBeginRetroFrameIfNeeded();
421 BeginFrameArgs
adjusted_args(args
);
422 adjusted_args
.deadline
-= EstimatedParentDrawTime();
424 bool should_defer_begin_frame
;
425 if (settings_
.using_synchronous_renderer_compositor
) {
426 should_defer_begin_frame
= false;
428 should_defer_begin_frame
=
429 !begin_retro_frame_args_
.empty() || begin_retro_frame_posted_
||
430 !frame_source_
->NeedsBeginFrames() ||
431 (state_machine_
.begin_impl_frame_state() !=
432 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
435 if (should_defer_begin_frame
) {
436 begin_retro_frame_args_
.push_back(adjusted_args
);
437 TRACE_EVENT_INSTANT0(
438 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
439 // Queuing the frame counts as "using it", so we need to return true.
441 BeginImplFrame(adjusted_args
);
446 void Scheduler::SetChildrenNeedBeginFrames(bool children_need_begin_frames
) {
447 DCHECK(settings_
.forward_begin_frames_to_children
);
448 state_machine_
.SetChildrenNeedBeginFrames(children_need_begin_frames
);
449 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
452 // BeginRetroFrame is called for BeginFrames that we've deferred because
453 // the scheduler was in the middle of processing a previous BeginFrame.
454 void Scheduler::BeginRetroFrame() {
455 TRACE_EVENT0("cc", "Scheduler::BeginRetroFrame");
456 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
457 DCHECK(begin_retro_frame_posted_
);
458 begin_retro_frame_posted_
= false;
460 // If there aren't any retroactive BeginFrames, then we've lost the
461 // OutputSurface and should abort.
462 if (begin_retro_frame_args_
.empty())
465 // Discard expired BeginRetroFrames
466 // Today, we should always end up with at most one un-expired BeginRetroFrame
467 // because deadlines will not be greater than the next frame time. We don't
468 // DCHECK though because some systems don't always have monotonic timestamps.
469 // TODO(brianderson): In the future, long deadlines could result in us not
470 // draining the queue if we don't catch up. If we consistently can't catch
471 // up, our fallback should be to lower our frame rate.
472 base::TimeTicks now
= Now();
474 while (!begin_retro_frame_args_
.empty()) {
475 const BeginFrameArgs
& args
= begin_retro_frame_args_
.front();
476 base::TimeTicks expiration_time
= args
.frame_time
+ args
.interval
;
477 if (now
<= expiration_time
)
479 TRACE_EVENT_INSTANT2(
480 "cc", "Scheduler::BeginRetroFrame discarding", TRACE_EVENT_SCOPE_THREAD
,
481 "expiration_time - now", (expiration_time
- now
).InMillisecondsF(),
482 "BeginFrameArgs", begin_retro_frame_args_
.front().AsValue());
483 begin_retro_frame_args_
.pop_front();
484 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
487 if (begin_retro_frame_args_
.empty()) {
488 TRACE_EVENT_INSTANT0("cc",
489 "Scheduler::BeginRetroFrames all expired",
490 TRACE_EVENT_SCOPE_THREAD
);
492 BeginFrameArgs front
= begin_retro_frame_args_
.front();
493 begin_retro_frame_args_
.pop_front();
494 BeginImplFrame(front
);
498 // There could be a race between the posted BeginRetroFrame and a new
499 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
500 // will check if there is a pending BeginRetroFrame to ensure we handle
501 // BeginFrames in FIFO order.
502 void Scheduler::PostBeginRetroFrameIfNeeded() {
503 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
504 "Scheduler::PostBeginRetroFrameIfNeeded",
507 if (!frame_source_
->NeedsBeginFrames())
510 if (begin_retro_frame_args_
.empty() || begin_retro_frame_posted_
)
513 // begin_retro_frame_args_ should always be empty for the
514 // synchronous compositor.
515 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
517 if (state_machine_
.begin_impl_frame_state() !=
518 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
521 begin_retro_frame_posted_
= true;
522 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_closure_
);
525 // BeginImplFrame starts a compositor frame that will wait up until a deadline
526 // for a BeginMainFrame+activation to complete before it times out and draws
527 // any asynchronous animation and scroll/pinch updates.
528 void Scheduler::BeginImplFrame(const BeginFrameArgs
& args
) {
529 bool main_thread_is_in_high_latency_mode
=
530 state_machine_
.MainThreadIsInHighLatencyMode();
532 "Scheduler::BeginImplFrame",
535 "main_thread_is_high_latency",
536 main_thread_is_in_high_latency_mode
);
537 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
539 main_thread_is_in_high_latency_mode
);
540 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
541 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
542 DCHECK(state_machine_
.HasInitializedOutputSurface());
544 advance_commit_state_task_
.Cancel();
546 begin_impl_frame_args_
= args
;
547 begin_impl_frame_args_
.deadline
-= client_
->DrawDurationEstimate();
549 if (!state_machine_
.impl_latency_takes_priority() &&
550 main_thread_is_in_high_latency_mode
&&
551 CanCommitAndActivateBeforeDeadline()) {
552 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
555 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
556 state_machine_
.OnBeginImplFrame(begin_impl_frame_args_
);
557 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
559 ProcessScheduledActions();
561 state_machine_
.OnBeginImplFrameDeadlinePending();
563 if (settings_
.using_synchronous_renderer_compositor
) {
564 // The synchronous renderer compositor has to make its GL calls
566 // TODO(brianderson): Have the OutputSurface initiate the deadline tasks
567 // so the synchronous renderer compositor can take advantage of splitting
568 // up the BeginImplFrame and deadline as well.
569 OnBeginImplFrameDeadline();
571 ScheduleBeginImplFrameDeadline();
575 void Scheduler::ScheduleBeginImplFrameDeadline() {
576 // The synchronous compositor does not post a deadline task.
577 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
579 begin_impl_frame_deadline_task_
.Cancel();
580 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
582 begin_impl_frame_deadline_mode_
=
583 state_machine_
.CurrentBeginImplFrameDeadlineMode();
585 base::TimeTicks deadline
;
586 switch (begin_impl_frame_deadline_mode_
) {
587 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
588 // We are ready to draw a new active tree immediately.
589 // We don't use Now() here because it's somewhat expensive to call.
590 deadline
= base::TimeTicks();
592 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
593 // We are animating on the impl thread but we can wait for some time.
594 deadline
= begin_impl_frame_args_
.deadline
;
596 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
597 // We are blocked for one reason or another and we should wait.
598 // TODO(brianderson): Handle long deadlines (that are past the next
599 // frame's frame time) properly instead of using this hack.
601 begin_impl_frame_args_
.frame_time
+ begin_impl_frame_args_
.interval
;
606 "cc", "Scheduler::ScheduleBeginImplFrameDeadline", "deadline", deadline
);
608 base::TimeDelta delta
= deadline
- Now();
609 if (delta
<= base::TimeDelta())
610 delta
= base::TimeDelta();
611 task_runner_
->PostDelayedTask(
612 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
615 void Scheduler::RescheduleBeginImplFrameDeadlineIfNeeded() {
616 if (settings_
.using_synchronous_renderer_compositor
)
619 if (state_machine_
.begin_impl_frame_state() !=
620 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
623 if (begin_impl_frame_deadline_mode_
!=
624 state_machine_
.CurrentBeginImplFrameDeadlineMode())
625 ScheduleBeginImplFrameDeadline();
628 void Scheduler::OnBeginImplFrameDeadline() {
629 TRACE_EVENT0("cc", "Scheduler::OnBeginImplFrameDeadline");
630 begin_impl_frame_deadline_task_
.Cancel();
631 // We split the deadline actions up into two phases so the state machine
632 // has a chance to trigger actions that should occur durring and after
633 // the deadline separately. For example:
634 // * Sending the BeginMainFrame will not occur after the deadline in
635 // order to wait for more user-input before starting the next commit.
636 // * Creating a new OuputSurface will not occur during the deadline in
637 // order to allow the state machine to "settle" first.
638 state_machine_
.OnBeginImplFrameDeadline();
639 ProcessScheduledActions();
640 state_machine_
.OnBeginImplFrameIdle();
641 ProcessScheduledActions();
643 client_
->DidBeginImplFrameDeadline();
646 void Scheduler::PollForAnticipatedDrawTriggers() {
647 TRACE_EVENT0("cc", "Scheduler::PollForAnticipatedDrawTriggers");
648 poll_for_draw_triggers_task_
.Cancel();
649 state_machine_
.DidEnterPollForAnticipatedDrawTriggers();
650 ProcessScheduledActions();
651 state_machine_
.DidLeavePollForAnticipatedDrawTriggers();
654 void Scheduler::PollToAdvanceCommitState() {
655 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
656 advance_commit_state_task_
.Cancel();
657 ProcessScheduledActions();
660 void Scheduler::DrawAndSwapIfPossible() {
661 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
662 state_machine_
.DidDrawIfPossibleCompleted(result
);
665 void Scheduler::ProcessScheduledActions() {
666 // We do not allow ProcessScheduledActions to be recursive.
667 // The top-level call will iteratively execute the next action for us anyway.
668 if (inside_process_scheduled_actions_
)
671 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
673 SchedulerStateMachine::Action action
;
675 action
= state_machine_
.NextAction();
676 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
677 "SchedulerStateMachine",
680 VLOG(2) << "Scheduler::ProcessScheduledActions: "
681 << SchedulerStateMachine::ActionToString(action
) << " "
682 << state_machine_
.GetStatesForDebugging();
683 state_machine_
.UpdateState(action
);
684 base::AutoReset
<SchedulerStateMachine::Action
>
685 mark_inside_action(&inside_action_
, action
);
687 case SchedulerStateMachine::ACTION_NONE
:
689 case SchedulerStateMachine::ACTION_ANIMATE
:
690 client_
->ScheduledActionAnimate();
692 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
:
693 client_
->ScheduledActionSendBeginMainFrame();
695 case SchedulerStateMachine::ACTION_COMMIT
:
696 client_
->ScheduledActionCommit();
698 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
:
699 client_
->ScheduledActionActivateSyncTree();
701 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
702 DrawAndSwapIfPossible();
704 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
:
705 client_
->ScheduledActionDrawAndSwapForced();
707 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
708 // No action is actually performed, but this allows the state machine to
709 // advance out of its waiting to draw state without actually drawing.
711 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
712 client_
->ScheduledActionBeginOutputSurfaceCreation();
714 case SchedulerStateMachine::ACTION_MANAGE_TILES
:
715 client_
->ScheduledActionManageTiles();
718 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
720 SetupNextBeginFrameIfNeeded();
721 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
723 RescheduleBeginImplFrameDeadlineIfNeeded();
726 bool Scheduler::WillDrawIfNeeded() const {
727 return !state_machine_
.PendingDrawsShouldBeAborted();
730 scoped_refptr
<base::debug::ConvertableToTraceFormat
> Scheduler::AsValue()
732 scoped_refptr
<base::debug::TracedValue
> state
=
733 new base::debug::TracedValue();
734 AsValueInto(state
.get());
738 void Scheduler::AsValueInto(base::debug::TracedValue
* state
) const {
739 state
->BeginDictionary("state_machine");
740 state_machine_
.AsValueInto(state
, Now());
741 state
->EndDictionary();
743 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
744 bool frame_tracing_enabled
= false;
745 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
746 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
747 &frame_tracing_enabled
);
748 if (frame_tracing_enabled
) {
749 state
->BeginDictionary("frame_source_");
750 frame_source_
->AsValueInto(state
);
751 state
->EndDictionary();
754 state
->BeginDictionary("scheduler_state");
755 state
->SetDouble("time_until_anticipated_draw_time_ms",
756 (AnticipatedDrawTime() - Now()).InMillisecondsF());
757 state
->SetDouble("estimated_parent_draw_time_ms",
758 estimated_parent_draw_time_
.InMillisecondsF());
759 state
->SetBoolean("last_set_needs_begin_frame_",
760 frame_source_
->NeedsBeginFrames());
761 state
->SetBoolean("begin_retro_frame_posted_", begin_retro_frame_posted_
);
762 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
763 state
->SetBoolean("begin_impl_frame_deadline_task_",
764 !begin_impl_frame_deadline_task_
.IsCancelled());
765 state
->SetBoolean("poll_for_draw_triggers_task_",
766 !poll_for_draw_triggers_task_
.IsCancelled());
767 state
->SetBoolean("advance_commit_state_task_",
768 !advance_commit_state_task_
.IsCancelled());
769 state
->BeginDictionary("begin_impl_frame_args");
770 begin_impl_frame_args_
.AsValueInto(state
);
771 state
->EndDictionary();
773 state
->EndDictionary();
775 state
->BeginDictionary("client_state");
776 state
->SetDouble("draw_duration_estimate_ms",
777 client_
->DrawDurationEstimate().InMillisecondsF());
779 "begin_main_frame_to_commit_duration_estimate_ms",
780 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
782 "commit_to_activate_duration_estimate_ms",
783 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
784 state
->EndDictionary();
787 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
788 // Check if the main thread computation and commit can be finished before the
789 // impl thread's deadline.
790 base::TimeTicks estimated_draw_time
=
791 begin_impl_frame_args_
.frame_time
+
792 client_
->BeginMainFrameToCommitDurationEstimate() +
793 client_
->CommitToActivateDurationEstimate();
796 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
797 "CanCommitAndActivateBeforeDeadline",
798 "time_left_after_drawing_ms",
799 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
803 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
806 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
807 return (state_machine_
.commit_state() ==
808 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
809 state_machine_
.commit_state() ==
810 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);