Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / gpu_channel.h
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.
4
5 #ifndef CONTENT_COMMON_GPU_GPU_CHANNEL_H_
6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_H_
7
8 #include <deque>
9 #include <string>
10
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"
26
27 struct GPUCreateCommandBufferConfig;
28
29 namespace base {
30 class MessageLoopProxy;
31 class WaitableEvent;
32 }
33
34 namespace gpu {
35 class PreemptionFlag;
36 namespace gles2 {
37 class ImageManager;
38 }
39 }
40
41 namespace IPC {
42 class MessageFilter;
43 }
44
45 namespace content {
46 class DevToolsGpuAgent;
47 class GpuChannelManager;
48 class GpuChannelMessageFilter;
49 class GpuWatchdog;
50
51 // Encapsulates an IPC channel between the GPU process and one renderer
52 // process. On the renderer side there's a corresponding GpuChannelHost.
53 class GpuChannel : public IPC::Listener,
54                    public IPC::Sender,
55                    public base::RefCountedThreadSafe<GpuChannel> {
56  public:
57   // Takes ownership of the renderer process handle.
58   GpuChannel(GpuChannelManager* gpu_channel_manager,
59              GpuWatchdog* watchdog,
60              gfx::GLShareGroup* share_group,
61              gpu::gles2::MailboxManager* mailbox_manager,
62              int client_id,
63              bool software);
64
65   void Init(base::MessageLoopProxy* io_message_loop,
66             base::WaitableEvent* shutdown_event);
67
68   // Get the GpuChannelManager that owns this channel.
69   GpuChannelManager* gpu_channel_manager() const {
70     return gpu_channel_manager_;
71   }
72
73   // Returns the name of the associated IPC channel.
74   std::string GetChannelName();
75
76 #if defined(OS_POSIX)
77   int TakeRendererFileDescriptor();
78 #endif  // defined(OS_POSIX)
79
80   base::ProcessId renderer_pid() const { return channel_->peer_pid(); }
81
82   int client_id() const { return client_id_; }
83
84   scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
85     return io_message_loop_;
86   }
87
88   // IPC::Listener implementation:
89   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
90   virtual void OnChannelError() OVERRIDE;
91
92   // IPC::Sender implementation:
93   virtual bool Send(IPC::Message* msg) OVERRIDE;
94
95   // Requeue the message that is currently being processed to the beginning of
96   // the queue. Used when the processing of a message gets aborted because of
97   // unscheduling conditions.
98   void RequeueMessage();
99
100   // This is called when a command buffer transitions from the unscheduled
101   // state to the scheduled state, which potentially means the channel
102   // transitions from the unscheduled to the scheduled state. When this occurs
103   // deferred IPC messaged are handled.
104   void OnScheduled();
105
106   // This is called when a command buffer transitions between scheduled and
107   // descheduled states. When any stub is descheduled, we stop preempting
108   // other channels.
109   void StubSchedulingChanged(bool scheduled);
110
111   bool CreateViewCommandBuffer(
112       const gfx::GLSurfaceHandle& window,
113       int32 surface_id,
114       const GPUCreateCommandBufferConfig& init_params,
115       int32 route_id);
116
117   void CreateImage(
118       gfx::PluginWindowHandle window,
119       int32 image_id,
120       gfx::Size* size);
121   void DeleteImage(int32 image_id);
122
123   gfx::GLShareGroup* share_group() const { return share_group_.get(); }
124
125   GpuCommandBufferStub* LookupCommandBuffer(int32 route_id);
126
127   void LoseAllContexts();
128   void MarkAllContextsLost();
129
130   // Destroy channel and all contained contexts.
131   void DestroySoon();
132
133   // Called to add a listener for a particular message routing ID.
134   // Returns true if succeeded.
135   bool AddRoute(int32 route_id, IPC::Listener* listener);
136
137   // Called to remove a listener for a particular message routing ID.
138   void RemoveRoute(int32 route_id);
139
140   gpu::PreemptionFlag* GetPreemptionFlag();
141
142   bool handle_messages_scheduled() const { return handle_messages_scheduled_; }
143   uint64 messages_processed() const { return messages_processed_; }
144
145   // If |preemption_flag->IsSet()|, any stub on this channel
146   // should stop issuing GL commands. Setting this to NULL stops deferral.
147   void SetPreemptByFlag(
148       scoped_refptr<gpu::PreemptionFlag> preemption_flag);
149
150   void CacheShader(const std::string& key, const std::string& shader);
151
152   void AddFilter(IPC::MessageFilter* filter);
153   void RemoveFilter(IPC::MessageFilter* filter);
154
155   uint64 GetMemoryUsage();
156
157  protected:
158   virtual ~GpuChannel();
159
160  private:
161   friend class base::RefCountedThreadSafe<GpuChannel>;
162   friend class GpuChannelMessageFilter;
163
164   void OnDestroy();
165
166   bool OnControlMessageReceived(const IPC::Message& msg);
167
168   void HandleMessage();
169
170   // Message handlers.
171   void OnCreateOffscreenCommandBuffer(
172       const gfx::Size& size,
173       const GPUCreateCommandBufferConfig& init_params,
174       int32 route_id,
175       bool* succeeded);
176   void OnDestroyCommandBuffer(int32 route_id);
177   void OnDevToolsStartEventsRecording(int32 route_id, bool* succeeded);
178   void OnDevToolsStopEventsRecording();
179
180   // Decrement the count of unhandled IPC messages and defer preemption.
181   void MessageProcessed();
182
183   // The lifetime of objects of this class is managed by a GpuChannelManager.
184   // The GpuChannelManager destroy all the GpuChannels that they own when they
185   // are destroyed. So a raw pointer is safe.
186   GpuChannelManager* gpu_channel_manager_;
187
188   scoped_ptr<IPC::SyncChannel> channel_;
189
190   uint64 messages_processed_;
191
192   // Whether the processing of IPCs on this channel is stalled and we should
193   // preempt other GpuChannels.
194   scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
195
196   // If non-NULL, all stubs on this channel should stop processing GL
197   // commands (via their GpuScheduler) when preempted_flag_->IsSet()
198   scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
199
200   std::deque<IPC::Message*> deferred_messages_;
201
202   // The id of the client who is on the other side of the channel.
203   int client_id_;
204
205   // Uniquely identifies the channel within this GPU process.
206   std::string channel_id_;
207
208   // Used to implement message routing functionality to CommandBuffer objects
209   MessageRouter router_;
210
211   // The share group that all contexts associated with a particular renderer
212   // process use.
213   scoped_refptr<gfx::GLShareGroup> share_group_;
214
215   scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
216   scoped_refptr<gpu::gles2::ImageManager> image_manager_;
217
218   typedef IDMap<GpuCommandBufferStub, IDMapOwnPointer> StubMap;
219   StubMap stubs_;
220
221   bool log_messages_;  // True if we should log sent and received messages.
222   gpu::gles2::DisallowedFeatures disallowed_features_;
223   GpuWatchdog* watchdog_;
224   bool software_;
225   bool handle_messages_scheduled_;
226   IPC::Message* currently_processing_message_;
227
228   base::WeakPtrFactory<GpuChannel> weak_factory_;
229
230   scoped_refptr<GpuChannelMessageFilter> filter_;
231   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
232   scoped_ptr<DevToolsGpuAgent> devtools_gpu_agent_;
233
234   size_t num_stubs_descheduled_;
235
236   DISALLOW_COPY_AND_ASSIGN(GpuChannel);
237 };
238
239 }  // namespace content
240
241 #endif  // CONTENT_COMMON_GPU_GPU_CHANNEL_H_