blob: ab02526fdec44b39c5ae2f85dab94580fa5b0728 [file] [log] [blame]
Mohsen Izadi0b9fbb62018-08-30 20:30:001// 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 Izadibabed2b2018-08-31 01:37:3912#include <vector>
Mohsen Izadi0b9fbb62018-08-30 20:30:0013
Mohsen Izadibabed2b2018-08-31 01:37:3914#include "base/callback.h"
John Abd-El-Malekb36a3792021-04-19 20:54:3115#include "base/containers/flat_map.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0016#include "base/macros.h"
17#include "base/memory/scoped_refptr.h"
18#include "base/memory/weak_ptr.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0019#include "base/process/process_handle.h"
20#include "base/sequence_checker.h"
Bo Liu95570f32020-03-16 23:42:1721#include "base/timer/timer.h"
Peter McNeeley40245f02021-05-04 21:50:5022#include "base/values.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0023#include "build/build_config.h"
Miyoung Shin8b0b0f22019-07-17 04:35:0824#include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h"
Robert Seseka9b47c92019-08-30 01:22:5425#include "components/ui_devtools/buildflags.h"
Peter McNeeley40245f02021-05-04 21:50:5026#include "components/viz/common/buildflags.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0027#include "components/viz/host/viz_host_export.h"
28#include "gpu/command_buffer/common/activity_flags.h"
Mohsen Izadi312c6922018-09-07 14:21:3629#include "gpu/config/gpu_domain_guilt.h"
Ken Rockotb2928f32019-03-12 04:43:0330#include "mojo/public/cpp/bindings/pending_receiver.h"
Shimi Zhang88ae9222019-07-19 16:54:1631#include "mojo/public/cpp/bindings/pending_remote.h"
32#include "mojo/public/cpp/bindings/receiver.h"
33#include "mojo/public/cpp/bindings/remote.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0034#include "mojo/public/cpp/system/message_pipe.h"
Ken Rockotb2928f32019-03-12 04:43:0335#include "services/service_manager/public/mojom/service.mojom.h"
Miyoung Shin2be27f52019-07-27 15:35:3936#include "services/viz/privileged/mojom/compositing/frame_sink_manager.mojom.h"
37#include "services/viz/privileged/mojom/gl/gpu_host.mojom.h"
38#include "services/viz/privileged/mojom/gl/gpu_service.mojom.h"
39#include "services/viz/privileged/mojom/viz_main.mojom.h"
Anton Bikineev1156b5f2021-05-15 22:35:3640#include "third_party/abseil-cpp/absl/types/optional.h"
Alexander Dunaeve6e09c12020-10-23 16:43:4241#include "ui/gfx/gpu_extra_info.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0042#include "url/gurl.h"
43
Maggie Chena6ece772020-04-27 17:19:0644#if defined(OS_WIN)
45#include "services/viz/privileged/mojom/gl/info_collection_gpu_service.mojom.h"
46#endif
47
Mohsen Izadi0b9fbb62018-08-30 20:30:0048namespace gfx {
49struct FontRenderParams;
50}
51
52namespace gpu {
53class ShaderCacheFactory;
54class ShaderDiskCache;
55} // namespace gpu
56
Mohsen Izadi0b9fbb62018-08-30 20:30:0057namespace viz {
58
Peter McNeeley40245f02021-05-04 21:50:5059class VIZ_HOST_EXPORT GpuHostImpl : public mojom::GpuHost
60#if BUILDFLAG(USE_VIZ_DEBUGGER)
61 ,
62 public mojom::VizDebugOutput
63#endif
64{
Mohsen Izadi0b9fbb62018-08-30 20:30:0065 public:
66 class VIZ_HOST_EXPORT Delegate {
67 public:
Mohsen Izadi0b9fbb62018-08-30 20:30:0068 virtual gpu::GPUInfo GetGPUInfo() const = 0;
69 virtual gpu::GpuFeatureInfo GetGpuFeatureInfo() const = 0;
Mohsen Izadif9505d82018-10-06 01:34:0370 virtual void DidInitialize(
Mohsen Izadi0b9fbb62018-08-30 20:30:0071 const gpu::GPUInfo& gpu_info,
72 const gpu::GpuFeatureInfo& gpu_feature_info,
Anton Bikineev1156b5f2021-05-15 22:35:3673 const absl::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
74 const absl::optional<gpu::GpuFeatureInfo>&
Jonah Ryan-Davisa70577c2019-06-26 18:54:3175 gpu_feature_info_for_hardware_gpu,
Alexander Dunaeve6e09c12020-10-23 16:43:4276 const gfx::GpuExtraInfo& gpu_extra_info) = 0;
Mohsen Izadi0b9fbb62018-08-30 20:30:0077 virtual void DidFailInitialize() = 0;
78 virtual void DidCreateContextSuccessfully() = 0;
Bo Liu95570f32020-03-16 23:42:1779 virtual void MaybeShutdownGpuProcess() = 0;
Jonah Ryan-Davisa54a3002020-12-08 17:34:5080 virtual void DidUpdateGPUInfo(const gpu::GPUInfo& gpu_info) = 0;
Maggie Chen8f177172020-02-11 00:02:2881#if defined(OS_WIN)
82 virtual void DidUpdateOverlayInfo(const gpu::OverlayInfo& overlay_info) = 0;
Maggie Chen05364bf32020-06-03 08:34:0683 virtual void DidUpdateHDRStatus(bool hdr_enabled) = 0;
Maggie Chen8f177172020-02-11 00:02:2884#endif
Mohsen Izadi312c6922018-09-07 14:21:3685 virtual void BlockDomainFrom3DAPIs(const GURL& url,
86 gpu::DomainGuilt guilt) = 0;
Mohsen Izadi0b9fbb62018-08-30 20:30:0087 virtual void DisableGpuCompositing() = 0;
88 virtual bool GpuAccessAllowed() const = 0;
89 virtual gpu::ShaderCacheFactory* GetShaderCacheFactory() = 0;
90 virtual void RecordLogMessage(int32_t severity,
91 const std::string& header,
92 const std::string& message) = 0;
Julie Jeongeun Kim7b4a6aa12019-09-20 14:57:1293 virtual void BindDiscardableMemoryReceiver(
94 mojo::PendingReceiver<
95 discardable_memory::mojom::DiscardableSharedMemoryManager>
96 receiver) = 0;
Mohsen Izadi63d85e72018-09-06 16:00:2197 virtual void BindInterface(
98 const std::string& interface_name,
99 mojo::ScopedMessagePipeHandle interface_pipe) = 0;
Mohsen Izadi63d85e72018-09-06 16:00:21100#if defined(USE_OZONE)
101 virtual void TerminateGpuProcess(const std::string& message) = 0;
Mohsen Izadi63d85e72018-09-06 16:00:21102#endif
Mohsen Izadi0b9fbb62018-08-30 20:30:00103
104 protected:
105 virtual ~Delegate() {}
106 };
107
108 struct VIZ_HOST_EXPORT InitParams {
109 InitParams();
110 InitParams(InitParams&&);
111 ~InitParams();
112
113 // An ID that changes for each GPU restart.
114 int restart_id = -1;
115
Mohsen Izadi0b9fbb62018-08-30 20:30:00116 // Whether caching GPU shader on disk is disabled or not.
Mohsen Izadi50c28052018-09-07 00:32:14117 bool disable_gpu_shader_disk_cache = false;
Mohsen Izadi0b9fbb62018-08-30 20:30:00118
119 // A string representing the product name and version; used to build a
120 // prefix for shader keys.
121 std::string product;
122
123 // Number of frames to CompositorFrame activation deadline.
Anton Bikineev1156b5f2021-05-15 22:35:36124 absl::optional<uint32_t> deadline_to_synchronize_surfaces;
Mohsen Izadi63d85e72018-09-06 16:00:21125
126 // Task runner corresponding to the main thread.
127 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner;
Maggie Chena6ece772020-04-27 17:19:06128
129 // Whether this GPU process is used for GPU info collection only.
130 bool info_collection_gpu_process = false;
Mohsen Izadi0b9fbb62018-08-30 20:30:00131 };
132
133 enum class EstablishChannelStatus {
134 kGpuAccessDenied, // GPU access was not allowed.
135 kGpuHostInvalid, // Request failed because the GPU host became invalid
136 // while processing the request (e.g. the GPU process
137 // may have been killed). The caller should normally
138 // make another request to establish a new channel.
139 kSuccess,
140 };
141 using EstablishChannelCallback =
142 base::OnceCallback<void(mojo::ScopedMessagePipeHandle,
143 const gpu::GPUInfo&,
144 const gpu::GpuFeatureInfo&,
145 EstablishChannelStatus)>;
146
Mohsen Izadif9505d82018-10-06 01:34:03147 GpuHostImpl(Delegate* delegate,
Ken Rockot7f8a2822021-04-06 23:27:52148 mojo::PendingRemote<mojom::VizMain> viz_main,
Mohsen Izadif9505d82018-10-06 01:34:03149 InitParams params);
Mohsen Izadi0b9fbb62018-08-30 20:30:00150 ~GpuHostImpl() override;
151
152 static void InitFontRenderParams(const gfx::FontRenderParams& params);
Mohsen Izadif9505d82018-10-06 01:34:03153 static void ResetFontRenderParams();
Mohsen Izadi0b9fbb62018-08-30 20:30:00154
kylechar9b37f172019-09-19 22:55:27155 void SetProcessId(base::ProcessId pid);
Mohsen Izadi0b9fbb62018-08-30 20:30:00156 void OnProcessCrashed();
157
Mohsen Izadibabed2b2018-08-31 01:37:39158 // Adds a connection error handler for the GpuService.
159 void AddConnectionErrorHandler(base::OnceClosure handler);
160
Mohsen Izadi0b9fbb62018-08-30 20:30:00161 void BlockLiveOffscreenContexts();
162
163 // Connects to FrameSinkManager running in the Viz service.
Mario Sanchez Prada745dd5e2019-10-16 14:14:30164 void ConnectFrameSinkManager(
165 mojo::PendingReceiver<mojom::FrameSinkManager> receiver,
Alexander Alekseev81b01612020-07-10 22:56:29166 mojo::PendingRemote<mojom::FrameSinkManagerClient> client,
167 const DebugRendererSettings& debug_renderer_settings);
Mohsen Izadi0b9fbb62018-08-30 20:30:00168
Robert Seseka9b47c92019-08-30 01:22:54169#if BUILDFLAG(USE_VIZ_DEVTOOLS)
Sean Gilhulyda1ee4b2018-11-13 21:56:09170 // Connects to Viz DevTools running in the Viz service.
171 void ConnectVizDevTools(mojom::VizDevToolsParamsPtr params);
172#endif
173
Mohsen Izadi0b9fbb62018-08-30 20:30:00174 // Tells the GPU service to create a new channel for communication with a
175 // client. Once the GPU service responds asynchronously with the channel
John Abd-El-Malekb36a3792021-04-19 20:54:31176 // handle and GPUInfo, we call the callback. If |sync| is true then the
177 // callback will be run before this method returns, and note that the
178 // browser GPU info data might not be initialized as well.
Mohsen Izadi0b9fbb62018-08-30 20:30:00179 void EstablishGpuChannel(int client_id,
180 uint64_t client_tracing_id,
181 bool is_gpu_host,
John Abd-El-Malekb36a3792021-04-19 20:54:31182 bool sync,
Mohsen Izadi0b9fbb62018-08-30 20:30:00183 EstablishChannelCallback callback);
John Abd-El-Malekb36a3792021-04-19 20:54:31184 void CloseChannel(int client_id);
Mohsen Izadi0b9fbb62018-08-30 20:30:00185
Peter McNeeley40245f02021-05-04 21:50:50186#if BUILDFLAG(USE_VIZ_DEBUGGER)
187 // Command as a Json string that the visual debugging instance interprets as
188 // stream filtering.
189 void FilterVisualDebugStream(base::Value filter_data);
190
191 // Establishes the connection between the visual debugging instance and the
192 // output stream.
193 void StartVisualDebugStream(
194 base::RepeatingCallback<void(base::Value)> callback);
195
196 void StopVisualDebugStream();
197#endif
198
Mohsen Izadi0b9fbb62018-08-30 20:30:00199 void SendOutstandingReplies();
200
Mohsen Izadi63d85e72018-09-06 16:00:21201 void BindInterface(const std::string& interface_name,
202 mojo::ScopedMessagePipeHandle interface_pipe);
Mohsen Izadi63d85e72018-09-06 16:00:21203
Mohsen Izadi0b9fbb62018-08-30 20:30:00204 mojom::GpuService* gpu_service();
205
Maggie Chena6ece772020-04-27 17:19:06206#if defined(OS_WIN)
207 mojom::InfoCollectionGpuService* info_collection_gpu_service();
208#endif
209
Mohsen Izadi0b9fbb62018-08-30 20:30:00210 bool wake_up_gpu_before_drawing() const {
211 return wake_up_gpu_before_drawing_;
212 }
213
214 private:
Mohsen Izadif9505d82018-10-06 01:34:03215 friend class GpuHostImplTestApi;
216
Mohsen Izadi63d85e72018-09-06 16:00:21217#if defined(USE_OZONE)
218 void InitOzone();
219 void TerminateGpuProcess(const std::string& message);
220#endif // defined(USE_OZONE)
221
Mohsen Izadi0b9fbb62018-08-30 20:30:00222 std::string GetShaderPrefixKey();
223
224 void LoadedShader(int32_t client_id,
225 const std::string& key,
226 const std::string& data);
227
228 void CreateChannelCache(int32_t client_id);
229
230 void OnChannelEstablished(int client_id,
John Abd-El-Malekb36a3792021-04-19 20:54:31231 bool sync,
232 mojo::ScopedMessagePipeHandle channel_handle,
233 const gpu::GPUInfo& gpu_info,
234 const gpu::GpuFeatureInfo& gpu_feature_info);
Bo Liu95570f32020-03-16 23:42:17235 void MaybeShutdownGpuProcess();
Mohsen Izadi0b9fbb62018-08-30 20:30:00236
237 // mojom::GpuHost:
238 void DidInitialize(
239 const gpu::GPUInfo& gpu_info,
240 const gpu::GpuFeatureInfo& gpu_feature_info,
Anton Bikineev1156b5f2021-05-15 22:35:36241 const absl::optional<gpu::GPUInfo>& gpu_info_for_hardware_gpu,
242 const absl::optional<gpu::GpuFeatureInfo>&
Jonah Ryan-Davisa70577c2019-06-26 18:54:31243 gpu_feature_info_for_hardware_gpu,
Alexander Dunaeve6e09c12020-10-23 16:43:42244 const gfx::GpuExtraInfo& gpu_extra_info) override;
Mohsen Izadi0b9fbb62018-08-30 20:30:00245 void DidFailInitialize() override;
246 void DidCreateContextSuccessfully() override;
247 void DidCreateOffscreenContext(const GURL& url) override;
248 void DidDestroyOffscreenContext(const GURL& url) override;
249 void DidDestroyChannel(int32_t client_id) override;
Bo Liu95570f32020-03-16 23:42:17250 void DidDestroyAllChannels() override;
Mohsen Izadi0b9fbb62018-08-30 20:30:00251 void DidLoseContext(bool offscreen,
252 gpu::error::ContextLostReason reason,
253 const GURL& active_url) override;
254 void DisableGpuCompositing() override;
Jonah Ryan-Davisa54a3002020-12-08 17:34:50255 void DidUpdateGPUInfo(const gpu::GPUInfo& gpu_info) override;
Mohsen Izadi50c28052018-09-07 00:32:14256#if defined(OS_WIN)
Maggie Chen8f177172020-02-11 00:02:28257 void DidUpdateOverlayInfo(const gpu::OverlayInfo& overlay_info) override;
Maggie Chen05364bf32020-06-03 08:34:06258 void DidUpdateHDRStatus(bool hdr_enabled) override;
Mohsen Izadi0b9fbb62018-08-30 20:30:00259 void SetChildSurface(gpu::SurfaceHandle parent,
260 gpu::SurfaceHandle child) override;
Mohsen Izadi50c28052018-09-07 00:32:14261#endif
Mohsen Izadi0b9fbb62018-08-30 20:30:00262 void StoreShaderToDisk(int32_t client_id,
263 const std::string& key,
264 const std::string& shader) override;
265 void RecordLogMessage(int32_t severity,
266 const std::string& header,
267 const std::string& message) override;
268
Peter McNeeley40245f02021-05-04 21:50:50269 // Implements mojom::VizDebugOutput and is called by VizDebugger.
270#if BUILDFLAG(USE_VIZ_DEBUGGER)
271 void LogFrame(base::Value frame_data) override;
272#endif
273
Mohsen Izadi0b9fbb62018-08-30 20:30:00274 Delegate* const delegate_;
Ken Rockot7f8a2822021-04-06 23:27:52275 mojo::Remote<mojom::VizMain> viz_main_;
Mohsen Izadi0b9fbb62018-08-30 20:30:00276 const InitParams params_;
277
Mohsen Izadi63d85e72018-09-06 16:00:21278 // Task runner corresponding to the thread |this| is created on.
279 scoped_refptr<base::SingleThreadTaskRunner> host_thread_task_runner_;
280
Shimi Zhang88ae9222019-07-19 16:54:16281 mojo::Remote<mojom::GpuService> gpu_service_remote_;
Maggie Chena6ece772020-04-27 17:19:06282#if defined(OS_WIN)
283 mojo::Remote<mojom::InfoCollectionGpuService>
284 info_collection_gpu_service_remote_;
285#endif
Shimi Zhang88ae9222019-07-19 16:54:16286 mojo::Receiver<mojom::GpuHost> gpu_host_receiver_{this};
Mohsen Izadi0b9fbb62018-08-30 20:30:00287 gpu::GpuProcessHostActivityFlags activity_flags_;
288
Peter McNeeley40245f02021-05-04 21:50:50289#if BUILDFLAG(USE_VIZ_DEBUGGER)
290 mojo::Receiver<mojom::VizDebugOutput> viz_debug_output_{this};
291 base::RepeatingCallback<void(base::Value)> viz_debug_output_callback_;
292#endif
293
Mohsen Izadi0b9fbb62018-08-30 20:30:00294 base::ProcessId pid_ = base::kNullProcessId;
295
Mohsen Izadibabed2b2018-08-31 01:37:39296 // List of connection error handlers for the GpuService.
297 std::vector<base::OnceClosure> connection_error_handlers_;
298
Mohsen Izadi0b9fbb62018-08-30 20:30:00299 // The following are a list of driver bug workarounds that will only be
300 // set to true in DidInitialize(), where GPU service has started and GPU
301 // driver bug workarounds have been computed and sent back.
302 bool wake_up_gpu_before_drawing_ = false;
Mohsen Izadi0b9fbb62018-08-30 20:30:00303
304 // Track the URLs of the pages which have live offscreen contexts, assumed to
305 // be associated with untrusted content such as WebGL. For best robustness,
306 // when any context lost notification is received, assume all of these URLs
307 // are guilty, and block automatic execution of 3D content from those domains.
308 std::multiset<GURL> urls_with_live_offscreen_contexts_;
309
310 std::map<int32_t, scoped_refptr<gpu::ShaderDiskCache>>
311 client_id_to_shader_cache_;
312 std::string shader_prefix_key_;
313
314 // These are the channel requests that we have already sent to the GPU
315 // service, but haven't heard back about yet.
John Abd-El-Malekb36a3792021-04-19 20:54:31316 base::flat_map<int, EstablishChannelCallback> channel_requests_;
Mohsen Izadi0b9fbb62018-08-30 20:30:00317
Bo Liu95570f32020-03-16 23:42:17318 base::OneShotTimer shutdown_timeout_;
319
Mohsen Izadi0b9fbb62018-08-30 20:30:00320 SEQUENCE_CHECKER(sequence_checker_);
321
Jeremy Roman5c341f6d2019-07-15 15:56:10322 base::WeakPtrFactory<GpuHostImpl> weak_ptr_factory_{this};
Mohsen Izadi0b9fbb62018-08-30 20:30:00323
324 DISALLOW_COPY_AND_ASSIGN(GpuHostImpl);
325};
326
327} // namespace viz
328
329#endif // COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_