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_COMMON_GPU_GPU_CHANNEL_H_ 6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_H_ 7 8 #include <deque> 9 #include <string> 10 11 #include "base/id_map.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_vector.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/process/process.h" 17 #include "build/build_config.h" 18 #include "content/common/gpu/gpu_command_buffer_stub.h" 19 #include "content/common/gpu/gpu_memory_manager.h" 20 #include "content/common/message_router.h" 21 #include "ipc/ipc_sync_channel.h" 22 #include "ui/gfx/native_widget_types.h" 23 #include "ui/gfx/size.h" 24 #include "ui/gl/gl_share_group.h" 25 #include "ui/gl/gpu_preference.h" 26 27 #if defined(OS_ANDROID) 28 #include "content/common/android/surface_texture_peer.h" 29 #endif 30 31 struct GPUCreateCommandBufferConfig; 32 33 namespace base { 34 class MessageLoopProxy; 35 class WaitableEvent; 36 } 37 38 namespace gpu { 39 class PreemptionFlag; 40 namespace gles2 { 41 class ImageManager; 42 } 43 } 44 45 #if defined(OS_ANDROID) 46 namespace content { 47 class StreamTextureManagerAndroid; 48 } 49 #endif 50 51 namespace content { 52 class DevToolsGpuAgent; 53 class GpuChannelManager; 54 class GpuChannelMessageFilter; 55 struct GpuRenderingStats; 56 class GpuVideoEncodeAccelerator; 57 class GpuWatchdog; 58 59 // Encapsulates an IPC channel between the GPU process and one renderer 60 // process. On the renderer side there's a corresponding GpuChannelHost. 61 class GpuChannel : public IPC::Listener, 62 public IPC::Sender, 63 public base::RefCountedThreadSafe<GpuChannel> { 64 public: 65 // Takes ownership of the renderer process handle. 66 GpuChannel(GpuChannelManager* gpu_channel_manager, 67 GpuWatchdog* watchdog, 68 gfx::GLShareGroup* share_group, 69 gpu::gles2::MailboxManager* mailbox_manager, 70 int client_id, 71 bool software); 72 73 bool Init(base::MessageLoopProxy* io_message_loop, 74 base::WaitableEvent* shutdown_event); 75 76 // Get the GpuChannelManager that owns this channel. 77 GpuChannelManager* gpu_channel_manager() const { 78 return gpu_channel_manager_; 79 } 80 81 // Returns the name of the associated IPC channel. 82 std::string GetChannelName(); 83 84 #if defined(OS_POSIX) 85 int TakeRendererFileDescriptor(); 86 #endif // defined(OS_POSIX) 87 88 base::ProcessId renderer_pid() const { return channel_->peer_pid(); } 89 90 scoped_refptr<base::MessageLoopProxy> io_message_loop() const { 91 return io_message_loop_; 92 } 93 94 // IPC::Listener implementation: 95 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 96 virtual void OnChannelError() OVERRIDE; 97 98 // IPC::Sender implementation: 99 virtual bool Send(IPC::Message* msg) OVERRIDE; 100 101 // Requeue the message that is currently being processed to the beginning of 102 // the queue. Used when the processing of a message gets aborted because of 103 // unscheduling conditions. 104 void RequeueMessage(); 105 106 // This is called when a command buffer transitions from the unscheduled 107 // state to the scheduled state, which potentially means the channel 108 // transitions from the unscheduled to the scheduled state. When this occurs 109 // deferred IPC messaged are handled. 110 void OnScheduled(); 111 112 // This is called when a command buffer transitions between scheduled and 113 // descheduled states. When any stub is descheduled, we stop preempting 114 // other channels. 115 void StubSchedulingChanged(bool scheduled); 116 117 void CreateViewCommandBuffer( 118 const gfx::GLSurfaceHandle& window, 119 int32 surface_id, 120 const GPUCreateCommandBufferConfig& init_params, 121 int32* route_id); 122 123 void CreateImage( 124 gfx::PluginWindowHandle window, 125 int32 image_id, 126 gfx::Size* size); 127 void DeleteImage(int32 image_id); 128 129 gfx::GLShareGroup* share_group() const { return share_group_.get(); } 130 131 GpuCommandBufferStub* LookupCommandBuffer(int32 route_id); 132 133 void LoseAllContexts(); 134 void MarkAllContextsLost(); 135 136 // Destroy channel and all contained contexts. 137 void DestroySoon(); 138 139 // Generate a route ID guaranteed to be unique for this channel. 140 int GenerateRouteID(); 141 142 // Called to add/remove a listener for a particular message routing ID. 143 void AddRoute(int32 route_id, IPC::Listener* listener); 144 void RemoveRoute(int32 route_id); 145 146 gpu::PreemptionFlag* GetPreemptionFlag(); 147 148 bool handle_messages_scheduled() const { return handle_messages_scheduled_; } 149 uint64 messages_processed() const { return messages_processed_; } 150 151 // If |preemption_flag->IsSet()|, any stub on this channel 152 // should stop issuing GL commands. Setting this to NULL stops deferral. 153 void SetPreemptByFlag( 154 scoped_refptr<gpu::PreemptionFlag> preemption_flag); 155 156 #if defined(OS_ANDROID) 157 StreamTextureManagerAndroid* stream_texture_manager() { 158 return stream_texture_manager_.get(); 159 } 160 #endif 161 162 void CacheShader(const std::string& key, const std::string& shader); 163 164 void AddFilter(IPC::ChannelProxy::MessageFilter* filter); 165 void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter); 166 167 protected: 168 virtual ~GpuChannel(); 169 170 private: 171 friend class base::RefCountedThreadSafe<GpuChannel>; 172 friend class GpuChannelMessageFilter; 173 174 void OnDestroy(); 175 176 bool OnControlMessageReceived(const IPC::Message& msg); 177 178 void HandleMessage(); 179 180 // Message handlers. 181 void OnCreateOffscreenCommandBuffer( 182 const gfx::Size& size, 183 const GPUCreateCommandBufferConfig& init_params, 184 int32* route_id); 185 void OnDestroyCommandBuffer(int32 route_id); 186 void OnCreateVideoEncoder(int32* route_id); 187 void OnDestroyVideoEncoder(int32 route_id); 188 void OnDevToolsStartEventsRecording(int32* route_id); 189 void OnDevToolsStopEventsRecording(); 190 191 #if defined(OS_ANDROID) 192 // Register the StreamTextureProxy class with the gpu process so that all 193 // the callbacks will be correctly forwarded to the renderer. 194 void OnRegisterStreamTextureProxy(int32 stream_id, int32* route_id); 195 196 // Create a java surface texture object and send it to the renderer process 197 // through binder thread. 198 void OnEstablishStreamTexture( 199 int32 stream_id, int32 primary_id, int32 secondary_id); 200 201 // Set the size of StreamTexture. 202 void OnSetStreamTextureSize(int32 stream_id, const gfx::Size& size); 203 #endif 204 205 // Collect rendering stats. 206 void OnCollectRenderingStatsForSurface( 207 int32 surface_id, GpuRenderingStats* stats); 208 209 // Decrement the count of unhandled IPC messages and defer preemption. 210 void MessageProcessed(); 211 212 // The lifetime of objects of this class is managed by a GpuChannelManager. 213 // The GpuChannelManager destroy all the GpuChannels that they own when they 214 // are destroyed. So a raw pointer is safe. 215 GpuChannelManager* gpu_channel_manager_; 216 217 scoped_ptr<IPC::SyncChannel> channel_; 218 219 uint64 messages_processed_; 220 221 // Whether the processing of IPCs on this channel is stalled and we should 222 // preempt other GpuChannels. 223 scoped_refptr<gpu::PreemptionFlag> preempting_flag_; 224 225 // If non-NULL, all stubs on this channel should stop processing GL 226 // commands (via their GpuScheduler) when preempted_flag_->IsSet() 227 scoped_refptr<gpu::PreemptionFlag> preempted_flag_; 228 229 std::deque<IPC::Message*> deferred_messages_; 230 231 // The id of the client who is on the other side of the channel. 232 int client_id_; 233 234 // Uniquely identifies the channel within this GPU process. 235 std::string channel_id_; 236 237 // Used to implement message routing functionality to CommandBuffer objects 238 MessageRouter router_; 239 240 // The share group that all contexts associated with a particular renderer 241 // process use. 242 scoped_refptr<gfx::GLShareGroup> share_group_; 243 244 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_; 245 scoped_refptr<gpu::gles2::ImageManager> image_manager_; 246 #if defined(OS_ANDROID) 247 scoped_ptr<StreamTextureManagerAndroid> stream_texture_manager_; 248 #endif 249 250 typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap; 251 StubMap stubs_; 252 253 typedef IDMap<GpuVideoEncodeAccelerator, IDMapOwnPointer> EncoderMap; 254 EncoderMap video_encoders_; 255 256 bool log_messages_; // True if we should log sent and received messages. 257 gpu::gles2::DisallowedFeatures disallowed_features_; 258 GpuWatchdog* watchdog_; 259 bool software_; 260 bool handle_messages_scheduled_; 261 bool processed_get_state_fast_; 262 IPC::Message* currently_processing_message_; 263 264 base::WeakPtrFactory<GpuChannel> weak_factory_; 265 266 scoped_refptr<GpuChannelMessageFilter> filter_; 267 scoped_refptr<base::MessageLoopProxy> io_message_loop_; 268 scoped_ptr<DevToolsGpuAgent> devtools_gpu_agent_; 269 270 size_t num_stubs_descheduled_; 271 272 DISALLOW_COPY_AND_ASSIGN(GpuChannel); 273 }; 274 275 } // namespace content 276 277 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_ 278