blob: 6271db8649d6578bc3f477239a691bc3f0f30f18 [file] [log] [blame]
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00001/*
bsalomonc44be0e2014-07-25 07:32:33 -07002 * Copyright 2014 Google Inc.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon6d3fe022014-07-25 08:35:45 -07008#ifndef GrGpuResource_DEFINED
9#define GrGpuResource_DEFINED
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000010
Brian Salomonb4ba8262018-12-20 20:37:55 -050011#include "../private/GrResourceKey.h"
Brian Salomonc5886032017-07-19 11:48:05 -040012#include "../private/GrTypesPriv.h"
Ben Wagnerd5148e32018-07-16 17:44:06 -040013#include "../private/SkNoncopyable.h"
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000014
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000015class GrContext;
bsalomon37dd3312014-11-03 08:47:23 -080016class GrGpu;
bsalomon0ea80f42015-02-11 10:49:59 -080017class GrResourceCache;
ericrk0a5fa482015-09-15 14:16:10 -070018class SkTraceMemoryDump;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000019
20/**
bsalomon00b76bd2014-09-03 14:05:49 -070021 * 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.
mtklein6f076652015-01-13 08:22:43 -080024 *
bsalomon00b76bd2014-09-03 14:05:49 -070025 * 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 Phillipsf2361d22016-10-25 14:20:06 -040027 * that read and write the resource via GrOpList and by any object that must own a
bsalomon00b76bd2014-09-03 14:05:49 -070028 * GrGpuResource and is itself owned (directly or indirectly) by Skia-client code.
bsalomonbcf0a522014-10-08 08:40:09 -070029 * 2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read
bsalomon00b76bd2014-09-03 14:05:49 -070030 * of the resource by the GPU as a result of a skia API call but hasn't executed it yet.
bsalomonbcf0a522014-10-08 08:40:09 -070031 * 3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a
bsalomon00b76bd2014-09-03 14:05:49 -070032 * 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.
bsalomonbcf0a522014-10-08 08:40:09 -070035 *
bsalomon3f324322015-04-08 11:01:54 -070036 * 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
bsalomonbcf0a522014-10-08 08:40:09 -070045 * able to give access via friendship to only the functions related to pending IO operations.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000046 */
bsalomonbcf0a522014-10-08 08:40:09 -070047template <typename DERIVED> class GrIORef : public SkNoncopyable {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000048public:
bsalomon00b76bd2014-09-03 14:05:49 -070049 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
bungeman6bd52842016-10-27 09:30:08 -070050 // templated helper classes (e.g. sk_sp). However, we have different categories of
bsalomon00b76bd2014-09-03 14:05:49 -070051 // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
52 // not intended to cross thread boundaries.
bsalomon00b76bd2014-09-03 14:05:49 -070053 void ref() const {
Brian Salomon9323b8b2014-10-07 15:07:38 -040054 this->validate();
bsalomonbcf0a522014-10-08 08:40:09 -070055 ++fRefCnt;
bsalomonc44be0e2014-07-25 07:32:33 -070056 }
bsalomon00b76bd2014-09-03 14:05:49 -070057
58 void unref() const {
59 this->validate();
mtklein2766c002015-06-26 11:45:03 -070060
bsalomon3f324322015-04-08 11:01:54 -070061 if (!(--fRefCnt)) {
62 if (!static_cast<const DERIVED*>(this)->notifyRefCountIsZero()) {
63 return;
64 }
65 }
66
67 this->didRemoveRefOrPendingIO(kRef_CntType);
bsalomon00b76bd2014-09-03 14:05:49 -070068 }
69
bsalomon00b76bd2014-09-03 14:05:49 -070070 void validate() const {
71#ifdef SK_DEBUG
72 SkASSERT(fRefCnt >= 0);
73 SkASSERT(fPendingReads >= 0);
74 SkASSERT(fPendingWrites >= 0);
bsalomon12299ab2014-11-14 13:33:09 -080075 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0);
bsalomonc44be0e2014-07-25 07:32:33 -070076#endif
bsalomon00b76bd2014-09-03 14:05:49 -070077 }
78
79protected:
Brian Salomon9f7d9a22018-12-11 19:01:32 +000080 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
bsalomon00b76bd2014-09-03 14:05:49 -070081
bsalomon3f324322015-04-08 11:01:54 -070082 enum CntType {
83 kRef_CntType,
84 kPendingRead_CntType,
85 kPendingWrite_CntType,
86 };
87
bsalomon8d034a12014-09-22 12:21:08 -070088 bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
89 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
90 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
91
bsalomon6d4488c2014-11-11 07:27:16 -080092 bool internalHasRef() const { return SkToBool(fRefCnt); }
Eric Karl914a36b2017-10-12 12:44:50 -070093 bool internalHasUniqueRef() const { return fRefCnt == 1; }
bsalomon6d4488c2014-11-11 07:27:16 -080094
bsalomon00b76bd2014-09-03 14:05:49 -070095private:
Brian Salomon9f7d9a22018-12-11 19:01:32 +000096 friend class GrIORefProxy; // needs to forward on wrapped IO calls
Brian Salomonf046e152017-01-11 15:24:47 -050097 // This is for a unit test.
98 template <typename T>
99 friend void testingOnly_getIORefCnts(const T*, int* refCnt, int* readCnt, int* writeCnt);
robertphillips1125a032016-11-16 11:17:17 -0800100
bsalomon00b76bd2014-09-03 14:05:49 -0700101 void addPendingRead() const {
102 this->validate();
103 ++fPendingReads;
104 }
105
106 void completedRead() const {
107 this->validate();
108 --fPendingReads;
bsalomon3f324322015-04-08 11:01:54 -0700109 this->didRemoveRefOrPendingIO(kPendingRead_CntType);
bsalomon00b76bd2014-09-03 14:05:49 -0700110 }
111
112 void addPendingWrite() const {
113 this->validate();
114 ++fPendingWrites;
115 }
116
117 void completedWrite() const {
118 this->validate();
119 --fPendingWrites;
bsalomon3f324322015-04-08 11:01:54 -0700120 this->didRemoveRefOrPendingIO(kPendingWrite_CntType);
bsalomon00b76bd2014-09-03 14:05:49 -0700121 }
122
123private:
bsalomon3f324322015-04-08 11:01:54 -0700124 void didRemoveRefOrPendingIO(CntType cntTypeRemoved) const {
bsalomon12299ab2014-11-14 13:33:09 -0800125 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
bsalomon3f324322015-04-08 11:01:54 -0700126 static_cast<const DERIVED*>(this)->notifyAllCntsAreZero(cntTypeRemoved);
bsalomonbcf0a522014-10-08 08:40:09 -0700127 }
128 }
129
bsalomon00b76bd2014-09-03 14:05:49 -0700130 mutable int32_t fRefCnt;
131 mutable int32_t fPendingReads;
132 mutable int32_t fPendingWrites;
133
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000134 friend class GrResourceCache; // to check IO ref counts.
bsalomonbcf0a522014-10-08 08:40:09 -0700135
136 template <typename, GrIOType> friend class GrPendingIOResource;
bsalomon00b76bd2014-09-03 14:05:49 -0700137};
138
139/**
bsalomon0ea80f42015-02-11 10:49:59 -0800140 * Base class for objects that can be kept in the GrResourceCache.
bsalomon00b76bd2014-09-03 14:05:49 -0700141 */
bsalomon544fe232014-10-08 10:24:07 -0700142class SK_API GrGpuResource : public GrIORef<GrGpuResource> {
bsalomon00b76bd2014-09-03 14:05:49 -0700143public:
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000144
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000145 /**
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000146 * Tests whether a object has been abandoned or released. All objects will
147 * be in this state after their creating GrContext is destroyed or has
148 * contextLost called. It's up to the client to test wasDestroyed() before
149 * attempting to use an object if it holds refs on objects across
150 * ~GrContext, freeResources with the force flag, or contextLost.
151 *
152 * @return true if the object has been released or abandoned,
153 * false otherwise.
154 */
Ben Wagnera93a14a2017-08-28 10:34:05 -0400155 bool wasDestroyed() const { return nullptr == fGpu; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000156
157 /**
158 * Retrieves the context that owns the object. Note that it is possible for
159 * this to return NULL. When objects have been release()ed or abandon()ed
160 * they no longer have an owning context. Destroying a GrContext
161 * automatically releases all its resources.
162 */
163 const GrContext* getContext() const;
164 GrContext* getContext();
165
bsalomonc44be0e2014-07-25 07:32:33 -0700166 /**
167 * Retrieves the amount of GPU memory used by this resource in bytes. It is
168 * approximate since we aren't aware of additional padding or copies made
169 * by the driver.
170 *
171 * @return the amount of GPU memory used in bytes
172 */
bsalomon69ed47f2014-11-12 11:13:39 -0800173 size_t gpuMemorySize() const {
174 if (kInvalidGpuMemorySize == fGpuMemorySize) {
175 fGpuMemorySize = this->onGpuMemorySize();
176 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
177 }
178 return fGpuMemorySize;
179 }
bsalomonc44be0e2014-07-25 07:32:33 -0700180
Robert Phillips294870f2016-11-11 12:38:40 -0500181 class UniqueID {
182 public:
183 static UniqueID InvalidID() {
184 return UniqueID(uint32_t(SK_InvalidUniqueID));
185 }
186
187 UniqueID() {}
188
189 explicit UniqueID(uint32_t id) : fID(id) {}
190
191 uint32_t asUInt() const { return fID; }
192
193 bool operator==(const UniqueID& other) const {
194 return fID == other.fID;
195 }
196 bool operator!=(const UniqueID& other) const {
197 return !(*this == other);
198 }
199
200 void makeInvalid() { fID = SK_InvalidUniqueID; }
201 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
202
203 protected:
204 uint32_t fID;
205 };
206
bsalomonc44be0e2014-07-25 07:32:33 -0700207 /**
bsalomon52e9d632014-09-05 12:23:12 -0700208 * 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
bsalomonc44be0e2014-07-25 07:32:33 -0700210 * 0.
211 */
Robert Phillips294870f2016-11-11 12:38:40 -0500212 UniqueID uniqueID() const { return fUniqueID; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000213
bsalomon8718aaf2015-02-19 07:24:21 -0800214 /** 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; }
bsalomon563ff602015-02-02 17:25:26 -0800217
bsalomon453cf402014-11-11 14:15:57 -0800218 /**
bsalomon3582d3e2015-02-13 14:20:05 -0800219 * Internal-only helper class used for manipulations of the resource by the cache.
bsalomon453cf402014-11-11 14:15:57 -0800220 */
221 class CacheAccess;
222 inline CacheAccess cacheAccess();
223 inline const CacheAccess cacheAccess() const;
224
junov436293a2014-12-11 10:32:32 -0800225 /**
bsalomon3582d3e2015-02-13 14:20:05 -0800226 * Internal-only helper class used for manipulations of the resource by internal code.
227 */
228 class ResourcePriv;
229 inline ResourcePriv resourcePriv();
230 inline const ResourcePriv resourcePriv() const;
231
232 /**
junov436293a2014-12-11 10:32:32 -0800233 * Removes references to objects in the underlying 3D API without freeing them.
234 * Called by CacheAccess.
235 * In general this method should not be called outside of skia. It was
236 * made by public for a special case where it needs to be called in Blink
237 * when a texture becomes unsafe to use after having been shared through
238 * a texture mailbox.
239 */
240 void abandon();
241
ericrk0a5fa482015-09-15 14:16:10 -0700242 /**
243 * Dumps memory usage information for this GrGpuResource to traceMemoryDump.
244 * Typically, subclasses should not need to override this, and should only
245 * need to override setMemoryBacking.
246 **/
247 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
248
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400249 /**
250 * Describes the type of gpu resource that is represented by the implementing
251 * class (e.g. texture, buffer object, stencil). This data is used for diagnostic
252 * purposes by dumpMemoryStatistics().
253 *
254 * The value returned is expected to be long lived and will not be copied by the caller.
255 */
256 virtual const char* getResourceType() const = 0;
257
robertphillips8abb3702016-08-31 14:04:06 -0700258 static uint32_t CreateUniqueID();
259
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000260protected:
kkinnunen2e6055b2016-04-22 01:48:29 -0700261 // This must be called by every non-wrapped GrGpuObject. It should be called once the object is
262 // fully initialized (i.e. only from the constructors of the final class).
263 void registerWithCache(SkBudgeted);
bsalomon16961262014-08-26 14:01:07 -0700264
kkinnunen2e6055b2016-04-22 01:48:29 -0700265 // This must be called by every GrGpuObject that references any wrapped backend objects. It
266 // should be called once the object is fully initialized (i.e. only from the constructors of the
267 // final class).
Greg Daniel2268ad22018-11-15 09:27:38 -0500268 void registerWithCacheWrapped(bool purgeImmediately = false);
kkinnunen2e6055b2016-04-22 01:48:29 -0700269
270 GrGpuResource(GrGpu*);
bsalomon6d3fe022014-07-25 08:35:45 -0700271 virtual ~GrGpuResource();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000272
273 GrGpu* getGpu() const { return fGpu; }
274
bsalomon12299ab2014-11-14 13:33:09 -0800275 /** Overridden to free GPU resources in the backend API. */
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000276 virtual void onRelease() { }
bsalomon12299ab2014-11-14 13:33:09 -0800277 /** Overridden to abandon any internal handles, ptrs, etc to backend API resources.
278 This may be called when the underlying 3D context is no longer valid and so no
279 backend API calls should be made. */
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000280 virtual void onAbandon() { }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000281
bsalomonc44be0e2014-07-25 07:32:33 -0700282 /**
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400283 * Allows subclasses to add additional backing information to the SkTraceMemoryDump.
ericrk0a5fa482015-09-15 14:16:10 -0700284 **/
285 virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {}
286
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400287 /**
288 * Returns a string that uniquely identifies this resource.
289 */
290 SkString getResourceName() const;
291
292 /**
293 * A helper for subclasses that override dumpMemoryStatistics(). This method using a format
294 * consistent with the default implementation of dumpMemoryStatistics() but allows the caller
295 * to customize various inputs.
296 */
297 void dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump, const SkString& resourceName,
298 const char* type, size_t size) const;
299
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000300
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000301private:
Brian Salomon614c1a82018-12-19 15:42:06 -0500302 bool isPurgeable() const { return !this->internalHasRef() && !this->internalHasPendingIO(); }
303
bsalomon12299ab2014-11-14 13:33:09 -0800304 /**
kkinnunen2e6055b2016-04-22 01:48:29 -0700305 * 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 **/
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000310 virtual void computeScratchKey(GrScratchKey*) const { }
kkinnunen2e6055b2016-04-22 01:48:29 -0700311
312 /**
bsalomon12299ab2014-11-14 13:33:09 -0800313 * Frees the object in the underlying 3D API. Called by CacheAccess.
314 */
315 void release();
316
bsalomon69ed47f2014-11-12 11:13:39 -0800317 virtual size_t onGpuMemorySize() const = 0;
318
Brian Salomon614c1a82018-12-19 15:42:06 -0500319 /**
320 * Called by GrResourceCache when a resource transitions from being unpurgeable to purgeable.
321 */
322 virtual void becamePurgeable() {}
323
bsalomon9f2d1572015-02-17 11:47:40 -0800324 // See comments in CacheAccess and ResourcePriv.
bsalomonf99e9612015-02-19 08:24:16 -0800325 void setUniqueKey(const GrUniqueKey&);
bsalomon8718aaf2015-02-19 07:24:21 -0800326 void removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700327 void notifyAllCntsAreZero(CntType) const;
328 bool notifyRefCountIsZero() const;
bsalomon10e23ca2014-11-25 05:52:06 -0800329 void removeScratchKey();
bsalomonafe30052015-01-16 07:32:33 -0800330 void makeBudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800331 void makeUnbudgeted();
bsalomonbcf0a522014-10-08 08:40:09 -0700332
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000333#ifdef SK_DEBUG
Brian Salomon5e150852017-03-22 14:53:13 -0400334 friend class GrGpu; // for assert in GrGpu to access getGpu
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000335#endif
Brian Salomon5e150852017-03-22 14:53:13 -0400336
bsalomonf320e042015-02-17 15:09:34 -0800337 // An index into a heap when this resource is purgeable or an array when not. This is maintained
338 // by the cache.
Brian Salomon5e150852017-03-22 14:53:13 -0400339 int fCacheArrayIndex;
bsalomon9f2d1572015-02-17 11:47:40 -0800340 // This value reflects how recently this resource was accessed in the cache. This is maintained
341 // by the cache.
Brian Salomon5e150852017-03-22 14:53:13 -0400342 uint32_t fTimestamp;
Brian Salomon5e150852017-03-22 14:53:13 -0400343 GrStdSteadyClock::time_point fTimeWhenBecamePurgeable;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000344
bsalomon69ed47f2014-11-12 11:13:39 -0800345 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
Brian Salomon5e150852017-03-22 14:53:13 -0400346 GrScratchKey fScratchKey;
347 GrUniqueKey fUniqueKey;
bsalomon84c8e622014-11-17 09:33:27 -0800348
349 // This is not ref'ed but abandon() or release() will be called before the GrGpu object
350 // is destroyed. Those calls set will this to NULL.
Brian Salomon5e150852017-03-22 14:53:13 -0400351 GrGpu* fGpu;
352 mutable size_t fGpuMemorySize;
bsalomon84c8e622014-11-17 09:33:27 -0800353
Brian Salomon5e150852017-03-22 14:53:13 -0400354 SkBudgeted fBudgeted;
Greg Daniel2268ad22018-11-15 09:27:38 -0500355 bool fShouldPurgeImmediately;
Brian Salomon5e150852017-03-22 14:53:13 -0400356 bool fRefsWrappedObjects;
357 const UniqueID fUniqueID;
bsalomon744998e2014-08-28 09:54:34 -0700358
bsalomonbcf0a522014-10-08 08:40:09 -0700359 typedef GrIORef<GrGpuResource> INHERITED;
Brian Salomon9f7d9a22018-12-11 19:01:32 +0000360 friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and notifyRefCntIsZero.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000361};
362
363#endif