Commit 6626eab7 authored by kylechar's avatar kylechar Committed by Commit Bot

Use VizCompositorThreadRunner in VizMainImpl.

Add missing code VizCompositorThreadRunner to support GPU compositing
and GPU process force shutdown. Remove code in VizMainImpl and just use
VizCompositorThreadRunner there.

Bug: 849639
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel
Change-Id: I34ae6adec13d8be11e563f2c32de36504b6711b4
Reviewed-on: https://chromium-review.googlesource.com/1165842
Commit-Queue: kylechar <kylechar@chromium.org>
Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Cr-Commit-Position: refs/heads/master@{#583827}
parent 31199d36
......@@ -14,8 +14,12 @@
#include "build/build_config.h"
#include "components/viz/common/switches.h"
#include "components/viz/service/display_embedder/gpu_display_provider.h"
#include "components/viz/service/display_embedder/in_process_gpu_memory_buffer_manager.h"
#include "components/viz/service/display_embedder/server_shared_bitmap_manager.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/service/gl/gpu_service_impl.h"
#include "gpu/ipc/command_buffer_task_executor.h"
#include "gpu/ipc/service/gpu_memory_buffer_factory.h"
#include "ui/gfx/switches.h"
namespace viz {
......@@ -23,7 +27,13 @@ namespace {
const char kThreadName[] = "VizCompositorThread";
std::unique_ptr<base::Thread> CreateAndStartCompositorThread() {
std::unique_ptr<VizCompositorThreadType> CreateAndStartCompositorThread() {
#if defined(OS_ANDROID)
auto thread = std::make_unique<base::android::JavaHandlerThread>(
kThreadName, base::ThreadPriority::DISPLAY);
thread->Start();
return thread;
#else // !defined(OS_ANDROID)
auto thread = std::make_unique<base::Thread>(kThreadName);
base::Thread::Options thread_options;
......@@ -37,6 +47,7 @@ std::unique_ptr<base::Thread> CreateAndStartCompositorThread() {
CHECK(thread->StartWithOptions(thread_options));
return thread;
#endif // !defined(OS_ANDROID)
}
} // namespace
......@@ -59,27 +70,72 @@ void VizCompositorThreadRunner::CreateFrameSinkManager(
FROM_HERE,
base::BindOnce(
&VizCompositorThreadRunner::CreateFrameSinkManagerOnCompositorThread,
base::Unretained(this), std::move(params)));
base::Unretained(this), std::move(params), nullptr, nullptr, nullptr,
nullptr));
}
void VizCompositorThreadRunner::CreateFrameSinkManager(
mojom::FrameSinkManagerParamsPtr params,
scoped_refptr<gpu::CommandBufferTaskExecutor> task_executor,
GpuServiceImpl* gpu_service) {
auto* gpu_channel_manager = gpu_service->gpu_channel_manager();
auto* image_factory = gpu_service->gpu_image_factory();
// All of the unretained objects are owned on the GPU thread and destroyed
// after VizCompositorThread has been shutdown.
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
&VizCompositorThreadRunner::CreateFrameSinkManagerOnCompositorThread,
base::Unretained(this), std::move(params), std::move(task_executor),
base::Unretained(gpu_service), base::Unretained(image_factory),
base::Unretained(gpu_channel_manager)));
}
void VizCompositorThreadRunner::CleanupForShutdown(
base::OnceClosure cleanup_finished_callback) {
task_runner_->PostTaskAndReply(
FROM_HERE,
base::BindOnce(
&VizCompositorThreadRunner::CleanupForShutdownOnCompositorThread,
base::Unretained(this)),
std::move(cleanup_finished_callback));
}
void VizCompositorThreadRunner::CreateFrameSinkManagerOnCompositorThread(
mojom::FrameSinkManagerParamsPtr params) {
mojom::FrameSinkManagerParamsPtr params,
scoped_refptr<gpu::CommandBufferTaskExecutor> task_executor,
GpuServiceImpl* gpu_service,
gpu::ImageFactory* image_factory,
gpu::GpuChannelManager* gpu_channel_manager) {
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(!frame_sink_manager_);
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
server_shared_bitmap_manager_ = std::make_unique<ServerSharedBitmapManager>();
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
server_shared_bitmap_manager_.get(), "ServerSharedBitmapManager",
base::ThreadTaskRunnerHandle::Get());
task_runner_);
// Create GpuDisplayProvider usable for software compositing only.
display_provider_ = std::make_unique<GpuDisplayProvider>(
params->restart_id, server_shared_bitmap_manager_.get(),
command_line->HasSwitch(switches::kHeadless),
command_line->HasSwitch(switches::kRunAllCompositorStagesBeforeDraw));
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
const bool headless = command_line->HasSwitch(switches::kHeadless);
const bool run_all_compositor_stages_before_draw =
command_line->HasSwitch(switches::kRunAllCompositorStagesBeforeDraw);
if (task_executor) {
// Create DisplayProvider usable for GPU + software compositing.
display_provider_ = std::make_unique<GpuDisplayProvider>(
params->restart_id, gpu_service, std::move(task_executor), gpu_service,
std::make_unique<InProcessGpuMemoryBufferManager>(gpu_channel_manager),
image_factory, server_shared_bitmap_manager_.get(), headless,
run_all_compositor_stages_before_draw);
} else {
// Create DisplayProvider usable for software compositing only.
display_provider_ = std::make_unique<GpuDisplayProvider>(
params->restart_id, server_shared_bitmap_manager_.get(), headless,
run_all_compositor_stages_before_draw);
}
// Create FrameSinkManagerImpl.
base::Optional<uint32_t> activation_deadline_in_frames;
if (params->use_activation_deadline)
activation_deadline_in_frames = params->activation_deadline_in_frames;
......@@ -92,6 +148,13 @@ void VizCompositorThreadRunner::CreateFrameSinkManagerOnCompositorThread(
std::move(params->frame_sink_manager_client)));
}
void VizCompositorThreadRunner::CleanupForShutdownOnCompositorThread() {
DCHECK(task_runner_->BelongsToCurrentThread());
if (frame_sink_manager_)
frame_sink_manager_->ForceShutdown();
}
void VizCompositorThreadRunner::TearDownOnCompositorThread() {
DCHECK(task_runner_->BelongsToCurrentThread());
......
......@@ -9,37 +9,79 @@
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "components/viz/service/viz_service_export.h"
#include "services/viz/privileged/interfaces/viz_main.mojom.h"
#if defined(OS_ANDROID)
#include "base/android/java_handler_thread.h"
#endif
namespace base {
class SingleThreadTaskRunner;
class Thread;
} // namespace base
namespace gpu {
class CommandBufferTaskExecutor;
class GpuChannelManager;
class ImageFactory;
} // namespace gpu
namespace viz {
class DisplayProvider;
class FrameSinkManagerImpl;
class GpuServiceImpl;
class ServerSharedBitmapManager;
#if defined(OS_ANDROID)
using VizCompositorThreadType = base::android::JavaHandlerThread;
#else
using VizCompositorThreadType = base::Thread;
#endif
// Starts and runs the VizCompositorThread. The thread will be started when this
// object is constructed. Objects on the thread will be initialized after
// calling CreateFrameSinkManager(). Destructor will teardown objects on thread
// and then stop the thread.
// TODO(kylechar): Convert VizMainImpl to use VizCompositorThreadRunner.
class VIZ_SERVICE_EXPORT VizCompositorThreadRunner {
public:
VizCompositorThreadRunner();
// Performs teardown on thread and then stops thread.
~VizCompositorThreadRunner();
// Create FrameSinkManager from |params|. This can be called from the thread
// that owns |this| to initialize state on VizCompositorThreadRunner.
// Returns the TaskRunner for VizCompositorThread.
base::SingleThreadTaskRunner* task_runner() { return task_runner_.get(); }
// Creates FrameSinkManager from |params|. The version with |gpu_service| and
// |task_executor| supports both GPU and software compositing, while the
// version without supports only software compositing. Should be called from
// the thread that owns |this| to initialize state on VizCompositorThread.
void CreateFrameSinkManager(mojom::FrameSinkManagerParamsPtr params);
void CreateFrameSinkManager(
mojom::FrameSinkManagerParamsPtr params,
scoped_refptr<gpu::CommandBufferTaskExecutor> task_executor,
GpuServiceImpl* gpu_service);
// Performs cleanup on VizCompositorThread needed before forcing thread to
// shut down. Ensures VizCompositorThread teardown during the destructor
// doesn't block on PostTasks back to the GPU thread. After cleanup has
// finished |cleanup_finished_callback| will be run. Should be called from the
// thread that owns |this|.
//
// This is intended to be used when the GPU thread wants to force restart. The
// cleanup is normally handled by the browser process before GPU process
// shutdown, except if the GPU thread is forcing restart.
void CleanupForShutdown(base::OnceClosure cleanup_finished_callback);
private:
void CreateFrameSinkManagerOnCompositorThread(
mojom::FrameSinkManagerParamsPtr params);
mojom::FrameSinkManagerParamsPtr params,
scoped_refptr<gpu::CommandBufferTaskExecutor> task_executor,
GpuServiceImpl* gpu_service,
gpu::ImageFactory* image_factory,
gpu::GpuChannelManager* gpu_channel_manager);
void CleanupForShutdownOnCompositorThread();
void TearDownOnCompositorThread();
// Start variables to be accessed only on |task_runner_|.
......@@ -48,7 +90,7 @@ class VIZ_SERVICE_EXPORT VizCompositorThreadRunner {
std::unique_ptr<FrameSinkManagerImpl> frame_sink_manager_;
// End variables to be accessed only on |task_runner_|.
std::unique_ptr<base::Thread> thread_;
std::unique_ptr<VizCompositorThreadType> thread_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(VizCompositorThreadRunner);
......
......@@ -10,6 +10,7 @@
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include "components/viz/service/main/viz_compositor_thread_runner.h"
#include "gpu/ipc/in_process_command_buffer.h"
#include "gpu/ipc/service/gpu_init.h"
#include "mojo/public/cpp/bindings/associated_binding_set.h"
......@@ -18,10 +19,6 @@
#include "services/viz/privileged/interfaces/viz_main.mojom.h"
#include "ui/gfx/font_render_params.h"
#if defined(OS_ANDROID)
#include "base/android/java_handler_thread.h"
#endif
namespace gpu {
class SyncPointManager;
} // namespace gpu
......@@ -35,10 +32,7 @@ class MojoUkmRecorder;
}
namespace viz {
class DisplayProvider;
class FrameSinkManagerImpl;
class GpuServiceImpl;
class ServerSharedBitmapManager;
#if defined(OS_ANDROID)
using CompositorThreadType = base::android::JavaHandlerThread;
......@@ -121,17 +115,8 @@ class VizMainImpl : public gpu::GpuSandboxHelper, public mojom::VizMain {
void CreateUkmRecorderIfNeeded(service_manager::Connector* connector);
void CreateFrameSinkManagerInternal(mojom::FrameSinkManagerParamsPtr params);
void CreateFrameSinkManagerOnCompositorThread(
mojom::FrameSinkManagerParamsPtr params);
void TearDownOnCompositorThread();
// Performs necessary cleanup on the compositor thread to allow for a clean
// process exit.
void CleanupForShutdownOnCompositorThread();
// Cleanly exits the process. This is only used with OOP-D when there is a
// compositor thread.
// Cleanly exits the process.
void ExitProcess();
// gpu::GpuSandboxHelper:
......@@ -162,24 +147,24 @@ class VizMainImpl : public gpu::GpuSandboxHelper, public mojom::VizMain {
// This is created for OOP-D only. It allows the display compositor to use
// InProcessCommandBuffer to send GPU commands to the GPU thread from the
// compositor thread.
// TODO(kylechar): The only reason this member variable exists is so the last
// reference is released and the object is destroyed on the GPU thread. This
// works because |task_executor_| is destroyed after the VizCompositorThread
// has been shutdown. All usage of CommandBufferTaskExecutor has the same
// pattern, where the last scoped_refptr is released on the GPU thread after
// all InProcessCommandBuffers are destroyed, so the class doesn't need to be
// RefCountedThreadSafe.
scoped_refptr<gpu::CommandBufferTaskExecutor> task_executor_;
// If the gpu service is not yet ready then we stash pending
// FrameSinkManagerParams.
mojom::FrameSinkManagerParamsPtr pending_frame_sink_manager_params_;
// Provides mojo interfaces for creating and managing FrameSinks. These live
// on the compositor thread.
std::unique_ptr<ServerSharedBitmapManager> server_shared_bitmap_manager_;
std::unique_ptr<DisplayProvider> display_provider_;
std::unique_ptr<FrameSinkManagerImpl> frame_sink_manager_;
// Runs the VizCompositorThread for the display compositor with OOP-D.
std::unique_ptr<VizCompositorThreadRunner> viz_compositor_thread_runner_;
const scoped_refptr<base::SingleThreadTaskRunner> gpu_thread_task_runner_;
// The main thread for the display compositor.
std::unique_ptr<CompositorThreadType> compositor_thread_;
scoped_refptr<base::SingleThreadTaskRunner> compositor_thread_task_runner_;
std::unique_ptr<ukm::MojoUkmRecorder> ukm_recorder_;
std::unique_ptr<base::PowerMonitor> power_monitor_;
mojo::Binding<mojom::VizMain> binding_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment