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