cc: Split ReleaseResources into Release and Recreate.

This patch splits the work we do in ReleaseResources into two parts:
ReleaseResources and RecreateResources. If any part of invalidating or
creating new resources requires access to new structures (such as a new
tile manager and new output surface), this is the way to get this
information.

That is, ReleaseResources happens while old objects are alive and
accessible. RecreateResources happens when the new objects are alive and
accessible.

R=danakj, enne

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

Cr-Commit-Position: refs/heads/master@{#314913}
diff --git a/cc/trees/layer_tree_host_impl.cc b/cc/trees/layer_tree_host_impl.cc
index f51c61a..672ffe8 100644
--- a/cc/trees/layer_tree_host_impl.cc
+++ b/cc/trees/layer_tree_host_impl.cc
@@ -116,7 +116,6 @@
   gfx::ScrollOffset viewport_in_content_coordinates_;
 };
 
-
 void DidVisibilityChange(LayerTreeHostImpl* id, bool visible) {
   if (visible) {
     TRACE_EVENT_ASYNC_BEGIN1("cc", "LayerTreeHostImpl::SetVisible", id,
@@ -1616,6 +1615,7 @@
     DestroyTileManager();
     CreateAndSetTileManager();
   }
+  RecreateTreeResources();
 
   // We have released tilings for both active and pending tree.
   // We would not have any content to draw until the pending tree is activated.
@@ -1934,6 +1934,14 @@
   EvictAllUIResources();
 }
 
+void LayerTreeHostImpl::RecreateTreeResources() {
+  active_tree_->RecreateResources();
+  if (pending_tree_)
+    pending_tree_->RecreateResources();
+  if (recycle_tree_)
+    recycle_tree_->RecreateResources();
+}
+
 void LayerTreeHostImpl::CreateAndSetRenderer() {
   DCHECK(!renderer_);
   DCHECK(output_surface_);
@@ -2125,8 +2133,12 @@
   resource_provider_ = nullptr;
   output_surface_ = nullptr;
 
-  if (!output_surface->BindToClient(this))
+  if (!output_surface->BindToClient(this)) {
+    // Avoid recreating tree resources because we might not have enough
+    // information to do this yet (eg. we don't have a TileManager at this
+    // point).
     return false;
+  }
 
   output_surface_ = output_surface.Pass();
   resource_provider_ = ResourceProvider::Create(
@@ -2143,6 +2155,7 @@
 
   if (settings_.impl_side_painting)
     CreateAndSetTileManager();
+  RecreateTreeResources();
 
   // Initialize vsync parameters to sane values.
   const base::TimeDelta display_refresh_interval =
@@ -2192,6 +2205,7 @@
   CreateAndSetRenderer();
   EnforceZeroBudget(false);
   CreateAndSetTileManager();
+  RecreateTreeResources();
 
   client_->SetNeedsCommitOnImplThread();
 }
@@ -2211,6 +2225,7 @@
   CreateAndSetRenderer();
   EnforceZeroBudget(true);
   CreateAndSetTileManager();
+  RecreateTreeResources();
 
   client_->SetNeedsCommitOnImplThread();
 }