summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/webgpu/dawn_object.h
blob: eb608ca1f04e0b1a5ac9eb13d3097a8def11d7b9 (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
152
153
154
155
156
157
158
159
160
161
// 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"
#include "third_party/blink/renderer/platform/heap/visitor.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

#define DAWN_OBJECTS                          \
  X(BindGroup, bindGroup)                     \
  X(BindGroupLayout, bindGroupLayout)         \
  X(Buffer, buffer)                           \
  X(CommandBuffer, commandBuffer)             \
  X(CommandEncoder, commandEncoder)           \
  X(ComputePassEncoder, computePassEncoder)   \
  X(ComputePipeline, computePipeline)         \
  X(Device, device)                           \
  X(ExternalTexture, externalTexture)         \
  X(Instance, instance)                       \
  X(PipelineLayout, pipelineLayout)           \
  X(QuerySet, querySet)                       \
  X(Queue, queue)                             \
  X(RenderBundle, renderBundle)               \
  X(RenderBundleEncoder, renderBundleEncoder) \
  X(RenderPassEncoder, renderPassEncoder)     \
  X(RenderPipeline, renderPipeline)           \
  X(Sampler, sampler)                         \
  X(ShaderModule, shaderModule)               \
  X(Surface, surface)                         \
  X(SwapChain, swapChain)                     \
  X(Texture, texture)                         \
  X(TextureView, textureView)

namespace gpu {
namespace webgpu {

class WebGPUInterface;

}  // namespace webgpu
}  // namespace gpu

namespace blink {

template <typename T>
struct WGPUReleaseFn;

#define X(Name, name)                                         \
  template <>                                                 \
  struct WGPUReleaseFn<WGPU##Name> {                          \
    static constexpr void (*DawnProcTable::*fn)(WGPU##Name) = \
        &DawnProcTable::name##Release;                        \
  };
DAWN_OBJECTS
#undef X

class GPUDevice;

// 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;
  base::WeakPtr<WebGraphicsContext3DProviderWrapper> GetContextProviderWeakPtr()
      const {
    return dawn_control_client_->GetContextProviderWeakPtr();
  }
  const DawnProcTable& GetProcs() const {
    return dawn_control_client_->GetProcs();
  }

  // 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();

  // GPUObjectBase mixin implementation
  const String& label() const { return label_; }
  void setLabel(const String& value);

 private:
  scoped_refptr<DawnControlClientHolder> dawn_control_client_;
  String label_;
};

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

  WGPUDevice GetDeviceHandle();

  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),
        device_handle_(GetDeviceHandle()) {
    // All WebGPU Blink objects created directly or by the Device hold a
    // Member<GPUDevice> which keeps the device alive. However, this does not
    // enforce that the GPUDevice is finalized after all objects referencing it.
    // Add an extra ref in this constructor, and a release in the destructor to
    // ensure that the Dawn device is destroyed last.
    // TODO(enga): Investigate removing Member<GPUDevice>.
    GetProcs().deviceReference(device_handle_);
  }

  ~DawnObject() override {
    // Note: The device is released last because all child objects must be
    // destroyed first.
    (GetProcs().*WGPUReleaseFn<Handle>::fn)(handle_);
    GetProcs().deviceRelease(device_handle_);
  }

  Handle GetHandle() const { return handle_; }

 private:
  Handle const handle_;
  WGPUDevice device_handle_;
};

template <>
class DawnObject<WGPUDevice> : public DawnObjectBase {
 public:
  DawnObject(scoped_refptr<DawnControlClientHolder> dawn_control_client,
             WGPUDevice handle)
      : DawnObjectBase(dawn_control_client), handle_(handle) {}
  ~DawnObject() { GetProcs().deviceRelease(handle_); }

  WGPUDevice GetHandle() const { return handle_; }

 private:
  WGPUDevice const handle_;
};

}  // namespace blink

#undef DAWN_OBJECTS

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_