Add P2PDatagramSocket and P2PStreamSocket interfaces.
[chromium-blink-merge.git] / content / common / gpu / gpu_channel.h
blob6920df596549c0f8cdc1ae262432a115a20df5f8
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_
8 #include <deque>
9 #include <string>
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 "base/trace_event/memory_dump_provider.h"
18 #include "build/build_config.h"
19 #include "content/common/gpu/gpu_command_buffer_stub.h"
20 #include "content/common/gpu/gpu_memory_manager.h"
21 #include "content/common/gpu/gpu_result_codes.h"
22 #include "content/common/message_router.h"
23 #include "gpu/command_buffer/service/valuebuffer_manager.h"
24 #include "ipc/ipc_sync_channel.h"
25 #include "ui/gfx/geometry/size.h"
26 #include "ui/gfx/native_widget_types.h"
27 #include "ui/gl/gl_share_group.h"
28 #include "ui/gl/gpu_preference.h"
30 struct GPUCreateCommandBufferConfig;
32 namespace base {
33 class WaitableEvent;
36 namespace gpu {
37 class PreemptionFlag;
38 union ValueState;
39 class ValueStateMap;
40 namespace gles2 {
41 class SubscriptionRefSet;
45 namespace IPC {
46 class AttachmentBroker;
47 class MessageFilter;
50 namespace content {
51 class GpuChannelManager;
52 class GpuChannelMessageFilter;
53 class GpuJpegDecodeAccelerator;
54 class GpuWatchdog;
56 // Encapsulates an IPC channel between the GPU process and one renderer
57 // process. On the renderer side there's a corresponding GpuChannelHost.
58 class GpuChannel : public IPC::Listener,
59 public IPC::Sender,
60 public gpu::gles2::SubscriptionRefSet::Observer,
61 public base::trace_event::MemoryDumpProvider {
62 public:
63 // Takes ownership of the renderer process handle.
64 GpuChannel(GpuChannelManager* gpu_channel_manager,
65 GpuWatchdog* watchdog,
66 gfx::GLShareGroup* share_group,
67 gpu::gles2::MailboxManager* mailbox_manager,
68 int client_id,
69 bool software,
70 bool allow_future_sync_points);
71 ~GpuChannel() override;
73 void Init(base::SingleThreadTaskRunner* io_task_runner,
74 base::WaitableEvent* shutdown_event,
75 IPC::AttachmentBroker* broker);
77 // Get the GpuChannelManager that owns this channel.
78 GpuChannelManager* gpu_channel_manager() const {
79 return gpu_channel_manager_;
82 // Returns the name of the associated IPC channel.
83 std::string GetChannelName();
85 #if defined(OS_POSIX)
86 base::ScopedFD TakeRendererFileDescriptor();
87 #endif // defined(OS_POSIX)
89 base::ProcessId renderer_pid() const { return channel_->GetPeerPID(); }
91 int client_id() const { return client_id_; }
93 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() const {
94 return io_task_runner_;
97 // IPC::Listener implementation:
98 bool OnMessageReceived(const IPC::Message& msg) override;
99 void OnChannelError() override;
101 // IPC::Sender implementation:
102 bool Send(IPC::Message* msg) override;
104 // Requeue the message that is currently being processed to the beginning of
105 // the queue. Used when the processing of a message gets aborted because of
106 // unscheduling conditions.
107 void RequeueMessage();
109 // SubscriptionRefSet::Observer implementation
110 void OnAddSubscription(unsigned int target) override;
111 void OnRemoveSubscription(unsigned int target) override;
113 // This is called when a command buffer transitions from the unscheduled
114 // state to the scheduled state, which potentially means the channel
115 // transitions from the unscheduled to the scheduled state. When this occurs
116 // deferred IPC messaged are handled.
117 void OnScheduled();
119 // This is called when a command buffer transitions between scheduled and
120 // descheduled states. When any stub is descheduled, we stop preempting
121 // other channels.
122 void StubSchedulingChanged(bool scheduled);
124 CreateCommandBufferResult CreateViewCommandBuffer(
125 const gfx::GLSurfaceHandle& window,
126 int32 surface_id,
127 const GPUCreateCommandBufferConfig& init_params,
128 int32 route_id);
130 gfx::GLShareGroup* share_group() const { return share_group_.get(); }
132 GpuCommandBufferStub* LookupCommandBuffer(int32 route_id);
134 void LoseAllContexts();
135 void MarkAllContextsLost();
137 // Called to add a listener for a particular message routing ID.
138 // Returns true if succeeded.
139 bool AddRoute(int32 route_id, IPC::Listener* listener);
141 // Called to remove a listener for a particular message routing ID.
142 void RemoveRoute(int32 route_id);
144 gpu::PreemptionFlag* GetPreemptionFlag();
146 bool handle_messages_scheduled() const { return handle_messages_scheduled_; }
147 uint64 messages_processed() const { return messages_processed_; }
149 // If |preemption_flag->IsSet()|, any stub on this channel
150 // should stop issuing GL commands. Setting this to NULL stops deferral.
151 void SetPreemptByFlag(
152 scoped_refptr<gpu::PreemptionFlag> preemption_flag);
154 void CacheShader(const std::string& key, const std::string& shader);
156 void AddFilter(IPC::MessageFilter* filter);
157 void RemoveFilter(IPC::MessageFilter* filter);
159 uint64 GetMemoryUsage();
161 scoped_refptr<gfx::GLImage> CreateImageForGpuMemoryBuffer(
162 const gfx::GpuMemoryBufferHandle& handle,
163 const gfx::Size& size,
164 gfx::GpuMemoryBuffer::Format format,
165 uint32 internalformat);
167 bool allow_future_sync_points() const { return allow_future_sync_points_; }
169 void HandleUpdateValueState(unsigned int target,
170 const gpu::ValueState& state);
172 // Visible for testing.
173 const gpu::ValueStateMap* pending_valuebuffer_state() const {
174 return pending_valuebuffer_state_.get();
177 // base::trace_event::MemoryDumpProvider implementation.
178 bool OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd) override;
180 private:
181 friend class GpuChannelMessageFilter;
183 void OnDestroy();
185 bool OnControlMessageReceived(const IPC::Message& msg);
187 void HandleMessage();
189 // Message handlers.
190 void OnCreateOffscreenCommandBuffer(
191 const gfx::Size& size,
192 const GPUCreateCommandBufferConfig& init_params,
193 int32 route_id,
194 bool* succeeded);
195 void OnDestroyCommandBuffer(int32 route_id);
196 void OnCreateJpegDecoder(int32 route_id, IPC::Message* reply_msg);
198 // Decrement the count of unhandled IPC messages and defer preemption.
199 void MessageProcessed();
201 // The lifetime of objects of this class is managed by a GpuChannelManager.
202 // The GpuChannelManager destroy all the GpuChannels that they own when they
203 // are destroyed. So a raw pointer is safe.
204 GpuChannelManager* gpu_channel_manager_;
206 scoped_ptr<IPC::SyncChannel> channel_;
208 uint64 messages_processed_;
210 // Whether the processing of IPCs on this channel is stalled and we should
211 // preempt other GpuChannels.
212 scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
214 // If non-NULL, all stubs on this channel should stop processing GL
215 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
216 scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
218 std::deque<IPC::Message*> deferred_messages_;
220 // The id of the client who is on the other side of the channel.
221 int client_id_;
223 // Uniquely identifies the channel within this GPU process.
224 std::string channel_id_;
226 // Used to implement message routing functionality to CommandBuffer objects
227 MessageRouter router_;
229 // The share group that all contexts associated with a particular renderer
230 // process use.
231 scoped_refptr<gfx::GLShareGroup> share_group_;
233 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
235 scoped_refptr<gpu::gles2::SubscriptionRefSet> subscription_ref_set_;
237 scoped_refptr<gpu::ValueStateMap> pending_valuebuffer_state_;
239 typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap;
240 StubMap stubs_;
242 scoped_ptr<GpuJpegDecodeAccelerator> jpeg_decoder_;
244 bool log_messages_; // True if we should log sent and received messages.
245 gpu::gles2::DisallowedFeatures disallowed_features_;
246 GpuWatchdog* watchdog_;
247 bool software_;
248 bool handle_messages_scheduled_;
249 IPC::Message* currently_processing_message_;
251 scoped_refptr<GpuChannelMessageFilter> filter_;
252 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
254 size_t num_stubs_descheduled_;
256 bool allow_future_sync_points_;
258 // Member variables should appear before the WeakPtrFactory, to ensure
259 // that any WeakPtrs to Controller are invalidated before its members
260 // variable's destructors are executed, rendering them invalid.
261 base::WeakPtrFactory<GpuChannel> weak_factory_;
263 DISALLOW_COPY_AND_ASSIGN(GpuChannel);
266 } // namespace content
268 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_