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 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 15 | class GrContext; |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 16 | class GrGpu; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 17 | class GrResourceCache; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 18 | class SkTraceMemoryDump; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 21 | * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base |
| 22 | * class to isolate the ref-cnting behavior and provide friendship without exposing all of |
| 23 | * GrGpuResource. |
mtklein | 6f07665 | 2015-01-13 08:22:43 -0800 | [diff] [blame] | 24 | * |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 25 | * Gpu resources can have three types of refs: |
| 26 | * 1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 27 | * that read and write the resource via GrOpList and by any object that must own a |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 28 | * GrGpuResource and is itself owned (directly or indirectly) by Skia-client code. |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 29 | * 2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 30 | * of the resource by the GPU as a result of a skia API call but hasn't executed it yet. |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 31 | * 3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 32 | * write to the resource by the GPU as a result of a skia API call but hasn't executed it yet. |
| 33 | * |
| 34 | * The latter two ref types are private and intended only for Gr core code. |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 35 | * |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 36 | * PRIOR to the last ref/IO count being removed DERIVED::notifyAllCntsWillBeZero() will be called |
| 37 | * (static poly morphism using CRTP). It is legal for additional ref's or pending IOs to be added |
| 38 | * during this time. AFTER all the ref/io counts reach zero DERIVED::notifyAllCntsAreZero() will be |
| 39 | * called. Similarly when the ref (but not necessarily pending read/write) count reaches 0 |
| 40 | * DERIVED::notifyRefCountIsZero() will be called. In the case when an unref() causes both |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 41 | * the ref cnt to reach zero and the other counts are zero, notifyRefCountIsZero() will be called |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 42 | * before notifyAllCntsAreZero(). Moreover, if notifyRefCountIsZero() returns false then |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 43 | * notifyAllCntsAreZero() won't be called at all. notifyRefCountIsZero() must return false if the |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 44 | * object may be deleted after notifyRefCntIsZero() returns. |
| 45 | * |
| 46 | * GrIORef and GrGpuResource are separate classes for organizational reasons and to be |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 47 | * able to give access via friendship to only the functions related to pending IO operations. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 48 | */ |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 49 | template <typename DERIVED> class GrIORef : public SkNoncopyable { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 50 | public: |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 51 | // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 52 | // templated helper classes (e.g. sk_sp). However, we have different categories of |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 53 | // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are |
| 54 | // not intended to cross thread boundaries. |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 55 | void ref() const { |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 56 | // Only the cache should be able to add the first ref to a resource. |
| 57 | SkASSERT(fRefCnt > 0); |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 58 | this->validate(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 59 | ++fRefCnt; |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 60 | } |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 61 | |
| 62 | void unref() const { |
| 63 | this->validate(); |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 64 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 65 | if (fRefCnt == 1) { |
| 66 | if (!this->internalHasPendingIO()) { |
| 67 | static_cast<const DERIVED*>(this)->notifyAllCntsWillBeZero(); |
| 68 | } |
| 69 | SkASSERT(fRefCnt > 0); |
| 70 | } |
| 71 | if (--fRefCnt == 0) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 72 | if (!static_cast<const DERIVED*>(this)->notifyRefCountIsZero()) { |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | this->didRemoveRefOrPendingIO(kRef_CntType); |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 80 | void validate() const { |
| 81 | #ifdef SK_DEBUG |
| 82 | SkASSERT(fRefCnt >= 0); |
| 83 | SkASSERT(fPendingReads >= 0); |
| 84 | SkASSERT(fPendingWrites >= 0); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 85 | SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0); |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 86 | #endif |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 89 | #if GR_TEST_UTILS |
| 90 | int32_t testingOnly_getRefCnt() const { return fRefCnt; } |
| 91 | int32_t testingOnly_getPendingReads() const { return fPendingReads; } |
| 92 | int32_t testingOnly_getPendingWrites() const { return fPendingWrites; } |
| 93 | #endif |
| 94 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 95 | protected: |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 96 | GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { } |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 97 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 98 | enum CntType { |
| 99 | kRef_CntType, |
| 100 | kPendingRead_CntType, |
| 101 | kPendingWrite_CntType, |
| 102 | }; |
| 103 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 104 | bool internalHasPendingRead() const { return SkToBool(fPendingReads); } |
| 105 | bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); } |
| 106 | bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); } |
| 107 | |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 108 | bool internalHasRef() const { return SkToBool(fRefCnt); } |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 109 | bool internalHasUniqueRef() const { return fRefCnt == 1; } |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 110 | |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 111 | // Privileged method that allows going from ref count = 0 to ref count = 1. |
| 112 | void addInitialRef() const { |
| 113 | this->validate(); |
| 114 | ++fRefCnt; |
| 115 | } |
| 116 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 117 | private: |
| 118 | void addPendingRead() const { |
| 119 | this->validate(); |
| 120 | ++fPendingReads; |
| 121 | } |
| 122 | |
| 123 | void completedRead() const { |
| 124 | this->validate(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 125 | if (fPendingReads == 1 && !fPendingWrites && !fRefCnt) { |
| 126 | static_cast<const DERIVED*>(this)->notifyAllCntsWillBeZero(); |
| 127 | } |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 128 | --fPendingReads; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 129 | this->didRemoveRefOrPendingIO(kPendingRead_CntType); |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void addPendingWrite() const { |
| 133 | this->validate(); |
| 134 | ++fPendingWrites; |
| 135 | } |
| 136 | |
| 137 | void completedWrite() const { |
| 138 | this->validate(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 139 | if (fPendingWrites == 1 && !fPendingReads && !fRefCnt) { |
| 140 | static_cast<const DERIVED*>(this)->notifyAllCntsWillBeZero(); |
| 141 | } |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 142 | --fPendingWrites; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 143 | this->didRemoveRefOrPendingIO(kPendingWrite_CntType); |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 144 | } |
| 145 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 146 | void didRemoveRefOrPendingIO(CntType cntTypeRemoved) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 147 | if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 148 | static_cast<const DERIVED*>(this)->notifyAllCntsAreZero(cntTypeRemoved); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 152 | mutable int32_t fRefCnt; |
| 153 | mutable int32_t fPendingReads; |
| 154 | mutable int32_t fPendingWrites; |
| 155 | |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 156 | friend class GrResourceCache; // to check IO ref counts. |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 157 | |
| 158 | template <typename, GrIOType> friend class GrPendingIOResource; |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 162 | * Base class for objects that can be kept in the GrResourceCache. |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 163 | */ |
bsalomon | 544fe23 | 2014-10-08 10:24:07 -0700 | [diff] [blame] | 164 | class SK_API GrGpuResource : public GrIORef<GrGpuResource> { |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 165 | public: |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 166 | /** |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 167 | * Tests whether a object has been abandoned or released. All objects will |
| 168 | * be in this state after their creating GrContext is destroyed or has |
| 169 | * contextLost called. It's up to the client to test wasDestroyed() before |
| 170 | * attempting to use an object if it holds refs on objects across |
| 171 | * ~GrContext, freeResources with the force flag, or contextLost. |
| 172 | * |
| 173 | * @return true if the object has been released or abandoned, |
| 174 | * false otherwise. |
| 175 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 176 | bool wasDestroyed() const { return nullptr == fGpu; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Retrieves the context that owns the object. Note that it is possible for |
| 180 | * this to return NULL. When objects have been release()ed or abandon()ed |
| 181 | * they no longer have an owning context. Destroying a GrContext |
| 182 | * automatically releases all its resources. |
| 183 | */ |
| 184 | const GrContext* getContext() const; |
| 185 | GrContext* getContext(); |
| 186 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 187 | /** |
| 188 | * Retrieves the amount of GPU memory used by this resource in bytes. It is |
| 189 | * approximate since we aren't aware of additional padding or copies made |
| 190 | * by the driver. |
| 191 | * |
| 192 | * @return the amount of GPU memory used in bytes |
| 193 | */ |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 194 | size_t gpuMemorySize() const { |
| 195 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
| 196 | fGpuMemorySize = this->onGpuMemorySize(); |
| 197 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 198 | } |
| 199 | return fGpuMemorySize; |
| 200 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 201 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 202 | class UniqueID { |
| 203 | public: |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 204 | UniqueID() = default; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 205 | |
| 206 | explicit UniqueID(uint32_t id) : fID(id) {} |
| 207 | |
| 208 | uint32_t asUInt() const { return fID; } |
| 209 | |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 210 | bool operator==(const UniqueID& other) const { return fID == other.fID; } |
| 211 | bool operator!=(const UniqueID& other) const { return !(*this == other); } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 212 | |
| 213 | void makeInvalid() { fID = SK_InvalidUniqueID; } |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 214 | bool isInvalid() const { return fID == SK_InvalidUniqueID; } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 215 | |
| 216 | protected: |
Brian Salomon | 8c8806f | 2019-02-07 13:55:57 -0500 | [diff] [blame] | 217 | uint32_t fID = SK_InvalidUniqueID; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 218 | }; |
| 219 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 220 | /** |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 221 | * Gets an id that is unique for this GrGpuResource object. It is static in that it does |
| 222 | * 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] | 223 | * 0. |
| 224 | */ |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 225 | UniqueID uniqueID() const { return fUniqueID; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 226 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 227 | /** Returns the current unique key for the resource. It will be invalid if the resource has no |
| 228 | associated unique key. */ |
| 229 | const GrUniqueKey& getUniqueKey() const { return fUniqueKey; } |
bsalomon | 563ff60 | 2015-02-02 17:25:26 -0800 | [diff] [blame] | 230 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 231 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 232 | * Internal-only helper class used for manipulations of the resource by the cache. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 233 | */ |
| 234 | class CacheAccess; |
| 235 | inline CacheAccess cacheAccess(); |
| 236 | inline const CacheAccess cacheAccess() const; |
| 237 | |
junov | 436293a | 2014-12-11 10:32:32 -0800 | [diff] [blame] | 238 | /** |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 239 | * Internal-only helper class used for manipulations of the resource by GrSurfaceProxy. |
| 240 | */ |
| 241 | class ProxyAccess; |
| 242 | inline ProxyAccess proxyAccess(); |
| 243 | |
| 244 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 245 | * Internal-only helper class used for manipulations of the resource by internal code. |
| 246 | */ |
| 247 | class ResourcePriv; |
| 248 | inline ResourcePriv resourcePriv(); |
| 249 | inline const ResourcePriv resourcePriv() const; |
| 250 | |
| 251 | /** |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 252 | * Dumps memory usage information for this GrGpuResource to traceMemoryDump. |
| 253 | * Typically, subclasses should not need to override this, and should only |
| 254 | * need to override setMemoryBacking. |
| 255 | **/ |
| 256 | virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 257 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 258 | /** |
| 259 | * Describes the type of gpu resource that is represented by the implementing |
| 260 | * class (e.g. texture, buffer object, stencil). This data is used for diagnostic |
| 261 | * purposes by dumpMemoryStatistics(). |
| 262 | * |
| 263 | * The value returned is expected to be long lived and will not be copied by the caller. |
| 264 | */ |
| 265 | virtual const char* getResourceType() const = 0; |
| 266 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 267 | static uint32_t CreateUniqueID(); |
| 268 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 269 | protected: |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 270 | // This must be called by every non-wrapped GrGpuObject. It should be called once the object is |
| 271 | // fully initialized (i.e. only from the constructors of the final class). |
| 272 | void registerWithCache(SkBudgeted); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 273 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 274 | // This must be called by every GrGpuObject that references any wrapped backend objects. It |
| 275 | // should be called once the object is fully initialized (i.e. only from the constructors of the |
| 276 | // final class). |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 277 | void registerWithCacheWrapped(GrWrapCacheable); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 278 | |
| 279 | GrGpuResource(GrGpu*); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 280 | virtual ~GrGpuResource(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 281 | |
| 282 | GrGpu* getGpu() const { return fGpu; } |
| 283 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 284 | /** Overridden to free GPU resources in the backend API. */ |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 285 | virtual void onRelease() { } |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 286 | /** Overridden to abandon any internal handles, ptrs, etc to backend API resources. |
| 287 | This may be called when the underlying 3D context is no longer valid and so no |
| 288 | backend API calls should be made. */ |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 289 | virtual void onAbandon() { } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 290 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 291 | /** |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 292 | * Allows subclasses to add additional backing information to the SkTraceMemoryDump. |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 293 | **/ |
| 294 | virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {} |
| 295 | |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 296 | /** |
| 297 | * Returns a string that uniquely identifies this resource. |
| 298 | */ |
| 299 | SkString getResourceName() const; |
| 300 | |
| 301 | /** |
| 302 | * A helper for subclasses that override dumpMemoryStatistics(). This method using a format |
| 303 | * consistent with the default implementation of dumpMemoryStatistics() but allows the caller |
| 304 | * to customize various inputs. |
| 305 | */ |
| 306 | void dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump, const SkString& resourceName, |
| 307 | const char* type, size_t size) const; |
| 308 | |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 309 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 310 | private: |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 311 | bool isPurgeable() const; |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 312 | bool hasRef() const; |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 313 | bool hasRefOrPendingIO() const; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 314 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 315 | /** |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 316 | * Called by the registerWithCache if the resource is available to be used as scratch. |
| 317 | * Resource subclasses should override this if the instances should be recycled as scratch |
| 318 | * resources and populate the scratchKey with the key. |
| 319 | * By default resources are not recycled as scratch. |
| 320 | **/ |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 321 | virtual void computeScratchKey(GrScratchKey*) const {} |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 322 | |
| 323 | /** |
Brian Salomon | 35ba614 | 2019-01-24 13:08:59 -0500 | [diff] [blame] | 324 | * Removes references to objects in the underlying 3D API without freeing them. |
| 325 | * Called by CacheAccess. |
| 326 | */ |
| 327 | void abandon(); |
| 328 | |
| 329 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 330 | * Frees the object in the underlying 3D API. Called by CacheAccess. |
| 331 | */ |
| 332 | void release(); |
| 333 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 334 | virtual size_t onGpuMemorySize() const = 0; |
| 335 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 336 | /** |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 337 | * Called by GrResourceCache when a resource loses its last ref or pending IO. |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 338 | */ |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 339 | virtual void willRemoveLastRefOrPendingIO() {} |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 340 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 341 | // See comments in CacheAccess and ResourcePriv. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 342 | void setUniqueKey(const GrUniqueKey&); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 343 | void removeUniqueKey(); |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 344 | void notifyAllCntsWillBeZero() const; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 345 | void notifyAllCntsAreZero(CntType) const; |
| 346 | bool notifyRefCountIsZero() const; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 347 | void removeScratchKey(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 348 | void makeBudgeted(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 349 | void makeUnbudgeted(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 350 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 351 | #ifdef SK_DEBUG |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 352 | friend class GrGpu; // for assert in GrGpu to access getGpu |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 353 | #endif |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 354 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 355 | // An index into a heap when this resource is purgeable or an array when not. This is maintained |
| 356 | // by the cache. |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 357 | int fCacheArrayIndex; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 358 | // This value reflects how recently this resource was accessed in the cache. This is maintained |
| 359 | // by the cache. |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 360 | uint32_t fTimestamp; |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 361 | GrStdSteadyClock::time_point fTimeWhenBecamePurgeable; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 362 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 363 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 364 | GrScratchKey fScratchKey; |
| 365 | GrUniqueKey fUniqueKey; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 366 | |
| 367 | // This is not ref'ed but abandon() or release() will be called before the GrGpu object |
| 368 | // is destroyed. Those calls set will this to NULL. |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 369 | GrGpu* fGpu; |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 370 | mutable size_t fGpuMemorySize = kInvalidGpuMemorySize; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 371 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 372 | GrBudgetedType fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable; |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 373 | bool fRefsWrappedObjects = false; |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 374 | const UniqueID fUniqueID; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 375 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 376 | typedef GrIORef<GrGpuResource> INHERITED; |
Brian Salomon | 9f7d9a2 | 2018-12-11 19:01:32 +0000 | [diff] [blame] | 377 | friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and notifyRefCntIsZero. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 380 | class GrGpuResource::ProxyAccess { |
| 381 | private: |
| 382 | ProxyAccess(GrGpuResource* resource) : fResource(resource) {} |
| 383 | |
| 384 | /** 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] | 385 | void ref(GrResourceCache* cache); |
Brian Salomon | 01ceae9 | 2019-04-02 11:49:54 -0400 | [diff] [blame] | 386 | |
| 387 | // No taking addresses of this type. |
| 388 | const CacheAccess* operator&() const = delete; |
| 389 | CacheAccess* operator&() = delete; |
| 390 | |
| 391 | GrGpuResource* fResource; |
| 392 | |
| 393 | friend class GrGpuResource; |
| 394 | friend class GrSurfaceProxy; |
| 395 | friend class GrIORefProxy; |
| 396 | }; |
| 397 | |
| 398 | inline GrGpuResource::ProxyAccess GrGpuResource::proxyAccess() { return ProxyAccess(this); } |
| 399 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 400 | #endif |