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 GrResource_DEFINED |
9 #define GrResource_DEFINED | 9 #define GrResource_DEFINED |
10 | 10 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 * for this to return NULL. When resources have been release()ed or | 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 | 60 * abandon()ed they no longer have an owning context. Destroying a |
61 * GrContext automatically releases all its resources. | 61 * GrContext automatically releases all its resources. |
62 */ | 62 */ |
63 const GrContext* getContext() const; | 63 const GrContext* getContext() const; |
64 GrContext* getContext(); | 64 GrContext* getContext(); |
65 | 65 |
66 void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry;
} | 66 void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry;
} |
67 GrResourceEntry* getCacheEntry() { return fCacheEntry; } | 67 GrResourceEntry* getCacheEntry() { return fCacheEntry; } |
68 | 68 |
69 void incDeferredRefCount() const { SkASSERT(fDeferredRefCount >= 0); ++fDefe
rredRefCount; } | 69 void incDeferredRefCount() const { |
70 void decDeferredRefCount() const { SkASSERT(fDeferredRefCount > 0); --fDefer
redRefCount; } | 70 SkASSERT(fDeferredRefCount >= 0); |
| 71 ++fDeferredRefCount; |
| 72 } |
| 73 |
| 74 void decDeferredRefCount() const { |
| 75 SkASSERT(fDeferredRefCount > 0); |
| 76 --fDeferredRefCount; |
| 77 if (0 == fDeferredRefCount && this->needsDeferredUnref()) { |
| 78 SkASSERT(this->getRefCnt() > 1); |
| 79 this->unref(); |
| 80 } |
| 81 } |
| 82 |
71 int getDeferredRefCount() const { return fDeferredRefCount; } | 83 int getDeferredRefCount() const { return fDeferredRefCount; } |
72 | 84 |
| 85 void setNeedsDeferredUnref() { fFlags |= kDeferredUnref_FlagBit; } |
| 86 |
73 protected: | 87 protected: |
74 /** | 88 /** |
75 * isWrapped indicates we have wrapped a client-created backend resource in
a GrResource. If it | 89 * isWrapped indicates we have wrapped a client-created backend resource in
a GrResource. If it |
76 * is true then the client is responsible for the lifetime of the underlying
backend resource. | 90 * is true then the client is responsible for the lifetime of the underlying
backend resource. |
77 * Otherwise, our onRelease() should free the resource. | 91 * Otherwise, our onRelease() should free the resource. |
78 */ | 92 */ |
79 GrResource(GrGpu* gpu, bool isWrapped); | 93 GrResource(GrGpu* gpu, bool isWrapped); |
80 virtual ~GrResource(); | 94 virtual ~GrResource(); |
81 | 95 |
82 GrGpu* getGpu() const { return fGpu; } | 96 GrGpu* getGpu() const { return fGpu; } |
83 | 97 |
84 // Derived classes should always call their parent class' onRelease | 98 // Derived classes should always call their parent class' onRelease |
85 // and onAbandon methods in their overrides. | 99 // and onAbandon methods in their overrides. |
86 virtual void onRelease() {}; | 100 virtual void onRelease() {}; |
87 virtual void onAbandon() {}; | 101 virtual void onAbandon() {}; |
88 | 102 |
89 bool isInCache() const { return NULL != fCacheEntry; } | 103 bool isInCache() const { return NULL != fCacheEntry; } |
90 bool isWrapped() const { return kWrapped_Flag & fFlags; } | 104 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } |
| 105 bool needsDeferredUnref() const { return SkToBool(kDeferredUnref_FlagBit & f
Flags); } |
91 | 106 |
92 private: | 107 private: |
93 #ifdef SK_DEBUG | 108 #ifdef SK_DEBUG |
94 friend class GrGpu; // for assert in GrGpu to access getGpu | 109 friend class GrGpu; // for assert in GrGpu to access getGpu |
95 #endif | 110 #endif |
96 | 111 |
97 // We're in an internal doubly linked list | 112 // We're in an internal doubly linked list |
98 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); | 113 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); |
99 | 114 |
100 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there | 115 GrGpu* fGpu; // not reffed. The GrGpu can be dele
ted while there |
101 // are still live GrResources. It wi
ll call | 116 // are still live GrResources. It wi
ll call |
102 // release() on all such resources i
n its | 117 // release() on all such resources i
n its |
103 // destructor. | 118 // destructor. |
104 GrResourceEntry* fCacheEntry; // NULL if not in cache | 119 GrResourceEntry* fCacheEntry; // NULL if not in cache |
105 mutable int fDeferredRefCount; // How many references in deferred d
rawing buffers. | 120 mutable int fDeferredRefCount; // How many references in deferred d
rawing buffers. |
106 | 121 |
107 enum Flags { | 122 enum Flags { |
108 kWrapped_Flag = 0x1, | 123 /** |
| 124 * This resource wraps a GPU resource given to us by the user. |
| 125 * Lifetime management is left up to the user (i.e., we will not |
| 126 * free it). |
| 127 */ |
| 128 kWrapped_FlagBit = 0x1, |
| 129 |
| 130 /** |
| 131 * 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 |
| 133 * 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. |
| 135 */ |
| 136 kDeferredUnref_FlagBit = 0x2, |
109 }; | 137 }; |
110 uint32_t fFlags; | 138 uint32_t fFlags; |
111 | 139 |
112 typedef SkRefCnt INHERITED; | 140 typedef SkRefCnt INHERITED; |
113 }; | 141 }; |
114 | 142 |
115 #endif | 143 #endif |
OLD | NEW |