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