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 throttle_frame_production_(scheduler_settings
.throttle_frame_production
),
91 settings_(scheduler_settings
),
93 layer_tree_host_id_(layer_tree_host_id
),
94 task_runner_(task_runner
),
95 state_machine_(scheduler_settings
),
96 inside_process_scheduled_actions_(false),
97 inside_action_(SchedulerStateMachine::ACTION_NONE
),
99 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
100 "Scheduler::Scheduler",
102 settings_
.AsValue());
104 DCHECK(!state_machine_
.BeginFrameNeeded());
106 begin_retro_frame_closure_
=
107 base::Bind(&Scheduler::BeginRetroFrame
, weak_factory_
.GetWeakPtr());
108 begin_impl_frame_deadline_closure_
= base::Bind(
109 &Scheduler::OnBeginImplFrameDeadline
, weak_factory_
.GetWeakPtr());
110 poll_for_draw_triggers_closure_
= base::Bind(
111 &Scheduler::PollForAnticipatedDrawTriggers
, weak_factory_
.GetWeakPtr());
112 advance_commit_state_closure_
= base::Bind(
113 &Scheduler::PollToAdvanceCommitState
, weak_factory_
.GetWeakPtr());
115 frame_source_
= BeginFrameSourceMultiplexer::Create();
116 frame_source_
->AddObserver(this);
118 // Primary frame source
119 primary_frame_source_
=
120 frame_sources_constructor
->ConstructPrimaryFrameSource(this);
121 frame_source_
->AddSource(primary_frame_source_
);
122 primary_frame_source_
->SetClientReady();
124 // Background ticking frame source
125 background_frame_source_
=
126 frame_sources_constructor
->ConstructBackgroundFrameSource(this);
127 frame_source_
->AddSource(background_frame_source_
);
129 // Unthrottled frame source
130 unthrottled_frame_source_
=
131 frame_sources_constructor
->ConstructUnthrottledFrameSource(this);
132 frame_source_
->AddSource(unthrottled_frame_source_
);
135 Scheduler::~Scheduler() {
136 if (frame_source_
->NeedsBeginFrames())
137 frame_source_
->SetNeedsBeginFrames(false);
140 base::TimeTicks
Scheduler::Now() const {
141 base::TimeTicks now
= gfx::FrameTime::Now();
142 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"),
149 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase
,
150 base::TimeDelta interval
) {
151 // TODO(brianderson): We should not be receiving 0 intervals.
152 if (interval
== base::TimeDelta())
153 interval
= BeginFrameArgs::DefaultInterval();
156 vsync_observer_
->OnUpdateVSyncParameters(timebase
, interval
);
159 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time
) {
160 DCHECK_GE(draw_time
.ToInternalValue(), 0);
161 estimated_parent_draw_time_
= draw_time
;
164 void Scheduler::SetCanStart() {
165 state_machine_
.SetCanStart();
166 ProcessScheduledActions();
169 void Scheduler::UpdateActiveFrameSource() {
170 if (state_machine_
.visible()) {
171 if (throttle_frame_production_
) {
172 frame_source_
->SetActiveSource(primary_frame_source_
);
174 frame_source_
->SetActiveSource(unthrottled_frame_source_
);
177 frame_source_
->SetActiveSource(background_frame_source_
);
179 ProcessScheduledActions();
182 void Scheduler::SetVisible(bool visible
) {
183 state_machine_
.SetVisible(visible
);
184 UpdateActiveFrameSource();
187 void Scheduler::SetCanDraw(bool can_draw
) {
188 state_machine_
.SetCanDraw(can_draw
);
189 ProcessScheduledActions();
192 void Scheduler::NotifyReadyToActivate() {
193 state_machine_
.NotifyReadyToActivate();
194 ProcessScheduledActions();
197 void Scheduler::NotifyReadyToDraw() {
198 // Empty for now, until we take action based on the notification as part of
199 // crbugs 352894, 383157, 421923.
202 void Scheduler::SetThrottleFrameProduction(bool throttle
) {
203 throttle_frame_production_
= throttle
;
204 UpdateActiveFrameSource();
207 void Scheduler::SetNeedsCommit() {
208 state_machine_
.SetNeedsCommit();
209 ProcessScheduledActions();
212 void Scheduler::SetNeedsRedraw() {
213 state_machine_
.SetNeedsRedraw();
214 ProcessScheduledActions();
217 void Scheduler::SetNeedsAnimate() {
218 state_machine_
.SetNeedsAnimate();
219 ProcessScheduledActions();
222 void Scheduler::SetNeedsPrepareTiles() {
223 DCHECK(!IsInsideAction(SchedulerStateMachine::ACTION_PREPARE_TILES
));
224 state_machine_
.SetNeedsPrepareTiles();
225 ProcessScheduledActions();
228 void Scheduler::SetMaxSwapsPending(int max
) {
229 state_machine_
.SetMaxSwapsPending(max
);
232 void Scheduler::DidSwapBuffers() {
233 state_machine_
.DidSwapBuffers();
235 // There is no need to call ProcessScheduledActions here because
236 // swapping should not trigger any new actions.
237 if (!inside_process_scheduled_actions_
) {
238 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
242 void Scheduler::DidSwapBuffersComplete() {
243 state_machine_
.DidSwapBuffersComplete();
244 ProcessScheduledActions();
247 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
248 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
249 ProcessScheduledActions();
252 void Scheduler::NotifyReadyToCommit() {
253 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
254 state_machine_
.NotifyReadyToCommit();
255 ProcessScheduledActions();
258 void Scheduler::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
259 TRACE_EVENT1("cc", "Scheduler::BeginMainFrameAborted", "reason",
260 CommitEarlyOutReasonToString(reason
));
261 state_machine_
.BeginMainFrameAborted(reason
);
262 ProcessScheduledActions();
265 void Scheduler::DidPrepareTiles() {
266 state_machine_
.DidPrepareTiles();
269 void Scheduler::DidLoseOutputSurface() {
270 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
271 begin_retro_frame_args_
.clear();
272 begin_retro_frame_task_
.Cancel();
273 state_machine_
.DidLoseOutputSurface();
274 ProcessScheduledActions();
277 void Scheduler::DidCreateAndInitializeOutputSurface() {
278 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
279 DCHECK(!frame_source_
->NeedsBeginFrames());
280 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
281 state_machine_
.DidCreateAndInitializeOutputSurface();
282 ProcessScheduledActions();
285 void Scheduler::NotifyBeginMainFrameStarted() {
286 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
287 state_machine_
.NotifyBeginMainFrameStarted();
290 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
291 if (!frame_source_
->NeedsBeginFrames() ||
292 begin_impl_frame_args_
.interval
<= base::TimeDelta())
293 return base::TimeTicks();
295 base::TimeTicks now
= Now();
296 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
297 begin_impl_frame_args_
.deadline
);
298 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
299 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
302 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
303 return begin_impl_frame_args_
.frame_time
;
306 void Scheduler::SetupNextBeginFrameIfNeeded() {
307 // Never call SetNeedsBeginFrames if the frame source already has the right
309 if (frame_source_
->NeedsBeginFrames() != state_machine_
.BeginFrameNeeded()) {
310 if (state_machine_
.BeginFrameNeeded()) {
311 // Call SetNeedsBeginFrames(true) as soon as possible.
312 frame_source_
->SetNeedsBeginFrames(true);
313 } else if (state_machine_
.begin_impl_frame_state() ==
314 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
) {
315 // Call SetNeedsBeginFrames(false) in between frames only.
316 frame_source_
->SetNeedsBeginFrames(false);
317 client_
->SendBeginMainFrameNotExpectedSoon();
321 PostBeginRetroFrameIfNeeded();
324 // We may need to poll when we can't rely on BeginFrame to advance certain
325 // state or to avoid deadlock.
326 void Scheduler::SetupPollingMechanisms() {
327 bool needs_advance_commit_state_timer
= false;
328 // Setup PollForAnticipatedDrawTriggers if we need to monitor state but
329 // aren't expecting any more BeginFrames. This should only be needed by
330 // the synchronous compositor when BeginFrameNeeded is false.
331 if (state_machine_
.ShouldPollForAnticipatedDrawTriggers()) {
332 DCHECK(!state_machine_
.SupportsProactiveBeginFrame());
333 if (poll_for_draw_triggers_task_
.IsCancelled()) {
334 poll_for_draw_triggers_task_
.Reset(poll_for_draw_triggers_closure_
);
335 base::TimeDelta delay
= begin_impl_frame_args_
.IsValid()
336 ? begin_impl_frame_args_
.interval
337 : BeginFrameArgs::DefaultInterval();
338 task_runner_
->PostDelayedTask(
339 FROM_HERE
, poll_for_draw_triggers_task_
.callback(), delay
);
342 poll_for_draw_triggers_task_
.Cancel();
344 // At this point we'd prefer to advance through the commit flow by
345 // drawing a frame, however it's possible that the frame rate controller
346 // will not give us a BeginFrame until the commit completes. See
347 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
348 // we set a repeating timer to poll on ProcessScheduledActions until we
349 // successfully reach BeginFrame. Synchronous compositor does not use
350 // frame rate controller or have the circular wait in the bug.
351 if (IsBeginMainFrameSentOrStarted() &&
352 !settings_
.using_synchronous_renderer_compositor
) {
353 needs_advance_commit_state_timer
= true;
357 if (needs_advance_commit_state_timer
) {
358 if (advance_commit_state_task_
.IsCancelled() &&
359 begin_impl_frame_args_
.IsValid()) {
360 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
361 // set the interval to twice the interval from the previous frame.
362 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
363 task_runner_
->PostDelayedTask(FROM_HERE
,
364 advance_commit_state_task_
.callback(),
365 begin_impl_frame_args_
.interval
* 2);
368 advance_commit_state_task_
.Cancel();
372 // BeginFrame is the mechanism that tells us that now is a good time to start
373 // making a frame. Usually this means that user input for the frame is complete.
374 // If the scheduler is busy, we queue the BeginFrame to be handled later as
375 // a BeginRetroFrame.
376 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
377 TRACE_EVENT1("cc,benchmark", "Scheduler::BeginFrame", "args", args
.AsValue());
379 // Deliver BeginFrames to children.
380 if (state_machine_
.children_need_begin_frames()) {
381 BeginFrameArgs
adjusted_args_for_children(args
);
382 // Adjust a deadline for child schedulers.
383 // TODO(simonhong): Once we have commitless update, we can get rid of
384 // BeginMainFrameToCommitDurationEstimate() +
385 // CommitToActivateDurationEstimate().
386 adjusted_args_for_children
.deadline
-=
387 (client_
->BeginMainFrameToCommitDurationEstimate() +
388 client_
->CommitToActivateDurationEstimate() +
389 client_
->DrawDurationEstimate() + EstimatedParentDrawTime());
390 client_
->SendBeginFramesToChildren(adjusted_args_for_children
);
393 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
394 // sent us the last BeginFrame we have missed. As we might not be able to
395 // actually make rendering for this call, handle it like a "retro frame".
396 // TODO(brainderson): Add a test for this functionality ASAP!
397 if (args
.type
== BeginFrameArgs::MISSED
) {
398 begin_retro_frame_args_
.push_back(args
);
399 PostBeginRetroFrameIfNeeded();
403 BeginFrameArgs
adjusted_args(args
);
404 adjusted_args
.deadline
-= EstimatedParentDrawTime();
406 bool should_defer_begin_frame
;
407 if (settings_
.using_synchronous_renderer_compositor
) {
408 should_defer_begin_frame
= false;
410 should_defer_begin_frame
=
411 !begin_retro_frame_args_
.empty() ||
412 !begin_retro_frame_task_
.IsCancelled() ||
413 !frame_source_
->NeedsBeginFrames() ||
414 (state_machine_
.begin_impl_frame_state() !=
415 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
418 if (should_defer_begin_frame
) {
419 begin_retro_frame_args_
.push_back(adjusted_args
);
420 TRACE_EVENT_INSTANT0(
421 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
422 // Queuing the frame counts as "using it", so we need to return true.
424 BeginImplFrame(adjusted_args
);
429 void Scheduler::SetChildrenNeedBeginFrames(bool children_need_begin_frames
) {
430 state_machine_
.SetChildrenNeedBeginFrames(children_need_begin_frames
);
431 ProcessScheduledActions();
434 // BeginRetroFrame is called for BeginFrames that we've deferred because
435 // the scheduler was in the middle of processing a previous BeginFrame.
436 void Scheduler::BeginRetroFrame() {
437 TRACE_EVENT0("cc,benchmark", "Scheduler::BeginRetroFrame");
438 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
439 DCHECK(!begin_retro_frame_args_
.empty());
440 DCHECK(!begin_retro_frame_task_
.IsCancelled());
441 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
442 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
444 begin_retro_frame_task_
.Cancel();
446 // Discard expired BeginRetroFrames
447 // Today, we should always end up with at most one un-expired BeginRetroFrame
448 // because deadlines will not be greater than the next frame time. We don't
449 // DCHECK though because some systems don't always have monotonic timestamps.
450 // TODO(brianderson): In the future, long deadlines could result in us not
451 // draining the queue if we don't catch up. If we consistently can't catch
452 // up, our fallback should be to lower our frame rate.
453 base::TimeTicks now
= Now();
455 while (!begin_retro_frame_args_
.empty()) {
456 const BeginFrameArgs
& args
= begin_retro_frame_args_
.front();
457 base::TimeTicks expiration_time
= args
.frame_time
+ args
.interval
;
458 if (now
<= expiration_time
)
460 TRACE_EVENT_INSTANT2(
461 "cc", "Scheduler::BeginRetroFrame discarding", TRACE_EVENT_SCOPE_THREAD
,
462 "expiration_time - now", (expiration_time
- now
).InMillisecondsF(),
463 "BeginFrameArgs", begin_retro_frame_args_
.front().AsValue());
464 begin_retro_frame_args_
.pop_front();
465 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
468 if (begin_retro_frame_args_
.empty()) {
469 TRACE_EVENT_INSTANT0("cc",
470 "Scheduler::BeginRetroFrames all expired",
471 TRACE_EVENT_SCOPE_THREAD
);
473 BeginFrameArgs front
= begin_retro_frame_args_
.front();
474 begin_retro_frame_args_
.pop_front();
475 BeginImplFrame(front
);
479 // There could be a race between the posted BeginRetroFrame and a new
480 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
481 // will check if there is a pending BeginRetroFrame to ensure we handle
482 // BeginFrames in FIFO order.
483 void Scheduler::PostBeginRetroFrameIfNeeded() {
484 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
485 "Scheduler::PostBeginRetroFrameIfNeeded",
488 if (!frame_source_
->NeedsBeginFrames())
491 if (begin_retro_frame_args_
.empty() || !begin_retro_frame_task_
.IsCancelled())
494 // begin_retro_frame_args_ should always be empty for the
495 // synchronous compositor.
496 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
498 if (state_machine_
.begin_impl_frame_state() !=
499 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
502 begin_retro_frame_task_
.Reset(begin_retro_frame_closure_
);
504 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_task_
.callback());
507 // BeginImplFrame starts a compositor frame that will wait up until a deadline
508 // for a BeginMainFrame+activation to complete before it times out and draws
509 // any asynchronous animation and scroll/pinch updates.
510 void Scheduler::BeginImplFrame(const BeginFrameArgs
& args
) {
511 bool main_thread_is_in_high_latency_mode
=
512 state_machine_
.MainThreadIsInHighLatencyMode();
513 TRACE_EVENT2("cc,benchmark",
514 "Scheduler::BeginImplFrame",
517 "main_thread_is_high_latency",
518 main_thread_is_in_high_latency_mode
);
519 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
521 main_thread_is_in_high_latency_mode
);
522 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
523 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
524 DCHECK(state_machine_
.HasInitializedOutputSurface());
526 advance_commit_state_task_
.Cancel();
528 begin_impl_frame_args_
= args
;
529 begin_impl_frame_args_
.deadline
-= client_
->DrawDurationEstimate();
531 if (!state_machine_
.impl_latency_takes_priority() &&
532 main_thread_is_in_high_latency_mode
&&
533 CanCommitAndActivateBeforeDeadline()) {
534 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
537 state_machine_
.OnBeginImplFrame();
538 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
539 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
541 ProcessScheduledActions();
543 state_machine_
.OnBeginImplFrameDeadlinePending();
545 if (settings_
.using_synchronous_renderer_compositor
) {
546 // The synchronous renderer compositor has to make its GL calls
548 // TODO(brianderson): Have the OutputSurface initiate the deadline tasks
549 // so the synchronous renderer compositor can take advantage of splitting
550 // up the BeginImplFrame and deadline as well.
551 OnBeginImplFrameDeadline();
553 ScheduleBeginImplFrameDeadline();
557 void Scheduler::ScheduleBeginImplFrameDeadline() {
558 // The synchronous compositor does not post a deadline task.
559 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
561 begin_impl_frame_deadline_task_
.Cancel();
562 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
564 begin_impl_frame_deadline_mode_
=
565 state_machine_
.CurrentBeginImplFrameDeadlineMode();
567 base::TimeTicks deadline
;
568 switch (begin_impl_frame_deadline_mode_
) {
569 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
570 // We are ready to draw a new active tree immediately.
571 // We don't use Now() here because it's somewhat expensive to call.
572 deadline
= base::TimeTicks();
574 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
575 // We are animating on the impl thread but we can wait for some time.
576 deadline
= begin_impl_frame_args_
.deadline
;
578 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
579 // We are blocked for one reason or another and we should wait.
580 // TODO(brianderson): Handle long deadlines (that are past the next
581 // frame's frame time) properly instead of using this hack.
583 begin_impl_frame_args_
.frame_time
+ begin_impl_frame_args_
.interval
;
588 "cc", "Scheduler::ScheduleBeginImplFrameDeadline", "deadline", deadline
);
590 base::TimeDelta delta
= deadline
- Now();
591 if (delta
<= base::TimeDelta())
592 delta
= base::TimeDelta();
593 task_runner_
->PostDelayedTask(
594 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
597 void Scheduler::RescheduleBeginImplFrameDeadlineIfNeeded() {
598 if (settings_
.using_synchronous_renderer_compositor
)
601 if (state_machine_
.begin_impl_frame_state() !=
602 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
605 if (begin_impl_frame_deadline_mode_
!=
606 state_machine_
.CurrentBeginImplFrameDeadlineMode())
607 ScheduleBeginImplFrameDeadline();
610 void Scheduler::OnBeginImplFrameDeadline() {
611 TRACE_EVENT0("cc,benchmark", "Scheduler::OnBeginImplFrameDeadline");
612 begin_impl_frame_deadline_task_
.Cancel();
613 // We split the deadline actions up into two phases so the state machine
614 // has a chance to trigger actions that should occur durring and after
615 // the deadline separately. For example:
616 // * Sending the BeginMainFrame will not occur after the deadline in
617 // order to wait for more user-input before starting the next commit.
618 // * Creating a new OuputSurface will not occur during the deadline in
619 // order to allow the state machine to "settle" first.
621 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
622 tracked_objects::ScopedTracker
tracking_profile1(
623 FROM_HERE_WITH_EXPLICIT_FUNCTION(
624 "461509 Scheduler::OnBeginImplFrameDeadline1"));
625 state_machine_
.OnBeginImplFrameDeadline();
626 ProcessScheduledActions();
628 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
629 tracked_objects::ScopedTracker
tracking_profile2(
630 FROM_HERE_WITH_EXPLICIT_FUNCTION(
631 "461509 Scheduler::OnBeginImplFrameDeadline2"));
632 state_machine_
.OnBeginImplFrameIdle();
633 ProcessScheduledActions();
635 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
636 tracked_objects::ScopedTracker
tracking_profile3(
637 FROM_HERE_WITH_EXPLICIT_FUNCTION(
638 "461509 Scheduler::OnBeginImplFrameDeadline3"));
639 client_
->DidBeginImplFrameDeadline();
640 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
643 void Scheduler::PollForAnticipatedDrawTriggers() {
644 TRACE_EVENT0("cc", "Scheduler::PollForAnticipatedDrawTriggers");
645 poll_for_draw_triggers_task_
.Cancel();
646 state_machine_
.DidEnterPollForAnticipatedDrawTriggers();
647 ProcessScheduledActions();
648 state_machine_
.DidLeavePollForAnticipatedDrawTriggers();
651 void Scheduler::PollToAdvanceCommitState() {
652 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
653 advance_commit_state_task_
.Cancel();
654 ProcessScheduledActions();
657 void Scheduler::DrawAndSwapIfPossible() {
658 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
659 state_machine_
.DidDrawIfPossibleCompleted(result
);
662 void Scheduler::SetDeferCommits(bool defer_commits
) {
663 TRACE_EVENT1("cc", "Scheduler::SetDeferCommits",
666 state_machine_
.SetDeferCommits(defer_commits
);
667 ProcessScheduledActions();
670 void Scheduler::ProcessScheduledActions() {
671 // We do not allow ProcessScheduledActions to be recursive.
672 // The top-level call will iteratively execute the next action for us anyway.
673 if (inside_process_scheduled_actions_
)
676 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
678 SchedulerStateMachine::Action action
;
680 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
681 tracked_objects::ScopedTracker
tracking_profile1(
682 FROM_HERE_WITH_EXPLICIT_FUNCTION(
683 "461509 Scheduler::ProcessScheduledActions1"));
684 action
= state_machine_
.NextAction();
685 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
686 "SchedulerStateMachine",
689 VLOG(2) << "Scheduler::ProcessScheduledActions: "
690 << SchedulerStateMachine::ActionToString(action
) << " "
691 << state_machine_
.GetStatesForDebugging();
692 state_machine_
.UpdateState(action
);
693 base::AutoReset
<SchedulerStateMachine::Action
>
694 mark_inside_action(&inside_action_
, action
);
696 case SchedulerStateMachine::ACTION_NONE
:
698 case SchedulerStateMachine::ACTION_ANIMATE
: {
699 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
701 tracked_objects::ScopedTracker
tracking_profile2(
702 FROM_HERE_WITH_EXPLICIT_FUNCTION(
703 "461509 Scheduler::ProcessScheduledActions2"));
704 client_
->ScheduledActionAnimate();
707 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
: {
708 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
710 tracked_objects::ScopedTracker
tracking_profile3(
711 FROM_HERE_WITH_EXPLICIT_FUNCTION(
712 "461509 Scheduler::ProcessScheduledActions3"));
713 client_
->ScheduledActionSendBeginMainFrame();
716 case SchedulerStateMachine::ACTION_COMMIT
: {
717 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
719 tracked_objects::ScopedTracker
tracking_profile4(
720 FROM_HERE_WITH_EXPLICIT_FUNCTION(
721 "461509 Scheduler::ProcessScheduledActions4"));
722 client_
->ScheduledActionCommit();
725 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
: {
726 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
728 tracked_objects::ScopedTracker
tracking_profile5(
729 FROM_HERE_WITH_EXPLICIT_FUNCTION(
730 "461509 Scheduler::ProcessScheduledActions5"));
731 client_
->ScheduledActionActivateSyncTree();
734 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
735 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
737 tracked_objects::ScopedTracker
tracking_profile6(
738 FROM_HERE_WITH_EXPLICIT_FUNCTION(
739 "461509 Scheduler::ProcessScheduledActions6"));
740 DrawAndSwapIfPossible();
743 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
: {
744 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
746 tracked_objects::ScopedTracker
tracking_profile7(
747 FROM_HERE_WITH_EXPLICIT_FUNCTION(
748 "461509 Scheduler::ProcessScheduledActions7"));
749 client_
->ScheduledActionDrawAndSwapForced();
752 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
753 // No action is actually performed, but this allows the state machine to
754 // advance out of its waiting to draw state without actually drawing.
756 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
: {
757 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
759 tracked_objects::ScopedTracker
tracking_profile8(
760 FROM_HERE_WITH_EXPLICIT_FUNCTION(
761 "461509 Scheduler::ProcessScheduledActions8"));
762 client_
->ScheduledActionBeginOutputSurfaceCreation();
765 case SchedulerStateMachine::ACTION_PREPARE_TILES
: {
766 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
768 tracked_objects::ScopedTracker
tracking_profile9(
769 FROM_HERE_WITH_EXPLICIT_FUNCTION(
770 "461509 Scheduler::ProcessScheduledActions9"));
771 client_
->ScheduledActionPrepareTiles();
775 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
777 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
778 tracked_objects::ScopedTracker
tracking_profile10(
779 FROM_HERE_WITH_EXPLICIT_FUNCTION(
780 "461509 Scheduler::ProcessScheduledActions10"));
782 SetupPollingMechanisms();
784 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
786 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
787 tracked_objects::ScopedTracker
tracking_profile11(
788 FROM_HERE_WITH_EXPLICIT_FUNCTION(
789 "461509 Scheduler::ProcessScheduledActions11"));
791 RescheduleBeginImplFrameDeadlineIfNeeded();
793 SetupNextBeginFrameIfNeeded();
796 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> Scheduler::AsValue()
798 scoped_refptr
<base::trace_event::TracedValue
> state
=
799 new base::trace_event::TracedValue();
800 AsValueInto(state
.get());
804 void Scheduler::AsValueInto(base::trace_event::TracedValue
* state
) const {
805 state
->BeginDictionary("state_machine");
806 state_machine_
.AsValueInto(state
);
807 state
->EndDictionary();
809 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
810 bool frame_tracing_enabled
= false;
811 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
812 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
813 &frame_tracing_enabled
);
814 if (frame_tracing_enabled
) {
815 state
->BeginDictionary("frame_source_");
816 frame_source_
->AsValueInto(state
);
817 state
->EndDictionary();
820 state
->BeginDictionary("scheduler_state");
821 state
->SetDouble("time_until_anticipated_draw_time_ms",
822 (AnticipatedDrawTime() - Now()).InMillisecondsF());
823 state
->SetDouble("estimated_parent_draw_time_ms",
824 estimated_parent_draw_time_
.InMillisecondsF());
825 state
->SetBoolean("last_set_needs_begin_frame_",
826 frame_source_
->NeedsBeginFrames());
827 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
828 state
->SetBoolean("begin_retro_frame_task_",
829 !begin_retro_frame_task_
.IsCancelled());
830 state
->SetBoolean("begin_impl_frame_deadline_task_",
831 !begin_impl_frame_deadline_task_
.IsCancelled());
832 state
->SetBoolean("poll_for_draw_triggers_task_",
833 !poll_for_draw_triggers_task_
.IsCancelled());
834 state
->SetBoolean("advance_commit_state_task_",
835 !advance_commit_state_task_
.IsCancelled());
836 state
->BeginDictionary("begin_impl_frame_args");
837 begin_impl_frame_args_
.AsValueInto(state
);
838 state
->EndDictionary();
840 base::TimeTicks now
= Now();
841 base::TimeTicks frame_time
= begin_impl_frame_args_
.frame_time
;
842 base::TimeTicks deadline
= begin_impl_frame_args_
.deadline
;
843 base::TimeDelta interval
= begin_impl_frame_args_
.interval
;
844 state
->BeginDictionary("major_timestamps_in_ms");
845 state
->SetDouble("0_interval", interval
.InMillisecondsF());
846 state
->SetDouble("1_now_to_deadline", (deadline
- now
).InMillisecondsF());
847 state
->SetDouble("2_frame_time_to_now", (now
- frame_time
).InMillisecondsF());
848 state
->SetDouble("3_frame_time_to_deadline",
849 (deadline
- frame_time
).InMillisecondsF());
850 state
->SetDouble("4_now", (now
- base::TimeTicks()).InMillisecondsF());
851 state
->SetDouble("5_frame_time",
852 (frame_time
- base::TimeTicks()).InMillisecondsF());
853 state
->SetDouble("6_deadline",
854 (deadline
- base::TimeTicks()).InMillisecondsF());
855 state
->EndDictionary();
857 state
->EndDictionary();
859 state
->BeginDictionary("client_state");
860 state
->SetDouble("draw_duration_estimate_ms",
861 client_
->DrawDurationEstimate().InMillisecondsF());
863 "begin_main_frame_to_commit_duration_estimate_ms",
864 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
866 "commit_to_activate_duration_estimate_ms",
867 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
868 state
->EndDictionary();
871 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
872 // Check if the main thread computation and commit can be finished before the
873 // impl thread's deadline.
874 base::TimeTicks estimated_draw_time
=
875 begin_impl_frame_args_
.frame_time
+
876 client_
->BeginMainFrameToCommitDurationEstimate() +
877 client_
->CommitToActivateDurationEstimate();
880 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
881 "CanCommitAndActivateBeforeDeadline",
882 "time_left_after_drawing_ms",
883 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
887 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
890 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
891 return (state_machine_
.commit_state() ==
892 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
893 state_machine_
.commit_state() ==
894 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);