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"
8 #include "base/auto_reset.h"
9 #include "base/debug/trace_event.h"
10 #include "base/debug/trace_event_argument.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "cc/debug/devtools_instrumentation.h"
14 #include "cc/debug/traced_value.h"
15 #include "cc/scheduler/delay_based_time_source.h"
16 #include "ui/gfx/frame_time.h"
20 BeginFrameSource
* SchedulerFrameSourcesConstructor::ConstructPrimaryFrameSource(
21 Scheduler
* scheduler
) {
22 if (!scheduler
->settings_
.throttle_frame_production
) {
24 "Scheduler::Scheduler()",
26 "BackToBackBeginFrameSource");
27 DCHECK(!scheduler
->primary_frame_source_internal_
);
28 scheduler
->primary_frame_source_internal_
=
29 BackToBackBeginFrameSource::Create(scheduler
->task_runner_
.get());
30 return scheduler
->primary_frame_source_internal_
.get();
31 } else if (scheduler
->settings_
.begin_frame_scheduling_enabled
) {
33 "Scheduler::Scheduler()",
36 return scheduler
->client_
->ExternalBeginFrameSource();
39 "Scheduler::Scheduler()",
41 "SyntheticBeginFrameSource");
42 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_source
=
43 SyntheticBeginFrameSource::Create(scheduler
->task_runner_
.get(),
45 BeginFrameArgs::DefaultInterval());
47 DCHECK(!scheduler
->vsync_observer_
);
48 scheduler
->vsync_observer_
= synthetic_source
.get();
50 DCHECK(!scheduler
->primary_frame_source_internal_
);
51 scheduler
->primary_frame_source_internal_
= synthetic_source
.Pass();
52 return scheduler
->primary_frame_source_internal_
.get();
57 SchedulerFrameSourcesConstructor::ConstructBackgroundFrameSource(
58 Scheduler
* scheduler
) {
60 "Scheduler::Scheduler()",
61 "BackgroundFrameSource",
62 "SyntheticBeginFrameSource");
63 DCHECK(!(scheduler
->background_frame_source_internal_
));
64 scheduler
->background_frame_source_internal_
=
65 SyntheticBeginFrameSource::Create(scheduler
->task_runner_
.get(),
67 base::TimeDelta::FromSeconds(1));
68 return scheduler
->background_frame_source_internal_
.get();
72 SchedulerClient
* client
,
73 const SchedulerSettings
& scheduler_settings
,
74 int layer_tree_host_id
,
75 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
76 base::PowerMonitor
* power_monitor
,
77 SchedulerFrameSourcesConstructor
* frame_sources_constructor
)
79 primary_frame_source_(NULL
),
80 background_frame_source_(NULL
),
81 primary_frame_source_internal_(),
82 background_frame_source_internal_(),
83 vsync_observer_(NULL
),
84 settings_(scheduler_settings
),
86 layer_tree_host_id_(layer_tree_host_id
),
87 task_runner_(task_runner
),
88 power_monitor_(power_monitor
),
89 begin_retro_frame_posted_(false),
90 state_machine_(scheduler_settings
),
91 inside_process_scheduled_actions_(false),
92 inside_action_(SchedulerStateMachine::ACTION_NONE
),
94 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
95 "Scheduler::Scheduler",
99 DCHECK(!state_machine_
.BeginFrameNeeded());
101 begin_retro_frame_closure_
=
102 base::Bind(&Scheduler::BeginRetroFrame
, weak_factory_
.GetWeakPtr());
103 begin_impl_frame_deadline_closure_
= base::Bind(
104 &Scheduler::OnBeginImplFrameDeadline
, weak_factory_
.GetWeakPtr());
105 poll_for_draw_triggers_closure_
= base::Bind(
106 &Scheduler::PollForAnticipatedDrawTriggers
, weak_factory_
.GetWeakPtr());
107 advance_commit_state_closure_
= base::Bind(
108 &Scheduler::PollToAdvanceCommitState
, weak_factory_
.GetWeakPtr());
110 frame_source_
= BeginFrameSourceMultiplexer::Create();
111 frame_source_
->AddObserver(this);
113 // Primary frame source
114 primary_frame_source_
=
115 frame_sources_constructor
->ConstructPrimaryFrameSource(this);
116 frame_source_
->AddSource(primary_frame_source_
);
118 // Background ticking frame source
119 background_frame_source_
=
120 frame_sources_constructor
->ConstructBackgroundFrameSource(this);
121 frame_source_
->AddSource(background_frame_source_
);
123 SetupPowerMonitoring();
126 Scheduler::~Scheduler() {
127 TeardownPowerMonitoring();
130 base::TimeTicks
Scheduler::Now() const {
131 base::TimeTicks now
= gfx::FrameTime::Now();
132 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"),
139 void Scheduler::SetupPowerMonitoring() {
140 if (settings_
.disable_hi_res_timer_tasks_on_battery
) {
141 DCHECK(power_monitor_
);
142 power_monitor_
->AddObserver(this);
143 state_machine_
.SetImplLatencyTakesPriorityOnBattery(
144 power_monitor_
->IsOnBatteryPower());
148 void Scheduler::TeardownPowerMonitoring() {
149 if (settings_
.disable_hi_res_timer_tasks_on_battery
) {
150 DCHECK(power_monitor_
);
151 power_monitor_
->RemoveObserver(this);
155 void Scheduler::OnPowerStateChange(bool on_battery_power
) {
156 DCHECK(settings_
.disable_hi_res_timer_tasks_on_battery
);
157 state_machine_
.SetImplLatencyTakesPriorityOnBattery(on_battery_power
);
160 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase
,
161 base::TimeDelta interval
) {
162 // TODO(brianderson): We should not be receiving 0 intervals.
163 if (interval
== base::TimeDelta())
164 interval
= BeginFrameArgs::DefaultInterval();
167 vsync_observer_
->OnUpdateVSyncParameters(timebase
, interval
);
170 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time
) {
171 DCHECK_GE(draw_time
.ToInternalValue(), 0);
172 estimated_parent_draw_time_
= draw_time
;
175 void Scheduler::SetCanStart() {
176 state_machine_
.SetCanStart();
177 ProcessScheduledActions();
180 void Scheduler::SetVisible(bool visible
) {
181 state_machine_
.SetVisible(visible
);
183 frame_source_
->SetActiveSource(primary_frame_source_
);
185 frame_source_
->SetActiveSource(background_frame_source_
);
187 ProcessScheduledActions();
190 void Scheduler::SetCanDraw(bool can_draw
) {
191 state_machine_
.SetCanDraw(can_draw
);
192 ProcessScheduledActions();
195 void Scheduler::NotifyReadyToActivate() {
196 state_machine_
.NotifyReadyToActivate();
197 ProcessScheduledActions();
200 void Scheduler::SetNeedsCommit() {
201 state_machine_
.SetNeedsCommit();
202 ProcessScheduledActions();
205 void Scheduler::SetNeedsRedraw() {
206 state_machine_
.SetNeedsRedraw();
207 ProcessScheduledActions();
210 void Scheduler::SetNeedsAnimate() {
211 state_machine_
.SetNeedsAnimate();
212 ProcessScheduledActions();
215 void Scheduler::SetNeedsManageTiles() {
216 DCHECK(!IsInsideAction(SchedulerStateMachine::ACTION_MANAGE_TILES
));
217 state_machine_
.SetNeedsManageTiles();
218 ProcessScheduledActions();
221 void Scheduler::SetMaxSwapsPending(int max
) {
222 state_machine_
.SetMaxSwapsPending(max
);
225 void Scheduler::DidSwapBuffers() {
226 state_machine_
.DidSwapBuffers();
228 // There is no need to call ProcessScheduledActions here because
229 // swapping should not trigger any new actions.
230 if (!inside_process_scheduled_actions_
) {
231 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
235 void Scheduler::SetSwapUsedIncompleteTile(bool used_incomplete_tile
) {
236 state_machine_
.SetSwapUsedIncompleteTile(used_incomplete_tile
);
237 ProcessScheduledActions();
240 void Scheduler::DidSwapBuffersComplete() {
241 state_machine_
.DidSwapBuffersComplete();
242 ProcessScheduledActions();
245 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
246 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
247 ProcessScheduledActions();
250 void Scheduler::NotifyReadyToCommit() {
251 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
252 state_machine_
.NotifyReadyToCommit();
253 ProcessScheduledActions();
256 void Scheduler::BeginMainFrameAborted(bool did_handle
) {
257 TRACE_EVENT0("cc", "Scheduler::BeginMainFrameAborted");
258 state_machine_
.BeginMainFrameAborted(did_handle
);
259 ProcessScheduledActions();
262 void Scheduler::DidManageTiles() {
263 state_machine_
.DidManageTiles();
266 void Scheduler::DidLoseOutputSurface() {
267 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
268 state_machine_
.DidLoseOutputSurface();
269 if (frame_source_
->NeedsBeginFrames())
270 frame_source_
->SetNeedsBeginFrames(false);
271 begin_retro_frame_args_
.clear();
272 ProcessScheduledActions();
275 void Scheduler::DidCreateAndInitializeOutputSurface() {
276 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
277 DCHECK(!frame_source_
->NeedsBeginFrames());
278 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
279 state_machine_
.DidCreateAndInitializeOutputSurface();
280 ProcessScheduledActions();
283 void Scheduler::NotifyBeginMainFrameStarted() {
284 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
285 state_machine_
.NotifyBeginMainFrameStarted();
288 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
289 if (!frame_source_
->NeedsBeginFrames() ||
290 begin_impl_frame_args_
.interval
<= base::TimeDelta())
291 return base::TimeTicks();
293 base::TimeTicks now
= Now();
294 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
295 begin_impl_frame_args_
.deadline
);
296 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
297 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
300 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
301 return begin_impl_frame_args_
.frame_time
;
304 void Scheduler::SetupNextBeginFrameIfNeeded() {
305 if (!task_runner_
.get())
308 bool needs_begin_frame
= state_machine_
.BeginFrameNeeded();
310 bool at_end_of_deadline
=
311 (state_machine_
.begin_impl_frame_state() ==
312 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
);
314 bool should_call_set_needs_begin_frame
=
315 // Always request the BeginFrame immediately if it wasn't needed before.
316 (needs_begin_frame
&& !frame_source_
->NeedsBeginFrames()) ||
317 // Only stop requesting BeginFrames after a deadline.
318 (!needs_begin_frame
&& frame_source_
->NeedsBeginFrames() &&
321 if (should_call_set_needs_begin_frame
) {
322 frame_source_
->SetNeedsBeginFrames(needs_begin_frame
);
325 if (at_end_of_deadline
) {
326 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
329 PostBeginRetroFrameIfNeeded();
330 SetupPollingMechanisms(needs_begin_frame
);
333 // We may need to poll when we can't rely on BeginFrame to advance certain
334 // state or to avoid deadlock.
335 void Scheduler::SetupPollingMechanisms(bool needs_begin_frame
) {
336 bool needs_advance_commit_state_timer
= false;
337 // Setup PollForAnticipatedDrawTriggers if we need to monitor state but
338 // aren't expecting any more BeginFrames. This should only be needed by
339 // the synchronous compositor when BeginFrameNeeded is false.
340 if (state_machine_
.ShouldPollForAnticipatedDrawTriggers()) {
341 DCHECK(!state_machine_
.SupportsProactiveBeginFrame());
342 DCHECK(!needs_begin_frame
);
343 if (poll_for_draw_triggers_task_
.IsCancelled()) {
344 poll_for_draw_triggers_task_
.Reset(poll_for_draw_triggers_closure_
);
345 base::TimeDelta delay
= begin_impl_frame_args_
.IsValid()
346 ? begin_impl_frame_args_
.interval
347 : BeginFrameArgs::DefaultInterval();
348 task_runner_
->PostDelayedTask(
349 FROM_HERE
, poll_for_draw_triggers_task_
.callback(), delay
);
352 poll_for_draw_triggers_task_
.Cancel();
354 // At this point we'd prefer to advance through the commit flow by
355 // drawing a frame, however it's possible that the frame rate controller
356 // will not give us a BeginFrame until the commit completes. See
357 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
358 // we set a repeating timer to poll on ProcessScheduledActions until we
359 // successfully reach BeginFrame. Synchronous compositor does not use
360 // frame rate controller or have the circular wait in the bug.
361 if (IsBeginMainFrameSentOrStarted() &&
362 !settings_
.using_synchronous_renderer_compositor
) {
363 needs_advance_commit_state_timer
= true;
367 if (needs_advance_commit_state_timer
) {
368 if (advance_commit_state_task_
.IsCancelled() &&
369 begin_impl_frame_args_
.IsValid()) {
370 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
371 // set the interval to twice the interval from the previous frame.
372 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
373 task_runner_
->PostDelayedTask(FROM_HERE
,
374 advance_commit_state_task_
.callback(),
375 begin_impl_frame_args_
.interval
* 2);
378 advance_commit_state_task_
.Cancel();
382 // BeginFrame is the mechanism that tells us that now is a good time to start
383 // making a frame. Usually this means that user input for the frame is complete.
384 // If the scheduler is busy, we queue the BeginFrame to be handled later as
385 // a BeginRetroFrame.
386 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
387 TRACE_EVENT1("cc", "Scheduler::BeginFrame", "args", args
.AsValue());
389 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
390 // sent us the last BeginFrame we have missed. As we might not be able to
391 // actually make rendering for this call, handle it like a "retro frame".
392 // TODO(brainderson): Add a test for this functionality ASAP!
393 if (args
.type
== BeginFrameArgs::MISSED
) {
394 begin_retro_frame_args_
.push_back(args
);
395 PostBeginRetroFrameIfNeeded();
399 BeginFrameArgs
adjusted_args(args
);
400 adjusted_args
.deadline
-= EstimatedParentDrawTime();
402 bool should_defer_begin_frame
;
403 if (settings_
.using_synchronous_renderer_compositor
) {
404 should_defer_begin_frame
= false;
406 should_defer_begin_frame
=
407 !begin_retro_frame_args_
.empty() || begin_retro_frame_posted_
||
408 !frame_source_
->NeedsBeginFrames() ||
409 (state_machine_
.begin_impl_frame_state() !=
410 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
413 if (should_defer_begin_frame
) {
414 begin_retro_frame_args_
.push_back(adjusted_args
);
415 TRACE_EVENT_INSTANT0(
416 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
417 // Queuing the frame counts as "using it", so we need to return true.
419 BeginImplFrame(adjusted_args
);
424 // BeginRetroFrame is called for BeginFrames that we've deferred because
425 // the scheduler was in the middle of processing a previous BeginFrame.
426 void Scheduler::BeginRetroFrame() {
427 TRACE_EVENT0("cc", "Scheduler::BeginRetroFrame");
428 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
429 DCHECK(begin_retro_frame_posted_
);
430 begin_retro_frame_posted_
= false;
432 // If there aren't any retroactive BeginFrames, then we've lost the
433 // OutputSurface and should abort.
434 if (begin_retro_frame_args_
.empty())
437 // Discard expired BeginRetroFrames
438 // Today, we should always end up with at most one un-expired BeginRetroFrame
439 // because deadlines will not be greater than the next frame time. We don't
440 // DCHECK though because some systems don't always have monotonic timestamps.
441 // TODO(brianderson): In the future, long deadlines could result in us not
442 // draining the queue if we don't catch up. If we consistently can't catch
443 // up, our fallback should be to lower our frame rate.
444 base::TimeTicks now
= Now();
445 base::TimeDelta draw_duration_estimate
= client_
->DrawDurationEstimate();
446 while (!begin_retro_frame_args_
.empty()) {
447 base::TimeTicks adjusted_deadline
= AdjustedBeginImplFrameDeadline(
448 begin_retro_frame_args_
.front(), draw_duration_estimate
);
449 if (now
<= adjusted_deadline
)
452 TRACE_EVENT_INSTANT2("cc",
453 "Scheduler::BeginRetroFrame discarding",
454 TRACE_EVENT_SCOPE_THREAD
,
456 (adjusted_deadline
- now
).InMicroseconds(),
458 begin_retro_frame_args_
.front().AsValue());
459 begin_retro_frame_args_
.pop_front();
460 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
463 if (begin_retro_frame_args_
.empty()) {
464 TRACE_EVENT_INSTANT0("cc",
465 "Scheduler::BeginRetroFrames all expired",
466 TRACE_EVENT_SCOPE_THREAD
);
468 BeginImplFrame(begin_retro_frame_args_
.front());
469 begin_retro_frame_args_
.pop_front();
473 // There could be a race between the posted BeginRetroFrame and a new
474 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
475 // will check if there is a pending BeginRetroFrame to ensure we handle
476 // BeginFrames in FIFO order.
477 void Scheduler::PostBeginRetroFrameIfNeeded() {
478 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
479 "Scheduler::PostBeginRetroFrameIfNeeded",
482 if (!frame_source_
->NeedsBeginFrames())
485 if (begin_retro_frame_args_
.empty() || begin_retro_frame_posted_
)
488 // begin_retro_frame_args_ should always be empty for the
489 // synchronous compositor.
490 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
492 if (state_machine_
.begin_impl_frame_state() !=
493 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
496 begin_retro_frame_posted_
= true;
497 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_closure_
);
500 // BeginImplFrame starts a compositor frame that will wait up until a deadline
501 // for a BeginMainFrame+activation to complete before it times out and draws
502 // any asynchronous animation and scroll/pinch updates.
503 void Scheduler::BeginImplFrame(const BeginFrameArgs
& args
) {
504 bool main_thread_is_in_high_latency_mode
=
505 state_machine_
.MainThreadIsInHighLatencyMode();
507 "Scheduler::BeginImplFrame",
510 "main_thread_is_high_latency",
511 main_thread_is_in_high_latency_mode
);
512 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
514 main_thread_is_in_high_latency_mode
);
515 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
516 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
517 DCHECK(state_machine_
.HasInitializedOutputSurface());
519 advance_commit_state_task_
.Cancel();
521 base::TimeDelta draw_duration_estimate
= client_
->DrawDurationEstimate();
522 begin_impl_frame_args_
= args
;
523 begin_impl_frame_args_
.deadline
-= draw_duration_estimate
;
525 if (!state_machine_
.impl_latency_takes_priority() &&
526 main_thread_is_in_high_latency_mode
&&
527 CanCommitAndActivateBeforeDeadline()) {
528 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
531 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
532 state_machine_
.OnBeginImplFrame(begin_impl_frame_args_
);
533 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
535 ProcessScheduledActions();
537 state_machine_
.OnBeginImplFrameDeadlinePending();
538 ScheduleBeginImplFrameDeadline(
539 AdjustedBeginImplFrameDeadline(args
, draw_duration_estimate
));
542 base::TimeTicks
Scheduler::AdjustedBeginImplFrameDeadline(
543 const BeginFrameArgs
& args
,
544 base::TimeDelta draw_duration_estimate
) const {
545 if (settings_
.using_synchronous_renderer_compositor
) {
546 // The synchronous compositor needs to draw right away.
547 return base::TimeTicks();
548 } else if (state_machine_
.ShouldTriggerBeginImplFrameDeadlineEarly()) {
549 // We are ready to draw a new active tree immediately.
550 return base::TimeTicks();
551 } else if (state_machine_
.needs_redraw()) {
552 // We have an animation or fast input path on the impl thread that wants
553 // to draw, so don't wait too long for a new active tree.
554 return args
.deadline
- draw_duration_estimate
;
556 // The impl thread doesn't have anything it wants to draw and we are just
557 // waiting for a new active tree, so post the deadline for the next
558 // expected BeginImplFrame start. This allows us to draw immediately when
559 // there is a new active tree, instead of waiting for the next
561 // TODO(brianderson): Handle long deadlines (that are past the next frame's
562 // frame time) properly instead of using this hack.
563 return args
.frame_time
+ args
.interval
;
567 void Scheduler::ScheduleBeginImplFrameDeadline(base::TimeTicks deadline
) {
569 "cc", "Scheduler::ScheduleBeginImplFrameDeadline", "deadline", deadline
);
570 if (settings_
.using_synchronous_renderer_compositor
) {
571 // The synchronous renderer compositor has to make its GL calls
573 // TODO(brianderson): Have the OutputSurface initiate the deadline tasks
574 // so the sychronous renderer compositor can take advantage of splitting
575 // up the BeginImplFrame and deadline as well.
576 OnBeginImplFrameDeadline();
579 begin_impl_frame_deadline_task_
.Cancel();
580 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
582 base::TimeDelta delta
= deadline
- Now();
583 if (delta
<= base::TimeDelta())
584 delta
= base::TimeDelta();
585 task_runner_
->PostDelayedTask(
586 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
589 void Scheduler::OnBeginImplFrameDeadline() {
590 TRACE_EVENT0("cc", "Scheduler::OnBeginImplFrameDeadline");
591 begin_impl_frame_deadline_task_
.Cancel();
593 // We split the deadline actions up into two phases so the state machine
594 // has a chance to trigger actions that should occur durring and after
595 // the deadline separately. For example:
596 // * Sending the BeginMainFrame will not occur after the deadline in
597 // order to wait for more user-input before starting the next commit.
598 // * Creating a new OuputSurface will not occur during the deadline in
599 // order to allow the state machine to "settle" first.
600 state_machine_
.OnBeginImplFrameDeadline();
601 ProcessScheduledActions();
602 state_machine_
.OnBeginImplFrameIdle();
603 ProcessScheduledActions();
605 client_
->DidBeginImplFrameDeadline();
608 void Scheduler::PollForAnticipatedDrawTriggers() {
609 TRACE_EVENT0("cc", "Scheduler::PollForAnticipatedDrawTriggers");
610 poll_for_draw_triggers_task_
.Cancel();
611 state_machine_
.DidEnterPollForAnticipatedDrawTriggers();
612 ProcessScheduledActions();
613 state_machine_
.DidLeavePollForAnticipatedDrawTriggers();
616 void Scheduler::PollToAdvanceCommitState() {
617 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
618 advance_commit_state_task_
.Cancel();
619 ProcessScheduledActions();
622 void Scheduler::DrawAndSwapIfPossible() {
623 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
624 state_machine_
.DidDrawIfPossibleCompleted(result
);
627 void Scheduler::ProcessScheduledActions() {
628 // We do not allow ProcessScheduledActions to be recursive.
629 // The top-level call will iteratively execute the next action for us anyway.
630 if (inside_process_scheduled_actions_
)
633 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
635 SchedulerStateMachine::Action action
;
637 action
= state_machine_
.NextAction();
638 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
639 "SchedulerStateMachine",
642 VLOG(2) << "Scheduler::ProcessScheduledActions: "
643 << SchedulerStateMachine::ActionToString(action
) << " "
644 << state_machine_
.GetStatesForDebugging();
645 state_machine_
.UpdateState(action
);
646 base::AutoReset
<SchedulerStateMachine::Action
>
647 mark_inside_action(&inside_action_
, action
);
649 case SchedulerStateMachine::ACTION_NONE
:
651 case SchedulerStateMachine::ACTION_ANIMATE
:
652 client_
->ScheduledActionAnimate();
654 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
:
655 client_
->ScheduledActionSendBeginMainFrame();
657 case SchedulerStateMachine::ACTION_COMMIT
:
658 client_
->ScheduledActionCommit();
660 case SchedulerStateMachine::ACTION_UPDATE_VISIBLE_TILES
:
661 client_
->ScheduledActionUpdateVisibleTiles();
663 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
:
664 client_
->ScheduledActionActivateSyncTree();
666 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
667 DrawAndSwapIfPossible();
669 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
:
670 client_
->ScheduledActionDrawAndSwapForced();
672 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
673 // No action is actually performed, but this allows the state machine to
674 // advance out of its waiting to draw state without actually drawing.
676 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
677 client_
->ScheduledActionBeginOutputSurfaceCreation();
679 case SchedulerStateMachine::ACTION_MANAGE_TILES
:
680 client_
->ScheduledActionManageTiles();
683 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
685 SetupNextBeginFrameIfNeeded();
686 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
688 if (state_machine_
.ShouldTriggerBeginImplFrameDeadlineEarly()) {
689 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
690 ScheduleBeginImplFrameDeadline(base::TimeTicks());
694 bool Scheduler::WillDrawIfNeeded() const {
695 return !state_machine_
.PendingDrawsShouldBeAborted();
698 scoped_refptr
<base::debug::ConvertableToTraceFormat
> Scheduler::AsValue()
700 scoped_refptr
<base::debug::TracedValue
> state
=
701 new base::debug::TracedValue();
702 AsValueInto(state
.get());
706 void Scheduler::AsValueInto(base::debug::TracedValue
* state
) const {
707 state
->BeginDictionary("state_machine");
708 state_machine_
.AsValueInto(state
, Now());
709 state
->EndDictionary();
711 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
712 bool frame_tracing_enabled
= false;
713 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
714 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
715 &frame_tracing_enabled
);
716 if (frame_tracing_enabled
) {
717 state
->BeginDictionary("frame_source_");
718 frame_source_
->AsValueInto(state
);
719 state
->EndDictionary();
722 state
->BeginDictionary("scheduler_state");
723 state
->SetDouble("time_until_anticipated_draw_time_ms",
724 (AnticipatedDrawTime() - Now()).InMillisecondsF());
725 state
->SetDouble("estimated_parent_draw_time_ms",
726 estimated_parent_draw_time_
.InMillisecondsF());
727 state
->SetBoolean("last_set_needs_begin_frame_",
728 frame_source_
->NeedsBeginFrames());
729 state
->SetBoolean("begin_retro_frame_posted_", begin_retro_frame_posted_
);
730 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
731 state
->SetBoolean("begin_impl_frame_deadline_task_",
732 !begin_impl_frame_deadline_task_
.IsCancelled());
733 state
->SetBoolean("poll_for_draw_triggers_task_",
734 !poll_for_draw_triggers_task_
.IsCancelled());
735 state
->SetBoolean("advance_commit_state_task_",
736 !advance_commit_state_task_
.IsCancelled());
737 state
->BeginDictionary("begin_impl_frame_args");
738 begin_impl_frame_args_
.AsValueInto(state
);
739 state
->EndDictionary();
741 state
->EndDictionary();
743 state
->BeginDictionary("client_state");
744 state
->SetDouble("draw_duration_estimate_ms",
745 client_
->DrawDurationEstimate().InMillisecondsF());
747 "begin_main_frame_to_commit_duration_estimate_ms",
748 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
750 "commit_to_activate_duration_estimate_ms",
751 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
752 state
->EndDictionary();
755 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
756 // Check if the main thread computation and commit can be finished before the
757 // impl thread's deadline.
758 base::TimeTicks estimated_draw_time
=
759 begin_impl_frame_args_
.frame_time
+
760 client_
->BeginMainFrameToCommitDurationEstimate() +
761 client_
->CommitToActivateDurationEstimate();
764 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
765 "CanCommitAndActivateBeforeDeadline",
766 "time_left_after_drawing_ms",
767 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
771 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
774 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
775 return (state_machine_
.commit_state() ==
776 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
777 state_machine_
.commit_state() ==
778 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);