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::SetSwapUsedIncompleteTile(bool used_incomplete_tile
) {
248 state_machine_
.SetSwapUsedIncompleteTile(used_incomplete_tile
);
249 ProcessScheduledActions();
252 void Scheduler::DidSwapBuffersComplete() {
253 state_machine_
.DidSwapBuffersComplete();
254 ProcessScheduledActions();
257 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
258 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
259 ProcessScheduledActions();
262 void Scheduler::NotifyReadyToCommit() {
263 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
264 state_machine_
.NotifyReadyToCommit();
265 ProcessScheduledActions();
268 void Scheduler::BeginMainFrameAborted(bool did_handle
) {
269 TRACE_EVENT0("cc", "Scheduler::BeginMainFrameAborted");
270 state_machine_
.BeginMainFrameAborted(did_handle
);
271 ProcessScheduledActions();
274 void Scheduler::DidManageTiles() {
275 state_machine_
.DidManageTiles();
278 void Scheduler::DidLoseOutputSurface() {
279 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
280 state_machine_
.DidLoseOutputSurface();
281 if (frame_source_
->NeedsBeginFrames())
282 frame_source_
->SetNeedsBeginFrames(false);
283 begin_retro_frame_args_
.clear();
284 ProcessScheduledActions();
287 void Scheduler::DidCreateAndInitializeOutputSurface() {
288 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
289 DCHECK(!frame_source_
->NeedsBeginFrames());
290 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
291 state_machine_
.DidCreateAndInitializeOutputSurface();
292 ProcessScheduledActions();
295 void Scheduler::NotifyBeginMainFrameStarted() {
296 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
297 state_machine_
.NotifyBeginMainFrameStarted();
300 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
301 if (!frame_source_
->NeedsBeginFrames() ||
302 begin_impl_frame_args_
.interval
<= base::TimeDelta())
303 return base::TimeTicks();
305 base::TimeTicks now
= Now();
306 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
307 begin_impl_frame_args_
.deadline
);
308 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
309 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
312 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
313 return begin_impl_frame_args_
.frame_time
;
316 void Scheduler::SetupNextBeginFrameIfNeeded() {
317 if (!task_runner_
.get())
320 bool needs_begin_frame
= state_machine_
.BeginFrameNeeded();
322 bool at_end_of_deadline
=
323 (state_machine_
.begin_impl_frame_state() ==
324 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
);
326 bool should_call_set_needs_begin_frame
=
327 // Always request the BeginFrame immediately if it wasn't needed before.
328 (needs_begin_frame
&& !frame_source_
->NeedsBeginFrames()) ||
329 // Only stop requesting BeginFrames after a deadline.
330 (!needs_begin_frame
&& frame_source_
->NeedsBeginFrames() &&
333 if (should_call_set_needs_begin_frame
) {
334 frame_source_
->SetNeedsBeginFrames(needs_begin_frame
);
337 if (at_end_of_deadline
) {
338 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
341 PostBeginRetroFrameIfNeeded();
342 SetupPollingMechanisms(needs_begin_frame
);
345 // We may need to poll when we can't rely on BeginFrame to advance certain
346 // state or to avoid deadlock.
347 void Scheduler::SetupPollingMechanisms(bool needs_begin_frame
) {
348 bool needs_advance_commit_state_timer
= false;
349 // Setup PollForAnticipatedDrawTriggers if we need to monitor state but
350 // aren't expecting any more BeginFrames. This should only be needed by
351 // the synchronous compositor when BeginFrameNeeded is false.
352 if (state_machine_
.ShouldPollForAnticipatedDrawTriggers()) {
353 DCHECK(!state_machine_
.SupportsProactiveBeginFrame());
354 DCHECK(!needs_begin_frame
);
355 if (poll_for_draw_triggers_task_
.IsCancelled()) {
356 poll_for_draw_triggers_task_
.Reset(poll_for_draw_triggers_closure_
);
357 base::TimeDelta delay
= begin_impl_frame_args_
.IsValid()
358 ? begin_impl_frame_args_
.interval
359 : BeginFrameArgs::DefaultInterval();
360 task_runner_
->PostDelayedTask(
361 FROM_HERE
, poll_for_draw_triggers_task_
.callback(), delay
);
364 poll_for_draw_triggers_task_
.Cancel();
366 // At this point we'd prefer to advance through the commit flow by
367 // drawing a frame, however it's possible that the frame rate controller
368 // will not give us a BeginFrame until the commit completes. See
369 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
370 // we set a repeating timer to poll on ProcessScheduledActions until we
371 // successfully reach BeginFrame. Synchronous compositor does not use
372 // frame rate controller or have the circular wait in the bug.
373 if (IsBeginMainFrameSentOrStarted() &&
374 !settings_
.using_synchronous_renderer_compositor
) {
375 needs_advance_commit_state_timer
= true;
379 if (needs_advance_commit_state_timer
) {
380 if (advance_commit_state_task_
.IsCancelled() &&
381 begin_impl_frame_args_
.IsValid()) {
382 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
383 // set the interval to twice the interval from the previous frame.
384 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
385 task_runner_
->PostDelayedTask(FROM_HERE
,
386 advance_commit_state_task_
.callback(),
387 begin_impl_frame_args_
.interval
* 2);
390 advance_commit_state_task_
.Cancel();
394 // BeginFrame is the mechanism that tells us that now is a good time to start
395 // making a frame. Usually this means that user input for the frame is complete.
396 // If the scheduler is busy, we queue the BeginFrame to be handled later as
397 // a BeginRetroFrame.
398 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
399 TRACE_EVENT1("cc", "Scheduler::BeginFrame", "args", args
.AsValue());
401 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
402 // sent us the last BeginFrame we have missed. As we might not be able to
403 // actually make rendering for this call, handle it like a "retro frame".
404 // TODO(brainderson): Add a test for this functionality ASAP!
405 if (args
.type
== BeginFrameArgs::MISSED
) {
406 begin_retro_frame_args_
.push_back(args
);
407 PostBeginRetroFrameIfNeeded();
411 BeginFrameArgs
adjusted_args(args
);
412 adjusted_args
.deadline
-= EstimatedParentDrawTime();
414 bool should_defer_begin_frame
;
415 if (settings_
.using_synchronous_renderer_compositor
) {
416 should_defer_begin_frame
= false;
418 should_defer_begin_frame
=
419 !begin_retro_frame_args_
.empty() || begin_retro_frame_posted_
||
420 !frame_source_
->NeedsBeginFrames() ||
421 (state_machine_
.begin_impl_frame_state() !=
422 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
425 if (should_defer_begin_frame
) {
426 begin_retro_frame_args_
.push_back(adjusted_args
);
427 TRACE_EVENT_INSTANT0(
428 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
429 // Queuing the frame counts as "using it", so we need to return true.
431 BeginImplFrame(adjusted_args
);
436 // BeginRetroFrame is called for BeginFrames that we've deferred because
437 // the scheduler was in the middle of processing a previous BeginFrame.
438 void Scheduler::BeginRetroFrame() {
439 TRACE_EVENT0("cc", "Scheduler::BeginRetroFrame");
440 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
441 DCHECK(begin_retro_frame_posted_
);
442 begin_retro_frame_posted_
= false;
444 // If there aren't any retroactive BeginFrames, then we've lost the
445 // OutputSurface and should abort.
446 if (begin_retro_frame_args_
.empty())
449 // Discard expired BeginRetroFrames
450 // Today, we should always end up with at most one un-expired BeginRetroFrame
451 // because deadlines will not be greater than the next frame time. We don't
452 // DCHECK though because some systems don't always have monotonic timestamps.
453 // TODO(brianderson): In the future, long deadlines could result in us not
454 // draining the queue if we don't catch up. If we consistently can't catch
455 // up, our fallback should be to lower our frame rate.
456 base::TimeTicks now
= Now();
457 base::TimeDelta draw_duration_estimate
= client_
->DrawDurationEstimate();
458 while (!begin_retro_frame_args_
.empty()) {
459 base::TimeTicks adjusted_deadline
= AdjustedBeginImplFrameDeadline(
460 begin_retro_frame_args_
.front(), draw_duration_estimate
);
461 if (now
<= adjusted_deadline
)
464 TRACE_EVENT_INSTANT2("cc",
465 "Scheduler::BeginRetroFrame discarding",
466 TRACE_EVENT_SCOPE_THREAD
,
468 (adjusted_deadline
- now
).InMicroseconds(),
470 begin_retro_frame_args_
.front().AsValue());
471 begin_retro_frame_args_
.pop_front();
472 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
475 if (begin_retro_frame_args_
.empty()) {
476 TRACE_EVENT_INSTANT0("cc",
477 "Scheduler::BeginRetroFrames all expired",
478 TRACE_EVENT_SCOPE_THREAD
);
480 BeginImplFrame(begin_retro_frame_args_
.front());
481 begin_retro_frame_args_
.pop_front();
485 // There could be a race between the posted BeginRetroFrame and a new
486 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
487 // will check if there is a pending BeginRetroFrame to ensure we handle
488 // BeginFrames in FIFO order.
489 void Scheduler::PostBeginRetroFrameIfNeeded() {
490 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
491 "Scheduler::PostBeginRetroFrameIfNeeded",
494 if (!frame_source_
->NeedsBeginFrames())
497 if (begin_retro_frame_args_
.empty() || begin_retro_frame_posted_
)
500 // begin_retro_frame_args_ should always be empty for the
501 // synchronous compositor.
502 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
504 if (state_machine_
.begin_impl_frame_state() !=
505 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
508 begin_retro_frame_posted_
= true;
509 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_closure_
);
512 // BeginImplFrame starts a compositor frame that will wait up until a deadline
513 // for a BeginMainFrame+activation to complete before it times out and draws
514 // any asynchronous animation and scroll/pinch updates.
515 void Scheduler::BeginImplFrame(const BeginFrameArgs
& args
) {
516 bool main_thread_is_in_high_latency_mode
=
517 state_machine_
.MainThreadIsInHighLatencyMode();
519 "Scheduler::BeginImplFrame",
522 "main_thread_is_high_latency",
523 main_thread_is_in_high_latency_mode
);
524 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
526 main_thread_is_in_high_latency_mode
);
527 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
528 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
529 DCHECK(state_machine_
.HasInitializedOutputSurface());
531 advance_commit_state_task_
.Cancel();
533 base::TimeDelta draw_duration_estimate
= client_
->DrawDurationEstimate();
534 begin_impl_frame_args_
= args
;
535 begin_impl_frame_args_
.deadline
-= draw_duration_estimate
;
537 if (!state_machine_
.impl_latency_takes_priority() &&
538 main_thread_is_in_high_latency_mode
&&
539 CanCommitAndActivateBeforeDeadline()) {
540 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
543 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
544 state_machine_
.OnBeginImplFrame(begin_impl_frame_args_
);
545 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
547 ProcessScheduledActions();
549 state_machine_
.OnBeginImplFrameDeadlinePending();
550 ScheduleBeginImplFrameDeadline(
551 AdjustedBeginImplFrameDeadline(args
, draw_duration_estimate
));
554 base::TimeTicks
Scheduler::AdjustedBeginImplFrameDeadline(
555 const BeginFrameArgs
& args
,
556 base::TimeDelta draw_duration_estimate
) const {
557 if (settings_
.using_synchronous_renderer_compositor
) {
558 // The synchronous compositor needs to draw right away.
559 return base::TimeTicks();
560 } else if (state_machine_
.ShouldTriggerBeginImplFrameDeadlineEarly()) {
561 // We are ready to draw a new active tree immediately.
562 return base::TimeTicks();
563 } else if (state_machine_
.needs_redraw()) {
564 // We have an animation or fast input path on the impl thread that wants
565 // to draw, so don't wait too long for a new active tree.
566 return args
.deadline
- draw_duration_estimate
;
568 // The impl thread doesn't have anything it wants to draw and we are just
569 // waiting for a new active tree, so post the deadline for the next
570 // expected BeginImplFrame start. This allows us to draw immediately when
571 // there is a new active tree, instead of waiting for the next
573 // TODO(brianderson): Handle long deadlines (that are past the next frame's
574 // frame time) properly instead of using this hack.
575 return args
.frame_time
+ args
.interval
;
579 void Scheduler::ScheduleBeginImplFrameDeadline(base::TimeTicks deadline
) {
581 "cc", "Scheduler::ScheduleBeginImplFrameDeadline", "deadline", deadline
);
582 if (settings_
.using_synchronous_renderer_compositor
) {
583 // The synchronous renderer compositor has to make its GL calls
585 // TODO(brianderson): Have the OutputSurface initiate the deadline tasks
586 // so the sychronous renderer compositor can take advantage of splitting
587 // up the BeginImplFrame and deadline as well.
588 OnBeginImplFrameDeadline();
591 begin_impl_frame_deadline_task_
.Cancel();
592 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
594 base::TimeDelta delta
= deadline
- Now();
595 if (delta
<= base::TimeDelta())
596 delta
= base::TimeDelta();
597 task_runner_
->PostDelayedTask(
598 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
601 void Scheduler::OnBeginImplFrameDeadline() {
602 TRACE_EVENT0("cc", "Scheduler::OnBeginImplFrameDeadline");
603 begin_impl_frame_deadline_task_
.Cancel();
605 // We split the deadline actions up into two phases so the state machine
606 // has a chance to trigger actions that should occur durring and after
607 // the deadline separately. For example:
608 // * Sending the BeginMainFrame will not occur after the deadline in
609 // order to wait for more user-input before starting the next commit.
610 // * Creating a new OuputSurface will not occur during the deadline in
611 // order to allow the state machine to "settle" first.
612 state_machine_
.OnBeginImplFrameDeadline();
613 ProcessScheduledActions();
614 state_machine_
.OnBeginImplFrameIdle();
615 ProcessScheduledActions();
617 client_
->DidBeginImplFrameDeadline();
620 void Scheduler::PollForAnticipatedDrawTriggers() {
621 TRACE_EVENT0("cc", "Scheduler::PollForAnticipatedDrawTriggers");
622 poll_for_draw_triggers_task_
.Cancel();
623 state_machine_
.DidEnterPollForAnticipatedDrawTriggers();
624 ProcessScheduledActions();
625 state_machine_
.DidLeavePollForAnticipatedDrawTriggers();
628 void Scheduler::PollToAdvanceCommitState() {
629 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
630 advance_commit_state_task_
.Cancel();
631 ProcessScheduledActions();
634 void Scheduler::DrawAndSwapIfPossible() {
635 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
636 state_machine_
.DidDrawIfPossibleCompleted(result
);
639 void Scheduler::ProcessScheduledActions() {
640 // We do not allow ProcessScheduledActions to be recursive.
641 // The top-level call will iteratively execute the next action for us anyway.
642 if (inside_process_scheduled_actions_
)
645 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
647 SchedulerStateMachine::Action action
;
649 action
= state_machine_
.NextAction();
650 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
651 "SchedulerStateMachine",
654 VLOG(2) << "Scheduler::ProcessScheduledActions: "
655 << SchedulerStateMachine::ActionToString(action
) << " "
656 << state_machine_
.GetStatesForDebugging();
657 state_machine_
.UpdateState(action
);
658 base::AutoReset
<SchedulerStateMachine::Action
>
659 mark_inside_action(&inside_action_
, action
);
661 case SchedulerStateMachine::ACTION_NONE
:
663 case SchedulerStateMachine::ACTION_ANIMATE
:
664 client_
->ScheduledActionAnimate();
666 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
:
667 client_
->ScheduledActionSendBeginMainFrame();
669 case SchedulerStateMachine::ACTION_COMMIT
:
670 client_
->ScheduledActionCommit();
672 case SchedulerStateMachine::ACTION_UPDATE_VISIBLE_TILES
:
673 client_
->ScheduledActionUpdateVisibleTiles();
675 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
:
676 client_
->ScheduledActionActivateSyncTree();
678 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
679 DrawAndSwapIfPossible();
681 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
:
682 client_
->ScheduledActionDrawAndSwapForced();
684 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
685 // No action is actually performed, but this allows the state machine to
686 // advance out of its waiting to draw state without actually drawing.
688 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
689 client_
->ScheduledActionBeginOutputSurfaceCreation();
691 case SchedulerStateMachine::ACTION_MANAGE_TILES
:
692 client_
->ScheduledActionManageTiles();
695 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
697 SetupNextBeginFrameIfNeeded();
698 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
700 if (state_machine_
.ShouldTriggerBeginImplFrameDeadlineEarly()) {
701 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
702 ScheduleBeginImplFrameDeadline(base::TimeTicks());
706 bool Scheduler::WillDrawIfNeeded() const {
707 return !state_machine_
.PendingDrawsShouldBeAborted();
710 scoped_refptr
<base::debug::ConvertableToTraceFormat
> Scheduler::AsValue()
712 scoped_refptr
<base::debug::TracedValue
> state
=
713 new base::debug::TracedValue();
714 AsValueInto(state
.get());
718 void Scheduler::AsValueInto(base::debug::TracedValue
* state
) const {
719 state
->BeginDictionary("state_machine");
720 state_machine_
.AsValueInto(state
, Now());
721 state
->EndDictionary();
723 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
724 bool frame_tracing_enabled
= false;
725 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
726 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
727 &frame_tracing_enabled
);
728 if (frame_tracing_enabled
) {
729 state
->BeginDictionary("frame_source_");
730 frame_source_
->AsValueInto(state
);
731 state
->EndDictionary();
734 state
->BeginDictionary("scheduler_state");
735 state
->SetDouble("time_until_anticipated_draw_time_ms",
736 (AnticipatedDrawTime() - Now()).InMillisecondsF());
737 state
->SetDouble("estimated_parent_draw_time_ms",
738 estimated_parent_draw_time_
.InMillisecondsF());
739 state
->SetBoolean("last_set_needs_begin_frame_",
740 frame_source_
->NeedsBeginFrames());
741 state
->SetBoolean("begin_retro_frame_posted_", begin_retro_frame_posted_
);
742 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
743 state
->SetBoolean("begin_impl_frame_deadline_task_",
744 !begin_impl_frame_deadline_task_
.IsCancelled());
745 state
->SetBoolean("poll_for_draw_triggers_task_",
746 !poll_for_draw_triggers_task_
.IsCancelled());
747 state
->SetBoolean("advance_commit_state_task_",
748 !advance_commit_state_task_
.IsCancelled());
749 state
->BeginDictionary("begin_impl_frame_args");
750 begin_impl_frame_args_
.AsValueInto(state
);
751 state
->EndDictionary();
753 state
->EndDictionary();
755 state
->BeginDictionary("client_state");
756 state
->SetDouble("draw_duration_estimate_ms",
757 client_
->DrawDurationEstimate().InMillisecondsF());
759 "begin_main_frame_to_commit_duration_estimate_ms",
760 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
762 "commit_to_activate_duration_estimate_ms",
763 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
764 state
->EndDictionary();
767 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
768 // Check if the main thread computation and commit can be finished before the
769 // impl thread's deadline.
770 base::TimeTicks estimated_draw_time
=
771 begin_impl_frame_args_
.frame_time
+
772 client_
->BeginMainFrameToCommitDurationEstimate() +
773 client_
->CommitToActivateDurationEstimate();
776 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
777 "CanCommitAndActivateBeforeDeadline",
778 "time_left_after_drawing_ms",
779 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
783 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
786 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
787 return (state_machine_
.commit_state() ==
788 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
789 state_machine_
.commit_state() ==
790 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);