Revert of [Chromecast] Ignore interfaces not used to connect to internet. (patchset...
[chromium-blink-merge.git] / content / common / gpu / client / command_buffer_proxy_impl.h
blob4bd2006bb5bc741b60975bfc5c6a3d8df3289a92
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.
5 #ifndef CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
6 #define CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_
8 #include <map>
9 #include <queue>
10 #include <string>
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/containers/scoped_ptr_hash_map.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "gpu/command_buffer/client/gpu_control.h"
20 #include "gpu/command_buffer/common/command_buffer.h"
21 #include "gpu/command_buffer/common/command_buffer_shared.h"
22 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
23 #include "ipc/ipc_listener.h"
24 #include "ui/events/latency_info.h"
26 struct GPUCommandBufferConsoleMessage;
28 namespace base {
29 class SharedMemory;
32 namespace gpu {
33 struct Mailbox;
36 namespace media {
37 class VideoDecodeAccelerator;
38 class VideoEncodeAccelerator;
41 namespace content {
42 class GpuChannelHost;
44 // Client side proxy that forwards messages synchronously to a
45 // CommandBufferStub.
46 class CommandBufferProxyImpl
47 : public gpu::CommandBuffer,
48 public gpu::GpuControl,
49 public IPC::Listener,
50 public base::SupportsWeakPtr<CommandBufferProxyImpl> {
51 public:
52 class DeletionObserver {
53 public:
54 // Called during the destruction of the CommandBufferProxyImpl.
55 virtual void OnWillDeleteImpl() = 0;
57 protected:
58 virtual ~DeletionObserver() {}
61 typedef base::Callback<void(
62 const std::string& msg, int id)> GpuConsoleMessageCallback;
64 CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
65 ~CommandBufferProxyImpl() override;
67 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
68 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns
69 // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
70 // Note that the GpuVideoDecodeAccelerator may still fail to be created in
71 // the GPU process, even if this returns non-NULL. In this case the VDA client
72 // is notified of an error later, after Initialize().
73 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder();
75 // Sends an IPC message to create a GpuVideoEncodeAccelerator. Creates and
76 // returns it as an owned pointer to a media::VideoEncodeAccelerator. Returns
77 // NULL on failure to create the GpuVideoEncodeAcceleratorHost.
78 // Note that the GpuVideoEncodeAccelerator may still fail to be created in
79 // the GPU process, even if this returns non-NULL. In this case the VEA client
80 // is notified of an error later, after Initialize();
81 scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder();
83 // IPC::Listener implementation:
84 bool OnMessageReceived(const IPC::Message& message) override;
85 void OnChannelError() override;
87 // CommandBuffer implementation:
88 bool Initialize() override;
89 State GetLastState() override;
90 int32 GetLastToken() override;
91 void Flush(int32 put_offset) override;
92 void OrderingBarrier(int32 put_offset) override;
93 void WaitForTokenInRange(int32 start, int32 end) override;
94 void WaitForGetOffsetInRange(int32 start, int32 end) override;
95 void SetGetBuffer(int32 shm_id) override;
96 scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
97 int32* id) override;
98 void DestroyTransferBuffer(int32 id) override;
100 // gpu::GpuControl implementation:
101 gpu::Capabilities GetCapabilities() override;
102 int32 CreateImage(ClientBuffer buffer,
103 size_t width,
104 size_t height,
105 unsigned internalformat) override;
106 void DestroyImage(int32 id) override;
107 int32 CreateGpuMemoryBufferImage(size_t width,
108 size_t height,
109 unsigned internalformat,
110 unsigned usage) override;
111 uint32 InsertSyncPoint() override;
112 uint32_t InsertFutureSyncPoint() override;
113 void RetireSyncPoint(uint32_t sync_point) override;
114 void SignalSyncPoint(uint32 sync_point,
115 const base::Closure& callback) override;
116 void SignalQuery(uint32 query, const base::Closure& callback) override;
117 void SetSurfaceVisible(bool visible) override;
118 uint32 CreateStreamTexture(uint32 texture_id) override;
119 void SetLock(base::Lock* lock) override;
121 int GetRouteID() const;
122 bool ProduceFrontBuffer(const gpu::Mailbox& mailbox);
123 void SetContextLostCallback(const base::Closure& callback);
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);
132 bool EnsureBackbuffer();
134 void SetOnConsoleMessageCallback(
135 const GpuConsoleMessageCallback& callback);
137 void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
138 using SwapBuffersCompletionCallback =
139 base::Callback<void(const std::vector<ui::LatencyInfo>& latency_info)>;
140 void SetSwapBuffersCompletionCallback(
141 const SwapBuffersCompletionCallback& callback);
143 using UpdateVSyncParametersCallback =
144 base::Callback<void(base::TimeTicks timebase, base::TimeDelta interval)>;
145 void SetUpdateVSyncParametersCallback(
146 const UpdateVSyncParametersCallback& callback);
148 // TODO(apatrick): this is a temporary optimization while skia is calling
149 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
150 // ints redundantly when only the error is needed for the
151 // CommandBufferProxyImpl implementation.
152 gpu::error::Error GetLastError() override;
154 GpuChannelHost* channel() const { return channel_; }
156 base::SharedMemoryHandle GetSharedStateHandle() const {
157 return shared_state_shm_->handle();
160 private:
161 typedef std::map<int32, scoped_refptr<gpu::Buffer> > TransferBufferMap;
162 typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
163 typedef base::ScopedPtrHashMap<int32, scoped_ptr<gfx::GpuMemoryBuffer>>
164 GpuMemoryBufferMap;
166 void CheckLock() {
167 if (lock_)
168 lock_->AssertAcquired();
171 // Send an IPC message over the GPU channel. This is private to fully
172 // encapsulate the channel; all callers of this function must explicitly
173 // verify that the context has not been lost.
174 bool Send(IPC::Message* msg);
176 // Message handlers:
177 void OnUpdateState(const gpu::CommandBuffer::State& state);
178 void OnDestroyed(gpu::error::ContextLostReason reason,
179 gpu::error::Error error);
180 void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
181 void OnSetMemoryAllocation(const gpu::MemoryAllocation& allocation);
182 void OnSignalSyncPointAck(uint32 id);
183 void OnSwapBuffersCompleted(const std::vector<ui::LatencyInfo>& latency_info);
184 void OnUpdateVSyncParameters(base::TimeTicks timebase,
185 base::TimeDelta interval);
187 // Try to read an updated copy of the state from shared memory.
188 void TryUpdateState();
190 // The shared memory area used to update state.
191 gpu::CommandBufferSharedState* shared_state() const;
193 base::Lock* lock_;
195 // Unowned list of DeletionObservers.
196 ObserverList<DeletionObserver> deletion_observers_;
198 // The last cached state received from the service.
199 State last_state_;
201 // The shared memory area used to update state.
202 scoped_ptr<base::SharedMemory> shared_state_shm_;
204 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
205 // raw pointer is ok.
206 GpuChannelHost* channel_;
207 int route_id_;
208 unsigned int flush_count_;
209 int32 last_put_offset_;
210 int32 last_barrier_put_offset_;
212 base::Closure context_lost_callback_;
214 MemoryAllocationChangedCallback memory_allocation_changed_callback_;
216 GpuConsoleMessageCallback console_message_callback_;
218 // Tasks to be invoked in SignalSyncPoint responses.
219 uint32 next_signal_id_;
220 SignalTaskMap signal_tasks_;
222 // Local cache of id to gpu memory buffer mapping.
223 GpuMemoryBufferMap gpu_memory_buffers_;
225 gpu::Capabilities capabilities_;
227 std::vector<ui::LatencyInfo> latency_info_;
229 SwapBuffersCompletionCallback swap_buffers_completion_callback_;
230 UpdateVSyncParametersCallback update_vsync_parameters_completion_callback_;
232 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
235 } // namespace content
237 #endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_