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 "ipc/ipc_sync_channel.h"
23 #include "ui/gfx/native_widget_types.h"
24 #include "ui/gfx/size.h"
25 #include "ui/gl/gl_share_group.h"
26 #include "ui/gl/gpu_preference.h"
28 struct GPUCreateCommandBufferConfig
;
31 class MessageLoopProxy
;
44 class DevToolsGpuAgent
;
45 class GpuChannelManager
;
46 class GpuChannelMessageFilter
;
49 // Encapsulates an IPC channel between the GPU process and one renderer
50 // process. On the renderer side there's a corresponding GpuChannelHost.
51 class GpuChannel
: public IPC::Listener
, public IPC::Sender
{
53 // Takes ownership of the renderer process handle.
54 GpuChannel(GpuChannelManager
* gpu_channel_manager
,
55 GpuWatchdog
* watchdog
,
56 gfx::GLShareGroup
* share_group
,
57 gpu::gles2::MailboxManager
* mailbox_manager
,
60 bool allow_future_sync_points
);
61 ~GpuChannel() override
;
63 void Init(base::MessageLoopProxy
* io_message_loop
,
64 base::WaitableEvent
* shutdown_event
);
66 // Get the GpuChannelManager that owns this channel.
67 GpuChannelManager
* gpu_channel_manager() const {
68 return gpu_channel_manager_
;
71 // Returns the name of the associated IPC channel.
72 std::string
GetChannelName();
75 base::ScopedFD
TakeRendererFileDescriptor();
76 #endif // defined(OS_POSIX)
78 base::ProcessId
renderer_pid() const { return channel_
->GetPeerPID(); }
80 int client_id() const { return client_id_
; }
82 scoped_refptr
<base::MessageLoopProxy
> io_message_loop() const {
83 return io_message_loop_
;
86 // IPC::Listener implementation:
87 bool OnMessageReceived(const IPC::Message
& msg
) override
;
88 void OnChannelError() override
;
90 // IPC::Sender implementation:
91 bool Send(IPC::Message
* msg
) override
;
93 // Requeue the message that is currently being processed to the beginning of
94 // the queue. Used when the processing of a message gets aborted because of
95 // unscheduling conditions.
96 void RequeueMessage();
98 // This is called when a command buffer transitions from the unscheduled
99 // state to the scheduled state, which potentially means the channel
100 // transitions from the unscheduled to the scheduled state. When this occurs
101 // deferred IPC messaged are handled.
104 // This is called when a command buffer transitions between scheduled and
105 // descheduled states. When any stub is descheduled, we stop preempting
107 void StubSchedulingChanged(bool scheduled
);
109 CreateCommandBufferResult
CreateViewCommandBuffer(
110 const gfx::GLSurfaceHandle
& window
,
112 const GPUCreateCommandBufferConfig
& init_params
,
115 gfx::GLShareGroup
* share_group() const { return share_group_
.get(); }
117 GpuCommandBufferStub
* LookupCommandBuffer(int32 route_id
);
119 void LoseAllContexts();
120 void MarkAllContextsLost();
122 // Called to add a listener for a particular message routing ID.
123 // Returns true if succeeded.
124 bool AddRoute(int32 route_id
, IPC::Listener
* listener
);
126 // Called to remove a listener for a particular message routing ID.
127 void RemoveRoute(int32 route_id
);
129 gpu::PreemptionFlag
* GetPreemptionFlag();
131 bool handle_messages_scheduled() const { return handle_messages_scheduled_
; }
132 uint64
messages_processed() const { return messages_processed_
; }
134 // If |preemption_flag->IsSet()|, any stub on this channel
135 // should stop issuing GL commands. Setting this to NULL stops deferral.
136 void SetPreemptByFlag(
137 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
);
139 void CacheShader(const std::string
& key
, const std::string
& shader
);
141 void AddFilter(IPC::MessageFilter
* filter
);
142 void RemoveFilter(IPC::MessageFilter
* filter
);
144 uint64
GetMemoryUsage();
146 bool allow_future_sync_points() const { return allow_future_sync_points_
; }
149 friend class GpuChannelMessageFilter
;
153 bool OnControlMessageReceived(const IPC::Message
& msg
);
155 void HandleMessage();
158 void OnCreateOffscreenCommandBuffer(
159 const gfx::Size
& size
,
160 const GPUCreateCommandBufferConfig
& init_params
,
163 void OnDestroyCommandBuffer(int32 route_id
);
164 void OnDevToolsStartEventsRecording(int32 route_id
, bool* succeeded
);
165 void OnDevToolsStopEventsRecording();
167 // Decrement the count of unhandled IPC messages and defer preemption.
168 void MessageProcessed();
170 // The lifetime of objects of this class is managed by a GpuChannelManager.
171 // The GpuChannelManager destroy all the GpuChannels that they own when they
172 // are destroyed. So a raw pointer is safe.
173 GpuChannelManager
* gpu_channel_manager_
;
175 scoped_ptr
<IPC::SyncChannel
> channel_
;
177 uint64 messages_processed_
;
179 // Whether the processing of IPCs on this channel is stalled and we should
180 // preempt other GpuChannels.
181 scoped_refptr
<gpu::PreemptionFlag
> preempting_flag_
;
183 // If non-NULL, all stubs on this channel should stop processing GL
184 // commands (via their GpuScheduler) when preempted_flag_->IsSet()
185 scoped_refptr
<gpu::PreemptionFlag
> preempted_flag_
;
187 std::deque
<IPC::Message
*> deferred_messages_
;
189 // The id of the client who is on the other side of the channel.
192 // Uniquely identifies the channel within this GPU process.
193 std::string channel_id_
;
195 // Used to implement message routing functionality to CommandBuffer objects
196 MessageRouter router_
;
198 // The share group that all contexts associated with a particular renderer
200 scoped_refptr
<gfx::GLShareGroup
> share_group_
;
202 scoped_refptr
<gpu::gles2::MailboxManager
> mailbox_manager_
;
204 typedef IDMap
<GpuCommandBufferStub
, IDMapOwnPointer
> StubMap
;
207 bool log_messages_
; // True if we should log sent and received messages.
208 gpu::gles2::DisallowedFeatures disallowed_features_
;
209 GpuWatchdog
* watchdog_
;
211 bool handle_messages_scheduled_
;
212 IPC::Message
* currently_processing_message_
;
214 scoped_refptr
<GpuChannelMessageFilter
> filter_
;
215 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
216 scoped_ptr
<DevToolsGpuAgent
> devtools_gpu_agent_
;
218 size_t num_stubs_descheduled_
;
220 bool allow_future_sync_points_
;
222 // Member variables should appear before the WeakPtrFactory, to ensure
223 // that any WeakPtrs to Controller are invalidated before its members
224 // variable's destructors are executed, rendering them invalid.
225 base::WeakPtrFactory
<GpuChannel
> weak_factory_
;
227 DISALLOW_COPY_AND_ASSIGN(GpuChannel
);
230 } // namespace content
232 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_