summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/webgpu/dawn_object.h
blob: 63c2d95f9e8221cea247a9dc65f8a9fb2db4da69 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_

#include <dawn/dawn_proc_table.h>
#include <dawn/webgpu.h>

#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/graphics/gpu/dawn_control_client_holder.h"

namespace gpu {
namespace webgpu {

class WebGPUInterface;

}  // namespace webgpu
}  // namespace gpu

namespace blink {

class GPUDevice;
class Visitor;

// This class allows objects to hold onto a DawnControlClientHolder.
// The DawnControlClientHolder is used to hold the WebGPUInterface and keep
// track of whether or not the client has been destroyed. If the client is
// destroyed, we should not call any Dawn functions.
class DawnObjectBase {
 public:
  explicit DawnObjectBase(
      scoped_refptr<DawnControlClientHolder> dawn_control_client);

  const scoped_refptr<DawnControlClientHolder>& GetDawnControlClient() const;
  bool IsDawnControlClientDestroyed() const;
  gpu::webgpu::WebGPUInterface* GetInterface() const;
  const DawnProcTable& GetProcs() const;

 private:
  scoped_refptr<DawnControlClientHolder> dawn_control_client_;
};

// This class allows objects to hold onto a DawnControlClientHolder and a
// device client id. Now one GPUDevice is related to one WebGPUSerializer in
// the client side of WebGPU context. When the GPUDevice and all the other
// WebGPU objects that are created from the GPUDevice are destroyed, this
// object will be destroyed and in the destructor of this object we will
// trigger the clean-ups to the corresponding WebGPUSerailzer and other data
// structures in the GPU process.
class DawnDeviceClientSerializerHolder
    : public RefCounted<DawnDeviceClientSerializerHolder> {
 public:
  DawnDeviceClientSerializerHolder(
      scoped_refptr<DawnControlClientHolder> dawn_control_client,
      uint64_t device_client_id);

 private:
  friend class RefCounted<DawnDeviceClientSerializerHolder>;
  friend class DeviceTreeObject;
  ~DawnDeviceClientSerializerHolder();

  scoped_refptr<DawnControlClientHolder> dawn_control_client_;
  uint64_t device_client_id_;
};

// This class is the parent of GPUDevice and all the WebGPU objects that are
// created from a GPUDevice, which holds a
// scoped_refptr<DawnDeviceClientSerializerHolder> and provides functions to
// access all the members inside it. When a GPUDevice and all the WebGPU
// objects created from it are destroyed, the refcount of
// DawnDeviceClientSerializerHolder will become 0 and the clean-ups to the
// corresponding WebGPUSerailzer and other data structures in the GPU process
// will be triggered.
class DeviceTreeObject {
 public:
  explicit DeviceTreeObject(scoped_refptr<DawnDeviceClientSerializerHolder>
                                device_client_seralizer_holder)
      : device_client_serializer_holder_(
            std::move(device_client_seralizer_holder)) {}

  const scoped_refptr<DawnControlClientHolder>& GetDawnControlClient() const;
  bool IsDawnControlClientDestroyed() const;
  gpu::webgpu::WebGPUInterface* GetInterface() const;
  const DawnProcTable& GetProcs() const;

  uint64_t GetDeviceClientID() const;

  // Ensure commands up until now on this object's parent device are flushed by
  // the end of the task.
  void EnsureFlush();

  // Flush commands up until now on this object's parent device immediately.
  void FlushNow();

 protected:
  scoped_refptr<DawnDeviceClientSerializerHolder>
      device_client_serializer_holder_;
};

class DawnObjectImpl : public ScriptWrappable, public DeviceTreeObject {
 public:
  explicit DawnObjectImpl(GPUDevice* device);
  ~DawnObjectImpl() override;

  void Trace(Visitor* visitor) const override;

 protected:
  Member<GPUDevice> device_;
};

template <typename Handle>
class DawnObject : public DawnObjectImpl {
 public:
  DawnObject(GPUDevice* device, Handle handle)
      : DawnObjectImpl(device), handle_(handle) {}
  ~DawnObject() override = default;

  Handle GetHandle() const { return handle_; }

 private:
  Handle const handle_;
};

template <>
class DawnObject<WGPUDevice> : public DeviceTreeObject {
 public:
  DawnObject(scoped_refptr<DawnControlClientHolder> dawn_control_client,
             uint64_t device_client_id,
             WGPUDevice handle)
      : DeviceTreeObject(base::MakeRefCounted<DawnDeviceClientSerializerHolder>(
            std::move(dawn_control_client),
            device_client_id)),
        handle_(handle) {}

  WGPUDevice GetHandle() const { return handle_; }

  const scoped_refptr<DawnDeviceClientSerializerHolder>&
  GetDeviceClientSerializerHolder() const {
    return device_client_serializer_holder_;
  }

 private:
  WGPUDevice const handle_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_