Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / gpu / gpu_process_host.h
blobb7d625bb9febcad05afcd8bd00acc0c9782ba176
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_
6 #define CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_
8 #include <map>
9 #include <queue>
10 #include <set>
11 #include <string>
13 #include "base/callback.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/time/time.h"
18 #include "content/browser/gpu/gpu_surface_tracker.h"
19 #include "content/common/content_export.h"
20 #include "content/common/gpu/gpu_memory_uma_stats.h"
21 #include "content/common/gpu/gpu_process_launch_causes.h"
22 #include "content/common/gpu/gpu_result_codes.h"
23 #include "content/public/browser/browser_child_process_host_delegate.h"
24 #include "content/public/browser/gpu_data_manager.h"
25 #include "gpu/command_buffer/common/constants.h"
26 #include "gpu/config/gpu_info.h"
27 #include "ipc/ipc_sender.h"
28 #include "ipc/message_filter.h"
29 #include "ui/gfx/geometry/size.h"
30 #include "ui/gfx/gpu_memory_buffer.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "url/gurl.h"
34 struct GPUCreateCommandBufferConfig;
36 namespace IPC {
37 struct ChannelHandle;
40 namespace content {
41 class BrowserChildProcessHostImpl;
42 class GpuMainThread;
43 class InProcessChildThreadParams;
44 class RenderWidgetHostViewFrameSubscriber;
45 class ShaderDiskCache;
47 typedef base::Thread* (*GpuMainThreadFactoryFunction)(
48 const InProcessChildThreadParams&);
50 class GpuProcessHost : public BrowserChildProcessHostDelegate,
51 public IPC::Sender,
52 public base::NonThreadSafe {
53 public:
54 enum GpuProcessKind {
55 GPU_PROCESS_KIND_UNSANDBOXED,
56 GPU_PROCESS_KIND_SANDBOXED,
57 GPU_PROCESS_KIND_COUNT
60 typedef base::Callback<void(const IPC::ChannelHandle&, const gpu::GPUInfo&)>
61 EstablishChannelCallback;
63 typedef base::Callback<void(CreateCommandBufferResult)>
64 CreateCommandBufferCallback;
66 typedef base::Callback<void(const gfx::GpuMemoryBufferHandle& handle)>
67 CreateGpuMemoryBufferCallback;
69 static bool gpu_enabled() { return gpu_enabled_; }
70 static int gpu_crash_count() { return gpu_crash_count_; }
72 // Creates a new GpuProcessHost or gets an existing one, resulting in the
73 // launching of a GPU process if required. Returns null on failure. It
74 // is not safe to store the pointer once control has returned to the message
75 // loop as it can be destroyed. Instead store the associated GPU host ID.
76 // This could return NULL if GPU access is not allowed (blacklisted).
77 CONTENT_EXPORT static GpuProcessHost* Get(GpuProcessKind kind,
78 CauseForGpuLaunch cause);
80 // Retrieves a list of process handles for all gpu processes.
81 static void GetProcessHandles(
82 const GpuDataManager::GetGpuProcessHandlesCallback& callback);
84 // Helper function to send the given message to the GPU process on the IO
85 // thread. Calls Get and if a host is returned, sends it. Can be called from
86 // any thread. Deletes the message if it cannot be sent.
87 CONTENT_EXPORT static void SendOnIO(GpuProcessKind kind,
88 CauseForGpuLaunch cause,
89 IPC::Message* message);
91 CONTENT_EXPORT static void RegisterGpuMainThreadFactory(
92 GpuMainThreadFactoryFunction create);
94 // Get the GPU process host for the GPU process with the given ID. Returns
95 // null if the process no longer exists.
96 static GpuProcessHost* FromID(int host_id);
97 int host_id() const { return host_id_; }
99 // IPC::Sender implementation.
100 bool Send(IPC::Message* msg) override;
102 // Adds a message filter to the GpuProcessHost's channel.
103 void AddFilter(IPC::MessageFilter* filter);
105 // Tells the GPU process to create a new channel for communication with a
106 // client. Once the GPU process responds asynchronously with the IPC handle
107 // and GPUInfo, we call the callback.
108 void EstablishGpuChannel(int client_id,
109 bool share_context,
110 bool allow_future_sync_points,
111 const EstablishChannelCallback& callback);
113 // Tells the GPU process to create a new command buffer that draws into the
114 // given surface.
115 void CreateViewCommandBuffer(
116 const gfx::GLSurfaceHandle& compositing_surface,
117 int surface_id,
118 int client_id,
119 const GPUCreateCommandBufferConfig& init_params,
120 int route_id,
121 const CreateCommandBufferCallback& callback);
123 // Tells the GPU process to create a new GPU memory buffer.
124 void CreateGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
125 const gfx::Size& size,
126 gfx::GpuMemoryBuffer::Format format,
127 gfx::GpuMemoryBuffer::Usage usage,
128 int client_id,
129 int32 surface_id,
130 const CreateGpuMemoryBufferCallback& callback);
132 // Tells the GPU process to destroy GPU memory buffer.
133 void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
134 int client_id,
135 int sync_point);
137 // What kind of GPU process, e.g. sandboxed or unsandboxed.
138 GpuProcessKind kind();
140 void ForceShutdown();
142 void BeginFrameSubscription(
143 int surface_id,
144 base::WeakPtr<RenderWidgetHostViewFrameSubscriber> subscriber);
145 void EndFrameSubscription(int surface_id);
146 void LoadedShader(const std::string& key, const std::string& data);
148 private:
149 static bool ValidateHost(GpuProcessHost* host);
151 GpuProcessHost(int host_id, GpuProcessKind kind);
152 ~GpuProcessHost() override;
154 bool Init();
156 // Post an IPC message to the UI shim's message handler on the UI thread.
157 void RouteOnUIThread(const IPC::Message& message);
159 // BrowserChildProcessHostDelegate implementation.
160 bool OnMessageReceived(const IPC::Message& message) override;
161 void OnChannelConnected(int32 peer_pid) override;
162 void OnProcessLaunched() override;
163 void OnProcessCrashed(int exit_code) override;
165 // Message handlers.
166 void OnInitialized(bool result, const gpu::GPUInfo& gpu_info);
167 void OnChannelEstablished(const IPC::ChannelHandle& channel_handle);
168 void OnCommandBufferCreated(CreateCommandBufferResult result);
169 void OnDestroyCommandBuffer(int32 surface_id);
170 void OnGpuMemoryBufferCreated(const gfx::GpuMemoryBufferHandle& handle);
171 void OnDidCreateOffscreenContext(const GURL& url);
172 void OnDidLoseContext(bool offscreen,
173 gpu::error::ContextLostReason reason,
174 const GURL& url);
175 void OnDidDestroyOffscreenContext(const GURL& url);
176 void OnGpuMemoryUmaStatsReceived(const GPUMemoryUmaStats& stats);
177 #if defined(OS_MACOSX)
178 void OnAcceleratedSurfaceBuffersSwapped(const IPC::Message& message);
179 #endif
181 void CreateChannelCache(int32 client_id);
182 void OnDestroyChannel(int32 client_id);
183 void OnCacheShader(int32 client_id, const std::string& key,
184 const std::string& shader);
186 bool LaunchGpuProcess(const std::string& channel_id);
188 void SendOutstandingReplies();
190 void BlockLiveOffscreenContexts();
192 // Update GPU crash counters. Disable GPU if crash limit is reached.
193 void RecordProcessCrash();
195 std::string GetShaderPrefixKey();
197 // The serial number of the GpuProcessHost / GpuProcessHostUIShim pair.
198 int host_id_;
200 // These are the channel requests that we have already sent to
201 // the GPU process, but haven't heard back about yet.
202 std::queue<EstablishChannelCallback> channel_requests_;
204 // The pending create command buffer requests we need to reply to.
205 std::queue<CreateCommandBufferCallback> create_command_buffer_requests_;
207 // The pending create gpu memory buffer requests we need to reply to.
208 std::queue<CreateGpuMemoryBufferCallback> create_gpu_memory_buffer_requests_;
210 // Surface ids for pending gpu memory buffer request refs.
211 std::queue<int32> create_gpu_memory_buffer_surface_refs_;
213 // Qeueud messages to send when the process launches.
214 std::queue<IPC::Message*> queued_messages_;
216 // Whether the GPU process is valid, set to false after Send() failed.
217 bool valid_;
219 // Whether we are running a GPU thread inside the browser process instead
220 // of a separate GPU process.
221 bool in_process_;
223 bool swiftshader_rendering_;
224 GpuProcessKind kind_;
226 scoped_ptr<base::Thread> in_process_gpu_thread_;
228 // Whether we actually launched a GPU process.
229 bool process_launched_;
231 // Whether the GPU process successfully initialized.
232 bool initialized_;
234 // Time Init started. Used to log total GPU process startup time to UMA.
235 base::TimeTicks init_start_time_;
237 // Whether this host recorded a GPU crash or not.
238 bool gpu_crash_recorded_;
240 // Master switch for enabling/disabling GPU acceleration for the current
241 // browser session. It does not change the acceleration settings for
242 // existing tabs, just the future ones.
243 static bool gpu_enabled_;
245 static bool hardware_gpu_enabled_;
247 static int gpu_crash_count_;
248 static int gpu_recent_crash_count_;
249 static bool crashed_before_;
250 static int swiftshader_crash_count_;
252 scoped_ptr<BrowserChildProcessHostImpl> process_;
254 // Track the URLs of the pages which have live offscreen contexts,
255 // assumed to be associated with untrusted content such as WebGL.
256 // For best robustness, when any context lost notification is
257 // received, assume all of these URLs are guilty, and block
258 // automatic execution of 3D content from those domains.
259 std::multiset<GURL> urls_with_live_offscreen_contexts_;
261 // Statics kept around to send to UMA histograms on GPU process lost.
262 bool uma_memory_stats_received_;
263 GPUMemoryUmaStats uma_memory_stats_;
265 // This map of frame subscribers are listening for frame presentation events.
266 // The key is the surface id and value is the subscriber.
267 typedef base::hash_map<int,
268 base::WeakPtr<RenderWidgetHostViewFrameSubscriber> >
269 FrameSubscriberMap;
270 FrameSubscriberMap frame_subscribers_;
272 typedef std::map<int32, scoped_refptr<ShaderDiskCache> >
273 ClientIdToShaderCacheMap;
274 ClientIdToShaderCacheMap client_id_to_shader_cache_;
276 std::string shader_prefix_key_;
278 // Keep an extra reference to the SurfaceRef stored in the GpuSurfaceTracker
279 // in this map so that we don't destroy it whilst the GPU process is
280 // drawing to it.
281 typedef std::multimap<int, scoped_refptr<GpuSurfaceTracker::SurfaceRef> >
282 SurfaceRefMap;
283 SurfaceRefMap surface_refs_;
285 DISALLOW_COPY_AND_ASSIGN(GpuProcessHost);
288 } // namespace content
290 #endif // CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_