Allow shutdown GPU when not in use
There are roughly two changes in this CL.
First is in CompositorImpl and BrowserGpuChannelHostFactory to drop the
gpu channel from the browser process when all CompositorImpls are
destroyed. BGCHF will check if it has the only reference to the
GpuChannelHost, and if so, destroy it. The check is performed in
CompositorImpl destructor to minimize impact to chrome; it is possible
to consider checking when CompositorImpl becomes invisible in the
future.
Second piece is GpuChannelManager in the GPU process will send a message
when all GpuChannels are destroyed. This message is passed to
GpuHostImpl, which will has a timeout of 10s. If there are no pending
or new establish channel requests in this time, then it will tell
GpuProcessHost. GpuProcessHost will give the content embedder a chance
to decide whether to kill the GPU process, and if yes, shutdown
the GPU process.
There are different GPU shutdown paths. This implementation just
deletes GpuProcessHost. The benefit over paths that tell the GPU process
to quit its main loop is deleting GpuProcessHost can guarantee no
trasient failures. All establish channel request are routed through the
browser IO thread. GpuHostImpl makes sure to only shutdown when there
are no requests and the GpuProcessHost is deleted synchrnously on the IO
thread. So subsequent requests will start by creating a new
GpuProcessHost and launching a new GPU process.
On Android, deleting GpuProcessHost will remove all bindings to the GPU
process, at which point Android OS will kill the GPU process; most
desktop platforms will send sigterm, then after a timeout, sigkill to
the GPU process. This does not involve cleanly exiting the GPU main
loop, which in the past has caused crashes.
Enable this behavior in weblayer, which does not use any other
gpu-hosted services.
Bug: 1058509
Change-Id: I81055a118d0e9b3f7e8ed8203780ad1b124e3f6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2102592
Commit-Queue: Bo <[email protected]>
Reviewed-by: Robert Sesek <[email protected]>
Reviewed-by: Avi Drissman <[email protected]>
Reviewed-by: kylechar <[email protected]>
Reviewed-by: Khushal <[email protected]>
Cr-Commit-Position: refs/heads/master@{#750780}
diff --git a/components/viz/host/gpu_host_impl.h b/components/viz/host/gpu_host_impl.h
index 1620bb2..32f0657 100644
--- a/components/viz/host/gpu_host_impl.h
+++ b/components/viz/host/gpu_host_impl.h
@@ -18,6 +18,7 @@
#include "base/optional.h"
#include "base/process/process_handle.h"
#include "base/sequence_checker.h"
+#include "base/timer/timer.h"
#include "build/build_config.h"
#include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h"
#include "components/ui_devtools/buildflags.h"
@@ -65,6 +66,7 @@
const gpu::GpuExtraInfo& gpu_extra_info) = 0;
virtual void DidFailInitialize() = 0;
virtual void DidCreateContextSuccessfully() = 0;
+ virtual void MaybeShutdownGpuProcess() = 0;
#if defined(OS_WIN)
virtual void DidUpdateOverlayInfo(const gpu::OverlayInfo& overlay_info) = 0;
#endif
@@ -200,6 +202,7 @@
void OnChannelEstablished(int client_id,
mojo::ScopedMessagePipeHandle channel_handle);
+ void MaybeShutdownGpuProcess();
// mojom::GpuHost:
void DidInitialize(
@@ -214,6 +217,7 @@
void DidCreateOffscreenContext(const GURL& url) override;
void DidDestroyOffscreenContext(const GURL& url) override;
void DidDestroyChannel(int32_t client_id) override;
+ void DidDestroyAllChannels() override;
void DidLoseContext(bool offscreen,
gpu::error::ContextLostReason reason,
const GURL& active_url) override;
@@ -266,6 +270,8 @@
// service, but haven't heard back about yet.
base::queue<EstablishChannelCallback> channel_requests_;
+ base::OneShotTimer shutdown_timeout_;
+
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<GpuHostImpl> weak_ptr_factory_{this};