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_COMMON_GPU_GPU_CHANNEL_H_
6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_H_
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"
27 struct GPUCreateCommandBufferConfig;
30 class MessageLoopProxy;
42 class DevToolsGpuAgent;
43 class GpuChannelManager;
44 class GpuChannelMessageFilter;
45 class GpuVideoEncodeAccelerator;
48 // Encapsulates an IPC channel between the GPU process and one renderer
49 // process. On the renderer side there's a corresponding GpuChannelHost.
50 class GpuChannel : public IPC::Listener,
52 public base::RefCountedThreadSafe<GpuChannel> {
54 // Takes ownership of the renderer process handle.
55 GpuChannel(GpuChannelManager* gpu_channel_manager,
56 GpuWatchdog* watchdog,
57 gfx::GLShareGroup* share_group,
58 gpu::gles2::MailboxManager* mailbox_manager,
62 void Init(base::MessageLoopProxy* io_message_loop,
63 base::WaitableEvent* shutdown_event);
65 // Get the GpuChannelManager that owns this channel.
66 GpuChannelManager* gpu_channel_manager() const {
67 return gpu_channel_manager_;
70 // Returns the name of the associated IPC channel.
71 std::string GetChannelName();
74 int TakeRendererFileDescriptor();
75 #endif // defined(OS_POSIX)
77 base::ProcessId renderer_pid() const { return channel_->peer_pid(); }
79 scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
80 return io_message_loop_;
83 // IPC::Listener implementation:
84 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
85 virtual void OnChannelError() OVERRIDE;
87 // IPC::Sender implementation:
88 virtual bool Send(IPC::Message* msg) OVERRIDE;
90 // Requeue the message that is currently being processed to the beginning of
91 // the queue. Used when the processing of a message gets aborted because of
92 // unscheduling conditions.
93 void RequeueMessage();
95 // This is called when a command buffer transitions from the unscheduled
96 // state to the scheduled state, which potentially means the channel
97 // transitions from the unscheduled to the scheduled state. When this occurs
98 // deferred IPC messaged are handled.
101 // This is called when a command buffer transitions between scheduled and
102 // descheduled states. When any stub is descheduled, we stop preempting
104 void StubSchedulingChanged(bool scheduled);
106 void CreateViewCommandBuffer(
107 const gfx::GLSurfaceHandle& window,
109 const GPUCreateCommandBufferConfig& init_params,
113 gfx::PluginWindowHandle window,
116 void DeleteImage(int32 image_id);
118 gfx::GLShareGroup* share_group() const { return share_group_.get(); }
120 GpuCommandBufferStub* LookupCommandBuffer(int32 route_id);
122 void LoseAllContexts();
123 void MarkAllContextsLost();
125 // Destroy channel and all contained contexts.
128 // Generate a route ID guaranteed to be unique for this channel.
129 int32 GenerateRouteID();
131 // Called to add/remove a listener for a particular message routing ID.
132 void AddRoute(int32 route_id, IPC::Listener* listener);
133 void RemoveRoute(int32 route_id);
135 gpu::PreemptionFlag* GetPreemptionFlag();
137 bool handle_messages_scheduled() const { return handle_messages_scheduled_; }
138 uint64 messages_processed() const { return messages_processed_; }
140 // If |preemption_flag->IsSet()|, any stub on this channel
141 // should stop issuing GL commands. Setting this to NULL stops deferral.
142 void SetPreemptByFlag(
143 scoped_refptr<gpu::PreemptionFlag> preemption_flag);
145 void CacheShader(const std::string& key, const std::string& shader);
147 void AddFilter(IPC::ChannelProxy::MessageFilter* filter);
148 void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter);
150 uint64 GetMemoryUsage();
153 virtual ~GpuChannel();
156 friend class base::RefCountedThreadSafe<GpuChannel>;
157 friend class GpuChannelMessageFilter;
161 bool OnControlMessageReceived(const IPC::Message& msg);
163 void HandleMessage();
166 void OnCreateOffscreenCommandBuffer(
167 const gfx::Size& size,
168 const GPUCreateCommandBufferConfig& init_params,
170 void OnDestroyCommandBuffer(int32 route_id);
171 void OnCreateVideoEncoder(int32* route_id);
172 void OnDestroyVideoEncoder(int32 route_id);
173 void OnDevToolsStartEventsRecording(int32* route_id);
174 void OnDevToolsStopEventsRecording();
176 // Decrement the count of unhandled IPC messages and defer preemption.
177 void MessageProcessed();
179 // The lifetime of objects of this class is managed by a GpuChannelManager.
180 // The GpuChannelManager destroy all the GpuChannels that they own when they
181 // are destroyed. So a raw pointer is safe.
182 GpuChannelManager* gpu_channel_manager_;
184 scoped_ptr<IPC::SyncChannel> channel_;
186 uint64 messages_processed_;
188 // Whether the processing of IPCs on this channel is stalled and we should
189 // preempt other GpuChannels.
190 scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
192 // If non-NULL, all stubs on this channel should stop processing GL
193 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
194 scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
196 std::deque<IPC::Message*> deferred_messages_;
198 // The id of the client who is on the other side of the channel.
201 // Uniquely identifies the channel within this GPU process.
202 std::string channel_id_;
204 // Used to implement message routing functionality to CommandBuffer objects
205 MessageRouter router_;
207 // The share group that all contexts associated with a particular renderer
209 scoped_refptr<gfx::GLShareGroup> share_group_;
211 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
212 scoped_refptr<gpu::gles2::ImageManager> image_manager_;
214 typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap;
217 typedef IDMap<GpuVideoEncodeAccelerator, IDMapOwnPointer> EncoderMap;
218 EncoderMap video_encoders_;
220 bool log_messages_; // True if we should log sent and received messages.
221 gpu::gles2::DisallowedFeatures disallowed_features_;
222 GpuWatchdog* watchdog_;
224 bool handle_messages_scheduled_;
225 bool processed_get_state_fast_;
226 IPC::Message* currently_processing_message_;
228 base::WeakPtrFactory<GpuChannel> weak_factory_;
230 scoped_refptr<GpuChannelMessageFilter> filter_;
231 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
232 scoped_ptr<DevToolsGpuAgent> devtools_gpu_agent_;
234 size_t num_stubs_descheduled_;
236 DISALLOW_COPY_AND_ASSIGN(GpuChannel);
239 } // namespace content
241 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_