Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_ |
| 6 | #define COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <queue> |
| 10 | #include <set> |
| 11 | #include <string> |
Mohsen Izadi | babed2b | 2018-08-31 01:37:39 | [diff] [blame] | 12 | #include <vector> |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 13 | |
Mohsen Izadi | babed2b | 2018-08-31 01:37:39 | [diff] [blame] | 14 | #include "base/callback.h" |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 15 | #include "base/macros.h" |
| 16 | #include "base/memory/scoped_refptr.h" |
| 17 | #include "base/memory/weak_ptr.h" |
| 18 | #include "base/optional.h" |
| 19 | #include "base/process/process_handle.h" |
| 20 | #include "base/sequence_checker.h" |
| 21 | #include "build/build_config.h" |
Miyoung Shin | 8b0b0f2 | 2019-07-17 04:35:08 | [diff] [blame^] | 22 | #include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h" |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 23 | #include "components/viz/host/viz_host_export.h" |
| 24 | #include "gpu/command_buffer/common/activity_flags.h" |
Mohsen Izadi | 312c692 | 2018-09-07 14:21:36 | [diff] [blame] | 25 | #include "gpu/config/gpu_domain_guilt.h" |
Jonah Ryan-Davis | a70577c | 2019-06-26 18:54:31 | [diff] [blame] | 26 | #include "gpu/config/gpu_extra_info.h" |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 27 | #include "mojo/public/cpp/bindings/binding.h" |
Ken Rockot | b2928f3 | 2019-03-12 04:43:03 | [diff] [blame] | 28 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 29 | #include "mojo/public/cpp/system/message_pipe.h" |
Ken Rockot | b2928f3 | 2019-03-12 04:43:03 | [diff] [blame] | 30 | #include "services/service_manager/public/mojom/service.mojom.h" |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 31 | #include "services/viz/privileged/interfaces/compositing/frame_sink_manager.mojom.h" |
| 32 | #include "services/viz/privileged/interfaces/gl/gpu_host.mojom.h" |
| 33 | #include "services/viz/privileged/interfaces/gl/gpu_service.mojom.h" |
| 34 | #include "services/viz/privileged/interfaces/viz_main.mojom.h" |
| 35 | #include "url/gurl.h" |
| 36 | |
| 37 | namespace gfx { |
| 38 | struct FontRenderParams; |
| 39 | } |
| 40 | |
| 41 | namespace gpu { |
| 42 | class ShaderCacheFactory; |
| 43 | class ShaderDiskCache; |
| 44 | } // namespace gpu |
| 45 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 46 | namespace viz { |
| 47 | |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 48 | // Contains either an interface or an associated interface pointer to a |
| 49 | // mojom::VizMain implementation and routes the requests appropriately. |
| 50 | class VIZ_HOST_EXPORT VizMainWrapper { |
| 51 | public: |
| 52 | explicit VizMainWrapper(mojom::VizMainPtr viz_main_ptr); |
| 53 | explicit VizMainWrapper(mojom::VizMainAssociatedPtr viz_main_associated_ptr); |
| 54 | ~VizMainWrapper(); |
| 55 | |
| 56 | void CreateGpuService( |
| 57 | mojom::GpuServiceRequest request, |
| 58 | mojom::GpuHostPtr gpu_host, |
| 59 | discardable_memory::mojom::DiscardableSharedMemoryManagerPtr |
| 60 | discardable_memory_manager, |
| 61 | mojo::ScopedSharedBufferHandle activity_flags, |
| 62 | gfx::FontRenderParams::SubpixelRendering subpixel_rendering); |
| 63 | void CreateFrameSinkManager(mojom::FrameSinkManagerParamsPtr params); |
Sean Gilhuly | da1ee4b | 2018-11-13 21:56:09 | [diff] [blame] | 64 | #if defined(USE_VIZ_DEVTOOLS) |
| 65 | void CreateVizDevTools(mojom::VizDevToolsParamsPtr params); |
| 66 | #endif |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | mojom::VizMainPtr viz_main_ptr_; |
| 70 | mojom::VizMainAssociatedPtr viz_main_associated_ptr_; |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(VizMainWrapper); |
| 73 | }; |
| 74 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 75 | class VIZ_HOST_EXPORT GpuHostImpl : public mojom::GpuHost { |
| 76 | public: |
| 77 | class VIZ_HOST_EXPORT Delegate { |
| 78 | public: |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 79 | virtual gpu::GPUInfo GetGPUInfo() const = 0; |
| 80 | virtual gpu::GpuFeatureInfo GetGpuFeatureInfo() const = 0; |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 81 | virtual void DidInitialize( |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 82 | const gpu::GPUInfo& gpu_info, |
| 83 | const gpu::GpuFeatureInfo& gpu_feature_info, |
| 84 | const base::Optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu, |
| 85 | const base::Optional<gpu::GpuFeatureInfo>& |
Jonah Ryan-Davis | a70577c | 2019-06-26 18:54:31 | [diff] [blame] | 86 | gpu_feature_info_for_hardware_gpu, |
| 87 | const gpu::GpuExtraInfo& gpu_extra_info) = 0; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 88 | virtual void DidFailInitialize() = 0; |
| 89 | virtual void DidCreateContextSuccessfully() = 0; |
Mohsen Izadi | 312c692 | 2018-09-07 14:21:36 | [diff] [blame] | 90 | virtual void BlockDomainFrom3DAPIs(const GURL& url, |
| 91 | gpu::DomainGuilt guilt) = 0; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 92 | virtual void DisableGpuCompositing() = 0; |
| 93 | virtual bool GpuAccessAllowed() const = 0; |
| 94 | virtual gpu::ShaderCacheFactory* GetShaderCacheFactory() = 0; |
| 95 | virtual void RecordLogMessage(int32_t severity, |
| 96 | const std::string& header, |
| 97 | const std::string& message) = 0; |
| 98 | virtual void BindDiscardableMemoryRequest( |
| 99 | discardable_memory::mojom::DiscardableSharedMemoryManagerRequest |
| 100 | request) = 0; |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 101 | virtual void BindInterface( |
| 102 | const std::string& interface_name, |
| 103 | mojo::ScopedMessagePipeHandle interface_pipe) = 0; |
Ken Rockot | b2928f3 | 2019-03-12 04:43:03 | [diff] [blame] | 104 | virtual void RunService( |
| 105 | const std::string& service_name, |
| 106 | mojo::PendingReceiver<service_manager::mojom::Service> receiver) = 0; |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 107 | #if defined(USE_OZONE) |
| 108 | virtual void TerminateGpuProcess(const std::string& message) = 0; |
| 109 | |
| 110 | // TODO(https://crbug.com/806092): Remove this when legacy IPC-based Ozone |
| 111 | // is removed. |
| 112 | virtual void SendGpuProcessMessage(IPC::Message* message) = 0; |
| 113 | #endif |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 114 | |
| 115 | protected: |
| 116 | virtual ~Delegate() {} |
| 117 | }; |
| 118 | |
| 119 | struct VIZ_HOST_EXPORT InitParams { |
| 120 | InitParams(); |
| 121 | InitParams(InitParams&&); |
| 122 | ~InitParams(); |
| 123 | |
| 124 | // An ID that changes for each GPU restart. |
| 125 | int restart_id = -1; |
| 126 | |
| 127 | // Whether GPU is running in-process or not. |
| 128 | bool in_process = false; |
| 129 | |
| 130 | // Whether caching GPU shader on disk is disabled or not. |
Mohsen Izadi | 50c2805 | 2018-09-07 00:32:14 | [diff] [blame] | 131 | bool disable_gpu_shader_disk_cache = false; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 132 | |
| 133 | // A string representing the product name and version; used to build a |
| 134 | // prefix for shader keys. |
| 135 | std::string product; |
| 136 | |
| 137 | // Number of frames to CompositorFrame activation deadline. |
Mohsen Izadi | 50c2805 | 2018-09-07 00:32:14 | [diff] [blame] | 138 | base::Optional<uint32_t> deadline_to_synchronize_surfaces; |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 139 | |
| 140 | // Task runner corresponding to the main thread. |
| 141 | scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | enum class EstablishChannelStatus { |
| 145 | kGpuAccessDenied, // GPU access was not allowed. |
| 146 | kGpuHostInvalid, // Request failed because the GPU host became invalid |
| 147 | // while processing the request (e.g. the GPU process |
| 148 | // may have been killed). The caller should normally |
| 149 | // make another request to establish a new channel. |
| 150 | kSuccess, |
| 151 | }; |
| 152 | using EstablishChannelCallback = |
| 153 | base::OnceCallback<void(mojo::ScopedMessagePipeHandle, |
| 154 | const gpu::GPUInfo&, |
| 155 | const gpu::GpuFeatureInfo&, |
| 156 | EstablishChannelStatus)>; |
| 157 | |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 158 | GpuHostImpl(Delegate* delegate, |
| 159 | std::unique_ptr<VizMainWrapper> viz_main_ptr, |
| 160 | InitParams params); |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 161 | ~GpuHostImpl() override; |
| 162 | |
| 163 | static void InitFontRenderParams(const gfx::FontRenderParams& params); |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 164 | static void ResetFontRenderParams(); |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 165 | |
| 166 | void OnProcessLaunched(base::ProcessId pid); |
| 167 | void OnProcessCrashed(); |
| 168 | |
Mohsen Izadi | babed2b | 2018-08-31 01:37:39 | [diff] [blame] | 169 | // Adds a connection error handler for the GpuService. |
| 170 | void AddConnectionErrorHandler(base::OnceClosure handler); |
| 171 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 172 | void BlockLiveOffscreenContexts(); |
| 173 | |
| 174 | // Connects to FrameSinkManager running in the Viz service. |
| 175 | void ConnectFrameSinkManager(mojom::FrameSinkManagerRequest request, |
| 176 | mojom::FrameSinkManagerClientPtrInfo client); |
| 177 | |
Sean Gilhuly | da1ee4b | 2018-11-13 21:56:09 | [diff] [blame] | 178 | #if defined(USE_VIZ_DEVTOOLS) |
| 179 | // Connects to Viz DevTools running in the Viz service. |
| 180 | void ConnectVizDevTools(mojom::VizDevToolsParamsPtr params); |
| 181 | #endif |
| 182 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 183 | // Tells the GPU service to create a new channel for communication with a |
| 184 | // client. Once the GPU service responds asynchronously with the channel |
| 185 | // handle and GPUInfo, we call the callback. |
| 186 | void EstablishGpuChannel(int client_id, |
| 187 | uint64_t client_tracing_id, |
| 188 | bool is_gpu_host, |
| 189 | EstablishChannelCallback callback); |
| 190 | |
| 191 | void SendOutstandingReplies(); |
| 192 | |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 193 | void BindInterface(const std::string& interface_name, |
| 194 | mojo::ScopedMessagePipeHandle interface_pipe); |
Ken Rockot | b2928f3 | 2019-03-12 04:43:03 | [diff] [blame] | 195 | void RunService( |
| 196 | const std::string& service_name, |
| 197 | mojo::PendingReceiver<service_manager::mojom::Service> receiver); |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 198 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 199 | mojom::GpuService* gpu_service(); |
| 200 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 201 | bool wake_up_gpu_before_drawing() const { |
| 202 | return wake_up_gpu_before_drawing_; |
| 203 | } |
| 204 | |
| 205 | private: |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 206 | friend class GpuHostImplTestApi; |
| 207 | |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 208 | #if defined(USE_OZONE) |
| 209 | void InitOzone(); |
| 210 | void TerminateGpuProcess(const std::string& message); |
| 211 | #endif // defined(USE_OZONE) |
| 212 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 213 | std::string GetShaderPrefixKey(); |
| 214 | |
| 215 | void LoadedShader(int32_t client_id, |
| 216 | const std::string& key, |
| 217 | const std::string& data); |
| 218 | |
| 219 | void CreateChannelCache(int32_t client_id); |
| 220 | |
| 221 | void OnChannelEstablished(int client_id, |
| 222 | mojo::ScopedMessagePipeHandle channel_handle); |
| 223 | |
| 224 | // mojom::GpuHost: |
| 225 | void DidInitialize( |
| 226 | const gpu::GPUInfo& gpu_info, |
| 227 | const gpu::GpuFeatureInfo& gpu_feature_info, |
| 228 | const base::Optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu, |
| 229 | const base::Optional<gpu::GpuFeatureInfo>& |
Jonah Ryan-Davis | a70577c | 2019-06-26 18:54:31 | [diff] [blame] | 230 | gpu_feature_info_for_hardware_gpu, |
| 231 | const gpu::GpuExtraInfo& gpu_extra_info) override; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 232 | void DidFailInitialize() override; |
| 233 | void DidCreateContextSuccessfully() override; |
| 234 | void DidCreateOffscreenContext(const GURL& url) override; |
| 235 | void DidDestroyOffscreenContext(const GURL& url) override; |
| 236 | void DidDestroyChannel(int32_t client_id) override; |
| 237 | void DidLoseContext(bool offscreen, |
| 238 | gpu::error::ContextLostReason reason, |
| 239 | const GURL& active_url) override; |
| 240 | void DisableGpuCompositing() override; |
Mohsen Izadi | 50c2805 | 2018-09-07 00:32:14 | [diff] [blame] | 241 | #if defined(OS_WIN) |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 242 | void SetChildSurface(gpu::SurfaceHandle parent, |
| 243 | gpu::SurfaceHandle child) override; |
Mohsen Izadi | 50c2805 | 2018-09-07 00:32:14 | [diff] [blame] | 244 | #endif |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 245 | void StoreShaderToDisk(int32_t client_id, |
| 246 | const std::string& key, |
| 247 | const std::string& shader) override; |
| 248 | void RecordLogMessage(int32_t severity, |
| 249 | const std::string& header, |
| 250 | const std::string& message) override; |
| 251 | |
| 252 | Delegate* const delegate_; |
Mohsen Izadi | f9505d8 | 2018-10-06 01:34:03 | [diff] [blame] | 253 | std::unique_ptr<VizMainWrapper> viz_main_ptr_; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 254 | const InitParams params_; |
| 255 | |
Mohsen Izadi | 63d85e7 | 2018-09-06 16:00:21 | [diff] [blame] | 256 | // Task runner corresponding to the thread |this| is created on. |
| 257 | scoped_refptr<base::SingleThreadTaskRunner> host_thread_task_runner_; |
| 258 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 259 | mojom::GpuServicePtr gpu_service_ptr_; |
| 260 | mojo::Binding<mojom::GpuHost> gpu_host_binding_; |
| 261 | gpu::GpuProcessHostActivityFlags activity_flags_; |
| 262 | |
| 263 | base::ProcessId pid_ = base::kNullProcessId; |
| 264 | |
Mohsen Izadi | babed2b | 2018-08-31 01:37:39 | [diff] [blame] | 265 | // List of connection error handlers for the GpuService. |
| 266 | std::vector<base::OnceClosure> connection_error_handlers_; |
| 267 | |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 268 | // The following are a list of driver bug workarounds that will only be |
| 269 | // set to true in DidInitialize(), where GPU service has started and GPU |
| 270 | // driver bug workarounds have been computed and sent back. |
| 271 | bool wake_up_gpu_before_drawing_ = false; |
| 272 | bool dont_disable_webgl_when_compositor_context_lost_ = false; |
| 273 | |
| 274 | // Track the URLs of the pages which have live offscreen contexts, assumed to |
| 275 | // be associated with untrusted content such as WebGL. For best robustness, |
| 276 | // when any context lost notification is received, assume all of these URLs |
| 277 | // are guilty, and block automatic execution of 3D content from those domains. |
| 278 | std::multiset<GURL> urls_with_live_offscreen_contexts_; |
| 279 | |
| 280 | std::map<int32_t, scoped_refptr<gpu::ShaderDiskCache>> |
| 281 | client_id_to_shader_cache_; |
| 282 | std::string shader_prefix_key_; |
| 283 | |
| 284 | // These are the channel requests that we have already sent to the GPU |
| 285 | // service, but haven't heard back about yet. |
| 286 | base::queue<EstablishChannelCallback> channel_requests_; |
| 287 | |
| 288 | SEQUENCE_CHECKER(sequence_checker_); |
| 289 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 290 | base::WeakPtrFactory<GpuHostImpl> weak_ptr_factory_{this}; |
Mohsen Izadi | 0b9fbb6 | 2018-08-30 20:30:00 | [diff] [blame] | 291 | |
| 292 | DISALLOW_COPY_AND_ASSIGN(GpuHostImpl); |
| 293 | }; |
| 294 | |
| 295 | } // namespace viz |
| 296 | |
| 297 | #endif // COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_ |