Bug 1863873 - Block ability to perform audio decoding outside of Utility on release...
[gecko.git] / dom / webgpu / Buffer.h
blob2f809a47686ad4b6602033cfb552690975f18d68
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef GPU_BUFFER_H_
7 #define GPU_BUFFER_H_
9 #include "js/RootingAPI.h"
10 #include "mozilla/dom/Nullable.h"
11 #include "mozilla/webgpu/WebGPUTypes.h"
12 #include "nsTArray.h"
13 #include "ObjectModel.h"
14 #include "mozilla/ipc/RawShmem.h"
15 #include <memory>
17 namespace mozilla {
18 class ErrorResult;
20 namespace dom {
21 struct GPUBufferDescriptor;
22 template <typename T>
23 class Optional;
24 enum class GPUBufferMapState : uint8_t;
25 } // namespace dom
27 namespace webgpu {
29 class Device;
31 struct MappedInfo {
32 // True if mapping is requested for writing.
33 bool mWritable = false;
34 // Populated by `GetMappedRange`.
35 nsTArray<JS::Heap<JSObject*>> mArrayBuffers;
36 BufferAddress mOffset;
37 BufferAddress mSize;
38 MappedInfo() = default;
39 MappedInfo(const MappedInfo&) = delete;
42 class Buffer final : public ObjectBase, public ChildOf<Device> {
43 public:
44 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(Buffer)
45 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Buffer)
46 GPU_DECL_JS_WRAP(Buffer)
48 static already_AddRefed<Buffer> Create(Device* aDevice, RawId aDeviceId,
49 const dom::GPUBufferDescriptor& aDesc,
50 ErrorResult& aRv);
52 already_AddRefed<dom::Promise> MapAsync(uint32_t aMode, uint64_t aOffset,
53 const dom::Optional<uint64_t>& aSize,
54 ErrorResult& aRv);
55 void GetMappedRange(JSContext* aCx, uint64_t aOffset,
56 const dom::Optional<uint64_t>& aSize,
57 JS::Rooted<JSObject*>* aObject, ErrorResult& aRv);
58 void Unmap(JSContext* aCx, ErrorResult& aRv);
59 void Destroy(JSContext* aCx, ErrorResult& aRv);
61 const RawId mId;
63 uint64_t Size() const { return mSize; }
64 uint32_t Usage() const { return mUsage; }
65 dom::GPUBufferMapState MapState() const;
67 private:
68 Buffer(Device* const aParent, RawId aId, BufferAddress aSize, uint32_t aUsage,
69 ipc::WritableSharedMemoryMapping&& aShmem);
70 virtual ~Buffer();
71 Device& GetDevice() { return *mParent; }
72 void Drop();
73 void UnmapArrayBuffers(JSContext* aCx, ErrorResult& aRv);
74 void RejectMapRequest(dom::Promise* aPromise, nsACString& message);
75 void AbortMapRequest();
76 void SetMapped(BufferAddress aOffset, BufferAddress aSize, bool aWritable);
78 // Note: we can't map a buffer with the size that don't fit into `size_t`
79 // (which may be smaller than `BufferAddress`), but general not all buffers
80 // are mapped.
81 const BufferAddress mSize;
82 const uint32_t mUsage;
83 nsString mLabel;
84 // Information about the currently active mapping.
85 Maybe<MappedInfo> mMapped;
86 RefPtr<dom::Promise> mMapRequest;
87 // mShmem does not point to a shared memory segment if the buffer is not
88 // mappable.
89 std::shared_ptr<ipc::WritableSharedMemoryMapping> mShmem;
92 } // namespace webgpu
93 } // namespace mozilla
95 #endif // GPU_BUFFER_H_