blob: 215e5a3b8dfb1e1082326634c08a786e70c7dd93 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/text/GrTextBlobCache.h"
joshualittb7133be2015-04-08 09:08:31 -07009
Florin Malita4a01ac92017-03-13 16:45:28 -040010DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage)
11
Herb Derby7b4ea9b2020-05-20 17:31:36 -040012// This function is captured by the above macro using implementations from SkMessageBus.h
Chris Dalton9a986cf2018-10-18 15:27:59 -060013static inline bool SkShouldPostMessageToBus(
14 const GrTextBlobCache::PurgeBlobMessage& msg, uint32_t msgBusUniqueID) {
15 return msg.fContextID == msgBusUniqueID;
16}
17
Herb Derbya50830b2020-05-21 14:01:09 -040018GrTextBlobCache::GrTextBlobCache(PurgeMore purgeMore, uint32_t messageBusID)
Herb Derbyd6fe76a2020-05-21 12:05:04 -040019 : fPurgeMore(purgeMore)
Herb Derby7b4ea9b2020-05-20 17:31:36 -040020 , fSizeBudget(kDefaultBudget)
Herb Derbya50830b2020-05-21 14:01:09 -040021 , fMessageBusID(messageBusID)
22 , fPurgeBlobInbox(messageBusID) { }
Herb Derby7b4ea9b2020-05-20 17:31:36 -040023
joshualittb7133be2015-04-08 09:08:31 -070024GrTextBlobCache::~GrTextBlobCache() {
Robert Phillips303cd582018-02-14 18:54:01 -050025 this->freeAll();
joshualittb7133be2015-04-08 09:08:31 -070026}
27
Herb Derby7b4ea9b2020-05-20 17:31:36 -040028sk_sp<GrTextBlob>
29GrTextBlobCache::makeCachedBlob(const SkGlyphRunList& glyphRunList, const GrTextBlob::Key& key,
30 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby692e59d2020-06-05 11:37:16 -040031 const SkMatrix& viewMatrix, GrColor color) {
32 sk_sp<GrTextBlob> cacheBlob(GrTextBlob::Make(glyphRunList, viewMatrix, color));
Herb Derby7b4ea9b2020-05-20 17:31:36 -040033 cacheBlob->setupKey(key, blurRec, glyphRunList.paint());
34 this->add(cacheBlob);
Herb Derbya50830b2020-05-21 14:01:09 -040035 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID);
Herb Derby7b4ea9b2020-05-20 17:31:36 -040036 return cacheBlob;
37}
38
39sk_sp<GrTextBlob> GrTextBlobCache::find(const GrTextBlob::Key& key) const {
40 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
41 return idEntry ? idEntry->find(key) : nullptr;
42}
43
44void GrTextBlobCache::remove(GrTextBlob* blob) {
45 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
46 auto* idEntry = fBlobIDCache.find(id);
47 SkASSERT(idEntry);
48
49 fCurrentSize -= blob->size();
50 fBlobList.remove(blob);
51 idEntry->removeBlob(blob);
52 if (idEntry->fBlobs.empty()) {
53 fBlobIDCache.remove(id);
54 }
55}
56
57void GrTextBlobCache::makeMRU(GrTextBlob* blob) {
58 if (fBlobList.head() == blob) {
59 return;
60 }
61
62 fBlobList.remove(blob);
63 fBlobList.addToHead(blob);
64}
65
joshualitt26ffc002015-04-16 11:24:04 -070066void GrTextBlobCache::freeAll() {
Florin Malita33fdb8d2017-03-07 16:51:57 -050067 fBlobIDCache.foreach([this](uint32_t, BlobIDCacheEntry* entry) {
Florin Malitac337c9e2017-03-10 18:02:29 +000068 for (const auto& blob : entry->fBlobs) {
69 fBlobList.remove(blob.get());
Florin Malita33fdb8d2017-03-07 16:51:57 -050070 }
71 });
72
73 fBlobIDCache.reset();
joshualitt20ccd402016-01-05 08:56:56 -080074
Herb Derbyb12175f2018-05-23 16:38:09 -040075 fCurrentSize = 0;
76
joshualitt20ccd402016-01-05 08:56:56 -080077 // There should be no allocations in the memory pool at this point
Florin Malita33fdb8d2017-03-07 16:51:57 -050078 SkASSERT(fBlobList.isEmpty());
joshualitt26ffc002015-04-16 11:24:04 -070079}
Florin Malita4a01ac92017-03-13 16:45:28 -040080
Herb Derby7b4ea9b2020-05-20 17:31:36 -040081void GrTextBlobCache::setBudget(size_t budget) {
82 fSizeBudget = budget;
83 this->checkPurge();
84}
85
Jim Van Verth474d6872017-12-14 13:00:05 -050086void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
87 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040088 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040089}
Jim Van Verth76d917c2017-12-13 09:26:37 -050090
91void GrTextBlobCache::purgeStaleBlobs() {
92 SkTArray<PurgeBlobMessage> msgs;
93 fPurgeBlobInbox.poll(&msgs);
94
95 for (const auto& msg : msgs) {
Brian Salomon238069b2018-07-11 15:58:57 -040096 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050097 if (!idEntry) {
98 // no cache entries for id
99 continue;
100 }
101
102 // remove all blob entries from the LRU list
103 for (const auto& blob : idEntry->fBlobs) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400104 fCurrentSize -= blob->size();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500105 fBlobList.remove(blob.get());
106 }
107
108 // drop the idEntry itself (unrefs all blobs)
Brian Salomon238069b2018-07-11 15:58:57 -0400109 fBlobIDCache.remove(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500110 }
111}
112
Herb Derby86240592018-05-24 16:12:31 -0400113void GrTextBlobCache::checkPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500114 // First, purge all stale blob IDs.
115 this->purgeStaleBlobs();
116
117 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -0400118 if (fCurrentSize > fSizeBudget) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400119 TextBlobList::Iter iter;
120 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -0400121 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -0400122 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500123 // Backup the iterator before removing and unrefing the blob
124 iter.prev();
125
126 this->remove(lruBlob);
127 }
128
129 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
130 // use the call back and try to free some more. If we are still overbudget after this,
131 // then this single textblob is over our budget
132 if (blob && lruBlob == blob) {
Herb Derbyd6fe76a2020-05-21 12:05:04 -0400133 fPurgeMore();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500134 }
135
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400136 #ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -0400137 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500138 SkDebugf("Single textblob is larger than our whole budget");
139 }
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400140 #endif
Jim Van Verth76d917c2017-12-13 09:26:37 -0500141 }
142}
143
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400144void GrTextBlobCache::add(sk_sp<GrTextBlob> blob) {
145 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
146 auto* idEntry = fBlobIDCache.find(id);
147 if (!idEntry) {
148 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
149 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500150
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400151 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
152 GrTextBlob* rawBlobPtr = blob.get();
153 fBlobList.addToHead(rawBlobPtr);
154 fCurrentSize += blob->size();
155 idEntry->addBlob(std::move(blob));
Jim Van Verth76d917c2017-12-13 09:26:37 -0500156
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400157 this->checkPurge(rawBlobPtr);
158}
159
160GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
161
162GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
163
164uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) {
165 return entry.fID;
166}
167
168void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) {
169 SkASSERT(blob);
170 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
171 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
172
173 fBlobs.emplace_back(std::move(blob));
174}
175
176void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) {
177 SkASSERT(blob);
178 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
179
180 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
181 SkASSERT(index >= 0);
182
183 fBlobs.removeShuffle(index);
184}
185
186sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const {
187 auto index = this->findBlobIndex(key);
188 return index < 0 ? nullptr : fBlobs[index];
189}
190
191int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const {
192 for (int i = 0; i < fBlobs.count(); ++i) {
193 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
194 return i;
195 }
196 }
197 return -1;
198}