gpu_channel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifndef GPU_IPC_SERVICE_GPU_CHANNEL_H_
  5. #define GPU_IPC_SERVICE_GPU_CHANNEL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/callback.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/scoped_refptr.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/process/process_handle.h"
  17. #include "base/task/single_thread_task_runner.h"
  18. #include "base/unguessable_token.h"
  19. #include "build/build_config.h"
  20. #include "gpu/command_buffer/common/capabilities.h"
  21. #include "gpu/command_buffer/common/context_result.h"
  22. #include "gpu/command_buffer/service/sync_point_manager.h"
  23. #include "gpu/ipc/common/gpu_channel.mojom.h"
  24. #include "gpu/ipc/common/gpu_disk_cache_type.h"
  25. #include "gpu/ipc/service/command_buffer_stub.h"
  26. #include "gpu/ipc/service/gpu_ipc_service_export.h"
  27. #include "gpu/ipc/service/shared_image_stub.h"
  28. #include "ipc/ipc_sync_channel.h"
  29. #include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h"
  30. #include "mojo/public/cpp/bindings/pending_receiver.h"
  31. #include "ui/gfx/geometry/size.h"
  32. #include "ui/gfx/native_widget_types.h"
  33. #include "ui/gl/gl_share_group.h"
  34. #include "ui/gl/gpu_preference.h"
  35. namespace base {
  36. class WaitableEvent;
  37. }
  38. namespace gpu {
  39. class DCOMPTexture;
  40. class GpuChannelManager;
  41. class GpuChannelMessageFilter;
  42. class ImageDecodeAcceleratorStub;
  43. class ImageDecodeAcceleratorWorker;
  44. class Scheduler;
  45. class SharedImageStub;
  46. class StreamTexture;
  47. class SyncPointManager;
  48. // Encapsulates an IPC channel between the GPU process and one renderer
  49. // process. On the renderer side there's a corresponding GpuChannelHost.
  50. class GPU_IPC_SERVICE_EXPORT GpuChannel : public IPC::Listener {
  51. public:
  52. GpuChannel(const GpuChannel&) = delete;
  53. GpuChannel& operator=(const GpuChannel&) = delete;
  54. ~GpuChannel() override;
  55. static std::unique_ptr<GpuChannel> Create(
  56. GpuChannelManager* gpu_channel_manager,
  57. const base::UnguessableToken& channel_token,
  58. Scheduler* scheduler,
  59. SyncPointManager* sync_point_manager,
  60. scoped_refptr<gl::GLShareGroup> share_group,
  61. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  62. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
  63. int32_t client_id,
  64. uint64_t client_tracing_id,
  65. bool is_gpu_host,
  66. ImageDecodeAcceleratorWorker* image_decode_accelerator_worker);
  67. // Init() sets up the underlying IPC channel. Use a separate method because
  68. // we don't want to do that in tests.
  69. void Init(IPC::ChannelHandle channel_handle,
  70. base::WaitableEvent* shutdown_event);
  71. void InitForTesting(IPC::Channel* channel);
  72. base::WeakPtr<GpuChannel> AsWeakPtr();
  73. using CommandBufferMediaBinder =
  74. base::RepeatingCallback<void(CommandBufferStub*,
  75. mojo::GenericPendingAssociatedReceiver)>;
  76. void set_command_buffer_media_binder(CommandBufferMediaBinder binder) {
  77. command_buffer_media_binder_ = std::move(binder);
  78. }
  79. const CommandBufferMediaBinder& command_buffer_media_binder() const {
  80. return command_buffer_media_binder_;
  81. }
  82. // Get the GpuChannelManager that owns this channel.
  83. GpuChannelManager* gpu_channel_manager() const {
  84. return gpu_channel_manager_;
  85. }
  86. Scheduler* scheduler() const { return scheduler_; }
  87. SyncPointManager* sync_point_manager() const { return sync_point_manager_; }
  88. const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() const {
  89. return task_runner_;
  90. }
  91. void set_client_pid(base::ProcessId pid) { client_pid_ = pid; }
  92. base::ProcessId client_pid() const { return client_pid_; }
  93. int client_id() const { return client_id_; }
  94. uint64_t client_tracing_id() const { return client_tracing_id_; }
  95. const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner() const {
  96. return io_task_runner_;
  97. }
  98. bool is_gpu_host() const { return is_gpu_host_; }
  99. // IPC::Listener implementation:
  100. bool OnMessageReceived(const IPC::Message& msg) override;
  101. void OnChannelError() override;
  102. void OnCommandBufferScheduled(CommandBufferStub* stub);
  103. void OnCommandBufferDescheduled(CommandBufferStub* stub);
  104. gl::GLShareGroup* share_group() const { return share_group_.get(); }
  105. CommandBufferStub* LookupCommandBuffer(int32_t route_id);
  106. bool HasActiveWebGLContext() const;
  107. void MarkAllContextsLost();
  108. // Called to add a listener for a particular message routing ID.
  109. // Returns true if succeeded.
  110. bool AddRoute(int32_t route_id, SequenceId sequence_id);
  111. // Called to remove a listener for a particular message routing ID.
  112. void RemoveRoute(int32_t route_id);
  113. void RegisterCacheHandle(const gpu::GpuDiskCacheHandle& handle);
  114. void CacheBlob(gpu::GpuDiskCacheType type,
  115. const std::string& key,
  116. const std::string& shader);
  117. uint64_t GetMemoryUsage() const;
  118. scoped_refptr<gl::GLImage> CreateImageForGpuMemoryBuffer(
  119. gfx::GpuMemoryBufferHandle handle,
  120. const gfx::Size& size,
  121. gfx::BufferFormat format,
  122. gfx::BufferPlane plane,
  123. SurfaceHandle surface_handle);
  124. // Executes a DeferredRequest that was previously received and has now been
  125. // scheduled by the scheduler.
  126. void ExecuteDeferredRequest(mojom::DeferredRequestParamsPtr params);
  127. void WaitForTokenInRange(
  128. int32_t routing_id,
  129. int32_t start,
  130. int32_t end,
  131. mojom::GpuChannel::WaitForTokenInRangeCallback callback);
  132. void WaitForGetOffsetInRange(
  133. int32_t routing_id,
  134. uint32_t set_get_buffer_count,
  135. int32_t start,
  136. int32_t end,
  137. mojom::GpuChannel::WaitForGetOffsetInRangeCallback callback);
  138. mojom::GpuChannel& GetGpuChannelForTesting();
  139. ImageDecodeAcceleratorStub* GetImageDecodeAcceleratorStubForTesting() const;
  140. #if BUILDFLAG(IS_ANDROID)
  141. const CommandBufferStub* GetOneStub() const;
  142. bool CreateStreamTexture(
  143. int32_t stream_id,
  144. mojo::PendingAssociatedReceiver<mojom::StreamTexture> receiver);
  145. // Called by StreamTexture to remove the GpuChannel's reference to the
  146. // StreamTexture.
  147. void DestroyStreamTexture(int32_t stream_id);
  148. #endif
  149. #if BUILDFLAG(IS_WIN)
  150. bool CreateDCOMPTexture(
  151. int32_t route_id,
  152. mojo::PendingAssociatedReceiver<mojom::DCOMPTexture> receiver);
  153. // Called by DCOMPTexture to remove the GpuChannel's reference to the
  154. // DCOMPTexture.
  155. void DestroyDCOMPTexture(int32_t route_id);
  156. bool RegisterOverlayStateObserver(
  157. mojo::PendingRemote<gpu::mojom::OverlayStateObserver>
  158. promotion_hint_observer,
  159. const gpu::Mailbox& mailbox);
  160. #endif // BUILDFLAG(IS_WIN)
  161. SharedImageStub* shared_image_stub() const {
  162. return shared_image_stub_.get();
  163. }
  164. void CreateCommandBuffer(
  165. mojom::CreateCommandBufferParamsPtr init_params,
  166. int32_t routing_id,
  167. base::UnsafeSharedMemoryRegion shared_state_shm,
  168. mojo::PendingAssociatedReceiver<mojom::CommandBuffer> receiver,
  169. mojo::PendingAssociatedRemote<mojom::CommandBufferClient> client,
  170. mojom::GpuChannel::CreateCommandBufferCallback callback);
  171. void DestroyCommandBuffer(int32_t routing_id);
  172. #if BUILDFLAG(IS_FUCHSIA)
  173. void RegisterSysmemBufferCollection(const base::UnguessableToken& id,
  174. mojo::PlatformHandle token,
  175. gfx::BufferFormat format,
  176. gfx::BufferUsage usage,
  177. bool register_with_image_pipe);
  178. void ReleaseSysmemBufferCollection(const base::UnguessableToken& id);
  179. #endif // BUILDFLAG(IS_FUCHSIA)
  180. private:
  181. // Takes ownership of the renderer process handle.
  182. GpuChannel(GpuChannelManager* gpu_channel_manager,
  183. const base::UnguessableToken& channel_token,
  184. Scheduler* scheduler,
  185. SyncPointManager* sync_point_manager,
  186. scoped_refptr<gl::GLShareGroup> share_group,
  187. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  188. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
  189. int32_t client_id,
  190. uint64_t client_tracing_id,
  191. bool is_gpu_host,
  192. ImageDecodeAcceleratorWorker* image_decode_accelerator_worker);
  193. void OnDestroyCommandBuffer(int32_t route_id);
  194. // Message handlers for control messages.
  195. bool CreateSharedImageStub();
  196. std::unique_ptr<IPC::SyncChannel> sync_channel_; // nullptr in tests.
  197. raw_ptr<IPC::Sender>
  198. channel_; // Same as sync_channel_.get() except in tests.
  199. base::ProcessId client_pid_ = base::kNullProcessId;
  200. // An optional binder to handle associated interface requests from the Media
  201. // stack, targeting a specific CommandBuffer.
  202. CommandBufferMediaBinder command_buffer_media_binder_;
  203. // Map of routing id to command buffer stub.
  204. base::flat_map<int32_t, std::unique_ptr<CommandBufferStub>> stubs_;
  205. // Map of stream id to scheduler sequence id.
  206. base::flat_map<int32_t, SequenceId> stream_sequences_;
  207. // Map of disk cache type to the handle.
  208. base::flat_map<gpu::GpuDiskCacheType, gpu::GpuDiskCacheHandle> caches_;
  209. // The lifetime of objects of this class is managed by a GpuChannelManager.
  210. // The GpuChannelManager destroy all the GpuChannels that they own when they
  211. // are destroyed. So a raw pointer is safe.
  212. const raw_ptr<GpuChannelManager> gpu_channel_manager_;
  213. const raw_ptr<Scheduler> scheduler_;
  214. // Sync point manager. Outlives the channel and is guaranteed to outlive the
  215. // message loop.
  216. const raw_ptr<SyncPointManager> sync_point_manager_;
  217. // The id of the client who is on the other side of the channel.
  218. const int32_t client_id_;
  219. // The tracing ID used for memory allocations associated with this client.
  220. const uint64_t client_tracing_id_;
  221. // The task runners for the main thread and the io thread.
  222. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  223. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  224. // The share group that all contexts associated with a particular renderer
  225. // process use.
  226. scoped_refptr<gl::GLShareGroup> share_group_;
  227. std::unique_ptr<SharedImageStub> shared_image_stub_;
  228. const bool is_gpu_host_;
  229. #if BUILDFLAG(IS_ANDROID)
  230. // Set of active StreamTextures.
  231. base::flat_map<int32_t, scoped_refptr<StreamTexture>> stream_textures_;
  232. #endif
  233. #if BUILDFLAG(IS_WIN)
  234. // Set of active DCOMPTextures.
  235. base::flat_map<int32_t, scoped_refptr<DCOMPTexture>> dcomp_textures_;
  236. #endif
  237. // State shared with the IO thread. Receives all GpuChannel interface messages
  238. // and schedules tasks for them appropriately.
  239. const scoped_refptr<GpuChannelMessageFilter> filter_;
  240. // Member variables should appear before the WeakPtrFactory, to ensure that
  241. // any WeakPtrs to Controller are invalidated before its members variable's
  242. // destructors are executed, rendering them invalid.
  243. base::WeakPtrFactory<GpuChannel> weak_factory_{this};
  244. };
  245. } // namespace gpu
  246. #endif // GPU_IPC_SERVICE_GPU_CHANNEL_H_