| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_ | 5 #ifndef CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_ |
| 6 #define CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_ | 6 #define CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/atomic_sequence_num.h" | 14 #include "base/atomic_sequence_num.h" |
| 15 #include "base/containers/scoped_ptr_hash_map.h" | 15 #include "base/containers/scoped_ptr_hash_map.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "base/process/process.h" | 20 #include "base/process/process.h" |
| 21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
| 23 #include "content/common/gpu/client/ipc/gpu_channel_host_ipc_transport.h" |
| 23 #include "content/common/gpu/gpu_process_launch_causes.h" | 24 #include "content/common/gpu/gpu_process_launch_causes.h" |
| 24 #include "content/common/gpu/gpu_result_codes.h" | 25 #include "content/common/gpu/gpu_result_codes.h" |
| 25 #include "content/common/gpu/gpu_stream_priority.h" | 26 #include "content/common/gpu/gpu_stream_priority.h" |
| 26 #include "content/common/message_router.h" | |
| 27 #include "gpu/config/gpu_info.h" | 27 #include "gpu/config/gpu_info.h" |
| 28 #include "ipc/ipc_channel_handle.h" | |
| 29 #include "ipc/ipc_sync_channel.h" | |
| 30 #include "ipc/message_filter.h" | |
| 31 #include "media/video/jpeg_decode_accelerator.h" | 28 #include "media/video/jpeg_decode_accelerator.h" |
| 32 #include "ui/events/latency_info.h" | 29 #include "ui/events/latency_info.h" |
| 33 #include "ui/gfx/geometry/size.h" | 30 #include "ui/gfx/geometry/size.h" |
| 34 #include "ui/gfx/gpu_memory_buffer.h" | 31 #include "ui/gfx/gpu_memory_buffer.h" |
| 35 #include "ui/gfx/native_widget_types.h" | 32 #include "ui/gfx/native_widget_types.h" |
| 36 #include "ui/gl/gpu_preference.h" | 33 #include "ui/gl/gpu_preference.h" |
| 37 | 34 |
| 38 class GURL; | 35 class GURL; |
| 39 class TransportTextureService; | 36 class TransportTextureService; |
| 40 struct GPUCreateCommandBufferConfig; | |
| 41 | 37 |
| 42 namespace base { | 38 namespace base { |
| 43 class MessageLoop; | 39 class MessageLoop; |
| 44 class WaitableEvent; | 40 class WaitableEvent; |
| 45 } | 41 } |
| 46 | 42 |
| 47 namespace IPC { | 43 namespace IPC { |
| 48 class SyncMessageFilter; | 44 class SyncMessageFilter; |
| 49 } | 45 } |
| 50 | 46 |
| 51 namespace media { | 47 namespace media { |
| 52 class JpegDecodeAccelerator; | 48 class JpegDecodeAccelerator; |
| 53 class VideoDecodeAccelerator; | 49 class VideoDecodeAccelerator; |
| 54 class VideoEncodeAccelerator; | 50 class VideoEncodeAccelerator; |
| 55 } | 51 } |
| 56 | 52 |
| 57 namespace gpu { | 53 namespace gpu { |
| 58 class GpuMemoryBufferManager; | 54 class GpuMemoryBufferManager; |
| 59 } | 55 } |
| 60 | 56 |
| 61 namespace content { | 57 namespace content { |
| 62 class CommandBufferProxyImpl; | 58 class CommandBufferProxyImpl; |
| 63 class GpuChannelHost; | 59 class GpuChannelHost; |
| 64 | 60 class GpuChannelHostFactory; |
| 65 class CONTENT_EXPORT GpuChannelHostFactory { | 61 struct GpuCreateCommandBufferConfig; |
| 66 public: | |
| 67 virtual ~GpuChannelHostFactory() {} | |
| 68 | |
| 69 virtual bool IsMainThread() = 0; | |
| 70 virtual scoped_refptr<base::SingleThreadTaskRunner> | |
| 71 GetIOThreadTaskRunner() = 0; | |
| 72 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(size_t size) = 0; | |
| 73 virtual CreateCommandBufferResult CreateViewCommandBuffer( | |
| 74 int32_t surface_id, | |
| 75 const GPUCreateCommandBufferConfig& init_params, | |
| 76 int32_t route_id) = 0; | |
| 77 }; | |
| 78 | 62 |
| 79 // Encapsulates an IPC channel between the client and one GPU process. | 63 // Encapsulates an IPC channel between the client and one GPU process. |
| 80 // On the GPU process side there's a corresponding GpuChannel. | 64 // On the GPU process side there's a corresponding GpuChannel. |
| 81 // Every method can be called on any thread with a message loop, except for the | 65 // Every method can be called on any thread with a message loop, except for the |
| 82 // IO thread. | 66 // IO thread. |
| 83 class GpuChannelHost : public IPC::Sender, | 67 class GpuChannelHost : public base::RefCountedThreadSafe<GpuChannelHost> { |
| 84 public base::RefCountedThreadSafe<GpuChannelHost> { | |
| 85 public: | 68 public: |
| 86 // Must be called on the main thread (as defined by the factory). | 69 // Must be called on the main thread (as defined by the factory). |
| 87 static scoped_refptr<GpuChannelHost> Create( | 70 static scoped_refptr<GpuChannelHost> Create( |
| 88 GpuChannelHostFactory* factory, | 71 scoped_ptr<GpuChannelHostIPCTransport> transport, |
| 89 int channel_id, | |
| 90 const gpu::GPUInfo& gpu_info, | 72 const gpu::GPUInfo& gpu_info, |
| 91 const IPC::ChannelHandle& channel_handle, | |
| 92 base::WaitableEvent* shutdown_event, | |
| 93 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager); | 73 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager); |
| 94 | 74 |
| 95 static const int32_t kDefaultStreamId = -1; | 75 static const int32_t kDefaultStreamId = -1; |
| 96 static const GpuStreamPriority kDefaultStreamPriority = | 76 static const GpuStreamPriority kDefaultStreamPriority = |
| 97 GpuStreamPriority::NORMAL; | 77 GpuStreamPriority::NORMAL; |
| 98 | 78 |
| 99 bool IsLost() const { | 79 bool IsLost() const { |
| 100 DCHECK(channel_filter_.get()); | 80 DCHECK(transport_.get()); |
| 101 return channel_filter_->IsLost(); | 81 return transport_->IsLost(); |
| 102 } | 82 } |
| 103 | 83 |
| 104 int channel_id() const { return channel_id_; } | |
| 105 | |
| 106 // The GPU stats reported by the GPU process. | 84 // The GPU stats reported by the GPU process. |
| 107 const gpu::GPUInfo& gpu_info() const { return gpu_info_; } | 85 const gpu::GPUInfo& gpu_info() const { return gpu_info_; } |
| 108 | 86 |
| 109 // IPC::Sender implementation: | |
| 110 bool Send(IPC::Message* msg) override; | |
| 111 | |
| 112 // Set an ordering barrier. AsyncFlushes any pending barriers on other | 87 // Set an ordering barrier. AsyncFlushes any pending barriers on other |
| 113 // routes. Combines multiple OrderingBarriers into a single AsyncFlush. | 88 // routes. Combines multiple OrderingBarriers into a single AsyncFlush. |
| 114 // Returns the flush ID for the stream or 0 if put offset was not changed. | 89 // Returns the flush ID for the stream or 0 if put offset was not changed. |
| 115 uint32_t OrderingBarrier(int32_t route_id, | 90 uint32_t OrderingBarrier(CommandBufferIPCTransport* transport, |
| 116 int32_t stream_id, | 91 int32_t stream_id, |
| 117 int32_t put_offset, | 92 int32_t put_offset, |
| 118 uint32_t flush_count, | 93 uint32_t flush_count, |
| 119 const std::vector<ui::LatencyInfo>& latency_info, | 94 const std::vector<ui::LatencyInfo>& latency_info, |
| 120 bool put_offset_changed, | 95 bool put_offset_changed, |
| 121 bool do_flush); | 96 bool do_flush); |
| 122 | 97 |
| 123 void FlushPendingStream(int32_t stream_id); | 98 void FlushPendingStream(int32_t stream_id); |
| 124 | 99 |
| 125 // Create and connect to a command buffer in the GPU process. | 100 // Create and connect to a command buffer in the GPU process. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 146 scoped_ptr<media::JpegDecodeAccelerator> CreateJpegDecoder( | 121 scoped_ptr<media::JpegDecodeAccelerator> CreateJpegDecoder( |
| 147 media::JpegDecodeAccelerator::Client* client); | 122 media::JpegDecodeAccelerator::Client* client); |
| 148 | 123 |
| 149 // Destroy a command buffer created by this channel. | 124 // Destroy a command buffer created by this channel. |
| 150 void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer); | 125 void DestroyCommandBuffer(CommandBufferProxyImpl* command_buffer); |
| 151 | 126 |
| 152 // Destroy this channel. Must be called on the main thread, before | 127 // Destroy this channel. Must be called on the main thread, before |
| 153 // destruction. | 128 // destruction. |
| 154 void DestroyChannel(); | 129 void DestroyChannel(); |
| 155 | 130 |
| 156 // Add a route for the current message loop. | |
| 157 void AddRoute(int route_id, base::WeakPtr<IPC::Listener> listener); | |
| 158 void RemoveRoute(int route_id); | |
| 159 | |
| 160 GpuChannelHostFactory* factory() const { return factory_; } | |
| 161 | |
| 162 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() const { | 131 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() const { |
| 163 return gpu_memory_buffer_manager_; | 132 return gpu_memory_buffer_manager_; |
| 164 } | 133 } |
| 165 | 134 |
| 166 // Returns a handle to the shared memory that can be sent via IPC to the | 135 // Returns a handle to the shared memory that can be sent via IPC to the |
| 167 // GPU process. The caller is responsible for ensuring it is closed. Returns | 136 // GPU process. The caller is responsible for ensuring it is closed. Returns |
| 168 // an invalid handle on failure. | 137 // an invalid handle on failure. |
| 169 base::SharedMemoryHandle ShareToGpuProcess( | 138 base::SharedMemoryHandle ShareToGpuProcess( |
| 170 base::SharedMemoryHandle source_handle); | 139 base::SharedMemoryHandle source_handle); |
| 171 | 140 |
| 172 // Reserve one unused transfer buffer ID. | 141 // Reserve one unused transfer buffer ID. |
| 173 int32_t ReserveTransferBufferId(); | 142 int32_t ReserveTransferBufferId(); |
| 174 | 143 |
| 175 // Returns a GPU memory buffer handle to the buffer that can be sent via | 144 // Returns a GPU memory buffer handle to the buffer that can be sent via |
| 176 // IPC to the GPU process. The caller is responsible for ensuring it is | 145 // IPC to the GPU process. The caller is responsible for ensuring it is |
| 177 // closed. Returns an invalid handle on failure. | 146 // closed. Returns an invalid handle on failure. |
| 178 gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuProcess( | 147 gfx::GpuMemoryBufferHandle ShareGpuMemoryBufferToGpuProcess( |
| 179 const gfx::GpuMemoryBufferHandle& source_handle, | 148 const gfx::GpuMemoryBufferHandle& source_handle, |
| 180 bool* requires_sync_point); | 149 bool* requires_sync_point); |
| 181 | 150 |
| 182 // Reserve one unused image ID. | 151 // Reserve one unused image ID. |
| 183 int32_t ReserveImageId(); | 152 int32_t ReserveImageId(); |
| 184 | 153 |
| 185 // Generate a route ID guaranteed to be unique for this channel. | |
| 186 int32_t GenerateRouteID(); | |
| 187 | |
| 188 // Generate a stream ID guaranteed to be unique for this channel. | 154 // Generate a stream ID guaranteed to be unique for this channel. |
| 189 int32_t GenerateStreamID(); | 155 int32_t GenerateStreamID(); |
| 190 | 156 |
| 191 // Sends a synchronous nop to the server which validate that all previous IPC | 157 // Sends a synchronous nop to the server which validate that all previous IPC |
| 192 // messages have been received. Once the synchronous nop has been sent to the | 158 // messages have been received. Once the synchronous nop has been sent to the |
| 193 // server all previous flushes will all be marked as validated, including | 159 // server all previous flushes will all be marked as validated, including |
| 194 // flushes for other streams on the same channel. Once a validation has been | 160 // flushes for other streams on the same channel. Once a validation has been |
| 195 // sent, it will return the highest validated flush id for the stream. | 161 // sent, it will return the highest validated flush id for the stream. |
| 196 // If the validation fails (which can only happen upon context lost), the | 162 // If the validation fails (which can only happen upon context lost), the |
| 197 // highest validated flush id will not change. If no flush ID were ever | 163 // highest validated flush id will not change. If no flush ID were ever |
| 198 // validated then it will return 0 (Note the lowest valid flush ID is 1). | 164 // validated then it will return 0 (Note the lowest valid flush ID is 1). |
| 199 uint32_t ValidateFlushIDReachedServer(int32_t stream_id, bool force_validate); | 165 uint32_t ValidateFlushIDReachedServer(int32_t stream_id, bool force_validate); |
| 200 | 166 |
| 201 // Returns the highest validated flush ID for a given stream. | 167 // Returns the highest validated flush ID for a given stream. |
| 202 uint32_t GetHighestValidatedFlushID(int32_t stream_id); | 168 uint32_t GetHighestValidatedFlushID(int32_t stream_id); |
| 203 | 169 |
| 204 private: | 170 private: |
| 205 friend class base::RefCountedThreadSafe<GpuChannelHost>; | 171 friend class base::RefCountedThreadSafe<GpuChannelHost>; |
| 206 | 172 |
| 207 // A filter used internally to route incoming messages from the IO thread | |
| 208 // to the correct message loop. It also maintains some shared state between | |
| 209 // all the contexts. | |
| 210 class MessageFilter : public IPC::MessageFilter { | |
| 211 public: | |
| 212 MessageFilter(); | |
| 213 | |
| 214 // Called on the IO thread. | |
| 215 void AddRoute(int32_t route_id, | |
| 216 base::WeakPtr<IPC::Listener> listener, | |
| 217 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 218 // Called on the IO thread. | |
| 219 void RemoveRoute(int32_t route_id); | |
| 220 | |
| 221 // IPC::MessageFilter implementation | |
| 222 // (called on the IO thread): | |
| 223 bool OnMessageReceived(const IPC::Message& msg) override; | |
| 224 void OnChannelError() override; | |
| 225 | |
| 226 // The following methods can be called on any thread. | |
| 227 | |
| 228 // Whether the channel is lost. | |
| 229 bool IsLost() const; | |
| 230 | |
| 231 private: | |
| 232 struct ListenerInfo { | |
| 233 ListenerInfo(); | |
| 234 ~ListenerInfo(); | |
| 235 | |
| 236 base::WeakPtr<IPC::Listener> listener; | |
| 237 scoped_refptr<base::SingleThreadTaskRunner> task_runner; | |
| 238 }; | |
| 239 | |
| 240 ~MessageFilter() override; | |
| 241 | |
| 242 // Threading notes: |listeners_| is only accessed on the IO thread. Every | |
| 243 // other field is protected by |lock_|. | |
| 244 base::hash_map<int32_t, ListenerInfo> listeners_; | |
| 245 | |
| 246 // Protects all fields below this one. | |
| 247 mutable base::Lock lock_; | |
| 248 | |
| 249 // Whether the channel has been lost. | |
| 250 bool lost_; | |
| 251 }; | |
| 252 | |
| 253 struct StreamFlushInfo { | 173 struct StreamFlushInfo { |
| 254 StreamFlushInfo(); | 174 StreamFlushInfo(); |
| 255 ~StreamFlushInfo(); | 175 ~StreamFlushInfo(); |
| 256 | 176 |
| 257 // These are global per stream. | 177 // These are global per stream. |
| 258 uint32_t next_stream_flush_id; | 178 uint32_t next_stream_flush_id; |
| 259 uint32_t flushed_stream_flush_id; | 179 uint32_t flushed_stream_flush_id; |
| 260 uint32_t verified_stream_flush_id; | 180 uint32_t verified_stream_flush_id; |
| 261 | 181 |
| 262 // These are local per context. | 182 // These are local per context. |
| 263 bool flush_pending; | 183 bool flush_pending; |
| 264 int32_t route_id; | 184 // int32_t route_id; |
| 185 CommandBufferIPCTransport* transport; |
| 265 int32_t put_offset; | 186 int32_t put_offset; |
| 266 uint32_t flush_count; | 187 uint32_t flush_count; |
| 267 uint32_t flush_id; | 188 uint32_t flush_id; |
| 268 std::vector<ui::LatencyInfo> latency_info; | 189 std::vector<ui::LatencyInfo> latency_info; |
| 269 }; | 190 }; |
| 270 | 191 |
| 271 GpuChannelHost(GpuChannelHostFactory* factory, | 192 GpuChannelHost(scoped_ptr<GpuChannelHostIPCTransport> transport, |
| 272 int channel_id, | |
| 273 const gpu::GPUInfo& gpu_info, | 193 const gpu::GPUInfo& gpu_info, |
| 274 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager); | 194 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager); |
| 275 ~GpuChannelHost() override; | 195 ~GpuChannelHost(); |
| 276 void Connect(const IPC::ChannelHandle& channel_handle, | 196 |
| 277 base::WaitableEvent* shutdown_event); | |
| 278 bool InternalSend(IPC::Message* msg); | |
| 279 void InternalFlush(StreamFlushInfo* flush_info); | 197 void InternalFlush(StreamFlushInfo* flush_info); |
| 280 | 198 |
| 281 // Threading notes: all fields are constant during the lifetime of |this| | |
| 282 // except: | |
| 283 // - |next_image_id_|, atomic type | |
| 284 // - |next_route_id_|, atomic type | |
| 285 // - |next_stream_id_|, atomic type | |
| 286 // - |channel_| and |stream_flush_info_|, protected by |context_lock_| | |
| 287 GpuChannelHostFactory* const factory_; | |
| 288 | |
| 289 const int channel_id_; | |
| 290 const gpu::GPUInfo gpu_info_; | 199 const gpu::GPUInfo gpu_info_; |
| 291 | 200 |
| 292 scoped_refptr<MessageFilter> channel_filter_; | |
| 293 | |
| 294 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_; | 201 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_; |
| 295 | 202 |
| 296 // A filter for sending messages from thread other than the main thread. | |
| 297 scoped_refptr<IPC::SyncMessageFilter> sync_filter_; | |
| 298 | |
| 299 // Image IDs are allocated in sequence. | 203 // Image IDs are allocated in sequence. |
| 300 base::AtomicSequenceNumber next_image_id_; | 204 base::AtomicSequenceNumber next_image_id_; |
| 301 | 205 |
| 302 // Route IDs are allocated in sequence. | |
| 303 base::AtomicSequenceNumber next_route_id_; | |
| 304 | |
| 305 // Stream IDs are allocated in sequence. | 206 // Stream IDs are allocated in sequence. |
| 306 base::AtomicSequenceNumber next_stream_id_; | 207 base::AtomicSequenceNumber next_stream_id_; |
| 307 | 208 |
| 308 // Protects channel_ and stream_flush_info_. | 209 // Protects channel_ and stream_flush_info_. |
| 309 mutable base::Lock context_lock_; | 210 mutable base::Lock context_lock_; |
| 310 scoped_ptr<IPC::SyncChannel> channel_; | |
| 311 base::hash_map<int32_t, StreamFlushInfo> stream_flush_info_; | 211 base::hash_map<int32_t, StreamFlushInfo> stream_flush_info_; |
| 312 | 212 |
| 213 scoped_ptr<GpuChannelHostIPCTransport> transport_; |
| 214 |
| 313 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost); | 215 DISALLOW_COPY_AND_ASSIGN(GpuChannelHost); |
| 314 }; | 216 }; |
| 315 | 217 |
| 316 } // namespace content | 218 } // namespace content |
| 317 | 219 |
| 318 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_ | 220 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_CHANNEL_HOST_H_ |
| OLD | NEW |