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"
|
|
|
|
|
2019-12-10 20:07:18 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
#include "mozilla/ipc/Shmem.h"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace webgpu {
|
|
|
|
|
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
|
|
|
|
if (tmp->mMapping) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mMapping->mArrayBuffer)
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2018-06-13 20:43:48 +03:00
|
|
|
|
2020-04-15 00:26:45 +03:00
|
|
|
Buffer::Mapping::Mapping(ipc::Shmem&& aShmem, JSObject* aArrayBuffer,
|
|
|
|
bool aWrite)
|
2019-12-10 20:07:18 +03:00
|
|
|
: mShmem(MakeUnique<ipc::Shmem>(std::move(aShmem))),
|
2020-04-15 00:26:45 +03:00
|
|
|
mArrayBuffer(aArrayBuffer),
|
|
|
|
mWrite(aWrite) {}
|
2019-12-10 20:07:18 +03:00
|
|
|
|
|
|
|
Buffer::Buffer(Device* const aParent, RawId aId, BufferAddress aSize)
|
|
|
|
: ChildOf(aParent), mId(aId), mSize(aSize) {
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer::~Buffer() {
|
2020-01-22 10:31:51 +03:00
|
|
|
Cleanup();
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Buffer::Cleanup() {
|
2019-12-10 20:07:18 +03:00
|
|
|
if (mParent) {
|
2020-04-07 01:29:18 +03:00
|
|
|
auto bridge = mParent->GetBridge();
|
2020-01-24 19:27:09 +03:00
|
|
|
if (bridge && bridge->IsOpen()) {
|
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-01-22 10:31:51 +03:00
|
|
|
mMapping.reset();
|
2019-12-10 20:07:18 +03:00
|
|
|
}
|
|
|
|
|
2020-04-15 00:26:45 +03:00
|
|
|
void Buffer::InitMapping(ipc::Shmem&& aShmem, JSObject* aArrayBuffer,
|
|
|
|
bool aWrite) {
|
|
|
|
mMapping.emplace(std::move(aShmem), aArrayBuffer, aWrite);
|
2019-12-10 20:07:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<dom::Promise> Buffer::MapReadAsync(ErrorResult& aRv) {
|
|
|
|
RefPtr<dom::Promise> promise = dom::Promise::Create(GetParentObject(), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (mMapping) {
|
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;
|
|
|
|
}
|
|
|
|
const auto checked = CheckedInt<size_t>(mSize);
|
|
|
|
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);
|
|
|
|
|
|
|
|
auto mappingPromise = mParent->MapBufferForReadAsync(mId, size, aRv);
|
|
|
|
if (!mappingPromise) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mappingPromise->Then(
|
|
|
|
GetMainThreadSerialEventTarget(), __func__,
|
|
|
|
[promise, size, self](ipc::Shmem&& aShmem) {
|
|
|
|
MOZ_ASSERT(aShmem.Size<uint8_t>() == size);
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(self->GetParentObject())) {
|
2020-02-03 23:37:32 +03:00
|
|
|
promise->MaybeRejectWithAbortError("Owning page was unloaded!");
|
2019-12-10 20:07:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> arrayBuffer(
|
|
|
|
jsapi.cx(),
|
|
|
|
Device::CreateExternalArrayBuffer(jsapi.cx(), size, aShmem));
|
|
|
|
if (!arrayBuffer) {
|
|
|
|
ErrorResult rv;
|
|
|
|
rv.StealExceptionFromJSContext(jsapi.cx());
|
2020-02-14 18:42:30 +03:00
|
|
|
promise->MaybeReject(std::move(rv));
|
2019-12-10 20:07:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS::Rooted<JS::Value> val(jsapi.cx(), JS::ObjectValue(*arrayBuffer));
|
2020-04-15 00:26:45 +03:00
|
|
|
self->mMapping.emplace(std::move(aShmem), arrayBuffer, false);
|
2019-12-10 20:07:18 +03:00
|
|
|
promise->MaybeResolve(val);
|
|
|
|
},
|
|
|
|
[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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Buffer::Unmap(JSContext* aCx, ErrorResult& aRv) {
|
|
|
|
if (!mMapping) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS::Rooted<JSObject*> rooted(aCx, mMapping->mArrayBuffer);
|
|
|
|
bool ok = JS::DetachArrayBuffer(aCx, rooted);
|
|
|
|
if (!ok) {
|
|
|
|
aRv.NoteJSContextException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-15 00:26:45 +03:00
|
|
|
mParent->UnmapBuffer(mId, std::move(mMapping->mShmem), mMapping->mWrite);
|
2019-12-10 20:07:18 +03:00
|
|
|
mMapping.reset();
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-13 20:43:48 +03:00
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|