OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | |
10 | |
11 #ifndef GrResourceCache_DEFINED | 9 #ifndef GrResourceCache_DEFINED |
12 #define GrResourceCache_DEFINED | 10 #define GrResourceCache_DEFINED |
13 | 11 |
| 12 #include "GrDrawTargetCaps.h" |
14 #include "GrResourceKey.h" | 13 #include "GrResourceKey.h" |
15 #include "SkTMultiMap.h" | 14 #include "SkTMultiMap.h" |
16 #include "SkMessageBus.h" | 15 #include "SkMessageBus.h" |
17 #include "SkTInternalLList.h" | 16 #include "SkTInternalLList.h" |
18 | 17 |
19 class GrGpuResource; | 18 class GrGpuResource; |
20 class GrResourceCache; | 19 class GrResourceCache; |
21 class GrResourceCacheEntry; | 20 class GrResourceCacheEntry; |
22 | 21 |
23 | 22 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 * list backwards from the tail, since those are the least recently used. | 80 * list backwards from the tail, since those are the least recently used. |
82 * | 81 * |
83 * For fast searches, we maintain a hash map based on the GrResourceKey. | 82 * For fast searches, we maintain a hash map based on the GrResourceKey. |
84 * | 83 * |
85 * It is a goal to make the GrResourceCache the central repository and bookkeep
er | 84 * It is a goal to make the GrResourceCache the central repository and bookkeep
er |
86 * of all resources. It should replace the linked list of GrGpuResources that | 85 * of all resources. It should replace the linked list of GrGpuResources that |
87 * GrGpu uses to call abandon/release. | 86 * GrGpu uses to call abandon/release. |
88 */ | 87 */ |
89 class GrResourceCache { | 88 class GrResourceCache { |
90 public: | 89 public: |
91 GrResourceCache(int maxCount, size_t maxBytes); | 90 GrResourceCache(const GrDrawTargetCaps*, int maxCount, size_t maxBytes); |
92 ~GrResourceCache(); | 91 ~GrResourceCache(); |
93 | 92 |
94 /** | 93 /** |
95 * Return the current resource cache limits. | 94 * Return the current resource cache limits. |
96 * | 95 * |
97 * @param maxResource If non-null, returns maximum number of resources | 96 * @param maxResource If non-null, returns maximum number of resources |
98 * that can be held in the cache. | 97 * that can be held in the cache. |
99 * @param maxBytes If non-null, returns maximum number of bytes of | 98 * @param maxBytes If non-null, returns maximum number of bytes of |
100 * gpu memory that can be held in the cache. | 99 * gpu memory that can be held in the cache. |
101 */ | 100 */ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 /** | 133 /** |
135 * Returns the number of bytes consumed by cached resources. | 134 * Returns the number of bytes consumed by cached resources. |
136 */ | 135 */ |
137 size_t getCachedResourceBytes() const { return fEntryBytes; } | 136 size_t getCachedResourceBytes() const { return fEntryBytes; } |
138 | 137 |
139 /** | 138 /** |
140 * Returns the number of cached resources. | 139 * Returns the number of cached resources. |
141 */ | 140 */ |
142 int getCachedResourceCount() const { return fEntryCount; } | 141 int getCachedResourceCount() const { return fEntryCount; } |
143 | 142 |
144 // For a found or added resource to be completely exclusive to the caller | |
145 // both the kNoOtherOwners and kHide flags need to be specified | |
146 enum OwnershipFlags { | |
147 kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other
owners | |
148 kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future
'find's | |
149 }; | |
150 | |
151 /** | 143 /** |
152 * Search for an entry with the same Key. If found, return it. | 144 * Search for an entry with the same Key. If found, return it. |
153 * If not found, return null. | 145 * If not found, return null. |
154 * If ownershipFlags includes kNoOtherOwners and a resource is returned | |
155 * then that resource has no other refs to it. | |
156 * If ownershipFlags includes kHide and a resource is returned then that | |
157 * resource will not be returned from future 'find' calls until it is | |
158 * 'freed' (and recycled) or makeNonExclusive is called. | |
159 * For a resource to be completely exclusive to a caller both kNoOtherOwner
s | |
160 * and kHide must be specified. | |
161 */ | 146 */ |
162 GrGpuResource* find(const GrResourceKey& key, | 147 GrGpuResource* find(const GrResourceKey& key); |
163 uint32_t ownershipFlags = 0); | 148 |
| 149 void makeResourceMRU(GrGpuResource*); |
| 150 |
| 151 /** Called by GrGpuResources when they detects that they are newly purgable.
*/ |
| 152 void notifyPurgable(const GrGpuResource*); |
164 | 153 |
165 /** | 154 /** |
166 * Add the new resource to the cache (by creating a new cache entry based | 155 * Add the new resource to the cache (by creating a new cache entry based |
167 * on the provided key and resource). | 156 * on the provided key and resource). |
168 * | 157 * |
169 * Ownership of the resource is transferred to the resource cache, | 158 * Ownership of the resource is transferred to the resource cache, |
170 * which will unref() it when it is purged or deleted. | 159 * which will unref() it when it is purged or deleted. |
171 * | |
172 * If ownershipFlags includes kHide, subsequent calls to 'find' will not | |
173 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive | |
174 * is called. | |
175 */ | 160 */ |
176 void addResource(const GrResourceKey& key, | 161 void addResource(const GrResourceKey& key, GrGpuResource* resource); |
177 GrGpuResource* resource, | |
178 uint32_t ownershipFlags = 0); | |
179 | 162 |
180 /** | 163 /** |
181 * Determines if the cache contains an entry matching a key. If a matching | 164 * Determines if the cache contains an entry matching a key. If a matching |
182 * entry exists but was detached then it will not be found. | 165 * entry exists but was detached then it will not be found. |
183 */ | 166 */ |
184 bool hasKey(const GrResourceKey& key) const { return SkToBool(fCache.find(ke
y)); } | 167 bool hasKey(const GrResourceKey& key) const { return SkToBool(fCache.find(ke
y)); } |
185 | 168 |
186 /** | 169 /** |
187 * Hide 'entry' so that future searches will not find it. Such | |
188 * hidden entries will not be purged. The entry still counts against | |
189 * the cache's budget and should be made non-exclusive when exclusive access | |
190 * is no longer needed. | |
191 */ | |
192 void makeExclusive(GrResourceCacheEntry* entry); | |
193 | |
194 /** | |
195 * Restore 'entry' so that it can be found by future searches. 'entry' | |
196 * will also be purgeable (provided its lock count is now 0.) | |
197 */ | |
198 void makeNonExclusive(GrResourceCacheEntry* entry); | |
199 | |
200 /** | |
201 * Notify the cache that the size of a resource has changed. | 170 * Notify the cache that the size of a resource has changed. |
202 */ | 171 */ |
203 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); | 172 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); |
204 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); | 173 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); |
205 | 174 |
206 /** | 175 /** |
207 * Remove a resource from the cache and delete it! | 176 * Remove a resource from the cache and delete it! |
208 */ | 177 */ |
209 void deleteResource(GrResourceCacheEntry* entry); | 178 void deleteResource(GrResourceCacheEntry* entry); |
210 | 179 |
(...skipping 19 matching lines...) Expand all Loading... |
230 void validate() const; | 199 void validate() const; |
231 #else | 200 #else |
232 void validate() const {} | 201 void validate() const {} |
233 #endif | 202 #endif |
234 | 203 |
235 #if GR_CACHE_STATS | 204 #if GR_CACHE_STATS |
236 void printStats(); | 205 void printStats(); |
237 #endif | 206 #endif |
238 | 207 |
239 private: | 208 private: |
240 enum BudgetBehaviors { | 209 void internalDetach(GrResourceCacheEntry*); |
241 kAccountFor_BudgetBehavior, | 210 void attachToHead(GrResourceCacheEntry*); |
242 kIgnore_BudgetBehavior | 211 void purgeInvalidated(); |
243 }; | 212 void internalPurge(int extraCount, size_t extraBytes); |
| 213 #ifdef SK_DEBUG |
| 214 static size_t countBytes(const SkTInternalLList<GrResourceCacheEntry>& list)
; |
| 215 #endif |
244 | 216 |
245 void internalDetach(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccou
ntFor_BudgetBehavior); | 217 typedef SkTMultiMap<GrResourceCacheEntry, GrResourceKey> CacheMap; |
246 void attachToHead(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccount
For_BudgetBehavior); | 218 CacheMap fCache; |
247 | |
248 void removeInvalidResource(GrResourceCacheEntry* entry); | |
249 | |
250 SkTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache; | |
251 | 219 |
252 // We're an internal doubly linked list | 220 // We're an internal doubly linked list |
253 typedef SkTInternalLList<GrResourceCacheEntry> EntryList; | 221 typedef SkTInternalLList<GrResourceCacheEntry> EntryList; |
254 EntryList fList; | 222 EntryList fList; |
255 | |
256 #ifdef SK_DEBUG | |
257 // These objects cannot be returned by a search | |
258 EntryList fExclusiveList; | |
259 #endif | |
260 | 223 |
261 // our budget, used in purgeAsNeeded() | 224 // our budget, used in purgeAsNeeded() |
262 int fMaxCount; | 225 int fMaxCount; |
263 size_t fMaxBytes; | 226 size_t fMaxBytes; |
264 | 227 |
265 // our current stats, related to our budget | 228 // our current stats, related to our budget |
266 #if GR_CACHE_STATS | 229 #if GR_CACHE_STATS |
267 int fHighWaterEntryCount; | 230 int fHighWaterEntryCount; |
268 size_t fHighWaterEntryBytes; | 231 size_t fHighWaterEntryBytes; |
269 int fHighWaterClientDetachedCount; | |
270 size_t fHighWaterClientDetachedBytes; | |
271 #endif | 232 #endif |
272 | 233 |
273 int fEntryCount; | 234 int fEntryCount; |
274 size_t fEntryBytes; | 235 size_t fEntryBytes; |
275 int fClientDetachedCount; | |
276 size_t fClientDetachedBytes; | |
277 | 236 |
278 // prevents recursive purging | 237 // prevents recursive purging |
279 bool fPurging; | 238 bool fPurging; |
280 | 239 |
281 PFOverbudgetCB fOverbudgetCB; | 240 PFOverbudgetCB fOverbudgetCB; |
282 void* fOverbudgetData; | 241 void* fOverbudgetData; |
283 | 242 |
284 void internalPurge(int extraCount, size_t extraBytes); | 243 SkAutoTUnref<const GrDrawTargetCaps> fCaps; |
285 | 244 |
286 // Listen for messages that a resource has been invalidated and purge cached
junk proactively. | 245 // Listen for messages that a resource has been invalidated and purge cached
junk proactively. |
287 SkMessageBus<GrResourceInvalidatedMessage>::Inbox fInvalidationInbox; | 246 typedef SkMessageBus<GrResourceInvalidatedMessage>::Inbox Inbox; |
288 void purgeInvalidated(); | 247 Inbox fInvalidationInbox; |
289 | |
290 #ifdef SK_DEBUG | |
291 static size_t countBytes(const SkTInternalLList<GrResourceCacheEntry>& list)
; | |
292 #endif | |
293 }; | 248 }; |
294 | 249 |
295 /////////////////////////////////////////////////////////////////////////////// | 250 /////////////////////////////////////////////////////////////////////////////// |
296 | 251 |
297 #ifdef SK_DEBUG | 252 #ifdef SK_DEBUG |
298 class GrAutoResourceCacheValidate { | 253 class GrAutoResourceCacheValidate { |
299 public: | 254 public: |
300 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) { | 255 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) { |
301 cache->validate(); | 256 cache->validate(); |
302 } | 257 } |
303 ~GrAutoResourceCacheValidate() { | 258 ~GrAutoResourceCacheValidate() { |
304 fCache->validate(); | 259 fCache->validate(); |
305 } | 260 } |
306 private: | 261 private: |
307 GrResourceCache* fCache; | 262 GrResourceCache* fCache; |
308 }; | 263 }; |
309 #else | 264 #else |
310 class GrAutoResourceCacheValidate { | 265 class GrAutoResourceCacheValidate { |
311 public: | 266 public: |
312 GrAutoResourceCacheValidate(GrResourceCache*) {} | 267 GrAutoResourceCacheValidate(GrResourceCache*) {} |
313 }; | 268 }; |
314 #endif | 269 #endif |
315 | 270 |
316 #endif | 271 #endif |
OLD | NEW |