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