blob: fa02e7ea3d63e7ef714ae2cf74c18a822f80c21a [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"
Mohsen Izadi0b9fbb62018-08-30 20:30:0015#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 Shin8b0b0f22019-07-17 04:35:0822#include "components/discardable_memory/public/mojom/discardable_shared_memory_manager.mojom.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0023#include "components/viz/host/viz_host_export.h"
24#include "gpu/command_buffer/common/activity_flags.h"
Mohsen Izadi312c6922018-09-07 14:21:3625#include "gpu/config/gpu_domain_guilt.h"
Jonah Ryan-Davisa70577c2019-06-26 18:54:3126#include "gpu/config/gpu_extra_info.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0027#include "mojo/public/cpp/bindings/binding.h"
Ken Rockotb2928f32019-03-12 04:43:0328#include "mojo/public/cpp/bindings/pending_receiver.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0029#include "mojo/public/cpp/system/message_pipe.h"
Ken Rockotb2928f32019-03-12 04:43:0330#include "services/service_manager/public/mojom/service.mojom.h"
Mohsen Izadi0b9fbb62018-08-30 20:30:0031#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
37namespace gfx {
38struct FontRenderParams;
39}
40
41namespace gpu {
42class ShaderCacheFactory;
43class ShaderDiskCache;
44} // namespace gpu
45
Mohsen Izadi0b9fbb62018-08-30 20:30:0046namespace viz {
47
Mohsen Izadif9505d82018-10-06 01:34:0348// Contains either an interface or an associated interface pointer to a
49// mojom::VizMain implementation and routes the requests appropriately.
50class 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 Gilhulyda1ee4b2018-11-13 21:56:0964#if defined(USE_VIZ_DEVTOOLS)
65 void CreateVizDevTools(mojom::VizDevToolsParamsPtr params);
66#endif
Mohsen Izadif9505d82018-10-06 01:34:0367
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 Izadi0b9fbb62018-08-30 20:30:0075class VIZ_HOST_EXPORT GpuHostImpl : public mojom::GpuHost {
76 public:
77 class VIZ_HOST_EXPORT Delegate {
78 public:
Mohsen Izadi0b9fbb62018-08-30 20:30:0079 virtual gpu::GPUInfo GetGPUInfo() const = 0;
80 virtual gpu::GpuFeatureInfo GetGpuFeatureInfo() const = 0;
Mohsen Izadif9505d82018-10-06 01:34:0381 virtual void DidInitialize(
Mohsen Izadi0b9fbb62018-08-30 20:30:0082 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-Davisa70577c2019-06-26 18:54:3186 gpu_feature_info_for_hardware_gpu,
87 const gpu::GpuExtraInfo& gpu_extra_info) = 0;
Mohsen Izadi0b9fbb62018-08-30 20:30:0088 virtual void DidFailInitialize() = 0;
89 virtual void DidCreateContextSuccessfully() = 0;
Mohsen Izadi312c6922018-09-07 14:21:3690 virtual void BlockDomainFrom3DAPIs(const GURL& url,
91 gpu::DomainGuilt guilt) = 0;
Mohsen Izadi0b9fbb62018-08-30 20:30:0092 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 Izadi63d85e72018-09-06 16:00:21101 virtual void BindInterface(
102 const std::string& interface_name,
103 mojo::ScopedMessagePipeHandle interface_pipe) = 0;
Ken Rockotb2928f32019-03-12 04:43:03104 virtual void RunService(
105 const std::string& service_name,
106 mojo::PendingReceiver<service_manager::mojom::Service> receiver) = 0;
Mohsen Izadi63d85e72018-09-06 16:00:21107#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 Izadi0b9fbb62018-08-30 20:30:00114
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 Izadi50c28052018-09-07 00:32:14131 bool disable_gpu_shader_disk_cache = false;
Mohsen Izadi0b9fbb62018-08-30 20:30:00132
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 Izadi50c28052018-09-07 00:32:14138 base::Optional<uint32_t> deadline_to_synchronize_surfaces;
Mohsen Izadi63d85e72018-09-06 16:00:21139
140 // Task runner corresponding to the main thread.
141 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner;
Mohsen Izadi0b9fbb62018-08-30 20:30:00142 };
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 Izadif9505d82018-10-06 01:34:03158 GpuHostImpl(Delegate* delegate,
159 std::unique_ptr<VizMainWrapper> viz_main_ptr,
160 InitParams params);
Mohsen Izadi0b9fbb62018-08-30 20:30:00161 ~GpuHostImpl() override;
162
163 static void InitFontRenderParams(const gfx::FontRenderParams& params);
Mohsen Izadif9505d82018-10-06 01:34:03164 static void ResetFontRenderParams();
Mohsen Izadi0b9fbb62018-08-30 20:30:00165
166 void OnProcessLaunched(base::ProcessId pid);
167 void OnProcessCrashed();
168
Mohsen Izadibabed2b2018-08-31 01:37:39169 // Adds a connection error handler for the GpuService.
170 void AddConnectionErrorHandler(base::OnceClosure handler);
171
Mohsen Izadi0b9fbb62018-08-30 20:30:00172 void BlockLiveOffscreenContexts();
173
174 // Connects to FrameSinkManager running in the Viz service.
175 void ConnectFrameSinkManager(mojom::FrameSinkManagerRequest request,
176 mojom::FrameSinkManagerClientPtrInfo client);
177
Sean Gilhulyda1ee4b2018-11-13 21:56:09178#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 Izadi0b9fbb62018-08-30 20:30:00183 // 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 Izadi63d85e72018-09-06 16:00:21193 void BindInterface(const std::string& interface_name,
194 mojo::ScopedMessagePipeHandle interface_pipe);
Ken Rockotb2928f32019-03-12 04:43:03195 void RunService(
196 const std::string& service_name,
197 mojo::PendingReceiver<service_manager::mojom::Service> receiver);
Mohsen Izadi63d85e72018-09-06 16:00:21198
Mohsen Izadi0b9fbb62018-08-30 20:30:00199 mojom::GpuService* gpu_service();
200
Mohsen Izadi0b9fbb62018-08-30 20:30:00201 bool wake_up_gpu_before_drawing() const {
202 return wake_up_gpu_before_drawing_;
203 }
204
205 private:
Mohsen Izadif9505d82018-10-06 01:34:03206 friend class GpuHostImplTestApi;
207
Mohsen Izadi63d85e72018-09-06 16:00:21208#if defined(USE_OZONE)
209 void InitOzone();
210 void TerminateGpuProcess(const std::string& message);
211#endif // defined(USE_OZONE)
212
Mohsen Izadi0b9fbb62018-08-30 20:30:00213 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-Davisa70577c2019-06-26 18:54:31230 gpu_feature_info_for_hardware_gpu,
231 const gpu::GpuExtraInfo& gpu_extra_info) override;
Mohsen Izadi0b9fbb62018-08-30 20:30:00232 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 Izadi50c28052018-09-07 00:32:14241#if defined(OS_WIN)
Mohsen Izadi0b9fbb62018-08-30 20:30:00242 void SetChildSurface(gpu::SurfaceHandle parent,
243 gpu::SurfaceHandle child) override;
Mohsen Izadi50c28052018-09-07 00:32:14244#endif
Mohsen Izadi0b9fbb62018-08-30 20:30:00245 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 Izadif9505d82018-10-06 01:34:03253 std::unique_ptr<VizMainWrapper> viz_main_ptr_;
Mohsen Izadi0b9fbb62018-08-30 20:30:00254 const InitParams params_;
255
Mohsen Izadi63d85e72018-09-06 16:00:21256 // Task runner corresponding to the thread |this| is created on.
257 scoped_refptr<base::SingleThreadTaskRunner> host_thread_task_runner_;
258
Mohsen Izadi0b9fbb62018-08-30 20:30:00259 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 Izadibabed2b2018-08-31 01:37:39265 // List of connection error handlers for the GpuService.
266 std::vector<base::OnceClosure> connection_error_handlers_;
267
Mohsen Izadi0b9fbb62018-08-30 20:30:00268 // 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 Roman5c341f6d2019-07-15 15:56:10290 base::WeakPtrFactory<GpuHostImpl> weak_ptr_factory_{this};
Mohsen Izadi0b9fbb62018-08-30 20:30:00291
292 DISALLOW_COPY_AND_ASSIGN(GpuHostImpl);
293};
294
295} // namespace viz
296
297#endif // COMPONENTS_VIZ_HOST_GPU_HOST_IMPL_H_