blob: 29c855e705665116a3c17e08640a089dd4a499aa [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
Michael Ludwig3824f582020-06-24 16:38:56 +000024GrTextBlobCache::~GrTextBlobCache() {
25 this->freeAll();
26}
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 Derbybe202052020-06-05 17:22:38 -040031 const SkMatrix& viewMatrix) {
32 sk_sp<GrTextBlob> cacheBlob(GrTextBlob::Make(glyphRunList, viewMatrix));
Herb Derby7b4ea9b2020-05-20 17:31:36 -040033 cacheBlob->setupKey(key, blurRec, glyphRunList.paint());
Herb Derbyc5f25bc2020-06-23 14:02:23 -040034 this->internalAdd(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) {
Herb Derbyc5f25bc2020-06-23 14:02:23 -040045 this->internalRemove(blob);
46}
47
48void GrTextBlobCache::internalRemove(GrTextBlob* blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -040049 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
50 auto* idEntry = fBlobIDCache.find(id);
51 SkASSERT(idEntry);
52
53 fCurrentSize -= blob->size();
54 fBlobList.remove(blob);
55 idEntry->removeBlob(blob);
56 if (idEntry->fBlobs.empty()) {
57 fBlobIDCache.remove(id);
58 }
59}
60
61void GrTextBlobCache::makeMRU(GrTextBlob* blob) {
62 if (fBlobList.head() == blob) {
63 return;
64 }
65
66 fBlobList.remove(blob);
67 fBlobList.addToHead(blob);
68}
69
joshualitt26ffc002015-04-16 11:24:04 -070070void GrTextBlobCache::freeAll() {
Michael Ludwig3824f582020-06-24 16:38:56 +000071 fBlobIDCache.foreach([this](uint32_t, BlobIDCacheEntry* entry) {
72 for (const auto& blob : entry->fBlobs) {
73 fBlobList.remove(blob.get());
74 }
75 });
76
Florin Malita33fdb8d2017-03-07 16:51:57 -050077 fBlobIDCache.reset();
Michael Ludwig3824f582020-06-24 16:38:56 +000078
Herb Derbyb12175f2018-05-23 16:38:09 -040079 fCurrentSize = 0;
Michael Ludwig3824f582020-06-24 16:38:56 +000080
81 // There should be no allocations in the memory pool at this point
82 SkASSERT(fBlobList.isEmpty());
joshualitt26ffc002015-04-16 11:24:04 -070083}
Florin Malita4a01ac92017-03-13 16:45:28 -040084
Herb Derby7b4ea9b2020-05-20 17:31:36 -040085void GrTextBlobCache::setBudget(size_t budget) {
86 fSizeBudget = budget;
Herb Derbyc5f25bc2020-06-23 14:02:23 -040087 this->internalCheckPurge();
Herb Derby7b4ea9b2020-05-20 17:31:36 -040088}
89
Jim Van Verth474d6872017-12-14 13:00:05 -050090void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
91 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040092 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040093}
Jim Van Verth76d917c2017-12-13 09:26:37 -050094
95void GrTextBlobCache::purgeStaleBlobs() {
Herb Derbyc5f25bc2020-06-23 14:02:23 -040096 this->internalPurgeStaleBlobs();
97}
98
99void GrTextBlobCache::internalPurgeStaleBlobs() {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500100 SkTArray<PurgeBlobMessage> msgs;
101 fPurgeBlobInbox.poll(&msgs);
102
103 for (const auto& msg : msgs) {
Brian Salomon238069b2018-07-11 15:58:57 -0400104 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500105 if (!idEntry) {
106 // no cache entries for id
107 continue;
108 }
109
110 // remove all blob entries from the LRU list
111 for (const auto& blob : idEntry->fBlobs) {
Herb Derbyb12175f2018-05-23 16:38:09 -0400112 fCurrentSize -= blob->size();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500113 fBlobList.remove(blob.get());
114 }
115
116 // drop the idEntry itself (unrefs all blobs)
Brian Salomon238069b2018-07-11 15:58:57 -0400117 fBlobIDCache.remove(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500118 }
119}
120
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400121size_t GrTextBlobCache::usedBytes() const {
122 return fCurrentSize;
123}
124
125void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500126 // First, purge all stale blob IDs.
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400127 this->internalPurgeStaleBlobs();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500128
129 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -0400130 if (fCurrentSize > fSizeBudget) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400131 TextBlobList::Iter iter;
132 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -0400133 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -0400134 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500135 // Backup the iterator before removing and unrefing the blob
136 iter.prev();
137
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400138 this->internalRemove(lruBlob);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500139 }
140
141 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
142 // use the call back and try to free some more. If we are still overbudget after this,
143 // then this single textblob is over our budget
144 if (blob && lruBlob == blob) {
Herb Derbyd6fe76a2020-05-21 12:05:04 -0400145 fPurgeMore();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500146 }
147
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400148 #ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -0400149 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500150 SkDebugf("Single textblob is larger than our whole budget");
151 }
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400152 #endif
Jim Van Verth76d917c2017-12-13 09:26:37 -0500153 }
154}
155
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400156void GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400157 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
158 auto* idEntry = fBlobIDCache.find(id);
159 if (!idEntry) {
160 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
161 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500162
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400163 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
164 GrTextBlob* rawBlobPtr = blob.get();
165 fBlobList.addToHead(rawBlobPtr);
166 fCurrentSize += blob->size();
167 idEntry->addBlob(std::move(blob));
Jim Van Verth76d917c2017-12-13 09:26:37 -0500168
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400169 this->internalCheckPurge(rawBlobPtr);
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400170}
171
172GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
173
174GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
175
176uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) {
177 return entry.fID;
178}
179
180void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) {
181 SkASSERT(blob);
182 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
183 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
184
185 fBlobs.emplace_back(std::move(blob));
186}
187
188void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) {
189 SkASSERT(blob);
190 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
191
192 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
193 SkASSERT(index >= 0);
194
195 fBlobs.removeShuffle(index);
196}
197
198sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const {
199 auto index = this->findBlobIndex(key);
200 return index < 0 ? nullptr : fBlobs[index];
201}
202
203int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const {
204 for (int i = 0; i < fBlobs.count(); ++i) {
205 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
206 return i;
207 }
208 }
209 return -1;
210}