blob: f981b597b9b4123f4ca916df6b51cbbf806c6396 [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());
Herb Derbyc5f25bc2020-06-23 14:02:23 -040030 this->internalAdd(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) {
Herb Derbyc5f25bc2020-06-23 14:02:23 -040041 this->internalRemove(blob);
42}
43
44void GrTextBlobCache::internalRemove(GrTextBlob* blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -040045 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.reset();
Herb Derby3b6b7472020-06-23 12:45:15 -040068 fBlobList.reset();
Herb Derbyb12175f2018-05-23 16:38:09 -040069 fCurrentSize = 0;
joshualitt26ffc002015-04-16 11:24:04 -070070}
Florin Malita4a01ac92017-03-13 16:45:28 -040071
Herb Derby7b4ea9b2020-05-20 17:31:36 -040072void GrTextBlobCache::setBudget(size_t budget) {
73 fSizeBudget = budget;
Herb Derbyc5f25bc2020-06-23 14:02:23 -040074 this->internalCheckPurge();
Herb Derby7b4ea9b2020-05-20 17:31:36 -040075}
76
Jim Van Verth474d6872017-12-14 13:00:05 -050077void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
78 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040079 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040080}
Jim Van Verth76d917c2017-12-13 09:26:37 -050081
82void GrTextBlobCache::purgeStaleBlobs() {
Herb Derbyc5f25bc2020-06-23 14:02:23 -040083 this->internalPurgeStaleBlobs();
84}
85
86void GrTextBlobCache::internalPurgeStaleBlobs() {
Jim Van Verth76d917c2017-12-13 09:26:37 -050087 SkTArray<PurgeBlobMessage> msgs;
88 fPurgeBlobInbox.poll(&msgs);
89
90 for (const auto& msg : msgs) {
Brian Salomon238069b2018-07-11 15:58:57 -040091 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050092 if (!idEntry) {
93 // no cache entries for id
94 continue;
95 }
96
97 // remove all blob entries from the LRU list
98 for (const auto& blob : idEntry->fBlobs) {
Herb Derbyb12175f2018-05-23 16:38:09 -040099 fCurrentSize -= blob->size();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500100 fBlobList.remove(blob.get());
101 }
102
103 // drop the idEntry itself (unrefs all blobs)
Brian Salomon238069b2018-07-11 15:58:57 -0400104 fBlobIDCache.remove(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500105 }
106}
107
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400108size_t GrTextBlobCache::usedBytes() const {
109 return fCurrentSize;
110}
111
112void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500113 // First, purge all stale blob IDs.
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400114 this->internalPurgeStaleBlobs();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500115
116 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -0400117 if (fCurrentSize > fSizeBudget) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400118 TextBlobList::Iter iter;
119 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -0400120 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -0400121 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500122 // Backup the iterator before removing and unrefing the blob
123 iter.prev();
124
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400125 this->internalRemove(lruBlob);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500126 }
127
128 // If we break out of the loop with lruBlob == blob, then we haven't purged enough
129 // use the call back and try to free some more. If we are still overbudget after this,
130 // then this single textblob is over our budget
131 if (blob && lruBlob == blob) {
Herb Derbyd6fe76a2020-05-21 12:05:04 -0400132 fPurgeMore();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500133 }
134
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400135 #ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -0400136 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500137 SkDebugf("Single textblob is larger than our whole budget");
138 }
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400139 #endif
Jim Van Verth76d917c2017-12-13 09:26:37 -0500140 }
141}
142
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400143void GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400144 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
145 auto* idEntry = fBlobIDCache.find(id);
146 if (!idEntry) {
147 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
148 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500149
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400150 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
151 GrTextBlob* rawBlobPtr = blob.get();
152 fBlobList.addToHead(rawBlobPtr);
153 fCurrentSize += blob->size();
154 idEntry->addBlob(std::move(blob));
Jim Van Verth76d917c2017-12-13 09:26:37 -0500155
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400156 this->internalCheckPurge(rawBlobPtr);
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400157}
158
159GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
160
161GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
162
163uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) {
164 return entry.fID;
165}
166
167void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) {
168 SkASSERT(blob);
169 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
170 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
171
172 fBlobs.emplace_back(std::move(blob));
173}
174
175void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) {
176 SkASSERT(blob);
177 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
178
179 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
180 SkASSERT(index >= 0);
181
182 fBlobs.removeShuffle(index);
183}
184
185sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const {
186 auto index = this->findBlobIndex(key);
187 return index < 0 ? nullptr : fBlobs[index];
188}
189
190int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const {
191 for (int i = 0; i < fBlobs.count(); ++i) {
192 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
193 return i;
194 }
195 }
196 return -1;
197}