Upstream version 7.36.149.0
[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/client/gpu_control.h"
19 #include "gpu/command_buffer/common/command_buffer.h"
20 #include "gpu/command_buffer/common/command_buffer_shared.h"
21 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
22 #include "ipc/ipc_listener.h"
23 #include "ui/events/latency_info.h"
24
25 struct GPUCommandBufferConsoleMessage;
26
27 namespace base {
28 class SharedMemory;
29 }
30
31 namespace gfx {
32 class GpuMemoryBuffer;
33 }
34
35 namespace gpu {
36 struct Mailbox;
37 }
38
39 namespace media {
40 class VideoDecodeAccelerator;
41 class VideoEncodeAccelerator;
42 }
43
44 namespace content {
45 class GpuChannelHost;
46
47 // Client side proxy that forwards messages synchronously to a
48 // CommandBufferStub.
49 class CommandBufferProxyImpl
50     : public gpu::CommandBuffer,
51       public gpu::GpuControl,
52       public IPC::Listener,
53       public base::SupportsWeakPtr<CommandBufferProxyImpl> {
54  public:
55   class DeletionObserver {
56    public:
57     // Called during the destruction of the CommandBufferProxyImpl.
58     virtual void OnWillDeleteImpl() = 0;
59
60    protected:
61     virtual ~DeletionObserver() {}
62   };
63
64   typedef base::Callback<void(
65       const std::string& msg, int id)> GpuConsoleMessageCallback;
66
67   CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
68   virtual ~CommandBufferProxyImpl();
69
70   // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
71   // returns it as an owned pointer to a media::VideoDecodeAccelerator.  Returns
72   // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
73   // Note that the GpuVideoDecodeAccelerator may still fail to be created in
74   // the GPU process, even if this returns non-NULL. In this case the VDA client
75   // is notified of an error later, after Initialize().
76   scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder();
77
78   // Sends an IPC message to create a GpuVideoEncodeAccelerator. Creates and
79   // returns it as an owned pointer to a media::VideoEncodeAccelerator.  Returns
80   // NULL on failure to create the GpuVideoEncodeAcceleratorHost.
81   // Note that the GpuVideoEncodeAccelerator may still fail to be created in
82   // the GPU process, even if this returns non-NULL. In this case the VEA client
83   // is notified of an error later, after Initialize();
84   scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder();
85
86   // IPC::Listener implementation:
87   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
88   virtual void OnChannelError() OVERRIDE;
89
90   // CommandBuffer implementation:
91   virtual bool Initialize() OVERRIDE;
92   virtual State GetLastState() OVERRIDE;
93   virtual int32 GetLastToken() OVERRIDE;
94   virtual void Flush(int32 put_offset) OVERRIDE;
95   virtual void WaitForTokenInRange(int32 start, int32 end) OVERRIDE;
96   virtual void WaitForGetOffsetInRange(int32 start, int32 end) OVERRIDE;
97   virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
98   virtual scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
99                                                           int32* id) OVERRIDE;
100   virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
101
102   // gpu::GpuControl implementation:
103   virtual gpu::Capabilities GetCapabilities() OVERRIDE;
104   virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(size_t width,
105                                                       size_t height,
106                                                       unsigned internalformat,
107                                                       unsigned usage,
108                                                       int32* id) OVERRIDE;
109   virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
110   virtual uint32 InsertSyncPoint() OVERRIDE;
111   virtual void SignalSyncPoint(uint32 sync_point,
112                                const base::Closure& callback) OVERRIDE;
113   virtual void SignalQuery(uint32 query,
114                            const base::Closure& callback) OVERRIDE;
115   virtual void SetSurfaceVisible(bool visible) OVERRIDE;
116   virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
117       OVERRIDE;
118   virtual void Echo(const base::Closure& callback) OVERRIDE;
119   virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
120
121   int GetRouteID() const;
122   bool ProduceFrontBuffer(const gpu::Mailbox& mailbox);
123   void SetChannelErrorCallback(const base::Closure& callback);
124
125   typedef base::Callback<void(const gpu::MemoryAllocation&)>
126       MemoryAllocationChangedCallback;
127   void SetMemoryAllocationChangedCallback(
128       const MemoryAllocationChangedCallback& callback);
129   void AddDeletionObserver(DeletionObserver* observer);
130   void RemoveDeletionObserver(DeletionObserver* observer);
131
132   bool EnsureBackbuffer();
133
134   void SetOnConsoleMessageCallback(
135       const GpuConsoleMessageCallback& callback);
136
137   void SetLatencyInfo(const std::vector<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, scoped_refptr<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
165   // Try to read an updated copy of the state from shared memory.
166   void TryUpdateState();
167
168   // The shared memory area used to update state.
169   gpu::CommandBufferSharedState* shared_state() const;
170
171   // Unowned list of DeletionObservers.
172   ObserverList<DeletionObserver> deletion_observers_;
173
174   // The last cached state received from the service.
175   State last_state_;
176
177   // The shared memory area used to update state.
178   scoped_ptr<base::SharedMemory> shared_state_shm_;
179
180   // |*this| is owned by |*channel_| and so is always outlived by it, so using a
181   // raw pointer is ok.
182   GpuChannelHost* channel_;
183   int route_id_;
184   unsigned int flush_count_;
185   int32 last_put_offset_;
186
187   // Tasks to be invoked in echo responses.
188   std::queue<base::Closure> echo_tasks_;
189
190   base::Closure channel_error_callback_;
191
192   MemoryAllocationChangedCallback memory_allocation_changed_callback_;
193
194   GpuConsoleMessageCallback console_message_callback_;
195
196   // Tasks to be invoked in SignalSyncPoint responses.
197   uint32 next_signal_id_;
198   SignalTaskMap signal_tasks_;
199
200   // Local cache of id to gpu memory buffer mapping.
201   GpuMemoryBufferMap gpu_memory_buffers_;
202
203   gpu::Capabilities capabilities_;
204
205   DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
206 };
207
208 }  // namespace content
209
210 #endif  // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_