Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / common / gpu / gpu_channel.h
blob0d8538f4889b72d4d094c5923b08417502cfcdcf
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_
8 #include <deque>
9 #include <string>
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;
30 namespace base {
31 class MessageLoopProxy;
32 class WaitableEvent;
35 namespace gpu {
36 class PreemptionFlag;
39 namespace IPC {
40 class MessageFilter;
43 namespace content {
44 class DevToolsGpuAgent;
45 class GpuChannelManager;
46 class GpuChannelMessageFilter;
47 class GpuWatchdog;
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 {
52 public:
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,
58 int client_id,
59 bool software,
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();
74 #if defined(OS_POSIX)
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.
102 void OnScheduled();
104 // This is called when a command buffer transitions between scheduled and
105 // descheduled states. When any stub is descheduled, we stop preempting
106 // other channels.
107 void StubSchedulingChanged(bool scheduled);
109 CreateCommandBufferResult CreateViewCommandBuffer(
110 const gfx::GLSurfaceHandle& window,
111 int32 surface_id,
112 const GPUCreateCommandBufferConfig& init_params,
113 int32 route_id);
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_; }
148 private:
149 friend class GpuChannelMessageFilter;
151 void OnDestroy();
153 bool OnControlMessageReceived(const IPC::Message& msg);
155 void HandleMessage();
157 // Message handlers.
158 void OnCreateOffscreenCommandBuffer(
159 const gfx::Size& size,
160 const GPUCreateCommandBufferConfig& init_params,
161 int32 route_id,
162 bool* succeeded);
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.
190 int client_id_;
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
199 // process use.
200 scoped_refptr<gfx::GLShareGroup> share_group_;
202 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
204 typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap;
205 StubMap stubs_;
207 bool log_messages_; // True if we should log sent and received messages.
208 gpu::gles2::DisallowedFeatures disallowed_features_;
209 GpuWatchdog* watchdog_;
210 bool software_;
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_