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