2018-10-15 05:54:30 -07:00
|
|
|
// Copyright 2018 The Dawn Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-04-19 13:38:44 -07:00
|
|
|
#include <mutex>
|
2023-05-26 03:32:17 -07:00
|
|
|
#include <utility>
|
2022-04-19 13:38:44 -07:00
|
|
|
|
2023-04-26 14:19:29 -07:00
|
|
|
#include "absl/strings/str_format.h"
|
2022-02-04 09:07:46 -08:00
|
|
|
#include "dawn/native/Device.h"
|
2022-05-01 07:40:55 -07:00
|
|
|
#include "dawn/native/ObjectBase.h"
|
2023-04-26 14:19:29 -07:00
|
|
|
#include "dawn/native/ObjectType_autogen.h"
|
2021-09-28 08:40:01 -07:00
|
|
|
|
2022-01-12 01:17:35 -08:00
|
|
|
namespace dawn::native {
|
2018-10-15 05:54:30 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
static constexpr uint64_t kErrorPayload = 0;
|
|
|
|
static constexpr uint64_t kNotErrorPayload = 1;
|
2019-11-19 06:35:10 -08:00
|
|
|
|
2022-05-31 13:55:39 -07:00
|
|
|
ErrorMonad::ErrorMonad() : RefCounted(kNotErrorPayload) {}
|
|
|
|
ErrorMonad::ErrorMonad(ErrorTag) : RefCounted(kErrorPayload) {}
|
2021-08-19 13:47:28 -07:00
|
|
|
|
2022-05-31 13:55:39 -07:00
|
|
|
bool ErrorMonad::IsError() const {
|
|
|
|
return GetRefCountPayload() == kErrorPayload;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectBase::ObjectBase(DeviceBase* device) : ErrorMonad(), mDevice(device) {}
|
|
|
|
|
|
|
|
ObjectBase::ObjectBase(DeviceBase* device, ErrorTag) : ErrorMonad(kError), mDevice(device) {}
|
2021-08-19 13:47:28 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
DeviceBase* ObjectBase::GetDevice() const {
|
2022-05-20 09:57:01 -07:00
|
|
|
return mDevice.Get();
|
2022-05-01 07:40:55 -07:00
|
|
|
}
|
2018-10-15 05:54:30 -07:00
|
|
|
|
2022-09-15 14:06:51 -07:00
|
|
|
void ApiObjectList::Track(ApiObjectBase* object) {
|
|
|
|
if (mMarkedDestroyed) {
|
|
|
|
object->DestroyImpl();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(mMutex);
|
|
|
|
mObjects.Prepend(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApiObjectList::Untrack(ApiObjectBase* object) {
|
|
|
|
std::lock_guard<std::mutex> lock(mMutex);
|
|
|
|
return object->RemoveFromList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiObjectList::Destroy() {
|
|
|
|
std::lock_guard<std::mutex> lock(mMutex);
|
|
|
|
mMarkedDestroyed = true;
|
|
|
|
while (!mObjects.empty()) {
|
|
|
|
auto* head = mObjects.head();
|
|
|
|
bool removed = head->RemoveFromList();
|
|
|
|
ASSERT(removed);
|
|
|
|
head->value()->DestroyImpl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
ApiObjectBase::ApiObjectBase(DeviceBase* device, const char* label) : ObjectBase(device) {
|
|
|
|
if (label) {
|
|
|
|
mLabel = label;
|
2021-09-28 08:40:01 -07:00
|
|
|
}
|
2022-05-01 07:40:55 -07:00
|
|
|
}
|
2021-09-28 08:40:01 -07:00
|
|
|
|
2023-04-20 16:35:14 -07:00
|
|
|
ApiObjectBase::ApiObjectBase(DeviceBase* device, ErrorTag tag, const char* label)
|
|
|
|
: ObjectBase(device, tag) {
|
|
|
|
if (label) {
|
|
|
|
mLabel = label;
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 08:40:01 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
ApiObjectBase::ApiObjectBase(DeviceBase* device, LabelNotImplementedTag tag) : ObjectBase(device) {}
|
2021-09-28 08:40:01 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
ApiObjectBase::~ApiObjectBase() {
|
|
|
|
ASSERT(!IsAlive());
|
|
|
|
}
|
2021-10-12 10:46:26 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
void ApiObjectBase::APISetLabel(const char* label) {
|
2023-05-26 03:32:17 -07:00
|
|
|
SetLabel(label);
|
2022-05-01 07:40:55 -07:00
|
|
|
}
|
2021-08-19 13:47:28 -07:00
|
|
|
|
2023-04-24 10:33:37 -07:00
|
|
|
void ApiObjectBase::APIRelease() {
|
|
|
|
// TODO(crbug.com/dawn/1769): We have to lock the entire APIRelease() method.
|
|
|
|
// This is because some objects are cached as raw pointers by the device. And the cache lookup
|
|
|
|
// would have been racing with the ref count's decrement here if there had not been any locking
|
|
|
|
// in place. This is temporary solution until we improve the cache's implementation.
|
|
|
|
auto deviceLock(GetDevice()->GetScopedLockSafeForDelete());
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2023-05-26 03:32:17 -07:00
|
|
|
void ApiObjectBase::SetLabel(std::string label) {
|
|
|
|
mLabel = std::move(label);
|
|
|
|
SetLabelImpl();
|
|
|
|
}
|
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
const std::string& ApiObjectBase::GetLabel() const {
|
|
|
|
return mLabel;
|
|
|
|
}
|
2021-09-28 08:40:01 -07:00
|
|
|
|
2023-04-26 14:19:29 -07:00
|
|
|
void ApiObjectBase::FormatLabel(absl::FormatSink* s) const {
|
|
|
|
s->Append(ObjectTypeAsString(GetType()));
|
|
|
|
if (!mLabel.empty()) {
|
|
|
|
s->Append(absl::StrFormat(" \"%s\"", mLabel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
void ApiObjectBase::SetLabelImpl() {}
|
2021-08-19 13:47:28 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
bool ApiObjectBase::IsAlive() const {
|
|
|
|
return IsInList();
|
|
|
|
}
|
2021-10-12 10:46:26 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
void ApiObjectBase::DeleteThis() {
|
|
|
|
Destroy();
|
|
|
|
RefCounted::DeleteThis();
|
|
|
|
}
|
2021-10-19 15:43:13 -07:00
|
|
|
|
2023-04-05 12:35:07 -07:00
|
|
|
void ApiObjectBase::LockAndDeleteThis() {
|
|
|
|
auto deviceLock(GetDevice()->GetScopedLockSafeForDelete());
|
|
|
|
DeleteThis();
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:06:51 -07:00
|
|
|
ApiObjectList* ApiObjectBase::GetObjectTrackingList() {
|
2022-05-01 07:40:55 -07:00
|
|
|
ASSERT(GetDevice() != nullptr);
|
2022-09-15 14:06:51 -07:00
|
|
|
return GetDevice()->GetObjectTrackingList(GetType());
|
2022-05-01 07:40:55 -07:00
|
|
|
}
|
2021-10-12 10:46:26 -07:00
|
|
|
|
2022-05-01 07:40:55 -07:00
|
|
|
void ApiObjectBase::Destroy() {
|
2022-09-15 14:06:51 -07:00
|
|
|
if (!IsAlive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ApiObjectList* list = GetObjectTrackingList();
|
|
|
|
ASSERT(list != nullptr);
|
|
|
|
if (list->Untrack(this)) {
|
2022-05-01 07:40:55 -07:00
|
|
|
DestroyImpl();
|
2021-10-12 10:46:26 -07:00
|
|
|
}
|
2022-05-01 07:40:55 -07:00
|
|
|
}
|
2021-10-12 10:46:26 -07:00
|
|
|
|
2022-01-12 01:17:35 -08:00
|
|
|
} // namespace dawn::native
|