Skip to content

Commit

Permalink
Add message_loop_type to GpuPreferences
Browse files Browse the repository at this point in the history
Currently, the GPU process checks ui::OzonePlatform to determine the
type of message loop it should use. This means GPU process needs to
access ui::OzonePlatform instance before the instance is properly
initialized. If we check ui::OzonePlatform on the GPU host side and
send it to the GPU process via GpuPreferences, we can avoid accessing
ui::OzonePlatform before it being initialized.

BUG=958387

Change-Id: I7817cd317455f24fb55c5c8f5b8ea078a51b84b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1678160
Reviewed-by: kylechar <kylechar@chromium.org>
Reviewed-by: Ken Rockot <rockot@google.com>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Zhenyao Mo <zmo@chromium.org>
Commit-Queue: Mohsen Izadi <mohsen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#677743}
  • Loading branch information
Mohsen Izadi authored and Commit Bot committed Jul 16, 2019
1 parent 4509ea8 commit d66900e
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 31 deletions.
4 changes: 3 additions & 1 deletion components/viz/demo/service/demo_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <utility>

#include "base/message_loop/message_loop.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/service/main/viz_compositor_thread_runner.h"

Expand All @@ -20,7 +21,8 @@ DemoService::DemoService(viz::mojom::FrameSinkManagerRequest request,
params->activation_deadline_in_frames = 0u;
params->frame_sink_manager = std::move(request);
params->frame_sink_manager_client = client.PassInterface();
runner_ = std::make_unique<viz::VizCompositorThreadRunner>();
runner_ = std::make_unique<viz::VizCompositorThreadRunner>(
base::MessageLoop::TYPE_DEFAULT);
runner_->CreateFrameSinkManager(std::move(params));
}

Expand Down
18 changes: 6 additions & 12 deletions components/viz/service/main/viz_compositor_thread_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@
#include "components/ui_devtools/viz/overlay_agent_viz.h"
#endif

#if defined(USE_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif

