Revert 196708 "Merge cc initialization paths"

[Reason for revert: WebGLInfobarTest.ContextLossInfobarReload
was failing on the Linux Tests bot with high probability]

> Merge cc initialization paths
> 
> Code path between first initialization and recreate on context
> lost are merged. Do this by changing the first initialization
> behavior to match recreate. Now both are kicked off by the
> scheduler and blocks the main thread on the impl thread.
> 
> The scheduler is started in output surface lost state and
> immediately schedules recreation. This means the first
> initialization is no longer synchronous.
> 
> BUG=233664, 230197
> 
> First commited in r196480. Reverted in r196509 due to exposing
> WebGLInfobarTest that should never have passed on asan. Disable
> them on asan: https://codereview.chromium.org/14499007/
> 
> Review URL: https://chromiumcodereview.appspot.com/12544032

[email protected]

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@196790 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/cc/trees/single_thread_proxy.cc b/cc/trees/single_thread_proxy.cc
index 2ca82a46..f8ee95a0 100644
--- a/cc/trees/single_thread_proxy.cc
+++ b/cc/trees/single_thread_proxy.cc
@@ -25,7 +25,9 @@
 SingleThreadProxy::SingleThreadProxy(LayerTreeHost* layer_tree_host)
     : Proxy(scoped_ptr<Thread>(NULL)),
       layer_tree_host_(layer_tree_host),
+      output_surface_lost_(false),
       created_offscreen_context_provider_(false),
+      renderer_initialized_(false),
       next_frame_is_newly_committed_frame_(false),
       inside_draw_(false) {
   TRACE_EVENT0("cc", "SingleThreadProxy::SingleThreadProxy");
@@ -37,18 +39,16 @@
       << "Threaded compositing must be enabled to use impl-side painting.";
 }
 
