Upstream version 5.34.104.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/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 gpu::Capabilities GetCapabilities() 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 uint32 InsertSyncPoint() OVERRIDE;
107   virtual void SignalSyncPoint(uint32 sync_point,
108                                const base::Closure& callback) OVERRIDE;
109   virtual void SignalQuery(uint32 query,
110                            const base::Closure& callback) OVERRIDE;
111   virtual void SetSurfaceVisible(bool visible) OVERRIDE;
112   virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
113       OVERRIDE;
114   virtual void Echo(const base::Closure& callback) OVERRIDE;
115   virtual uint32 CreateStreamTexture(uint32 texture_id) OVERRIDE;
116
117   int GetRouteID() const;
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 EnsureBackbuffer();
129
130   void SetOnConsoleMessageCallback(
131       const GpuConsoleMessageCallback& callback);
132
133   void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
134
135   // TODO(apatrick): this is a temporary optimization while skia is calling
136   // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
137   // ints redundantly when only the error is needed for the
138   // CommandBufferProxyImpl implementation.
139   virtual gpu::error::Error GetLastError() OVERRIDE;
140
141   GpuChannelHost* channel() const { return channel_; }
142
143  private:
144   typedef std::map<int32, gpu::Buffer> TransferBufferMap;
145   typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
146   typedef std::map<int32, gfx::GpuMemoryBuffer*> GpuMemoryBufferMap;
147
148   // Send an IPC message over the GPU channel. This is private to fully
149   // encapsulate the channel; all callers of this function must explicitly
150   // verify that the context has not been lost.
151   bool Send(IPC::Message* msg);
152
153   // Message handlers:
154   void OnUpdateState(const gpu::CommandBuffer::State& state);
155   void OnDestroyed(gpu::error::ContextLostReason reason);
156   void OnEchoAck();
157   void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
158   void OnSetMemoryAllocation(const gpu::MemoryAllocation& allocation);
159   void OnSignalSyncPointAck(uint32 id);
160
161   // Try to read an updated copy of the state from shared memory.
162   void TryUpdateState();
163
164   // The shared memory area used to update state.
165   gpu::CommandBufferSharedState* shared_state() const {
166     return reinterpret_cast<gpu::CommandBufferSharedState*>(
167         shared_state_shm_->memory());
168   }
169
170   // Local cache of id to transfer buffer mapping.
171   TransferBufferMap transfer_buffers_;
172
173   // Unowned list of DeletionObservers.
174   ObserverList<DeletionObserver> deletion_observers_;
175
176   // The last cached state received from the service.
177   State last_state_;
178
179   // The shared memory area used to update state.
180   scoped_ptr<base::SharedMemory> shared_state_shm_;
181
182   // |*this| is owned by |*channel_| and so is always outlived by it, so using a
183   // raw pointer is ok.
184   GpuChannelHost* channel_;
185   int route_id_;
186   unsigned int flush_count_;
187   int32 last_put_offset_;
188
189   // Tasks to be invoked in echo responses.
190   std::queue<base::Closure> echo_tasks_;
191
192   base::Closure channel_error_callback_;
193
194   MemoryAllocationChangedCallback memory_allocation_changed_callback_;
195
196   GpuConsoleMessageCallback console_message_callback_;
197
198   // Tasks to be invoked in SignalSyncPoint responses.
199   uint32 next_signal_id_;
200   SignalTaskMap signal_tasks_;
201
202   // Local cache of id to gpu memory buffer mapping.
203   GpuMemoryBufferMap gpu_memory_buffers_;
204
205   gpu::Capabilities capabilities_;
206
207   DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
208 };
209
210 }  // namespace content
211
212 #endif  // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_