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/logging.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_argument.h"
15 #include "cc/debug/devtools_instrumentation.h"
16 #include "cc/debug/traced_value.h"
17 #include "cc/scheduler/delay_based_time_source.h"
18 #include "ui/gfx/frame_time.h"
22 BeginFrameSource
* SchedulerFrameSourcesConstructor::ConstructPrimaryFrameSource(
23 Scheduler
* scheduler
) {
24 if (scheduler
->settings_
.use_external_begin_frame_source
) {
26 "Scheduler::Scheduler()",
28 "ExternalBeginFrameSource");
29 DCHECK(scheduler
->primary_frame_source_internal_
)
30 << "Need external BeginFrameSource";
31 return scheduler
->primary_frame_source_internal_
.get();
34 "Scheduler::Scheduler()",
36 "SyntheticBeginFrameSource");
37 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_source
=
38 SyntheticBeginFrameSource::Create(scheduler
->task_runner_
.get(),
40 BeginFrameArgs::DefaultInterval());
42 DCHECK(!scheduler
->vsync_observer_
);
43 scheduler
->vsync_observer_
= synthetic_source
.get();
45 DCHECK(!scheduler
->primary_frame_source_internal_
);
46 scheduler
->primary_frame_source_internal_
= synthetic_source
.Pass();
47 return scheduler
->primary_frame_source_internal_
.get();
52 SchedulerFrameSourcesConstructor::ConstructBackgroundFrameSource(
53 Scheduler
* scheduler
) {
55 "Scheduler::Scheduler()",
56 "BackgroundFrameSource",
57 "SyntheticBeginFrameSource");
58 DCHECK(!(scheduler
->background_frame_source_internal_
));
59 scheduler
->background_frame_source_internal_
=
60 SyntheticBeginFrameSource::Create(
61 scheduler
->task_runner_
.get(), scheduler
->Now(),
62 scheduler
->settings_
.background_frame_interval
);
63 return scheduler
->background_frame_source_internal_
.get();
67 SchedulerFrameSourcesConstructor::ConstructUnthrottledFrameSource(
68 Scheduler
* scheduler
) {
69 TRACE_EVENT1("cc", "Scheduler::Scheduler()", "UnthrottledFrameSource",
70 "BackToBackBeginFrameSource");
71 DCHECK(!scheduler
->unthrottled_frame_source_internal_
);
72 scheduler
->unthrottled_frame_source_internal_
=
73 BackToBackBeginFrameSource::Create(scheduler
->task_runner_
.get());
74 return scheduler
->unthrottled_frame_source_internal_
.get();
78 SchedulerClient
* client
,
79 const SchedulerSettings
& scheduler_settings
,
80 int layer_tree_host_id
,
81 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
82 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
83 SchedulerFrameSourcesConstructor
* frame_sources_constructor
)
85 primary_frame_source_(NULL
),
86 background_frame_source_(NULL
),
87 primary_frame_source_internal_(external_begin_frame_source
.Pass()),
88 background_frame_source_internal_(),
89 vsync_observer_(NULL
),
90 authoritative_vsync_interval_(base::TimeDelta()),
91 last_vsync_timebase_(base::TimeTicks()),
92 throttle_frame_production_(scheduler_settings
.throttle_frame_production
),
93 settings_(scheduler_settings
),
95 layer_tree_host_id_(layer_tree_host_id
),
96 task_runner_(task_runner
),
97 begin_impl_frame_deadline_mode_(
98 SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
),
99 state_machine_(scheduler_settings
),
100 inside_process_scheduled_actions_(false),
101 inside_action_(SchedulerStateMachine::ACTION_NONE
),
102 weak_factory_(this) {
103 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
104 "Scheduler::Scheduler",
106 settings_
.AsValue());
108 DCHECK(!state_machine_
.BeginFrameNeeded());
110 begin_retro_frame_closure_
=
111 base::Bind(&Scheduler::BeginRetroFrame
, weak_factory_
.GetWeakPtr());
112 begin_impl_frame_deadline_closure_
= base::Bind(
113 &Scheduler::OnBeginImplFrameDeadline
, weak_factory_
.GetWeakPtr());
114 advance_commit_state_closure_
= base::Bind(
115 &Scheduler::PollToAdvanceCommitState
, weak_factory_
.GetWeakPtr());
117 frame_source_
= BeginFrameSourceMultiplexer::Create();
118 frame_source_
->AddObserver(this);
120 // Primary frame source
121 primary_frame_source_
=
122 frame_sources_constructor
->ConstructPrimaryFrameSource(this);
123 frame_source_
->AddSource(primary_frame_source_
);
124 primary_frame_source_
->SetClientReady();
126 // Background ticking frame source
127 background_frame_source_
=
128 frame_sources_constructor
->ConstructBackgroundFrameSource(this);
129 frame_source_
->AddSource(background_frame_source_
);
131 // Unthrottled frame source
132 unthrottled_frame_source_
=
133 frame_sources_constructor
->ConstructUnthrottledFrameSource(this);
134 frame_source_
->AddSource(unthrottled_frame_source_
);
137 Scheduler::~Scheduler() {
138 if (frame_source_
->NeedsBeginFrames())
139 frame_source_
->SetNeedsBeginFrames(false);
142 base::TimeTicks
Scheduler::Now() const {
143 base::TimeTicks now
= gfx::FrameTime::Now();
144 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"),
151 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase
,
152 base::TimeDelta interval
) {
153 if (authoritative_vsync_interval_
!= base::TimeDelta()) {
154 interval
= authoritative_vsync_interval_
;
155 } else if (interval
== base::TimeDelta()) {
156 // TODO(brianderson): We should not be receiving 0 intervals.
157 interval
= BeginFrameArgs::DefaultInterval();
160 last_vsync_timebase_
= timebase
;
163 vsync_observer_
->OnUpdateVSyncParameters(timebase
, interval
);
166 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time
) {
167 DCHECK_GE(draw_time
.ToInternalValue(), 0);
168 estimated_parent_draw_time_
= draw_time
;
171 void Scheduler::SetCanStart() {
172 state_machine_
.SetCanStart();
173 ProcessScheduledActions();
176 void Scheduler::UpdateActiveFrameSource() {
177 if (state_machine_
.visible()) {
178 if (throttle_frame_production_
) {
179 frame_source_
->SetActiveSource(primary_frame_source_
);
181 frame_source_
->SetActiveSource(unthrottled_frame_source_
);
184 frame_source_
->SetActiveSource(background_frame_source_
);
186 ProcessScheduledActions();
189 void Scheduler::SetVisible(bool visible
) {
190 state_machine_
.SetVisible(visible
);
191 UpdateActiveFrameSource();
194 void Scheduler::SetCanDraw(bool can_draw
) {
195 state_machine_
.SetCanDraw(can_draw
);
196 ProcessScheduledActions();
199 void Scheduler::NotifyReadyToActivate() {
200 state_machine_
.NotifyReadyToActivate();
201 ProcessScheduledActions();
204 void Scheduler::NotifyReadyToDraw() {
205 // Future work might still needed for crbug.com/352894.
206 state_machine_
.NotifyReadyToDraw();
207 ProcessScheduledActions();
210 void Scheduler::SetThrottleFrameProduction(bool throttle
) {
211 throttle_frame_production_
= throttle
;
212 UpdateActiveFrameSource();
215 void Scheduler::SetNeedsCommit() {
216 state_machine_
.SetNeedsCommit();
217 ProcessScheduledActions();
220 void Scheduler::SetNeedsRedraw() {
221 state_machine_
.SetNeedsRedraw();
222 ProcessScheduledActions();
225 void Scheduler::SetNeedsAnimate() {
226 state_machine_
.SetNeedsAnimate();
227 ProcessScheduledActions();
230 void Scheduler::SetNeedsPrepareTiles() {
231 DCHECK(!IsInsideAction(SchedulerStateMachine::ACTION_PREPARE_TILES
));
232 state_machine_
.SetNeedsPrepareTiles();
233 ProcessScheduledActions();
236 void Scheduler::SetWaitForReadyToDraw() {
237 state_machine_
.SetWaitForReadyToDraw();
238 ProcessScheduledActions();
241 void Scheduler::SetMaxSwapsPending(int max
) {
242 state_machine_
.SetMaxSwapsPending(max
);
245 void Scheduler::DidSwapBuffers() {
246 state_machine_
.DidSwapBuffers();
248 // There is no need to call ProcessScheduledActions here because
249 // swapping should not trigger any new actions.
250 if (!inside_process_scheduled_actions_
) {
251 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
255 void Scheduler::DidSwapBuffersComplete() {
256 state_machine_
.DidSwapBuffersComplete();
257 ProcessScheduledActions();
260 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
261 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
262 ProcessScheduledActions();
265 void Scheduler::NotifyReadyToCommit() {
266 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
267 state_machine_
.NotifyReadyToCommit();
268 ProcessScheduledActions();
271 void Scheduler::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
272 TRACE_EVENT1("cc", "Scheduler::BeginMainFrameAborted", "reason",
273 CommitEarlyOutReasonToString(reason
));
274 state_machine_
.BeginMainFrameAborted(reason
);
275 ProcessScheduledActions();
278 void Scheduler::DidPrepareTiles() {
279 state_machine_
.DidPrepareTiles();
282 void Scheduler::DidLoseOutputSurface() {
283 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
284 begin_retro_frame_args_
.clear();
285 begin_retro_frame_task_
.Cancel();
286 state_machine_
.DidLoseOutputSurface();
287 ProcessScheduledActions();
290 void Scheduler::DidCreateAndInitializeOutputSurface() {
291 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
292 DCHECK(!frame_source_
->NeedsBeginFrames());
293 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
294 state_machine_
.DidCreateAndInitializeOutputSurface();
295 ProcessScheduledActions();
298 void Scheduler::NotifyBeginMainFrameStarted() {
299 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
300 state_machine_
.NotifyBeginMainFrameStarted();
303 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
304 if (!frame_source_
->NeedsBeginFrames() ||
305 begin_impl_frame_args_
.interval
<= base::TimeDelta())
306 return base::TimeTicks();
308 base::TimeTicks now
= Now();
309 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
310 begin_impl_frame_args_
.deadline
);
311 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
312 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
315 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
316 return begin_impl_frame_args_
.frame_time
;
319 void Scheduler::SetupNextBeginFrameIfNeeded() {
320 // Never call SetNeedsBeginFrames if the frame source already has the right
322 if (frame_source_
->NeedsBeginFrames() != state_machine_
.BeginFrameNeeded()) {
323 if (state_machine_
.BeginFrameNeeded()) {
324 // Call SetNeedsBeginFrames(true) as soon as possible.
325 frame_source_
->SetNeedsBeginFrames(true);
326 } else if (state_machine_
.begin_impl_frame_state() ==
327 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
) {
328 // Call SetNeedsBeginFrames(false) in between frames only.
329 frame_source_
->SetNeedsBeginFrames(false);
330 client_
->SendBeginMainFrameNotExpectedSoon();
334 PostBeginRetroFrameIfNeeded();
337 // We may need to poll when we can't rely on BeginFrame to advance certain
338 // state or to avoid deadlock.
339 void Scheduler::SetupPollingMechanisms() {
340 // At this point we'd prefer to advance through the commit flow by
341 // drawing a frame, however it's possible that the frame rate controller
342 // will not give us a BeginFrame until the commit completes. See
343 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
344 // we set a repeating timer to poll on ProcessScheduledActions until we
345 // successfully reach BeginFrame. Synchronous compositor does not use
346 // frame rate controller or have the circular wait in the bug.
347 if (IsBeginMainFrameSentOrStarted() &&
348 !settings_
.using_synchronous_renderer_compositor
) {
349 if (advance_commit_state_task_
.IsCancelled() &&
350 begin_impl_frame_args_
.IsValid()) {
351 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
352 // set the interval to twice the interval from the previous frame.
353 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
354 task_runner_
->PostDelayedTask(FROM_HERE
,
355 advance_commit_state_task_
.callback(),
356 begin_impl_frame_args_
.interval
* 2);
359 advance_commit_state_task_
.Cancel();
363 // BeginFrame is the mechanism that tells us that now is a good time to start
364 // making a frame. Usually this means that user input for the frame is complete.
365 // If the scheduler is busy, we queue the BeginFrame to be handled later as
366 // a BeginRetroFrame.
367 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
368 TRACE_EVENT1("cc,benchmark", "Scheduler::BeginFrame", "args", args
.AsValue());
370 // Deliver BeginFrames to children.
371 if (state_machine_
.children_need_begin_frames()) {
372 BeginFrameArgs
adjusted_args_for_children(args
);
373 // Adjust a deadline for child schedulers.
374 // TODO(simonhong): Once we have commitless update, we can get rid of
375 // BeginMainFrameToCommitDurationEstimate() +
376 // CommitToActivateDurationEstimate().
377 adjusted_args_for_children
.deadline
-=
378 (client_
->BeginMainFrameToCommitDurationEstimate() +
379 client_
->CommitToActivateDurationEstimate() +
380 client_
->DrawDurationEstimate() + EstimatedParentDrawTime());
381 client_
->SendBeginFramesToChildren(adjusted_args_for_children
);
384 BeginFrameArgs
adjusted_args(args
);
385 adjusted_args
.deadline
-= EstimatedParentDrawTime();
387 if (settings_
.using_synchronous_renderer_compositor
) {
388 BeginImplFrameSynchronous(adjusted_args
);
392 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
393 // sent us the last BeginFrame we have missed. As we might not be able to
394 // actually make rendering for this call, handle it like a "retro frame".
395 // TODO(brainderson): Add a test for this functionality ASAP!
396 if (adjusted_args
.type
== BeginFrameArgs::MISSED
) {
397 begin_retro_frame_args_
.push_back(adjusted_args
);
398 PostBeginRetroFrameIfNeeded();
402 bool should_defer_begin_frame
=
403 !begin_retro_frame_args_
.empty() ||
404 !begin_retro_frame_task_
.IsCancelled() ||
405 !frame_source_
->NeedsBeginFrames() ||
406 (state_machine_
.begin_impl_frame_state() !=
407 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
409 if (should_defer_begin_frame
) {
410 begin_retro_frame_args_
.push_back(adjusted_args
);
411 TRACE_EVENT_INSTANT0(
412 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
413 // Queuing the frame counts as "using it", so we need to return true.
415 BeginImplFrameWithDeadline(adjusted_args
);
420 void Scheduler::SetChildrenNeedBeginFrames(bool children_need_begin_frames
) {
421 state_machine_
.SetChildrenNeedBeginFrames(children_need_begin_frames
);
422 ProcessScheduledActions();
425 void Scheduler::SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
) {
426 authoritative_vsync_interval_
= interval
;
428 vsync_observer_
->OnUpdateVSyncParameters(last_vsync_timebase_
, interval
);
431 void Scheduler::OnDrawForOutputSurface() {
432 DCHECK(settings_
.using_synchronous_renderer_compositor
);
433 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
434 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
435 DCHECK(!BeginImplFrameDeadlinePending());
437 state_machine_
.OnBeginImplFrameDeadline();
438 ProcessScheduledActions();
440 state_machine_
.OnBeginImplFrameIdle();
441 ProcessScheduledActions();
444 // BeginRetroFrame is called for BeginFrames that we've deferred because
445 // the scheduler was in the middle of processing a previous BeginFrame.
446 void Scheduler::BeginRetroFrame() {
447 TRACE_EVENT0("cc,benchmark", "Scheduler::BeginRetroFrame");
448 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
449 DCHECK(!begin_retro_frame_args_
.empty());
450 DCHECK(!begin_retro_frame_task_
.IsCancelled());
451 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
452 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
454 begin_retro_frame_task_
.Cancel();
456 // Discard expired BeginRetroFrames
457 // Today, we should always end up with at most one un-expired BeginRetroFrame
458 // because deadlines will not be greater than the next frame time. We don't
459 // DCHECK though because some systems don't always have monotonic timestamps.
460 // TODO(brianderson): In the future, long deadlines could result in us not
461 // draining the queue if we don't catch up. If we consistently can't catch
462 // up, our fallback should be to lower our frame rate.
463 base::TimeTicks now
= Now();
465 while (!begin_retro_frame_args_
.empty()) {
466 const BeginFrameArgs
& args
= begin_retro_frame_args_
.front();
467 base::TimeTicks expiration_time
= args
.frame_time
+ args
.interval
;
468 if (now
<= expiration_time
)
470 TRACE_EVENT_INSTANT2(
471 "cc", "Scheduler::BeginRetroFrame discarding", TRACE_EVENT_SCOPE_THREAD
,
472 "expiration_time - now", (expiration_time
- now
).InMillisecondsF(),
473 "BeginFrameArgs", begin_retro_frame_args_
.front().AsValue());
474 begin_retro_frame_args_
.pop_front();
475 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
478 if (begin_retro_frame_args_
.empty()) {
479 TRACE_EVENT_INSTANT0("cc",
480 "Scheduler::BeginRetroFrames all expired",
481 TRACE_EVENT_SCOPE_THREAD
);
483 BeginFrameArgs front
= begin_retro_frame_args_
.front();
484 begin_retro_frame_args_
.pop_front();
485 BeginImplFrameWithDeadline(front
);
489 // There could be a race between the posted BeginRetroFrame and a new
490 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
491 // will check if there is a pending BeginRetroFrame to ensure we handle
492 // BeginFrames in FIFO order.
493 void Scheduler::PostBeginRetroFrameIfNeeded() {
494 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
495 "Scheduler::PostBeginRetroFrameIfNeeded",
498 if (!frame_source_
->NeedsBeginFrames())
501 if (begin_retro_frame_args_
.empty() || !begin_retro_frame_task_
.IsCancelled())
504 // begin_retro_frame_args_ should always be empty for the
505 // synchronous compositor.
506 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
508 if (state_machine_
.begin_impl_frame_state() !=
509 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
512 begin_retro_frame_task_
.Reset(begin_retro_frame_closure_
);
514 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_task_
.callback());
517 void Scheduler::BeginImplFrameWithDeadline(const BeginFrameArgs
& args
) {
518 BeginImplFrame(args
);
520 // The deadline will be scheduled in ProcessScheduledActions.
521 state_machine_
.OnBeginImplFrameDeadlinePending();
522 ProcessScheduledActions();
525 void Scheduler::BeginImplFrameSynchronous(const BeginFrameArgs
& args
) {
526 BeginImplFrame(args
);
530 void Scheduler::FinishImplFrame() {
531 state_machine_
.OnBeginImplFrameIdle();
532 ProcessScheduledActions();
534 client_
->DidBeginImplFrameDeadline();
535 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
538 // BeginImplFrame starts a compositor frame that will wait up until a deadline
539 // for a BeginMainFrame+activation to complete before it times out and draws
540 // any asynchronous animation and scroll/pinch updates.
541 void Scheduler::BeginImplFrame(const BeginFrameArgs
& args
) {
542 bool main_thread_is_in_high_latency_mode
=
543 state_machine_
.MainThreadIsInHighLatencyMode();
544 TRACE_EVENT2("cc,benchmark",
545 "Scheduler::BeginImplFrame",
548 "main_thread_is_high_latency",
549 main_thread_is_in_high_latency_mode
);
550 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
552 main_thread_is_in_high_latency_mode
);
553 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
554 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
555 DCHECK(!BeginImplFrameDeadlinePending());
556 DCHECK(state_machine_
.HasInitializedOutputSurface());
558 advance_commit_state_task_
.Cancel();
560 begin_impl_frame_args_
= args
;
561 begin_impl_frame_args_
.deadline
-= client_
->DrawDurationEstimate();
563 if (!state_machine_
.impl_latency_takes_priority() &&
564 main_thread_is_in_high_latency_mode
&&
565 CanCommitAndActivateBeforeDeadline()) {
566 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
569 state_machine_
.OnBeginImplFrame();
570 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
571 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
573 ProcessScheduledActions();
576 void Scheduler::ScheduleBeginImplFrameDeadline() {
577 // The synchronous compositor does not post a deadline task.
578 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
580 begin_impl_frame_deadline_task_
.Cancel();
581 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
583 begin_impl_frame_deadline_mode_
=
584 state_machine_
.CurrentBeginImplFrameDeadlineMode();
586 base::TimeTicks deadline
;
587 switch (begin_impl_frame_deadline_mode_
) {
588 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
:
591 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
592 // We are ready to draw a new active tree immediately.
593 // We don't use Now() here because it's somewhat expensive to call.
594 deadline
= base::TimeTicks();
596 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
597 // We are animating on the impl thread but we can wait for some time.
598 deadline
= begin_impl_frame_args_
.deadline
;
600 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
601 // We are blocked for one reason or another and we should wait.
602 // TODO(brianderson): Handle long deadlines (that are past the next
603 // frame's frame time) properly instead of using this hack.
605 begin_impl_frame_args_
.frame_time
+ begin_impl_frame_args_
.interval
;
607 case SchedulerStateMachine::
608 BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
:
609 // We are blocked because we are waiting for ReadyToDraw signal. We would
610 // post deadline after we received ReadyToDraw singal.
611 TRACE_EVENT1("cc", "Scheduler::ScheduleBeginImplFrameDeadline",
612 "deadline_mode", "blocked_on_ready_to_draw");
616 TRACE_EVENT2("cc", "Scheduler::ScheduleBeginImplFrameDeadline", "mode",
617 SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
618 begin_impl_frame_deadline_mode_
),
619 "deadline", deadline
);
621 base::TimeDelta delta
= std::max(deadline
- Now(), base::TimeDelta());
622 task_runner_
->PostDelayedTask(
623 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
626 void Scheduler::ScheduleBeginImplFrameDeadlineIfNeeded() {
627 if (settings_
.using_synchronous_renderer_compositor
)
630 if (state_machine_
.begin_impl_frame_state() !=
631 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
634 if (begin_impl_frame_deadline_mode_
==
635 state_machine_
.CurrentBeginImplFrameDeadlineMode() &&
636 BeginImplFrameDeadlinePending())
639 ScheduleBeginImplFrameDeadline();
642 void Scheduler::OnBeginImplFrameDeadline() {
643 TRACE_EVENT0("cc,benchmark", "Scheduler::OnBeginImplFrameDeadline");
644 begin_impl_frame_deadline_task_
.Cancel();
645 // We split the deadline actions up into two phases so the state machine
646 // has a chance to trigger actions that should occur durring and after
647 // the deadline separately. For example:
648 // * Sending the BeginMainFrame will not occur after the deadline in
649 // order to wait for more user-input before starting the next commit.
650 // * Creating a new OuputSurface will not occur during the deadline in
651 // order to allow the state machine to "settle" first.
653 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
654 tracked_objects::ScopedTracker
tracking_profile1(
655 FROM_HERE_WITH_EXPLICIT_FUNCTION(
656 "461509 Scheduler::OnBeginImplFrameDeadline1"));
657 state_machine_
.OnBeginImplFrameDeadline();
658 ProcessScheduledActions();
663 void Scheduler::PollToAdvanceCommitState() {
664 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
665 advance_commit_state_task_
.Cancel();
666 ProcessScheduledActions();
669 void Scheduler::DrawAndSwapIfPossible() {
670 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
671 state_machine_
.DidDrawIfPossibleCompleted(result
);
674 void Scheduler::SetDeferCommits(bool defer_commits
) {
675 TRACE_EVENT1("cc", "Scheduler::SetDeferCommits",
678 state_machine_
.SetDeferCommits(defer_commits
);
679 ProcessScheduledActions();
682 void Scheduler::ProcessScheduledActions() {
683 // We do not allow ProcessScheduledActions to be recursive.
684 // The top-level call will iteratively execute the next action for us anyway.
685 if (inside_process_scheduled_actions_
)
688 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
690 SchedulerStateMachine::Action action
;
692 action
= state_machine_
.NextAction();
693 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
694 "SchedulerStateMachine",
697 VLOG(2) << "Scheduler::ProcessScheduledActions: "
698 << SchedulerStateMachine::ActionToString(action
) << " "
699 << state_machine_
.GetStatesForDebugging();
700 state_machine_
.UpdateState(action
);
701 base::AutoReset
<SchedulerStateMachine::Action
>
702 mark_inside_action(&inside_action_
, action
);
704 case SchedulerStateMachine::ACTION_NONE
:
706 case SchedulerStateMachine::ACTION_ANIMATE
:
707 client_
->ScheduledActionAnimate();
709 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
:
710 client_
->ScheduledActionSendBeginMainFrame();
712 case SchedulerStateMachine::ACTION_COMMIT
: {
713 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
715 tracked_objects::ScopedTracker
tracking_profile4(
716 FROM_HERE_WITH_EXPLICIT_FUNCTION(
717 "461509 Scheduler::ProcessScheduledActions4"));
718 client_
->ScheduledActionCommit();
721 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
:
722 client_
->ScheduledActionActivateSyncTree();
724 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
725 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
727 tracked_objects::ScopedTracker
tracking_profile6(
728 FROM_HERE_WITH_EXPLICIT_FUNCTION(
729 "461509 Scheduler::ProcessScheduledActions6"));
730 DrawAndSwapIfPossible();
733 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
:
734 client_
->ScheduledActionDrawAndSwapForced();
736 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
737 // No action is actually performed, but this allows the state machine to
738 // advance out of its waiting to draw state without actually drawing.
740 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
741 client_
->ScheduledActionBeginOutputSurfaceCreation();
743 case SchedulerStateMachine::ACTION_PREPARE_TILES
:
744 client_
->ScheduledActionPrepareTiles();
746 case SchedulerStateMachine::ACTION_INVALIDATE_OUTPUT_SURFACE
: {
747 client_
->ScheduledActionInvalidateOutputSurface();
751 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
753 SetupPollingMechanisms();
755 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
757 ScheduleBeginImplFrameDeadlineIfNeeded();
759 SetupNextBeginFrameIfNeeded();
762 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> Scheduler::AsValue()
764 scoped_refptr
<base::trace_event::TracedValue
> state
=
765 new base::trace_event::TracedValue();
766 AsValueInto(state
.get());
770 void Scheduler::AsValueInto(base::trace_event::TracedValue
* state
) const {
771 state
->BeginDictionary("state_machine");
772 state_machine_
.AsValueInto(state
);
773 state
->EndDictionary();
775 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
776 bool frame_tracing_enabled
= false;
777 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
778 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
779 &frame_tracing_enabled
);
780 if (frame_tracing_enabled
) {
781 state
->BeginDictionary("frame_source_");
782 frame_source_
->AsValueInto(state
);
783 state
->EndDictionary();
786 state
->BeginDictionary("scheduler_state");
787 state
->SetDouble("time_until_anticipated_draw_time_ms",
788 (AnticipatedDrawTime() - Now()).InMillisecondsF());
789 state
->SetDouble("estimated_parent_draw_time_ms",
790 estimated_parent_draw_time_
.InMillisecondsF());
791 state
->SetBoolean("last_set_needs_begin_frame_",
792 frame_source_
->NeedsBeginFrames());
793 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
794 state
->SetBoolean("begin_retro_frame_task_",
795 !begin_retro_frame_task_
.IsCancelled());
796 state
->SetBoolean("begin_impl_frame_deadline_task_",
797 !begin_impl_frame_deadline_task_
.IsCancelled());
798 state
->SetBoolean("advance_commit_state_task_",
799 !advance_commit_state_task_
.IsCancelled());
800 state
->BeginDictionary("begin_impl_frame_args");
801 begin_impl_frame_args_
.AsValueInto(state
);
802 state
->EndDictionary();
804 base::TimeTicks now
= Now();
805 base::TimeTicks frame_time
= begin_impl_frame_args_
.frame_time
;
806 base::TimeTicks deadline
= begin_impl_frame_args_
.deadline
;
807 base::TimeDelta interval
= begin_impl_frame_args_
.interval
;
808 state
->BeginDictionary("major_timestamps_in_ms");
809 state
->SetDouble("0_interval", interval
.InMillisecondsF());
810 state
->SetDouble("1_now_to_deadline", (deadline
- now
).InMillisecondsF());
811 state
->SetDouble("2_frame_time_to_now", (now
- frame_time
).InMillisecondsF());
812 state
->SetDouble("3_frame_time_to_deadline",
813 (deadline
- frame_time
).InMillisecondsF());
814 state
->SetDouble("4_now", (now
- base::TimeTicks()).InMillisecondsF());
815 state
->SetDouble("5_frame_time",
816 (frame_time
- base::TimeTicks()).InMillisecondsF());
817 state
->SetDouble("6_deadline",
818 (deadline
- base::TimeTicks()).InMillisecondsF());
819 state
->EndDictionary();
821 state
->EndDictionary();
823 state
->BeginDictionary("client_state");
824 state
->SetDouble("draw_duration_estimate_ms",
825 client_
->DrawDurationEstimate().InMillisecondsF());
827 "begin_main_frame_to_commit_duration_estimate_ms",
828 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
830 "commit_to_activate_duration_estimate_ms",
831 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
832 state
->EndDictionary();
835 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
836 // Check if the main thread computation and commit can be finished before the
837 // impl thread's deadline.
838 base::TimeTicks estimated_draw_time
=
839 begin_impl_frame_args_
.frame_time
+
840 client_
->BeginMainFrameToCommitDurationEstimate() +
841 client_
->CommitToActivateDurationEstimate();
844 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
845 "CanCommitAndActivateBeforeDeadline",
846 "time_left_after_drawing_ms",
847 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
851 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
854 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
855 return (state_machine_
.commit_state() ==
856 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
857 state_machine_
.commit_state() ==
858 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);