Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: cc/scheduler/scheduler.cc

Issue 267783004: Refactoring the way begin frame sources inside scheduler work. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Scheduler now uses frame sources, working on scheduler_unittests. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/scheduler/scheduler.h" 5 #include "cc/scheduler/scheduler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include "base/auto_reset.h" 8 #include "base/auto_reset.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "cc/debug/devtools_instrumentation.h" 11 #include "cc/debug/devtools_instrumentation.h"
12 #include "cc/debug/traced_value.h" 12 #include "cc/debug/traced_value.h"
13 #include "cc/scheduler/delay_based_time_source.h" 13 #include "cc/scheduler/delay_based_time_source.h"
14 #include "cc/scheduler/frame_source.h"
14 #include "ui/gfx/frame_time.h" 15 #include "ui/gfx/frame_time.h"
15 16
16 namespace cc { 17 namespace cc {
17 18
18 class SyntheticBeginFrameSource : public TimeSourceClient {
19 public:
20 SyntheticBeginFrameSource(Scheduler* scheduler,
21 base::SingleThreadTaskRunner* task_runner)
22 : scheduler_(scheduler) {
23 if (gfx::FrameTime::TimestampsAreHighRes()) {
24 time_source_ = DelayBasedTimeSourceHighRes::Create(
25 scheduler_->VSyncInterval(), task_runner);
26 } else {
27 time_source_ = DelayBasedTimeSource::Create(scheduler_->VSyncInterval(),
28 task_runner);
29 }
30 time_source_->SetClient(this);
31 }
32
33 virtual ~SyntheticBeginFrameSource() {}
34
35 // Updates the phase and frequency of the timer.
36 void CommitVSyncParameters(base::TimeTicks timebase,
37 base::TimeDelta interval) {
38 time_source_->SetTimebaseAndInterval(timebase, interval);
39 }
40
41 // Activates future BeginFrames and, if activating, pushes the most
42 // recently missed BeginFrame to the back of a retroactive queue.
43 void SetNeedsBeginFrame(bool needs_begin_frame,
44 std::deque<BeginFrameArgs>* begin_retro_frame_args) {
45 base::TimeTicks missed_tick_time =
46 time_source_->SetActive(needs_begin_frame);
47 if (!missed_tick_time.is_null()) {
48 begin_retro_frame_args->push_back(
49 CreateSyntheticBeginFrameArgs(missed_tick_time));
50 }
51 }
52
53 // TimeSourceClient implementation of OnTimerTick triggers a BeginFrame.
54 virtual void OnTimerTick() OVERRIDE {
55 BeginFrameArgs begin_frame_args(
56 CreateSyntheticBeginFrameArgs(time_source_->LastTickTime()));
57 scheduler_->BeginFrame(begin_frame_args);
58 }
59
60 private:
61 BeginFrameArgs CreateSyntheticBeginFrameArgs(base::TimeTicks frame_time) {
62 base::TimeTicks deadline =
63 time_source_->NextTickTime() - scheduler_->EstimatedParentDrawTime();
64 return BeginFrameArgs::Create(
65 frame_time, deadline, scheduler_->VSyncInterval());
66 }
67
68 Scheduler* scheduler_;
69 scoped_refptr<TimeSource> time_source_;
70 };
71
72 Scheduler::Scheduler( 19 Scheduler::Scheduler(
73 SchedulerClient* client, 20 SchedulerClient* client,
74 const SchedulerSettings& scheduler_settings, 21 const SchedulerSettings& scheduler_settings,
75 int layer_tree_host_id, 22 int layer_tree_host_id,
23 FrameSource* external_frame_source,
76 const scoped_refptr<base::SingleThreadTaskRunner>& impl_task_runner) 24 const scoped_refptr<base::SingleThreadTaskRunner>& impl_task_runner)
77 : settings_(scheduler_settings), 25 : settings_(scheduler_settings),
78 client_(client), 26 client_(client),
79 layer_tree_host_id_(layer_tree_host_id), 27 layer_tree_host_id_(layer_tree_host_id),
80 impl_task_runner_(impl_task_runner), 28 impl_task_runner_(impl_task_runner),
81 vsync_interval_(BeginFrameArgs::DefaultInterval()),
82 last_set_needs_begin_frame_(false), 29 last_set_needs_begin_frame_(false),
83 begin_unthrottled_frame_posted_(false), 30 begin_unthrottled_frame_posted_(false),
84 begin_retro_frame_posted_(false), 31 begin_retro_frame_posted_(false),
85 state_machine_(scheduler_settings), 32 state_machine_(scheduler_settings),
86 inside_process_scheduled_actions_(false), 33 inside_process_scheduled_actions_(false),
87 inside_action_(SchedulerStateMachine::ACTION_NONE), 34 inside_action_(SchedulerStateMachine::ACTION_NONE),
88 weak_factory_(this) { 35 weak_factory_(this) {
89 DCHECK(client_); 36 DCHECK(client_);
90 DCHECK(!state_machine_.BeginFrameNeeded()); 37 DCHECK(!state_machine_.BeginFrameNeeded());
91 if (settings_.main_frame_before_activation_enabled) { 38 if (settings_.main_frame_before_activation_enabled) {
92 DCHECK(settings_.main_frame_before_draw_enabled); 39 DCHECK(settings_.main_frame_before_draw_enabled);
93 } 40 }
94 41
95 begin_retro_frame_closure_ = 42 begin_retro_frame_closure_ =
96 base::Bind(&Scheduler::BeginRetroFrame, weak_factory_.GetWeakPtr()); 43 base::Bind(&Scheduler::BeginRetroFrame, weak_factory_.GetWeakPtr());
97 begin_unthrottled_frame_closure_ = 44 // begin_unthrottled_frame_closure_ =
98 base::Bind(&Scheduler::BeginUnthrottledFrame, weak_factory_.GetWeakPtr()); 45 // base::Bind(&Scheduler::BeginUnthrottledFrame,
46 // weak_factory_.GetWeakPtr());
99 begin_impl_frame_deadline_closure_ = base::Bind( 47 begin_impl_frame_deadline_closure_ = base::Bind(
100 &Scheduler::OnBeginImplFrameDeadline, weak_factory_.GetWeakPtr()); 48 &Scheduler::OnBeginImplFrameDeadline, weak_factory_.GetWeakPtr());
101 poll_for_draw_triggers_closure_ = base::Bind( 49 poll_for_draw_triggers_closure_ = base::Bind(
102 &Scheduler::PollForAnticipatedDrawTriggers, weak_factory_.GetWeakPtr()); 50 &Scheduler::PollForAnticipatedDrawTriggers, weak_factory_.GetWeakPtr());
103 advance_commit_state_closure_ = base::Bind( 51 advance_commit_state_closure_ = base::Bind(
104 &Scheduler::PollToAdvanceCommitState, weak_factory_.GetWeakPtr()); 52 &Scheduler::PollToAdvanceCommitState, weak_factory_.GetWeakPtr());
105 53
106 if (!settings_.begin_frame_scheduling_enabled) { 54 scoped_ptr<FrameSource> primary_frame_source;
107 SetupSyntheticBeginFrames(); 55 if (settings_.begin_frame_scheduling_enabled) {
56 TRACE_EVENT0("frame_time", "Scheduler::Scheduler() ProxyFrameSource");
brianderson 2014/05/07 17:20:16 Should the category be "cc" instead of "frame_time
mithro-old 2014/05/07 22:02:32 Good question. I was pondering introducing "fram
brianderson 2014/05/08 00:55:58 If we don't assign it to cc or a new category, the
57 primary_frame_source = scoped_ptr<FrameSource>(
58 new ProxyFrameSource(this, external_frame_source));
59 } else if (!settings_.throttle_frame_production) {
brianderson 2014/05/07 17:20:16 throttle_frame_production should take precidence,
mithro-old 2014/05/07 23:42:28 Done.
60 TRACE_EVENT0("frame_time", "Scheduler::Scheduler() BackToBackFrameSource");
61 primary_frame_source = scoped_ptr<FrameSource>(
62 new BackToBackFrameSource(this, impl_task_runner_));
63 } else {
64 TRACE_EVENT0("frame_time", "Scheduler::Scheduler() SyntheticFrameSource");
65 primary_frame_source = scoped_ptr<FrameSource>(new SyntheticFrameSource(
66 this, impl_task_runner_, base::TimeDelta::FromMilliseconds(16)));
108 } 67 }
68 scoped_ptr<FrameSource> background_frame_source(new SyntheticFrameSource(
69 this, impl_task_runner_, base::TimeDelta::FromSeconds(1)));
70
71 frame_source_ = scoped_ptr<DualFrameSource>(new DualFrameSource(
72 this, primary_frame_source.Pass(), background_frame_source.Pass()));
109 } 73 }
110 74
111 Scheduler::~Scheduler() { 75 Scheduler::~Scheduler() {
112 if (synthetic_begin_frame_source_) { 76 frame_source_->SetNeedsBeginFrame(false);
113 synthetic_begin_frame_source_->SetNeedsBeginFrame(false,
114 &begin_retro_frame_args_);
115 }
116 }
117
118 void Scheduler::SetupSyntheticBeginFrames() {
119 DCHECK(!synthetic_begin_frame_source_);
120 synthetic_begin_frame_source_.reset(
121 new SyntheticBeginFrameSource(this, impl_task_runner_.get()));
122 } 77 }
123 78
124 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase, 79 void Scheduler::CommitVSyncParameters(base::TimeTicks timebase,
125 base::TimeDelta interval) { 80 base::TimeDelta interval) {
126 // TODO(brianderson): We should not be receiving 0 intervals. 81 // TODO(brianderson): We should not be receiving 0 intervals.
127 if (interval == base::TimeDelta()) 82 if (interval == base::TimeDelta())
128 interval = BeginFrameArgs::DefaultInterval(); 83 interval = BeginFrameArgs::DefaultInterval();
129 vsync_interval_ = interval; 84
130 if (!settings_.begin_frame_scheduling_enabled) 85 frame_source_->SetTimeBaseAndInterval(timebase, interval);
131 synthetic_begin_frame_source_->CommitVSyncParameters(timebase, interval);
132 } 86 }
133 87
134 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time) { 88 void Scheduler::SetEstimatedParentDrawTime(base::TimeDelta draw_time) {
135 estimated_parent_draw_time_ = draw_time; 89 estimated_parent_draw_time_ = draw_time;
136 } 90 }
137 91
138 void Scheduler::SetCanStart() { 92 void Scheduler::SetCanStart() {
139 state_machine_.SetCanStart(); 93 state_machine_.SetCanStart();
140 ProcessScheduledActions(); 94 ProcessScheduledActions();
141 } 95 }
142 96
143 void Scheduler::SetVisible(bool visible) { 97 void Scheduler::SetVisible(bool visible) {
144 state_machine_.SetVisible(visible); 98 state_machine_.SetVisible(visible);
99 if (visible) {
100 frame_source_->SwitchSource(frame_source_->SourceSecondary());
101 } else {
102 frame_source_->SwitchSource(frame_source_->SourcePrimary());
103 }
145 ProcessScheduledActions(); 104 ProcessScheduledActions();
146 } 105 }
147 106
148 void Scheduler::SetCanDraw(bool can_draw) { 107 void Scheduler::SetCanDraw(bool can_draw) {
149 state_machine_.SetCanDraw(can_draw); 108 state_machine_.SetCanDraw(can_draw);
150 ProcessScheduledActions(); 109 ProcessScheduledActions();
151 } 110 }
152 111
153 void Scheduler::NotifyReadyToActivate() { 112 void Scheduler::NotifyReadyToActivate() {
154 state_machine_.NotifyReadyToActivate(); 113 state_machine_.NotifyReadyToActivate();
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 return timebase + (begin_impl_frame_args_.interval * intervals); 221 return timebase + (begin_impl_frame_args_.interval * intervals);
263 } 222 }
264 223
265 base::TimeTicks Scheduler::LastBeginImplFrameTime() { 224 base::TimeTicks Scheduler::LastBeginImplFrameTime() {
266 return begin_impl_frame_args_.frame_time; 225 return begin_impl_frame_args_.frame_time;
267 } 226 }
268 227
269 void Scheduler::SetupNextBeginFrameIfNeeded() { 228 void Scheduler::SetupNextBeginFrameIfNeeded() {
270 bool needs_begin_frame = state_machine_.BeginFrameNeeded(); 229 bool needs_begin_frame = state_machine_.BeginFrameNeeded();
271 230
272 if (settings_.throttle_frame_production) { 231 // Only stop requesting BeginFrames after a deadline.
273 SetupNextBeginFrameWhenVSyncThrottlingEnabled(needs_begin_frame);
274 } else {
275 SetupNextBeginFrameWhenVSyncThrottlingDisabled(needs_begin_frame);
276 }
277 SetupPollingMechanisms(needs_begin_frame);
278 }
279
280 // When we are throttling frame production, we request BeginFrames
281 // from the OutputSurface.
282 void Scheduler::SetupNextBeginFrameWhenVSyncThrottlingEnabled(
283 bool needs_begin_frame) {
284 bool at_end_of_deadline =
285 state_machine_.begin_impl_frame_state() ==
286 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE;
287
288 bool should_call_set_needs_begin_frame =
289 // Always request the BeginFrame immediately if it wasn't needed before.
290 (needs_begin_frame && !last_set_needs_begin_frame_) ||
291 // Only stop requesting BeginFrames after a deadline.
292 (!needs_begin_frame && last_set_needs_begin_frame_ && at_end_of_deadline);
293
294 if (should_call_set_needs_begin_frame) {
295 if (settings_.begin_frame_scheduling_enabled) {
296 client_->SetNeedsBeginFrame(needs_begin_frame);
297 } else {
298 synthetic_begin_frame_source_->SetNeedsBeginFrame(
299 needs_begin_frame, &begin_retro_frame_args_);
300 }
301 last_set_needs_begin_frame_ = needs_begin_frame;
302 }
303
304 PostBeginRetroFrameIfNeeded();
305 }
306
307 // When we aren't throttling frame production, we initiate a BeginFrame
308 // as soon as one is needed.
309 void Scheduler::SetupNextBeginFrameWhenVSyncThrottlingDisabled(
310 bool needs_begin_frame) {
311 last_set_needs_begin_frame_ = needs_begin_frame;
312
313 if (!needs_begin_frame || begin_unthrottled_frame_posted_)
314 return;
315
316 if (state_machine_.begin_impl_frame_state() != 232 if (state_machine_.begin_impl_frame_state() !=
317 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE && 233 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_IDLE &&
318 state_machine_.begin_impl_frame_state() != 234 state_machine_.begin_impl_frame_state() !=
319 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE) { 235 SchedulerStateMachine::BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE) {
320 return; 236 return;
brianderson 2014/05/07 18:09:59 I don't think we can't return early here, otherwis
mithro-old 2014/05/07 23:42:28 Will look at this further as I fix the unittests.
321 } 237 }
322 238
323 begin_unthrottled_frame_posted_ = true; 239 // Always request the BeginFrame immediately if it wasn't needed before.
324 impl_task_runner_->PostTask(FROM_HERE, begin_unthrottled_frame_closure_); 240 if (needs_begin_frame && !last_set_needs_begin_frame_) {
325 } 241 frame_source_->SetNeedsBeginFrame(needs_begin_frame);
242 last_set_needs_begin_frame_ = needs_begin_frame;
243 }
326 244
327 // BeginUnthrottledFrame is used when we aren't throttling frame production. 245 SetupPollingMechanisms(needs_begin_frame);
328 // This will usually be because VSync is disabled.
329 void Scheduler::BeginUnthrottledFrame() {
330 DCHECK(!settings_.throttle_frame_production);
331 DCHECK(begin_retro_frame_args_.empty());
332 246
333 base::TimeTicks now = gfx::FrameTime::Now(); 247 PostBeginRetroFrameIfNeeded();
334 base::TimeTicks deadline = now + vsync_interval_;
335
336 BeginFrameArgs begin_frame_args =
337 BeginFrameArgs::Create(now, deadline, vsync_interval_);
338 BeginImplFrame(begin_frame_args);
339
340 begin_unthrottled_frame_posted_ = false;
341 } 248 }
342 249
343 // We may need to poll when we can't rely on BeginFrame to advance certain 250 // We may need to poll when we can't rely on BeginFrame to advance certain
344 // state or to avoid deadlock. 251 // state or to avoid deadlock.
345 void Scheduler::SetupPollingMechanisms(bool needs_begin_frame) { 252 void Scheduler::SetupPollingMechanisms(bool needs_begin_frame) {
346 bool needs_advance_commit_state_timer = false; 253 bool needs_advance_commit_state_timer = false;
347 // Setup PollForAnticipatedDrawTriggers if we need to monitor state but 254 // Setup PollForAnticipatedDrawTriggers if we need to monitor state but
348 // aren't expecting any more BeginFrames. This should only be needed by 255 // aren't expecting any more BeginFrames. This should only be needed by
349 // the synchronous compositor when BeginFrameNeeded is false. 256 // the synchronous compositor when BeginFrameNeeded is false.
350 if (state_machine_.ShouldPollForAnticipatedDrawTriggers()) { 257 if (state_machine_.ShouldPollForAnticipatedDrawTriggers()) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 } else { 294 } else {
388 advance_commit_state_task_.Cancel(); 295 advance_commit_state_task_.Cancel();
389 } 296 }
390 } 297 }
391 298
392 // BeginFrame is the mechanism that tells us that now is a good time to start 299 // BeginFrame is the mechanism that tells us that now is a good time to start
393 // making a frame. Usually this means that user input for the frame is complete. 300 // making a frame. Usually this means that user input for the frame is complete.
394 // If the scheduler is busy, we queue the BeginFrame to be handled later as 301 // If the scheduler is busy, we queue the BeginFrame to be handled later as
395 // a BeginRetroFrame. 302 // a BeginRetroFrame.
396 void Scheduler::BeginFrame(const BeginFrameArgs& args) { 303 void Scheduler::BeginFrame(const BeginFrameArgs& args) {
397 TRACE_EVENT1("cc", "Scheduler::BeginFrame", "frame_time", args.frame_time); 304 TRACE_EVENT1(
305 "cc", "Scheduler::SchedulerBeginFrame", "frame_time", args.frame_time);
306
398 DCHECK(settings_.throttle_frame_production); 307 DCHECK(settings_.throttle_frame_production);
399 308
400 bool should_defer_begin_frame; 309 bool should_defer_begin_frame;
401 if (settings_.using_synchronous_renderer_compositor) { 310 if (settings_.using_synchronous_renderer_compositor) {
402 should_defer_begin_frame = false; 311 should_defer_begin_frame = false;
403 } else { 312 } else {
404 should_defer_begin_frame = 313 should_defer_begin_frame =
405 !begin_retro_frame_args_.empty() || begin_retro_frame_posted_ || 314 !begin_retro_frame_args_.empty() || begin_retro_frame_posted_ ||
406 !last_set_needs_begin_frame_ || 315 !last_set_needs_begin_frame_ ||
407 (state_machine_.begin_impl_frame_state() != 316 (state_machine_.begin_impl_frame_state() !=
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 } 592 }
684 } 593 }
685 594
686 bool Scheduler::WillDrawIfNeeded() const { 595 bool Scheduler::WillDrawIfNeeded() const {
687 return !state_machine_.PendingDrawsShouldBeAborted(); 596 return !state_machine_.PendingDrawsShouldBeAborted();
688 } 597 }
689 598
690 scoped_ptr<base::Value> Scheduler::StateAsValue() const { 599 scoped_ptr<base::Value> Scheduler::StateAsValue() const {
691 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue); 600 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue);
692 state->Set("state_machine", state_machine_.AsValue().release()); 601 state->Set("state_machine", state_machine_.AsValue().release());
602 state->Set("frame_source", frame_source_->FrameSourceAsValue().release());
693 603
694 scoped_ptr<base::DictionaryValue> scheduler_state(new base::DictionaryValue); 604 scoped_ptr<base::DictionaryValue> scheduler_state(new base::DictionaryValue);
695 scheduler_state->SetDouble( 605 scheduler_state->SetDouble(
696 "time_until_anticipated_draw_time_ms", 606 "time_until_anticipated_draw_time_ms",
697 (AnticipatedDrawTime() - base::TimeTicks::Now()).InMillisecondsF()); 607 (AnticipatedDrawTime() - base::TimeTicks::Now()).InMillisecondsF());
698 scheduler_state->SetDouble("vsync_interval_ms",
699 vsync_interval_.InMillisecondsF());
700 scheduler_state->SetDouble("estimated_parent_draw_time_ms", 608 scheduler_state->SetDouble("estimated_parent_draw_time_ms",
701 estimated_parent_draw_time_.InMillisecondsF()); 609 estimated_parent_draw_time_.InMillisecondsF());
702 scheduler_state->SetBoolean("last_set_needs_begin_frame_", 610 scheduler_state->SetBoolean("last_set_needs_begin_frame_",
703 last_set_needs_begin_frame_); 611 last_set_needs_begin_frame_);
704 scheduler_state->SetBoolean("begin_unthrottled_frame_posted_", 612 scheduler_state->SetBoolean("begin_unthrottled_frame_posted_",
705 begin_unthrottled_frame_posted_); 613 begin_unthrottled_frame_posted_);
706 scheduler_state->SetBoolean("begin_retro_frame_posted_", 614 scheduler_state->SetBoolean("begin_retro_frame_posted_",
707 begin_retro_frame_posted_); 615 begin_retro_frame_posted_);
708 scheduler_state->SetInteger("begin_retro_frame_args_", 616 scheduler_state->SetInteger("begin_retro_frame_args_",
709 begin_retro_frame_args_.size()); 617 begin_retro_frame_args_.size());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 } 656 }
749 657
750 bool Scheduler::IsBeginMainFrameSentOrStarted() const { 658 bool Scheduler::IsBeginMainFrameSentOrStarted() const {
751 return (state_machine_.commit_state() == 659 return (state_machine_.commit_state() ==
752 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT || 660 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_SENT ||
753 state_machine_.commit_state() == 661 state_machine_.commit_state() ==
754 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED); 662 SchedulerStateMachine::COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED);
755 } 663 }
756 664
757 } // namespace cc 665 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698