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/gpu/gpu_result_codes.h"
21 #include "content/common/message_router.h"
22 #include "gpu/command_buffer/service/valuebuffer_manager.h"
23 #include "ipc/ipc_sync_channel.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gfx/native_widget_types.h"
26 #include "ui/gl/gl_share_group.h"
27 #include "ui/gl/gpu_preference.h"
29 struct GPUCreateCommandBufferConfig
;
40 class SubscriptionRefSet
;
49 class GpuChannelManager
;
50 class GpuChannelMessageFilter
;
53 // Encapsulates an IPC channel between the GPU process and one renderer
54 // process. On the renderer side there's a corresponding GpuChannelHost.
55 class GpuChannel
: public IPC::Listener
, public IPC::Sender
,
56 public gpu::gles2::SubscriptionRefSet::Observer
{
58 // Takes ownership of the renderer process handle.
59 GpuChannel(GpuChannelManager
* gpu_channel_manager
,
60 GpuWatchdog
* watchdog
,
61 gfx::GLShareGroup
* share_group
,
62 gpu::gles2::MailboxManager
* mailbox_manager
,
65 bool allow_future_sync_points
);
66 ~GpuChannel() override
;
68 void Init(base::SingleThreadTaskRunner
* io_task_runner
,
69 base::WaitableEvent
* shutdown_event
);
71 // Get the GpuChannelManager that owns this channel.
72 GpuChannelManager
* gpu_channel_manager() const {
73 return gpu_channel_manager_
;
76 // Returns the name of the associated IPC channel.
77 std::string
GetChannelName();
80 base::ScopedFD
TakeRendererFileDescriptor();
81 #endif // defined(OS_POSIX)
83 base::ProcessId
renderer_pid() const { return channel_
->GetPeerPID(); }
85 int client_id() const { return client_id_
; }
87 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner() const {
88 return io_task_runner_
;
91 // IPC::Listener implementation:
92 bool OnMessageReceived(const IPC::Message
& msg
) override
;
93 void OnChannelError() override
;
95 // IPC::Sender implementation:
96 bool Send(IPC::Message
* msg
) override
;
98 // Requeue the message that is currently being processed to the beginning of
99 // the queue. Used when the processing of a message gets aborted because of
100 // unscheduling conditions.
101 void RequeueMessage();
103 // SubscriptionRefSet::Observer implementation
104 void OnAddSubscription(unsigned int target
) override
;
105 void OnRemoveSubscription(unsigned int target
) override
;
107 // This is called when a command buffer transitions from the unscheduled
108 // state to the scheduled state, which potentially means the channel
109 // transitions from the unscheduled to the scheduled state. When this occurs
110 // deferred IPC messaged are handled.
113 // This is called when a command buffer transitions between scheduled and
114 // descheduled states. When any stub is descheduled, we stop preempting
116 void StubSchedulingChanged(bool scheduled
);
118 CreateCommandBufferResult
CreateViewCommandBuffer(
119 const gfx::GLSurfaceHandle
& window
,
121 const GPUCreateCommandBufferConfig
& init_params
,
124 gfx::GLShareGroup
* share_group() const { return share_group_
.get(); }
126 GpuCommandBufferStub
* LookupCommandBuffer(int32 route_id
);
128 void LoseAllContexts();
129 void MarkAllContextsLost();
131 // Called to add a listener for a particular message routing ID.
132 // Returns true if succeeded.
133 bool AddRoute(int32 route_id
, IPC::Listener
* listener
);
135 // Called to remove a listener for a particular message routing ID.
136 void RemoveRoute(int32 route_id
);
138 gpu::PreemptionFlag
* GetPreemptionFlag();
140 bool handle_messages_scheduled() const { return handle_messages_scheduled_
; }
141 uint64
messages_processed() const { return messages_processed_
; }
143 // If |preemption_flag->IsSet()|, any stub on this channel
144 // should stop issuing GL commands. Setting this to NULL stops deferral.
145 void SetPreemptByFlag(
146 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
148 void CacheShader(const std::string
& key
, const std::string
& shader
);
150 void AddFilter(IPC::MessageFilter
* filter
);
151 void RemoveFilter(IPC::MessageFilter
* filter
);
153 uint64
GetMemoryUsage();
155 scoped_refptr
<gfx::GLImage
> CreateImageForGpuMemoryBuffer(
156 const gfx::GpuMemoryBufferHandle
& handle
,
157 const gfx::Size
& size
,
158 gfx::GpuMemoryBuffer::Format format
,
159 uint32 internalformat
);
161 bool allow_future_sync_points() const { return allow_future_sync_points_
; }
163 void HandleUpdateValueState(unsigned int target
,
164 const gpu::ValueState
& state
);
166 // Visible for testing.
167 const gpu::ValueStateMap
* pending_valuebuffer_state() const {
168 return pending_valuebuffer_state_
.get();
172 friend class GpuChannelMessageFilter
;
176 bool OnControlMessageReceived(const IPC::Message
& msg
);
178 void HandleMessage();
181 void OnCreateOffscreenCommandBuffer(
182 const gfx::Size
& size
,
183 const GPUCreateCommandBufferConfig
& init_params
,
186 void OnDestroyCommandBuffer(int32 route_id
);
188 // Decrement the count of unhandled IPC messages and defer preemption.
189 void MessageProcessed();
191 // The lifetime of objects of this class is managed by a GpuChannelManager.
192 // The GpuChannelManager destroy all the GpuChannels that they own when they
193 // are destroyed. So a raw pointer is safe.
194 GpuChannelManager
* gpu_channel_manager_
;
196 scoped_ptr
<IPC::SyncChannel
> channel_
;
198 uint64 messages_processed_
;
200 // Whether the processing of IPCs on this channel is stalled and we should
201 // preempt other GpuChannels.
202 scoped_refptr
<gpu::PreemptionFlag
> preempting_flag_
;
204 // If non-NULL, all stubs on this channel should stop processing GL
205 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
206 scoped_refptr
<gpu::PreemptionFlag
> preempted_flag_
;
208 std::deque
<IPC::Message
*> deferred_messages_
;
210 // The id of the client who is on the other side of the channel.
213 // Uniquely identifies the channel within this GPU process.
214 std::string channel_id_
;
216 // Used to implement message routing functionality to CommandBuffer objects
217 MessageRouter router_
;
219 // The share group that all contexts associated with a particular renderer
221 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
223 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
225 scoped_refptr
<gpu::gles2::SubscriptionRefSet
> subscription_ref_set_
;
227 scoped_refptr
<gpu::ValueStateMap
> pending_valuebuffer_state_
;
229 typedef IDMap
<GpuCommandBufferStub
, IDMapOwnPointer
> StubMap
;
232 bool log_messages_
; // True if we should log sent and received messages.
233 gpu::gles2::DisallowedFeatures disallowed_features_
;
234 GpuWatchdog
* watchdog_
;
236 bool handle_messages_scheduled_
;
237 IPC::Message
* currently_processing_message_
;
239 scoped_refptr
<GpuChannelMessageFilter
> filter_
;
240 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
242 size_t num_stubs_descheduled_
;
244 bool allow_future_sync_points_
;
246 // Member variables should appear before the WeakPtrFactory, to ensure
247 // that any WeakPtrs to Controller are invalidated before its members
248 // variable's destructors are executed, rendering them invalid.
249 base::WeakPtrFactory
<GpuChannel
> weak_factory_
;
251 DISALLOW_COPY_AND_ASSIGN(GpuChannel
);
254 } // namespace content
256 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_