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
;
32 class MessageLoopProxy
;
41 class SubscriptionRefSet
;
50 class GpuChannelManager
;
51 class GpuChannelMessageFilter
;
54 // Encapsulates an IPC channel between the GPU process and one renderer
55 // process. On the renderer side there's a corresponding GpuChannelHost.
56 class GpuChannel
: public IPC::Listener
, public IPC::Sender
,
57 public gpu::gles2::SubscriptionRefSet::Observer
{
59 // Takes ownership of the renderer process handle.
60 GpuChannel(GpuChannelManager
* gpu_channel_manager
,
61 GpuWatchdog
* watchdog
,
62 gfx::GLShareGroup
* share_group
,
63 gpu::gles2::MailboxManager
* mailbox_manager
,
66 bool allow_future_sync_points
);
67 ~GpuChannel() override
;
69 void Init(base::MessageLoopProxy
* io_message_loop
,
70 base::WaitableEvent
* shutdown_event
);
72 // Get the GpuChannelManager that owns this channel.
73 GpuChannelManager
* gpu_channel_manager() const {
74 return gpu_channel_manager_
;
77 // Returns the name of the associated IPC channel.
78 std::string
GetChannelName();
81 base::ScopedFD
TakeRendererFileDescriptor();
82 #endif // defined(OS_POSIX)
84 base::ProcessId
renderer_pid() const { return channel_
->GetPeerPID(); }
86 int client_id() const { return client_id_
; }
88 scoped_refptr
<base::MessageLoopProxy
> io_message_loop() const {
89 return io_message_loop_
;
92 // IPC::Listener implementation:
93 bool OnMessageReceived(const IPC::Message
& msg
) override
;
94 void OnChannelError() override
;
96 // IPC::Sender implementation:
97 bool Send(IPC::Message
* msg
) override
;
99 // Requeue the message that is currently being processed to the beginning of
100 // the queue. Used when the processing of a message gets aborted because of
101 // unscheduling conditions.
102 void RequeueMessage();
104 // SubscriptionRefSet::Observer implementation
105 void OnAddSubscription(unsigned int target
) override
;
106 void OnRemoveSubscription(unsigned int target
) override
;
108 // This is called when a command buffer transitions from the unscheduled
109 // state to the scheduled state, which potentially means the channel
110 // transitions from the unscheduled to the scheduled state. When this occurs
111 // deferred IPC messaged are handled.
114 // This is called when a command buffer transitions between scheduled and
115 // descheduled states. When any stub is descheduled, we stop preempting
117 void StubSchedulingChanged(bool scheduled
);
119 CreateCommandBufferResult
CreateViewCommandBuffer(
120 const gfx::GLSurfaceHandle
& window
,
122 const GPUCreateCommandBufferConfig
& init_params
,
125 gfx::GLShareGroup
* share_group() const { return share_group_
.get(); }
127 GpuCommandBufferStub
* LookupCommandBuffer(int32 route_id
);
129 void LoseAllContexts();
130 void MarkAllContextsLost();
132 // Called to add a listener for a particular message routing ID.
133 // Returns true if succeeded.
134 bool AddRoute(int32 route_id
, IPC::Listener
* listener
);
136 // Called to remove a listener for a particular message routing ID.
137 void RemoveRoute(int32 route_id
);
139 gpu::PreemptionFlag
* GetPreemptionFlag();
141 bool handle_messages_scheduled() const { return handle_messages_scheduled_
; }
142 uint64
messages_processed() const { return messages_processed_
; }
144 // If |preemption_flag->IsSet()|, any stub on this channel
145 // should stop issuing GL commands. Setting this to NULL stops deferral.
146 void SetPreemptByFlag(
147 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
149 void CacheShader(const std::string
& key
, const std::string
& shader
);
151 void AddFilter(IPC::MessageFilter
* filter
);
152 void RemoveFilter(IPC::MessageFilter
* filter
);
154 uint64
GetMemoryUsage();
156 scoped_refptr
<gfx::GLImage
> CreateImageForGpuMemoryBuffer(
157 const gfx::GpuMemoryBufferHandle
& handle
,
158 const gfx::Size
& size
,
159 gfx::GpuMemoryBuffer::Format format
,
160 uint32 internalformat
);
162 bool allow_future_sync_points() const { return allow_future_sync_points_
; }
164 void HandleUpdateValueState(unsigned int target
,
165 const gpu::ValueState
& state
);
167 // Visible for testing.
168 const gpu::ValueStateMap
* pending_valuebuffer_state() const {
169 return pending_valuebuffer_state_
.get();
173 friend class GpuChannelMessageFilter
;
177 bool OnControlMessageReceived(const IPC::Message
& msg
);
179 void HandleMessage();
182 void OnCreateOffscreenCommandBuffer(
183 const gfx::Size
& size
,
184 const GPUCreateCommandBufferConfig
& init_params
,
187 void OnDestroyCommandBuffer(int32 route_id
);
189 // Decrement the count of unhandled IPC messages and defer preemption.
190 void MessageProcessed();
192 // The lifetime of objects of this class is managed by a GpuChannelManager.
193 // The GpuChannelManager destroy all the GpuChannels that they own when they
194 // are destroyed. So a raw pointer is safe.
195 GpuChannelManager
* gpu_channel_manager_
;
197 scoped_ptr
<IPC::SyncChannel
> channel_
;
199 uint64 messages_processed_
;
201 // Whether the processing of IPCs on this channel is stalled and we should
202 // preempt other GpuChannels.
203 scoped_refptr
<gpu::PreemptionFlag
> preempting_flag_
;
205 // If non-NULL, all stubs on this channel should stop processing GL
206 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
207 scoped_refptr
<gpu::PreemptionFlag
> preempted_flag_
;
209 std::deque
<IPC::Message
*> deferred_messages_
;
211 // The id of the client who is on the other side of the channel.
214 // Uniquely identifies the channel within this GPU process.
215 std::string channel_id_
;
217 // Used to implement message routing functionality to CommandBuffer objects
218 MessageRouter router_
;
220 // The share group that all contexts associated with a particular renderer
222 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
224 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
226 scoped_refptr
<gpu::gles2::SubscriptionRefSet
> subscription_ref_set_
;
228 scoped_refptr
<gpu::ValueStateMap
> pending_valuebuffer_state_
;
230 typedef IDMap
<GpuCommandBufferStub
, IDMapOwnPointer
> StubMap
;
233 bool log_messages_
; // True if we should log sent and received messages.
234 gpu::gles2::DisallowedFeatures disallowed_features_
;
235 GpuWatchdog
* watchdog_
;
237 bool handle_messages_scheduled_
;
238 IPC::Message
* currently_processing_message_
;
240 scoped_refptr
<GpuChannelMessageFilter
> filter_
;
241 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
243 size_t num_stubs_descheduled_
;
245 bool allow_future_sync_points_
;
247 // Member variables should appear before the WeakPtrFactory, to ensure
248 // that any WeakPtrs to Controller are invalidated before its members
249 // variable's destructors are executed, rendering them invalid.
250 base::WeakPtrFactory
<GpuChannel
> weak_factory_
;
252 DISALLOW_COPY_AND_ASSIGN(GpuChannel
);
255 } // namespace content
257 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_