gpu_process_host.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 4 5#ifndef CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_ 6#define CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_ 7 8#include <map> 9#include <queue> 10#include <string> 11 12#include "base/callback.h" 13#include "base/memory/linked_ptr.h" 14#include "base/process.h" 15#include "base/threading/non_thread_safe.h" 16#include "base/time.h" 17#include "content/common/content_export.h" 18#include "content/common/gpu/gpu_process_launch_causes.h" 19#include "content/public/browser/browser_child_process_host_delegate.h" 20#include "content/public/common/gpu_info.h" 21#include "ipc/ipc_channel_proxy.h" 22#include "ipc/ipc_sender.h" 23#include "ui/gfx/native_widget_types.h" 24#include "ui/gfx/size.h" 25 26struct GPUCreateCommandBufferConfig; 27struct GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params; 28struct GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params; 29struct GpuHostMsg_AcceleratedSurfaceRelease_Params; 30 31namespace IPC { 32struct ChannelHandle; 33} 34 35namespace content { 36class BrowserChildProcessHostImpl; 37class GpuMainThread; 38 39class GpuProcessHost : public BrowserChildProcessHostDelegate, 40 public IPC::Sender, 41 public base::NonThreadSafe { 42 public: 43 enum GpuProcessKind { 44 GPU_PROCESS_KIND_UNSANDBOXED, 45 GPU_PROCESS_KIND_SANDBOXED, 46 GPU_PROCESS_KIND_COUNT 47 }; 48 49 typedef base::Callback<void(const IPC::ChannelHandle&, const GPUInfo&)> 50 EstablishChannelCallback; 51 52 typedef base::Callback<void(int32)> CreateCommandBufferCallback; 53 54 typedef base::Callback<void(const gfx::Size)> CreateImageCallback; 55 56 static bool gpu_enabled() { return gpu_enabled_; } 57 58 // Creates a new GpuProcessHost or gets an existing one, resulting in the 59 // launching of a GPU process if required. Returns null on failure. It 60 // is not safe to store the pointer once control has returned to the message 61 // loop as it can be destroyed. Instead store the associated GPU host ID. 62 // This could return NULL if GPU access is not allowed (blacklisted). 63 static GpuProcessHost* Get(GpuProcessKind kind, CauseForGpuLaunch cause); 64 65 // Helper function to send the given message to the GPU process on the IO 66 // thread. Calls Get and if a host is returned, sends it. Can be called from 67 // any thread. Deletes the message if it cannot be sent. 68 CONTENT_EXPORT static void SendOnIO(GpuProcessKind kind, 69 CauseForGpuLaunch cause, 70 IPC::Message* message); 71 72 // Get the GPU process host for the GPU process with the given ID. Returns 73 // null if the process no longer exists. 74 static GpuProcessHost* FromID(int host_id); 75 int host_id() const { return host_id_; } 76 77 // IPC::Sender implementation. 78 virtual bool Send(IPC::Message* msg) OVERRIDE; 79 80 // Adds a message filter to the GpuProcessHost's channel. 81 void AddFilter(IPC::ChannelProxy::MessageFilter* filter); 82 83 // Tells the GPU process to create a new channel for communication with a 84 // client. Once the GPU process responds asynchronously with the IPC handle 85 // and GPUInfo, we call the callback. 86 void EstablishGpuChannel(int client_id, 87 bool share_context, 88 const EstablishChannelCallback& callback); 89 90 // Tells the GPU process to create a new command buffer that draws into the 91 // given surface. 92 void CreateViewCommandBuffer( 93 const gfx::GLSurfaceHandle& compositing_surface, 94 int surface_id, 95 int client_id, 96 const GPUCreateCommandBufferConfig& init_params, 97 const CreateCommandBufferCallback& callback); 98 99 // Tells the GPU process to create a new image using the given window. 100 void CreateImage( 101 gfx::PluginWindowHandle window, 102 int client_id, 103 int image_id, 104 const CreateImageCallback& callback); 105 106 // Tells the GPU process to delete image. 107 void DeleteImage(int client_id, int image_id, int sync_point); 108 109 // Whether this GPU process is set up to use software rendering. 110 bool software_rendering(); 111 112 // What kind of GPU process, e.g. sandboxed or unsandboxed. 113 GpuProcessKind kind(); 114 115 void ForceShutdown(); 116 117 private: 118 static bool HostIsValid(GpuProcessHost* host); 119 120 GpuProcessHost(int host_id, GpuProcessKind kind); 121 virtual ~GpuProcessHost(); 122 123 bool Init(); 124 125 // Post an IPC message to the UI shim's message handler on the UI thread. 126 void RouteOnUIThread(const IPC::Message& message); 127 128 // BrowserChildProcessHostDelegate implementation. 129 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 130 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 131 virtual void OnProcessLaunched() OVERRIDE; 132 virtual void OnProcessCrashed(int exit_code) OVERRIDE; 133 134 // Message handlers. 135 void OnInitialized(bool result); 136 void OnChannelEstablished(const IPC::ChannelHandle& channel_handle); 137 void OnCommandBufferCreated(const int32 route_id); 138 void OnDestroyCommandBuffer(int32 surface_id); 139 void OnImageCreated(const gfx::Size size); 140 141#if defined(OS_MACOSX) 142 void OnAcceleratedSurfaceBuffersSwapped( 143 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params); 144#endif 145 // Note: Different implementations depending on USE_AURA. 146#if defined(OS_WIN) 147 void OnAcceleratedSurfaceBuffersSwapped( 148 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params); 149 void OnAcceleratedSurfacePostSubBuffer( 150 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params); 151 void OnAcceleratedSurfaceSuspend(int32 surface_id); 152 void OnAcceleratedSurfaceRelease( 153 const GpuHostMsg_AcceleratedSurfaceRelease_Params& params); 154#endif 155 156 bool LaunchGpuProcess(const std::string& channel_id); 157 158 void SendOutstandingReplies(); 159 void EstablishChannelError( 160 const EstablishChannelCallback& callback, 161 const IPC::ChannelHandle& channel_handle, 162 base::ProcessHandle client_process_for_gpu, 163 const GPUInfo& gpu_info); 164 void CreateCommandBufferError(const CreateCommandBufferCallback& callback, 165 int32 route_id); 166 void CreateImageError(const CreateImageCallback& callback, 167 const gfx::Size size); 168 169 // The serial number of the GpuProcessHost / GpuProcessHostUIShim pair. 170 int host_id_; 171 172 // These are the channel requests that we have already sent to 173 // the GPU process, but haven't heard back about yet. 174 std::queue<EstablishChannelCallback> channel_requests_; 175 176 // The pending create command buffer requests we need to reply to. 177 std::queue<CreateCommandBufferCallback> create_command_buffer_requests_; 178 179 // The pending create image requests we need to reply to. 180 std::queue<CreateImageCallback> create_image_requests_; 181 182#if defined(TOOLKIT_GTK) 183 // Encapsulates surfaces that we lock when creating view command buffers. 184 // We release this lock once the command buffer (or associated GPU process) 185 // is destroyed. This prevents the browser from destroying the surface 186 // while the GPU process is drawing to it. 187 188 // Multimap is used to simulate reference counting, see comment in 189 // GpuProcessHostUIShim::CreateViewCommandBuffer. 190 class SurfaceRef; 191 typedef std::multimap<int, linked_ptr<SurfaceRef> > SurfaceRefMap; 192 SurfaceRefMap surface_refs_; 193#endif 194 195 // Qeueud messages to send when the process launches. 196 std::queue<IPC::Message*> queued_messages_; 197 198 // Whether the GPU process is valid, set to false after Send() failed. 199 bool valid_; 200 201 // Whether we are running a GPU thread inside the browser process instead 202 // of a separate GPU process. 203 bool in_process_; 204 205 bool software_rendering_; 206 GpuProcessKind kind_; 207 208 scoped_ptr<GpuMainThread> in_process_gpu_thread_; 209 210 // Whether we actually launched a GPU process. 211 bool process_launched_; 212 213 // Time Init started. Used to log total GPU process startup time to UMA. 214 base::TimeTicks init_start_time_; 215 216 // Master switch for enabling/disabling GPU acceleration for the current 217 // browser session. It does not change the acceleration settings for 218 // existing tabs, just the future ones. 219 static bool gpu_enabled_; 220 221 static bool hardware_gpu_enabled_; 222 223 scoped_ptr<BrowserChildProcessHostImpl> process_; 224 225 DISALLOW_COPY_AND_ASSIGN(GpuProcessHost); 226}; 227 228} // namespace content 229 230#endif // CONTENT_BROWSER_GPU_GPU_PROCESS_HOST_H_ 231