-void SingleThreadProxy::Start(scoped_ptr<OutputSurface> first_output_surface) {
-  DCHECK(first_output_surface);
+void SingleThreadProxy::Start() {
   DebugScopedSetImplThread impl(this);
   layer_tree_host_impl_ = layer_tree_host_->CreateLayerTreeHostImpl(this);
-  first_output_surface_ = first_output_surface.Pass();
 }
 
 SingleThreadProxy::~SingleThreadProxy() {
   TRACE_EVENT0("cc", "SingleThreadProxy::~SingleThreadProxy");
   DCHECK(Proxy::IsMainThread());
-  // Make sure Stop() got called or never Started.
-  DCHECK(!layer_tree_host_impl_);
+  DCHECK(!layer_tree_host_impl_.get() &&
+         !layer_tree_host_);  // make sure Stop() got called.
 }
 
 bool SingleThreadProxy::CompositeAndReadback(void* pixels, gfx::Rect rect) {
@@ -90,6 +90,16 @@
   return layer_tree_host_impl_;
 }
 
+bool SingleThreadProxy::InitializeOutputSurface() {
+  DCHECK(Proxy::IsMainThread());
+  scoped_ptr<OutputSurface> output_surface =
+      layer_tree_host_->CreateOutputSurface();
+  if (!output_surface)
+    return false;
+  output_surface_before_initialization_ = output_surface.Pass();
+  return true;
+}
+
 void SingleThreadProxy::SetSurfaceReady() {
   // Scheduling is controlled by the embedder in the single thread case, so
   // nothing to do.
@@ -100,47 +110,51 @@
   layer_tree_host_impl_->SetVisible(visible);
 }
 
-void SingleThreadProxy::CreateAndInitializeOutputSurface() {
-  TRACE_EVENT0(
-      "cc", "SingleThreadProxy::CreateAndInitializeOutputSurface");
+bool SingleThreadProxy::InitializeRenderer() {
   DCHECK(Proxy::IsMainThread());
+  DCHECK(output_surface_before_initialization_.get());
+  {
+    DebugScopedSetImplThread impl(this);
+    bool ok = layer_tree_host_impl_->InitializeRenderer(
+        output_surface_before_initialization_.Pass());
+    if (ok) {
+      renderer_initialized_ = true;
+      renderer_capabilities_for_main_thread_ =
+          layer_tree_host_impl_->GetRendererCapabilities();
+    }
 
-  scoped_ptr<OutputSurface> output_surface = first_output_surface_.Pass();
-  if (!output_surface)
-    output_surface = layer_tree_host_->CreateOutputSurface();
-  if (!output_surface) {
-    OnOutputSurfaceInitializeAttempted(false);
-    return;
+    return ok;
   }
+}
 
+bool SingleThreadProxy::RecreateOutputSurface() {
+  TRACE_EVENT0("cc", "SingleThreadProxy::RecreateContext");
+  DCHECK(Proxy::IsMainThread());
+  DCHECK(output_surface_lost_);
+
+  scoped_ptr<OutputSurface> output_surface =
+      layer_tree_host_->CreateOutputSurface();
+  if (!output_surface)
+    return false;
   scoped_refptr<cc::ContextProvider> offscreen_context_provider;
   if (created_offscreen_context_provider_) {
     offscreen_context_provider =
         layer_tree_host_->client()->OffscreenContextProviderForMainThread();
-    if (!offscreen_context_provider) {
-      OnOutputSurfaceInitializeAttempted(false);
-      return;
-    }
+    if (!offscreen_context_provider)
+      return false;
   }
 
+  bool initialized;
   {
     DebugScopedSetMainThreadBlocked mainThreadBlocked(this);
     DebugScopedSetImplThread impl(this);
     layer_tree_host_->DeleteContentsTexturesOnImplThread(
         layer_tree_host_impl_->resource_provider());
-  }
-
-  bool initialized;
-  {
-    DebugScopedSetImplThread impl(this);
-
-    DCHECK(output_surface);
-    initialized = layer_tree_host_impl_->InitializeRenderer(
-        output_surface.Pass());
+    initialized =
+        layer_tree_host_impl_->InitializeRenderer(output_surface.Pass());
     if (initialized) {
       renderer_capabilities_for_main_thread_ =
           layer_tree_host_impl_->GetRendererCapabilities();
-
       layer_tree_host_impl_->resource_provider()->
           set_offscreen_context_provider(offscreen_context_provider);
     } else if (offscreen_context_provider) {
@@ -148,21 +162,15 @@
     }
   }
 
-  OnOutputSurfaceInitializeAttempted(initialized);
-}
+  if (initialized)
+    output_surface_lost_ = false;
 
-void SingleThreadProxy::OnOutputSurfaceInitializeAttempted(bool success) {
-  LayerTreeHost::CreateResult result =
-      layer_tree_host_->OnCreateAndInitializeOutputSurfaceAttempted(success);
-  if (result == LayerTreeHost::CreateFailedButTryAgain) {
-    // Force another recreation attempt to happen by requesting another commit.
-    SetNeedsCommit();
-  }
+  return initialized;
 }
 
 const RendererCapabilities& SingleThreadProxy::GetRendererCapabilities() const {
-  DCHECK(Proxy::IsMainThread());
-  DCHECK(!layer_tree_host_->output_surface_lost());
+  DCHECK(renderer_initialized_);
+  // Note: this gets called during the commit by the "impl" thread.
   return renderer_capabilities_for_main_thread_;
 }
 
@@ -360,10 +368,8 @@
 void SingleThreadProxy::ForceSerializeOnSwapBuffers() {
   {
     DebugScopedSetImplThread impl(this);
-    if (layer_tree_host_impl_->renderer()) {
-      DCHECK(!layer_tree_host_->output_surface_lost());
+    if (renderer_initialized_)
       layer_tree_host_impl_->renderer()->DoNoOp();
-    }
   }
 }
 
@@ -373,7 +379,7 @@
     LayerTreeHostImpl::FrameData* frame) {
   DCHECK(Proxy::IsMainThread());
 
-  if (!layer_tree_host_->InitializeOutputSurfaceIfNeeded())
+  if (!layer_tree_host_->InitializeRendererIfNeeded())
     return false;
 
   scoped_refptr<cc::ContextProvider> offscreen_context_provider;
@@ -413,9 +419,7 @@
     base::TimeTicks frame_begin_time,
     gfx::Rect device_viewport_damage_rect,
     LayerTreeHostImpl::FrameData* frame) {
-  DCHECK(!layer_tree_host_->output_surface_lost());
-
-  bool lost_output_surface = false;
+  DCHECK(!output_surface_lost_);
   {
     DebugScopedSetImplThread impl(this);
     base::AutoReset<bool> mark_inside(&inside_draw_, true);
@@ -440,7 +444,7 @@
     layer_tree_host_impl_->PrepareToDraw(frame, device_viewport_damage_rect);
     layer_tree_host_impl_->DrawLayers(frame, frame_begin_time);
     layer_tree_host_impl_->DidDrawAllLayers(*frame);
-    lost_output_surface = layer_tree_host_impl_->IsContextLost();
+    output_surface_lost_ = layer_tree_host_impl_->IsContextLost();
 
     bool start_ready_animations = true;
     layer_tree_host_impl_->UpdateAnimationState(start_ready_animations);
@@ -448,7 +452,7 @@
     layer_tree_host_impl_->BeginNextFrame();
   }
 
-  if (lost_output_surface) {
+  if (output_surface_lost_) {
     cc::ContextProvider* offscreen_contexts = layer_tree_host_impl_->
         resource_provider()->offscreen_context_provider();
     if (offscreen_contexts)