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::ConstructUnthrottledFrameSource(
53 Scheduler
* scheduler
) {
54 TRACE_EVENT1("cc", "Scheduler::Scheduler()", "UnthrottledFrameSource",
55 "BackToBackBeginFrameSource");
56 DCHECK(!scheduler
->unthrottled_frame_source_internal_
);
57 scheduler
->unthrottled_frame_source_internal_
=
58 BackToBackBeginFrameSource::Create(scheduler
->task_runner_
.get());
59 return scheduler
->unthrottled_frame_source_internal_
.get();
63 SchedulerClient
* client
,
64 const SchedulerSettings
& scheduler_settings
,
65 int layer_tree_host_id
,
66 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
67 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
68 SchedulerFrameSourcesConstructor
* frame_sources_constructor
)
70 primary_frame_source_(NULL
),
71 primary_frame_source_internal_(external_begin_frame_source
.Pass()),
72 vsync_observer_(NULL
),
73 authoritative_vsync_interval_(base::TimeDelta()),
74 last_vsync_timebase_(base::TimeTicks()),
75 throttle_frame_production_(false),
76 settings_(scheduler_settings
),
78 layer_tree_host_id_(layer_tree_host_id
),
79 task_runner_(task_runner
),
80 begin_impl_frame_deadline_mode_(
81 SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
),
82 state_machine_(scheduler_settings
),
83 inside_process_scheduled_actions_(false),
84 inside_action_(SchedulerStateMachine::ACTION_NONE
),
86 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
87 "Scheduler::Scheduler",
91 DCHECK(!state_machine_
.BeginFrameNeeded());
93 begin_retro_frame_closure_
=
94 base::Bind(&Scheduler::BeginRetroFrame
, weak_factory_
.GetWeakPtr());
95 begin_impl_frame_deadline_closure_
= base::Bind(
96 &Scheduler::OnBeginImplFrameDeadline
, weak_factory_
.GetWeakPtr());
97 advance_commit_state_closure_
= base::Bind(
98 &Scheduler::PollToAdvanceCommitState
, weak_factory_
.GetWeakPtr());
100 frame_source_
= BeginFrameSourceMultiplexer::Create();
101 frame_source_
->AddObserver(this);
103 // Primary frame source
104 primary_frame_source_
=
105 frame_sources_constructor
->ConstructPrimaryFrameSource(this);
106 frame_source_
->AddSource(primary_frame_source_
);
107 primary_frame_source_
->SetClientReady();
109 // Unthrottled frame source
110 unthrottled_frame_source_
=
111 frame_sources_constructor
->ConstructUnthrottledFrameSource(this);
112 frame_source_
->AddSource(unthrottled_frame_source_
);
114 SetThrottleFrameProduction(scheduler_settings
.throttle_frame_production
);
117 Scheduler::~Scheduler() {
118 if (frame_source_
->NeedsBeginFrames())
119 frame_source_
->SetNeedsBeginFrames(false);
122 base::TimeTicks
Scheduler::Now() const {
123 base::TimeTicks now
= gfx::FrameTime::Now();
124 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"),
131 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase
,
132 base::TimeDelta interval
) {
133 if (authoritative_vsync_interval_
!= base::TimeDelta()) {
134 interval
= authoritative_vsync_interval_
;
135 } else if (interval
== base::TimeDelta()) {
136 // TODO(brianderson): We should not be receiving 0 intervals.
137 interval
= BeginFrameArgs::DefaultInterval();
140 last_vsync_timebase_
= timebase
;
143 vsync_observer_
->OnUpdateVSyncParameters(timebase
, interval
);
146 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time
) {
147 DCHECK_GE(draw_time
.ToInternalValue(), 0);
148 estimated_parent_draw_time_
= draw_time
;
151 void Scheduler::SetCanStart() {
152 state_machine_
.SetCanStart();
153 ProcessScheduledActions();
156 void Scheduler::SetVisible(bool visible
) {
157 state_machine_
.SetVisible(visible
);
158 ProcessScheduledActions();
161 void Scheduler::SetCanDraw(bool can_draw
) {
162 state_machine_
.SetCanDraw(can_draw
);
163 ProcessScheduledActions();
166 void Scheduler::NotifyReadyToActivate() {
167 state_machine_
.NotifyReadyToActivate();
168 ProcessScheduledActions();
171 void Scheduler::NotifyReadyToDraw() {
172 // Future work might still needed for crbug.com/352894.
173 state_machine_
.NotifyReadyToDraw();
174 ProcessScheduledActions();
177 void Scheduler::SetThrottleFrameProduction(bool throttle
) {
178 throttle_frame_production_
= throttle
;
180 frame_source_
->SetActiveSource(primary_frame_source_
);
182 frame_source_
->SetActiveSource(unthrottled_frame_source_
);
184 ProcessScheduledActions();
187 void Scheduler::SetNeedsCommit() {
188 state_machine_
.SetNeedsCommit();
189 ProcessScheduledActions();
192 void Scheduler::SetNeedsRedraw() {
193 state_machine_
.SetNeedsRedraw();
194 ProcessScheduledActions();
197 void Scheduler::SetNeedsAnimate() {
198 state_machine_
.SetNeedsAnimate();
199 ProcessScheduledActions();
202 void Scheduler::SetNeedsPrepareTiles() {
203 DCHECK(!IsInsideAction(SchedulerStateMachine::ACTION_PREPARE_TILES
));
204 state_machine_
.SetNeedsPrepareTiles();
205 ProcessScheduledActions();
208 void Scheduler::SetWaitForReadyToDraw() {
209 state_machine_
.SetWaitForReadyToDraw();
210 ProcessScheduledActions();
213 void Scheduler::SetMaxSwapsPending(int max
) {
214 state_machine_
.SetMaxSwapsPending(max
);
217 void Scheduler::DidSwapBuffers() {
218 state_machine_
.DidSwapBuffers();
220 // There is no need to call ProcessScheduledActions here because
221 // swapping should not trigger any new actions.
222 if (!inside_process_scheduled_actions_
) {
223 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
227 void Scheduler::DidSwapBuffersComplete() {
228 state_machine_
.DidSwapBuffersComplete();
229 ProcessScheduledActions();
232 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
233 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
234 ProcessScheduledActions();
237 void Scheduler::NotifyReadyToCommit() {
238 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
239 state_machine_
.NotifyReadyToCommit();
240 ProcessScheduledActions();
243 void Scheduler::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
244 TRACE_EVENT1("cc", "Scheduler::BeginMainFrameAborted", "reason",
245 CommitEarlyOutReasonToString(reason
));
246 state_machine_
.BeginMainFrameAborted(reason
);
247 ProcessScheduledActions();
250 void Scheduler::DidPrepareTiles() {
251 state_machine_
.DidPrepareTiles();
254 void Scheduler::DidLoseOutputSurface() {
255 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
256 begin_retro_frame_args_
.clear();
257 begin_retro_frame_task_
.Cancel();
258 state_machine_
.DidLoseOutputSurface();
259 ProcessScheduledActions();
262 void Scheduler::DidCreateAndInitializeOutputSurface() {
263 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
264 DCHECK(!frame_source_
->NeedsBeginFrames());
265 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
266 state_machine_
.DidCreateAndInitializeOutputSurface();
267 ProcessScheduledActions();
270 void Scheduler::NotifyBeginMainFrameStarted() {
271 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
272 state_machine_
.NotifyBeginMainFrameStarted();
275 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
276 if (!frame_source_
->NeedsBeginFrames() ||
277 begin_impl_frame_args_
.interval
<= base::TimeDelta())
278 return base::TimeTicks();
280 base::TimeTicks now
= Now();
281 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
282 begin_impl_frame_args_
.deadline
);
283 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
284 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
287 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
288 return begin_impl_frame_args_
.frame_time
;
291 void Scheduler::SetupNextBeginFrameIfNeeded() {
292 // Never call SetNeedsBeginFrames if the frame source already has the right
294 if (frame_source_
->NeedsBeginFrames() != state_machine_
.BeginFrameNeeded()) {
295 if (state_machine_
.BeginFrameNeeded()) {
296 // Call SetNeedsBeginFrames(true) as soon as possible.
297 frame_source_
->SetNeedsBeginFrames(true);
298 } else if (state_machine_
.begin_impl_frame_state() ==
299 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
) {
300 // Call SetNeedsBeginFrames(false) in between frames only.
301 frame_source_
->SetNeedsBeginFrames(false);
302 client_
->SendBeginMainFrameNotExpectedSoon();
306 PostBeginRetroFrameIfNeeded();
309 // We may need to poll when we can't rely on BeginFrame to advance certain
310 // state or to avoid deadlock.
311 void Scheduler::SetupPollingMechanisms() {
312 // At this point we'd prefer to advance through the commit flow by
313 // drawing a frame, however it's possible that the frame rate controller
314 // will not give us a BeginFrame until the commit completes. See
315 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
316 // we set a repeating timer to poll on ProcessScheduledActions until we
317 // successfully reach BeginFrame. Synchronous compositor does not use
318 // frame rate controller or have the circular wait in the bug.
319 if (IsBeginMainFrameSentOrStarted() &&
320 !settings_
.using_synchronous_renderer_compositor
) {
321 if (advance_commit_state_task_
.IsCancelled() &&
322 begin_impl_frame_args_
.IsValid()) {
323 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
324 // set the interval to twice the interval from the previous frame.
325 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
326 task_runner_
->PostDelayedTask(FROM_HERE
,
327 advance_commit_state_task_
.callback(),
328 begin_impl_frame_args_
.interval
* 2);
331 advance_commit_state_task_
.Cancel();
335 // BeginFrame is the mechanism that tells us that now is a good time to start
336 // making a frame. Usually this means that user input for the frame is complete.
337 // If the scheduler is busy, we queue the BeginFrame to be handled later as
338 // a BeginRetroFrame.
339 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
340 TRACE_EVENT1("cc,benchmark", "Scheduler::BeginFrame", "args", args
.AsValue());
342 // Deliver BeginFrames to children.
343 if (state_machine_
.children_need_begin_frames()) {
344 BeginFrameArgs
adjusted_args_for_children(args
);
345 // Adjust a deadline for child schedulers.
346 // TODO(simonhong): Once we have commitless update, we can get rid of
347 // BeginMainFrameToCommitDurationEstimate() +
348 // CommitToActivateDurationEstimate().
349 adjusted_args_for_children
.deadline
-=
350 (client_
->BeginMainFrameToCommitDurationEstimate() +
351 client_
->CommitToActivateDurationEstimate() +
352 client_
->DrawDurationEstimate() + EstimatedParentDrawTime());
353 client_
->SendBeginFramesToChildren(adjusted_args_for_children
);
356 BeginFrameArgs
adjusted_args(args
);
357 adjusted_args
.deadline
-= EstimatedParentDrawTime();
359 if (settings_
.using_synchronous_renderer_compositor
) {
360 BeginImplFrameSynchronous(adjusted_args
);
364 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
365 // sent us the last BeginFrame we have missed. As we might not be able to
366 // actually make rendering for this call, handle it like a "retro frame".
367 // TODO(brainderson): Add a test for this functionality ASAP!
368 if (adjusted_args
.type
== BeginFrameArgs::MISSED
) {
369 begin_retro_frame_args_
.push_back(adjusted_args
);
370 PostBeginRetroFrameIfNeeded();
374 bool should_defer_begin_frame
=
375 !begin_retro_frame_args_
.empty() ||
376 !begin_retro_frame_task_
.IsCancelled() ||
377 !frame_source_
->NeedsBeginFrames() ||
378 (state_machine_
.begin_impl_frame_state() !=
379 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
381 if (should_defer_begin_frame
) {
382 begin_retro_frame_args_
.push_back(adjusted_args
);
383 TRACE_EVENT_INSTANT0(
384 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
385 // Queuing the frame counts as "using it", so we need to return true.
387 BeginImplFrameWithDeadline(adjusted_args
);
392 void Scheduler::SetChildrenNeedBeginFrames(bool children_need_begin_frames
) {
393 state_machine_
.SetChildrenNeedBeginFrames(children_need_begin_frames
);
394 ProcessScheduledActions();
397 void Scheduler::SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
) {
398 authoritative_vsync_interval_
= interval
;
400 vsync_observer_
->OnUpdateVSyncParameters(last_vsync_timebase_
, interval
);
403 void Scheduler::OnDrawForOutputSurface() {
404 DCHECK(settings_
.using_synchronous_renderer_compositor
);
405 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
406 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
407 DCHECK(!BeginImplFrameDeadlinePending());
409 state_machine_
.OnBeginImplFrameDeadline();
410 ProcessScheduledActions();
412 state_machine_
.OnBeginImplFrameIdle();
413 ProcessScheduledActions();
416 // BeginRetroFrame is called for BeginFrames that we've deferred because
417 // the scheduler was in the middle of processing a previous BeginFrame.
418 void Scheduler::BeginRetroFrame() {
419 TRACE_EVENT0("cc,benchmark", "Scheduler::BeginRetroFrame");
420 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
421 DCHECK(!begin_retro_frame_args_
.empty());
422 DCHECK(!begin_retro_frame_task_
.IsCancelled());
423 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
424 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
426 begin_retro_frame_task_
.Cancel();
428 // Discard expired BeginRetroFrames
429 // Today, we should always end up with at most one un-expired BeginRetroFrame
430 // because deadlines will not be greater than the next frame time. We don't
431 // DCHECK though because some systems don't always have monotonic timestamps.
432 // TODO(brianderson): In the future, long deadlines could result in us not
433 // draining the queue if we don't catch up. If we consistently can't catch
434 // up, our fallback should be to lower our frame rate.
435 base::TimeTicks now
= Now();
437 while (!begin_retro_frame_args_
.empty()) {
438 const BeginFrameArgs
& args
= begin_retro_frame_args_
.front();
439 base::TimeTicks expiration_time
= args
.frame_time
+ args
.interval
;
440 if (now
<= expiration_time
)
442 TRACE_EVENT_INSTANT2(
443 "cc", "Scheduler::BeginRetroFrame discarding", TRACE_EVENT_SCOPE_THREAD
,
444 "expiration_time - now", (expiration_time
- now
).InMillisecondsF(),
445 "BeginFrameArgs", begin_retro_frame_args_
.front().AsValue());
446 begin_retro_frame_args_
.pop_front();
447 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
450 if (begin_retro_frame_args_
.empty()) {
451 TRACE_EVENT_INSTANT0("cc",
452 "Scheduler::BeginRetroFrames all expired",
453 TRACE_EVENT_SCOPE_THREAD
);
455 BeginFrameArgs front
= begin_retro_frame_args_
.front();
456 begin_retro_frame_args_
.pop_front();
457 BeginImplFrameWithDeadline(front
);
461 // There could be a race between the posted BeginRetroFrame and a new
462 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
463 // will check if there is a pending BeginRetroFrame to ensure we handle
464 // BeginFrames in FIFO order.
465 void Scheduler::PostBeginRetroFrameIfNeeded() {
466 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
467 "Scheduler::PostBeginRetroFrameIfNeeded",
470 if (!frame_source_
->NeedsBeginFrames())
473 if (begin_retro_frame_args_
.empty() || !begin_retro_frame_task_
.IsCancelled())
476 // begin_retro_frame_args_ should always be empty for the
477 // synchronous compositor.
478 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
480 if (state_machine_
.begin_impl_frame_state() !=
481 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
484 begin_retro_frame_task_
.Reset(begin_retro_frame_closure_
);
486 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_task_
.callback());
489 void Scheduler::BeginImplFrameWithDeadline(const BeginFrameArgs
& args
) {
490 BeginImplFrame(args
);
492 // The deadline will be scheduled in ProcessScheduledActions.
493 state_machine_
.OnBeginImplFrameDeadlinePending();
494 ProcessScheduledActions();
497 void Scheduler::BeginImplFrameSynchronous(const BeginFrameArgs
& args
) {
498 BeginImplFrame(args
);
502 void Scheduler::FinishImplFrame() {
503 state_machine_
.OnBeginImplFrameIdle();
504 ProcessScheduledActions();
506 client_
->DidBeginImplFrameDeadline();
507 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
510 // BeginImplFrame starts a compositor frame that will wait up until a deadline
511 // for a BeginMainFrame+activation to complete before it times out and draws
512 // any asynchronous animation and scroll/pinch updates.
513 void Scheduler::BeginImplFrame(const BeginFrameArgs
& args
) {
514 bool main_thread_is_in_high_latency_mode
=
515 state_machine_
.MainThreadIsInHighLatencyMode();
516 TRACE_EVENT2("cc,benchmark",
517 "Scheduler::BeginImplFrame",
520 "main_thread_is_high_latency",
521 main_thread_is_in_high_latency_mode
);
522 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
524 main_thread_is_in_high_latency_mode
);
525 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
526 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
527 DCHECK(!BeginImplFrameDeadlinePending());
528 DCHECK(state_machine_
.HasInitializedOutputSurface());
530 advance_commit_state_task_
.Cancel();
532 begin_impl_frame_args_
= args
;
533 begin_impl_frame_args_
.deadline
-= client_
->DrawDurationEstimate();
535 if (!state_machine_
.impl_latency_takes_priority() &&
536 main_thread_is_in_high_latency_mode
&&
537 CanCommitAndActivateBeforeDeadline()) {
538 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
541 state_machine_
.OnBeginImplFrame();
542 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
543 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
545 ProcessScheduledActions();
548 void Scheduler::ScheduleBeginImplFrameDeadline() {
549 // The synchronous compositor does not post a deadline task.
550 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
552 begin_impl_frame_deadline_task_
.Cancel();
553 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
555 begin_impl_frame_deadline_mode_
=
556 state_machine_
.CurrentBeginImplFrameDeadlineMode();
558 base::TimeTicks deadline
;
559 switch (begin_impl_frame_deadline_mode_
) {
560 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
:
563 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
564 // We are ready to draw a new active tree immediately.
565 // We don't use Now() here because it's somewhat expensive to call.
566 deadline
= base::TimeTicks();
568 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
569 // We are animating on the impl thread but we can wait for some time.
570 deadline
= begin_impl_frame_args_
.deadline
;
572 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
573 // We are blocked for one reason or another and we should wait.
574 // TODO(brianderson): Handle long deadlines (that are past the next
575 // frame's frame time) properly instead of using this hack.
577 begin_impl_frame_args_
.frame_time
+ begin_impl_frame_args_
.interval
;
579 case SchedulerStateMachine::
580 BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
:
581 // We are blocked because we are waiting for ReadyToDraw signal. We would
582 // post deadline after we received ReadyToDraw singal.
583 TRACE_EVENT1("cc", "Scheduler::ScheduleBeginImplFrameDeadline",
584 "deadline_mode", "blocked_on_ready_to_draw");
588 TRACE_EVENT2("cc", "Scheduler::ScheduleBeginImplFrameDeadline", "mode",
589 SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
590 begin_impl_frame_deadline_mode_
),
591 "deadline", deadline
);
593 base::TimeDelta delta
= std::max(deadline
- Now(), base::TimeDelta());
594 task_runner_
->PostDelayedTask(
595 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
598 void Scheduler::ScheduleBeginImplFrameDeadlineIfNeeded() {
599 if (settings_
.using_synchronous_renderer_compositor
)
602 if (state_machine_
.begin_impl_frame_state() !=
603 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
606 if (begin_impl_frame_deadline_mode_
==
607 state_machine_
.CurrentBeginImplFrameDeadlineMode() &&
608 BeginImplFrameDeadlinePending())
611 ScheduleBeginImplFrameDeadline();
614 void Scheduler::OnBeginImplFrameDeadline() {
615 TRACE_EVENT0("cc,benchmark", "Scheduler::OnBeginImplFrameDeadline");
616 begin_impl_frame_deadline_task_
.Cancel();
617 // We split the deadline actions up into two phases so the state machine
618 // has a chance to trigger actions that should occur durring and after
619 // the deadline separately. For example:
620 // * Sending the BeginMainFrame will not occur after the deadline in
621 // order to wait for more user-input before starting the next commit.
622 // * Creating a new OuputSurface will not occur during the deadline in
623 // order to allow the state machine to "settle" first.
625 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
626 tracked_objects::ScopedTracker
tracking_profile1(
627 FROM_HERE_WITH_EXPLICIT_FUNCTION(
628 "461509 Scheduler::OnBeginImplFrameDeadline1"));
629 state_machine_
.OnBeginImplFrameDeadline();
630 ProcessScheduledActions();
635 void Scheduler::PollToAdvanceCommitState() {
636 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
637 advance_commit_state_task_
.Cancel();
638 ProcessScheduledActions();
641 void Scheduler::DrawAndSwapIfPossible() {
642 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
643 state_machine_
.DidDrawIfPossibleCompleted(result
);
646 void Scheduler::SetDeferCommits(bool defer_commits
) {
647 TRACE_EVENT1("cc", "Scheduler::SetDeferCommits",
650 state_machine_
.SetDeferCommits(defer_commits
);
651 ProcessScheduledActions();
654 void Scheduler::ProcessScheduledActions() {
655 // We do not allow ProcessScheduledActions to be recursive.
656 // The top-level call will iteratively execute the next action for us anyway.
657 if (inside_process_scheduled_actions_
)
660 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
662 SchedulerStateMachine::Action action
;
664 action
= state_machine_
.NextAction();
665 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
666 "SchedulerStateMachine",
669 VLOG(2) << "Scheduler::ProcessScheduledActions: "
670 << SchedulerStateMachine::ActionToString(action
) << " "
671 << state_machine_
.GetStatesForDebugging();
672 state_machine_
.UpdateState(action
);
673 base::AutoReset
<SchedulerStateMachine::Action
>
674 mark_inside_action(&inside_action_
, action
);
676 case SchedulerStateMachine::ACTION_NONE
:
678 case SchedulerStateMachine::ACTION_ANIMATE
:
679 client_
->ScheduledActionAnimate();
681 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
:
682 client_
->ScheduledActionSendBeginMainFrame();
684 case SchedulerStateMachine::ACTION_COMMIT
: {
685 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
687 tracked_objects::ScopedTracker
tracking_profile4(
688 FROM_HERE_WITH_EXPLICIT_FUNCTION(
689 "461509 Scheduler::ProcessScheduledActions4"));
690 client_
->ScheduledActionCommit();
693 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
:
694 client_
->ScheduledActionActivateSyncTree();
696 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
697 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
699 tracked_objects::ScopedTracker
tracking_profile6(
700 FROM_HERE_WITH_EXPLICIT_FUNCTION(
701 "461509 Scheduler::ProcessScheduledActions6"));
702 DrawAndSwapIfPossible();
705 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
:
706 client_
->ScheduledActionDrawAndSwapForced();
708 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
709 // No action is actually performed, but this allows the state machine to
710 // advance out of its waiting to draw state without actually drawing.
712 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
713 client_
->ScheduledActionBeginOutputSurfaceCreation();
715 case SchedulerStateMachine::ACTION_PREPARE_TILES
:
716 client_
->ScheduledActionPrepareTiles();
718 case SchedulerStateMachine::ACTION_INVALIDATE_OUTPUT_SURFACE
: {
719 client_
->ScheduledActionInvalidateOutputSurface();
723 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
725 SetupPollingMechanisms();
727 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
729 ScheduleBeginImplFrameDeadlineIfNeeded();
731 SetupNextBeginFrameIfNeeded();
734 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> Scheduler::AsValue()
736 scoped_refptr
<base::trace_event::TracedValue
> state
=
737 new base::trace_event::TracedValue();
738 AsValueInto(state
.get());
742 void Scheduler::AsValueInto(base::trace_event::TracedValue
* state
) const {
743 state
->BeginDictionary("state_machine");
744 state_machine_
.AsValueInto(state
);
745 state
->EndDictionary();
747 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
748 bool frame_tracing_enabled
= false;
749 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
750 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
751 &frame_tracing_enabled
);
752 if (frame_tracing_enabled
) {
753 state
->BeginDictionary("frame_source_");
754 frame_source_
->AsValueInto(state
);
755 state
->EndDictionary();
758 state
->BeginDictionary("scheduler_state");
759 state
->SetDouble("time_until_anticipated_draw_time_ms",
760 (AnticipatedDrawTime() - Now()).InMillisecondsF());
761 state
->SetDouble("estimated_parent_draw_time_ms",
762 estimated_parent_draw_time_
.InMillisecondsF());
763 state
->SetBoolean("last_set_needs_begin_frame_",
764 frame_source_
->NeedsBeginFrames());
765 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
766 state
->SetBoolean("begin_retro_frame_task_",
767 !begin_retro_frame_task_
.IsCancelled());
768 state
->SetBoolean("begin_impl_frame_deadline_task_",
769 !begin_impl_frame_deadline_task_
.IsCancelled());
770 state
->SetBoolean("advance_commit_state_task_",
771 !advance_commit_state_task_
.IsCancelled());
772 state
->BeginDictionary("begin_impl_frame_args");
773 begin_impl_frame_args_
.AsValueInto(state
);
774 state
->EndDictionary();
776 base::TimeTicks now
= Now();
777 base::TimeTicks frame_time
= begin_impl_frame_args_
.frame_time
;
778 base::TimeTicks deadline
= begin_impl_frame_args_
.deadline
;
779 base::TimeDelta interval
= begin_impl_frame_args_
.interval
;
780 state
->BeginDictionary("major_timestamps_in_ms");
781 state
->SetDouble("0_interval", interval
.InMillisecondsF());
782 state
->SetDouble("1_now_to_deadline", (deadline
- now
).InMillisecondsF());
783 state
->SetDouble("2_frame_time_to_now", (now
- frame_time
).InMillisecondsF());
784 state
->SetDouble("3_frame_time_to_deadline",
785 (deadline
- frame_time
).InMillisecondsF());
786 state
->SetDouble("4_now", (now
- base::TimeTicks()).InMillisecondsF());
787 state
->SetDouble("5_frame_time",
788 (frame_time
- base::TimeTicks()).InMillisecondsF());
789 state
->SetDouble("6_deadline",
790 (deadline
- base::TimeTicks()).InMillisecondsF());
791 state
->EndDictionary();
793 state
->EndDictionary();
795 state
->BeginDictionary("client_state");
796 state
->SetDouble("draw_duration_estimate_ms",
797 client_
->DrawDurationEstimate().InMillisecondsF());
799 "begin_main_frame_to_commit_duration_estimate_ms",
800 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
802 "commit_to_activate_duration_estimate_ms",
803 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
804 state
->EndDictionary();
807 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
808 // Check if the main thread computation and commit can be finished before the
809 // impl thread's deadline.
810 base::TimeTicks estimated_draw_time
=
811 begin_impl_frame_args_
.frame_time
+
812 client_
->BeginMainFrameToCommitDurationEstimate() +
813 client_
->CommitToActivateDurationEstimate();
816 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
817 "CanCommitAndActivateBeforeDeadline",
818 "time_left_after_drawing_ms",
819 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
823 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
826 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
827 return (state_machine_
.commit_state() ==
828 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
829 state_machine_
.commit_state() ==
830 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);