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