Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: src/gpu/GrResourceCache.cpp

Issue 608883003: GrResourceCache2 manages scratch texture. (Closed) Base URL: https://skia.googlesource.com/skia.git@surfimpl
Patch Set: fix and document hack Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2010 Google Inc. 3 * Copyright 2010 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 9
10 10
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 } 75 }
76 76
77 /////////////////////////////////////////////////////////////////////////////// 77 ///////////////////////////////////////////////////////////////////////////////
78 78
79 GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) : 79 GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
80 fMaxCount(maxCount), 80 fMaxCount(maxCount),
81 fMaxBytes(maxBytes) { 81 fMaxBytes(maxBytes) {
82 #if GR_CACHE_STATS 82 #if GR_CACHE_STATS
83 fHighWaterEntryCount = 0; 83 fHighWaterEntryCount = 0;
84 fHighWaterEntryBytes = 0; 84 fHighWaterEntryBytes = 0;
85 fHighWaterClientDetachedCount = 0;
86 fHighWaterClientDetachedBytes = 0;
87 #endif 85 #endif
88 86
89 fEntryCount = 0; 87 fEntryCount = 0;
90 fEntryBytes = 0; 88 fEntryBytes = 0;
91 fClientDetachedCount = 0;
92 fClientDetachedBytes = 0;
93 89
94 fPurging = false; 90 fPurging = false;
95 91
96 fOverbudgetCB = NULL; 92 fOverbudgetCB = NULL;
97 fOverbudgetData = NULL; 93 fOverbudgetData = NULL;
98 } 94 }
99 95
100 GrResourceCache::~GrResourceCache() { 96 GrResourceCache::~GrResourceCache() {
101 GrAutoResourceCacheValidate atcv(this); 97 GrAutoResourceCacheValidate atcv(this);
102 98
(...skipping 26 matching lines...) Expand all
129 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes); 125 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
130 126
131 fMaxCount = maxResources; 127 fMaxCount = maxResources;
132 fMaxBytes = maxResourceBytes; 128 fMaxBytes = maxResourceBytes;
133 129
134 if (smaller) { 130 if (smaller) {
135 this->purgeAsNeeded(); 131 this->purgeAsNeeded();
136 } 132 }
137 } 133 }
138 134
139 void GrResourceCache::internalDetach(GrResourceCacheEntry* entry, 135 void GrResourceCache::internalDetach(GrResourceCacheEntry* entry) {
140 BudgetBehaviors behavior) {
141 fList.remove(entry); 136 fList.remove(entry);
137 fEntryCount -= 1;
138 fEntryBytes -= entry->fCachedSize;
139 }
142 140
143 // update our stats 141 void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) {
144 if (kIgnore_BudgetBehavior == behavior) { 142 fList.addToHead(entry);
145 fClientDetachedCount += 1; 143
146 fClientDetachedBytes += entry->fCachedSize; 144 fEntryCount += 1;
145 fEntryBytes += entry->fCachedSize;
147 146
148 #if GR_CACHE_STATS 147 #if GR_CACHE_STATS
149 if (fHighWaterClientDetachedCount < fClientDetachedCount) { 148 if (fHighWaterEntryCount < fEntryCount) {
150 fHighWaterClientDetachedCount = fClientDetachedCount; 149 fHighWaterEntryCount = fEntryCount;
151 } 150 }
152 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) { 151 if (fHighWaterEntryBytes < fEntryBytes) {
153 fHighWaterClientDetachedBytes = fClientDetachedBytes; 152 fHighWaterEntryBytes = fEntryBytes;
154 } 153 }
155 #endif 154 #endif
156
157 } else {
158 SkASSERT(kAccountFor_BudgetBehavior == behavior);
159
160 fEntryCount -= 1;
161 fEntryBytes -= entry->fCachedSize;
162 }
163 }
164
165 void GrResourceCache::attachToHead(GrResourceCacheEntry* entry,
166 BudgetBehaviors behavior) {
167 fList.addToHead(entry);
168
169 // update our stats
170 if (kIgnore_BudgetBehavior == behavior) {
171 fClientDetachedCount -= 1;
172 fClientDetachedBytes -= entry->fCachedSize;
173 } else {
174 SkASSERT(kAccountFor_BudgetBehavior == behavior);
175
176 fEntryCount += 1;
177 fEntryBytes += entry->fCachedSize;
178
179 #if GR_CACHE_STATS
180 if (fHighWaterEntryCount < fEntryCount) {
181 fHighWaterEntryCount = fEntryCount;
182 }
183 if (fHighWaterEntryBytes < fEntryBytes) {
184 fHighWaterEntryBytes = fEntryBytes;
185 }
186 #endif
187 }
188 } 155 }
189 156
190 // This functor just searches for an entry with only a single ref (from 157 // This functor just searches for an entry with only a single ref (from
191 // the texture cache itself). Presumably in this situation no one else 158 // the texture cache itself). Presumably in this situation no one else
192 // is relying on the texture. 159 // is relying on the texture.
193 class GrTFindUnreffedFunctor { 160 class GrTFindUnreffedFunctor {
194 public: 161 public:
195 bool operator()(const GrResourceCacheEntry* entry) const { 162 bool operator()(const GrResourceCacheEntry* entry) const {
196 return entry->resource()->unique(); 163 return entry->resource()->isPurgable();
197 } 164 }
198 }; 165 };
199 166
200 GrGpuResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershi pFlags) { 167
168 void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
169 GrResourceCacheEntry* entry = resource->getCacheEntry();
170 if (entry) {
171 this->internalDetach(entry);
172 this->attachToHead(entry);
173 }
174 }
175
176 GrGpuResource* GrResourceCache::find(const GrResourceKey& key) {
201 GrAutoResourceCacheValidate atcv(this); 177 GrAutoResourceCacheValidate atcv(this);
202 178
203 GrResourceCacheEntry* entry = NULL; 179 GrResourceCacheEntry* entry = NULL;
204 180
205 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) { 181 entry = fCache.find(key);
206 GrTFindUnreffedFunctor functor;
207
208 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
209 } else {
210 entry = fCache.find(key);
211 }
212 182
213 if (NULL == entry) { 183 if (NULL == entry) {
214 return NULL; 184 return NULL;
215 } 185 }
216 186
217 if (ownershipFlags & kHide_OwnershipFlag) { 187 // Make this resource MRU
218 this->makeExclusive(entry); 188 this->internalDetach(entry);
219 } else { 189 this->attachToHead(entry);
220 // Make this resource MRU
221 this->internalDetach(entry);
222 this->attachToHead(entry);
223 }
224 190
191 // GrResourceCache2 is responsible for scratch resources.
192 SkASSERT(GrIORef::kNo_IsScratch == entry->resource()->fIsScratch);
225 return entry->fResource; 193 return entry->fResource;
226 } 194 }
227 195
228 void GrResourceCache::addResource(const GrResourceKey& key, 196 void GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resou rce) {
229 GrGpuResource* resource,
230 uint32_t ownershipFlags) {
231 SkASSERT(NULL == resource->getCacheEntry()); 197 SkASSERT(NULL == resource->getCacheEntry());
232 // we don't expect to create new resources during a purge. In theory 198 // we don't expect to create new resources during a purge. In theory
233 // this could cause purgeAsNeeded() into an infinite loop (e.g. 199 // this could cause purgeAsNeeded() into an infinite loop (e.g.
234 // each resource destroyed creates and locks 2 resources and 200 // each resource destroyed creates and locks 2 resources and
235 // unlocks 1 thereby causing a new purge). 201 // unlocks 1 thereby causing a new purge).
236 SkASSERT(!fPurging); 202 SkASSERT(!fPurging);
237 GrAutoResourceCacheValidate atcv(this); 203 GrAutoResourceCacheValidate atcv(this);
238 204
239 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, r esource)); 205 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, r esource));
240 resource->setCacheEntry(entry); 206 resource->setCacheEntry(entry);
241 207
242 this->attachToHead(entry); 208 this->attachToHead(entry);
243 fCache.insert(key, entry); 209 fCache.insert(key, entry);
244
245 if (ownershipFlags & kHide_OwnershipFlag) {
246 this->makeExclusive(entry);
247 }
248
249 }
250
251 void GrResourceCache::makeExclusive(GrResourceCacheEntry* entry) {
252 GrAutoResourceCacheValidate atcv(this);
253
254 SkASSERT(!entry->fIsExclusive);
255 entry->fIsExclusive = true;
256
257 // When scratch textures are detached (to hide them from future finds) they
258 // still count against the resource budget
259 this->internalDetach(entry, kIgnore_BudgetBehavior);
260 fCache.remove(entry->key(), entry);
261
262 #ifdef SK_DEBUG
263 fExclusiveList.addToHead(entry);
264 #endif
265 }
266
267 void GrResourceCache::removeInvalidResource(GrResourceCacheEntry* entry) {
268 // If the resource went invalid while it was detached then purge it
269 // This can happen when a 3D context was lost,
270 // the client called GrContext::abandonContext() to notify Gr,
271 // and then later an SkGpuDevice's destructor releases its backing
272 // texture (which was invalidated at contextDestroyed time).
273 // TODO: Safely delete the GrResourceCacheEntry as well.
274 fClientDetachedCount -= 1;
275 fEntryCount -= 1;
276 fClientDetachedBytes -= entry->fCachedSize;
277 fEntryBytes -= entry->fCachedSize;
278 entry->fCachedSize = 0;
279 }
280
281 void GrResourceCache::makeNonExclusive(GrResourceCacheEntry* entry) {
282 GrAutoResourceCacheValidate atcv(this);
283
284 #ifdef SK_DEBUG
285 fExclusiveList.remove(entry);
286 #endif
287
288 if (!entry->resource()->wasDestroyed()) {
289 // Since scratch textures still count against the cache budget even
290 // when they have been removed from the cache, re-adding them doesn't
291 // alter the budget information.
292 attachToHead(entry, kIgnore_BudgetBehavior);
293 fCache.insert(entry->key(), entry);
294
295 SkASSERT(entry->fIsExclusive);
296 entry->fIsExclusive = false;
297 } else {
298 this->removeInvalidResource(entry);
299 }
300 } 210 }
301 211
302 void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) { 212 void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
303 fEntryBytes += amountInc; 213 fEntryBytes += amountInc;
304 if (entry->fIsExclusive) {
305 fClientDetachedBytes += amountInc;
306 }
307 this->purgeAsNeeded(); 214 this->purgeAsNeeded();
308 } 215 }
309 216
310 void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) { 217 void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
311 fEntryBytes -= amountDec; 218 fEntryBytes -= amountDec;
312 if (entry->fIsExclusive) {
313 fClientDetachedBytes -= amountDec;
314 }
315 #ifdef SK_DEBUG 219 #ifdef SK_DEBUG
316 this->validate(); 220 this->validate();
317 #endif 221 #endif
318 } 222 }
319 223
320 /** 224 /**
321 * Destroying a resource may potentially trigger the unlock of additional 225 * Destroying a resource may potentially trigger the unlock of additional
322 * resources which in turn will trigger a nested purge. We block the nested 226 * resources which in turn will trigger a nested purge. We block the nested
323 * purge using the fPurging variable. However, the initial purge will keep 227 * purge using the fPurging variable. However, the initial purge will keep
324 * looping until either all resources in the cache are unlocked or we've met 228 * looping until either all resources in the cache are unlocked or we've met
(...skipping 27 matching lines...) Expand all
352 } 256 }
353 257
354 fPurging = false; 258 fPurging = false;
355 } 259 }
356 260
357 void GrResourceCache::purgeInvalidated() { 261 void GrResourceCache::purgeInvalidated() {
358 SkTDArray<GrResourceInvalidatedMessage> invalidated; 262 SkTDArray<GrResourceInvalidatedMessage> invalidated;
359 fInvalidationInbox.poll(&invalidated); 263 fInvalidationInbox.poll(&invalidated);
360 264
361 for (int i = 0; i < invalidated.count(); i++) { 265 for (int i = 0; i < invalidated.count(); i++) {
362 // We're somewhat missing an opportunity here. We could use the
363 // default find functor that gives us back resources whether we own
364 // them exclusively or not, and when they're not exclusively owned mark
365 // them for purging later when they do become exclusively owned.
366 //
367 // This is complicated and confusing. May try this in the future. For
368 // now, these resources are just LRU'd as if we never got the message.
369 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrT FindUnreffedFunctor())) { 266 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrT FindUnreffedFunctor())) {
370 this->deleteResource(entry); 267 this->deleteResource(entry);
371 } 268 }
372 } 269 }
373 } 270 }
374 271
375 void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) { 272 void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
376 SkASSERT(entry->fResource->unique()); 273 SkASSERT(entry->fResource->isPurgable());
377 274
378 // remove from our cache 275 // remove from our cache
379 fCache.remove(entry->key(), entry); 276 fCache.remove(entry->key(), entry);
380 277
381 // remove from our llist 278 // remove from our llist
382 this->internalDetach(entry); 279 this->internalDetach(entry);
383 delete entry; 280 delete entry;
384 } 281 }
385 282
386 void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) { 283 void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
(...skipping 18 matching lines...) Expand all
405 while (entry) { 302 while (entry) {
406 GrAutoResourceCacheValidate atcv(this); 303 GrAutoResourceCacheValidate atcv(this);
407 304
408 if ((fEntryCount+extraCount) <= fMaxCount && 305 if ((fEntryCount+extraCount) <= fMaxCount &&
409 (fEntryBytes+extraBytes) <= fMaxBytes) { 306 (fEntryBytes+extraBytes) <= fMaxBytes) {
410 withinBudget = true; 307 withinBudget = true;
411 break; 308 break;
412 } 309 }
413 310
414 GrResourceCacheEntry* prev = iter.prev(); 311 GrResourceCacheEntry* prev = iter.prev();
415 if (entry->fResource->unique()) { 312 if (entry->fResource->isPurgable()) {
416 changed = true; 313 changed = true;
417 this->deleteResource(entry); 314 this->deleteResource(entry);
418 } 315 }
419 entry = prev; 316 entry = prev;
420 } 317 }
421 } while (!withinBudget && changed); 318 } while (!withinBudget && changed);
422 } 319 }
423 320
424 void GrResourceCache::purgeAllUnlocked() { 321 void GrResourceCache::purgeAllUnlocked() {
425 GrAutoResourceCacheValidate atcv(this); 322 GrAutoResourceCacheValidate atcv(this);
426 323
427 // we can have one GrCacheable holding a lock on another 324 // we can have one GrCacheable holding a lock on another
428 // so we don't want to just do a simple loop kicking each 325 // so we don't want to just do a simple loop kicking each
429 // entry out. Instead change the budget and purge. 326 // entry out. Instead change the budget and purge.
430 327
431 size_t savedMaxBytes = fMaxBytes; 328 size_t savedMaxBytes = fMaxBytes;
432 int savedMaxCount = fMaxCount; 329 int savedMaxCount = fMaxCount;
433 fMaxBytes = (size_t) -1; 330 fMaxBytes = (size_t) -1;
434 fMaxCount = 0; 331 fMaxCount = 0;
435 this->purgeAsNeeded(); 332 this->purgeAsNeeded();
436 333
437 #ifdef SK_DEBUG 334 #ifdef SK_DEBUG
438 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
439 SkASSERT(countBytes(fExclusiveList) == fClientDetachedBytes);
440 if (!fCache.count()) { 335 if (!fCache.count()) {
441 // Items may have been detached from the cache (such as the backing
442 // texture for an SkGpuDevice). The above purge would not have removed
443 // them.
444 SkASSERT(fEntryCount == fClientDetachedCount);
445 SkASSERT(fEntryBytes == fClientDetachedBytes);
446 SkASSERT(fList.isEmpty()); 336 SkASSERT(fList.isEmpty());
447 } 337 }
448 #endif 338 #endif
449 339
450 fMaxBytes = savedMaxBytes; 340 fMaxBytes = savedMaxBytes;
451 fMaxCount = savedMaxCount; 341 fMaxCount = savedMaxCount;
452 } 342 }
453 343
454 /////////////////////////////////////////////////////////////////////////////// 344 ///////////////////////////////////////////////////////////////////////////////
455 345
(...skipping 11 matching lines...) Expand all
467 } 357 }
468 return bytes; 358 return bytes;
469 } 359 }
470 360
471 static bool both_zero_or_nonzero(int count, size_t bytes) { 361 static bool both_zero_or_nonzero(int count, size_t bytes) {
472 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); 362 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
473 } 363 }
474 364
475 void GrResourceCache::validate() const { 365 void GrResourceCache::validate() const {
476 fList.validate(); 366 fList.validate();
477 fExclusiveList.validate();
478 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes)); 367 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
479 SkASSERT(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); 368 SkASSERT(fEntryCount == fCache.count());
480 SkASSERT(fClientDetachedBytes <= fEntryBytes);
481 SkASSERT(fClientDetachedCount <= fEntryCount);
482 SkASSERT((fEntryCount - fClientDetachedCount) == fCache.count());
483 369
484 EntryList::Iter iter; 370 EntryList::Iter iter;
485 371
486 // check that the exclusively held entries are okay 372 // check that the shareable entries are okay
487 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fExclus iveList), 373 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList),
488 EntryList::Iter::kHead_IterSta rt); 374 EntryList::Iter::kHead_IterSta rt);
489 375
490 for ( ; entry; entry = iter.next()) {
491 entry->validate();
492 }
493
494 // check that the shareable entries are okay
495 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_Iter Start);
496
497 int count = 0; 376 int count = 0;
498 for ( ; entry; entry = iter.next()) { 377 for ( ; entry; entry = iter.next()) {
499 entry->validate(); 378 entry->validate();
500 SkASSERT(fCache.find(entry->key())); 379 SkASSERT(fCache.find(entry->key()));
501 count += 1; 380 count += 1;
502 } 381 }
503 SkASSERT(count == fEntryCount - fClientDetachedCount); 382 SkASSERT(count == fEntryCount);
504 383
505 size_t bytes = countBytes(fList); 384 size_t bytes = this->countBytes(fList);
506 SkASSERT(bytes == fEntryBytes - fClientDetachedBytes); 385 SkASSERT(bytes == fEntryBytes);
507 386 SkASSERT(fList.countEntries() == fEntryCount);
508 bytes = countBytes(fExclusiveList);
509 SkASSERT(bytes == fClientDetachedBytes);
510
511 SkASSERT(fList.countEntries() == fEntryCount - fClientDetachedCount);
512
513 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
514 } 387 }
515 #endif // SK_DEBUG 388 #endif // SK_DEBUG
516 389
517 #if GR_CACHE_STATS 390 #if GR_CACHE_STATS
518 391
519 void GrResourceCache::printStats() { 392 void GrResourceCache::printStats() {
520 int locked = 0; 393 int locked = 0;
521 394
522 EntryList::Iter iter; 395 EntryList::Iter iter;
523 396
524 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterSt art); 397 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterSt art);
525 398
526 for ( ; entry; entry = iter.prev()) { 399 for ( ; entry; entry = iter.prev()) {
527 if (entry->fResource->getRefCnt() > 1) { 400 if (entry->fResource->getRefCnt() > 1) {
528 ++locked; 401 ++locked;
529 } 402 }
530 } 403 }
531 404
532 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); 405 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
533 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n", 406 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
534 fEntryCount, locked, fHighWaterEntryCount); 407 fEntryCount, locked, fHighWaterEntryCount);
535 SkDebugf("\t\tEntry Bytes: current %d high %d\n", 408 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
536 fEntryBytes, fHighWaterEntryBytes); 409 fEntryBytes, fHighWaterEntryBytes);
537 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
538 fClientDetachedCount, fHighWaterClientDetachedCount);
539 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
540 fClientDetachedBytes, fHighWaterClientDetachedBytes);
541 } 410 }
542 411
543 #endif 412 #endif
544 413
545 /////////////////////////////////////////////////////////////////////////////// 414 ///////////////////////////////////////////////////////////////////////////////
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698