Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 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 | #ifndef DAWNNATIVE_OBJECTBASE_H_ |
| 16 | #define DAWNNATIVE_OBJECTBASE_H_ |
| 17 | |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 18 | #include "common/LinkedList.h" |
Rafael Cintron | 7e8385c | 2020-04-20 17:36:22 +0000 | [diff] [blame] | 19 | #include "common/RefCounted.h" |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 20 | #include "dawn_native/Forward.h" |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 21 | |
Brandon Jones | c95e574 | 2021-08-19 20:47:28 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 24 | namespace dawn_native { |
| 25 | |
| 26 | class DeviceBase; |
| 27 | |
| 28 | class ObjectBase : public RefCounted { |
| 29 | public: |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 30 | struct ErrorTag {}; |
| 31 | static constexpr ErrorTag kError = {}; |
| 32 | |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 33 | explicit ObjectBase(DeviceBase* device); |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 34 | ObjectBase(DeviceBase* device, ErrorTag tag); |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 35 | |
| 36 | DeviceBase* GetDevice() const; |
Corentin Wallez | a594f8f | 2019-02-13 13:09:18 +0000 | [diff] [blame] | 37 | bool IsError() const; |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 38 | |
| 39 | private: |
Loko Kung | fc5a7d4 | 2021-10-12 17:46:26 +0000 | [diff] [blame] | 40 | // Pointer to owning device. |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 41 | DeviceBase* mDevice; |
| 42 | }; |
| 43 | |
| 44 | class ApiObjectBase : public ObjectBase, public LinkNode<ApiObjectBase> { |
| 45 | public: |
| 46 | struct LabelNotImplementedTag {}; |
| 47 | static constexpr LabelNotImplementedTag kLabelNotImplemented = {}; |
Loko Kung | 2f3fe95 | 2021-10-12 18:53:57 +0000 | [diff] [blame] | 48 | struct UntrackedByDeviceTag {}; |
| 49 | static constexpr UntrackedByDeviceTag kUntrackedByDevice = {}; |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 50 | |
| 51 | ApiObjectBase(DeviceBase* device, LabelNotImplementedTag tag); |
| 52 | ApiObjectBase(DeviceBase* device, const char* label); |
| 53 | ApiObjectBase(DeviceBase* device, ErrorTag tag); |
Loko Kung | e1e9fd0 | 2021-11-01 18:14:21 +0000 | [diff] [blame] | 54 | ~ApiObjectBase() override; |
Loko Kung | 8d195d5 | 2021-09-28 15:40:01 +0000 | [diff] [blame] | 55 | |
| 56 | virtual ObjectType GetType() const = 0; |
| 57 | const std::string& GetLabel() const; |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 58 | |
Loko Kung | fc5a7d4 | 2021-10-12 17:46:26 +0000 | [diff] [blame] | 59 | // The ApiObjectBase is considered alive if it is tracked in a respective linked list owned |
| 60 | // by the owning device. |
| 61 | bool IsAlive() const; |
| 62 | |
Loko Kung | 6331f95 | 2021-12-01 18:54:40 +0000 | [diff] [blame] | 63 | // This needs to be public because it can be called from the device owning the object. |
| 64 | void Destroy(); |
Loko Kung | fc5a7d4 | 2021-10-12 17:46:26 +0000 | [diff] [blame] | 65 | |
Brandon Jones | c95e574 | 2021-08-19 20:47:28 +0000 | [diff] [blame] | 66 | // Dawn API |
| 67 | void APISetLabel(const char* label); |
Rafael Cintron | c64242d | 2020-04-06 18:20:02 +0000 | [diff] [blame] | 68 | |
Loko Kung | fc5a7d4 | 2021-10-12 17:46:26 +0000 | [diff] [blame] | 69 | protected: |
Loko Kung | bf9b3cc | 2021-10-19 22:43:13 +0000 | [diff] [blame] | 70 | // Overriding of the RefCounted's DeleteThis function ensures that instances of objects |
Loko Kung | e9c84c0 | 2021-11-11 01:39:52 +0000 | [diff] [blame] | 71 | // always call their derived class implementation of Destroy prior to the derived |
Loko Kung | bf9b3cc | 2021-10-19 22:43:13 +0000 | [diff] [blame] | 72 | // class being destroyed. This guarantees that when ApiObjects' reference counts drop to 0, |
| 73 | // then the underlying backend's Destroy calls are executed. We cannot naively put the call |
Loko Kung | e9c84c0 | 2021-11-11 01:39:52 +0000 | [diff] [blame] | 74 | // to Destroy in the destructor of this class because it calls DestroyImpl |
Loko Kung | bf9b3cc | 2021-10-19 22:43:13 +0000 | [diff] [blame] | 75 | // which is a virtual function often implemented in the Derived class which would already |
| 76 | // have been destroyed by the time ApiObject's destructor is called by C++'s destruction |
| 77 | // order. Note that some classes like BindGroup may override the DeleteThis function again, |
| 78 | // and they should ensure that their overriding versions call this underlying version |
| 79 | // somewhere. |
| 80 | void DeleteThis() override; |
Loko Kung | fc5a7d4 | 2021-10-12 17:46:26 +0000 | [diff] [blame] | 81 | void TrackInDevice(); |
Loko Kung | ff9a1f7 | 2021-11-01 23:42:52 +0000 | [diff] [blame] | 82 | |
Loko Kung | 6331f95 | 2021-12-01 18:54:40 +0000 | [diff] [blame] | 83 | // Sub-classes may override this function multiple times. Whenever overriding this function, |
| 84 | // however, users should be sure to call their parent's version in the new override to make |
| 85 | // sure that all destroy functionality is kept. This function is guaranteed to only be |
| 86 | // called once through the exposed Destroy function. |
| 87 | virtual void DestroyImpl() = 0; |
Loko Kung | fc5a7d4 | 2021-10-12 17:46:26 +0000 | [diff] [blame] | 88 | |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 89 | private: |
Brandon Jones | c95e574 | 2021-08-19 20:47:28 +0000 | [diff] [blame] | 90 | virtual void SetLabelImpl(); |
| 91 | |
Brandon Jones | c95e574 | 2021-08-19 20:47:28 +0000 | [diff] [blame] | 92 | std::string mLabel; |
Corentin Wallez | 9e4518b | 2018-10-15 12:54:30 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace dawn_native |
| 96 | |
| 97 | #endif // DAWNNATIVE_OBJECTBASE_H_ |