Chromium Code Reviews| Index: cc/scheduler/scheduler.cc |
| diff --git a/cc/scheduler/scheduler.cc b/cc/scheduler/scheduler.cc |
| index 8329e8e9601b823f1293d842f7e55ed4fe7a40fe..c5fa211a0d6e213fb8c76583805f978d6c3edf9c 100644 |
| --- a/cc/scheduler/scheduler.cc |
| +++ b/cc/scheduler/scheduler.cc |
| @@ -4,6 +4,8 @@ |
| #include "cc/scheduler/scheduler.h" |
| +#include "base/strings/stringprintf.h" |
| +#include <algorithm> |
| #include "base/auto_reset.h" |
| #include "base/debug/trace_event.h" |
| #include "base/logging.h" |
| @@ -43,9 +45,12 @@ void Scheduler::SetCanDraw(bool can_draw) { |
| ProcessScheduledActions(); |
| } |
| -void Scheduler::SetHasPendingTree(bool has_pending_tree) { |
| - state_machine_.SetHasPendingTree(has_pending_tree); |
| +void Scheduler::SetHasTrees(bool has_pending_tree, bool active_tree_is_null) { |
| + state_machine_.SetHasTrees(has_pending_tree, active_tree_is_null); |
| ProcessScheduledActions(); |
| + |
| + if (state_machine_.ShouldTriggerBeginFrameDeadlineEarly()) |
| + PostBeginFrameDeadline(base::TimeTicks()); |
| } |
| void Scheduler::SetNeedsCommit() { |
| @@ -83,6 +88,9 @@ void Scheduler::FinishCommit() { |
| TRACE_EVENT0("cc", "Scheduler::FinishCommit"); |
| state_machine_.FinishCommit(); |
| ProcessScheduledActions(); |
| + |
| + if (state_machine_.ShouldTriggerBeginFrameDeadlineEarly()) |
| + PostBeginFrameDeadline(base::TimeTicks()); |
| } |
| void Scheduler::BeginFrameAbortedByMainThread() { |
| @@ -113,12 +121,11 @@ base::TimeTicks Scheduler::AnticipatedDrawTime() { |
| last_begin_frame_args_.interval <= base::TimeDelta()) |
| return base::TimeTicks(); |
| - // TODO(brianderson): Express this in terms of the deadline. |
| base::TimeTicks now = base::TimeTicks::Now(); |
| - int64 intervals = 1 + ((now - last_begin_frame_args_.frame_time) / |
| - last_begin_frame_args_.interval); |
| - return last_begin_frame_args_.frame_time + |
| - (last_begin_frame_args_.interval * intervals); |
| + base::TimeTicks timebase = std::max(last_begin_frame_args_.frame_time, |
| + last_begin_frame_args_.deadline); |
| + int64 intervals = 1 + ((now - timebase) / last_begin_frame_args_.interval); |
| + return timebase + (last_begin_frame_args_.interval * intervals); |
| } |
| base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() { |
| @@ -128,51 +135,74 @@ base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() { |
| void Scheduler::SetupNextBeginFrameIfNeeded() { |
| bool needs_begin_frame_to_draw = |
| state_machine_.BeginFrameNeededToDrawByImplThread(); |
| - // We want to avoid proactive begin frames with the synchronous compositor |
| - // because every SetNeedsBeginFrame will force a redraw. |
| bool proactive_begin_frame_wanted = |
| - state_machine_.ProactiveBeginFrameWantedByImplThread() && |
| - !settings_.using_synchronous_renderer_compositor; |
| + state_machine_.ProactiveBeginFrameWantedByImplThread(); |
| + |
| + // We want to avoid proactive begin frames with the synchronous |
| + // compositor because every SetNeedsBeginFrame will force a redraw.s |
| bool needs_begin_frame = needs_begin_frame_to_draw || |
| proactive_begin_frame_wanted; |
| - bool immediate_disables_needed = |
| - settings_.using_synchronous_renderer_compositor; |
| + |
| + bool should_call_set_needs_begin_frame = |
| + // The synchronous renderer compositor needs immediate enables/disables. |
| + (settings_.using_synchronous_renderer_compositor && |
| + needs_begin_frame != last_set_needs_begin_frame_) || |
| + // Always request the BeginFrame immediately if it wasn't needed before. |
| + (needs_begin_frame && !last_set_needs_begin_frame_) || |
| + // Only disable the BeginFrame after a BeginFrame where we didn't swap. |
| + (!needs_begin_frame && last_set_needs_begin_frame_ && |
| + has_pending_begin_frame_ && !state_machine_.InsideBeginFrame()) || |
| + // We did not draw and swap this BeginFrame, |
| + // so we need to explicitly request another BeginFrame. |
| + (needs_begin_frame && has_pending_begin_frame_ && |
| + state_machine_.InsideBeginFrame()); |
| if (needs_begin_frame_to_draw) |
| safe_to_expect_begin_frame_ = true; |
| - // Determine if we need BeginFrame notifications. |
| - // If we do, always request the BeginFrame immediately. |
| - // If not, only disable on the next BeginFrame to avoid unnecessary toggles. |
| - // The synchronous renderer compositor requires immediate disables though. |
| - if ((needs_begin_frame || |
| - state_machine_.inside_begin_frame() || |
| - immediate_disables_needed) && |
| - (needs_begin_frame != last_set_needs_begin_frame_)) { |
| + if (should_call_set_needs_begin_frame) { |
| has_pending_begin_frame_ = false; |
| client_->SetNeedsBeginFrameOnImplThread(needs_begin_frame); |
| if (safe_to_expect_begin_frame_) |
| last_set_needs_begin_frame_ = needs_begin_frame; |
| } |
| - |
| - // Request another BeginFrame if we haven't drawn for now until we have |
| - // deadlines implemented. |
| - if (state_machine_.inside_begin_frame() && has_pending_begin_frame_) { |
| - has_pending_begin_frame_ = false; |
| - client_->SetNeedsBeginFrameOnImplThread(true); |
| - return; |
| - } |
| } |
| void Scheduler::BeginFrame(const BeginFrameArgs& args) { |
| TRACE_EVENT0("cc", "Scheduler::BeginFrame"); |
| DCHECK(!has_pending_begin_frame_); |
| + if (has_pending_begin_frame_) { |
| + // TODO(brianderson): Fix contention with background ticking. |
| + begin_frame_deadline_closure_.Cancel(); |
| + OnBeginFrameDeadline(); |
| + } |
| has_pending_begin_frame_ = true; |
| safe_to_expect_begin_frame_ = true; |
| last_begin_frame_args_ = args; |
| - state_machine_.DidEnterBeginFrame(args); |
| + last_begin_frame_args_.AdjustDeadline(-client_->DrawDurationEstimate()); |
| + state_machine_.OnBeginFrame(last_begin_frame_args_); |
| ProcessScheduledActions(); |
| - state_machine_.DidLeaveBeginFrame(); |
| + |
| + if (state_machine_.ShouldTriggerBeginFrameDeadlineEarly()) |
| + PostBeginFrameDeadline(base::TimeTicks()); |
| + else |
| + PostBeginFrameDeadline(last_begin_frame_args_.deadline); |
| +} |
| + |
| +void Scheduler::PostBeginFrameDeadline(base::TimeTicks deadline) { |
| + begin_frame_deadline_closure_.Cancel(); |
| + begin_frame_deadline_closure_.Reset( |
| + base::Bind(&Scheduler::OnBeginFrameDeadline, weak_factory_.GetWeakPtr())); |
| + client_->PostBeginFrameDeadline( |
| + begin_frame_deadline_closure_.callback(), deadline); |
| +} |
| + |
| +void Scheduler::OnBeginFrameDeadline() { |
| + TRACE_EVENT0("cc", "Scheduler::OnBeginFrameDeadline"); |
| + begin_frame_deadline_closure_.Cancel(); |
| + state_machine_.OnBeginFrameDeadline(); |
| + ProcessScheduledActions(); |
| + client_->DidFinishBeginFrameOnImplThread(); |
| } |
| void Scheduler::DrawAndSwapIfPossible() { |
| @@ -198,8 +228,16 @@ void Scheduler::ProcessScheduledActions() { |
| base::AutoReset<bool> mark_inside(&inside_process_scheduled_actions_, true); |
| - SchedulerStateMachine::Action action = state_machine_.NextAction(); |
| - while (action != SchedulerStateMachine::ACTION_NONE) { |
| + SchedulerStateMachine::Action action; |
| + do { |
| + std::string str; |
| + base::StringAppendF(&str, "SchedulerStateMachine : ticks %s", |
| + state_machine_.ToString().c_str()); |
| + TRACE_EVENT0("cc", str.c_str()); |
|
Sami
2013/07/02 10:28:06
Can we trace strings like this nowadays? I remembe
danakj
2013/07/02 13:24:24
I believe there are COPY versions of the macros to
|
| + |
| + //TRACE_EVENT1("cc", "SchedulerStateMachine", |
|
Sami
2013/07/02 10:28:06
We could also uncomment this and put it to the dis
|
| + // "state", state_machine_.ToString()); |
| + action = state_machine_.NextAction(); |
| state_machine_.UpdateState(action); |
| switch (action) { |
| case SchedulerStateMachine::ACTION_NONE: |
| @@ -229,8 +267,7 @@ void Scheduler::ProcessScheduledActions() { |
| client_->ScheduledActionAcquireLayerTexturesForMainThread(); |
| break; |
| } |
| - action = state_machine_.NextAction(); |
| - } |
| + } while (action != SchedulerStateMachine::ACTION_NONE); |
| SetupNextBeginFrameIfNeeded(); |
| client_->DidAnticipatedDrawTimeChange(AnticipatedDrawTime()); |