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