1 // Copyright 2018 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/ObjectBase.h" 16 #include "dawn_native/Device.h" 17 18 #include <mutex> 19 20 namespace dawn_native { 21 22 static constexpr uint64_t kErrorPayload = 0; 23 static constexpr uint64_t kNotErrorPayload = 1; 24 ObjectBase(DeviceBase * device)25 ObjectBase::ObjectBase(DeviceBase* device) : RefCounted(kNotErrorPayload), mDevice(device) { 26 } 27 ObjectBase(DeviceBase * device,ErrorTag)28 ObjectBase::ObjectBase(DeviceBase* device, ErrorTag) 29 : RefCounted(kErrorPayload), mDevice(device) { 30 } 31 GetDevice() const32 DeviceBase* ObjectBase::GetDevice() const { 33 return mDevice; 34 } 35 IsError() const36 bool ObjectBase::IsError() const { 37 return GetRefCountPayload() == kErrorPayload; 38 } 39 ApiObjectBase(DeviceBase * device,const char * label)40 ApiObjectBase::ApiObjectBase(DeviceBase* device, const char* label) : ObjectBase(device) { 41 if (label) { 42 mLabel = label; 43 } 44 } 45 ApiObjectBase(DeviceBase * device,ErrorTag tag)46 ApiObjectBase::ApiObjectBase(DeviceBase* device, ErrorTag tag) : ObjectBase(device, tag) { 47 } 48 ApiObjectBase(DeviceBase * device,LabelNotImplementedTag tag)49 ApiObjectBase::ApiObjectBase(DeviceBase* device, LabelNotImplementedTag tag) 50 : ObjectBase(device) { 51 } 52 ~ApiObjectBase()53 ApiObjectBase::~ApiObjectBase() { 54 ASSERT(!IsAlive()); 55 } 56 APISetLabel(const char * label)57 void ApiObjectBase::APISetLabel(const char* label) { 58 mLabel = label; 59 SetLabelImpl(); 60 } 61 GetLabel() const62 const std::string& ApiObjectBase::GetLabel() const { 63 return mLabel; 64 } 65 SetLabelImpl()66 void ApiObjectBase::SetLabelImpl() { 67 } 68 IsAlive() const69 bool ApiObjectBase::IsAlive() const { 70 return IsInList(); 71 } 72 DeleteThis()73 void ApiObjectBase::DeleteThis() { 74 Destroy(); 75 RefCounted::DeleteThis(); 76 } 77 TrackInDevice()78 void ApiObjectBase::TrackInDevice() { 79 ASSERT(GetDevice() != nullptr); 80 GetDevice()->TrackObject(this); 81 } 82 Destroy()83 void ApiObjectBase::Destroy() { 84 const std::lock_guard<std::mutex> lock(*GetDevice()->GetObjectListMutex(GetType())); 85 if (RemoveFromList()) { 86 DestroyImpl(); 87 } 88 } 89 90 } // namespace dawn_native 91