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/. */
9 #include "js/RootingAPI.h"
10 #include "mozilla/dom/Nullable.h"
11 #include "mozilla/webgpu/WebGPUTypes.h"
13 #include "ObjectModel.h"
14 #include "mozilla/ipc/RawShmem.h"
21 struct GPUBufferDescriptor
;
24 enum class GPUBufferMapState
: uint8_t;
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
;
38 MappedInfo() = default;
39 MappedInfo(const MappedInfo
&) = delete;
42 class Buffer final
: public ObjectBase
, public ChildOf
<Device
> {
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
,
52 already_AddRefed
<dom::Promise
> MapAsync(uint32_t aMode
, uint64_t aOffset
,
53 const dom::Optional
<uint64_t>& aSize
,
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
);
63 uint64_t Size() const { return mSize
; }
64 uint32_t Usage() const { return mUsage
; }
65 dom::GPUBufferMapState
MapState() const;
68 Buffer(Device
* const aParent
, RawId aId
, BufferAddress aSize
, uint32_t aUsage
,
69 ipc::WritableSharedMemoryMapping
&& aShmem
);
71 Device
& GetDevice() { return *mParent
; }
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
81 const BufferAddress mSize
;
82 const uint32_t mUsage
;
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
89 std::shared_ptr
<ipc::WritableSharedMemoryMapping
> mShmem
;
93 } // namespace mozilla
95 #endif // GPU_BUFFER_H_