Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrVkResource_DEFINED |
| 9 | #define GrVkResource_DEFINED |
| 10 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/SkTHash.h" |
| 13 | #include "include/utils/SkRandom.h" |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 14 | #include <atomic> |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 15 | |
| 16 | class GrVkGpu; |
| 17 | |
| 18 | // uncomment to enable tracing of resource refs |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 19 | #ifdef SK_DEBUG |
| 20 | #define SK_TRACE_VK_RESOURCES |
| 21 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | |
| 23 | /** \class GrVkResource |
| 24 | |
| 25 | GrVkResource is the base class for Vulkan resources that may be shared by multiple |
| 26 | objects. When an existing owner wants to share a reference, it calls ref(). |
| 27 | When an owner wants to release its reference, it calls unref(). When the |
| 28 | shared object's reference count goes to zero as the result of an unref() |
| 29 | call, its (virtual) destructor is called. It is an error for the |
| 30 | destructor to be called explicitly (or via the object going out of scope on |
| 31 | the stack or calling delete) if getRefCnt() > 1. |
| 32 | |
| 33 | This is nearly identical to SkRefCntBase. The exceptions are that unref() |
| 34 | takes a GrVkGpu, and any derived classes must implement freeGPUData() and |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 35 | possibly abandonGPUData(). |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | class GrVkResource : SkNoncopyable { |
| 39 | public: |
| 40 | // Simple refCount tracing, to ensure that everything ref'ed is unref'ed. |
| 41 | #ifdef SK_TRACE_VK_RESOURCES |
egdaniel | 00db3fd | 2016-07-29 08:55:53 -0700 | [diff] [blame] | 42 | struct Hash { |
| 43 | uint32_t operator()(const GrVkResource* const& r) const { |
| 44 | SkASSERT(r); |
| 45 | return r->fKey; |
| 46 | } |
| 47 | }; |
jvanverth | d5f6e9a | 2016-07-07 08:21:48 -0700 | [diff] [blame] | 48 | |
| 49 | class Trace { |
| 50 | public: |
| 51 | ~Trace() { |
egdaniel | 00db3fd | 2016-07-29 08:55:53 -0700 | [diff] [blame] | 52 | fHashSet.foreach([](const GrVkResource* r) { |
| 53 | r->dumpInfo(); |
| 54 | }); |
| 55 | SkASSERT(0 == fHashSet.count()); |
jvanverth | d5f6e9a | 2016-07-07 08:21:48 -0700 | [diff] [blame] | 56 | } |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 57 | |
| 58 | void add(const GrVkResource* r) { |
| 59 | fHashSet.add(r); |
| 60 | } |
| 61 | |
| 62 | void remove(const GrVkResource* r) { |
| 63 | fHashSet.remove(r); |
| 64 | } |
jvanverth | d5f6e9a | 2016-07-07 08:21:48 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
egdaniel | 00db3fd | 2016-07-29 08:55:53 -0700 | [diff] [blame] | 67 | SkTHashSet<const GrVkResource*, GrVkResource::Hash> fHashSet; |
jvanverth | d5f6e9a | 2016-07-07 08:21:48 -0700 | [diff] [blame] | 68 | }; |
jvanverth | d5f6e9a | 2016-07-07 08:21:48 -0700 | [diff] [blame] | 69 | |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 70 | static std::atomic<uint32_t> fKeyCounter; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | /** Default construct, initializing the reference count to 1. |
| 74 | */ |
| 75 | GrVkResource() : fRefCnt(1) { |
| 76 | #ifdef SK_TRACE_VK_RESOURCES |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 77 | fKey = fKeyCounter.fetch_add(+1, std::memory_order_relaxed); |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame] | 78 | GetTrace()->add(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 79 | #endif |
| 80 | } |
| 81 | |
| 82 | /** Destruct, asserting that the reference count is 1. |
| 83 | */ |
| 84 | virtual ~GrVkResource() { |
| 85 | #ifdef SK_DEBUG |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 86 | auto count = this->getRefCnt(); |
| 87 | SkASSERTF(count == 1, "fRefCnt was %d", count); |
| 88 | fRefCnt.store(0); // illegal value, to catch us if we reuse after delete |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | #endif |
| 90 | } |
| 91 | |
| 92 | #ifdef SK_DEBUG |
| 93 | /** Return the reference count. Use only for debugging. */ |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 94 | int32_t getRefCnt() const { return fRefCnt.load(); } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | /** May return true if the caller is the only owner. |
| 98 | * Ensures that all previous owner's actions are complete. |
| 99 | */ |
| 100 | bool unique() const { |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 101 | // The acquire barrier is only really needed if we return true. It |
| 102 | // prevents code conditioned on the result of unique() from running |
| 103 | // until previous owners are all totally done calling unref(). |
| 104 | return 1 == fRefCnt.load(std::memory_order_acquire); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | } |
| 106 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 107 | /** Increment the reference count. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 108 | Must be balanced by a call to unref() or unrefAndFreeResources(). |
| 109 | */ |
| 110 | void ref() const { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 111 | // No barrier required. |
| 112 | SkDEBUGCODE(int newRefCount = )fRefCnt.fetch_add(+1, std::memory_order_relaxed); |
| 113 | SkASSERT(newRefCount >= 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** Decrement the reference count. If the reference count is 1 before the |
| 117 | decrement, then delete the object. Note that if this is the case, then |
| 118 | the object needs to have been allocated via new, and not on the stack. |
| 119 | Any GPU data associated with this resource will be freed before it's deleted. |
| 120 | */ |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 121 | void unref(GrVkGpu* gpu) const { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 122 | SkASSERT(gpu); |
| 123 | // A release here acts in place of all releases we "should" have been doing in ref(). |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 124 | int newRefCount = fRefCnt.fetch_add(-1, std::memory_order_acq_rel); |
| 125 | SkASSERT(newRefCount >= 0); |
| 126 | if (newRefCount == 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 127 | // Like unique(), the acquire is only needed on success, to make sure |
| 128 | // code in internal_dispose() doesn't happen before the decrement. |
| 129 | this->internal_dispose(gpu); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** Unref without freeing GPU data. Used only when we're abandoning the resource */ |
| 134 | void unrefAndAbandon() const { |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 135 | SkASSERT(this->getRefCnt() > 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 136 | // A release here acts in place of all releases we "should" have been doing in ref(). |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 137 | int newRefCount = fRefCnt.fetch_add(-1, std::memory_order_acq_rel); |
| 138 | SkASSERT(newRefCount >= 0); |
| 139 | if (newRefCount == 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 140 | // Like unique(), the acquire is only needed on success, to make sure |
| 141 | // code in internal_dispose() doesn't happen before the decrement. |
| 142 | this->internal_dispose(); |
| 143 | } |
| 144 | } |
| 145 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 146 | // Called every time this resource is added to a command buffer. |
| 147 | virtual void notifyAddedToCommandBuffer() const {} |
| 148 | // Called every time this resource is removed from a command buffer (typically because |
| 149 | // the command buffer finished execution on the GPU but also when the command buffer |
| 150 | // is abandoned.) |
| 151 | virtual void notifyRemovedFromCommandBuffer() const {} |
| 152 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 153 | #ifdef SK_DEBUG |
| 154 | void validate() const { |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 155 | SkASSERT(this->getRefCnt() > 0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 156 | } |
| 157 | #endif |
| 158 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 159 | #ifdef SK_TRACE_VK_RESOURCES |
| 160 | /** Output a human-readable dump of this resource's information |
| 161 | */ |
| 162 | virtual void dumpInfo() const = 0; |
| 163 | #endif |
| 164 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 165 | private: |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame] | 166 | #ifdef SK_TRACE_VK_RESOURCES |
| 167 | static Trace* GetTrace() { |
| 168 | static Trace kTrace; |
| 169 | return &kTrace; |
| 170 | } |
| 171 | #endif |
| 172 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 173 | /** Must be implemented by any subclasses. |
| 174 | * Deletes any Vk data associated with this resource |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 175 | */ |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 176 | virtual void freeGPUData(GrVkGpu* gpu) const = 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 177 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 178 | /** |
| 179 | * Called from unrefAndAbandon. Resources should do any necessary cleanup without freeing |
| 180 | * underlying Vk objects. This must be overridden by subclasses that themselves store |
| 181 | * GrVkResources since those resource will need to be unrefed. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 182 | */ |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 183 | virtual void abandonGPUData() const {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Called when the ref count goes to 0. Will free Vk resources. |
| 187 | */ |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 188 | void internal_dispose(GrVkGpu* gpu) const { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 189 | this->freeGPUData(gpu); |
| 190 | #ifdef SK_TRACE_VK_RESOURCES |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame] | 191 | GetTrace()->remove(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 192 | #endif |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 193 | |
| 194 | #ifdef SK_DEBUG |
| 195 | SkASSERT(0 == this->getRefCnt()); |
| 196 | fRefCnt.store(1); |
| 197 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 198 | delete this; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Internal_dispose without freeing Vk resources. Used when we've lost context. |
| 203 | */ |
| 204 | void internal_dispose() const { |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 205 | this->abandonGPUData(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 206 | #ifdef SK_TRACE_VK_RESOURCES |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame] | 207 | GetTrace()->remove(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 208 | #endif |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 209 | |
| 210 | #ifdef SK_DEBUG |
| 211 | SkASSERT(0 == this->getRefCnt()); |
| 212 | fRefCnt.store(1); |
| 213 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 214 | delete this; |
| 215 | } |
| 216 | |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 217 | mutable std::atomic<int32_t> fRefCnt; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 218 | #ifdef SK_TRACE_VK_RESOURCES |
| 219 | uint32_t fKey; |
| 220 | #endif |
| 221 | |
| 222 | typedef SkNoncopyable INHERITED; |
| 223 | }; |
| 224 | |
egdaniel | c1be9bc | 2016-07-20 08:33:00 -0700 | [diff] [blame] | 225 | // This subclass allows for recycling |
| 226 | class GrVkRecycledResource : public GrVkResource { |
| 227 | public: |
| 228 | // When recycle is called and there is only one ref left on the resource, we will signal that |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 229 | // the resource can be recycled for reuse. If the sublass (or whoever is managing this resource) |
| 230 | // decides not to recycle the objects, it is their responsibility to call unref on the object. |
egdaniel | c1be9bc | 2016-07-20 08:33:00 -0700 | [diff] [blame] | 231 | void recycle(GrVkGpu* gpu) const { |
| 232 | if (this->unique()) { |
| 233 | this->onRecycle(gpu); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 234 | } else { |
| 235 | this->unref(gpu); |
egdaniel | c1be9bc | 2016-07-20 08:33:00 -0700 | [diff] [blame] | 236 | } |
egdaniel | c1be9bc | 2016-07-20 08:33:00 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | private: |
| 240 | virtual void onRecycle(GrVkGpu* gpu) const = 0; |
| 241 | }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 242 | |
| 243 | #endif |