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