gfx/skia/trunk/src/gpu/GrResourceCache.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2010 Google Inc.
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10
michael@0 11 #include "GrResourceCache.h"
michael@0 12 #include "GrResource.h"
michael@0 13
michael@0 14 DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
michael@0 15
michael@0 16 GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
michael@0 17 static int32_t gNextType = 0;
michael@0 18
michael@0 19 int32_t type = sk_atomic_inc(&gNextType);
michael@0 20 if (type >= (1 << 8 * sizeof(ResourceType))) {
michael@0 21 GrCrash("Too many Resource Types");
michael@0 22 }
michael@0 23
michael@0 24 return static_cast<ResourceType>(type);
michael@0 25 }
michael@0 26
michael@0 27 ///////////////////////////////////////////////////////////////////////////////
michael@0 28
michael@0 29 GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource)
michael@0 30 : fKey(key), fResource(resource) {
michael@0 31 // we assume ownership of the resource, and will unref it when we die
michael@0 32 SkASSERT(resource);
michael@0 33 resource->ref();
michael@0 34 }
michael@0 35
michael@0 36 GrResourceEntry::~GrResourceEntry() {
michael@0 37 fResource->setCacheEntry(NULL);
michael@0 38 fResource->unref();
michael@0 39 }
michael@0 40
michael@0 41 #ifdef SK_DEBUG
michael@0 42 void GrResourceEntry::validate() const {
michael@0 43 SkASSERT(fResource);
michael@0 44 SkASSERT(fResource->getCacheEntry() == this);
michael@0 45 fResource->validate();
michael@0 46 }
michael@0 47 #endif
michael@0 48
michael@0 49 ///////////////////////////////////////////////////////////////////////////////
michael@0 50
michael@0 51 GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
michael@0 52 fMaxCount(maxCount),
michael@0 53 fMaxBytes(maxBytes) {
michael@0 54 #if GR_CACHE_STATS
michael@0 55 fHighWaterEntryCount = 0;
michael@0 56 fHighWaterEntryBytes = 0;
michael@0 57 fHighWaterClientDetachedCount = 0;
michael@0 58 fHighWaterClientDetachedBytes = 0;
michael@0 59 #endif
michael@0 60
michael@0 61 fEntryCount = 0;
michael@0 62 fEntryBytes = 0;
michael@0 63 fClientDetachedCount = 0;
michael@0 64 fClientDetachedBytes = 0;
michael@0 65
michael@0 66 fPurging = false;
michael@0 67
michael@0 68 fOverbudgetCB = NULL;
michael@0 69 fOverbudgetData = NULL;
michael@0 70 }
michael@0 71
michael@0 72 GrResourceCache::~GrResourceCache() {
michael@0 73 GrAutoResourceCacheValidate atcv(this);
michael@0 74
michael@0 75 EntryList::Iter iter;
michael@0 76
michael@0 77 // Unlike the removeAll, here we really remove everything, including locked resources.
michael@0 78 while (GrResourceEntry* entry = fList.head()) {
michael@0 79 GrAutoResourceCacheValidate atcv(this);
michael@0 80
michael@0 81 // remove from our cache
michael@0 82 fCache.remove(entry->fKey, entry);
michael@0 83
michael@0 84 // remove from our llist
michael@0 85 this->internalDetach(entry);
michael@0 86
michael@0 87 delete entry;
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
michael@0 92 if (NULL != maxResources) {
michael@0 93 *maxResources = fMaxCount;
michael@0 94 }
michael@0 95 if (NULL != maxResourceBytes) {
michael@0 96 *maxResourceBytes = fMaxBytes;
michael@0 97 }
michael@0 98 }
michael@0 99
michael@0 100 void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
michael@0 101 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
michael@0 102
michael@0 103 fMaxCount = maxResources;
michael@0 104 fMaxBytes = maxResourceBytes;
michael@0 105
michael@0 106 if (smaller) {
michael@0 107 this->purgeAsNeeded();
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 void GrResourceCache::internalDetach(GrResourceEntry* entry,
michael@0 112 BudgetBehaviors behavior) {
michael@0 113 fList.remove(entry);
michael@0 114
michael@0 115 // update our stats
michael@0 116 if (kIgnore_BudgetBehavior == behavior) {
michael@0 117 fClientDetachedCount += 1;
michael@0 118 fClientDetachedBytes += entry->resource()->sizeInBytes();
michael@0 119
michael@0 120 #if GR_CACHE_STATS
michael@0 121 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
michael@0 122 fHighWaterClientDetachedCount = fClientDetachedCount;
michael@0 123 }
michael@0 124 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
michael@0 125 fHighWaterClientDetachedBytes = fClientDetachedBytes;
michael@0 126 }
michael@0 127 #endif
michael@0 128
michael@0 129 } else {
michael@0 130 SkASSERT(kAccountFor_BudgetBehavior == behavior);
michael@0 131
michael@0 132 fEntryCount -= 1;
michael@0 133 fEntryBytes -= entry->resource()->sizeInBytes();
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 void GrResourceCache::attachToHead(GrResourceEntry* entry,
michael@0 138 BudgetBehaviors behavior) {
michael@0 139 fList.addToHead(entry);
michael@0 140
michael@0 141 // update our stats
michael@0 142 if (kIgnore_BudgetBehavior == behavior) {
michael@0 143 fClientDetachedCount -= 1;
michael@0 144 fClientDetachedBytes -= entry->resource()->sizeInBytes();
michael@0 145 } else {
michael@0 146 SkASSERT(kAccountFor_BudgetBehavior == behavior);
michael@0 147
michael@0 148 fEntryCount += 1;
michael@0 149 fEntryBytes += entry->resource()->sizeInBytes();
michael@0 150
michael@0 151 #if GR_CACHE_STATS
michael@0 152 if (fHighWaterEntryCount < fEntryCount) {
michael@0 153 fHighWaterEntryCount = fEntryCount;
michael@0 154 }
michael@0 155 if (fHighWaterEntryBytes < fEntryBytes) {
michael@0 156 fHighWaterEntryBytes = fEntryBytes;
michael@0 157 }
michael@0 158 #endif
michael@0 159 }
michael@0 160 }
michael@0 161
michael@0 162 // This functor just searches for an entry with only a single ref (from
michael@0 163 // the texture cache itself). Presumably in this situation no one else
michael@0 164 // is relying on the texture.
michael@0 165 class GrTFindUnreffedFunctor {
michael@0 166 public:
michael@0 167 bool operator()(const GrResourceEntry* entry) const {
michael@0 168 return entry->resource()->unique();
michael@0 169 }
michael@0 170 };
michael@0 171
michael@0 172 GrResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
michael@0 173 GrAutoResourceCacheValidate atcv(this);
michael@0 174
michael@0 175 GrResourceEntry* entry = NULL;
michael@0 176
michael@0 177 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
michael@0 178 GrTFindUnreffedFunctor functor;
michael@0 179
michael@0 180 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
michael@0 181 } else {
michael@0 182 entry = fCache.find(key);
michael@0 183 }
michael@0 184
michael@0 185 if (NULL == entry) {
michael@0 186 return NULL;
michael@0 187 }
michael@0 188
michael@0 189 if (ownershipFlags & kHide_OwnershipFlag) {
michael@0 190 this->makeExclusive(entry);
michael@0 191 } else {
michael@0 192 // Make this resource MRU
michael@0 193 this->internalDetach(entry);
michael@0 194 this->attachToHead(entry);
michael@0 195 }
michael@0 196
michael@0 197 return entry->fResource;
michael@0 198 }
michael@0 199
michael@0 200 void GrResourceCache::addResource(const GrResourceKey& key,
michael@0 201 GrResource* resource,
michael@0 202 uint32_t ownershipFlags) {
michael@0 203 SkASSERT(NULL == resource->getCacheEntry());
michael@0 204 // we don't expect to create new resources during a purge. In theory
michael@0 205 // this could cause purgeAsNeeded() into an infinite loop (e.g.
michael@0 206 // each resource destroyed creates and locks 2 resources and
michael@0 207 // unlocks 1 thereby causing a new purge).
michael@0 208 SkASSERT(!fPurging);
michael@0 209 GrAutoResourceCacheValidate atcv(this);
michael@0 210
michael@0 211 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
michael@0 212 resource->setCacheEntry(entry);
michael@0 213
michael@0 214 this->attachToHead(entry);
michael@0 215 fCache.insert(key, entry);
michael@0 216
michael@0 217 if (ownershipFlags & kHide_OwnershipFlag) {
michael@0 218 this->makeExclusive(entry);
michael@0 219 }
michael@0 220
michael@0 221 }
michael@0 222
michael@0 223 void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
michael@0 224 GrAutoResourceCacheValidate atcv(this);
michael@0 225
michael@0 226 // When scratch textures are detached (to hide them from future finds) they
michael@0 227 // still count against the resource budget
michael@0 228 this->internalDetach(entry, kIgnore_BudgetBehavior);
michael@0 229 fCache.remove(entry->key(), entry);
michael@0 230
michael@0 231 #ifdef SK_DEBUG
michael@0 232 fExclusiveList.addToHead(entry);
michael@0 233 #endif
michael@0 234 }
michael@0 235
michael@0 236 void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
michael@0 237 // If the resource went invalid while it was detached then purge it
michael@0 238 // This can happen when a 3D context was lost,
michael@0 239 // the client called GrContext::contextDestroyed() to notify Gr,
michael@0 240 // and then later an SkGpuDevice's destructor releases its backing
michael@0 241 // texture (which was invalidated at contextDestroyed time).
michael@0 242 fClientDetachedCount -= 1;
michael@0 243 fEntryCount -= 1;
michael@0 244 size_t size = entry->resource()->sizeInBytes();
michael@0 245 fClientDetachedBytes -= size;
michael@0 246 fEntryBytes -= size;
michael@0 247 }
michael@0 248
michael@0 249 void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
michael@0 250 GrAutoResourceCacheValidate atcv(this);
michael@0 251
michael@0 252 #ifdef SK_DEBUG
michael@0 253 fExclusiveList.remove(entry);
michael@0 254 #endif
michael@0 255
michael@0 256 if (entry->resource()->isValid()) {
michael@0 257 // Since scratch textures still count against the cache budget even
michael@0 258 // when they have been removed from the cache, re-adding them doesn't
michael@0 259 // alter the budget information.
michael@0 260 attachToHead(entry, kIgnore_BudgetBehavior);
michael@0 261 fCache.insert(entry->key(), entry);
michael@0 262 } else {
michael@0 263 this->removeInvalidResource(entry);
michael@0 264 }
michael@0 265 }
michael@0 266
michael@0 267 /**
michael@0 268 * Destroying a resource may potentially trigger the unlock of additional
michael@0 269 * resources which in turn will trigger a nested purge. We block the nested
michael@0 270 * purge using the fPurging variable. However, the initial purge will keep
michael@0 271 * looping until either all resources in the cache are unlocked or we've met
michael@0 272 * the budget. There is an assertion in createAndLock to check against a
michael@0 273 * resource's destructor inserting new resources into the cache. If these
michael@0 274 * new resources were unlocked before purgeAsNeeded completed it could
michael@0 275 * potentially make purgeAsNeeded loop infinitely.
michael@0 276 *
michael@0 277 * extraCount and extraBytes are added to the current resource totals to account
michael@0 278 * for incoming resources (e.g., GrContext is about to add 10MB split between
michael@0 279 * 10 textures).
michael@0 280 */
michael@0 281 void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
michael@0 282 if (fPurging) {
michael@0 283 return;
michael@0 284 }
michael@0 285
michael@0 286 fPurging = true;
michael@0 287
michael@0 288 this->purgeInvalidated();
michael@0 289
michael@0 290 this->internalPurge(extraCount, extraBytes);
michael@0 291 if (((fEntryCount+extraCount) > fMaxCount ||
michael@0 292 (fEntryBytes+extraBytes) > fMaxBytes) &&
michael@0 293 NULL != fOverbudgetCB) {
michael@0 294 // Despite the purge we're still over budget. See if Ganesh can
michael@0 295 // release some resources and purge again.
michael@0 296 if ((*fOverbudgetCB)(fOverbudgetData)) {
michael@0 297 this->internalPurge(extraCount, extraBytes);
michael@0 298 }
michael@0 299 }
michael@0 300
michael@0 301 fPurging = false;
michael@0 302 }
michael@0 303
michael@0 304 void GrResourceCache::purgeInvalidated() {
michael@0 305 SkTDArray<GrResourceInvalidatedMessage> invalidated;
michael@0 306 fInvalidationInbox.poll(&invalidated);
michael@0 307
michael@0 308 for (int i = 0; i < invalidated.count(); i++) {
michael@0 309 // We're somewhat missing an opportunity here. We could use the
michael@0 310 // default find functor that gives us back resources whether we own
michael@0 311 // them exclusively or not, and when they're not exclusively owned mark
michael@0 312 // them for purging later when they do become exclusively owned.
michael@0 313 //
michael@0 314 // This is complicated and confusing. May try this in the future. For
michael@0 315 // now, these resources are just LRU'd as if we never got the message.
michael@0 316 while (GrResourceEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) {
michael@0 317 this->deleteResource(entry);
michael@0 318 }
michael@0 319 }
michael@0 320 }
michael@0 321
michael@0 322 void GrResourceCache::deleteResource(GrResourceEntry* entry) {
michael@0 323 SkASSERT(1 == entry->fResource->getRefCnt());
michael@0 324
michael@0 325 // remove from our cache
michael@0 326 fCache.remove(entry->key(), entry);
michael@0 327
michael@0 328 // remove from our llist
michael@0 329 this->internalDetach(entry);
michael@0 330 delete entry;
michael@0 331 }
michael@0 332
michael@0 333 void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
michael@0 334 SkASSERT(fPurging);
michael@0 335
michael@0 336 bool withinBudget = false;
michael@0 337 bool changed = false;
michael@0 338
michael@0 339 // The purging process is repeated several times since one pass
michael@0 340 // may free up other resources
michael@0 341 do {
michael@0 342 EntryList::Iter iter;
michael@0 343
michael@0 344 changed = false;
michael@0 345
michael@0 346 // Note: the following code relies on the fact that the
michael@0 347 // doubly linked list doesn't invalidate its data/pointers
michael@0 348 // outside of the specific area where a deletion occurs (e.g.,
michael@0 349 // in internalDetach)
michael@0 350 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
michael@0 351
michael@0 352 while (NULL != entry) {
michael@0 353 GrAutoResourceCacheValidate atcv(this);
michael@0 354
michael@0 355 if ((fEntryCount+extraCount) <= fMaxCount &&
michael@0 356 (fEntryBytes+extraBytes) <= fMaxBytes) {
michael@0 357 withinBudget = true;
michael@0 358 break;
michael@0 359 }
michael@0 360
michael@0 361 GrResourceEntry* prev = iter.prev();
michael@0 362 if (entry->fResource->unique()) {
michael@0 363 changed = true;
michael@0 364 this->deleteResource(entry);
michael@0 365 }
michael@0 366 entry = prev;
michael@0 367 }
michael@0 368 } while (!withinBudget && changed);
michael@0 369 }
michael@0 370
michael@0 371 void GrResourceCache::purgeAllUnlocked() {
michael@0 372 GrAutoResourceCacheValidate atcv(this);
michael@0 373
michael@0 374 // we can have one GrResource holding a lock on another
michael@0 375 // so we don't want to just do a simple loop kicking each
michael@0 376 // entry out. Instead change the budget and purge.
michael@0 377
michael@0 378 size_t savedMaxBytes = fMaxBytes;
michael@0 379 int savedMaxCount = fMaxCount;
michael@0 380 fMaxBytes = (size_t) -1;
michael@0 381 fMaxCount = 0;
michael@0 382 this->purgeAsNeeded();
michael@0 383
michael@0 384 #ifdef SK_DEBUG
michael@0 385 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
michael@0 386 SkASSERT(countBytes(fExclusiveList) == fClientDetachedBytes);
michael@0 387 if (!fCache.count()) {
michael@0 388 // Items may have been detached from the cache (such as the backing
michael@0 389 // texture for an SkGpuDevice). The above purge would not have removed
michael@0 390 // them.
michael@0 391 SkASSERT(fEntryCount == fClientDetachedCount);
michael@0 392 SkASSERT(fEntryBytes == fClientDetachedBytes);
michael@0 393 SkASSERT(fList.isEmpty());
michael@0 394 }
michael@0 395 #endif
michael@0 396
michael@0 397 fMaxBytes = savedMaxBytes;
michael@0 398 fMaxCount = savedMaxCount;
michael@0 399 }
michael@0 400
michael@0 401 ///////////////////////////////////////////////////////////////////////////////
michael@0 402
michael@0 403 #ifdef SK_DEBUG
michael@0 404 size_t GrResourceCache::countBytes(const EntryList& list) {
michael@0 405 size_t bytes = 0;
michael@0 406
michael@0 407 EntryList::Iter iter;
michael@0 408
michael@0 409 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
michael@0 410 EntryList::Iter::kTail_IterStart);
michael@0 411
michael@0 412 for ( ; NULL != entry; entry = iter.prev()) {
michael@0 413 bytes += entry->resource()->sizeInBytes();
michael@0 414 }
michael@0 415 return bytes;
michael@0 416 }
michael@0 417
michael@0 418 static bool both_zero_or_nonzero(int count, size_t bytes) {
michael@0 419 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
michael@0 420 }
michael@0 421
michael@0 422 void GrResourceCache::validate() const {
michael@0 423 fList.validate();
michael@0 424 fExclusiveList.validate();
michael@0 425 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
michael@0 426 SkASSERT(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
michael@0 427 SkASSERT(fClientDetachedBytes <= fEntryBytes);
michael@0 428 SkASSERT(fClientDetachedCount <= fEntryCount);
michael@0 429 SkASSERT((fEntryCount - fClientDetachedCount) == fCache.count());
michael@0 430
michael@0 431 EntryList::Iter iter;
michael@0 432
michael@0 433 // check that the exclusively held entries are okay
michael@0 434 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
michael@0 435 EntryList::Iter::kHead_IterStart);
michael@0 436
michael@0 437 for ( ; NULL != entry; entry = iter.next()) {
michael@0 438 entry->validate();
michael@0 439 }
michael@0 440
michael@0 441 // check that the shareable entries are okay
michael@0 442 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
michael@0 443
michael@0 444 int count = 0;
michael@0 445 for ( ; NULL != entry; entry = iter.next()) {
michael@0 446 entry->validate();
michael@0 447 SkASSERT(fCache.find(entry->key()));
michael@0 448 count += 1;
michael@0 449 }
michael@0 450 SkASSERT(count == fEntryCount - fClientDetachedCount);
michael@0 451
michael@0 452 size_t bytes = countBytes(fList);
michael@0 453 SkASSERT(bytes == fEntryBytes - fClientDetachedBytes);
michael@0 454
michael@0 455 bytes = countBytes(fExclusiveList);
michael@0 456 SkASSERT(bytes == fClientDetachedBytes);
michael@0 457
michael@0 458 SkASSERT(fList.countEntries() == fEntryCount - fClientDetachedCount);
michael@0 459
michael@0 460 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
michael@0 461 }
michael@0 462 #endif // SK_DEBUG
michael@0 463
michael@0 464 #if GR_CACHE_STATS
michael@0 465
michael@0 466 void GrResourceCache::printStats() {
michael@0 467 int locked = 0;
michael@0 468
michael@0 469 EntryList::Iter iter;
michael@0 470
michael@0 471 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
michael@0 472
michael@0 473 for ( ; NULL != entry; entry = iter.prev()) {
michael@0 474 if (entry->fResource->getRefCnt() > 1) {
michael@0 475 ++locked;
michael@0 476 }
michael@0 477 }
michael@0 478
michael@0 479 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
michael@0 480 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
michael@0 481 fEntryCount, locked, fHighWaterEntryCount);
michael@0 482 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
michael@0 483 fEntryBytes, fHighWaterEntryBytes);
michael@0 484 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
michael@0 485 fClientDetachedCount, fHighWaterClientDetachedCount);
michael@0 486 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
michael@0 487 fClientDetachedBytes, fHighWaterClientDetachedBytes);
michael@0 488 }
michael@0 489
michael@0 490 #endif
michael@0 491
michael@0 492 ///////////////////////////////////////////////////////////////////////////////

mercurial