gpu_host_impl.h 12 KB

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