SkResourceCache.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/core/SkResourceCache.h"
  8. #include "include/core/SkTraceMemoryDump.h"
  9. #include "include/private/SkMutex.h"
  10. #include "include/private/SkTo.h"
  11. #include "src/core/SkDiscardableMemory.h"
  12. #include "src/core/SkMessageBus.h"
  13. #include "src/core/SkMipMap.h"
  14. #include "src/core/SkOpts.h"
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage)
  18. static inline bool SkShouldPostMessageToBus(
  19. const SkResourceCache::PurgeSharedIDMessage&, uint32_t) {
  20. // SkResourceCache is typically used as a singleton and we don't label Inboxes so all messages
  21. // go to all inboxes.
  22. return true;
  23. }
  24. // This can be defined by the caller's build system
  25. //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE
  26. #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT
  27. # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024
  28. #endif
  29. #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT
  30. #define SK_DEFAULT_IMAGE_CACHE_LIMIT (32 * 1024 * 1024)
  31. #endif
  32. void SkResourceCache::Key::init(void* nameSpace, uint64_t sharedID, size_t dataSize) {
  33. SkASSERT(SkAlign4(dataSize) == dataSize);
  34. // fCount32 and fHash are not hashed
  35. static const int kUnhashedLocal32s = 2; // fCache32 + fHash
  36. static const int kSharedIDLocal32s = 2; // fSharedID_lo + fSharedID_hi
  37. static const int kHashedLocal32s = kSharedIDLocal32s + (sizeof(fNamespace) >> 2);
  38. static const int kLocal32s = kUnhashedLocal32s + kHashedLocal32s;
  39. static_assert(sizeof(Key) == (kLocal32s << 2), "unaccounted_key_locals");
  40. static_assert(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespace),
  41. "namespace_field_must_be_last");
  42. fCount32 = SkToS32(kLocal32s + (dataSize >> 2));
  43. fSharedID_lo = (uint32_t)(sharedID & 0xFFFFFFFF);
  44. fSharedID_hi = (uint32_t)(sharedID >> 32);
  45. fNamespace = nameSpace;
  46. // skip unhashed fields when computing the hash
  47. fHash = SkOpts::hash(this->as32() + kUnhashedLocal32s,
  48. (fCount32 - kUnhashedLocal32s) << 2);
  49. }
  50. #include "include/private/SkTHash.h"
  51. namespace {
  52. struct HashTraits {
  53. static uint32_t Hash(const SkResourceCache::Key& key) { return key.hash(); }
  54. static const SkResourceCache::Key& GetKey(const SkResourceCache::Rec* rec) {
  55. return rec->getKey();
  56. }
  57. };
  58. }
  59. class SkResourceCache::Hash :
  60. public SkTHashTable<SkResourceCache::Rec*, SkResourceCache::Key, HashTraits> {};
  61. ///////////////////////////////////////////////////////////////////////////////
  62. void SkResourceCache::init() {
  63. fHead = nullptr;
  64. fTail = nullptr;
  65. fHash = new Hash;
  66. fTotalBytesUsed = 0;
  67. fCount = 0;
  68. fSingleAllocationByteLimit = 0;
  69. // One of these should be explicit set by the caller after we return.
  70. fTotalByteLimit = 0;
  71. fDiscardableFactory = nullptr;
  72. }
  73. SkResourceCache::SkResourceCache(DiscardableFactory factory) {
  74. this->init();
  75. fDiscardableFactory = factory;
  76. }
  77. SkResourceCache::SkResourceCache(size_t byteLimit) {
  78. this->init();
  79. fTotalByteLimit = byteLimit;
  80. }
  81. SkResourceCache::~SkResourceCache() {
  82. Rec* rec = fHead;
  83. while (rec) {
  84. Rec* next = rec->fNext;
  85. delete rec;
  86. rec = next;
  87. }
  88. delete fHash;
  89. }
  90. ////////////////////////////////////////////////////////////////////////////////
  91. bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) {
  92. this->checkMessages();
  93. if (auto found = fHash->find(key)) {
  94. Rec* rec = *found;
  95. if (visitor(*rec, context)) {
  96. this->moveToHead(rec); // for our LRU
  97. return true;
  98. } else {
  99. this->remove(rec); // stale
  100. return false;
  101. }
  102. }
  103. return false;
  104. }
  105. static void make_size_str(size_t size, SkString* str) {
  106. const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 };
  107. int i = 0;
  108. while (suffix[i] && (size > 1024)) {
  109. i += 1;
  110. size >>= 10;
  111. }
  112. str->printf("%zu%c", size, suffix[i]);
  113. }
  114. static bool gDumpCacheTransactions;
  115. void SkResourceCache::add(Rec* rec, void* payload) {
  116. this->checkMessages();
  117. SkASSERT(rec);
  118. // See if we already have this key (racy inserts, etc.)
  119. if (Rec** preexisting = fHash->find(rec->getKey())) {
  120. Rec* prev = *preexisting;
  121. if (prev->canBePurged()) {
  122. // if it can be purged, the install may fail, so we have to remove it
  123. this->remove(prev);
  124. } else {
  125. // if it cannot be purged, we reuse it and delete the new one
  126. prev->postAddInstall(payload);
  127. delete rec;
  128. return;
  129. }
  130. }
  131. this->addToHead(rec);
  132. fHash->set(rec);
  133. rec->postAddInstall(payload);
  134. if (gDumpCacheTransactions) {
  135. SkString bytesStr, totalStr;
  136. make_size_str(rec->bytesUsed(), &bytesStr);
  137. make_size_str(fTotalBytesUsed, &totalStr);
  138. SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n",
  139. bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount);
  140. }
  141. // since the new rec may push us over-budget, we perform a purge check now
  142. this->purgeAsNeeded();
  143. }
  144. void SkResourceCache::remove(Rec* rec) {
  145. SkASSERT(rec->canBePurged());
  146. size_t used = rec->bytesUsed();
  147. SkASSERT(used <= fTotalBytesUsed);
  148. this->release(rec);
  149. fHash->remove(rec->getKey());
  150. fTotalBytesUsed -= used;
  151. fCount -= 1;
  152. //SkDebugf("-RC count [%3d] bytes %d\n", fCount, fTotalBytesUsed);
  153. if (gDumpCacheTransactions) {
  154. SkString bytesStr, totalStr;
  155. make_size_str(used, &bytesStr);
  156. make_size_str(fTotalBytesUsed, &totalStr);
  157. SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n",
  158. bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount);
  159. }
  160. delete rec;
  161. }
  162. void SkResourceCache::purgeAsNeeded(bool forcePurge) {
  163. size_t byteLimit;
  164. int countLimit;
  165. if (fDiscardableFactory) {
  166. countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT;
  167. byteLimit = UINT32_MAX; // no limit based on bytes
  168. } else {
  169. countLimit = SK_MaxS32; // no limit based on count
  170. byteLimit = fTotalByteLimit;
  171. }
  172. Rec* rec = fTail;
  173. while (rec) {
  174. if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) {
  175. break;
  176. }
  177. Rec* prev = rec->fPrev;
  178. if (rec->canBePurged()) {
  179. this->remove(rec);
  180. }
  181. rec = prev;
  182. }
  183. }
  184. //#define SK_TRACK_PURGE_SHAREDID_HITRATE
  185. #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE
  186. static int gPurgeCallCounter;
  187. static int gPurgeHitCounter;
  188. #endif
  189. void SkResourceCache::purgeSharedID(uint64_t sharedID) {
  190. if (0 == sharedID) {
  191. return;
  192. }
  193. #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE
  194. gPurgeCallCounter += 1;
  195. bool found = false;
  196. #endif
  197. // go backwards, just like purgeAsNeeded, just to make the code similar.
  198. // could iterate either direction and still be correct.
  199. Rec* rec = fTail;
  200. while (rec) {
  201. Rec* prev = rec->fPrev;
  202. if (rec->getKey().getSharedID() == sharedID) {
  203. // even though the "src" is now dead, caches could still be in-flight, so
  204. // we have to check if it can be removed.
  205. if (rec->canBePurged()) {
  206. this->remove(rec);
  207. }
  208. #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE
  209. found = true;
  210. #endif
  211. }
  212. rec = prev;
  213. }
  214. #ifdef SK_TRACK_PURGE_SHAREDID_HITRATE
  215. if (found) {
  216. gPurgeHitCounter += 1;
  217. }
  218. SkDebugf("PurgeShared calls=%d hits=%d rate=%g\n", gPurgeCallCounter, gPurgeHitCounter,
  219. gPurgeHitCounter * 100.0 / gPurgeCallCounter);
  220. #endif
  221. }
  222. void SkResourceCache::visitAll(Visitor visitor, void* context) {
  223. // go backwards, just like purgeAsNeeded, just to make the code similar.
  224. // could iterate either direction and still be correct.
  225. Rec* rec = fTail;
  226. while (rec) {
  227. visitor(*rec, context);
  228. rec = rec->fPrev;
  229. }
  230. }
  231. ///////////////////////////////////////////////////////////////////////////////////////////////////
  232. size_t SkResourceCache::setTotalByteLimit(size_t newLimit) {
  233. size_t prevLimit = fTotalByteLimit;
  234. fTotalByteLimit = newLimit;
  235. if (newLimit < prevLimit) {
  236. this->purgeAsNeeded();
  237. }
  238. return prevLimit;
  239. }
  240. SkCachedData* SkResourceCache::newCachedData(size_t bytes) {
  241. this->checkMessages();
  242. if (fDiscardableFactory) {
  243. SkDiscardableMemory* dm = fDiscardableFactory(bytes);
  244. return dm ? new SkCachedData(bytes, dm) : nullptr;
  245. } else {
  246. return new SkCachedData(sk_malloc_throw(bytes), bytes);
  247. }
  248. }
  249. ///////////////////////////////////////////////////////////////////////////////
  250. void SkResourceCache::release(Rec* rec) {
  251. Rec* prev = rec->fPrev;
  252. Rec* next = rec->fNext;
  253. if (!prev) {
  254. SkASSERT(fHead == rec);
  255. fHead = next;
  256. } else {
  257. prev->fNext = next;
  258. }
  259. if (!next) {
  260. fTail = prev;
  261. } else {
  262. next->fPrev = prev;
  263. }
  264. rec->fNext = rec->fPrev = nullptr;
  265. }
  266. void SkResourceCache::moveToHead(Rec* rec) {
  267. if (fHead == rec) {
  268. return;
  269. }
  270. SkASSERT(fHead);
  271. SkASSERT(fTail);
  272. this->validate();
  273. this->release(rec);
  274. fHead->fPrev = rec;
  275. rec->fNext = fHead;
  276. fHead = rec;
  277. this->validate();
  278. }
  279. void SkResourceCache::addToHead(Rec* rec) {
  280. this->validate();
  281. rec->fPrev = nullptr;
  282. rec->fNext = fHead;
  283. if (fHead) {
  284. fHead->fPrev = rec;
  285. }
  286. fHead = rec;
  287. if (!fTail) {
  288. fTail = rec;
  289. }
  290. fTotalBytesUsed += rec->bytesUsed();
  291. fCount += 1;
  292. this->validate();
  293. }
  294. ///////////////////////////////////////////////////////////////////////////////
  295. #ifdef SK_DEBUG
  296. void SkResourceCache::validate() const {
  297. if (nullptr == fHead) {
  298. SkASSERT(nullptr == fTail);
  299. SkASSERT(0 == fTotalBytesUsed);
  300. return;
  301. }
  302. if (fHead == fTail) {
  303. SkASSERT(nullptr == fHead->fPrev);
  304. SkASSERT(nullptr == fHead->fNext);
  305. SkASSERT(fHead->bytesUsed() == fTotalBytesUsed);
  306. return;
  307. }
  308. SkASSERT(nullptr == fHead->fPrev);
  309. SkASSERT(fHead->fNext);
  310. SkASSERT(nullptr == fTail->fNext);
  311. SkASSERT(fTail->fPrev);
  312. size_t used = 0;
  313. int count = 0;
  314. const Rec* rec = fHead;
  315. while (rec) {
  316. count += 1;
  317. used += rec->bytesUsed();
  318. SkASSERT(used <= fTotalBytesUsed);
  319. rec = rec->fNext;
  320. }
  321. SkASSERT(fCount == count);
  322. rec = fTail;
  323. while (rec) {
  324. SkASSERT(count > 0);
  325. count -= 1;
  326. SkASSERT(used >= rec->bytesUsed());
  327. used -= rec->bytesUsed();
  328. rec = rec->fPrev;
  329. }
  330. SkASSERT(0 == count);
  331. SkASSERT(0 == used);
  332. }
  333. #endif
  334. void SkResourceCache::dump() const {
  335. this->validate();
  336. SkDebugf("SkResourceCache: count=%d bytes=%d %s\n",
  337. fCount, fTotalBytesUsed, fDiscardableFactory ? "discardable" : "malloc");
  338. }
  339. size_t SkResourceCache::setSingleAllocationByteLimit(size_t newLimit) {
  340. size_t oldLimit = fSingleAllocationByteLimit;
  341. fSingleAllocationByteLimit = newLimit;
  342. return oldLimit;
  343. }
  344. size_t SkResourceCache::getSingleAllocationByteLimit() const {
  345. return fSingleAllocationByteLimit;
  346. }
  347. size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const {
  348. // fSingleAllocationByteLimit == 0 means the caller is asking for our default
  349. size_t limit = fSingleAllocationByteLimit;
  350. // if we're not discardable (i.e. we are fixed-budget) then cap the single-limit
  351. // to our budget.
  352. if (nullptr == fDiscardableFactory) {
  353. if (0 == limit) {
  354. limit = fTotalByteLimit;
  355. } else {
  356. limit = SkTMin(limit, fTotalByteLimit);
  357. }
  358. }
  359. return limit;
  360. }
  361. void SkResourceCache::checkMessages() {
  362. SkTArray<PurgeSharedIDMessage> msgs;
  363. fPurgeSharedIDInbox.poll(&msgs);
  364. for (int i = 0; i < msgs.count(); ++i) {
  365. this->purgeSharedID(msgs[i].fSharedID);
  366. }
  367. }
  368. ///////////////////////////////////////////////////////////////////////////////
  369. static SkResourceCache* gResourceCache = nullptr;
  370. static SkMutex& resource_cache_mutex() {
  371. static SkMutex& mutex = *(new SkMutex);
  372. return mutex;
  373. }
  374. /** Must hold resource_cache_mutex() when calling. */
  375. static SkResourceCache* get_cache() {
  376. // resource_cache_mutex() is always held when this is called, so we don't need to be fancy in here.
  377. resource_cache_mutex().assertHeld();
  378. if (nullptr == gResourceCache) {
  379. #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
  380. gResourceCache = new SkResourceCache(SkDiscardableMemory::Create);
  381. #else
  382. gResourceCache = new SkResourceCache(SK_DEFAULT_IMAGE_CACHE_LIMIT);
  383. #endif
  384. }
  385. return gResourceCache;
  386. }
  387. size_t SkResourceCache::GetTotalBytesUsed() {
  388. SkAutoMutexExclusive am(resource_cache_mutex());
  389. return get_cache()->getTotalBytesUsed();
  390. }
  391. size_t SkResourceCache::GetTotalByteLimit() {
  392. SkAutoMutexExclusive am(resource_cache_mutex());
  393. return get_cache()->getTotalByteLimit();
  394. }
  395. size_t SkResourceCache::SetTotalByteLimit(size_t newLimit) {
  396. SkAutoMutexExclusive am(resource_cache_mutex());
  397. return get_cache()->setTotalByteLimit(newLimit);
  398. }
  399. SkResourceCache::DiscardableFactory SkResourceCache::GetDiscardableFactory() {
  400. SkAutoMutexExclusive am(resource_cache_mutex());
  401. return get_cache()->discardableFactory();
  402. }
  403. SkCachedData* SkResourceCache::NewCachedData(size_t bytes) {
  404. SkAutoMutexExclusive am(resource_cache_mutex());
  405. return get_cache()->newCachedData(bytes);
  406. }
  407. void SkResourceCache::Dump() {
  408. SkAutoMutexExclusive am(resource_cache_mutex());
  409. get_cache()->dump();
  410. }
  411. size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) {
  412. SkAutoMutexExclusive am(resource_cache_mutex());
  413. return get_cache()->setSingleAllocationByteLimit(size);
  414. }
  415. size_t SkResourceCache::GetSingleAllocationByteLimit() {
  416. SkAutoMutexExclusive am(resource_cache_mutex());
  417. return get_cache()->getSingleAllocationByteLimit();
  418. }
  419. size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() {
  420. SkAutoMutexExclusive am(resource_cache_mutex());
  421. return get_cache()->getEffectiveSingleAllocationByteLimit();
  422. }
  423. void SkResourceCache::PurgeAll() {
  424. SkAutoMutexExclusive am(resource_cache_mutex());
  425. return get_cache()->purgeAll();
  426. }
  427. bool SkResourceCache::Find(const Key& key, FindVisitor visitor, void* context) {
  428. SkAutoMutexExclusive am(resource_cache_mutex());
  429. return get_cache()->find(key, visitor, context);
  430. }
  431. void SkResourceCache::Add(Rec* rec, void* payload) {
  432. SkAutoMutexExclusive am(resource_cache_mutex());
  433. get_cache()->add(rec, payload);
  434. }
  435. void SkResourceCache::VisitAll(Visitor visitor, void* context) {
  436. SkAutoMutexExclusive am(resource_cache_mutex());
  437. get_cache()->visitAll(visitor, context);
  438. }
  439. void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) {
  440. if (sharedID) {
  441. SkMessageBus<PurgeSharedIDMessage>::Post(PurgeSharedIDMessage(sharedID));
  442. }
  443. }
  444. ///////////////////////////////////////////////////////////////////////////////
  445. #include "include/core/SkGraphics.h"
  446. #include "include/core/SkImageFilter.h"
  447. size_t SkGraphics::GetResourceCacheTotalBytesUsed() {
  448. return SkResourceCache::GetTotalBytesUsed();
  449. }
  450. size_t SkGraphics::GetResourceCacheTotalByteLimit() {
  451. return SkResourceCache::GetTotalByteLimit();
  452. }
  453. size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) {
  454. return SkResourceCache::SetTotalByteLimit(newLimit);
  455. }
  456. size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() {
  457. return SkResourceCache::GetSingleAllocationByteLimit();
  458. }
  459. size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) {
  460. return SkResourceCache::SetSingleAllocationByteLimit(newLimit);
  461. }
  462. void SkGraphics::PurgeResourceCache() {
  463. SkImageFilter::PurgeCache();
  464. return SkResourceCache::PurgeAll();
  465. }
  466. /////////////
  467. static void dump_visitor(const SkResourceCache::Rec& rec, void*) {
  468. SkDebugf("RC: %12s bytes %9lu discardable %p\n",
  469. rec.getCategory(), rec.bytesUsed(), rec.diagnostic_only_getDiscardable());
  470. }
  471. void SkResourceCache::TestDumpMemoryStatistics() {
  472. VisitAll(dump_visitor, nullptr);
  473. }
  474. static void sk_trace_dump_visitor(const SkResourceCache::Rec& rec, void* context) {
  475. SkTraceMemoryDump* dump = static_cast<SkTraceMemoryDump*>(context);
  476. SkString dumpName = SkStringPrintf("skia/sk_resource_cache/%s_%p", rec.getCategory(), &rec);
  477. SkDiscardableMemory* discardable = rec.diagnostic_only_getDiscardable();
  478. if (discardable) {
  479. dump->setDiscardableMemoryBacking(dumpName.c_str(), *discardable);
  480. // The discardable memory size will be calculated by dumper, but we also dump what we think
  481. // the size of object in memory is irrespective of whether object is live or dead.
  482. dump->dumpNumericValue(dumpName.c_str(), "discardable_size", "bytes", rec.bytesUsed());
  483. } else {
  484. dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", rec.bytesUsed());
  485. dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr);
  486. }
  487. }
  488. void SkResourceCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) {
  489. // Since resource could be backed by malloc or discardable, the cache always dumps detailed
  490. // stats to be accurate.
  491. VisitAll(sk_trace_dump_visitor, dump);
  492. }