blob: 8a9b4e3f39a492700230af5e9112c2bc78a03eeb [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
Herb Derby7b4ea9b2020-05-20 17:31:36 -040024sk_sp<GrTextBlob>
25GrTextBlobCache::makeCachedBlob(const SkGlyphRunList& glyphRunList, const GrTextBlob::Key& key,
26 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derbybe202052020-06-05 17:22:38 -040027 const SkMatrix& viewMatrix) {
28 sk_sp<GrTextBlob> cacheBlob(GrTextBlob::Make(glyphRunList, viewMatrix));
Herb Derby7b4ea9b2020-05-20 17:31:36 -040029 cacheBlob->setupKey(key, blurRec, glyphRunList.paint());
30 this->add(cacheBlob);
Herb Derbya50830b2020-05-21 14:01:09 -040031 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID);
Herb Derby7b4ea9b2020-05-20 17:31:36 -040032 return cacheBlob;
33}
34
35sk_sp<GrTextBlob> GrTextBlobCache::find(const GrTextBlob::Key& key) const {
36 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
37 return idEntry ? idEntry->find(key) : nullptr;
38}
39
40void GrTextBlobCache::remove(GrTextBlob* blob) {
41 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
42 auto* idEntry = fBlobIDCache.find(id);
43 SkASSERT(idEntry);
44
45 fCurrentSize -= blob->size();
46 fBlobList.remove(blob);
47 idEntry->removeBlob(blob);
48 if (idEntry->fBlobs.empty()) {
49 fBlobIDCache.remove(id);
50 }
51}
52
53void GrTextBlobCache::makeMRU(GrTextBlob* blob) {
54 if (fBlobList.head() == blob) {
55 return;
56 }
57
58 fBlobList.remove(blob);
59 fBlobList.addToHead(blob);
60}
61
joshualitt26ffc002015-04-16 11:24:04 -070062void GrTextBlobCache::freeAll() {
Florin Malita33fdb8d2017-03-07 16:51:57 -050063 fBlobIDCache.reset();
Herb Derby3b6b7472020-06-23 12:45:15 -040064 fBlobList.reset();
Herb Derbyb12175f2018-05-23 16:38:09 -040065 fCurrentSize = 0;
joshualitt26ffc002015-04-16 11:24:04 -070066}
Florin Malita4a01ac92017-03-13 16:45:28 -040067
Herb Derby7b4ea9b2020-05-20 17:31:36 -040068void GrTextBlobCache::setBudget(size_t budget) {
69 fSizeBudget = budget;
70 this->checkPurge();
71}
72
Jim Van Verth474d6872017-12-14 13:00:05 -050073void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
74 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040075 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040076}
Jim Van Verth76d917c2017-12-13 09:26:37 -050077
78void GrTextBlobCache::purgeStaleBlobs() {
79 SkTArray<PurgeBlobMessage> msgs;
80 fPurgeBlobInbox.poll(&msgs);
81
82 for (const auto& msg : msgs) {
Brian Salomon238069b2018-07-11 15:58:57 -040083 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050084 if (!idEntry) {
85 // no cache entries for id
86 continue;
87 }
88
89 // remove all blob entries from the LRU list
90 for (const auto& blob : idEntry->fBlobs) {
Herb Derbyb12175f2018-05-23 16:38:09 -040091 fCurrentSize -= blob->size();
Jim Van Verth76d917c2017-12-13 09:26:37 -050092 fBlobList.remove(blob.get());
93 }
94
95 // drop the idEntry itself (unrefs all blobs)
Brian Salomon238069b2018-07-11 15:58:57 -040096 fBlobIDCache.remove(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050097 }
98}
99
Herb Derby86240592018-05-24 16:12:31 -0400100void GrTextBlobCache::checkPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500101 // First, purge all stale blob IDs.
102 this->purgeStaleBlobs();
103
104 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -0400105 if (fCurrentSize > fSizeBudget) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400106 TextBlobList::Iter iter;
107 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -0400108 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -0400109 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500110 // Backup the iterator before removing and unrefing the blob
111 iter.prev();
112
113 this->remove(lruBlob);
114 }
115
116 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
117 // use the call back and try to free some more. If we are still overbudget after this,
118 // then this single textblob is over our budget
119 if (blob && lruBlob == blob) {
Herb Derbyd6fe76a2020-05-21 12:05:04 -0400120 fPurgeMore();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500121 }
122
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400123 #ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -0400124 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500125 SkDebugf("Single textblob is larger than our whole budget");
126 }
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400127 #endif
Jim Van Verth76d917c2017-12-13 09:26:37 -0500128 }
129}
130
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400131void GrTextBlobCache::add(sk_sp<GrTextBlob> blob) {
132 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
133 auto* idEntry = fBlobIDCache.find(id);
134 if (!idEntry) {
135 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
136 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500137
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400138 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
139 GrTextBlob* rawBlobPtr = blob.get();
140 fBlobList.addToHead(rawBlobPtr);
141 fCurrentSize += blob->size();
142 idEntry->addBlob(std::move(blob));
Jim Van Verth76d917c2017-12-13 09:26:37 -0500143
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400144 this->checkPurge(rawBlobPtr);
145}
146
147GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
148
149GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
150
151uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) {
152 return entry.fID;
153}
154
155void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) {
156 SkASSERT(blob);
157 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
158 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
159
160 fBlobs.emplace_back(std::move(blob));
161}
162
163void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) {
164 SkASSERT(blob);
165 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
166
167 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
168 SkASSERT(index >= 0);
169
170 fBlobs.removeShuffle(index);
171}
172
173sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const {
174 auto index = this->findBlobIndex(key);
175 return index < 0 ? nullptr : fBlobs[index];
176}
177
178int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const {
179 for (int i = 0; i < fBlobs.count(); ++i) {
180 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
181 return i;
182 }
183 }
184 return -1;
185}