Move RenderThreadImpl's GpuChannelHost logic to ui::Gpu

RenderThreadImpl's logic has a fair amount of duplication with ui::Gpu.
In particular, RenderThreadImpl shouldn't need to cache the
GpuChannelHost, since it is available directly from the ui::Gpu.

Bug: 566273
Change-Id: I6cfb9f28b46bce64c4f83aa5f2fcb4263c171018
Reviewed-on: https://chromium-review.googlesource.com/767594
Reviewed-by: Sadrul Chowdhury <sadrul@chromium.org>
Commit-Queue: Antoine Labour <piman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516328}
diff --git a/content/renderer/render_thread_impl.cc b/content/renderer/render_thread_impl.cc
index 4d09510..d2fe278 100644
--- a/content/renderer/render_thread_impl.cc
+++ b/content/renderer/render_thread_impl.cc
@@ -1986,13 +1986,7 @@
 }
 
 void RenderThreadImpl::CompositingModeFallbackToSoftware() {
-  if (gpu_channel_) {
-    // TODO(danakj): Tell all clients of the compositor. We should send a more
-    // scoped message than this.
-    gpu_channel_->DestroyChannel();
-    gpu_channel_ = nullptr;
-  }
-
+  gpu_->LoseChannel();
   is_gpu_compositing_disabled_ = true;
 }
 
@@ -2000,21 +1994,11 @@
     bool* connection_error) {
   TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
 
-  if (gpu_channel_) {
-    // Do nothing if we already have a GPU channel or are already
-    // establishing one.
-    if (!gpu_channel_->IsLost())
-      return gpu_channel_;
-
-    // Recreate the channel if it has been lost.
-    gpu_channel_->DestroyChannel();
-    gpu_channel_ = nullptr;
-  }
-
-  gpu_channel_ = gpu_->EstablishGpuChannelSync(connection_error);
-  if (gpu_channel_)
-    GetContentClient()->SetGpuInfo(gpu_channel_->gpu_info());
-  return gpu_channel_;
+  scoped_refptr<gpu::GpuChannelHost> gpu_channel =
+      gpu_->EstablishGpuChannelSync(connection_error);
+  if (gpu_channel)
+    GetContentClient()->SetGpuInfo(gpu_channel->gpu_info());
+  return gpu_channel;
 }
 
 void RenderThreadImpl::RequestNewLayerTreeFrameSink(
@@ -2234,11 +2218,7 @@
 }
 
 gpu::GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
-  if (!gpu_channel_)
-    return nullptr;
-  if (gpu_channel_->IsLost())
-    return nullptr;
-  return gpu_channel_.get();
+  return gpu_->GetGpuChannel().get();
 }
 
 void RenderThreadImpl::CreateView(mojom::CreateViewParamsPtr params) {
diff --git a/content/renderer/render_thread_impl.h b/content/renderer/render_thread_impl.h
index 2456bad..4d92c81 100644
--- a/content/renderer/render_thread_impl.h
+++ b/content/renderer/render_thread_impl.h
@@ -729,9 +729,6 @@
   // software-based.
   bool is_gpu_compositing_disabled_ = false;
 
-  // The channel from the renderer process to the GPU process.
-  scoped_refptr<gpu::GpuChannelHost> gpu_channel_;
-
   // The message loop of the renderer main thread.
   // This message loop should be destructed before the RenderThreadImpl
   // shuts down Blink.
diff --git a/services/ui/public/cpp/gpu/gpu.cc b/services/ui/public/cpp/gpu/gpu.cc
index 62671a1..0c35cfe6 100644
--- a/services/ui/public/cpp/gpu/gpu.cc
+++ b/services/ui/public/cpp/gpu/gpu.cc
@@ -262,12 +262,18 @@
   return gpu_memory_buffer_manager_.get();
 }
 
-scoped_refptr<gpu::GpuChannelHost> Gpu::GetGpuChannel() {
+void Gpu::LoseChannel() {
   DCHECK(main_task_runner_->BelongsToCurrentThread());
-  if (gpu_channel_ && gpu_channel_->IsLost()) {
+  if (gpu_channel_) {
     gpu_channel_->DestroyChannel();
     gpu_channel_ = nullptr;
   }
+}
+
+scoped_refptr<gpu::GpuChannelHost> Gpu::GetGpuChannel() {
+  DCHECK(main_task_runner_->BelongsToCurrentThread());
+  if (gpu_channel_ && gpu_channel_->IsLost())
+    gpu_channel_ = nullptr;
   return gpu_channel_;
 }
 
diff --git a/services/ui/public/cpp/gpu/gpu.h b/services/ui/public/cpp/gpu/gpu.h
index 8598d0b..3aeff615 100644
--- a/services/ui/public/cpp/gpu/gpu.h
+++ b/services/ui/public/cpp/gpu/gpu.h
@@ -53,6 +53,9 @@
       bool* connection_error) override;
   gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() override;
 
+  void LoseChannel();
+  scoped_refptr<gpu::GpuChannelHost> GetGpuChannel();
+
  private:
   friend class GpuTest;
 
@@ -62,8 +65,6 @@
   Gpu(GpuPtrFactory factory,
       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
 
-  scoped_refptr<gpu::GpuChannelHost> GetGpuChannel();
-
   // Sends a request to establish a gpu channel. If a request is currently
   // pending this will do nothing.
   void SendEstablishGpuChannelRequest();