blob: 5c9162874855de4fad12e4eb38e5dd95d996eca7 [file] [log] [blame]
Corentin Wallez9e4518b2018-10-15 12:54:30 +00001// 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 Kung8d195d52021-09-28 15:40:01 +000018#include "common/LinkedList.h"
Rafael Cintron7e8385c2020-04-20 17:36:22 +000019#include "common/RefCounted.h"
Loko Kung8d195d52021-09-28 15:40:01 +000020#include "dawn_native/Forward.h"
Corentin Wallez9e4518b2018-10-15 12:54:30 +000021
Brandon Jonesc95e5742021-08-19 20:47:28 +000022#include <string>
23
Corentin Wallez9e4518b2018-10-15 12:54:30 +000024namespace dawn_native {
25
26 class DeviceBase;
27
28 class ObjectBase : public RefCounted {
29 public:
Corentin Walleza594f8f2019-02-13 13:09:18 +000030 struct ErrorTag {};
31 static constexpr ErrorTag kError = {};
32
Loko Kung8d195d52021-09-28 15:40:01 +000033 explicit ObjectBase(DeviceBase* device);
Corentin Walleza594f8f2019-02-13 13:09:18 +000034 ObjectBase(DeviceBase* device, ErrorTag tag);
Corentin Wallez9e4518b2018-10-15 12:54:30 +000035
36 DeviceBase* GetDevice() const;
Corentin Walleza594f8f2019-02-13 13:09:18 +000037 bool IsError() const;
Loko Kung8d195d52021-09-28 15:40:01 +000038
39 private:
Loko Kungfc5a7d42021-10-12 17:46:26 +000040 // Pointer to owning device.
Loko Kung8d195d52021-09-28 15:40:01 +000041 DeviceBase* mDevice;
42 };
43
44 class ApiObjectBase : public ObjectBase, public LinkNode<ApiObjectBase> {
45 public:
46 struct LabelNotImplementedTag {};
47 static constexpr LabelNotImplementedTag kLabelNotImplemented = {};
Loko Kung2f3fe952021-10-12 18:53:57 +000048 struct UntrackedByDeviceTag {};
49 static constexpr UntrackedByDeviceTag kUntrackedByDevice = {};
Loko Kung8d195d52021-09-28 15:40:01 +000050
51 ApiObjectBase(DeviceBase* device, LabelNotImplementedTag tag);
52 ApiObjectBase(DeviceBase* device, const char* label);
53 ApiObjectBase(DeviceBase* device, ErrorTag tag);
Loko Kunge1e9fd02021-11-01 18:14:21 +000054 ~ApiObjectBase() override;
Loko Kung8d195d52021-09-28 15:40:01 +000055
56 virtual ObjectType GetType() const = 0;
57 const std::string& GetLabel() const;
Corentin Wallez9e4518b2018-10-15 12:54:30 +000058
Loko Kungfc5a7d42021-10-12 17:46:26 +000059 // 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 Kung6331f952021-12-01 18:54:40 +000063 // This needs to be public because it can be called from the device owning the object.
64 void Destroy();
Loko Kungfc5a7d42021-10-12 17:46:26 +000065
Brandon Jonesc95e5742021-08-19 20:47:28 +000066 // Dawn API
67 void APISetLabel(const char* label);
Rafael Cintronc64242d2020-04-06 18:20:02 +000068
Loko Kungfc5a7d42021-10-12 17:46:26 +000069 protected:
Loko Kungbf9b3cc2021-10-19 22:43:13 +000070 // Overriding of the RefCounted's DeleteThis function ensures that instances of objects
Loko Kunge9c84c02021-11-11 01:39:52 +000071 // always call their derived class implementation of Destroy prior to the derived
Loko Kungbf9b3cc2021-10-19 22:43:13 +000072 // 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 Kunge9c84c02021-11-11 01:39:52 +000074 // to Destroy in the destructor of this class because it calls DestroyImpl
Loko Kungbf9b3cc2021-10-19 22:43:13 +000075 // 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 Kungfc5a7d42021-10-12 17:46:26 +000081 void TrackInDevice();
Loko Kungff9a1f72021-11-01 23:42:52 +000082
Loko Kung6331f952021-12-01 18:54:40 +000083 // 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 Kungfc5a7d42021-10-12 17:46:26 +000088
Corentin Wallez9e4518b2018-10-15 12:54:30 +000089 private:
Brandon Jonesc95e5742021-08-19 20:47:28 +000090 virtual void SetLabelImpl();
91
Brandon Jonesc95e5742021-08-19 20:47:28 +000092 std::string mLabel;
Corentin Wallez9e4518b2018-10-15 12:54:30 +000093 };
94
95} // namespace dawn_native
96
97#endif // DAWNNATIVE_OBJECTBASE_H_