commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 1 | /* |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 2 | * Copyright 2014 Google Inc. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 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 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 8 | #ifndef GrGpuResource_DEFINED |
| 9 | #define GrGpuResource_DEFINED |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/GrResourceKey.h" |
| 12 | #include "include/private/GrTypesPriv.h" |
| 13 | #include "include/private/SkNoncopyable.h" |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 14 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 15 | class GrGpu; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 16 | class GrResourceCache; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 17 | class SkTraceMemoryDump; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 18 | |
| 19 | /** |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 20 | * Base class for GrGpuResource. Provides the hooks for resources to interact with the cache. |
| 21 | * Separated out as a base class to isolate the ref-cnting behavior and provide friendship without |
| 22 | * exposing all of GrGpuResource. |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 23 | * |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 24 | * PRIOR to the last ref being removed DERIVED::notifyARefCntWillBeZero() will be called |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 25 | * (static poly morphism using CRTP). It is legal for additional ref's to be added |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 26 | * during this time. AFTER the ref count reaches zero DERIVED::notifyARefCntIsZero() will be |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 27 | * called. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 28 | */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 29 | template <typename DERIVED> class GrIORef : public SkNoncopyable { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 30 | public: |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 31 | bool unique() const { return fRefCnt == 1; } |
| 32 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 33 | void ref() const { |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 34 | // Only the cache should be able to add the first ref to a resource. |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 35 | SkASSERT(this->getRefCnt() > 0); |
| 36 | // No barrier required. |
| 37 | (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 38 | } |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 39 | |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 40 | // This enum is used to notify the GrResourceCache which type of ref just dropped to zero. |
| 41 | enum class LastRemovedRef { |
| 42 | kMainRef, // This refers to fRefCnt |
| 43 | kCommandBufferUsage, // This refers to fCommandBufferUsageCnt |
| 44 | }; |
| 45 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 46 | void unref() const { |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 47 | SkASSERT(this->getRefCnt() > 0); |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 48 | if (1 == fRefCnt.fetch_add(-1, std::memory_order_acq_rel)) { |
| 49 | this->notifyWillBeZero(LastRemovedRef::kMainRef); |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void addCommandBufferUsage() const { |
| 54 | // No barrier required. |
| 55 | (void)fCommandBufferUsageCnt.fetch_add(+1, std::memory_order_relaxed); |
| 56 | } |
| 57 | |
| 58 | void removeCommandBufferUsage() const { |
| 59 | SkASSERT(!this->hasNoCommandBufferUsages()); |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 60 | if (1 == fCommandBufferUsageCnt.fetch_add(-1, std::memory_order_acq_rel)) { |
| 61 | this->notifyWillBeZero(LastRemovedRef::kCommandBufferUsage); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 62 | } |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 65 | #if GR_TEST_UTILS |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 66 | int32_t testingOnly_getRefCnt() const { return this->getRefCnt(); } |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 67 | #endif |
| 68 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 69 | protected: |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 70 | GrIORef() : fRefCnt(1), fCommandBufferUsageCnt(0) {} |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 71 | |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 72 | bool internalHasRef() const { return SkToBool(this->getRefCnt()); } |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 73 | bool internalHasNoCommandBufferUsages() const { |
| 74 | return SkToBool(this->hasNoCommandBufferUsages()); |
| 75 | } |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 76 | |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 77 | // Privileged method that allows going from ref count = 0 to ref count = 1. |
| 78 | void addInitialRef() const { |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 79 | SkASSERT(fRefCnt >= 0); |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 80 | // No barrier required. |
| 81 | (void)fRefCnt.fetch_add(+1, std::memory_order_relaxed); |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 82 | } |
| 83 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 84 | private: |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 85 | void notifyWillBeZero(LastRemovedRef removedRef) const { |
Brian Salomon | 76d13bb | 2021-02-25 14:29:15 -0500 | [diff] [blame] | 86 | static_cast<const DERIVED*>(this)->notifyARefCntIsZero(removedRef); |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 89 | int32_t getRefCnt() const { return fRefCnt.load(std::memory_order_relaxed); } |
| 90 | |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 91 | bool hasNoCommandBufferUsages() const { |
| 92 | if (0 == fCommandBufferUsageCnt.load(std::memory_order_acquire)) { |
| 93 | // The acquire barrier is only really needed if we return true. It |
| 94 | // prevents code conditioned on the result of hasNoCommandBufferUsages() from running |
| 95 | // until previous owners are all totally done calling removeCommandBufferUsage(). |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
Brian Salomon | b810c25 | 2019-09-23 12:48:06 -0400 | [diff] [blame] | 101 | mutable std::atomic<int32_t> fRefCnt; |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 102 | mutable std::atomic<int32_t> fCommandBufferUsageCnt; |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 103 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 104 | using INHERITED = SkNoncopyable; |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /** |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 108 | * Base class for objects that can be kept in the GrResourceCache. |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 109 | */ |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 110 | class GrGpuResource : public GrIORef<GrGpuResource> { |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 111 | public: |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 112 | /** |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 113 | * Tests whether a object has been abandoned or released. All objects will |
| 114 | * be in this state after their creating GrContext is destroyed or has |
| 115 | * contextLost called. It's up to the client to test wasDestroyed() before |
| 116 | * attempting to use an object if it holds refs on objects across |
| 117 | * ~GrContext, freeResources with the force flag, or contextLost. |
| 118 | * |
| 119 | * @return true if the object has been released or abandoned, |
| 120 | * false otherwise. |
| 121 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 122 | bool wasDestroyed() const { return nullptr == fGpu; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Retrieves the context that owns the object. Note that it is possible for |
| 126 | * this to return NULL. When objects have been release()ed or abandon()ed |
Adlai Holler | e1c8a38 | 2021-04-08 15:30:12 -0400 | [diff] [blame] | 127 | * they no longer have an owning context. Destroying a GrDirectContext |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 128 | * automatically releases all its resources. |
| 129 | */ |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 130 | const GrDirectContext* getContext() const; |
| 131 | GrDirectContext* getContext(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 132 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Retrieves the amount of GPU memory used by this resource in bytes. It is |
| 135 | * approximate since we aren't aware of additional padding or copies made |
| 136 | * by the driver. |
| 137 | * |
| 138 | * @return the amount of GPU memory used in bytes |
| 139 | */ |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 140 | size_t gpuMemorySize() const { |
| 141 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
| 142 | fGpuMemorySize = this->onGpuMemorySize(); |
| 143 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 144 | } |
| 145 | return fGpuMemorySize; |
| 146 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 147 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 148 | class UniqueID { |
| 149 | public: |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 150 | UniqueID() = default; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 151 | |
| 152 | explicit UniqueID(uint32_t id) : fID(id) {} |
| 153 | |
| 154 | uint32_t asUInt() const { return fID; } |
| 155 | |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 156 | bool operator==(const UniqueID& other) const { return fID == other.fID; } |
| 157 | bool operator!=(const UniqueID& other) const { return !(*this == other); } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 158 | |
| 159 | void makeInvalid() { fID = SK_InvalidUniqueID; } |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 160 | bool isInvalid() const { return fID == SK_InvalidUniqueID; } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 161 | |
| 162 | protected: |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 163 | uint32_t fID = SK_InvalidUniqueID; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 164 | }; |
| 165 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 166 | /** |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 167 | * Gets an id that is unique for this GrGpuResource object. It is static in that it does |
| 168 | * not change when the content of the GrGpuResource object changes. This will never return |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 169 | * 0. |
| 170 | */ |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 171 | UniqueID uniqueID() const { return fUniqueID; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 172 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 173 | /** Returns the current unique key for the resource. It will be invalid if the resource has no |
| 174 | associated unique key. */ |
| 175 | const GrUniqueKey& getUniqueKey() const { return fUniqueKey; } |
bsalomon | 563ff60 | 2015-02-02 17:25:26 -0800 | [diff] [blame] | 176 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 177 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 178 | * Internal-only helper class used for manipulations of the resource by the cache. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 179 | */ |
| 180 | class CacheAccess; |
| 181 | inline CacheAccess cacheAccess(); |
John Stiles | ec9b4aa | 2020-08-07 13:05:14 -0400 | [diff] [blame] | 182 | inline const CacheAccess cacheAccess() const; // NOLINT(readability-const-return-type) |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 183 | |
junov | 436293a | 2014-12-11 10:32:32 -0800 | [diff] [blame] | 184 | /** |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 185 | * Internal-only helper class used for manipulations of the resource by GrSurfaceProxy. |
| 186 | */ |
| 187 | class ProxyAccess; |
| 188 | inline ProxyAccess proxyAccess(); |
| 189 | |
| 190 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 191 | * Internal-only helper class used for manipulations of the resource by internal code. |
| 192 | */ |
| 193 | class ResourcePriv; |
| 194 | inline ResourcePriv resourcePriv(); |
John Stiles | ec9b4aa | 2020-08-07 13:05:14 -0400 | [diff] [blame] | 195 | inline const ResourcePriv resourcePriv() const; // NOLINT(readability-const-return-type) |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 196 | |
| 197 | /** |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 198 | * Dumps memory usage information for this GrGpuResource to traceMemoryDump. |
| 199 | * Typically, subclasses should not need to override this, and should only |
| 200 | * need to override setMemoryBacking. |
| 201 | **/ |
| 202 | virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 203 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 204 | /** |
| 205 | * Describes the type of gpu resource that is represented by the implementing |
| 206 | * class (e.g. texture, buffer object, stencil). This data is used for diagnostic |
| 207 | * purposes by dumpMemoryStatistics(). |
| 208 | * |
| 209 | * The value returned is expected to be long lived and will not be copied by the caller. |
| 210 | */ |
| 211 | virtual const char* getResourceType() const = 0; |
| 212 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 213 | static uint32_t CreateUniqueID(); |
| 214 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 215 | protected: |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 216 | // This must be called by every non-wrapped GrGpuObject. It should be called once the object is |
| 217 | // fully initialized (i.e. only from the constructors of the final class). |
| 218 | void registerWithCache(SkBudgeted); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 219 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 220 | // This must be called by every GrGpuObject that references any wrapped backend objects. It |
| 221 | // should be called once the object is fully initialized (i.e. only from the constructors of the |
| 222 | // final class). |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 223 | void registerWithCacheWrapped(GrWrapCacheable); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 224 | |
| 225 | GrGpuResource(GrGpu*); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 226 | virtual ~GrGpuResource(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 227 | |
| 228 | GrGpu* getGpu() const { return fGpu; } |
| 229 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 230 | /** Overridden to free GPU resources in the backend API. */ |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 231 | virtual void onRelease() { } |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 232 | /** Overridden to abandon any internal handles, ptrs, etc to backend API resources. |
| 233 | This may be called when the underlying 3D context is no longer valid and so no |
| 234 | backend API calls should be made. */ |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 235 | virtual void onAbandon() { } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 236 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 237 | /** |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 238 | * Allows subclasses to add additional backing information to the SkTraceMemoryDump. |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 239 | **/ |
| 240 | virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {} |
| 241 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 242 | /** |
| 243 | * Returns a string that uniquely identifies this resource. |
| 244 | */ |
| 245 | SkString getResourceName() const; |
| 246 | |
| 247 | /** |
| 248 | * A helper for subclasses that override dumpMemoryStatistics(). This method using a format |
| 249 | * consistent with the default implementation of dumpMemoryStatistics() but allows the caller |
| 250 | * to customize various inputs. |
| 251 | */ |
| 252 | void dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump, const SkString& resourceName, |
| 253 | const char* type, size_t size) const; |
| 254 | |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 255 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 256 | private: |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 257 | bool isPurgeable() const; |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 258 | bool hasRef() const; |
Greg Daniel | 1fd8ac8 | 2020-12-11 11:22:01 -0500 | [diff] [blame] | 259 | bool hasNoCommandBufferUsages() const; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 260 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 261 | /** |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 262 | * Called by the registerWithCache if the resource is available to be used as scratch. |
| 263 | * Resource subclasses should override this if the instances should be recycled as scratch |
| 264 | * resources and populate the scratchKey with the key. |
| 265 | * By default resources are not recycled as scratch. |
| 266 | **/ |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 267 | virtual void computeScratchKey(GrScratchKey*) const {} |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 268 | |
| 269 | /** |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 270 | * Removes references to objects in the underlying 3D API without freeing them. |
| 271 | * Called by CacheAccess. |
| 272 | */ |
| 273 | void abandon(); |
| 274 | |
| 275 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 276 | * Frees the object in the underlying 3D API. Called by CacheAccess. |
| 277 | */ |
| 278 | void release(); |
| 279 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 280 | virtual size_t onGpuMemorySize() const = 0; |
| 281 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 282 | // See comments in CacheAccess and ResourcePriv. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 283 | void setUniqueKey(const GrUniqueKey&); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 284 | void removeUniqueKey(); |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 285 | void notifyARefCntIsZero(LastRemovedRef removedRef) const; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 286 | void removeScratchKey(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 287 | void makeBudgeted(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 288 | void makeUnbudgeted(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 289 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 290 | #ifdef SK_DEBUG |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 291 | friend class GrGpu; // for assert in GrGpu to access getGpu |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 292 | #endif |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 293 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 294 | // An index into a heap when this resource is purgeable or an array when not. This is maintained |
| 295 | // by the cache. |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 296 | int fCacheArrayIndex; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 297 | // This value reflects how recently this resource was accessed in the cache. This is maintained |
| 298 | // by the cache. |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 299 | uint32_t fTimestamp; |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 300 | GrStdSteadyClock::time_point fTimeWhenBecamePurgeable; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 301 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 302 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 303 | GrScratchKey fScratchKey; |
| 304 | GrUniqueKey fUniqueKey; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 305 | |
| 306 | // This is not ref'ed but abandon() or release() will be called before the GrGpu object |
| 307 | // is destroyed. Those calls set will this to NULL. |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 308 | GrGpu* fGpu; |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 309 | mutable size_t fGpuMemorySize = kInvalidGpuMemorySize; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 310 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 311 | GrBudgetedType fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable; |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 312 | bool fRefsWrappedObjects = false; |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 313 | const UniqueID fUniqueID; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 314 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 315 | using INHERITED = GrIORef<GrGpuResource>; |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 316 | friend class GrIORef<GrGpuResource>; // to access notifyRefCntWillBeZero and |
| 317 | // notifyARefCntIsZero. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 320 | class GrGpuResource::ProxyAccess { |
| 321 | private: |
| 322 | ProxyAccess(GrGpuResource* resource) : fResource(resource) {} |
| 323 | |
| 324 | /** Proxies are allowed to take a resource from no refs to one ref. */ |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 325 | void ref(GrResourceCache* cache); |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 326 | |
| 327 | // No taking addresses of this type. |
| 328 | const CacheAccess* operator&() const = delete; |
| 329 | CacheAccess* operator&() = delete; |
| 330 | |
| 331 | GrGpuResource* fResource; |
| 332 | |
| 333 | friend class GrGpuResource; |
| 334 | friend class GrSurfaceProxy; |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | inline GrGpuResource::ProxyAccess GrGpuResource::proxyAccess() { return ProxyAccess(this); } |
| 338 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 339 | #endif |