Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / client / command_buffer_proxy_impl.h
blobe2488914d6097c186370ac17ca2d3b4730b26f6e
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"
25 #include "ui/gfx/swap_result.h"
27 struct GPUCommandBufferConsoleMessage;
29 namespace base {
30 class SharedMemory;
33 namespace gpu {
34 struct Mailbox;
37 namespace media {
38 class VideoDecodeAccelerator;
39 class VideoEncodeAccelerator;
42 namespace content {
43 class GpuChannelHost;
45 // Client side proxy that forwards messages synchronously to a
46 // CommandBufferStub.
47 class CommandBufferProxyImpl
48 : public gpu::CommandBuffer,
49 public gpu::GpuControl,
50 public IPC::Listener,
51 public base::SupportsWeakPtr<CommandBufferProxyImpl> {
52 public:
53 class DeletionObserver {
54 public:
55 // Called during the destruction of the CommandBufferProxyImpl.
56 virtual void OnWillDeleteImpl() = 0;
58 protected:
59 virtual ~DeletionObserver() {}
62 typedef base::Callback<void(
63 const std::string& msg, int id)> GpuConsoleMessageCallback;
65 CommandBufferProxyImpl(GpuChannelHost* channel, int route_id);
66 ~CommandBufferProxyImpl() override;
68 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and
69 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns
70 // NULL on failure to create the GpuVideoDecodeAcceleratorHost.
71 // Note that the GpuVideoDecodeAccelerator may still fail to be created in
72 // the GPU process, even if this returns non-NULL. In this case the VDA client
73 // is notified of an error later, after Initialize().
74 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder();
76 // Sends an IPC message to create a GpuVideoEncodeAccelerator. Creates and
77 // returns it as an owned pointer to a media::VideoEncodeAccelerator. Returns
78 // NULL on failure to create the GpuVideoEncodeAcceleratorHost.
79 // Note that the GpuVideoEncodeAccelerator may still fail to be created in
80 // the GPU process, even if this returns non-NULL. In this case the VEA client
81 // is notified of an error later, after Initialize();
82 scoped_ptr<media::VideoEncodeAccelerator> CreateVideoEncoder();
84 // IPC::Listener implementation:
85 bool OnMessageReceived(const IPC::Message& message) override;
86 void OnChannelError() override;
88 // CommandBuffer implementation:
89 bool Initialize() override;
90 State GetLastState() override;
91 int32 GetLastToken() override;
92 void Flush(int32 put_offset) override;
93 void OrderingBarrier(int32 put_offset) override;
94 void WaitForTokenInRange(int32 start, int32 end) override;
95 void WaitForGetOffsetInRange(int32 start, int32 end) override;
96 void SetGetBuffer(int32 shm_id) override;
97 scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size,
98 int32* id) override;
99 void DestroyTransferBuffer(int32 id) override;
101 // gpu::GpuControl implementation:
102 gpu::Capabilities GetCapabilities() override;
103 int32 CreateImage(ClientBuffer buffer,
104 size_t width,
105 size_t height,
106 unsigned internalformat) override;
107 void DestroyImage(int32 id) override;
108 int32 CreateGpuMemoryBufferImage(size_t width,
109 size_t height,
110 unsigned internalformat,
111 unsigned usage) override;
112 uint32 InsertSyncPoint() override;
113 uint32_t InsertFutureSyncPoint() override;
114 void RetireSyncPoint(uint32_t sync_point) override;
115 void SignalSyncPoint(uint32 sync_point,
116 const base::Closure& callback) override;
117 void SignalQuery(uint32 query, const base::Closure& callback) override;
118 void SetSurfaceVisible(bool visible) override;
119 uint32 CreateStreamTexture(uint32 texture_id) override;
120 void SetLock(base::Lock* lock) override;
121 bool IsGpuChannelLost() override;
123 int GetRouteID() const;
124 bool ProduceFrontBuffer(const gpu::Mailbox& mailbox);
125 void SetContextLostCallback(const base::Closure& callback);
127 typedef base::Callback<void(const gpu::MemoryAllocation&)>
128 MemoryAllocationChangedCallback;
129 void SetMemoryAllocationChangedCallback(
130 const MemoryAllocationChangedCallback& callback);
131 void AddDeletionObserver(DeletionObserver* observer);
132 void RemoveDeletionObserver(DeletionObserver* observer);
134 bool EnsureBackbuffer();
136 void SetOnConsoleMessageCallback(
137 const GpuConsoleMessageCallback& callback);
139 void SetLatencyInfo(const std::vector<ui::LatencyInfo>& latency_info);
140 using SwapBuffersCompletionCallback =
141 base::Callback<void(const std::vector<ui::LatencyInfo>& latency_info,
142 gfx::SwapResult result)>;
143 void SetSwapBuffersCompletionCallback(
144 const SwapBuffersCompletionCallback& callback);
146 using UpdateVSyncParametersCallback =
147 base::Callback<void(base::TimeTicks timebase, base::TimeDelta interval)>;
148 void SetUpdateVSyncParametersCallback(
149 const UpdateVSyncParametersCallback& callback);
151 // TODO(apatrick): this is a temporary optimization while skia is calling
152 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6
153 // ints redundantly when only the error is needed for the
154 // CommandBufferProxyImpl implementation.
155 gpu::error::Error GetLastError() override;
157 GpuChannelHost* channel() const { return channel_; }
159 base::SharedMemoryHandle GetSharedStateHandle() const {
160 return shared_state_shm_->handle();
163 private:
164 typedef std::map<int32, scoped_refptr<gpu::Buffer> > TransferBufferMap;
165 typedef base::hash_map<uint32, base::Closure> SignalTaskMap;
167 void CheckLock() {
168 if (lock_)
169 lock_->AssertAcquired();
172 // Send an IPC message over the GPU channel. This is private to fully
173 // encapsulate the channel; all callers of this function must explicitly
174 // verify that the context has not been lost.
175 bool Send(IPC::Message* msg);
177 // Message handlers:
178 void OnUpdateState(const gpu::CommandBuffer::State& state);
179 void OnDestroyed(gpu::error::ContextLostReason reason,
180 gpu::error::Error error);
181 void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message);
182 void OnSetMemoryAllocation(const gpu::MemoryAllocation& allocation);
183 void OnSignalSyncPointAck(uint32 id);
184 void OnSwapBuffersCompleted(const std::vector<ui::LatencyInfo>& latency_info,
185 gfx::SwapResult result);
186 void OnUpdateVSyncParameters(base::TimeTicks timebase,
187 base::TimeDelta interval);
189 // Try to read an updated copy of the state from shared memory.
190 void TryUpdateState();
192 // The shared memory area used to update state.
193 gpu::CommandBufferSharedState* shared_state() const;
195 base::Lock* lock_;
197 // Unowned list of DeletionObservers.
198 base::ObserverList<DeletionObserver> deletion_observers_;
200 // The last cached state received from the service.
201 State last_state_;
203 // The shared memory area used to update state.
204 scoped_ptr<base::SharedMemory> shared_state_shm_;
206 // |*this| is owned by |*channel_| and so is always outlived by it, so using a
207 // raw pointer is ok.
208 GpuChannelHost* channel_;
209 int route_id_;
210 unsigned int flush_count_;
211 int32 last_put_offset_;
212 int32 last_barrier_put_offset_;
214 base::Closure context_lost_callback_;
216 MemoryAllocationChangedCallback memory_allocation_changed_callback_;
218 GpuConsoleMessageCallback console_message_callback_;
220 // Tasks to be invoked in SignalSyncPoint responses.
221 uint32 next_signal_id_;
222 SignalTaskMap signal_tasks_;
224 gpu::Capabilities capabilities_;
226 std::vector<ui::LatencyInfo> latency_info_;
228 SwapBuffersCompletionCallback swap_buffers_completion_callback_;
229 UpdateVSyncParametersCallback update_vsync_parameters_completion_callback_;
231 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl);
234 } // namespace content
236 #endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_