#ifndef COMPONENTS_VIZ_HOST_GPU_CLIENT_H_
#define COMPONENTS_VIZ_HOST_GPU_CLIENT_H_
#include <map>
#include <memory>
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process_handle.h"
#include "base/task/single_thread_task_runner.h"
#include "components/viz/host/gpu_client_delegate.h"
#include "components/viz/host/gpu_host_impl.h"
#include "components/viz/host/viz_host_export.h"
#include "gpu/ipc/common/gpu_disk_cache_type.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/viz/public/mojom/gpu.mojom.h"
#include "services/webnn/public/mojom/webnn_context_provider.mojom.h"
namespace viz {
class VIZ_HOST_EXPORT GpuClient : public mojom::Gpu {
public:
using ConnectionErrorHandlerClosure =
base::OnceCallback<void(GpuClient* client)>;
GpuClient(std::unique_ptr<GpuClientDelegate> delegate,
int client_id,
uint64_t client_tracing_id,
bool enable_extra_handles_validation,
scoped_refptr<base::SingleThreadTaskRunner> task_runner);
GpuClient(const GpuClient&) = delete;
GpuClient& operator=(const GpuClient&) = delete;
~GpuClient() override;
void Add(mojo::PendingReceiver<mojom::Gpu> receiver);
void PreEstablishGpuChannel();
void SetClientPid(base::ProcessId client_pid);
void SetDiskCacheHandle(const gpu::GpuDiskCacheHandle& handle);
void RemoveDiskCacheHandles();
base::WeakPtr<GpuClient> GetWeakPtr();
void BindWebNNContextProvider(
mojo::PendingReceiver<webnn::mojom::WebNNContextProvider> receiver);
void EstablishGpuChannel(EstablishGpuChannelCallback callback) override;
void SetEstablishGpuChannelCallbackForTesting(
base::OnceCallback<void(bool)> callback);
#if BUILDFLAG(IS_CHROMEOS)
void CreateJpegDecodeAccelerator(
mojo::PendingReceiver<chromeos_camera::mojom::MjpegDecodeAccelerator>
jda_receiver) override;
#endif
void CreateVideoEncodeAcceleratorProvider(
mojo::PendingReceiver<media::mojom::VideoEncodeAcceleratorProvider>
vea_provider_receiver) override;
private:
enum class ErrorReason {
kInDestructor,
kConnectionLost
};
void OnError(ErrorReason reason);
void OnEstablishGpuChannel(
mojo::ScopedMessagePipeHandle channel_handle,
const gpu::GPUInfo& gpu_info,
const gpu::GpuFeatureInfo& gpu_feature_info,
const gpu::SharedImageCapabilities& shared_image_capabilities,
GpuHostImpl::EstablishChannelStatus status);
void ClearCallback();
std::unique_ptr<GpuClientDelegate> delegate_;
const int client_id_;
const uint64_t client_tracing_id_;
const bool enable_extra_handles_validation_;
mojo::ReceiverSet<mojom::Gpu> gpu_receivers_;
bool gpu_channel_requested_ = false;
EstablishGpuChannelCallback callback_;
base::OnceCallback<void(bool)> callback_for_testing_;
mojo::ScopedMessagePipeHandle channel_handle_;
gpu::GPUInfo gpu_info_;
gpu::GpuFeatureInfo gpu_feature_info_;
gpu::SharedImageCapabilities shared_image_capabilities_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::WeakPtrFactory<GpuClient> weak_factory_{this};
};
}
#endif