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 "SkInstCnt.h"
12 #include "SkTInternalLList.h"
14 class GrResourceCacheEntry;
19 * Base class for objects that can be kept in the GrResourceCache.
21 class GrGpuResource : public SkNoncopyable {
23 SK_DECLARE_INST_COUNT_ROOT(GrGpuResource)
25 // These method signatures are written to mirror SkRefCnt. However, we don't require
26 // thread safety as GrCacheable objects are not intended to cross thread boundaries.
27 // internal_dispose() exists because of GrTexture's reliance on it. It will be removed
29 void ref() const { ++fRefCnt; }
30 void unref() const { --fRefCnt; if (0 == fRefCnt) { this->internal_dispose(); } }
31 virtual void internal_dispose() const { SkDELETE(this); }
32 bool unique() const { return 1 == fRefCnt; }
34 void validate() const {
35 SkASSERT(fRefCnt > 0);
40 * Frees the object in the underlying 3D API. It must be safe to call this
41 * when the object has been previously abandoned.
46 * Removes references to objects in the underlying 3D API without freeing
47 * them. Used when the API context has been torn down before the GrContext.
52 * Tests whether a object has been abandoned or released. All objects will
53 * be in this state after their creating GrContext is destroyed or has
54 * contextLost called. It's up to the client to test wasDestroyed() before
55 * attempting to use an object if it holds refs on objects across
56 * ~GrContext, freeResources with the force flag, or contextLost.
58 * @return true if the object has been released or abandoned,
61 bool wasDestroyed() const { return NULL == fGpu; }
64 * Retrieves the context that owns the object. Note that it is possible for
65 * this to return NULL. When objects have been release()ed or abandon()ed
66 * they no longer have an owning context. Destroying a GrContext
67 * automatically releases all its resources.
69 const GrContext* getContext() const;
70 GrContext* getContext();
73 * Retrieves the amount of GPU memory used by this resource in bytes. It is
74 * approximate since we aren't aware of additional padding or copies made
77 * @return the amount of GPU memory used in bytes
79 virtual size_t gpuMemorySize() const = 0;
81 void setCacheEntry(GrResourceCacheEntry* cacheEntry) { fCacheEntry = cacheEntry; }
82 GrResourceCacheEntry* getCacheEntry() { return fCacheEntry; }
85 * Gets an id that is unique for this GrCacheable object. It is static in that it does
86 * not change when the content of the GrCacheable object changes. This will never return
89 uint32_t getUniqueID() const { return fUniqueID; }
92 GrGpuResource(GrGpu*, bool isWrapped);
93 virtual ~GrGpuResource();
95 bool isInCache() const { return NULL != fCacheEntry; }
97 GrGpu* getGpu() const { return fGpu; }
99 // Derived classes should always call their parent class' onRelease
100 // and onAbandon methods in their overrides.
101 virtual void onRelease() {};
102 virtual void onAbandon() {};
104 bool isWrapped() const { return kWrapped_FlagBit & fFlags; }
107 * This entry point should be called whenever gpuMemorySize() begins
108 * reporting a different size. If the object is in the cache, it will call
109 * gpuMemorySize() immediately and pass the new size on to the resource
112 void didChangeGpuMemorySize() const;
116 friend class GrGpu; // for assert in GrGpu to access getGpu
119 static uint32_t CreateUniqueID();
121 // We're in an internal doubly linked list
122 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
124 GrGpu* fGpu; // not reffed. The GrGpu can be deleted while there
125 // are still live GrGpuResources. It will call
126 // release() on all such objects in its destructor.
129 * This object wraps a GPU object given to us by the user.
130 * Lifetime management is left up to the user (i.e., we will not
133 kWrapped_FlagBit = 0x1,
138 mutable int32_t fRefCnt;
139 GrResourceCacheEntry* fCacheEntry; // NULL if not in cache
140 const uint32_t fUniqueID;
142 typedef SkNoncopyable INHERITED;