Moving background animation ticking from LayerTreeHostImpl into the Scheduler.

Background ticking currently happens in the LayerTreeHostImpl, this makes it very hard to preserve the monotonicity guarantee needed by frame times. http://crrev.com/267783004 set up the scheduler to become the source of background ticks and this CL makes use of that functionality.

BUG=345459

Committed: https://crrev.com/4df3c4366015739a7c6b6c1539a8d7c9198e38ef
Cr-Commit-Position: refs/heads/master@{#302757}

Committed: https://crrev.com/185438b3e5e3f43ce2996da0022f510fbe193a6e
Cr-Commit-Position: refs/heads/master@{#303423}

Review URL: https://codereview.chromium.org/595973002

Cr-Commit-Position: refs/heads/master@{#303467}
diff --git a/cc/trees/single_thread_proxy.cc b/cc/trees/single_thread_proxy.cc
index 439304a..94a6f28 100644
--- a/cc/trees/single_thread_proxy.cc
+++ b/cc/trees/single_thread_proxy.cc
@@ -101,7 +101,6 @@
   if (scheduler_on_impl_thread_)
     scheduler_on_impl_thread_->SetVisible(layer_tree_host_impl_->visible());
   // Changing visibility could change ShouldComposite().
-  UpdateBackgroundAnimateTicking();
 }
 
 void SingleThreadProxy::RequestNewOutputSurface() {
@@ -161,6 +160,24 @@
   SetNeedsCommit();
 }
 
+void SingleThreadProxy::DoAnimate() {
+  // Don't animate if there is no root layer.
+  // TODO(mithro): Both Animate and UpdateAnimationState already have a
+  // "!active_tree_->root_layer()" check?
+  if (!layer_tree_host_impl_->active_tree()->root_layer()) {
+    return;
+  }
+
+  layer_tree_host_impl_->Animate(
+      layer_tree_host_impl_->CurrentBeginFrameArgs().frame_time);
+
+  // If animations are not visible, update the animation state now as it
+  // won't happen in DoComposite.
+  if (!layer_tree_host_impl_->AnimationsAreVisible()) {
+    layer_tree_host_impl_->UpdateAnimationState(true);
+  }
+}
+
 void SingleThreadProxy::DoCommit() {
   TRACE_EVENT0("cc", "SingleThreadProxy::DoCommit");
   DCHECK(Proxy::IsMainThread());
@@ -202,8 +219,6 @@
 
     layer_tree_host_impl_->CommitComplete();
 
-    UpdateBackgroundAnimateTicking();
-
 #if DCHECK_IS_ON
     // In the single-threaded case, the scale and scroll deltas should never be
     // touched on the impl layer tree.
@@ -322,7 +337,6 @@
   TRACE_EVENT1(
       "cc", "SingleThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw);
   DCHECK(Proxy::IsImplThread());
-  UpdateBackgroundAnimateTicking();
   if (scheduler_on_impl_thread_)
     scheduler_on_impl_thread_->SetCanDraw(can_draw);
 }
@@ -341,7 +355,9 @@
 }
 
 void SingleThreadProxy::SetNeedsAnimateOnImplThread() {
-  SetNeedsRedrawOnImplThread();
+  client_->ScheduleComposite();
+  if (scheduler_on_impl_thread_)
+    scheduler_on_impl_thread_->SetNeedsAnimate();
 }
 
 void SingleThreadProxy::SetNeedsManageTilesOnImplThread() {
@@ -414,7 +430,6 @@
                    weak_factory_.GetWeakPtr()));
   }
 
-  UpdateBackgroundAnimateTicking();
   timing_history_.DidActivateSyncTree();
 }
 
@@ -492,6 +507,8 @@
       layer_tree_host_impl_->SynchronouslyInitializeAllTiles();
     }
 
+    DoAnimate();
+
     LayerTreeHostImpl::FrameData frame;
     DoComposite(frame_begin_time, &frame);
 
@@ -533,12 +550,6 @@
          layer_tree_host_impl_->CanDraw();
 }
 
-void SingleThreadProxy::UpdateBackgroundAnimateTicking() {
-  DCHECK(Proxy::IsImplThread());
-  layer_tree_host_impl_->UpdateBackgroundAnimateTicking(
-      !ShouldComposite() && layer_tree_host_impl_->active_tree()->root_layer());
-}
-
 void SingleThreadProxy::ScheduleRequestNewOutputSurface() {
   if (output_surface_creation_callback_.IsCancelled() &&
       !output_surface_creation_requested_) {
@@ -566,16 +577,11 @@
     // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
     // CanDraw() as well.
     if (!ShouldComposite()) {
-      UpdateBackgroundAnimateTicking();
       return DRAW_ABORTED_CANT_DRAW;
     }
 
     timing_history_.DidStartDrawing();
 
-    layer_tree_host_impl_->Animate(
-        layer_tree_host_impl_->CurrentBeginFrameArgs().frame_time);
-    UpdateBackgroundAnimateTicking();
-
     draw_result = layer_tree_host_impl_->PrepareToDraw(frame);
     draw_frame = draw_result == DRAW_SUCCESS;
     if (draw_frame)
@@ -736,8 +742,8 @@
 
 void SingleThreadProxy::ScheduledActionAnimate() {
   TRACE_EVENT0("cc", "ScheduledActionAnimate");
-  layer_tree_host_impl_->Animate(
-      layer_tree_host_impl_->CurrentBeginFrameArgs().frame_time);
+  DebugScopedSetImplThread impl(this);
+  DoAnimate();
 }
 
 void SingleThreadProxy::ScheduledActionUpdateVisibleTiles() {