1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_
#define COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_
#include <map>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process_handle.h"
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h"
#include "components/viz/common/buildflags.h"
#include "components/viz/host/viz_host_export.h"
#include "components/viz/service/debugger/mojom/viz_debugger.mojom.h"
#include "gpu/command_buffer/common/shared_image_capabilities.h"
#include "gpu/command_buffer/common/shm_count.h"
#include "gpu/config/gpu_domain_guilt.h"
#include "gpu/ipc/common/gpu_disk_cache_type.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "services/service_manager/public/mojom/service.mojom.h"
#include "services/viz/privileged/mojom/compositing/frame_sink_manager.mojom.h"
#include "services/viz/privileged/mojom/gl/gpu_host.mojom.h"
#include "services/viz/privileged/mojom/gl/gpu_logging.mojom.h"
#include "services/viz/privileged/mojom/gl/gpu_service.mojom.h"
#include "services/viz/privileged/mojom/viz_main.mojom.h"
#include "ui/gfx/gpu_extra_info.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_WIN)
#include "services/viz/privileged/mojom/gl/info_collection_gpu_service.mojom.h"
#include "ui/gfx/mojom/dxgi_info.mojom.h"
#endif
namespace gfx {
struct FontRenderParams;
}
namespace gpu {
class GpuDiskCacheFactory;
class GpuDiskCache;
} // namespace gpu
namespace viz {
class VIZ_HOST_EXPORT GpuHostImpl : public mojom::GpuHost,
public mojom::GpuLogging
#if BUILDFLAG(USE_VIZ_DEBUGGER)
,
public mojom::VizDebugOutput
#endif
{
public:
class VIZ_HOST_EXPORT Delegate {
public:
virtual gpu::GPUInfo GetGPUInfo() const = 0;
virtual gpu::GpuFeatureInfo GetGpuFeatureInfo() const = 0;
virtual void DidInitialize(
const gpu::GPUInfo& gpu_info,
const gpu::GpuFeatureInfo& gpu_feature_info,
const std::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
const std::optional<gpu::GpuFeatureInfo>&
gpu_feature_info_for_hardware_gpu,
const gfx::GpuExtraInfo& gpu_extra_info) = 0;
virtual void DidFailInitialize() = 0;
virtual void DidCreateContextSuccessfully() = 0;
virtual void MaybeShutdownGpuProcess() = 0;
virtual void DidUpdateGPUInfo(const gpu::GPUInfo& gpu_info) = 0;
#if BUILDFLAG(IS_WIN)
virtual void DidUpdateOverlayInfo(const gpu::OverlayInfo& overlay_info) = 0;
virtual void DidUpdateDXGIInfo(gfx::mojom::DXGIInfoPtr dxgi_info) = 0;
#endif
virtual void BlockDomainsFrom3DAPIs(const std::set<GURL>& urls,
gpu::DomainGuilt guilt) = 0;
virtual std::string GetIsolationKey(
int32_t client_id,
const blink::WebGPUExecutionContextToken& token) = 0;
virtual void DisableGpuCompositing() = 0;
virtual bool GpuAccessAllowed() const = 0;
virtual gpu::GpuDiskCacheFactory* GetGpuDiskCacheFactory() = 0;
virtual void RecordLogMessage(int32_t severity,
const std::string& header,
const std::string& message) = 0;
virtual void BindDiscardableMemoryReceiver(
mojo::PendingReceiver<
discardable_memory::mojom::DiscardableSharedMemoryManager>
receiver) = 0;
virtual void BindInterface(
const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe) = 0;
#if BUILDFLAG(IS_OZONE)
virtual void TerminateGpuProcess(const std::string& message) = 0;
#endif
protected:
virtual ~Delegate() = default;
};
struct VIZ_HOST_EXPORT InitParams {
InitParams();
InitParams(InitParams&&);
~InitParams();
// An ID that changes for each GPU restart.
int restart_id = -1;
// Whether caching GPU shader on disk is disabled or not.
bool disable_gpu_shader_disk_cache = false;
// A string representing the product name and version; used to build a
// prefix for shader keys.
std::string product;
// Number of frames to CompositorFrame activation deadline.
std::optional<uint32_t> deadline_to_synchronize_surfaces;
// Task runner corresponding to the main thread.
scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner;
// Whether this GPU process is used for GPU info collection only.
bool info_collection_gpu_process = false;
// Whether the GPU service is running in the host process.
bool gpu_service_running_in_process = false;
};
enum class EstablishChannelStatus {
kGpuAccessDenied, // GPU access was not allowed.
kGpuHostInvalid, // Request failed because the GPU host became invalid
// while processing the request (e.g. the GPU process
// may have been killed). The caller should normally
// make another request to establish a new channel.
kSuccess,
};
using EstablishChannelCallback =
base::OnceCallback<void(mojo::ScopedMessagePipeHandle,
const gpu::GPUInfo&,
const gpu::GpuFeatureInfo&,
const gpu::SharedImageCapabilities&,
EstablishChannelStatus)>;
GpuHostImpl(Delegate* delegate,
mojo::PendingRemote<mojom::VizMain> viz_main,
InitParams params);
GpuHostImpl(const GpuHostImpl&) = delete;
GpuHostImpl& operator=(const GpuHostImpl&) = delete;
~GpuHostImpl() override;
static void InitFontRenderParams(const gfx::FontRenderParams& params);
static void ResetFontRenderParams();
void SetProcessId(base::ProcessId pid);
void OnProcessCrashed();
// Adds a connection error handler for the GpuService.
void AddConnectionErrorHandler(base::OnceClosure handler);
void BlockLiveOffscreenContexts();
// Connects to FrameSinkManager running in the Viz service.
void ConnectFrameSinkManager(
mojo::PendingReceiver<mojom::FrameSinkManager> receiver,
mojo::PendingRemote<mojom::FrameSinkManagerClient> client,
const DebugRendererSettings& debug_renderer_settings);
// Tells the GPU service to create a new channel for communication with a
// client. Once the GPU service responds asynchronously with the channel
// handle and GPUInfo, we call the callback. If |sync| is true then the
// callback will be run before this method returns, and note that the
// browser GPU info data might not be initialized as well.
void EstablishGpuChannel(int client_id,
uint64_t client_tracing_id,
bool is_gpu_host,
bool sync,
EstablishChannelCallback callback);
void SetChannelClientPid(int client_id, base::ProcessId client_pid);
void SetChannelDiskCacheHandle(int client_id,
const gpu::GpuDiskCacheHandle& handle);
void RemoveChannelDiskCacheHandles(int client_id);
void CloseChannel(int client_id);
#if BUILDFLAG(USE_VIZ_DEBUGGER)
// Command as a Json string that the visual debugging instance interprets as
// stream filtering.
void FilterVisualDebugStream(base::Value::Dict filter_data);
// Establishes the connection between the visual debugging instance and the
// output stream.
void StartVisualDebugStream(
base::RepeatingCallback<void(base::Value)> callback);
void StopVisualDebugStream();
#endif
void SendOutstandingReplies();
void BindInterface(const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe);
mojom::GpuService* gpu_service();
#if BUILDFLAG(IS_WIN)
mojom::InfoCollectionGpuService* info_collection_gpu_service();
void AddChildWindow(gpu::SurfaceHandle parent_window,
gpu::SurfaceHandle child_window);
#endif
void MaybeSendFontRenderParams();
private:
friend class GpuHostImplTestApi;
#if BUILDFLAG(IS_OZONE)
void InitOzone();
void TerminateGpuProcess(const std::string& message);
#endif // BUILDFLAG(IS_OZONE)
std::string GetShaderPrefixKey();
void LoadedBlob(const gpu::GpuDiskCacheHandle& handle,
const std::string& key,
const std::string& data);
void OnDiskCacheHandleDestoyed(const gpu::GpuDiskCacheHandle& handle);
void OnChannelEstablished(
int client_id,
bool sync,
mojo::ScopedMessagePipeHandle channel_handle,
const gpu::GPUInfo& gpu_info,
const gpu::GpuFeatureInfo& gpu_feature_info,
const gpu::SharedImageCapabilities& shared_image_capabilities);
void MaybeShutdownGpuProcess();
// mojom::GpuHost:
void DidInitialize(
const gpu::GPUInfo& gpu_info,
const gpu::GpuFeatureInfo& gpu_feature_info,
const std::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
const std::optional<gpu::GpuFeatureInfo>&
gpu_feature_info_for_hardware_gpu,
const gfx::GpuExtraInfo& gpu_extra_info) override;
void DidFailInitialize() override;
void DidCreateContextSuccessfully() override;
void DidCreateOffscreenContext(const GURL& url) override;
void DidDestroyOffscreenContext(const GURL& url) override;
void DidDestroyChannel(int32_t client_id) override;
void DidDestroyAllChannels() override;
void DidLoseContext(gpu::error::ContextLostReason reason,
const GURL& active_url) override;
void DisableGpuCompositing() override;
void DidUpdateGPUInfo(const gpu::GPUInfo& gpu_info) override;
#if BUILDFLAG(IS_WIN)
void DidUpdateOverlayInfo(const gpu::OverlayInfo& overlay_info) override;
void DidUpdateDXGIInfo(gfx::mojom::DXGIInfoPtr dxgi_info) override;
#endif
void GetIsolationKey(int32_t client_id,
const blink::WebGPUExecutionContextToken& token,
GetIsolationKeyCallback cb) override;
void StoreBlobToDisk(const gpu::GpuDiskCacheHandle& handle,
const std::string& key,
const std::string& blob) override;
void ClearGrShaderDiskCache() override;
// mojom::GpuLogging:
void RecordLogMessage(int32_t severity,
const std::string& header,
const std::string& message) override;
// Implements mojom::VizDebugOutput and is called by VizDebugger.
#if BUILDFLAG(USE_VIZ_DEBUGGER)
void LogFrame(base::Value frame_data) override;
#endif
// Can be modified in tests by GpuHostImplTestApi.
raw_ptr<Delegate> delegate_;
mojo::Remote<mojom::VizMain> viz_main_;
const InitParams params_;
mojo::Remote<mojom::GpuService> gpu_service_remote_;
#if BUILDFLAG(IS_WIN)
mojo::Remote<mojom::InfoCollectionGpuService>
info_collection_gpu_service_remote_;
#endif
mojo::Receiver<mojom::GpuHost> gpu_host_receiver_{this};
mojo::Receiver<mojom::GpuLogging> gpu_logging_receiver_{this};
gpu::GpuProcessHostShmCount use_shader_cache_shm_count_;
#if BUILDFLAG(USE_VIZ_DEBUGGER)
mojo::Receiver<mojom::VizDebugOutput> viz_debug_output_{this};
base::RepeatingCallback<void(base::Value)> viz_debug_output_callback_;
#endif
base::ProcessId pid_ = base::kNullProcessId;
// List of connection error handlers for the GpuService.
std::vector<base::OnceClosure> connection_error_handlers_;
// Track the URLs of the pages which have live offscreen contexts, assumed to
// be associated with untrusted content such as WebGL. For best robustness,
// when any context lost notification is received, assume all of these URLs
// are guilty, and block automatic execution of 3D content from those domains.
std::multiset<GURL> urls_with_live_offscreen_contexts_;
std::multimap<int32_t, scoped_refptr<gpu::GpuDiskCache>> client_id_to_caches_;
std::string shader_prefix_key_;
// These are the channel requests that we have already sent to the GPU
// service, but haven't heard back about yet.
base::flat_map<int, EstablishChannelCallback> channel_requests_;
base::OneShotTimer shutdown_timeout_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<GpuHostImpl> weak_ptr_factory_{this};
};
} // namespace viz
#endif // COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_
|