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;
46 class DevToolsGpuAgent;
47 class GpuChannelManager;
48 class GpuChannelMessageFilter;
51 // Encapsulates an IPC channel between the GPU process and one renderer
52 // process. On the renderer side there's a corresponding GpuChannelHost.
53 class GpuChannel : public IPC::Listener,
55 public base::RefCountedThreadSafe<GpuChannel> {
57 // Takes ownership of the renderer process handle.
58 GpuChannel(GpuChannelManager* gpu_channel_manager,
59 GpuWatchdog* watchdog,
60 gfx::GLShareGroup* share_group,
61 gpu::gles2::MailboxManager* mailbox_manager,
65 void Init(base::MessageLoopProxy* io_message_loop,
66 base::WaitableEvent* shutdown_event);
68 // Get the GpuChannelManager that owns this channel.
69 GpuChannelManager* gpu_channel_manager() const {
70 return gpu_channel_manager_;
73 // Returns the name of the associated IPC channel.
74 std::string GetChannelName();
77 int TakeRendererFileDescriptor();
78 #endif // defined(OS_POSIX)
80 base::ProcessId renderer_pid() const { return channel_->peer_pid(); }
82 int client_id() const { return client_id_; }
84 scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
85 return io_message_loop_;
88 // IPC::Listener implementation:
89 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
90 virtual void OnChannelError() OVERRIDE;
92 // IPC::Sender implementation:
93 virtual bool Send(IPC::Message* msg) OVERRIDE;
95 // Requeue the message that is currently being processed to the beginning of
96 // the queue. Used when the processing of a message gets aborted because of
97 // unscheduling conditions.
98 void RequeueMessage();
100 // This is called when a command buffer transitions from the unscheduled
101 // state to the scheduled state, which potentially means the channel
102 // transitions from the unscheduled to the scheduled state. When this occurs
103 // deferred IPC messaged are handled.
106 // This is called when a command buffer transitions between scheduled and
107 // descheduled states. When any stub is descheduled, we stop preempting
109 void StubSchedulingChanged(bool scheduled);
111 bool CreateViewCommandBuffer(
112 const gfx::GLSurfaceHandle& window,
114 const GPUCreateCommandBufferConfig& init_params,
118 gfx::PluginWindowHandle window,
121 void DeleteImage(int32 image_id);
123 gfx::GLShareGroup* share_group() const { return share_group_.get(); }
125 GpuCommandBufferStub* LookupCommandBuffer(int32 route_id);
127 void LoseAllContexts();
128 void MarkAllContextsLost();
130 // Destroy channel and all contained contexts.
133 // Called to add a listener for a particular message routing ID.
134 // Returns true if succeeded.
135 bool AddRoute(int32 route_id, IPC::Listener* listener);
137 // Called to remove a listener for a particular message routing ID.
138 void RemoveRoute(int32 route_id);
140 gpu::PreemptionFlag* GetPreemptionFlag();
142 bool handle_messages_scheduled() const { return handle_messages_scheduled_; }
143 uint64 messages_processed() const { return messages_processed_; }
145 // If |preemption_flag->IsSet()|, any stub on this channel
146 // should stop issuing GL commands. Setting this to NULL stops deferral.
147 void SetPreemptByFlag(
148 scoped_refptr<gpu::PreemptionFlag> preemption_flag);
150 void CacheShader(const std::string& key, const std::string& shader);
152 void AddFilter(IPC::MessageFilter* filter);
153 void RemoveFilter(IPC::MessageFilter* filter);
155 uint64 GetMemoryUsage();
158 virtual ~GpuChannel();
161 friend class base::RefCountedThreadSafe<GpuChannel>;
162 friend class GpuChannelMessageFilter;
166 bool OnControlMessageReceived(const IPC::Message& msg);
168 void HandleMessage();
171 void OnCreateOffscreenCommandBuffer(
172 const gfx::Size& size,
173 const GPUCreateCommandBufferConfig& init_params,
176 void OnDestroyCommandBuffer(int32 route_id);
177 void OnDevToolsStartEventsRecording(int32 route_id, bool* succeeded);
178 void OnDevToolsStopEventsRecording();
180 // Decrement the count of unhandled IPC messages and defer preemption.
181 void MessageProcessed();
183 // The lifetime of objects of this class is managed by a GpuChannelManager.
184 // The GpuChannelManager destroy all the GpuChannels that they own when they
185 // are destroyed. So a raw pointer is safe.
186 GpuChannelManager* gpu_channel_manager_;
188 scoped_ptr<IPC::SyncChannel> channel_;
190 uint64 messages_processed_;
192 // Whether the processing of IPCs on this channel is stalled and we should
193 // preempt other GpuChannels.
194 scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
196 // If non-NULL, all stubs on this channel should stop processing GL
197 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
198 scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
200 std::deque<IPC::Message*> deferred_messages_;
202 // The id of the client who is on the other side of the channel.
205 // Uniquely identifies the channel within this GPU process.
206 std::string channel_id_;
208 // Used to implement message routing functionality to CommandBuffer objects
209 MessageRouter router_;
211 // The share group that all contexts associated with a particular renderer
213 scoped_refptr<gfx::GLShareGroup> share_group_;
215 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
216 scoped_refptr<gpu::gles2::ImageManager> image_manager_;
218 typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap;
221 bool log_messages_; // True if we should log sent and received messages.
222 gpu::gles2::DisallowedFeatures disallowed_features_;
223 GpuWatchdog* watchdog_;
225 bool handle_messages_scheduled_;
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_