namespace viz {
namespace {

const char kThreadName[] = "VizCompositorThread";

std::unique_ptr<VizCompositorThreadType> CreateAndStartCompositorThread() {
std::unique_ptr<VizCompositorThreadType> CreateAndStartCompositorThread(
base::MessageLoop::Type message_loop_type) {
const base::ThreadPriority thread_priority =
base::FeatureList::IsEnabled(features::kGpuUseDisplayThreadPriority)
? base::ThreadPriority::DISPLAY
Expand All @@ -55,11 +52,7 @@ std::unique_ptr<VizCompositorThreadType> CreateAndStartCompositorThread() {
auto thread = std::make_unique<base::Thread>(kThreadName);

base::Thread::Options thread_options;
#if defined(USE_OZONE)
// We may need a non-default message loop type for the platform surface.
thread_options.message_loop_type =
ui::OzonePlatform::GetInstance()->GetMessageLoopTypeForGpu();
#endif
thread_options.message_loop_type = message_loop_type;
thread_options.priority = thread_priority;

CHECK(thread->StartWithOptions(thread_options));
Expand All @@ -69,8 +62,9 @@ std::unique_ptr<VizCompositorThreadType> CreateAndStartCompositorThread() {

} // namespace

VizCompositorThreadRunner::VizCompositorThreadRunner()
: thread_(CreateAndStartCompositorThread()),
VizCompositorThreadRunner::VizCompositorThreadRunner(
base::MessageLoop::Type message_loop_type)
: thread_(CreateAndStartCompositorThread(message_loop_type)),
task_runner_(thread_->task_runner()) {}

VizCompositorThreadRunner::~VizCompositorThreadRunner() {
Expand Down
3 changes: 2 additions & 1 deletion components/viz/service/main/viz_compositor_thread_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/message_loop/message_loop.h"
#include "build/build_config.h"
#include "services/network/public/mojom/tcp_socket.mojom.h"
#include "services/viz/privileged/interfaces/viz_main.mojom.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ using VizCompositorThreadType = base::Thread;
// and then stop the thread.
class VizCompositorThreadRunner {
public:
VizCompositorThreadRunner();
explicit VizCompositorThreadRunner(base::MessageLoop::Type message_loop_type);
// Performs teardown on thread and then stops thread.
~VizCompositorThreadRunner();

Expand Down
4 changes: 2 additions & 2 deletions components/viz/service/main/viz_main_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ VizMainImpl::VizMainImpl(Delegate* delegate,
if (!dependencies_.io_thread_task_runner)
io_thread_ = CreateAndStartIOThread();
if (dependencies_.create_display_compositor) {
viz_compositor_thread_runner_ =
std::make_unique<VizCompositorThreadRunner>();
viz_compositor_thread_runner_ = std::make_unique<VizCompositorThreadRunner>(
gpu_init_->gpu_preferences().message_loop_type);
if (delegate_) {
delegate_->PostCompositorThreadCreated(
viz_compositor_thread_runner_->task_runner());
Expand Down
4 changes: 3 additions & 1 deletion content/browser/compositor/viz_process_transport_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/debug/dump_without_crashing.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/task/post_task.h"
#include "cc/mojo_embedder/async_layer_tree_frame_sink.h"
Expand Down Expand Up @@ -189,7 +190,8 @@ void VizProcessTransportFactory::ConnectHostFrameSinkManager() {

// GPU process access is disabled. Start a new thread to run the display
// compositor in-process and connect HostFrameSinkManager to it.
viz_compositor_thread_ = std::make_unique<viz::VizCompositorThreadRunner>();
viz_compositor_thread_ = std::make_unique<viz::VizCompositorThreadRunner>(
base::MessageLoop::TYPE_DEFAULT);

viz::mojom::FrameSinkManagerParamsPtr params =
viz::mojom::FrameSinkManagerParams::New();
Expand Down
2 changes: 1 addition & 1 deletion content/browser/gpu/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# embedded in mus won't be able to talk to the native ozone.
specific_include_rules = {
"gpu_data_manager_impl_private\.cc": [
"+ui/ozone/public/ozone_switches.h",
"+ui/ozone/public",
],
"gpu_process_host\.cc": [
"+ui/ozone/public",
Expand Down
8 changes: 7 additions & 1 deletion content/browser/gpu/gpu_data_manager_impl_private.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#include "ui/gl/gpu_switching_manager.h"

#if defined(USE_OZONE)
#include "ui/ozone/public/ozone_switches.h"
#include "ui/ozone/public/ozone_platform.h"
#endif
#if defined(OS_MACOSX)
#include <ApplicationServices/ApplicationServices.h>
Expand Down Expand Up @@ -278,6 +278,7 @@ void UpdateDx12VulkanInfoOnIO(
dx12_vulkan_version_info));
}
#endif

} // anonymous namespace

GpuDataManagerImplPrivate::GpuDataManagerImplPrivate(GpuDataManagerImpl* owner)
Expand Down Expand Up @@ -645,6 +646,11 @@ void GpuDataManagerImplPrivate::UpdateGpuPreferences(
gpu_preferences->disable_gpu_watchdog = true;
}
#endif

#if defined(USE_OZONE)
gpu_preferences->message_loop_type =
ui::OzonePlatform::GetInstance()->GetMessageLoopTypeForGpu();
#endif
}

void GpuDataManagerImplPrivate::DisableHardwareAcceleration() {
Expand Down
9 changes: 3 additions & 6 deletions content/gpu/gpu_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
#include "services/service_manager/sandbox/mac/sandbox_mac.h"
#endif

#if defined(USE_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif

#if BUILDFLAG(USE_VAAPI)
#include "media/gpu/vaapi/vaapi_wrapper.h"
#endif
Expand Down Expand Up @@ -276,8 +272,9 @@ int GpuMain(const MainFunctionParams& parameters) {
#elif defined(USE_OZONE)
// The MessagePump type required depends on the Ozone platform selected at
// runtime.
main_thread_task_executor.reset(new base::SingleThreadTaskExecutor(
ui::OzonePlatform::EnsureInstance()->GetMessageLoopTypeForGpu()));
main_thread_task_executor =
std::make_unique<base::SingleThreadTaskExecutor>(
gpu_preferences.message_loop_type);
#elif defined(OS_LINUX)
#error "Unsupported Linux platform."
#elif defined(OS_MACOSX)
Expand Down
4 changes: 4 additions & 0 deletions gpu/config/gpu_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "build/build_config.h"
#include "gpu/gpu_export.h"
#include "media/media_buildflags.h"
Expand Down Expand Up @@ -214,6 +215,9 @@ struct GPU_EXPORT GpuPreferences {
// Enable the WebGPU command buffer.
bool enable_webgpu = false;

// Determines message loop type for the GPU thread.
base::MessageLoop::Type message_loop_type = base::MessageLoop::TYPE_DEFAULT;

// Please update gpu_preferences_unittest.cc when making additions or
// changes to this struct.
};
Expand Down
16 changes: 10 additions & 6 deletions gpu/config/gpu_preferences_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void CheckGpuPreferencesEqual(GpuPreferences left, GpuPreferences right) {
EXPECT_EQ(left.enable_gpu_benchmarking_extension,
right.enable_gpu_benchmarking_extension);
EXPECT_EQ(left.enable_webgpu, right.enable_webgpu);
EXPECT_EQ(left.message_loop_type, right.message_loop_type);
}

} // namespace
Expand Down Expand Up @@ -105,11 +106,11 @@ TEST(GpuPreferencesTest, EncodeDecode) {
prefs_mojom.name = value; \
EXPECT_EQ(input_prefs.name, prefs_mojom.name);

#define GPU_PREFERENCES_FIELD_ENUM(name, value) \
input_prefs.name = value; \
EXPECT_NE(default_prefs.name, input_prefs.name); \
prefs_mojom.name = mojom::value; \
EXPECT_EQ(static_cast<uint32_t>(input_prefs.name), \
#define GPU_PREFERENCES_FIELD_ENUM(name, value, mojom_value) \
input_prefs.name = value; \
EXPECT_NE(default_prefs.name, input_prefs.name); \
prefs_mojom.name = mojom_value; \
EXPECT_EQ(static_cast<uint32_t>(input_prefs.name), \
static_cast<uint32_t>(prefs_mojom.name));

GPU_PREFERENCES_FIELD(disable_accelerated_video_decode, true)
Expand Down Expand Up @@ -150,9 +151,12 @@ TEST(GpuPreferencesTest, EncodeDecode) {
GPU_PREFERENCES_FIELD(enable_oop_rasterization, true)
GPU_PREFERENCES_FIELD(disable_oop_rasterization, true)
GPU_PREFERENCES_FIELD(watchdog_starts_backgrounded, true)
GPU_PREFERENCES_FIELD_ENUM(use_vulkan, VulkanImplementationName::kNative)
GPU_PREFERENCES_FIELD_ENUM(use_vulkan, VulkanImplementationName::kNative,
mojom::VulkanImplementationName::kNative)
GPU_PREFERENCES_FIELD(enable_gpu_benchmarking_extension, true)
GPU_PREFERENCES_FIELD(enable_webgpu, true)
GPU_PREFERENCES_FIELD_ENUM(message_loop_type, base::MessageLoop::TYPE_UI,
base::MessageLoop::TYPE_UI)

input_prefs.texture_target_exception_list.emplace_back(
gfx::BufferUsage::SCANOUT, gfx::BufferFormat::RGBA_8888);
Expand Down
2 changes: 2 additions & 0 deletions gpu/ipc/common/gpu_preferences.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// gpu/config/gpu_preferences.h
module gpu.mojom;

import "mojo/public/mojom/base/message_loop_type.mojom";
import "ui/gfx/mojo/buffer_types.mojom";

// Corresponds to gpu::VulkanImplementationName.
Expand Down Expand Up @@ -67,4 +68,5 @@ struct GpuPreferences {
bool enable_metal;
bool enable_gpu_benchmarking_extension;
bool enable_webgpu;
mojo_base.mojom.MessageLoopType message_loop_type;
};
8 changes: 8 additions & 0 deletions gpu/ipc/common/gpu_preferences_struct_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include <vector>

#include "base/message_loop/message_loop.h"
#include "gpu/config/gpu_preferences.h"
#include "gpu/ipc/common/gpu_preferences.mojom.h"
#include "mojo/public/cpp/base/message_loop_type_mojom_traits.h"
#include "ui/gfx/mojo/buffer_types_struct_traits.h"

namespace mojo {
Expand Down Expand Up @@ -119,6 +121,8 @@ struct StructTraits<gpu::mojom::GpuPreferencesDataView, gpu::GpuPreferences> {
out->enable_gpu_benchmarking_extension =
prefs.enable_gpu_benchmarking_extension();
out->enable_webgpu = prefs.enable_webgpu();
if (!prefs.ReadMessageLoopType(&out->message_loop_type))
return false;
return true;
}

Expand Down Expand Up @@ -268,6 +272,10 @@ struct StructTraits<gpu::mojom::GpuPreferencesDataView, gpu::GpuPreferences> {
static bool enable_webgpu(const gpu::GpuPreferences& prefs) {
return prefs.enable_webgpu;
}
static base::MessageLoop::Type message_loop_type(
const gpu::GpuPreferences& prefs) {
return prefs.message_loop_type;
}
};

} // namespace mojo
Expand Down
15 changes: 15 additions & 0 deletions mojo/public/cpp/base/message_loop_type.typemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

mojom = "//mojo/public/mojom/base/message_loop_type.mojom"
public_headers = [ "//base/message_loop/message_loop.h" ]
traits_headers = [ "//mojo/public/cpp/base/message_loop_type_mojom_traits.h" ]
sources = [
"//mojo/public/cpp/base/message_loop_type_mojom_traits.cc",
"//mojo/public/cpp/base/message_loop_type_mojom_traits.h",
]
public_deps = [
"//base",
]
type_mappings = [ "mojo_base.mojom.MessageLoopType=base::MessageLoop::Type" ]
76 changes: 76 additions & 0 deletions mojo/public/cpp/base/message_loop_type_mojom_traits.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/base/message_loop_type_mojom_traits.h"
#include "build/build_config.h"

namespace mojo {

// static
mojo_base::mojom::MessageLoopType
EnumTraits<mojo_base::mojom::MessageLoopType, base::MessageLoop::Type>::ToMojom(
base::MessageLoop::Type input) {
switch (input) {
case base::MessageLoop::TYPE_DEFAULT:
return mojo_base::mojom::MessageLoopType::kDefault;
case base::MessageLoop::TYPE_UI:
return mojo_base::mojom::MessageLoopType::kUi;
case base::MessageLoop::TYPE_CUSTOM:
return mojo_base::mojom::MessageLoopType::kCustom;
case base::MessageLoop::TYPE_IO:
return mojo_base::mojom::MessageLoopType::kIo;
#if defined(OS_ANDROID)
case base::MessageLoop::TYPE_JAVA:
return mojo_base::mojom::MessageLoopType::kJava;
#endif
#if defined(OS_MACOSX)
case base::MessageLoop::Type::NS_RUNLOOP:
return mojo_base::mojom::MessageLoopType::kNsRunloop;
#endif
#if defined(OS_WIN)
case base::MessageLoop::Type::UI_WITH_WM_QUIT_SUPPORT:
return mojo_base::mojom::MessageLoopType::kUiWithWmQuitSupport;
#endif
}
NOTREACHED();
return mojo_base::mojom::MessageLoopType::kDefault;
}

// static
bool EnumTraits<mojo_base::mojom::MessageLoopType, base::MessageLoop::Type>::
FromMojom(mojo_base::mojom::MessageLoopType input,
base::MessageLoop::Type* output) {
switch (input) {
case mojo_base::mojom::MessageLoopType::kDefault:
*output = base::MessageLoop::TYPE_DEFAULT;
return true;
case mojo_base::mojom::MessageLoopType::kUi:
*output = base::MessageLoop::TYPE_UI;
return true;
case mojo_base::mojom::MessageLoopType::kCustom:
*output = base::MessageLoop::TYPE_CUSTOM;
return true;
case mojo_base::mojom::MessageLoopType::kIo:
*output = base::MessageLoop::TYPE_IO;
return true;
#if defined(OS_ANDROID)
case mojo_base::mojom::MessageLoopType::kJava:
*output = base::MessageLoop::TYPE_JAVA;
return true;
#endif
#if defined(OS_MACOSX)
case mojo_base::mojom::MessageLoopType::kNsRunloop:
*output = base::MessageLoop::Type::NS_RUNLOOP;
return true;
#endif
#if defined(OS_WIN)
case mojo_base::mojom::MessageLoopType::kUiWithWmQuitSupport:
*output = base::MessageLoop::Type::UI_WITH_WM_QUIT_SUPPORT;
return true;
#endif
}
return false;
}

} // namespace mojo
26 changes: 26 additions & 0 deletions mojo/public/cpp/base/message_loop_type_mojom_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_CPP_BASE_MESSAGE_LOOP_TYPE_MOJOM_TRAITS_H_
#define MOJO_PUBLIC_CPP_BASE_MESSAGE_LOOP_TYPE_MOJOM_TRAITS_H_

#include "base/component_export.h"
#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/mojom/base/message_loop_type.mojom.h"

namespace mojo {

template <>
struct COMPONENT_EXPORT(MOJO_BASE_MOJOM)
EnumTraits<mojo_base::mojom::MessageLoopType, base::MessageLoop::Type> {
static mojo_base::mojom::MessageLoopType ToMojom(
base::MessageLoop::Type input);
static bool FromMojom(mojo_base::mojom::MessageLoopType input,
base::MessageLoop::Type* output);
};

} // namespace mojo

#endif // MOJO_PUBLIC_CPP_BASE_MESSAGE_LOOP_TYPE_MOJOM_TRAITS_H_
1 change: 1 addition & 0 deletions mojo/public/cpp/base/typemaps.gni
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typemaps = [
"//mojo/public/cpp/base/read_only_buffer.typemap",
"//mojo/public/cpp/base/memory_allocator_dump_cross_process_uid.typemap",
"//mojo/public/cpp/base/memory_pressure_level.typemap",
"//mojo/public/cpp/base/message_loop_type.typemap",
"//mojo/public/cpp/base/process_id.typemap",
"//mojo/public/cpp/base/ref_counted_memory.typemap",
"//mojo/public/cpp/base/shared_memory.typemap",
Expand Down
Loading

0 comments on commit d66900e

Please sign in to comment.