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