gpu_client.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 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 COMPONENTS_VIZ_HOST_GPU_CLIENT_H_
  5. #define COMPONENTS_VIZ_HOST_GPU_CLIENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/process/process_handle.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "components/viz/host/gpu_client_delegate.h"
  13. #include "components/viz/host/gpu_host_impl.h"
  14. #include "components/viz/host/viz_host_export.h"
  15. #include "gpu/ipc/common/gpu_disk_cache_type.h"
  16. #include "mojo/public/cpp/bindings/pending_receiver.h"
  17. #include "mojo/public/cpp/bindings/receiver_set.h"
  18. #include "services/viz/public/mojom/gpu.mojom.h"
  19. namespace viz {
  20. class VIZ_HOST_EXPORT GpuClient : public mojom::GpuMemoryBufferFactory,
  21. public mojom::Gpu {
  22. public:
  23. using ConnectionErrorHandlerClosure =
  24. base::OnceCallback<void(GpuClient* client)>;
  25. // GpuClient must be destroyed on the thread associated with |task_runner|.
  26. GpuClient(std::unique_ptr<GpuClientDelegate> delegate,
  27. int client_id,
  28. uint64_t client_tracing_id,
  29. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  30. GpuClient(const GpuClient&) = delete;
  31. GpuClient& operator=(const GpuClient&) = delete;
  32. ~GpuClient() override;
  33. // This needs to be run on the thread associated with |task_runner_|.
  34. void Add(mojo::PendingReceiver<mojom::Gpu> receiver);
  35. void PreEstablishGpuChannel();
  36. // Sets the PID of the client that will use this channel once the PID is
  37. // known.
  38. void SetClientPid(base::ProcessId client_pid);
  39. // Sets/removes disk cache handle(s) that can be used by this channel.
  40. void SetDiskCacheHandle(const gpu::GpuDiskCacheHandle& handle);
  41. void RemoveDiskCacheHandles();
  42. void SetConnectionErrorHandler(
  43. ConnectionErrorHandlerClosure connection_error_handler);
  44. base::WeakPtr<GpuClient> GetWeakPtr();
  45. // mojom::GpuMemoryBufferFactory overrides:
  46. void CreateGpuMemoryBuffer(
  47. gfx::GpuMemoryBufferId id,
  48. const gfx::Size& size,
  49. gfx::BufferFormat format,
  50. gfx::BufferUsage usage,
  51. mojom::GpuMemoryBufferFactory::CreateGpuMemoryBufferCallback callback)
  52. override;
  53. void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
  54. const gpu::SyncToken& sync_token) override;
  55. void CopyGpuMemoryBuffer(gfx::GpuMemoryBufferHandle buffer_handle,
  56. base::UnsafeSharedMemoryRegion shared_memory,
  57. CopyGpuMemoryBufferCallback callback) override;
  58. // mojom::Gpu overrides:
  59. void CreateGpuMemoryBufferFactory(
  60. mojo::PendingReceiver<mojom::GpuMemoryBufferFactory> receiver) override;
  61. void EstablishGpuChannel(EstablishGpuChannelCallback callback) override;
  62. #if BUILDFLAG(IS_CHROMEOS_ASH)
  63. void CreateJpegDecodeAccelerator(
  64. mojo::PendingReceiver<chromeos_camera::mojom::MjpegDecodeAccelerator>
  65. jda_receiver) override;
  66. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  67. void CreateVideoEncodeAcceleratorProvider(
  68. mojo::PendingReceiver<media::mojom::VideoEncodeAcceleratorProvider>
  69. vea_provider_receiver) override;
  70. private:
  71. enum class ErrorReason {
  72. // OnError() is being called from the destructor.
  73. kInDestructor,
  74. // OnError() is being called because the connection was lost.
  75. kConnectionLost
  76. };
  77. void OnError(ErrorReason reason);
  78. void OnEstablishGpuChannel(mojo::ScopedMessagePipeHandle channel_handle,
  79. const gpu::GPUInfo& gpu_info,
  80. const gpu::GpuFeatureInfo& gpu_feature_info,
  81. GpuHostImpl::EstablishChannelStatus status);
  82. void OnCreateGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
  83. gfx::GpuMemoryBufferHandle handle);
  84. void ClearCallback();
  85. std::unique_ptr<GpuClientDelegate> delegate_;
  86. const int client_id_;
  87. const uint64_t client_tracing_id_;
  88. // Note that this map is placed before the ReceiverSet below, because pending
  89. // response callbacks cannot be destroyed while their originating connection
  90. // is still active.
  91. std::map<gfx::GpuMemoryBufferId, CreateGpuMemoryBufferCallback>
  92. pending_create_callbacks_;
  93. mojo::ReceiverSet<mojom::GpuMemoryBufferFactory>
  94. gpu_memory_buffer_factory_receivers_;
  95. mojo::ReceiverSet<mojom::Gpu> gpu_receivers_;
  96. bool gpu_channel_requested_ = false;
  97. EstablishGpuChannelCallback callback_;
  98. mojo::ScopedMessagePipeHandle channel_handle_;
  99. gpu::GPUInfo gpu_info_;
  100. gpu::GpuFeatureInfo gpu_feature_info_;
  101. ConnectionErrorHandlerClosure connection_error_handler_;
  102. // |task_runner_| is associated with the thread |gpu_bindings_| is bound
  103. // on. GpuClient instance is bound to this thread, and must be destroyed on
  104. // this thread.
  105. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  106. base::WeakPtrFactory<GpuClient> weak_factory_{this};
  107. };
  108. } // namespace viz
  109. #endif // COMPONENTS_VIZ_HOST_GPU_CLIENT_H_