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

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: wrap line and delete unneeded friends 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);
robertphillips 2014/09/29 15:25:07 rm this return; ?
bsalomon 2014/09/29 19:58:14 Done.
173 return;
174 }
175 }
176
177 GrGpuResource* GrResourceCache::find(const GrResourceKey& key) {
201 GrAutoResourceCacheValidate atcv(this); 178 GrAutoResourceCacheValidate atcv(this);
202 179
203 GrResourceCacheEntry* entry = NULL; 180 GrResourceCacheEntry* entry = NULL;
204 181
205 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) { 182 entry = fCache.find(key);
206 GrTFindUnreffedFunctor functor;
207
208 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
209 } else {
210 entry = fCache.find(key);
211 }
212 183
213 if (NULL == entry) { 184 if (NULL == entry) {
214 return NULL; 185 return NULL;
215 } 186 }
216 187
217 if (ownershipFlags & kHide_OwnershipFlag) { 188 // Make this resource MRU
218 this->makeExclusive(entry); 189 this->internalDetach(entry);
219 } else { 190 this->attachToHead(entry);
220 // Make this resource MRU
221 this->internalDetach(entry);
222 this->attachToHead(entry);
223 }
224 191
192 // GrResourceCache2 is responsible for scratch resources.
193 SkASSERT(GrIORef::kNo_IsScratch == entry->resource()->fIsScratch);
225 return entry->fResource; 194 return entry->fResource;
226 } 195 }
227 196
228 void GrResourceCache::addResource(const GrResourceKey& key, 197 void GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resou rce) {
229 GrGpuResource* resource,
230 uint32_t ownershipFlags) {
231 SkASSERT(NULL == resource->getCacheEntry()); 198 SkASSERT(NULL == resource->getCacheEntry());
232 // we don't expect to create new resources during a purge. In theory 199 // we don't expect to create new resources during a purge. In theory
233 // this could cause purgeAsNeeded() into an infinite loop (e.g. 200 // this could cause purgeAsNeeded() into an infinite loop (e.g.
234 // each resource destroyed creates and locks 2 resources and 201 // each resource destroyed creates and locks 2 resources and
235 // unlocks 1 thereby causing a new purge). 202 // unlocks 1 thereby causing a new purge).
236 SkASSERT(!fPurging); 203 SkASSERT(!fPurging);
237 GrAutoResourceCacheValidate atcv(this); 204 GrAutoResourceCacheValidate atcv(this);
238 205
239 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, r esource)); 206 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, r esource));
240 resource->setCacheEntry(entry); 207 resource->setCacheEntry(entry);
241 208
242 this->attachToHead(entry); 209 this->attachToHead(entry);
243 fCache.insert(key, entry); 210 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 } 211 }
301 212
302 void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) { 213 void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
303 fEntryBytes += amountInc; 214 fEntryBytes += amountInc;
304 if (entry->fIsExclusive) {
305 fClientDetachedBytes += amountInc;
306 }
307 this->purgeAsNeeded(); 215 this->purgeAsNeeded();
308 } 216 }
309 217
310 void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) { 218 void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
311 fEntryBytes -= amountDec; 219 fEntryBytes -= amountDec;
312 if (entry->fIsExclusive) {
313 fClientDetachedBytes -= amountDec;
314 }
315 #ifdef SK_DEBUG 220 #ifdef SK_DEBUG
316 this->validate(); 221 this->validate();
317 #endif 222 #endif
318 } 223 }
319 224
320 /** 225 /**
321 * Destroying a resource may potentially trigger the unlock of additional 226 * Destroying a resource may potentially trigger the unlock of additional
322 * resources which in turn will trigger a nested purge. We block the nested 227 * 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 228 * 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 229 * looping until either all resources in the cache are unlocked or we've met
(...skipping 27 matching lines...) Expand all
352 } 257 }
353 258
354 fPurging = false; 259 fPurging = false;
355 } 260 }
356 261
357 void GrResourceCache::purgeInvalidated() { 262 void GrResourceCache::purgeInvalidated() {
358 SkTDArray<GrResourceInvalidatedMessage> invalidated; 263 SkTDArray<GrResourceInvalidatedMessage> invalidated;
359 fInvalidationInbox.poll(&invalidated); 264 fInvalidationInbox.poll(&invalidated);
360 265
361 for (int i = 0; i < invalidated.count(); i++) { 266 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())) { 267 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrT FindUnreffedFunctor())) {
370 this->deleteResource(entry); 268 this->deleteResource(entry);
371 } 269 }
372 } 270 }
373 } 271 }
374 272
375 void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) { 273 void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
376 SkASSERT(entry->fResource->unique()); 274 SkASSERT(entry->fResource->isPurgable());
377 275
378 // remove from our cache 276 // remove from our cache
379 fCache.remove(entry->key(), entry); 277 fCache.remove(entry->key(), entry);
380 278
381 // remove from our llist 279 // remove from our llist
382 this->internalDetach(entry); 280 this->internalDetach(entry);
383 delete entry; 281 delete entry;
384 } 282 }
385 283
386 void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) { 284 void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
(...skipping 18 matching lines...) Expand all
405 while (entry) { 303 while (entry) {
406 GrAutoResourceCacheValidate atcv(this); 304 GrAutoResourceCacheValidate atcv(this);
407 305
408 if ((fEntryCount+extraCount) <= fMaxCount && 306 if ((fEntryCount+extraCount) <= fMaxCount &&
409 (fEntryBytes+extraBytes) <= fMaxBytes) { 307 (fEntryBytes+extraBytes) <= fMaxBytes) {
410 withinBudget = true; 308 withinBudget = true;
411 break; 309 break;
412 } 310 }
413 311
414 GrResourceCacheEntry* prev = iter.prev(); 312 GrResourceCacheEntry* prev = iter.prev();
415 if (entry->fResource->unique()) { 313 if (entry->fResource->isPurgable()) {
416 changed = true; 314 changed = true;
417 this->deleteResource(entry); 315 this->deleteResource(entry);
418 } 316 }
419 entry = prev; 317 entry = prev;
420 } 318 }
421 } while (!withinBudget && changed); 319 } while (!withinBudget && changed);
422 } 320 }
423 321
424 void GrResourceCache::purgeAllUnlocked() { 322 void GrResourceCache::purgeAllUnlocked() {
425 GrAutoResourceCacheValidate atcv(this); 323 GrAutoResourceCacheValidate atcv(this);
426 324
427 // we can have one GrCacheable holding a lock on another 325 // we can have one GrCacheable holding a lock on another
428 // so we don't want to just do a simple loop kicking each 326 // so we don't want to just do a simple loop kicking each
429 // entry out. Instead change the budget and purge. 327 // entry out. Instead change the budget and purge.
430 328
431 size_t savedMaxBytes = fMaxBytes; 329 size_t savedMaxBytes = fMaxBytes;
432 int savedMaxCount = fMaxCount; 330 int savedMaxCount = fMaxCount;
433 fMaxBytes = (size_t) -1; 331 fMaxBytes = (size_t) -1;
434 fMaxCount = 0; 332 fMaxCount = 0;
435 this->purgeAsNeeded(); 333 this->purgeAsNeeded();
436 334
437 #ifdef SK_DEBUG 335 #ifdef SK_DEBUG
438 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
439 SkASSERT(countBytes(fExclusiveList) == fClientDetachedBytes);
440 if (!fCache.count()) { 336 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()); 337 SkASSERT(fList.isEmpty());
447 } 338 }
448 #endif 339 #endif
449 340
450 fMaxBytes = savedMaxBytes; 341 fMaxBytes = savedMaxBytes;
451 fMaxCount = savedMaxCount; 342 fMaxCount = savedMaxCount;
452 } 343 }
453 344
454 /////////////////////////////////////////////////////////////////////////////// 345 ///////////////////////////////////////////////////////////////////////////////
455 346
(...skipping 11 matching lines...) Expand all
467 } 358 }
468 return bytes; 359 return bytes;
469 } 360 }
470 361
471 static bool both_zero_or_nonzero(int count, size_t bytes) { 362 static bool both_zero_or_nonzero(int count, size_t bytes) {
472 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); 363 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
473 } 364 }
474 365
475 void GrResourceCache::validate() const { 366 void GrResourceCache::validate() const {
476 fList.validate(); 367 fList.validate();
477 fExclusiveList.validate();
478 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes)); 368 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
479 SkASSERT(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); 369 SkASSERT(fEntryCount == fCache.count());
480 SkASSERT(fClientDetachedBytes <= fEntryBytes);
481 SkASSERT(fClientDetachedCount <= fEntryCount);
482 SkASSERT((fEntryCount - fClientDetachedCount) == fCache.count());
483 370
484 EntryList::Iter iter; 371 EntryList::Iter iter;
485 372
486 // check that the exclusively held entries are okay 373 // check that the shareable entries are okay
487 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fExclus iveList), 374 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList),
488 EntryList::Iter::kHead_IterSta rt); 375 EntryList::Iter::kHead_IterSta rt);
489 376
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; 377 int count = 0;
498 for ( ; entry; entry = iter.next()) { 378 for ( ; entry; entry = iter.next()) {
499 entry->validate(); 379 entry->validate();
500 SkASSERT(fCache.find(entry->key())); 380 SkASSERT(fCache.find(entry->key()));
501 count += 1; 381 count += 1;
502 } 382 }
503 SkASSERT(count == fEntryCount - fClientDetachedCount); 383 SkASSERT(count == fEntryCount);
504 384
505 size_t bytes = countBytes(fList); 385 size_t bytes = this->countBytes(fList);
506 SkASSERT(bytes == fEntryBytes - fClientDetachedBytes); 386 SkASSERT(bytes == fEntryBytes);
507 387 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 } 388 }
515 #endif // SK_DEBUG 389 #endif // SK_DEBUG
516 390
517 #if GR_CACHE_STATS 391 #if GR_CACHE_STATS
518 392
519 void GrResourceCache::printStats() { 393 void GrResourceCache::printStats() {
520 int locked = 0; 394 int locked = 0;
521 395
522 EntryList::Iter iter; 396 EntryList::Iter iter;
523 397
524 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterSt art); 398 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterSt art);
525 399
526 for ( ; entry; entry = iter.prev()) { 400 for ( ; entry; entry = iter.prev()) {
527 if (entry->fResource->getRefCnt() > 1) { 401 if (entry->fResource->getRefCnt() > 1) {
528 ++locked; 402 ++locked;
529 } 403 }
530 } 404 }
531 405
532 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); 406 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
533 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n", 407 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
534 fEntryCount, locked, fHighWaterEntryCount); 408 fEntryCount, locked, fHighWaterEntryCount);
535 SkDebugf("\t\tEntry Bytes: current %d high %d\n", 409 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
536 fEntryBytes, fHighWaterEntryBytes); 410 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 } 411 }
542 412
543 #endif 413 #endif
544 414
545 /////////////////////////////////////////////////////////////////////////////// 415 ///////////////////////////////////////////////////////////////////////////////
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698