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);
120 frame_source_
->SetActiveSource(nullptr);
123 base::TimeTicks
Scheduler::Now() const {
124 base::TimeTicks now
= gfx::FrameTime::Now();
125 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.now"),
132 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase
,
133 base::TimeDelta interval
) {
134 if (authoritative_vsync_interval_
!= base::TimeDelta()) {
135 interval
= authoritative_vsync_interval_
;
136 } else if (interval
== base::TimeDelta()) {
137 // TODO(brianderson): We should not be receiving 0 intervals.
138 interval
= BeginFrameArgs::DefaultInterval();
141 last_vsync_timebase_
= timebase
;
144 vsync_observer_
->OnUpdateVSyncParameters(timebase
, interval
);
147 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time
) {
148 DCHECK_GE(draw_time
.ToInternalValue(), 0);
149 estimated_parent_draw_time_
= draw_time
;
152 void Scheduler::SetCanStart() {
153 state_machine_
.SetCanStart();
154 ProcessScheduledActions();
157 void Scheduler::SetVisible(bool visible
) {
158 state_machine_
.SetVisible(visible
);
159 ProcessScheduledActions();
162 void Scheduler::SetCanDraw(bool can_draw
) {
163 state_machine_
.SetCanDraw(can_draw
);
164 ProcessScheduledActions();
167 void Scheduler::NotifyReadyToActivate() {
168 state_machine_
.NotifyReadyToActivate();
169 ProcessScheduledActions();
172 void Scheduler::NotifyReadyToDraw() {
173 // Future work might still needed for crbug.com/352894.
174 state_machine_
.NotifyReadyToDraw();
175 ProcessScheduledActions();
178 void Scheduler::SetThrottleFrameProduction(bool throttle
) {
179 throttle_frame_production_
= throttle
;
181 frame_source_
->SetActiveSource(primary_frame_source_
);
183 frame_source_
->SetActiveSource(unthrottled_frame_source_
);
185 ProcessScheduledActions();
188 void Scheduler::SetNeedsCommit() {
189 state_machine_
.SetNeedsCommit();
190 ProcessScheduledActions();
193 void Scheduler::SetNeedsRedraw() {
194 state_machine_
.SetNeedsRedraw();
195 ProcessScheduledActions();
198 void Scheduler::SetNeedsAnimate() {
199 state_machine_
.SetNeedsAnimate();
200 ProcessScheduledActions();
203 void Scheduler::SetNeedsPrepareTiles() {
204 DCHECK(!IsInsideAction(SchedulerStateMachine::ACTION_PREPARE_TILES
));
205 state_machine_
.SetNeedsPrepareTiles();
206 ProcessScheduledActions();
209 void Scheduler::SetWaitForReadyToDraw() {
210 state_machine_
.SetWaitForReadyToDraw();
211 ProcessScheduledActions();
214 void Scheduler::SetMaxSwapsPending(int max
) {
215 state_machine_
.SetMaxSwapsPending(max
);
218 void Scheduler::DidSwapBuffers() {
219 state_machine_
.DidSwapBuffers();
221 // There is no need to call ProcessScheduledActions here because
222 // swapping should not trigger any new actions.
223 if (!inside_process_scheduled_actions_
) {
224 DCHECK_EQ(state_machine_
.NextAction(), SchedulerStateMachine::ACTION_NONE
);
228 void Scheduler::DidSwapBuffersComplete() {
229 state_machine_
.DidSwapBuffersComplete();
230 ProcessScheduledActions();
233 void Scheduler::SetImplLatencyTakesPriority(bool impl_latency_takes_priority
) {
234 state_machine_
.SetImplLatencyTakesPriority(impl_latency_takes_priority
);
235 ProcessScheduledActions();
238 void Scheduler::NotifyReadyToCommit() {
239 TRACE_EVENT0("cc", "Scheduler::NotifyReadyToCommit");
240 state_machine_
.NotifyReadyToCommit();
241 ProcessScheduledActions();
244 void Scheduler::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
245 TRACE_EVENT1("cc", "Scheduler::BeginMainFrameAborted", "reason",
246 CommitEarlyOutReasonToString(reason
));
247 state_machine_
.BeginMainFrameAborted(reason
);
248 ProcessScheduledActions();
251 void Scheduler::DidPrepareTiles() {
252 state_machine_
.DidPrepareTiles();
255 void Scheduler::DidLoseOutputSurface() {
256 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface");
257 begin_retro_frame_args_
.clear();
258 begin_retro_frame_task_
.Cancel();
259 state_machine_
.DidLoseOutputSurface();
260 ProcessScheduledActions();
263 void Scheduler::DidCreateAndInitializeOutputSurface() {
264 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface");
265 DCHECK(!frame_source_
->NeedsBeginFrames());
266 DCHECK(begin_impl_frame_deadline_task_
.IsCancelled());
267 state_machine_
.DidCreateAndInitializeOutputSurface();
268 ProcessScheduledActions();
271 void Scheduler::NotifyBeginMainFrameStarted() {
272 TRACE_EVENT0("cc", "Scheduler::NotifyBeginMainFrameStarted");
273 state_machine_
.NotifyBeginMainFrameStarted();
276 base::TimeTicks
Scheduler::AnticipatedDrawTime() const {
277 if (!frame_source_
->NeedsBeginFrames() ||
278 begin_impl_frame_args_
.interval
<= base::TimeDelta())
279 return base::TimeTicks();
281 base::TimeTicks now
= Now();
282 base::TimeTicks timebase
= std::max(begin_impl_frame_args_
.frame_time
,
283 begin_impl_frame_args_
.deadline
);
284 int64 intervals
= 1 + ((now
- timebase
) / begin_impl_frame_args_
.interval
);
285 return timebase
+ (begin_impl_frame_args_
.interval
* intervals
);
288 base::TimeTicks
Scheduler::LastBeginImplFrameTime() {
289 return begin_impl_frame_args_
.frame_time
;
292 void Scheduler::SetupNextBeginFrameIfNeeded() {
293 // Never call SetNeedsBeginFrames if the frame source already has the right
295 if (frame_source_
->NeedsBeginFrames() != state_machine_
.BeginFrameNeeded()) {
296 if (state_machine_
.BeginFrameNeeded()) {
297 // Call SetNeedsBeginFrames(true) as soon as possible.
298 frame_source_
->SetNeedsBeginFrames(true);
299 } else if (state_machine_
.begin_impl_frame_state() ==
300 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
) {
301 // Call SetNeedsBeginFrames(false) in between frames only.
302 frame_source_
->SetNeedsBeginFrames(false);
303 client_
->SendBeginMainFrameNotExpectedSoon();
307 PostBeginRetroFrameIfNeeded();
310 // We may need to poll when we can't rely on BeginFrame to advance certain
311 // state or to avoid deadlock.
312 void Scheduler::SetupPollingMechanisms() {
313 // At this point we'd prefer to advance through the commit flow by
314 // drawing a frame, however it's possible that the frame rate controller
315 // will not give us a BeginFrame until the commit completes. See
316 // crbug.com/317430 for an example of a swap ack being held on commit. Thus
317 // we set a repeating timer to poll on ProcessScheduledActions until we
318 // successfully reach BeginFrame. Synchronous compositor does not use
319 // frame rate controller or have the circular wait in the bug.
320 if (IsBeginMainFrameSentOrStarted() &&
321 !settings_
.using_synchronous_renderer_compositor
) {
322 if (advance_commit_state_task_
.IsCancelled() &&
323 begin_impl_frame_args_
.IsValid()) {
324 // Since we'd rather get a BeginImplFrame by the normal mechanism, we
325 // set the interval to twice the interval from the previous frame.
326 advance_commit_state_task_
.Reset(advance_commit_state_closure_
);
327 task_runner_
->PostDelayedTask(FROM_HERE
,
328 advance_commit_state_task_
.callback(),
329 begin_impl_frame_args_
.interval
* 2);
332 advance_commit_state_task_
.Cancel();
336 // BeginFrame is the mechanism that tells us that now is a good time to start
337 // making a frame. Usually this means that user input for the frame is complete.
338 // If the scheduler is busy, we queue the BeginFrame to be handled later as
339 // a BeginRetroFrame.
340 bool Scheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) {
341 TRACE_EVENT1("cc,benchmark", "Scheduler::BeginFrame", "args", args
.AsValue());
343 // Deliver BeginFrames to children.
344 if (state_machine_
.children_need_begin_frames()) {
345 BeginFrameArgs
adjusted_args_for_children(args
);
346 // Adjust a deadline for child schedulers.
347 // TODO(simonhong): Once we have commitless update, we can get rid of
348 // BeginMainFrameToCommitDurationEstimate() +
349 // CommitToActivateDurationEstimate().
350 adjusted_args_for_children
.deadline
-=
351 (client_
->BeginMainFrameToCommitDurationEstimate() +
352 client_
->CommitToActivateDurationEstimate() +
353 client_
->DrawDurationEstimate() + EstimatedParentDrawTime());
354 client_
->SendBeginFramesToChildren(adjusted_args_for_children
);
357 BeginFrameArgs
adjusted_args(args
);
358 adjusted_args
.deadline
-= EstimatedParentDrawTime();
360 if (settings_
.using_synchronous_renderer_compositor
) {
361 BeginImplFrameSynchronous(adjusted_args
);
365 // We have just called SetNeedsBeginFrame(true) and the BeginFrameSource has
366 // sent us the last BeginFrame we have missed. As we might not be able to
367 // actually make rendering for this call, handle it like a "retro frame".
368 // TODO(brainderson): Add a test for this functionality ASAP!
369 if (adjusted_args
.type
== BeginFrameArgs::MISSED
) {
370 begin_retro_frame_args_
.push_back(adjusted_args
);
371 PostBeginRetroFrameIfNeeded();
375 bool should_defer_begin_frame
=
376 !begin_retro_frame_args_
.empty() ||
377 !begin_retro_frame_task_
.IsCancelled() ||
378 !frame_source_
->NeedsBeginFrames() ||
379 (state_machine_
.begin_impl_frame_state() !=
380 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
382 if (should_defer_begin_frame
) {
383 begin_retro_frame_args_
.push_back(adjusted_args
);
384 TRACE_EVENT_INSTANT0(
385 "cc", "Scheduler::BeginFrame deferred", TRACE_EVENT_SCOPE_THREAD
);
386 // Queuing the frame counts as "using it", so we need to return true.
388 BeginImplFrameWithDeadline(adjusted_args
);
393 void Scheduler::SetChildrenNeedBeginFrames(bool children_need_begin_frames
) {
394 state_machine_
.SetChildrenNeedBeginFrames(children_need_begin_frames
);
395 ProcessScheduledActions();
398 void Scheduler::SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
) {
399 authoritative_vsync_interval_
= interval
;
401 vsync_observer_
->OnUpdateVSyncParameters(last_vsync_timebase_
, interval
);
404 void Scheduler::SetVideoNeedsBeginFrames(bool video_needs_begin_frames
) {
405 state_machine_
.SetVideoNeedsBeginFrames(video_needs_begin_frames
);
406 ProcessScheduledActions();
409 void Scheduler::OnDrawForOutputSurface() {
410 DCHECK(settings_
.using_synchronous_renderer_compositor
);
411 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
412 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
413 DCHECK(!BeginImplFrameDeadlinePending());
415 state_machine_
.OnBeginImplFrameDeadline();
416 ProcessScheduledActions();
418 state_machine_
.OnBeginImplFrameIdle();
419 ProcessScheduledActions();
422 // BeginRetroFrame is called for BeginFrames that we've deferred because
423 // the scheduler was in the middle of processing a previous BeginFrame.
424 void Scheduler::BeginRetroFrame() {
425 TRACE_EVENT0("cc,benchmark", "Scheduler::BeginRetroFrame");
426 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
427 DCHECK(!begin_retro_frame_args_
.empty());
428 DCHECK(!begin_retro_frame_task_
.IsCancelled());
429 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
430 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
432 begin_retro_frame_task_
.Cancel();
434 // Discard expired BeginRetroFrames
435 // Today, we should always end up with at most one un-expired BeginRetroFrame
436 // because deadlines will not be greater than the next frame time. We don't
437 // DCHECK though because some systems don't always have monotonic timestamps.
438 // TODO(brianderson): In the future, long deadlines could result in us not
439 // draining the queue if we don't catch up. If we consistently can't catch
440 // up, our fallback should be to lower our frame rate.
441 base::TimeTicks now
= Now();
443 while (!begin_retro_frame_args_
.empty()) {
444 const BeginFrameArgs
& args
= begin_retro_frame_args_
.front();
445 base::TimeTicks expiration_time
= args
.frame_time
+ args
.interval
;
446 if (now
<= expiration_time
)
448 TRACE_EVENT_INSTANT2(
449 "cc", "Scheduler::BeginRetroFrame discarding", TRACE_EVENT_SCOPE_THREAD
,
450 "expiration_time - now", (expiration_time
- now
).InMillisecondsF(),
451 "BeginFrameArgs", begin_retro_frame_args_
.front().AsValue());
452 begin_retro_frame_args_
.pop_front();
453 frame_source_
->DidFinishFrame(begin_retro_frame_args_
.size());
456 if (begin_retro_frame_args_
.empty()) {
457 TRACE_EVENT_INSTANT0("cc",
458 "Scheduler::BeginRetroFrames all expired",
459 TRACE_EVENT_SCOPE_THREAD
);
461 BeginFrameArgs front
= begin_retro_frame_args_
.front();
462 begin_retro_frame_args_
.pop_front();
463 BeginImplFrameWithDeadline(front
);
467 // There could be a race between the posted BeginRetroFrame and a new
468 // BeginFrame arriving via the normal mechanism. Scheduler::BeginFrame
469 // will check if there is a pending BeginRetroFrame to ensure we handle
470 // BeginFrames in FIFO order.
471 void Scheduler::PostBeginRetroFrameIfNeeded() {
472 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
473 "Scheduler::PostBeginRetroFrameIfNeeded",
476 if (!frame_source_
->NeedsBeginFrames())
479 if (begin_retro_frame_args_
.empty() || !begin_retro_frame_task_
.IsCancelled())
482 // begin_retro_frame_args_ should always be empty for the
483 // synchronous compositor.
484 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
486 if (state_machine_
.begin_impl_frame_state() !=
487 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
)
490 begin_retro_frame_task_
.Reset(begin_retro_frame_closure_
);
492 task_runner_
->PostTask(FROM_HERE
, begin_retro_frame_task_
.callback());
495 void Scheduler::BeginImplFrameWithDeadline(const BeginFrameArgs
& args
) {
496 bool main_thread_is_in_high_latency_mode
=
497 state_machine_
.MainThreadIsInHighLatencyMode();
498 TRACE_EVENT2("cc,benchmark", "Scheduler::BeginImplFrame", "args",
499 args
.AsValue(), "main_thread_is_high_latency",
500 main_thread_is_in_high_latency_mode
);
501 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
502 "MainThreadLatency", main_thread_is_in_high_latency_mode
);
504 advance_commit_state_task_
.Cancel();
506 begin_impl_frame_args_
= args
;
507 begin_impl_frame_args_
.deadline
-= client_
->DrawDurationEstimate();
509 if (!state_machine_
.impl_latency_takes_priority() &&
510 main_thread_is_in_high_latency_mode
&&
511 CanCommitAndActivateBeforeDeadline()) {
512 state_machine_
.SetSkipNextBeginMainFrameToReduceLatency();
517 // The deadline will be scheduled in ProcessScheduledActions.
518 state_machine_
.OnBeginImplFrameDeadlinePending();
519 ProcessScheduledActions();
522 void Scheduler::BeginImplFrameSynchronous(const BeginFrameArgs
& args
) {
523 TRACE_EVENT1("cc,benchmark", "Scheduler::BeginImplFrame", "args",
525 begin_impl_frame_args_
= 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() {
542 DCHECK_EQ(state_machine_
.begin_impl_frame_state(),
543 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE
);
544 DCHECK(!BeginImplFrameDeadlinePending());
545 DCHECK(state_machine_
.HasInitializedOutputSurface());
546 DCHECK(advance_commit_state_task_
.IsCancelled());
548 state_machine_
.OnBeginImplFrame();
549 devtools_instrumentation::DidBeginFrame(layer_tree_host_id_
);
550 client_
->WillBeginImplFrame(begin_impl_frame_args_
);
552 ProcessScheduledActions();
555 void Scheduler::ScheduleBeginImplFrameDeadline() {
556 // The synchronous compositor does not post a deadline task.
557 DCHECK(!settings_
.using_synchronous_renderer_compositor
);
559 begin_impl_frame_deadline_task_
.Cancel();
560 begin_impl_frame_deadline_task_
.Reset(begin_impl_frame_deadline_closure_
);
562 begin_impl_frame_deadline_mode_
=
563 state_machine_
.CurrentBeginImplFrameDeadlineMode();
565 base::TimeTicks deadline
;
566 switch (begin_impl_frame_deadline_mode_
) {
567 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
:
570 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
571 // We are ready to draw a new active tree immediately.
572 // We don't use Now() here because it's somewhat expensive to call.
573 deadline
= base::TimeTicks();
575 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
576 // We are animating on the impl thread but we can wait for some time.
577 deadline
= begin_impl_frame_args_
.deadline
;
579 case SchedulerStateMachine::BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
580 // We are blocked for one reason or another and we should wait.
581 // TODO(brianderson): Handle long deadlines (that are past the next
582 // frame's frame time) properly instead of using this hack.
584 begin_impl_frame_args_
.frame_time
+ begin_impl_frame_args_
.interval
;
586 case SchedulerStateMachine::
587 BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
:
588 // We are blocked because we are waiting for ReadyToDraw signal. We would
589 // post deadline after we received ReadyToDraw singal.
590 TRACE_EVENT1("cc", "Scheduler::ScheduleBeginImplFrameDeadline",
591 "deadline_mode", "blocked_on_ready_to_draw");
595 TRACE_EVENT2("cc", "Scheduler::ScheduleBeginImplFrameDeadline", "mode",
596 SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
597 begin_impl_frame_deadline_mode_
),
598 "deadline", deadline
);
600 base::TimeDelta delta
= std::max(deadline
- Now(), base::TimeDelta());
601 task_runner_
->PostDelayedTask(
602 FROM_HERE
, begin_impl_frame_deadline_task_
.callback(), delta
);
605 void Scheduler::ScheduleBeginImplFrameDeadlineIfNeeded() {
606 if (settings_
.using_synchronous_renderer_compositor
)
609 if (state_machine_
.begin_impl_frame_state() !=
610 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
613 if (begin_impl_frame_deadline_mode_
==
614 state_machine_
.CurrentBeginImplFrameDeadlineMode() &&
615 BeginImplFrameDeadlinePending())
618 ScheduleBeginImplFrameDeadline();
621 void Scheduler::OnBeginImplFrameDeadline() {
622 TRACE_EVENT0("cc,benchmark", "Scheduler::OnBeginImplFrameDeadline");
623 begin_impl_frame_deadline_task_
.Cancel();
624 // We split the deadline actions up into two phases so the state machine
625 // has a chance to trigger actions that should occur durring and after
626 // the deadline separately. For example:
627 // * Sending the BeginMainFrame will not occur after the deadline in
628 // order to wait for more user-input before starting the next commit.
629 // * Creating a new OuputSurface will not occur during the deadline in
630 // order to allow the state machine to "settle" first.
632 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is fixed.
633 tracked_objects::ScopedTracker
tracking_profile1(
634 FROM_HERE_WITH_EXPLICIT_FUNCTION(
635 "461509 Scheduler::OnBeginImplFrameDeadline1"));
636 state_machine_
.OnBeginImplFrameDeadline();
637 ProcessScheduledActions();
642 void Scheduler::PollToAdvanceCommitState() {
643 TRACE_EVENT0("cc", "Scheduler::PollToAdvanceCommitState");
644 advance_commit_state_task_
.Cancel();
645 ProcessScheduledActions();
648 void Scheduler::DrawAndSwapIfPossible() {
649 DrawResult result
= client_
->ScheduledActionDrawAndSwapIfPossible();
650 state_machine_
.DidDrawIfPossibleCompleted(result
);
653 void Scheduler::SetDeferCommits(bool defer_commits
) {
654 TRACE_EVENT1("cc", "Scheduler::SetDeferCommits",
657 state_machine_
.SetDeferCommits(defer_commits
);
658 ProcessScheduledActions();
661 void Scheduler::ProcessScheduledActions() {
662 // We do not allow ProcessScheduledActions to be recursive.
663 // The top-level call will iteratively execute the next action for us anyway.
664 if (inside_process_scheduled_actions_
)
667 base::AutoReset
<bool> mark_inside(&inside_process_scheduled_actions_
, true);
669 SchedulerStateMachine::Action action
;
671 action
= state_machine_
.NextAction();
672 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
673 "SchedulerStateMachine",
676 VLOG(2) << "Scheduler::ProcessScheduledActions: "
677 << SchedulerStateMachine::ActionToString(action
) << " "
678 << state_machine_
.GetStatesForDebugging();
679 state_machine_
.UpdateState(action
);
680 base::AutoReset
<SchedulerStateMachine::Action
>
681 mark_inside_action(&inside_action_
, action
);
683 case SchedulerStateMachine::ACTION_NONE
:
685 case SchedulerStateMachine::ACTION_ANIMATE
:
686 client_
->ScheduledActionAnimate();
688 case SchedulerStateMachine::ACTION_SEND_BEGIN_MAIN_FRAME
:
689 client_
->ScheduledActionSendBeginMainFrame();
691 case SchedulerStateMachine::ACTION_COMMIT
: {
692 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
694 tracked_objects::ScopedTracker
tracking_profile4(
695 FROM_HERE_WITH_EXPLICIT_FUNCTION(
696 "461509 Scheduler::ProcessScheduledActions4"));
697 client_
->ScheduledActionCommit();
700 case SchedulerStateMachine::ACTION_ACTIVATE_SYNC_TREE
:
701 client_
->ScheduledActionActivateSyncTree();
703 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
704 // TODO(robliao): Remove ScopedTracker below once crbug.com/461509 is
706 tracked_objects::ScopedTracker
tracking_profile6(
707 FROM_HERE_WITH_EXPLICIT_FUNCTION(
708 "461509 Scheduler::ProcessScheduledActions6"));
709 DrawAndSwapIfPossible();
712 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_FORCED
:
713 client_
->ScheduledActionDrawAndSwapForced();
715 case SchedulerStateMachine::ACTION_DRAW_AND_SWAP_ABORT
:
716 // No action is actually performed, but this allows the state machine to
717 // advance out of its waiting to draw state without actually drawing.
719 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
720 client_
->ScheduledActionBeginOutputSurfaceCreation();
722 case SchedulerStateMachine::ACTION_PREPARE_TILES
:
723 client_
->ScheduledActionPrepareTiles();
725 case SchedulerStateMachine::ACTION_INVALIDATE_OUTPUT_SURFACE
: {
726 client_
->ScheduledActionInvalidateOutputSurface();
730 } while (action
!= SchedulerStateMachine::ACTION_NONE
);
732 SetupPollingMechanisms();
734 client_
->DidAnticipatedDrawTimeChange(AnticipatedDrawTime());
736 ScheduleBeginImplFrameDeadlineIfNeeded();
738 SetupNextBeginFrameIfNeeded();
741 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> Scheduler::AsValue()
743 scoped_refptr
<base::trace_event::TracedValue
> state
=
744 new base::trace_event::TracedValue();
745 AsValueInto(state
.get());
749 void Scheduler::AsValueInto(base::trace_event::TracedValue
* state
) const {
750 state
->BeginDictionary("state_machine");
751 state_machine_
.AsValueInto(state
);
752 state
->EndDictionary();
754 // Only trace frame sources when explicitly enabled - http://crbug.com/420607
755 bool frame_tracing_enabled
= false;
756 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
757 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
758 &frame_tracing_enabled
);
759 if (frame_tracing_enabled
) {
760 state
->BeginDictionary("frame_source_");
761 frame_source_
->AsValueInto(state
);
762 state
->EndDictionary();
765 state
->BeginDictionary("scheduler_state");
766 state
->SetDouble("time_until_anticipated_draw_time_ms",
767 (AnticipatedDrawTime() - Now()).InMillisecondsF());
768 state
->SetDouble("estimated_parent_draw_time_ms",
769 estimated_parent_draw_time_
.InMillisecondsF());
770 state
->SetBoolean("last_set_needs_begin_frame_",
771 frame_source_
->NeedsBeginFrames());
772 state
->SetInteger("begin_retro_frame_args_", begin_retro_frame_args_
.size());
773 state
->SetBoolean("begin_retro_frame_task_",
774 !begin_retro_frame_task_
.IsCancelled());
775 state
->SetBoolean("begin_impl_frame_deadline_task_",
776 !begin_impl_frame_deadline_task_
.IsCancelled());
777 state
->SetBoolean("advance_commit_state_task_",
778 !advance_commit_state_task_
.IsCancelled());
779 state
->BeginDictionary("begin_impl_frame_args");
780 begin_impl_frame_args_
.AsValueInto(state
);
781 state
->EndDictionary();
783 base::TimeTicks now
= Now();
784 base::TimeTicks frame_time
= begin_impl_frame_args_
.frame_time
;
785 base::TimeTicks deadline
= begin_impl_frame_args_
.deadline
;
786 base::TimeDelta interval
= begin_impl_frame_args_
.interval
;
787 state
->BeginDictionary("major_timestamps_in_ms");
788 state
->SetDouble("0_interval", interval
.InMillisecondsF());
789 state
->SetDouble("1_now_to_deadline", (deadline
- now
).InMillisecondsF());
790 state
->SetDouble("2_frame_time_to_now", (now
- frame_time
).InMillisecondsF());
791 state
->SetDouble("3_frame_time_to_deadline",
792 (deadline
- frame_time
).InMillisecondsF());
793 state
->SetDouble("4_now", (now
- base::TimeTicks()).InMillisecondsF());
794 state
->SetDouble("5_frame_time",
795 (frame_time
- base::TimeTicks()).InMillisecondsF());
796 state
->SetDouble("6_deadline",
797 (deadline
- base::TimeTicks()).InMillisecondsF());
798 state
->EndDictionary();
800 state
->EndDictionary();
802 state
->BeginDictionary("client_state");
803 state
->SetDouble("draw_duration_estimate_ms",
804 client_
->DrawDurationEstimate().InMillisecondsF());
806 "begin_main_frame_to_commit_duration_estimate_ms",
807 client_
->BeginMainFrameToCommitDurationEstimate().InMillisecondsF());
809 "commit_to_activate_duration_estimate_ms",
810 client_
->CommitToActivateDurationEstimate().InMillisecondsF());
811 state
->EndDictionary();
814 bool Scheduler::CanCommitAndActivateBeforeDeadline() const {
815 // Check if the main thread computation and commit can be finished before the
816 // impl thread's deadline.
817 base::TimeTicks estimated_draw_time
=
818 begin_impl_frame_args_
.frame_time
+
819 client_
->BeginMainFrameToCommitDurationEstimate() +
820 client_
->CommitToActivateDurationEstimate();
823 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler"),
824 "CanCommitAndActivateBeforeDeadline",
825 "time_left_after_drawing_ms",
826 (begin_impl_frame_args_
.deadline
- estimated_draw_time
).InMillisecondsF(),
830 return estimated_draw_time
< begin_impl_frame_args_
.deadline
;
833 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
834 return (state_machine_
.commit_state() ==
835 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
836 state_machine_
.commit_state() ==
837 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
);