command_buffer_proxy_impl.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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 "gpu/ipc/command_buffer_proxy.h" 13 14#include "base/callback.h" 15#include "base/compiler_specific.h" 16#include "base/containers/hash_tables.h" 17#include "base/memory/ref_counted.h" 18#include "base/memory/weak_ptr.h" 19#include "base/observer_list.h" 20#include "content/common/gpu/gpu_memory_allocation.h" 21#include "content/common/gpu/gpu_memory_allocation.h" 22#include "content/common/gpu/surface_capturer.h" 23#include "gpu/command_buffer/common/command_buffer.h" 24#include "gpu/command_buffer/common/command_buffer_shared.h" 25#include "ipc/ipc_listener.h" 26#include "media/video/video_decode_accelerator.h" 27#include "ui/events/latency_info.h" 28 29struct GPUCommandBufferConsoleMessage; 30 31namespace base { 32class SharedMemory; 33} 34 35namespace gpu { 36struct Mailbox; 37} 38 39namespace content { 40class GpuChannelHost; 41 42// Client side proxy that forwards messages synchronously to a 43// CommandBufferStub. 44class CommandBufferProxyImpl 45 : public CommandBufferProxy, 46 public IPC::Listener, 47 public base::SupportsWeakPtr<CommandBufferProxyImpl> { 48 public: 49 class DeletionObserver { 50 public: 51 // Called during the destruction of the CommandBufferProxyImpl. 52 virtual void OnWillDeleteImpl() = 0; 53 54 protected: 55 virtual ~DeletionObserver() {} 56 }; 57 58 typedef base::Callback<void( 59 const std::string& msg, int id)> GpuConsoleMessageCallback; 60 61 CommandBufferProxyImpl(GpuChannelHost* channel, int route_id); 62 virtual ~CommandBufferProxyImpl(); 63 64 // Sends an IPC message to create a GpuVideoDecodeAccelerator. Creates and 65 // returns it as an owned pointer to a media::VideoDecodeAccelerator. Returns 66 // NULL on failure to create the GpuVideoDecodeAcceleratorHost. 67 // Note that the GpuVideoDecodeAccelerator may still fail to be created in 68 // the GPU process, even if this returns non-NULL. In this case the client is 69 // notified of an error later. 70 scoped_ptr<media::VideoDecodeAccelerator> CreateVideoDecoder( 71 media::VideoCodecProfile profile, 72 media::VideoDecodeAccelerator::Client* client); 73 74 // Send an IPC message to create a SurfaceCapturer. Returns NULL on failure 75 // to create the SurfaceCapturer. 76 scoped_ptr<SurfaceCapturer> CreateSurfaceCapturer( 77 SurfaceCapturer::Client* client); 78 79 // IPC::Listener implementation: 80 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 81 virtual void OnChannelError() OVERRIDE; 82 83 // CommandBufferProxy implementation: 84 virtual int GetRouteID() const OVERRIDE; 85 virtual bool Echo(const base::Closure& callback) OVERRIDE; 86 virtual bool ProduceFrontBuffer(const gpu::Mailbox& mailbox) OVERRIDE; 87 virtual void SetChannelErrorCallback(const base::Closure& callback) OVERRIDE; 88 89 // CommandBuffer implementation: 90 virtual bool Initialize() OVERRIDE; 91 virtual State GetState() OVERRIDE; 92 virtual State GetLastState() OVERRIDE; 93 virtual int32 GetLastToken() OVERRIDE; 94 virtual void Flush(int32 put_offset) OVERRIDE; 95 virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE; 96 virtual void SetGetBuffer(int32 shm_id) OVERRIDE; 97 virtual void SetGetOffset(int32 get_offset) OVERRIDE; 98 virtual gpu::Buffer CreateTransferBuffer(size_t size, 99 int32* id) OVERRIDE; 100 virtual void DestroyTransferBuffer(int32 id) OVERRIDE; 101 virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE; 102 virtual void SetToken(int32 token) OVERRIDE; 103 virtual void SetParseError(gpu::error::Error error) OVERRIDE; 104 virtual void SetContextLostReason( 105 gpu::error::ContextLostReason reason) OVERRIDE; 106 virtual uint32 InsertSyncPoint() OVERRIDE; 107 108 void SetMemoryAllocationChangedCallback( 109 const base::Callback<void(const GpuMemoryAllocationForRenderer&)>& 110 callback); 111 112 void AddDeletionObserver(DeletionObserver* observer); 113 void RemoveDeletionObserver(DeletionObserver* observer); 114 115 bool DiscardBackbuffer(); 116 bool EnsureBackbuffer(); 117 118 // Makes this command buffer invoke a task when a sync point is reached, or 119 // the command buffer that inserted that sync point is destroyed. 120 bool SignalSyncPoint(uint32 sync_point, 121 const base::Closure& callback); 122 123 // Makes this command buffer invoke a task when a query is completed, or 124 // the command buffer that inserted that sync point is destroyed or the 125 // query was deleted. Should be invoked after endQuery. 126 bool SignalQuery(unsigned query, const base::Closure& callback); 127 128 // Generates n unique mailbox names that can be used with 129 // GL_texture_mailbox_CHROMIUM. Unlike genMailboxCHROMIUM, this IPC is 130 // handled only on the GPU process' IO thread, and so is not effectively 131 // a finish. 132 bool GenerateMailboxNames(unsigned num, std::vector<gpu::Mailbox>* names); 133 134 // Sends an IPC message with the new state of surface visibility. 135 bool SetSurfaceVisible(bool visible); 136 137 void SetOnConsoleMessageCallback( 138 const GpuConsoleMessageCallback& callback); 139 140 void SetLatencyInfo(const ui::LatencyInfo& latency_info); 141 142 // TODO(apatrick): this is a temporary optimization while skia is calling 143 // ContentGLContext::MakeCurrent prior to every GL call. It saves returning 6 144 // ints redundantly when only the error is needed for the 145 // CommandBufferProxyImpl implementation. 146 virtual gpu::error::Error GetLastError() OVERRIDE; 147 148 void SendManagedMemoryStats(const GpuManagedMemoryStats& stats); 149 150 GpuChannelHost* channel() const { return channel_; } 151 152 private: 153 typedef std::map<int32, gpu::Buffer> TransferBufferMap; 154 typedef base::hash_map<uint32, base::Closure> SignalTaskMap; 155 156 // Send an IPC message over the GPU channel. This is private to fully 157 // encapsulate the channel; all callers of this function must explicitly 158 // verify that the context has not been lost. 159 bool Send(IPC::Message* msg); 160 161 // Message handlers: 162 void OnUpdateState(const gpu::CommandBuffer::State& state); 163 void OnDestroyed(gpu::error::ContextLostReason reason); 164 void OnEchoAck(); 165 void OnConsoleMessage(const GPUCommandBufferConsoleMessage& message); 166 void OnSetMemoryAllocation(const GpuMemoryAllocationForRenderer& allocation); 167 void OnSignalSyncPointAck(uint32 id); 168 void OnGenerateMailboxNamesReply(const std::vector<std::string>& names); 169 170 // Try to read an updated copy of the state from shared memory. 171 void TryUpdateState(); 172 173 // The shared memory area used to update state. 174 gpu::CommandBufferSharedState* shared_state() const { 175 return reinterpret_cast<gpu::CommandBufferSharedState*>( 176 shared_state_shm_->memory()); 177 } 178 179 // Local cache of id to transfer buffer mapping. 180 TransferBufferMap transfer_buffers_; 181 182 // Unowned list of DeletionObservers. 183 ObserverList<DeletionObserver> deletion_observers_; 184 185 // The last cached state received from the service. 186 State last_state_; 187 188 // The shared memory area used to update state. 189 scoped_ptr<base::SharedMemory> shared_state_shm_; 190 191 // |*this| is owned by |*channel_| and so is always outlived by it, so using a 192 // raw pointer is ok. 193 GpuChannelHost* channel_; 194 int route_id_; 195 unsigned int flush_count_; 196 int32 last_put_offset_; 197 198 // Tasks to be invoked in echo responses. 199 std::queue<base::Closure> echo_tasks_; 200 201 base::Closure channel_error_callback_; 202 203 base::Callback<void(const GpuMemoryAllocationForRenderer&)> 204 memory_allocation_changed_callback_; 205 206 GpuConsoleMessageCallback console_message_callback_; 207 208 // Tasks to be invoked in SignalSyncPoint responses. 209 uint32 next_signal_id_; 210 SignalTaskMap signal_tasks_; 211 212 DISALLOW_COPY_AND_ASSIGN(CommandBufferProxyImpl); 213}; 214 215} // namespace content 216 217#endif // CONTENT_COMMON_GPU_CLIENT_COMMAND_BUFFER_PROXY_IMPL_H_ 218