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 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 11 | #include "GrResourceKey.h" |
| 12 | #include "GrTypesPriv.h" |
junov | 5756aff | 2014-12-11 14:59:31 -0800 | [diff] [blame] | 13 | #include "SkData.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 |
| 27 | * that read and write the resource via GrDrawTarget and by any object that must own a |
| 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 |
| 50 | // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of |
| 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); } |
| 95 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 96 | private: |
| 97 | void addPendingRead() const { |
| 98 | this->validate(); |
| 99 | ++fPendingReads; |
| 100 | } |
| 101 | |
| 102 | void completedRead() const { |
| 103 | this->validate(); |
| 104 | --fPendingReads; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 105 | this->didRemoveRefOrPendingIO(kPendingRead_CntType); |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void addPendingWrite() const { |
| 109 | this->validate(); |
| 110 | ++fPendingWrites; |
| 111 | } |
| 112 | |
| 113 | void completedWrite() const { |
| 114 | this->validate(); |
| 115 | --fPendingWrites; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 116 | this->didRemoveRefOrPendingIO(kPendingWrite_CntType); |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private: |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 120 | void didRemoveRefOrPendingIO(CntType cntTypeRemoved) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 121 | if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 122 | static_cast<const DERIVED*>(this)->notifyAllCntsAreZero(cntTypeRemoved); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 126 | mutable int32_t fRefCnt; |
| 127 | mutable int32_t fPendingReads; |
| 128 | mutable int32_t fPendingWrites; |
| 129 | |
bsalomon | ac8d619 | 2014-09-04 14:13:44 -0700 | [diff] [blame] | 130 | // This class is used to manage conversion of refs to pending reads/writes. |
bsalomon | f96ba02 | 2014-09-17 08:05:40 -0700 | [diff] [blame] | 131 | friend class GrGpuResourceRef; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 132 | friend class GrResourceCache; // to check IO ref counts. |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 133 | |
| 134 | template <typename, GrIOType> friend class GrPendingIOResource; |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /** |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 138 | * Base class for objects that can be kept in the GrResourceCache. |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 139 | */ |
bsalomon | 544fe23 | 2014-10-08 10:24:07 -0700 | [diff] [blame] | 140 | class SK_API GrGpuResource : public GrIORef<GrGpuResource> { |
bsalomon | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame] | 141 | public: |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 142 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 143 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 144 | enum LifeCycle { |
| 145 | /** |
| 146 | * The resource is cached and owned by Skia. Resources with this status may be kept alive |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 147 | * by the cache as either scratch or unique resources even when there are no refs to them. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 148 | * The cache may release them whenever there are no refs. |
| 149 | */ |
| 150 | kCached_LifeCycle, |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 151 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 152 | /** |
| 153 | * The resource is uncached. As soon as there are no more refs to it, it is released. Under |
| 154 | * the hood the cache may opaquely recycle it as a cached resource. |
| 155 | */ |
| 156 | kUncached_LifeCycle, |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 157 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 158 | /** |
| 159 | * Similar to uncached, but Skia does not manage the lifetime of the underlying backend |
| 160 | * 3D API object(s). The client is responsible for freeing those. Used to inject client- |
| 161 | * created GPU resources into Skia (e.g. to render to a client-created texture). |
| 162 | */ |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 163 | kBorrowed_LifeCycle, |
| 164 | |
| 165 | /** |
| 166 | * An external resource with ownership transfered into Skia. Skia will free the resource. |
| 167 | */ |
| 168 | kAdopted_LifeCycle, |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 171 | /** |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 172 | * Tests whether a object has been abandoned or released. All objects will |
| 173 | * be in this state after their creating GrContext is destroyed or has |
| 174 | * contextLost called. It's up to the client to test wasDestroyed() before |
| 175 | * attempting to use an object if it holds refs on objects across |
| 176 | * ~GrContext, freeResources with the force flag, or contextLost. |
| 177 | * |
| 178 | * @return true if the object has been released or abandoned, |
| 179 | * false otherwise. |
| 180 | */ |
| 181 | bool wasDestroyed() const { return NULL == fGpu; } |
| 182 | |
| 183 | /** |
| 184 | * Retrieves the context that owns the object. Note that it is possible for |
| 185 | * this to return NULL. When objects have been release()ed or abandon()ed |
| 186 | * they no longer have an owning context. Destroying a GrContext |
| 187 | * automatically releases all its resources. |
| 188 | */ |
| 189 | const GrContext* getContext() const; |
| 190 | GrContext* getContext(); |
| 191 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Retrieves the amount of GPU memory used by this resource in bytes. It is |
| 194 | * approximate since we aren't aware of additional padding or copies made |
| 195 | * by the driver. |
| 196 | * |
| 197 | * @return the amount of GPU memory used in bytes |
| 198 | */ |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 199 | size_t gpuMemorySize() const { |
| 200 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
| 201 | fGpuMemorySize = this->onGpuMemorySize(); |
| 202 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 203 | } |
| 204 | return fGpuMemorySize; |
| 205 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 206 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 207 | /** |
bsalomon | 52e9d63 | 2014-09-05 12:23:12 -0700 | [diff] [blame] | 208 | * Gets an id that is unique for this GrGpuResource object. It is static in that it does |
| 209 | * 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] | 210 | * 0. |
| 211 | */ |
| 212 | uint32_t getUniqueID() const { return fUniqueID; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 213 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 214 | /** Returns the current unique key for the resource. It will be invalid if the resource has no |
| 215 | associated unique key. */ |
| 216 | const GrUniqueKey& getUniqueKey() const { return fUniqueKey; } |
bsalomon | 563ff60 | 2015-02-02 17:25:26 -0800 | [diff] [blame] | 217 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 218 | /** |
junov | 5756aff | 2014-12-11 14:59:31 -0800 | [diff] [blame] | 219 | * Attach a custom data object to this resource. The data will remain attached |
| 220 | * for the lifetime of this resource (until it is abandoned or released). |
| 221 | * Takes a ref on data. Previously attached data, if any, is unrefed. |
| 222 | * Returns the data argument, for convenience. |
| 223 | */ |
| 224 | const SkData* setCustomData(const SkData* data); |
| 225 | |
| 226 | /** |
| 227 | * Returns the custom data object that was attached to this resource by |
| 228 | * calling setCustomData. |
| 229 | */ |
| 230 | const SkData* getCustomData() const { return fData.get(); } |
| 231 | |
| 232 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 233 | * Internal-only helper class used for manipulations of the resource by the cache. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 234 | */ |
| 235 | class CacheAccess; |
| 236 | inline CacheAccess cacheAccess(); |
| 237 | inline const CacheAccess cacheAccess() const; |
| 238 | |
junov | 436293a | 2014-12-11 10:32:32 -0800 | [diff] [blame] | 239 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 240 | * Internal-only helper class used for manipulations of the resource by internal code. |
| 241 | */ |
| 242 | class ResourcePriv; |
| 243 | inline ResourcePriv resourcePriv(); |
| 244 | inline const ResourcePriv resourcePriv() const; |
| 245 | |
| 246 | /** |
junov | 436293a | 2014-12-11 10:32:32 -0800 | [diff] [blame] | 247 | * Removes references to objects in the underlying 3D API without freeing them. |
| 248 | * Called by CacheAccess. |
| 249 | * In general this method should not be called outside of skia. It was |
| 250 | * made by public for a special case where it needs to be called in Blink |
| 251 | * when a texture becomes unsafe to use after having been shared through |
| 252 | * a texture mailbox. |
| 253 | */ |
| 254 | void abandon(); |
| 255 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 256 | /** |
| 257 | * Dumps memory usage information for this GrGpuResource to traceMemoryDump. |
| 258 | * Typically, subclasses should not need to override this, and should only |
| 259 | * need to override setMemoryBacking. |
| 260 | **/ |
| 261 | virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 262 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 263 | protected: |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 264 | // This must be called by every GrGpuObject. It should be called once the object is fully |
| 265 | // initialized (i.e. not in a base class constructor). |
| 266 | void registerWithCache(); |
| 267 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 268 | GrGpuResource(GrGpu*, LifeCycle); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 269 | virtual ~GrGpuResource(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 270 | |
| 271 | GrGpu* getGpu() const { return fGpu; } |
| 272 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 273 | /** Overridden to free GPU resources in the backend API. */ |
| 274 | virtual void onRelease() { } |
| 275 | /** Overridden to abandon any internal handles, ptrs, etc to backend API resources. |
| 276 | This may be called when the underlying 3D context is no longer valid and so no |
| 277 | backend API calls should be made. */ |
| 278 | virtual void onAbandon() { } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 279 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 280 | bool shouldFreeResources() const { return fLifeCycle != kBorrowed_LifeCycle; } |
| 281 | |
| 282 | bool isExternal() const { |
| 283 | return GrGpuResource::kAdopted_LifeCycle == fLifeCycle || |
| 284 | GrGpuResource::kBorrowed_LifeCycle == fLifeCycle; |
| 285 | } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 286 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 287 | /** |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 288 | * This entry point should be called whenever gpuMemorySize() should report a different size. |
| 289 | * The cache will call gpuMemorySize() to update the current size of the resource. |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 290 | */ |
| 291 | void didChangeGpuMemorySize() const; |
| 292 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 293 | /** |
| 294 | * Optionally called by the GrGpuResource subclass if the resource can be used as scratch. |
| 295 | * By default resources are not usable as scratch. This should only be called once. |
| 296 | **/ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 297 | void setScratchKey(const GrScratchKey& scratchKey); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 298 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Allows subclasses to add additional backing information to the SkTraceMemoryDump. Called by |
| 301 | * onMemoryDump. The default implementation adds no backing information. |
| 302 | **/ |
| 303 | virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {} |
| 304 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 305 | private: |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 306 | /** |
| 307 | * Frees the object in the underlying 3D API. Called by CacheAccess. |
| 308 | */ |
| 309 | void release(); |
| 310 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 311 | virtual size_t onGpuMemorySize() const = 0; |
| 312 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 313 | // See comments in CacheAccess and ResourcePriv. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 314 | void setUniqueKey(const GrUniqueKey&); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 315 | void removeUniqueKey(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 316 | void notifyAllCntsAreZero(CntType) const; |
| 317 | bool notifyRefCountIsZero() const; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 318 | void removeScratchKey(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 319 | void makeBudgeted(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 320 | void makeUnbudgeted(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 321 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 322 | #ifdef SK_DEBUG |
| 323 | friend class GrGpu; // for assert in GrGpu to access getGpu |
| 324 | #endif |
| 325 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 326 | static uint32_t CreateUniqueID(); |
| 327 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 328 | // An index into a heap when this resource is purgeable or an array when not. This is maintained |
| 329 | // by the cache. |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 330 | int fCacheArrayIndex; |
| 331 | // This value reflects how recently this resource was accessed in the cache. This is maintained |
| 332 | // by the cache. |
| 333 | uint32_t fTimestamp; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 334 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 335 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 336 | GrScratchKey fScratchKey; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 337 | GrUniqueKey fUniqueKey; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 338 | |
| 339 | // This is not ref'ed but abandon() or release() will be called before the GrGpu object |
| 340 | // is destroyed. Those calls set will this to NULL. |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 341 | GrGpu* fGpu; |
| 342 | mutable size_t fGpuMemorySize; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 343 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 344 | LifeCycle fLifeCycle; |
| 345 | const uint32_t fUniqueID; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 346 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 347 | SkAutoTUnref<const SkData> fData; |
junov | 5756aff | 2014-12-11 14:59:31 -0800 | [diff] [blame] | 348 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 349 | typedef GrIORef<GrGpuResource> INHERITED; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 350 | friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and notifyRefCntIsZero. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | #endif |