2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGpuResource_DEFINED
9 #define GrGpuResource_DEFINED
11 #include "GrResourceKey.h"
12 #include "GrTypesPriv.h"
13 #include "SkInstCnt.h"
14 #include "SkTInternalLList.h"
18 class GrResourceCache2;
19 class GrResourceCacheEntry;
22 * Base class for GrGpuResource. Handles the various types of refs we need. Separated out as a base
23 * class to isolate the ref-cnting behavior and provide friendship without exposing all of
26 * Gpu resources can have three types of refs:
27 * 1) Normal ref (+ by ref(), - by unref()): These are used by code that is issuing draw calls
28 * that read and write the resource via GrDrawTarget and by any object that must own a
29 * GrGpuResource and is itself owned (directly or indirectly) by Skia-client code.
30 * 2) Pending read (+ by addPendingRead(), - by completedRead()): GrContext has scheduled a read
31 * of the resource by the GPU as a result of a skia API call but hasn't executed it yet.
32 * 3) Pending write (+ by addPendingWrite(), - by completedWrite()): GrContext has scheduled a
33 * write to the resource by the GPU as a result of a skia API call but hasn't executed it yet.
35 * The latter two ref types are private and intended only for Gr core code.
37 * When an item is purgable DERIVED:notifyIsPurgable() will be called (static poly morphism using
38 * CRTP). GrIORef and GrGpuResource are separate classes for organizational reasons and to be
39 * able to give access via friendship to only the functions related to pending IO operations.
41 template <typename DERIVED> class GrIORef : public SkNoncopyable {
43 SK_DECLARE_INST_COUNT_ROOT(GrIORef)
45 // Some of the signatures are written to mirror SkRefCnt so that GrGpuResource can work with
46 // templated helper classes (e.g. SkAutoTUnref). However, we have different categories of
47 // refs (e.g. pending reads). We also don't require thread safety as GrCacheable objects are
48 // not intended to cross thread boundaries.
60 bool isPurgable() const { return this->reffedOnlyByCache() && !this->internalHasPendingIO(); }
61 bool reffedOnlyByCache() const { return 1 == fRefCnt; }
63 void validate() const {
65 SkASSERT(fRefCnt >= 0);
66 SkASSERT(fPendingReads >= 0);
67 SkASSERT(fPendingWrites >= 0);
68 SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0);
73 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { }
75 bool internalHasPendingRead() const { return SkToBool(fPendingReads); }
76 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); }
77 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendingReads); }
80 void addPendingRead() const {
85 void completedRead() const {
91 void addPendingWrite() const {
96 void completedWrite() const {
103 void didUnref() const {
104 if (0 == fPendingReads && 0 == fPendingWrites) {
106 // Must call derived destructor since this is not a virtual class.
107 SkDELETE(static_cast<const DERIVED*>(this));
108 } else if (1 == fRefCnt) {
109 // The one ref is the cache's
110 static_cast<const DERIVED*>(this)->notifyIsPurgable();
115 mutable int32_t fRefCnt;
116 mutable int32_t fPendingReads;
117 mutable int32_t fPendingWrites;
119 // This class is used to manage conversion of refs to pending reads/writes.
120 friend class GrGpuResourceRef;
121 friend class GrResourceCache2; // to check IO ref counts.
123 template <typename, GrIOType> friend class GrPendingIOResource;
127 * Base class for objects that can be kept in the GrResourceCache.
129 class SK_API GrGpuResource : public GrIORef<GrGpuResource> {
131 SK_DECLARE_INST_COUNT(GrGpuResource)
134 * Frees the object in the underlying 3D API. It must be safe to call this
135 * when the object has been previously abandoned.
140 * Removes references to objects in the underlying 3D API without freeing
141 * them. Used when the API context has been torn down before the GrContext.
146 * 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.
152 * @return true if the object has been released or abandoned,
155 bool wasDestroyed() const { return NULL == fGpu; }
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.
163 const GrContext* getContext() const;
164 GrContext* getContext();
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
171 * @return the amount of GPU memory used in bytes
173 virtual size_t gpuMemorySize() const = 0;
175 void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; }
176 GrResourceCacheEntry* getCacheEntry() const { return fCacheEntry; }
177 bool isScratch() const;
180 * If this resource can be used as a scratch resource this returns a valid
181 * scratch key. Otherwise it returns a key for which isNullScratch is true.
183 const GrResourceKey& getScratchKey() const { return fScratchKey; }
186 * If this resource is currently cached by its contents then this will return
187 * the content key. Otherwise, NULL is returned.
189 const GrResourceKey* getContentKey() const;
192 * Gets an id that is unique for this GrGpuResource object. It is static in that it does
193 * not change when the content of the GrGpuResource object changes. This will never return
196 uint32_t getUniqueID() const { return fUniqueID; }
199 // This must be called by every GrGpuObject. It should be called once the object is fully
200 // initialized (i.e. not in a base class constructor).
201 void registerWithCache();
203 GrGpuResource(GrGpu*, bool isWrapped);
204 virtual ~GrGpuResource();
206 bool isInCache() const { return SkToBool(fCacheEntry); }
208 GrGpu* getGpu() const { return fGpu; }
210 // Derived classes should always call their parent class' onRelease
211 // and onAbandon methods in their overrides.
212 virtual void onRelease() {};
213 virtual void onAbandon() {};
215 bool isWrapped() const { return kWrapped_FlagBit & fFlags; }
218 * This entry point should be called whenever gpuMemorySize() begins
219 * reporting a different size. If the object is in the cache, it will call
220 * gpuMemorySize() immediately and pass the new size on to the resource
223 void didChangeGpuMemorySize() const;
226 * Optionally called by the GrGpuResource subclass if the resource can be used as scratch.
227 * By default resources are not usable as scratch. This should only be called once.
229 void setScratchKey(const GrResourceKey& scratchKey);
232 void notifyIsPurgable() const;
235 friend class GrGpu; // for assert in GrGpu to access getGpu
238 static uint32_t CreateUniqueID();
240 // We're in an internal doubly linked list owned by GrResourceCache2
241 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
243 // This is not ref'ed but abandon() or release() will be called before the GrGpu object
244 // is destroyed. Those calls set will this to NULL.
249 * This object wraps a GPU object given to us by the user.
250 * Lifetime management is left up to the user (i.e., we will not
253 kWrapped_FlagBit = 0x1,
258 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache
259 const uint32_t fUniqueID;
261 GrResourceKey fScratchKey;
263 typedef GrIORef<GrGpuResource> INHERITED;
264 friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable.