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 #if defined(OS_ANDROID)
28 #include "content/common/android/surface_texture_peer.h"
31 struct GPUCreateCommandBufferConfig
;
34 class MessageLoopProxy
;
45 #if defined(OS_ANDROID)
47 class StreamTextureManagerAndroid
;
52 class GpuChannelManager
;
53 class GpuChannelMessageFilter
;
54 struct GpuRenderingStats
;
55 class GpuVideoEncodeAccelerator
;
58 // Encapsulates an IPC channel between the GPU process and one renderer
59 // process. On the renderer side there's a corresponding GpuChannelHost.
60 class GpuChannel
: public IPC::Listener
,
62 public base::RefCountedThreadSafe
<GpuChannel
> {
64 // Takes ownership of the renderer process handle.
65 GpuChannel(GpuChannelManager
* gpu_channel_manager
,
66 GpuWatchdog
* watchdog
,
67 gfx::GLShareGroup
* share_group
,
68 gpu::gles2::MailboxManager
* mailbox_manager
,
72 bool Init(base::MessageLoopProxy
* io_message_loop
,
73 base::WaitableEvent
* shutdown_event
);
75 // Get the GpuChannelManager that owns this channel.
76 GpuChannelManager
* gpu_channel_manager() const {
77 return gpu_channel_manager_
;
80 // Returns the name of the associated IPC channel.
81 std::string
GetChannelName();
84 int TakeRendererFileDescriptor();
85 #endif // defined(OS_POSIX)
87 base::ProcessId
renderer_pid() const { return channel_
->peer_pid(); }
89 scoped_refptr
<base::MessageLoopProxy
> io_message_loop() const {
90 return io_message_loop_
;
93 // IPC::Listener implementation:
94 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
95 virtual void OnChannelError() OVERRIDE
;
97 // IPC::Sender implementation:
98 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
100 // Requeue the message that is currently being processed to the beginning of
101 // the queue. Used when the processing of a message gets aborted because of
102 // unscheduling conditions.
103 void RequeueMessage();
105 // This is called when a command buffer transitions from the unscheduled
106 // state to the scheduled state, which potentially means the channel
107 // transitions from the unscheduled to the scheduled state. When this occurs
108 // deferred IPC messaged are handled.
111 // This is called when a command buffer transitions between scheduled and
112 // descheduled states. When any stub is descheduled, we stop preempting
114 void StubSchedulingChanged(bool scheduled
);
116 void CreateViewCommandBuffer(
117 const gfx::GLSurfaceHandle
& window
,
119 const GPUCreateCommandBufferConfig
& init_params
,
123 gfx::PluginWindowHandle window
,
126 void DeleteImage(int32 image_id
);
128 gfx::GLShareGroup
* share_group() const { return share_group_
.get(); }
130 GpuCommandBufferStub
* LookupCommandBuffer(int32 route_id
);
132 void LoseAllContexts();
133 void MarkAllContextsLost();
135 // Destroy channel and all contained contexts.
138 // Generate a route ID guaranteed to be unique for this channel.
139 int GenerateRouteID();
141 // Called to add/remove a listener for a particular message routing ID.
142 void AddRoute(int32 route_id
, IPC::Listener
* listener
);
143 void RemoveRoute(int32 route_id
);
145 gpu::PreemptionFlag
* GetPreemptionFlag();
147 bool handle_messages_scheduled() const { return handle_messages_scheduled_
; }
148 uint64
messages_processed() const { return messages_processed_
; }
150 // If |preemption_flag->IsSet()|, any stub on this channel
151 // should stop issuing GL commands. Setting this to NULL stops deferral.
152 void SetPreemptByFlag(
153 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
155 #if defined(OS_ANDROID)
156 StreamTextureManagerAndroid
* stream_texture_manager() {
157 return stream_texture_manager_
.get();
161 void CacheShader(const std::string
& key
, const std::string
& shader
);
163 void AddFilter(IPC::ChannelProxy::MessageFilter
* filter
);
164 void RemoveFilter(IPC::ChannelProxy::MessageFilter
* filter
);
167 virtual ~GpuChannel();
170 friend class base::RefCountedThreadSafe
<GpuChannel
>;
171 friend class GpuChannelMessageFilter
;
175 bool OnControlMessageReceived(const IPC::Message
& msg
);
177 void HandleMessage();
180 void OnCreateOffscreenCommandBuffer(
181 const gfx::Size
& size
,
182 const GPUCreateCommandBufferConfig
& init_params
,
184 void OnDestroyCommandBuffer(int32 route_id
);
185 void OnCreateVideoEncoder(int32
* route_id
);
186 void OnDestroyVideoEncoder(int32 route_id
);
188 #if defined(OS_ANDROID)
189 // Register the StreamTextureProxy class with the gpu process so that all
190 // the callbacks will be correctly forwarded to the renderer.
191 void OnRegisterStreamTextureProxy(int32 stream_id
, int32
* route_id
);
193 // Create a java surface texture object and send it to the renderer process
194 // through binder thread.
195 void OnEstablishStreamTexture(
196 int32 stream_id
, int32 primary_id
, int32 secondary_id
);
198 // Set the size of StreamTexture.
199 void OnSetStreamTextureSize(int32 stream_id
, const gfx::Size
& size
);
202 // Collect rendering stats.
203 void OnCollectRenderingStatsForSurface(
204 int32 surface_id
, GpuRenderingStats
* stats
);
206 // Decrement the count of unhandled IPC messages and defer preemption.
207 void MessageProcessed();
209 // The lifetime of objects of this class is managed by a GpuChannelManager.
210 // The GpuChannelManager destroy all the GpuChannels that they own when they
211 // are destroyed. So a raw pointer is safe.
212 GpuChannelManager
* gpu_channel_manager_
;
214 scoped_ptr
<IPC::SyncChannel
> channel_
;
216 uint64 messages_processed_
;
218 // Whether the processing of IPCs on this channel is stalled and we should
219 // preempt other GpuChannels.
220 scoped_refptr
<gpu::PreemptionFlag
> preempting_flag_
;
222 // If non-NULL, all stubs on this channel should stop processing GL
223 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
224 scoped_refptr
<gpu::PreemptionFlag
> preempted_flag_
;
226 std::deque
<IPC::Message
*> deferred_messages_
;
228 // The id of the client who is on the other side of the channel.
231 // Uniquely identifies the channel within this GPU process.
232 std::string channel_id_
;
234 // Used to implement message routing functionality to CommandBuffer objects
235 MessageRouter router_
;
237 // The share group that all contexts associated with a particular renderer
239 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
241 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
242 scoped_refptr
<gpu::gles2::ImageManager
> image_manager_
;
243 #if defined(OS_ANDROID)
244 scoped_ptr
<StreamTextureManagerAndroid
> stream_texture_manager_
;
247 #if defined(ENABLE_GPU)
248 typedef IDMap
<GpuCommandBufferStub
, IDMapOwnPointer
> StubMap
;
250 #endif // defined (ENABLE_GPU)
252 typedef IDMap
<GpuVideoEncodeAccelerator
, IDMapOwnPointer
> EncoderMap
;
253 EncoderMap video_encoders_
;
255 bool log_messages_
; // True if we should log sent and received messages.
256 gpu::gles2::DisallowedFeatures disallowed_features_
;
257 GpuWatchdog
* watchdog_
;
259 bool handle_messages_scheduled_
;
260 bool processed_get_state_fast_
;
261 IPC::Message
* currently_processing_message_
;
263 base::WeakPtrFactory
<GpuChannel
> weak_factory_
;
265 scoped_refptr
<GpuChannelMessageFilter
> filter_
;
266 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
268 size_t num_stubs_descheduled_
;
270 DISALLOW_COPY_AND_ASSIGN(GpuChannel
);
273 } // namespace content
275 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_