blob: 3d8120be71355066dc033ca2cb6a107002780682 [file] [log] [blame]
joshualittb7133be2015-04-08 09:08:31 -07001/*
2 * Copyright 2015 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
8#include "GrTextBlobCache.h"
9
Florin Malita4a01ac92017-03-13 16:45:28 -040010DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage)
11
Chris Dalton9a986cf2018-10-18 15:27:59 -060012static inline bool SkShouldPostMessageToBus(
13 const GrTextBlobCache::PurgeBlobMessage& msg, uint32_t msgBusUniqueID) {
14 return msg.fContextID == msgBusUniqueID;
15}
16
joshualittb7133be2015-04-08 09:08:31 -070017GrTextBlobCache::~GrTextBlobCache() {
Robert Phillips303cd582018-02-14 18:54:01 -050018 this->freeAll();
joshualittb7133be2015-04-08 09:08:31 -070019}
20
joshualitt26ffc002015-04-16 11:24:04 -070021void GrTextBlobCache::freeAll() {
Florin Malita33fdb8d2017-03-07 16:51:57 -050022 fBlobIDCache.foreach([this](uint32_t, BlobIDCacheEntry* entry) {
Florin Malitac337c9e2017-03-10 18:02:29 +000023 for (const auto& blob : entry->fBlobs) {
24 fBlobList.remove(blob.get());
Florin Malita33fdb8d2017-03-07 16:51:57 -050025 }
26 });
27
28 fBlobIDCache.reset();
joshualitt20ccd402016-01-05 08:56:56 -080029
Herb Derbyb12175f2018-05-23 16:38:09 -040030 fCurrentSize = 0;
31
joshualitt20ccd402016-01-05 08:56:56 -080032 // There should be no allocations in the memory pool at this point
Florin Malita33fdb8d2017-03-07 16:51:57 -050033 SkASSERT(fBlobList.isEmpty());
joshualitt26ffc002015-04-16 11:24:04 -070034}
Florin Malita4a01ac92017-03-13 16:45:28 -040035
Jim Van Verth474d6872017-12-14 13:00:05 -050036void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
37 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040038 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040039}
Jim Van Verth76d917c2017-12-13 09:26:37 -050040
41void GrTextBlobCache::purgeStaleBlobs() {
42 SkTArray<PurgeBlobMessage> msgs;
43 fPurgeBlobInbox.poll(&msgs);
44
45 for (const auto& msg : msgs) {
Brian Salomon238069b2018-07-11 15:58:57 -040046 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050047 if (!idEntry) {
48 // no cache entries for id
49 continue;
50 }
51
52 // remove all blob entries from the LRU list
53 for (const auto& blob : idEntry->fBlobs) {
Herb Derbyb12175f2018-05-23 16:38:09 -040054 fCurrentSize -= blob->size();
Jim Van Verth76d917c2017-12-13 09:26:37 -050055 fBlobList.remove(blob.get());
56 }
57
58 // drop the idEntry itself (unrefs all blobs)
Brian Salomon238069b2018-07-11 15:58:57 -040059 fBlobIDCache.remove(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050060 }
61}
62
Herb Derby86240592018-05-24 16:12:31 -040063void GrTextBlobCache::checkPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -050064 // First, purge all stale blob IDs.
65 this->purgeStaleBlobs();
66
67 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -040068 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -050069 BitmapBlobList::Iter iter;
70 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -040071 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -040072 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -050073 // Backup the iterator before removing and unrefing the blob
74 iter.prev();
75
76 this->remove(lruBlob);
77 }
78
79 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
80 // use the call back and try to free some more. If we are still overbudget after this,
81 // then this single textblob is over our budget
82 if (blob && lruBlob == blob) {
83 (*fCallback)(fData);
84 }
85
86#ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -040087 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -050088 SkDebugf("Single textblob is larger than our whole budget");
89 }
90#endif
91 }
92}
93
94
95