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 "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
;
41 class SubscriptionRefSet
;
46 class AttachmentBroker
;
51 class GpuChannelManager
;
52 class GpuChannelMessageFilter
;
53 class GpuJpegDecodeAccelerator
;
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
,
60 public gpu::gles2::SubscriptionRefSet::Observer
,
61 public base::trace_event::MemoryDumpProvider
{
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
,
69 uint64_t client_tracing_id
,
71 bool allow_future_sync_points
);
72 ~GpuChannel() override
;
74 void Init(base::SingleThreadTaskRunner
* io_task_runner
,
75 base::WaitableEvent
* shutdown_event
,
76 IPC::AttachmentBroker
* broker
);
78 // Get the GpuChannelManager that owns this channel.
79 GpuChannelManager
* gpu_channel_manager() const {
80 return gpu_channel_manager_
;
83 // Returns the name of the associated IPC channel.
84 std::string
GetChannelName();
87 base::ScopedFD
TakeRendererFileDescriptor();
88 #endif // defined(OS_POSIX)
90 base::ProcessId
renderer_pid() const { return channel_
->GetPeerPID(); }
92 int client_id() const { return client_id_
; }
94 uint64_t client_tracing_id() const { return client_tracing_id_
; }
96 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner() const {
97 return io_task_runner_
;
100 // IPC::Listener implementation:
101 bool OnMessageReceived(const IPC::Message
& msg
) override
;
102 void OnChannelError() override
;
104 // IPC::Sender implementation:
105 bool Send(IPC::Message
* msg
) override
;
107 // Requeue the message that is currently being processed to the beginning of
108 // the queue. Used when the processing of a message gets aborted because of
109 // unscheduling conditions.
110 void RequeueMessage();
112 // SubscriptionRefSet::Observer implementation
113 void OnAddSubscription(unsigned int target
) override
;
114 void OnRemoveSubscription(unsigned int target
) override
;
116 // This is called when a command buffer transitions from the unscheduled
117 // state to the scheduled state, which potentially means the channel
118 // transitions from the unscheduled to the scheduled state. When this occurs
119 // deferred IPC messaged are handled.
122 // This is called when a command buffer transitions between scheduled and
123 // descheduled states. When any stub is descheduled, we stop preempting
125 void StubSchedulingChanged(bool scheduled
);
127 CreateCommandBufferResult
CreateViewCommandBuffer(
128 const gfx::GLSurfaceHandle
& window
,
130 const GPUCreateCommandBufferConfig
& init_params
,
133 gfx::GLShareGroup
* share_group() const { return share_group_
.get(); }
135 GpuCommandBufferStub
* LookupCommandBuffer(int32 route_id
);
137 void LoseAllContexts();
138 void MarkAllContextsLost();
140 // Called to add a listener for a particular message routing ID.
141 // Returns true if succeeded.
142 bool AddRoute(int32 route_id
, IPC::Listener
* listener
);
144 // Called to remove a listener for a particular message routing ID.
145 void RemoveRoute(int32 route_id
);
147 gpu::PreemptionFlag
* GetPreemptionFlag();
149 bool handle_messages_scheduled() const { return handle_messages_scheduled_
; }
150 uint64
messages_processed() const { return messages_processed_
; }
152 // If |preemption_flag->IsSet()|, any stub on this channel
153 // should stop issuing GL commands. Setting this to NULL stops deferral.
154 void SetPreemptByFlag(
155 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
157 void CacheShader(const std::string
& key
, const std::string
& shader
);
159 void AddFilter(IPC::MessageFilter
* filter
);
160 void RemoveFilter(IPC::MessageFilter
* filter
);
162 uint64
GetMemoryUsage();
164 scoped_refptr
<gfx::GLImage
> CreateImageForGpuMemoryBuffer(
165 const gfx::GpuMemoryBufferHandle
& handle
,
166 const gfx::Size
& size
,
167 gfx::BufferFormat format
,
168 uint32 internalformat
);
170 bool allow_future_sync_points() const { return allow_future_sync_points_
; }
172 void HandleUpdateValueState(unsigned int target
,
173 const gpu::ValueState
& state
);
175 // Visible for testing.
176 const gpu::ValueStateMap
* pending_valuebuffer_state() const {
177 return pending_valuebuffer_state_
.get();
180 // base::trace_event::MemoryDumpProvider implementation.
181 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs
& args
,
182 base::trace_event::ProcessMemoryDump
* pmd
) override
;
185 friend class GpuChannelMessageFilter
;
189 bool OnControlMessageReceived(const IPC::Message
& msg
);
191 void HandleMessage();
194 void OnCreateOffscreenCommandBuffer(
195 const gfx::Size
& size
,
196 const GPUCreateCommandBufferConfig
& init_params
,
199 void OnDestroyCommandBuffer(int32 route_id
);
200 void OnCreateJpegDecoder(int32 route_id
, IPC::Message
* reply_msg
);
202 // Decrement the count of unhandled IPC messages and defer preemption.
203 void MessageProcessed();
205 // The lifetime of objects of this class is managed by a GpuChannelManager.
206 // The GpuChannelManager destroy all the GpuChannels that they own when they
207 // are destroyed. So a raw pointer is safe.
208 GpuChannelManager
* gpu_channel_manager_
;
210 scoped_ptr
<IPC::SyncChannel
> channel_
;
212 uint64 messages_processed_
;
214 // Whether the processing of IPCs on this channel is stalled and we should
215 // preempt other GpuChannels.
216 scoped_refptr
<gpu::PreemptionFlag
> preempting_flag_
;
218 // If non-NULL, all stubs on this channel should stop processing GL
219 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
220 scoped_refptr
<gpu::PreemptionFlag
> preempted_flag_
;
222 std::deque
<IPC::Message
*> deferred_messages_
;
224 // The id of the client who is on the other side of the channel.
227 // The tracing ID used for memory allocations associated with this client.
228 uint64_t client_tracing_id_
;
230 // Uniquely identifies the channel within this GPU process.
231 std::string channel_id_
;
233 // Used to implement message routing functionality to CommandBuffer objects
234 MessageRouter router_
;
236 // The share group that all contexts associated with a particular renderer
238 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
240 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
242 scoped_refptr
<gpu::gles2::SubscriptionRefSet
> subscription_ref_set_
;
244 scoped_refptr
<gpu::ValueStateMap
> pending_valuebuffer_state_
;
246 typedef IDMap
<GpuCommandBufferStub
, IDMapOwnPointer
> StubMap
;
249 scoped_ptr
<GpuJpegDecodeAccelerator
> jpeg_decoder_
;
251 bool log_messages_
; // True if we should log sent and received messages.
252 gpu::gles2::DisallowedFeatures disallowed_features_
;
253 GpuWatchdog
* watchdog_
;
255 bool handle_messages_scheduled_
;
256 IPC::Message
* currently_processing_message_
;
258 scoped_refptr
<GpuChannelMessageFilter
> filter_
;
259 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
261 size_t num_stubs_descheduled_
;
263 bool allow_future_sync_points_
;
265 // Member variables should appear before the WeakPtrFactory, to ensure
266 // that any WeakPtrs to Controller are invalidated before its members
267 // variable's destructors are executed, rendering them invalid.
268 base::WeakPtrFactory
<GpuChannel
> weak_factory_
;
270 DISALLOW_COPY_AND_ASSIGN(GpuChannel
);
273 } // namespace content
275 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_