| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrResource_DEFINED | 8 #ifndef GrGpuObject_DEFINED |
| 9 #define GrResource_DEFINED | 9 #define GrGpuObject_DEFINED |
| 10 | 10 |
| 11 #include "SkRefCnt.h" | 11 #include "GrCacheable.h" |
| 12 #include "SkTInternalLList.h" | 12 #include "SkTInternalLList.h" |
| 13 | 13 |
| 14 class GrGpu; | 14 class GrGpu; |
| 15 class GrContext; | 15 class GrContext; |
| 16 class GrResourceEntry; | |
| 17 | 16 |
| 18 /** | 17 /** |
| 19 * Base class for the GPU resources created by a GrContext. | 18 * Base class for the GPU objects created by a GrContext. |
| 20 */ | 19 */ |
| 21 class GrResource : public SkRefCnt { | 20 class GrGpuObject : public GrCacheable { |
| 22 public: | 21 public: |
| 23 SK_DECLARE_INST_COUNT(GrResource) | 22 SK_DECLARE_INST_COUNT(GrGpuObject) |
| 24 | 23 |
| 25 /** | 24 /** |
| 26 * Frees the resource in the underlying 3D API. It must be safe to call this | 25 * Frees the object in the underlying 3D API. It must be safe to call this |
| 27 * when the resource has been previously abandoned. | 26 * when the object has been previously abandoned. |
| 28 */ | 27 */ |
| 29 void release(); | 28 void release(); |
| 30 | 29 |
| 31 /** | 30 /** |
| 32 * Removes references to objects in the underlying 3D API without freeing | 31 * Removes references to objects in the underlying 3D API without freeing |
| 33 * them. Used when the API context has been torn down before the GrContext. | 32 * them. Used when the API context has been torn down before the GrContext. |
| 34 */ | 33 */ |
| 35 void abandon(); | 34 void abandon(); |
| 36 | 35 |
| 37 /** | 36 /** |
| 38 * Tests whether a resource has been abandoned or released. All resources | 37 * Tests whether a object has been abandoned or released. All objects will |
| 39 * will be in this state after their creating GrContext is destroyed or has | 38 * be in this state after their creating GrContext is destroyed or has |
| 40 * contextLost called. It's up to the client to test isValid() before | 39 * contextLost called. It's up to the client to test wasDestroyed() before |
| 41 * attempting to use a resource if it holds refs on resources across | 40 * attempting to use an object if it holds refs on objects across |
| 42 * ~GrContext, freeResources with the force flag, or contextLost. | 41 * ~GrContext, freeResources with the force flag, or contextLost. |
| 43 * | 42 * |
| 44 * @return true if the resource has been released or abandoned, | 43 * @return true if the object has been released or abandoned, |
| 45 * false otherwise. | 44 * false otherwise. |
| 46 */ | 45 */ |
| 47 bool isValid() const { return NULL != fGpu; } | 46 bool wasDestroyed() const { return NULL == fGpu; } |
| 48 | 47 |
| 49 /** | 48 /** |
| 50 * Retrieves the size of the object in GPU memory. This is approximate since | 49 * Retrieves the context that owns the object. Note that it is possible for |
| 51 * we aren't aware of additional padding or copies made by the driver. | 50 * this to return NULL. When objects have been release()ed or abandon()ed |
| 52 * | 51 * they no longer have an owning context. Destroying a GrContext |
| 53 * @return the size of the buffer in bytes | 52 * automatically releases all its resources. |
| 54 */ | |
| 55 virtual size_t sizeInBytes() const = 0; | |
| 56 | |
| 57 /** | |
| 58 * Retrieves the context that owns the resource. Note that it is possible | |
| 59 * for this to return NULL. When resources have been release()ed or | |
| 60 * abandon()ed they no longer have an owning context. Destroying a | |
| 61 * GrContext automatically releases all its resources. | |
| 62 */ | 53 */ |
| 63 const GrContext* getContext() const; | 54 const GrContext* getContext() const; |
| 64 GrContext* getContext(); | 55 GrContext* getContext(); |
| 65 | 56 |
| 66 void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry;
} | |
| 67 GrResourceEntry* getCacheEntry() { return fCacheEntry; } | |
| 68 | |
| 69 void incDeferredRefCount() const { | 57 void incDeferredRefCount() const { |
| 70 SkASSERT(fDeferredRefCount >= 0); | 58 SkASSERT(fDeferredRefCount >= 0); |
| 71 ++fDeferredRefCount; | 59 ++fDeferredRefCount; |
| 72 } | 60 } |
| 73 | 61 |
| 74 void decDeferredRefCount() const { | 62 void decDeferredRefCount() const { |
| 75 SkASSERT(fDeferredRefCount > 0); | 63 SkASSERT(fDeferredRefCount > 0); |
| 76 --fDeferredRefCount; | 64 --fDeferredRefCount; |
| 77 if (0 == fDeferredRefCount && this->needsDeferredUnref()) { | 65 if (0 == fDeferredRefCount && this->needsDeferredUnref()) { |
| 78 SkASSERT(this->getRefCnt() > 1); | 66 SkASSERT(this->getRefCnt() > 1); |
| 79 this->unref(); | 67 this->unref(); |
| 80 } | 68 } |
| 81 } | 69 } |
| 82 | 70 |
| 83 int getDeferredRefCount() const { return fDeferredRefCount; } | 71 int getDeferredRefCount() const { return fDeferredRefCount; } |
| 84 | 72 |
| 85 void setNeedsDeferredUnref() { fFlags |= kDeferredUnref_FlagBit; } | 73 void setNeedsDeferredUnref() { fFlags |= kDeferredUnref_FlagBit; } |
| 86 | 74 |
| 75 virtual bool isValidOnGpu() const SK_OVERRIDE { return !this->wasDestroyed()
; } |
| 76 |
| 87 protected: | 77 protected: |
| 88 /** | 78 /** |
| 89 * isWrapped indicates we have wrapped a client-created backend resource in
a GrResource. If it | 79 * isWrapped indicates we have wrapped a client-created backend object in a
GrGpuObject. If it |
| 90 * is true then the client is responsible for the lifetime of the underlying
backend resource. | 80 * is true then the client is responsible for the lifetime of the underlying
backend object. |
| 91 * Otherwise, our onRelease() should free the resource. | 81 * Otherwise, our onRelease() should free the object. |
| 92 */ | 82 */ |
| 93 GrResource(GrGpu* gpu, bool isWrapped); | 83 GrGpuObject(GrGpu* gpu, bool isWrapped); |
| 94 virtual ~GrResource(); | 84 virtual ~GrGpuObject(); |
| 95 | 85 |
| 96 GrGpu* getGpu() const { return fGpu; } | 86 GrGpu* getGpu() const { return fGpu; } |
| 97 | 87 |
| 98 // Derived classes should always call their parent class' onRelease | 88 // Derived classes should always call their parent class' onRelease |
| 99 // and onAbandon methods in their overrides. | 89 // and onAbandon methods in their overrides. |
| 100 virtual void onRelease() {}; | 90 virtual void onRelease() {}; |
| 101 virtual void onAbandon() {}; | 91 virtual void onAbandon() {}; |
| 102 | 92 |
| 103 bool isInCache() const { return NULL != fCacheEntry; } | |
| 104 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } | 93 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } |
| 105 bool needsDeferredUnref() const { return SkToBool(kDeferredUnref_FlagBit & f
Flags); } | 94 bool needsDeferredUnref() const { return SkToBool(kDeferredUnref_FlagBit & f
Flags); } |
| 106 | 95 |
| 107 private: | 96 private: |
| 108 #ifdef SK_DEBUG | 97 #ifdef SK_DEBUG |
| 109 friend class GrGpu; // for assert in GrGpu to access getGpu | 98 friend class GrGpu; // for assert in GrGpu to access getGpu |
| 110 #endif | 99 #endif |
| 111 | 100 |
| 112 // We're in an internal doubly linked list | 101 // We're in an internal doubly linked list |
| 113 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); | 102 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuObject); |
| 114 | 103 |
| 115 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there | 104 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there |
| 116 // are still live GrResources. It wi
ll call | 105 // are still live GrGpuObjects. It w
ill call |
| 117 // release() on all such resources i
n its | 106 // release() on all such objects in
its destructor. |
| 118 // destructor. | |
| 119 GrResourceEntry* fCacheEntry; // NULL if not in cache | |
| 120 mutable int fDeferredRefCount; // How many references in deferred d
rawing buffers. | 107 mutable int fDeferredRefCount; // How many references in deferred d
rawing buffers. |
| 121 | 108 |
| 122 enum Flags { | 109 enum Flags { |
| 123 /** | 110 /** |
| 124 * This resource wraps a GPU resource given to us by the user. | 111 * This object wraps a GPU object given to us by the user. |
| 125 * Lifetime management is left up to the user (i.e., we will not | 112 * Lifetime management is left up to the user (i.e., we will not |
| 126 * free it). | 113 * free it). |
| 127 */ | 114 */ |
| 128 kWrapped_FlagBit = 0x1, | 115 kWrapped_FlagBit = 0x1, |
| 129 | 116 |
| 130 /** | 117 /** |
| 131 * This texture should be de-refed when the deferred ref count goes | 118 * This texture should be de-refed when the deferred ref count goes |
| 132 * to zero. A resource gets into this state when the resource cache | 119 * to zero. An object gets into this state when the resource cache |
| 133 * is holding a ref-of-obligation (i.e., someone needs to own it but | 120 * is holding a ref-of-obligation (i.e., someone needs to own it but |
| 134 * no one else wants to) but doesn't really want to keep it around. | 121 * no one else wants to) but doesn't really want to keep it around. |
| 135 */ | 122 */ |
| 136 kDeferredUnref_FlagBit = 0x2, | 123 kDeferredUnref_FlagBit = 0x2, |
| 137 }; | 124 }; |
| 138 uint32_t fFlags; | 125 uint32_t fFlags; |
| 139 | 126 |
| 140 typedef SkRefCnt INHERITED; | 127 typedef GrCacheable INHERITED; |
| 141 }; | 128 }; |
| 142 | 129 |
| 143 #endif | 130 #endif |
| OLD | NEW |