2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-06-13 20:43:48 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
#ifndef GPU_BUFFER_H_
|
|
|
|
#define GPU_BUFFER_H_
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "js/RootingAPI.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "mozilla/dom/Nullable.h"
|
2020-10-19 20:19:12 +03:00
|
|
|
#include "mozilla/ipc/Shmem.h"
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "mozilla/webgpu/WebGPUTypes.h"
|
2021-03-25 13:19:44 +03:00
|
|
|
#include "nsTArray.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "ObjectModel.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
2021-03-25 13:19:44 +03:00
|
|
|
class ErrorResult;
|
|
|
|
|
|
|
|
namespace dom {
|
2022-08-10 18:55:05 +03:00
|
|
|
struct GPUBufferDescriptor;
|
2021-03-25 13:19:44 +03:00
|
|
|
template <typename T>
|
|
|
|
class Optional;
|
2022-08-10 18:55:05 +03:00
|
|
|
} // namespace dom
|
2021-03-25 13:19:44 +03:00
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
namespace ipc {
|
|
|
|
class Shmem;
|
|
|
|
} // namespace ipc
|
2018-06-13 20:43:48 +03:00
|
|
|
namespace webgpu {
|
|
|
|
|
|
|
|
class Device;
|
|
|
|
|
2020-10-19 20:19:12 +03:00
|
|
|
struct MappedInfo {
|
|
|
|
// True if mapping is requested for writing.
|
|
|
|
bool mWritable = false;
|
|
|
|
// Populated by `GetMappedRange`.
|
|
|
|
nsTArray<JS::Heap<JSObject*>> mArrayBuffers;
|
2022-08-10 18:55:05 +03:00
|
|
|
BufferAddress mOffset;
|
|
|
|
BufferAddress mSize;
|
2020-10-19 20:19:12 +03:00
|
|
|
MappedInfo() = default;
|
|
|
|
MappedInfo(const MappedInfo&) = delete;
|
|
|
|
};
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
class Buffer final : public ObjectBase, public ChildOf<Device> {
|
2018-06-13 20:43:48 +03:00
|
|
|
public:
|
2022-09-07 14:22:51 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(Buffer)
|
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Buffer)
|
2019-10-02 19:46:03 +03:00
|
|
|
GPU_DECL_JS_WRAP(Buffer)
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2022-08-10 18:55:05 +03:00
|
|
|
static already_AddRefed<Buffer> Create(Device* aDevice, RawId aDeviceId,
|
|
|
|
const dom::GPUBufferDescriptor& aDesc,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2022-08-10 18:55:11 +03:00
|
|
|
already_AddRefed<dom::Promise> MapAsync(uint32_t aMode, uint64_t aOffset,
|
|
|
|
const dom::Optional<uint64_t>& aSize,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
void GetMappedRange(JSContext* aCx, uint64_t aOffset,
|
|
|
|
const dom::Optional<uint64_t>& aSize,
|
|
|
|
JS::Rooted<JSObject*>* aObject, ErrorResult& aRv);
|
|
|
|
void Unmap(JSContext* aCx, ErrorResult& aRv);
|
|
|
|
void Destroy(JSContext* aCx, ErrorResult& aRv);
|
2019-12-10 20:07:18 +03:00
|
|
|
|
2020-01-24 19:27:09 +03:00
|
|
|
const RawId mId;
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
private:
|
2022-08-10 18:55:05 +03:00
|
|
|
Buffer(Device* const aParent, RawId aId, BufferAddress aSize, uint32_t aUsage,
|
|
|
|
ipc::Shmem&& aShmem);
|
2018-06-13 20:43:48 +03:00
|
|
|
virtual ~Buffer();
|
2022-08-10 18:55:07 +03:00
|
|
|
Device& GetDevice() { return *mParent; }
|
2022-08-10 18:55:09 +03:00
|
|
|
void Drop();
|
2022-05-17 17:14:12 +03:00
|
|
|
void UnmapArrayBuffers(JSContext* aCx, ErrorResult& aRv);
|
2022-08-10 18:55:05 +03:00
|
|
|
void RejectMapRequest(dom::Promise* aPromise, nsACString& message);
|
|
|
|
void AbortMapRequest();
|
2022-08-10 18:55:11 +03:00
|
|
|
void SetMapped(BufferAddress aOffset, BufferAddress aSize, bool aWritable);
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
// Note: we can't map a buffer with the size that don't fit into `size_t`
|
|
|
|
// (which may be smaller than `BufferAddress`), but general not all buffers
|
|
|
|
// are mapped.
|
|
|
|
const BufferAddress mSize;
|
2022-06-29 19:43:19 +03:00
|
|
|
const uint32_t mUsage;
|
2019-12-10 20:07:18 +03:00
|
|
|
nsString mLabel;
|
2020-10-19 20:19:12 +03:00
|
|
|
// Information about the currently active mapping.
|
|
|
|
Maybe<MappedInfo> mMapped;
|
2022-08-10 18:55:05 +03:00
|
|
|
RefPtr<dom::Promise> mMapRequest;
|
2022-08-10 18:55:03 +03:00
|
|
|
// mShmem does not point to a shared memory segment if the buffer is not
|
|
|
|
// mappable.
|
|
|
|
ipc::Shmem mShmem;
|
2018-06-13 20:43:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
#endif // GPU_BUFFER_H_
|