summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/webgpu/gpu_buffer.h
blob: b7266242464755b8c9fa725459e22685f1efa625 (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
// 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_GPU_BUFFER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_BUFFER_H_

#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
#include "third_party/blink/renderer/core/typed_arrays/flexible_array_buffer_view.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_object.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"

namespace blink {

class DOMArrayBuffer;
class GPUBufferDescriptor;
class ScriptPromiseResolver;

class GPUBuffer : public DawnObject<WGPUBuffer> {
  DEFINE_WRAPPERTYPEINFO();

 public:
  static GPUBuffer* Create(GPUDevice* device,
                           const GPUBufferDescriptor* webgpu_desc);
  static std::pair<GPUBuffer*, DOMArrayBuffer*> CreateMapped(
      GPUDevice* device,
      const GPUBufferDescriptor* webgpu_desc,
      ExceptionState& exception_state);
  explicit GPUBuffer(GPUDevice* device, uint64_t size, WGPUBuffer buffer);
  ~GPUBuffer() override;

  void Trace(Visitor* visitor) override;

  // gpu_buffer.idl
  void setSubData(uint64_t dst_byte_offset,
                  const FlexibleArrayBufferView& src,
                  uint64_t src_byte_offset,
                  uint64_t byte_length,
                  ExceptionState& exception_state);
  ScriptPromise mapReadAsync(ScriptState* script_state,
                             ExceptionState& exception_state);
  ScriptPromise mapWriteAsync(ScriptState* script_state,
                              ExceptionState& exception_state);
  void unmap(ScriptState* script_state);
  void destroy(ScriptState* script_state);
  // TODO(crbug.com/877147): implement GPUBuffer.

 private:
  void OnMapAsyncCallback(ScriptPromiseResolver* resolver,
                          WGPUBufferMapAsyncStatus status,
                          void* data,
                          uint64_t data_length);
  void DetachArrayBufferForCurrentMapping(ScriptState* script_state);

  uint64_t size_;
  Member<DOMArrayBuffer> mapped_buffer_;

  DISALLOW_COPY_AND_ASSIGN(GPUBuffer);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_BUFFER_H_