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
|
|
|
#include "mozilla/dom/WebGPUBinding.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "Buffer.h"
|
|
|
|
|
2020-11-16 22:57:04 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "mozilla/ipc/Shmem.h"
|
2020-11-16 22:57:04 +03:00
|
|
|
#include "ipc/WebGPUChild.h"
|
2021-09-10 20:19:41 +03:00
|
|
|
#include "js/ArrayBuffer.h"
|
2020-10-19 20:19:12 +03:00
|
|
|
#include "js/RootingAPI.h"
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-10-02 19:46:03 +03:00
|
|
|
#include "nsWrapperCache.h"
|
2018-06-13 20:43:48 +03:00
|
|
|
#include "Device.h"
|
|
|
|
|
2022-05-09 23:41:19 +03:00
|
|
|
namespace mozilla::webgpu {
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
GPU_IMPL_JS_WRAP(Buffer)
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(Buffer, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(Buffer, Release)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Buffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Buffer)
|
2020-01-22 10:31:51 +03:00
|
|
|
tmp->Cleanup();
|
2019-12-10 20:07:18 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Buffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Buffer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
2020-10-19 20:19:12 +03:00
|
|
|
if (tmp->mMapped) {
|
|
|
|
for (uint32_t i = 0; i < tmp->mMapped->mArrayBuffers.Length(); ++i) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(
|
|
|
|
mMapped->mArrayBuffers[i])
|
|
|
|
}
|
2019-12-10 20:07:18 +03:00
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2021-04-17 01:32:44 +03:00
|
|
|
Buffer::Buffer(Device* const aParent, RawId aId, BufferAddress aSize,
|
|
|
|
bool aMappable)
|
|
|
|
: ChildOf(aParent), mId(aId), mSize(aSize), mMappable(aMappable) {
|
2019-12-10 20:07:18 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer::~Buffer() {
|
2020-01-22 10:31:51 +03:00
|
|
|
Cleanup();
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Buffer::Cleanup() {
|
2020-10-19 20:19:12 +03:00
|
|
|
if (mValid && mParent) {
|
|
|
|
mValid = false;
|
2022-05-17 17:14:12 +03:00
|
|
|
|
|
|
|
if (mMapped && !mMapped->mArrayBuffers.IsEmpty()) {
|
|
|
|
// The array buffers could live longer than us and our shmem, so make sure
|
|
|
|
// we clear the external buffer bindings.
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
if (jsapi.Init(mParent->GetOwnerGlobal())) {
|
|
|
|
IgnoredErrorResult rv;
|
|
|
|
UnmapArrayBuffers(jsapi.cx(), rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:29:18 +03:00
|
|
|
auto bridge = mParent->GetBridge();
|
2020-01-24 19:27:09 +03:00
|
|
|
if (bridge && bridge->IsOpen()) {
|
2021-08-14 09:11:58 +03:00
|
|
|
// Note: even if the buffer is considered mapped,
|
|
|
|
// the shmem may be empty before the mapAsync callback
|
|
|
|
// is resolved.
|
|
|
|
if (mMapped && mMapped->mShmem.IsReadable()) {
|
|
|
|
// Note: if the bridge is closed, all associated shmems are already
|
|
|
|
// deleted.
|
|
|
|
bridge->DeallocShmem(mMapped->mShmem);
|
|
|
|
}
|
2020-03-23 10:54:08 +03:00
|
|
|
bridge->SendBufferDestroy(mId);
|
2020-01-24 19:27:09 +03:00
|
|
|
}
|
2019-12-10 20:07:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:19:12 +03:00
|
|
|
void Buffer::SetMapped(ipc::Shmem&& aShmem, bool aWritable) {
|
|
|
|
MOZ_ASSERT(!mMapped);
|
|
|
|
mMapped.emplace();
|
|
|
|
mMapped->mShmem = std::move(aShmem);
|
|
|
|
mMapped->mWritable = aWritable;
|
2019-12-10 20:07:18 +03:00
|
|
|
}
|
|
|
|
|
2020-10-19 20:19:12 +03:00
|
|
|
already_AddRefed<dom::Promise> Buffer::MapAsync(
|
|
|
|
uint32_t aMode, uint64_t aOffset, const dom::Optional<uint64_t>& aSize,
|
|
|
|
ErrorResult& aRv) {
|
2019-12-10 20:07:18 +03:00
|
|
|
RefPtr<dom::Promise> promise = dom::Promise::Create(GetParentObject(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-19 20:19:12 +03:00
|
|
|
if (mMapped) {
|
2020-02-03 23:19:11 +03:00
|
|
|
aRv.ThrowInvalidStateError("Unable to map a buffer that is already mapped");
|
2019-12-10 20:07:18 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-04-17 01:32:44 +03:00
|
|
|
if (!mMappable) {
|
|
|
|
aRv.ThrowInvalidStateError("Unable to map a buffer that is not mappable");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-19 20:19:12 +03:00
|
|
|
// Initialize with a dummy shmem, it will become real after the promise is
|
|
|
|
// resolved.
|
|
|
|
SetMapped(ipc::Shmem(), aMode == dom::GPUMapMode_Binding::WRITE);
|
|
|
|
|
|
|
|
const auto checked = aSize.WasPassed() ? CheckedInt<size_t>(aSize.Value())
|
|
|
|
: CheckedInt<size_t>(mSize) - aOffset;
|
2019-12-10 20:07:18 +03:00
|
|
|
if (!checked.isValid()) {
|
2020-03-07 00:04:58 +03:00
|
|
|
aRv.ThrowRangeError("Mapped size is too large");
|
2019-12-10 20:07:18 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& size = checked.value();
|
|
|
|
RefPtr<Buffer> self(this);
|
|
|
|
|
2020-10-19 20:19:12 +03:00
|
|
|
auto mappingPromise = mParent->MapBufferAsync(mId, aMode, aOffset, size, aRv);
|
2019-12-10 20:07:18 +03:00
|
|
|
if (!mappingPromise) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mappingPromise->Then(
|
2022-02-10 01:01:48 +03:00
|
|
|
GetCurrentSerialEventTarget(), __func__,
|
2020-10-19 20:19:12 +03:00
|
|
|
[promise, self](ipc::Shmem&& aShmem) {
|
|
|
|
self->mMapped->mShmem = std::move(aShmem);
|
|
|
|
promise->MaybeResolve(0);
|
2019-12-10 20:07:18 +03:00
|
|
|
},
|
|
|
|
[promise](const ipc::ResponseRejectReason&) {
|
2020-02-03 23:37:32 +03:00
|
|
|
promise->MaybeRejectWithAbortError("Internal communication error!");
|
2019-12-10 20:07:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:19:12 +03:00
|
|
|
void Buffer::GetMappedRange(JSContext* aCx, uint64_t aOffset,
|
|
|
|
const dom::Optional<uint64_t>& aSize,
|
|
|
|
JS::Rooted<JSObject*>* aObject, ErrorResult& aRv) {
|
|
|
|
const auto checkedOffset = CheckedInt<size_t>(aOffset);
|
|
|
|
const auto checkedSize = aSize.WasPassed()
|
|
|
|
? CheckedInt<size_t>(aSize.Value())
|
|
|
|
: CheckedInt<size_t>(mSize) - aOffset;
|
2022-05-17 17:14:12 +03:00
|
|
|
const auto checkedMinBufferSize = checkedOffset + checkedSize;
|
|
|
|
if (!checkedOffset.isValid() || !checkedSize.isValid() ||
|
|
|
|
!checkedMinBufferSize.isValid()) {
|
2020-10-19 20:19:12 +03:00
|
|
|
aRv.ThrowRangeError("Invalid mapped range");
|
2020-10-19 18:08:55 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-10-19 20:19:12 +03:00
|
|
|
if (!mMapped || !mMapped->IsReady()) {
|
|
|
|
aRv.ThrowInvalidStateError("Buffer is not mapped");
|
|
|
|
return;
|
|
|
|
}
|
2022-05-17 17:14:12 +03:00
|
|
|
if (checkedMinBufferSize.value() > mMapped->mShmem.Size<uint8_t>()) {
|
|
|
|
aRv.ThrowOperationError("Mapped range exceeds buffer size");
|
|
|
|
return;
|
|
|
|
}
|
2020-10-19 20:19:12 +03:00
|
|
|
|
|
|
|
auto* const arrayBuffer = mParent->CreateExternalArrayBuffer(
|
|
|
|
aCx, checkedOffset.value(), checkedSize.value(), mMapped->mShmem);
|
|
|
|
if (!arrayBuffer) {
|
2019-12-10 20:07:18 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
2020-10-19 20:19:12 +03:00
|
|
|
|
|
|
|
aObject->set(arrayBuffer);
|
|
|
|
mMapped->mArrayBuffers.AppendElement(*aObject);
|
|
|
|
}
|
|
|
|
|
2022-05-17 17:14:12 +03:00
|
|
|
void Buffer::UnmapArrayBuffers(JSContext* aCx, ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(mMapped);
|
2020-10-19 20:19:12 +03:00
|
|
|
|
2022-05-17 17:14:12 +03:00
|
|
|
bool detachedArrayBuffers = true;
|
2020-10-19 20:19:12 +03:00
|
|
|
for (const auto& arrayBuffer : mMapped->mArrayBuffers) {
|
|
|
|
JS::Rooted<JSObject*> rooted(aCx, arrayBuffer);
|
2022-05-17 17:14:12 +03:00
|
|
|
if (!JS::DetachArrayBuffer(aCx, rooted)) {
|
|
|
|
detachedArrayBuffers = false;
|
2020-10-19 20:19:12 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-17 17:14:12 +03:00
|
|
|
mMapped->mArrayBuffers.Clear();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!detachedArrayBuffers)) {
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Buffer::Unmap(JSContext* aCx, ErrorResult& aRv) {
|
|
|
|
if (!mMapped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnmapArrayBuffers(aCx, aRv);
|
2021-04-17 01:32:44 +03:00
|
|
|
mParent->UnmapBuffer(mId, std::move(mMapped->mShmem), mMapped->mWritable,
|
|
|
|
mMappable);
|
2020-10-19 20:19:12 +03:00
|
|
|
mMapped.reset();
|
2019-12-10 20:07:18 +03:00
|
|
|
}
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2020-04-16 22:28:22 +03:00
|
|
|
void Buffer::Destroy() {
|
|
|
|
// TODO: we don't have to implement it right now, but it's used by the
|
|
|
|
// examples
|
|
|
|
}
|
|
|
|
|
2022-05-09 23:41:19 +03:00
|
|
|
} // namespace mozilla::webgpu
|