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_
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/native_widget_types.h"
30 #include "ui/gfx/size.h"
33 struct GPUCreateCommandBufferConfig;
34 struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params;
35 struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params;
36 struct GpuHostMsg_AcceleratedSurfaceRelease_Params;
39 struct GpuMemoryBufferHandle;
47 class BrowserChildProcessHostImpl;
49 class RenderWidgetHostViewFrameSubscriber;
50 class ShaderDiskCache;
52 typedef base::Thread* (*GpuMainThreadFactoryFunction)(const std::string& id);
54 class GpuProcessHost : public BrowserChildProcessHostDelegate,
56 public base::NonThreadSafe {
59 GPU_PROCESS_KIND_UNSANDBOXED,
60 GPU_PROCESS_KIND_SANDBOXED,
61 GPU_PROCESS_KIND_COUNT
64 typedef base::Callback<void(const IPC::ChannelHandle&, const gpu::GPUInfo&)>
65 EstablishChannelCallback;
67 typedef base::Callback<void(CreateCommandBufferResult)>
68 CreateCommandBufferCallback;
70 typedef base::Callback<void(const gfx::Size)> CreateImageCallback;
72 typedef base::Callback<void(const gfx::GpuMemoryBufferHandle& handle)>
73 CreateGpuMemoryBufferCallback;
75 static bool gpu_enabled() { return gpu_enabled_; }
77 // Creates a new GpuProcessHost or gets an existing one, resulting in the
78 // launching of a GPU process if required. Returns null on failure. It
79 // is not safe to store the pointer once control has returned to the message
80 // loop as it can be destroyed. Instead store the associated GPU host ID.
81 // This could return NULL if GPU access is not allowed (blacklisted).
82 CONTENT_EXPORT static GpuProcessHost* Get(GpuProcessKind kind,
83 CauseForGpuLaunch cause);
85 // Retrieves a list of process handles for all gpu processes.
86 static void GetProcessHandles(
87 const GpuDataManager::GetGpuProcessHandlesCallback& callback);
89 // Helper function to send the given message to the GPU process on the IO
90 // thread. Calls Get and if a host is returned, sends it. Can be called from
91 // any thread. Deletes the message if it cannot be sent.
92 CONTENT_EXPORT static void SendOnIO(GpuProcessKind kind,
93 CauseForGpuLaunch cause,
94 IPC::Message* message);
96 CONTENT_EXPORT static void RegisterGpuMainThreadFactory(
97 GpuMainThreadFactoryFunction create);
99 // Get the GPU process host for the GPU process with the given ID. Returns
100 // null if the process no longer exists.
101 static GpuProcessHost* FromID(int host_id);
102 int host_id() const { return host_id_; }
104 // IPC::Sender implementation.
105 virtual bool Send(IPC::Message* msg) OVERRIDE;
107 // Adds a message filter to the GpuProcessHost's channel.
108 void AddFilter(IPC::MessageFilter* filter);
110 // Tells the GPU process to create a new channel for communication with a
111 // client. Once the GPU process responds asynchronously with the IPC handle
112 // and GPUInfo, we call the callback.
113 void EstablishGpuChannel(int client_id,
115 const EstablishChannelCallback& callback);
117 // Tells the GPU process to create a new command buffer that draws into the
119 void CreateViewCommandBuffer(
120 const gfx::GLSurfaceHandle& compositing_surface,
123 const GPUCreateCommandBufferConfig& init_params,
125 const CreateCommandBufferCallback& callback);
127 // Tells the GPU process to create a new image using the given window.
129 gfx::PluginWindowHandle window,
132 const CreateImageCallback& callback);
134 // Tells the GPU process to delete image.
135 void DeleteImage(int client_id, int image_id, int sync_point);
137 void CreateGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle,
138 const gfx::Size& size,
139 unsigned internalformat,
141 const CreateGpuMemoryBufferCallback& callback);
142 void DestroyGpuMemoryBuffer(const gfx::GpuMemoryBufferHandle& handle,
145 // What kind of GPU process, e.g. sandboxed or unsandboxed.
146 GpuProcessKind kind();
148 void ForceShutdown();
150 void BeginFrameSubscription(
152 base::WeakPtr<RenderWidgetHostViewFrameSubscriber> subscriber);
153 void EndFrameSubscription(int surface_id);
154 void LoadedShader(const std::string& key, const std::string& data);
157 static bool ValidateHost(GpuProcessHost* host);
159 GpuProcessHost(int host_id, GpuProcessKind kind);
160 virtual ~GpuProcessHost();
164 // Post an IPC message to the UI shim's message handler on the UI thread.
165 void RouteOnUIThread(const IPC::Message& message);
167 // BrowserChildProcessHostDelegate implementation.
168 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
169 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
170 virtual void OnProcessLaunched() OVERRIDE;
171 virtual void OnProcessCrashed(int exit_code) OVERRIDE;
174 void OnInitialized(bool result, const gpu::GPUInfo& gpu_info);
175 void OnChannelEstablished(const IPC::ChannelHandle& channel_handle);
176 void OnCommandBufferCreated(CreateCommandBufferResult result);
177 void OnDestroyCommandBuffer(int32 surface_id);
178 void OnImageCreated(const gfx::Size size);
179 void OnGpuMemoryBufferCreated(const gfx::GpuMemoryBufferHandle& handle);
180 void OnDidCreateOffscreenContext(const GURL& url);
181 void OnDidLoseContext(bool offscreen,
182 gpu::error::ContextLostReason reason,
184 void OnDidDestroyOffscreenContext(const GURL& url);
185 void OnGpuMemoryUmaStatsReceived(const GPUMemoryUmaStats& stats);
186 #if defined(OS_MACOSX)
187 void OnAcceleratedSurfaceBuffersSwapped(
188 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params);
191 void CreateChannelCache(int32 client_id);
192 void OnDestroyChannel(int32 client_id);
193 void OnCacheShader(int32 client_id, const std::string& key,
194 const std::string& shader);
196 bool LaunchGpuProcess(const std::string& channel_id);
198 void SendOutstandingReplies();
200 void BlockLiveOffscreenContexts();
202 std::string GetShaderPrefixKey();
204 // The serial number of the GpuProcessHost / GpuProcessHostUIShim pair.
207 // These are the channel requests that we have already sent to
208 // the GPU process, but haven't heard back about yet.
209 std::queue<EstablishChannelCallback> channel_requests_;
211 // The pending create command buffer requests we need to reply to.
212 std::queue<CreateCommandBufferCallback> create_command_buffer_requests_;
214 // The pending create image requests we need to reply to.
215 std::queue<CreateImageCallback> create_image_requests_;
217 // The pending create gpu memory buffer requests we need to reply to.
218 std::queue<CreateGpuMemoryBufferCallback> create_gpu_memory_buffer_requests_;
220 // Qeueud messages to send when the process launches.
221 std::queue<IPC::Message*> queued_messages_;
223 // Whether the GPU process is valid, set to false after Send() failed.
226 // Whether we are running a GPU thread inside the browser process instead
227 // of a separate GPU process.
230 bool swiftshader_rendering_;
231 GpuProcessKind kind_;
233 scoped_ptr<base::Thread> in_process_gpu_thread_;
235 // Whether we actually launched a GPU process.
236 bool process_launched_;
238 // Whether the GPU process successfully initialized.
241 // Time Init started. Used to log total GPU process startup time to UMA.
242 base::TimeTicks init_start_time_;
244 // Master switch for enabling/disabling GPU acceleration for the current
245 // browser session. It does not change the acceleration settings for
246 // existing tabs, just the future ones.
247 static bool gpu_enabled_;
249 static bool hardware_gpu_enabled_;
251 scoped_ptr<BrowserChildProcessHostImpl> process_;
253 // Track the URLs of the pages which have live offscreen contexts,
254 // assumed to be associated with untrusted content such as WebGL.
255 // For best robustness, when any context lost notification is
256 // received, assume all of these URLs are guilty, and block
257 // automatic execution of 3D content from those domains.
258 std::multiset<GURL> urls_with_live_offscreen_contexts_;
260 // Statics kept around to send to UMA histograms on GPU process lost.
261 bool uma_memory_stats_received_;
262 GPUMemoryUmaStats uma_memory_stats_;
264 // This map of frame subscribers are listening for frame presentation events.
265 // The key is the surface id and value is the subscriber.
266 typedef base::hash_map<int,
267 base::WeakPtr<RenderWidgetHostViewFrameSubscriber> >
269 FrameSubscriberMap frame_subscribers_;
271 typedef std::map<int32, scoped_refptr<ShaderDiskCache> >
272 ClientIdToShaderCacheMap;
273 ClientIdToShaderCacheMap client_id_to_shader_cache_;
275 std::string shader_prefix_key_;
277 // Keep an extra reference to the SurfaceRef stored in the GpuSurfaceTracker
278 // in this map so that we don't destroy it whilst the GPU process is
280 typedef std::multimap<int, scoped_refptr<GpuSurfaceTracker::SurfaceRef> >
282 SurfaceRefMap surface_refs_;
284 DISALLOW_COPY_AND_ASSIGN(GpuProcessHost);
287 } // namespace content
289 #endif // CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_