OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 GrGpuResource_DEFINED | 8 #ifndef GrGpuResource_DEFINED |
9 #define GrGpuResource_DEFINED | 9 #define GrGpuResource_DEFINED |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 this->validate(); | 49 this->validate(); |
50 ++fRefCnt; | 50 ++fRefCnt; |
51 } | 51 } |
52 | 52 |
53 void unref() const { | 53 void unref() const { |
54 this->validate(); | 54 this->validate(); |
55 --fRefCnt; | 55 --fRefCnt; |
56 this->didUnref(); | 56 this->didUnref(); |
57 } | 57 } |
58 | 58 |
59 bool isPurgable() const { return this->reffedOnlyByCache() && !this->interna
lHasPendingIO(); } | |
60 bool reffedOnlyByCache() const { return 1 == fRefCnt; } | |
61 | |
62 void validate() const { | 59 void validate() const { |
63 #ifdef SK_DEBUG | 60 #ifdef SK_DEBUG |
64 SkASSERT(fRefCnt >= 0); | 61 SkASSERT(fRefCnt >= 0); |
65 SkASSERT(fPendingReads >= 0); | 62 SkASSERT(fPendingReads >= 0); |
66 SkASSERT(fPendingWrites >= 0); | 63 SkASSERT(fPendingWrites >= 0); |
67 SkASSERT(fRefCnt + fPendingReads + fPendingWrites > 0); | 64 SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 0); |
68 #endif | 65 #endif |
69 } | 66 } |
70 | 67 |
71 protected: | 68 protected: |
72 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { } | 69 GrIORef() : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { } |
73 | 70 |
| 71 bool isPurgable() const { return !this->internalHasRef() && !this->internalH
asPendingIO(); } |
| 72 |
74 bool internalHasPendingRead() const { return SkToBool(fPendingReads); } | 73 bool internalHasPendingRead() const { return SkToBool(fPendingReads); } |
75 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); } | 74 bool internalHasPendingWrite() const { return SkToBool(fPendingWrites); } |
76 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendin
gReads); } | 75 bool internalHasPendingIO() const { return SkToBool(fPendingWrites | fPendin
gReads); } |
77 | 76 |
78 bool internalHasRef() const { return SkToBool(fRefCnt); } | 77 bool internalHasRef() const { return SkToBool(fRefCnt); } |
79 | 78 |
80 private: | 79 private: |
81 void addPendingRead() const { | 80 void addPendingRead() const { |
82 this->validate(); | 81 this->validate(); |
83 ++fPendingReads; | 82 ++fPendingReads; |
(...skipping 11 matching lines...) Expand all Loading... |
95 } | 94 } |
96 | 95 |
97 void completedWrite() const { | 96 void completedWrite() const { |
98 this->validate(); | 97 this->validate(); |
99 --fPendingWrites; | 98 --fPendingWrites; |
100 this->didUnref(); | 99 this->didUnref(); |
101 } | 100 } |
102 | 101 |
103 private: | 102 private: |
104 void didUnref() const { | 103 void didUnref() const { |
105 if (0 == fPendingReads && 0 == fPendingWrites) { | 104 if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) { |
106 if (0 == fRefCnt) { | 105 static_cast<const DERIVED*>(this)->notifyIsPurgable(); |
107 // Must call derived destructor since this is not a virtual clas
s. | |
108 SkDELETE(static_cast<const DERIVED*>(this)); | |
109 } else if (1 == fRefCnt) { | |
110 // The one ref is the cache's | |
111 static_cast<const DERIVED*>(this)->notifyIsPurgable(); | |
112 } | |
113 } | 106 } |
114 } | 107 } |
115 | 108 |
116 mutable int32_t fRefCnt; | 109 mutable int32_t fRefCnt; |
117 mutable int32_t fPendingReads; | 110 mutable int32_t fPendingReads; |
118 mutable int32_t fPendingWrites; | 111 mutable int32_t fPendingWrites; |
119 | 112 |
120 // This class is used to manage conversion of refs to pending reads/writes. | 113 // This class is used to manage conversion of refs to pending reads/writes. |
121 friend class GrGpuResourceRef; | 114 friend class GrGpuResourceRef; |
122 friend class GrResourceCache2; // to check IO ref counts. | 115 friend class GrResourceCache2; // to check IO ref counts. |
123 | 116 |
124 template <typename, GrIOType> friend class GrPendingIOResource; | 117 template <typename, GrIOType> friend class GrPendingIOResource; |
125 }; | 118 }; |
126 | 119 |
127 /** | 120 /** |
128 * Base class for objects that can be kept in the GrResourceCache2. | 121 * Base class for objects that can be kept in the GrResourceCache2. |
129 */ | 122 */ |
130 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { | 123 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { |
131 public: | 124 public: |
132 SK_DECLARE_INST_COUNT(GrGpuResource) | 125 SK_DECLARE_INST_COUNT(GrGpuResource) |
133 | 126 |
134 /** | 127 /** |
135 * Frees the object in the underlying 3D API. It must be safe to call this | |
136 * when the object has been previously abandoned. | |
137 */ | |
138 void release(); | |
139 | |
140 /** | |
141 * Removes references to objects in the underlying 3D API without freeing | |
142 * them. Used when the API context has been torn down before the GrContext. | |
143 */ | |
144 void abandon(); | |
145 | |
146 /** | |
147 * Tests whether a object has been abandoned or released. All objects will | 128 * 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 | 129 * 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 | 130 * 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 | 131 * attempting to use an object if it holds refs on objects across |
151 * ~GrContext, freeResources with the force flag, or contextLost. | 132 * ~GrContext, freeResources with the force flag, or contextLost. |
152 * | 133 * |
153 * @return true if the object has been released or abandoned, | 134 * @return true if the object has been released or abandoned, |
154 * false otherwise. | 135 * false otherwise. |
155 */ | 136 */ |
156 bool wasDestroyed() const { return NULL == fGpu; } | 137 bool wasDestroyed() const { return NULL == fGpu; } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 protected: | 177 protected: |
197 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully | 178 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully |
198 // initialized (i.e. not in a base class constructor). | 179 // initialized (i.e. not in a base class constructor). |
199 void registerWithCache(); | 180 void registerWithCache(); |
200 | 181 |
201 GrGpuResource(GrGpu*, bool isWrapped); | 182 GrGpuResource(GrGpu*, bool isWrapped); |
202 virtual ~GrGpuResource(); | 183 virtual ~GrGpuResource(); |
203 | 184 |
204 GrGpu* getGpu() const { return fGpu; } | 185 GrGpu* getGpu() const { return fGpu; } |
205 | 186 |
206 // Derived classes should always call their parent class' onRelease | 187 /** Overridden to free GPU resources in the backend API. */ |
207 // and onAbandon methods in their overrides. | 188 virtual void onRelease() { } |
208 virtual void onRelease() {}; | 189 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. |
209 virtual void onAbandon() {}; | 190 This may be called when the underlying 3D context is no longer valid and
so no |
| 191 backend API calls should be made. */ |
| 192 virtual void onAbandon() { } |
210 | 193 |
211 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } | 194 bool isWrapped() const { return kWrapped_FlagBit & fFlags; } |
212 | 195 |
213 /** | 196 /** |
214 * This entry point should be called whenever gpuMemorySize() should report
a different size. | 197 * This entry point should be called whenever gpuMemorySize() should report
a different size. |
215 * The cache will call gpuMemorySize() to update the current size of the res
ource. | 198 * The cache will call gpuMemorySize() to update the current size of the res
ource. |
216 */ | 199 */ |
217 void didChangeGpuMemorySize() const; | 200 void didChangeGpuMemorySize() const; |
218 | 201 |
219 /** | 202 /** |
220 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. | 203 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. |
221 * By default resources are not usable as scratch. This should only be calle
d once. | 204 * By default resources are not usable as scratch. This should only be calle
d once. |
222 **/ | 205 **/ |
223 void setScratchKey(const GrResourceKey& scratchKey); | 206 void setScratchKey(const GrResourceKey& scratchKey); |
224 | 207 |
225 private: | 208 private: |
| 209 /** |
| 210 * Frees the object in the underlying 3D API. Called by CacheAccess. |
| 211 */ |
| 212 void release(); |
| 213 |
| 214 /** |
| 215 * Removes references to objects in the underlying 3D API without freeing th
em. |
| 216 * Called by CacheAccess. |
| 217 */ |
| 218 void abandon(); |
| 219 |
226 virtual size_t onGpuMemorySize() const = 0; | 220 virtual size_t onGpuMemorySize() const = 0; |
227 | 221 |
228 // See comments in CacheAccess. | 222 // See comments in CacheAccess. |
229 bool setContentKey(const GrResourceKey& contentKey); | 223 bool setContentKey(const GrResourceKey& contentKey); |
230 | 224 |
231 void notifyIsPurgable() const; | 225 void notifyIsPurgable() const; |
232 | 226 |
233 #ifdef SK_DEBUG | 227 #ifdef SK_DEBUG |
234 friend class GrGpu; // for assert in GrGpu to access getGpu | 228 friend class GrGpu; // for assert in GrGpu to access getGpu |
235 #endif | 229 #endif |
(...skipping 27 matching lines...) Expand all Loading... |
263 // keys. | 257 // keys. |
264 GrResourceKey fScratchKey; | 258 GrResourceKey fScratchKey; |
265 GrResourceKey fContentKey; | 259 GrResourceKey fContentKey; |
266 bool fContentKeySet; | 260 bool fContentKeySet; |
267 | 261 |
268 typedef GrIORef<GrGpuResource> INHERITED; | 262 typedef GrIORef<GrGpuResource> INHERITED; |
269 friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable. | 263 friend class GrIORef<GrGpuResource>; // to access notifyIsPurgable. |
270 }; | 264 }; |
271 | 265 |
272 #endif | 266 #endif |
OLD | NEW |