blob: 32376e524bbf325cedee3af01594665fe20a8930 [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
bsalomonbcf0a522014-10-08 08:40:09 -070011#include "GrResourceKey.h"
12#include "GrTypesPriv.h"
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000013
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000014class GrContext;
bsalomon37dd3312014-11-03 08:47:23 -080015class GrGpu;
bsalomon0ea80f42015-02-11 10:49:59 -080016class GrResourceCache;
ericrk0a5fa482015-09-15 14:16:10 -070017class SkTraceMemoryDump;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000018
19/**
bsalomon00b76bd2014-09-03 14:05:49 -070020 * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base
21 * class to isolate the ref-cnting behavior and provide friendship without exposing all of
22 * GrGpuResource.
mtklein6f076652015-01-13 08:22:43 -080023 *
bsalomon00b76bd2014-09-03 14:05:49 -070024 * Gpu resources can have three types of refs:
25 * 1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls
Robert Phillipsf2361d22016-10-25 14:20:06 -040026 * that read and write the resource via GrOpList and by any object that must own a
bsalomon00b76bd2014-09-03 14:05:49 -070027 * GrGpuResource and is itself owned (directly or indirectly) by Skia-client code.
bsalomonbcf0a522014-10-08 08:40:09 -070028 * 2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read
bsalomon00b76bd2014-09-03 14:05:49 -070029 * 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 -070030 * 3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a
bsalomon00b76bd2014-09-03 14:05:49 -070031 * write to the resource by the GPU as a result of a skia API call but hasn't executed it yet.
32 *
33 * The latter two ref types are private and intended only for Gr core code.
bsalomonbcf0a522014-10-08 08:40:09 -070034 *
bsalomon3f324322015-04-08 11:01:54 -070035 * When all the ref/io counts reach zero DERIVED::notifyAllCntsAreZero() will be called (static poly
36 * morphism using CRTP). Similarly when the ref (but not necessarily pending read/write) count
37 * reaches 0 DERIVED::notifyRefCountIsZero() will be called. In the case when an unref() causes both
38 * the ref cnt to reach zero and the other counts are zero, notifyRefCountIsZero() will be called
39 * before notifyIsPurgeable(). Moreover, if notifyRefCountIsZero() returns false then
40 * notifyAllRefCntsAreZero() won't be called at all. notifyRefCountIsZero() must return false if the
41 * object may be deleted after notifyRefCntIsZero() returns.
42 *
43 * GrIORef and GrGpuResource are separate classes for organizational reasons and to be
bsalomonbcf0a522014-10-08 08:40:09 -070044 * able to give access via friendship to only the functions related to pending IO operations.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000045 */
bsalomonbcf0a522014-10-08 08:40:09 -070046template <typename DERIVED> class GrIORef : public SkNoncopyable {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000047public:
bsalomon00b76bd2014-09-03 14:05:49 -070048 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
bungeman6bd52842016-10-27 09:30:08 -070049 // templated helper classes (e.g. sk_sp). However, we have different categories of
bsalomon00b76bd2014-09-03 14:05:49 -070050 // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
51 // not intended to cross thread boundaries.
bsalomon00b76bd2014-09-03 14:05:49 -070052 void ref() const {
Brian Salomon9323b8b2014-10-07 15:07:38 -040053 this->validate();
bsalomonbcf0a522014-10-08 08:40:09 -070054 ++fRefCnt;
bsalomonc44be0e2014-07-25 07:32:33 -070055 }
bsalomon00b76bd2014-09-03 14:05:49 -070056
57 void unref() const {
58 this->validate();
mtklein2766c002015-06-26 11:45:03 -070059
bsalomon3f324322015-04-08 11:01:54 -070060 if (!(--fRefCnt)) {
61 if (!static_cast<const DERIVED*>(this)->notifyRefCountIsZero()) {
62 return;
63 }
64 }
65
66 this->didRemoveRefOrPendingIO(kRef_CntType);
bsalomon00b76bd2014-09-03 14:05:49 -070067 }
68
bsalomon00b76bd2014-09-03 14:05:49 -070069 void validate() const {
70#ifdef SK_DEBUG
71 SkASSERT(fRefCnt >= 0);
72 SkASSERT(fPendingReads >= 0);
73 SkASSERT(fPendingWrites >= 0);
bsalomon12299ab2014-11-14 13:33:09 -080074 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0);
bsalomonc44be0e2014-07-25 07:32:33 -070075#endif
bsalomon00b76bd2014-09-03 14:05:49 -070076 }
77
Brian Salomonbc6b99d2017-01-11 10:32:34 -050078 void testingOnly_getCounts(int* refCnt, int* readCnt, int* writeCnt) const;
79
bsalomon00b76bd2014-09-03 14:05:49 -070080protected:
bsalomon1e2530b2014-10-09 09:57:18 -070081 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
bsalomon00b76bd2014-09-03 14:05:49 -070082
bsalomon3f324322015-04-08 11:01:54 -070083 enum CntType {
84 kRef_CntType,
85 kPendingRead_CntType,
86 kPendingWrite_CntType,
87 };
88
bsalomon63c992f2015-01-23 12:47:59 -080089 bool isPurgeable() const { return !this->internalHasRef() && !this->internalHasPendingIO(); }
bsalomon12299ab2014-11-14 13:33:09 -080090
bsalomon8d034a12014-09-22 12:21:08 -070091 bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
92 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
93 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
94
bsalomon6d4488c2014-11-11 07:27:16 -080095 bool internalHasRef() const { return SkToBool(fRefCnt); }
96
bsalomon00b76bd2014-09-03 14:05:49 -070097private:
robertphillips1125a032016-11-16 11:17:17 -080098 friend class GrIORefProxy; // needs to forward on wrapped IO calls
99
bsalomon00b76bd2014-09-03 14:05:49 -0700100 void addPendingRead() const {
101 this->validate();
102 ++fPendingReads;
103 }
104
105 void completedRead() const {
106 this->validate();
107 --fPendingReads;
bsalomon3f324322015-04-08 11:01:54 -0700108 this->didRemoveRefOrPendingIO(kPendingRead_CntType);
bsalomon00b76bd2014-09-03 14:05:49 -0700109 }
110
111 void addPendingWrite() const {
112 this->validate();
113 ++fPendingWrites;
114 }
115
116 void completedWrite() const {
117 this->validate();
118 --fPendingWrites;
bsalomon3f324322015-04-08 11:01:54 -0700119 this->didRemoveRefOrPendingIO(kPendingWrite_CntType);
bsalomon00b76bd2014-09-03 14:05:49 -0700120 }
121
122private:
bsalomon3f324322015-04-08 11:01:54 -0700123 void didRemoveRefOrPendingIO(CntType cntTypeRemoved) const {
bsalomon12299ab2014-11-14 13:33:09 -0800124 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) {
bsalomon3f324322015-04-08 11:01:54 -0700125 static_cast<const DERIVED*>(this)->notifyAllCntsAreZero(cntTypeRemoved);
bsalomonbcf0a522014-10-08 08:40:09 -0700126 }
127 }
128
bsalomon00b76bd2014-09-03 14:05:49 -0700129 mutable int32_t fRefCnt;
130 mutable int32_t fPendingReads;
131 mutable int32_t fPendingWrites;
132
bsalomonac8d6192014-09-04 14:13:44 -0700133 // This class is used to manage conversion of refs to pending reads/writes.
bsalomonf96ba022014-09-17 08:05:40 -0700134 friend class GrGpuResourceRef;
bsalomon0ea80f42015-02-11 10:49:59 -0800135 friend class GrResourceCache; // to check IO ref counts.
bsalomonbcf0a522014-10-08 08:40:09 -0700136
137 template <typename, GrIOType> friend class GrPendingIOResource;
bsalomon00b76bd2014-09-03 14:05:49 -0700138};
139
140/**
bsalomon0ea80f42015-02-11 10:49:59 -0800141 * Base class for objects that can be kept in the GrResourceCache.
bsalomon00b76bd2014-09-03 14:05:49 -0700142 */
bsalomon544fe232014-10-08 10:24:07 -0700143class SK_API GrGpuResource : public GrIORef<GrGpuResource> {
bsalomon00b76bd2014-09-03 14:05:49 -0700144public:
ericrk0a5fa482015-09-15 14:16:10 -0700145
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000146 /**
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000147 * Tests whether a object has been abandoned or released. All objects will
148 * be in this state after their creating GrContext is destroyed or has
149 * contextLost called. It's up to the client to test wasDestroyed() before
150 * attempting to use an object if it holds refs on objects across
151 * ~GrContext, freeResources with the force flag, or contextLost.
152 *
153 * @return true if the object has been released or abandoned,
154 * false otherwise.
155 */
156 bool wasDestroyed() const { return NULL == fGpu; }
157
158 /**
159 * Retrieves the context that owns the object. Note that it is possible for
160 * this to return NULL. When objects have been release()ed or abandon()ed
161 * they no longer have an owning context. Destroying a GrContext
162 * automatically releases all its resources.
163 */
164 const GrContext* getContext() const;
165 GrContext* getContext();
166
bsalomonc44be0e2014-07-25 07:32:33 -0700167 /**
168 * Retrieves the amount of GPU memory used by this resource in bytes. It is
169 * approximate since we aren't aware of additional padding or copies made
170 * by the driver.
171 *
172 * @return the amount of GPU memory used in bytes
173 */
bsalomon69ed47f2014-11-12 11:13:39 -0800174 size_t gpuMemorySize() const {
175 if (kInvalidGpuMemorySize == fGpuMemorySize) {
176 fGpuMemorySize = this->onGpuMemorySize();
177 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize);
178 }
179 return fGpuMemorySize;
180 }
bsalomonc44be0e2014-07-25 07:32:33 -0700181
Robert Phillips294870f2016-11-11 12:38:40 -0500182 class UniqueID {
183 public:
184 static UniqueID InvalidID() {
185 return UniqueID(uint32_t(SK_InvalidUniqueID));
186 }
187
188 UniqueID() {}
189
190 explicit UniqueID(uint32_t id) : fID(id) {}
191
192 uint32_t asUInt() const { return fID; }
193
194 bool operator==(const UniqueID& other) const {
195 return fID == other.fID;
196 }
197 bool operator!=(const UniqueID& other) const {
198 return !(*this == other);
199 }
200
201 void makeInvalid() { fID = SK_InvalidUniqueID; }
202 bool isInvalid() const { return SK_InvalidUniqueID == fID; }
203
204 protected:
205 uint32_t fID;
206 };
207
bsalomonc44be0e2014-07-25 07:32:33 -0700208 /**
bsalomon52e9d632014-09-05 12:23:12 -0700209 * Gets an id that is unique for this GrGpuResource object. It is static in that it does
210 * not change when the content of the GrGpuResource object changes. This will never return
bsalomonc44be0e2014-07-25 07:32:33 -0700211 * 0.
212 */
Robert Phillips294870f2016-11-11 12:38:40 -0500213 UniqueID uniqueID() const { return fUniqueID; }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000214
bsalomon8718aaf2015-02-19 07:24:21 -0800215 /** Returns the current unique key for the resource. It will be invalid if the resource has no
216 associated unique key. */
217 const GrUniqueKey& getUniqueKey() const { return fUniqueKey; }
bsalomon563ff602015-02-02 17:25:26 -0800218
bsalomon453cf402014-11-11 14:15:57 -0800219 /**
bsalomon3582d3e2015-02-13 14:20:05 -0800220 * Internal-only helper class used for manipulations of the resource by the cache.
bsalomon453cf402014-11-11 14:15:57 -0800221 */
222 class CacheAccess;
223 inline CacheAccess cacheAccess();
224 inline const CacheAccess cacheAccess() const;
225
junov436293a2014-12-11 10:32:32 -0800226 /**
bsalomon3582d3e2015-02-13 14:20:05 -0800227 * Internal-only helper class used for manipulations of the resource by internal code.
228 */
229 class ResourcePriv;
230 inline ResourcePriv resourcePriv();
231 inline const ResourcePriv resourcePriv() const;
232
233 /**
junov436293a2014-12-11 10:32:32 -0800234 * Removes references to objects in the underlying 3D API without freeing them.
235 * Called by CacheAccess.
236 * In general this method should not be called outside of skia. It was
237 * made by public for a special case where it needs to be called in Blink
238 * when a texture becomes unsafe to use after having been shared through
239 * a texture mailbox.
240 */
241 void abandon();
242
ericrk0a5fa482015-09-15 14:16:10 -0700243 /**
244 * Dumps memory usage information for this GrGpuResource to traceMemoryDump.
245 * Typically, subclasses should not need to override this, and should only
246 * need to override setMemoryBacking.
247 **/
248 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
249
robertphillips8abb3702016-08-31 14:04:06 -0700250 static uint32_t CreateUniqueID();
251
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000252protected:
kkinnunen2e6055b2016-04-22 01:48:29 -0700253 // This must be called by every non-wrapped GrGpuObject. It should be called once the object is
254 // fully initialized (i.e. only from the constructors of the final class).
255 void registerWithCache(SkBudgeted);
bsalomon16961262014-08-26 14:01:07 -0700256
kkinnunen2e6055b2016-04-22 01:48:29 -0700257 // This must be called by every GrGpuObject that references any wrapped backend objects. It
258 // should be called once the object is fully initialized (i.e. only from the constructors of the
259 // final class).
260 void registerWithCacheWrapped();
261
262 GrGpuResource(GrGpu*);
bsalomon6d3fe022014-07-25 08:35:45 -0700263 virtual ~GrGpuResource();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000264
265 GrGpu* getGpu() const { return fGpu; }
266
bsalomon12299ab2014-11-14 13:33:09 -0800267 /** Overridden to free GPU resources in the backend API. */
268 virtual void onRelease() { }
269 /** Overridden to abandon any internal handles, ptrs, etc to backend API resources.
270 This may be called when the underlying 3D context is no longer valid and so no
271 backend API calls should be made. */
272 virtual void onAbandon() { }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000273
bsalomonc44be0e2014-07-25 07:32:33 -0700274 /**
bsalomon69ed47f2014-11-12 11:13:39 -0800275 * This entry point should be called whenever gpuMemorySize() should report a different size.
276 * The cache will call gpuMemorySize() to update the current size of the resource.
bsalomonc44be0e2014-07-25 07:32:33 -0700277 */
278 void didChangeGpuMemorySize() const;
279
bsalomon744998e2014-08-28 09:54:34 -0700280 /**
ericrk0a5fa482015-09-15 14:16:10 -0700281 * Allows subclasses to add additional backing information to the SkTraceMemoryDump. Called by
282 * onMemoryDump. The default implementation adds no backing information.
283 **/
284 virtual void setMemoryBacking(SkTraceMemoryDump*, const SkString&) const {}
285
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000286private:
bsalomon12299ab2014-11-14 13:33:09 -0800287 /**
kkinnunen2e6055b2016-04-22 01:48:29 -0700288 * Called by the registerWithCache if the resource is available to be used as scratch.
289 * Resource subclasses should override this if the instances should be recycled as scratch
290 * resources and populate the scratchKey with the key.
291 * By default resources are not recycled as scratch.
292 **/
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400293 virtual void computeScratchKey(GrScratchKey*) const { }
kkinnunen2e6055b2016-04-22 01:48:29 -0700294
295 /**
bsalomon12299ab2014-11-14 13:33:09 -0800296 * Frees the object in the underlying 3D API. Called by CacheAccess.
297 */
298 void release();
299
bsalomon69ed47f2014-11-12 11:13:39 -0800300 virtual size_t onGpuMemorySize() const = 0;
301
bsalomon9f2d1572015-02-17 11:47:40 -0800302 // See comments in CacheAccess and ResourcePriv.
bsalomonf99e9612015-02-19 08:24:16 -0800303 void setUniqueKey(const GrUniqueKey&);
bsalomon8718aaf2015-02-19 07:24:21 -0800304 void removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700305 void notifyAllCntsAreZero(CntType) const;
306 bool notifyRefCountIsZero() const;
bsalomon10e23ca2014-11-25 05:52:06 -0800307 void removeScratchKey();
bsalomonafe30052015-01-16 07:32:33 -0800308 void makeBudgeted();
bsalomonc2f35b72015-01-23 07:19:22 -0800309 void makeUnbudgeted();
bsalomonbcf0a522014-10-08 08:40:09 -0700310
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000311#ifdef SK_DEBUG
312 friend class GrGpu; // for assert in GrGpu to access getGpu
313#endif
bsalomonf320e042015-02-17 15:09:34 -0800314 // An index into a heap when this resource is purgeable or an array when not. This is maintained
315 // by the cache.
bsalomon9f2d1572015-02-17 11:47:40 -0800316 int fCacheArrayIndex;
317 // This value reflects how recently this resource was accessed in the cache. This is maintained
318 // by the cache.
319 uint32_t fTimestamp;
bsalomone2e87f32016-09-22 12:42:11 -0700320 uint32_t fExternalFlushCntWhenBecamePurgeable;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000321
bsalomon69ed47f2014-11-12 11:13:39 -0800322 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
bsalomon24db3b12015-01-23 04:24:04 -0800323 GrScratchKey fScratchKey;
bsalomon8718aaf2015-02-19 07:24:21 -0800324 GrUniqueKey fUniqueKey;
bsalomon84c8e622014-11-17 09:33:27 -0800325
326 // This is not ref'ed but abandon() or release() will be called before the GrGpu object
327 // is destroyed. Those calls set will this to NULL.
bsalomon24db3b12015-01-23 04:24:04 -0800328 GrGpu* fGpu;
329 mutable size_t fGpuMemorySize;
bsalomon84c8e622014-11-17 09:33:27 -0800330
kkinnunen2e6055b2016-04-22 01:48:29 -0700331 SkBudgeted fBudgeted;
332 bool fRefsWrappedObjects;
Robert Phillips294870f2016-11-11 12:38:40 -0500333 const UniqueID fUniqueID;
bsalomon744998e2014-08-28 09:54:34 -0700334
bsalomonbcf0a522014-10-08 08:40:09 -0700335 typedef GrIORef<GrGpuResource> INHERITED;
bsalomon3f324322015-04-08 11:01:54 -0700336 friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and notifyRefCntIsZero.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000337};
338
339#endif