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 GPU_IPC_SERVICE_GPU_CHANNEL_H_ 6 #define GPU_IPC_SERVICE_GPU_CHANNEL_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <memory> 12 #include <string> 13 14 #include "base/callback.h" 15 #include "base/containers/flat_map.h" 16 #include "base/macros.h" 17 #include "base/memory/ref_counted.h" 18 #include "base/memory/scoped_refptr.h" 19 #include "base/memory/weak_ptr.h" 20 #include "base/process/process.h" 21 #include "base/single_thread_task_runner.h" 22 #include "base/trace_event/memory_dump_provider.h" 23 #include "build/build_config.h" 24 #include "gpu/command_buffer/common/context_result.h" 25 #include "gpu/command_buffer/service/sync_point_manager.h" 26 #include "gpu/ipc/service/command_buffer_stub.h" 27 #include "gpu/ipc/service/gpu_ipc_service_export.h" 28 #include "gpu/ipc/service/shared_image_stub.h" 29 #include "ipc/ipc_sender.h" 30 #include "ipc/ipc_sync_channel.h" 31 #include "ipc/message_router.h" 32 #include "ui/gfx/geometry/size.h" 33 #include "ui/gfx/native_widget_types.h" 34 #include "ui/gl/gl_share_group.h" 35 #include "ui/gl/gpu_preference.h" 36 37 struct GPUCreateCommandBufferConfig; 38 39 namespace base { 40 class WaitableEvent; 41 } 42 43 namespace gpu { 44 class GpuChannelManager; 45 class GpuChannelMessageFilter; 46 class ImageDecodeAcceleratorStub; 47 class ImageDecodeAcceleratorWorker; 48 class Scheduler; 49 class SharedImageStub; 50 class StreamTexture; 51 class SyncPointManager; 52 53 // Encapsulates an IPC channel between the GPU process and one renderer 54 // process. On the renderer side there's a corresponding GpuChannelHost. 55 class GPU_IPC_SERVICE_EXPORT GpuChannel : public IPC::Listener, 56 public IPC::Sender { 57 public: 58 ~GpuChannel() override; 59 60 static std::unique_ptr<GpuChannel> Create( 61 GpuChannelManager* gpu_channel_manager, 62 Scheduler* scheduler, 63 SyncPointManager* sync_point_manager, 64 scoped_refptr<gl::GLShareGroup> share_group, 65 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 66 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 67 int32_t client_id, 68 uint64_t client_tracing_id, 69 bool is_gpu_host, 70 ImageDecodeAcceleratorWorker* image_decode_accelerator_worker); 71 72 // Init() sets up the underlying IPC channel. Use a separate method because 73 // we don't want to do that in tests. 74 void Init(IPC::ChannelHandle channel_handle, 75 base::WaitableEvent* shutdown_event); 76 77 void InitForTesting(IPC::Channel* channel); 78 79 base::WeakPtr<GpuChannel> AsWeakPtr(); 80 81 void SetUnhandledMessageListener(IPC::Listener* listener); 82 83 // Get the GpuChannelManager that owns this channel. gpu_channel_manager()84 GpuChannelManager* gpu_channel_manager() const { 85 return gpu_channel_manager_; 86 } 87 scheduler()88 Scheduler* scheduler() const { return scheduler_; } 89 sync_point_manager()90 SyncPointManager* sync_point_manager() const { return sync_point_manager_; } 91 image_manager()92 gles2::ImageManager* image_manager() const { return image_manager_.get(); } 93 task_runner()94 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const { 95 return task_runner_; 96 } 97 98 base::ProcessId GetClientPID() const; 99 bool IsConnected() const; 100 client_id()101 int client_id() const { return client_id_; } 102 client_tracing_id()103 uint64_t client_tracing_id() const { return client_tracing_id_; } 104 io_task_runner()105 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner() const { 106 return io_task_runner_; 107 } 108 is_gpu_host()109 bool is_gpu_host() const { return is_gpu_host_; } 110 111 // IPC::Listener implementation: 112 bool OnMessageReceived(const IPC::Message& msg) override; 113 void OnChannelConnected(int32_t peer_pid) override; 114 void OnChannelError() override; 115 116 // IPC::Sender implementation: 117 bool Send(IPC::Message* msg) override; 118 119 void AddFilter(IPC::MessageFilter* filter); 120 void RemoveFilter(IPC::MessageFilter* filter); 121 122 void OnCommandBufferScheduled(CommandBufferStub* stub); 123 void OnCommandBufferDescheduled(CommandBufferStub* stub); 124 share_group()125 gl::GLShareGroup* share_group() const { return share_group_.get(); } 126 127 CommandBufferStub* LookupCommandBuffer(int32_t route_id); 128 129 bool HasActiveWebGLContext() const; 130 void MarkAllContextsLost(); 131 132 // Called to add a listener for a particular message routing ID. 133 // Returns true if succeeded. 134 bool AddRoute(int32_t route_id, 135 SequenceId sequence_id, 136 IPC::Listener* listener); 137 138 // Called to remove a listener for a particular message routing ID. 139 void RemoveRoute(int32_t route_id); 140 141 void CacheShader(const std::string& key, const std::string& shader); 142 143 uint64_t GetMemoryUsage() const; 144 145 scoped_refptr<gl::GLImage> CreateImageForGpuMemoryBuffer( 146 gfx::GpuMemoryBufferHandle handle, 147 const gfx::Size& size, 148 gfx::BufferFormat format, 149 SurfaceHandle surface_handle); 150 151 void HandleMessage(const IPC::Message& msg); 152 153 // Some messages such as WaitForGetOffsetInRange and WaitForTokenInRange are 154 // processed as soon as possible because the client is blocked until they 155 // are completed. 156 void HandleOutOfOrderMessage(const IPC::Message& msg); 157 158 void HandleMessageForTesting(const IPC::Message& msg); 159 160 ImageDecodeAcceleratorStub* GetImageDecodeAcceleratorStub() const; 161 162 #if defined(OS_ANDROID) 163 const CommandBufferStub* GetOneStub() const; 164 165 // Called by StreamTexture to remove the GpuChannel's reference to the 166 // StreamTexture. 167 void DestroyStreamTexture(int32_t stream_id); 168 #endif 169 shared_image_stub()170 SharedImageStub* shared_image_stub() const { 171 return shared_image_stub_.get(); 172 } 173 174 private: 175 // Takes ownership of the renderer process handle. 176 GpuChannel(GpuChannelManager* gpu_channel_manager, 177 Scheduler* scheduler, 178 SyncPointManager* sync_point_manager, 179 scoped_refptr<gl::GLShareGroup> share_group, 180 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 181 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 182 int32_t client_id, 183 uint64_t client_tracing_id, 184 bool is_gpu_host, 185 ImageDecodeAcceleratorWorker* image_decode_accelerator_worker); 186 187 bool OnControlMessageReceived(const IPC::Message& msg); 188 189 void HandleMessageHelper(const IPC::Message& msg); 190 191 // Message handlers for control messages. 192 void OnCreateCommandBuffer(const GPUCreateCommandBufferConfig& init_params, 193 int32_t route_id, 194 base::UnsafeSharedMemoryRegion shared_state_shm, 195 gpu::ContextResult* result, 196 gpu::Capabilities* capabilities); 197 void OnDestroyCommandBuffer(int32_t route_id); 198 void OnCreateStreamTexture(int32_t stream_id, bool* succeeded); 199 bool CreateSharedImageStub(); 200 201 std::unique_ptr<IPC::SyncChannel> sync_channel_; // nullptr in tests. 202 IPC::Sender* channel_; // Same as sync_channel_.get() except in tests. 203 204 base::ProcessId peer_pid_ = base::kNullProcessId; 205 206 // The message filter on the io thread. 207 scoped_refptr<GpuChannelMessageFilter> filter_; 208 209 // Map of routing id to command buffer stub. 210 base::flat_map<int32_t, std::unique_ptr<CommandBufferStub>> stubs_; 211 212 // Map of stream id to scheduler sequence id. 213 base::flat_map<int32_t, SequenceId> stream_sequences_; 214 215 // The lifetime of objects of this class is managed by a GpuChannelManager. 216 // The GpuChannelManager destroy all the GpuChannels that they own when they 217 // are destroyed. So a raw pointer is safe. 218 GpuChannelManager* const gpu_channel_manager_; 219 220 Scheduler* const scheduler_; 221 222 // Sync point manager. Outlives the channel and is guaranteed to outlive the 223 // message loop. 224 SyncPointManager* const sync_point_manager_; 225 226 IPC::Listener* unhandled_message_listener_ = nullptr; 227 228 // Used to implement message routing functionality to CommandBuffer objects 229 IPC::MessageRouter router_; 230 231 // The id of the client who is on the other side of the channel. 232 const int32_t client_id_; 233 234 // The tracing ID used for memory allocations associated with this client. 235 const uint64_t client_tracing_id_; 236 237 // The task runners for the main thread and the io thread. 238 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 239 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 240 241 // The share group that all contexts associated with a particular renderer 242 // process use. 243 scoped_refptr<gl::GLShareGroup> share_group_; 244 245 std::unique_ptr<gles2::ImageManager> image_manager_; 246 std::unique_ptr<SharedImageStub> shared_image_stub_; 247 248 const bool is_gpu_host_; 249 250 #if defined(OS_ANDROID) 251 // Set of active StreamTextures. 252 base::flat_map<int32_t, scoped_refptr<StreamTexture>> stream_textures_; 253 #endif 254 255 // Member variables should appear before the WeakPtrFactory, to ensure that 256 // any WeakPtrs to Controller are invalidated before its members variable's 257 // destructors are executed, rendering them invalid. 258 base::WeakPtrFactory<GpuChannel> weak_factory_{this}; 259 260 DISALLOW_COPY_AND_ASSIGN(GpuChannel); 261 }; 262 263 } // namespace gpu 264 265 #endif // GPU_IPC_SERVICE_GPU_CHANNEL_H_ 266