- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / command_buffer_proxy_impl.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_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
6 #define CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
7
8 #include <map>
9 #include <queue>
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "gpu/command_buffer/common/command_buffer.h"
19 #include "gpu/command_buffer/common/command_buffer_shared.h"
20 #include "gpu/command_buffer/common/gpu_control.h"
21 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
22 #include "ipc/ipc_listener.h"
23 #include "media/video/video_decode_accelerator.h"
24 #include "ui/events/latency_info.h"
25
26 struct GPUCommandBufferConsoleMessage;
27
28 namespace base {
29 class SharedMemory;
30 }
31
32 namespace gfx {
33 class GpuMemoryBuffer;
34 }
35
36 namespace gpu {
37 struct Mailbox;
38 }
39
40 namespace content {
41 class GpuChannelHost;
42
43 // Client side proxy that forwards messages synchronously to a
44 // CommandBufferStub.
45 class CommandBufferProxyImpl
46     : public gpu::CommandBuffer,
47       public gpu::GpuControl,
48       public IPC::Listener,
49       public base::SupportsWeakPtr<CommandBufferProxyImpl> {
50  public:
51   class DeletionObserver {
52    public:
53     // Called during the destruction of the CommandBufferProxyImpl.
54     virtual void OnWillDeleteImpl() = 0;
55
56    protected:
57     virtual ~DeletionObserver() {}
58   };
59
60   typedef base::Callback<void(
61       const std::string& msg, int id)> GpuConsoleMessageCallback;
62
63   CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
64   virtual ~CommandBufferProxyImpl();
65
66   // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
67   // returns it as an owned pointer to a media::VideoDecodeAccelerator.  Returns
68   // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
69   // Note that the GpuVideoDecodeAccelerator may still fail to be created in
70   // the GPU process, even if this returns non-NULL. In this case the client is
71   // notified of an error later.
72   scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder(
73       media::VideoCodecProfile profile,
74       media::VideoDecodeAccelerator::Client* client);
75
76   // IPC::Listener implementation:
77   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
78   virtual void OnChannelError() OVERRIDE;
79
80   // CommandBuffer implementation:
81   virtual bool Initialize() OVERRIDE;
82   virtual State GetState() OVERRIDE;
83   virtual State GetLastState() OVERRIDE;
84   virtual int32 GetLastToken() OVERRIDE;
85   virtual void Flush(int32 put_offset) OVERRIDE;
86   virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
87   virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
88   virtual void SetGetOffset(int32 get_offset) OVERRIDE;
89   virtual gpu::Buffer CreateTransferBuffer(size_t size,
90                                            int32* id) OVERRIDE;
91   virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
92   virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
93   virtual void SetToken(int32 token) OVERRIDE;
94   virtual void SetParseError(gpu::error::Error error) OVERRIDE;
95   virtual void SetContextLostReason(
96       gpu::error::ContextLostReason reason) OVERRIDE;
97
98   // gpu::GpuControl implementation:
99   virtual bool SupportsGpuMemoryBuffer() OVERRIDE;
100   virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
101       size_t width,
102       size_t height,
103       unsigned internalformat,
104       int32* id) OVERRIDE;
105   virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
106   virtual bool GenerateMailboxNames(unsigned num,
107                                     std::vector<gpu::Mailbox>* names) OVERRIDE;
108   virtual uint32 InsertSyncPoint() OVERRIDE;
109   virtual void SignalSyncPoint(uint32 sync_point,
110                                const base::Closure& callback) OVERRIDE;
111   virtual void SignalQuery(uint32 query,
112                            const base::Closure& callback) OVERRIDE;
113   virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
114       OVERRIDE;
115
116   int GetRouteID() const;
117   bool Echo(const base::Closure& callback);
118   bool ProduceFrontBuffer(const gpu::Mailbox& mailbox);
119   void SetChannelErrorCallback(const base::Closure& callback);
120
121   typedef base::Callback<void(const gpu::MemoryAllocation&)>
122       MemoryAllocationChangedCallback;
123   void SetMemoryAllocationChangedCallback(
124       const MemoryAllocationChangedCallback& callback);
125   void AddDeletionObserver(DeletionObserver* observer);
126   void RemoveDeletionObserver(DeletionObserver* observer);
127
128   bool DiscardBackbuffer();
129   bool EnsureBackbuffer();
130
131   // Sends an IPC message with the new state of surface visibility.
132   bool SetSurfaceVisible(bool visible);
133
134   void SetOnConsoleMessageCallback(
135       const GpuConsoleMessageCallback& callback);
136
137   void SetLatencyInfo(const ui::LatencyInfo& latency_info);
138
139   // TODO(apatrick): this is a temporary optimization while skia is calling
140   // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
141   // ints redundantly when only the error is needed for the
142   // CommandBufferProxyImpl implementation.
143   virtual gpu::error::Error GetLastError() OVERRIDE;
144
145   GpuChannelHost* channel() const { return channel_; }
146
147  private:
148   typedef std::map<int32, gpu::Buffer> TransferBufferMap;
149   typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
150   typedef std::map<int32, gfx::GpuMemoryBuffer*> GpuMemoryBufferMap;
151
152   // Send an IPC message over the GPU channel. This is private to fully
153   // encapsulate the channel; all callers of this function must explicitly
154   // verify that the context has not been lost.
155   bool Send(IPC::Message* msg);
156
157   // Message handlers:
158   void OnUpdateState(const gpu::CommandBuffer::State& state);
159   void OnDestroyed(gpu::error::ContextLostReason reason);
160   void OnEchoAck();
161   void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
162   void OnSetMemoryAllocation(const gpu::MemoryAllocation& allocation);
163   void OnSignalSyncPointAck(uint32 id);
164   void OnGenerateMailboxNamesReply(const std::vector<std::string>& names);
165
166   // Try to read an updated copy of the state from shared memory.
167   void TryUpdateState();
168
169   // The shared memory area used to update state.
170   gpu::CommandBufferSharedState* shared_state() const {
171     return reinterpret_cast<gpu::CommandBufferSharedState*>(
172         shared_state_shm_->memory());
173   }
174
175   // Local cache of id to transfer buffer mapping.
176   TransferBufferMap transfer_buffers_;
177
178   // Unowned list of DeletionObservers.
179   ObserverList<DeletionObserver> deletion_observers_;
180
181   // The last cached state received from the service.
182   State last_state_;
183
184   // The shared memory area used to update state.
185   scoped_ptr<base::SharedMemory> shared_state_shm_;
186
187   // |*this| is owned by |*channel_| and so is always outlived by it, so using a
188   // raw pointer is ok.
189   GpuChannelHost* channel_;
190   int route_id_;
191   unsigned int flush_count_;
192   int32 last_put_offset_;
193
194   // Tasks to be invoked in echo responses.
195   std::queue<base::Closure> echo_tasks_;
196
197   base::Closure channel_error_callback_;
198
199   MemoryAllocationChangedCallback memory_allocation_changed_callback_;
200
201   GpuConsoleMessageCallback console_message_callback_;
202
203   // Tasks to be invoked in SignalSyncPoint responses.
204   uint32 next_signal_id_;
205   SignalTaskMap signal_tasks_;
206
207   // Local cache of id to gpu memory buffer mapping.
208   GpuMemoryBufferMap gpu_memory_buffers_;
209
210   DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
211 };
212
213 }  // namespace content
214
215 #endif  // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_