summaryrefslogtreecommitdiff
path: root/chromium/components/viz/host/gpu_client.h
blob: 33f01ec43e598c80a159204a4d8487477cfbad80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_VIZ_HOST_GPU_CLIENT_H_
#define COMPONENTS_VIZ_HOST_GPU_CLIENT_H_

#include <map>
#include <memory>

#include "base/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process_handle.h"
#include "build/chromeos_buildflags.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"

namespace viz {

class VIZ_HOST_EXPORT GpuClient : public mojom::GpuMemoryBufferFactory,
                                  public mojom::Gpu {
 public:
  using ConnectionErrorHandlerClosure =
      base::OnceCallback<void(GpuClient* client)>;

  // GpuClient must be destroyed on the thread associated with |task_runner|.
  GpuClient(std::unique_ptr<GpuClientDelegate> delegate,
            int client_id,
            uint64_t client_tracing_id,
            scoped_refptr<base::SingleThreadTaskRunner> task_runner);

  GpuClient(const GpuClient&) = delete;
  GpuClient& operator=(const GpuClient&) = delete;

  ~GpuClient() override;

  // This needs to be run on the thread associated with |task_runner_|.
  void Add(mojo::PendingReceiver<mojom::Gpu> receiver);

  void PreEstablishGpuChannel();

  // Sets the PID of the client that will use this channel once the PID is
  // known.
  void SetClientPid(base::ProcessId client_pid);

  // Sets/removes disk cache handle(s) that can be used by this channel.
  void SetDiskCacheHandle(const gpu::GpuDiskCacheHandle& handle);
  void RemoveDiskCacheHandles();

  void SetConnectionErrorHandler(
      ConnectionErrorHandlerClosure connection_error_handler);

  base::WeakPtr<GpuClient> GetWeakPtr();

  // mojom::GpuMemoryBufferFactory overrides:
  void CreateGpuMemoryBuffer(
      gfx::GpuMemoryBufferId id,
      const gfx::Size& size,
      gfx::BufferFormat format,
      gfx::BufferUsage usage,
      mojom::GpuMemoryBufferFactory::CreateGpuMemoryBufferCallback callback)
      override;
  void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
                              const gpu::SyncToken& sync_token) override;
  void CopyGpuMemoryBuffer(gfx::GpuMemoryBufferHandle buffer_handle,
                           base::UnsafeSharedMemoryRegion shared_memory,
                           CopyGpuMemoryBufferCallback callback) override;
  // mojom::Gpu overrides:
  void CreateGpuMemoryBufferFactory(
      mojo::PendingReceiver<mojom::GpuMemoryBufferFactory> receiver) override;
  void EstablishGpuChannel(EstablishGpuChannelCallback callback) override;

#if BUILDFLAG(IS_CHROMEOS_ASH)
  void CreateJpegDecodeAccelerator(
      mojo::PendingReceiver<chromeos_camera::mojom::MjpegDecodeAccelerator>
          jda_receiver) override;
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
  void CreateVideoEncodeAcceleratorProvider(
      mojo::PendingReceiver<media::mojom::VideoEncodeAcceleratorProvider>
          vea_provider_receiver) override;

 private:
  enum class ErrorReason {
    // OnError() is being called from the destructor.
    kInDestructor,
    // OnError() is being called because the connection was lost.
    kConnectionLost
  };
  void OnError(ErrorReason reason);
  void OnEstablishGpuChannel(mojo::ScopedMessagePipeHandle channel_handle,
                             const gpu::GPUInfo& gpu_info,
                             const gpu::GpuFeatureInfo& gpu_feature_info,
                             GpuHostImpl::EstablishChannelStatus status);
  void OnCreateGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
                               gfx::GpuMemoryBufferHandle handle);
  void ClearCallback();

  std::unique_ptr<GpuClientDelegate> delegate_;
  const int client_id_;
  const uint64_t client_tracing_id_;

  // Note that this map is placed before the ReceiverSet below, because pending
  // response callbacks cannot be destroyed while their originating connection
  // is still active.
  std::map<gfx::GpuMemoryBufferId, CreateGpuMemoryBufferCallback>
      pending_create_callbacks_;

  mojo::ReceiverSet<mojom::GpuMemoryBufferFactory>
      gpu_memory_buffer_factory_receivers_;
  mojo::ReceiverSet<mojom::Gpu> gpu_receivers_;
  bool gpu_channel_requested_ = false;
  EstablishGpuChannelCallback callback_;
  mojo::ScopedMessagePipeHandle channel_handle_;
  gpu::GPUInfo gpu_info_;
  gpu::GpuFeatureInfo gpu_feature_info_;
  ConnectionErrorHandlerClosure connection_error_handler_;
  // |task_runner_| is associated with the thread |gpu_bindings_| is bound
  // on. GpuClient instance is bound to this thread, and must be destroyed on
  // this thread.
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  base::WeakPtrFactory<GpuClient> weak_factory_{this};
};

}  // namespace viz

#endif  // COMPONENTS_VIZ_HOST_GPU_CLIENT_H_