blob: a7647be0cc8e674d53f7e2e73ac1ccd7689f8bf8 [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 Derby1ca54d42020-06-26 12:50:38 -040018GrTextBlobCache::GrTextBlobCache(uint32_t messageBusID)
19 : fSizeBudget(kDefaultBudget)
Herb Derbya50830b2020-05-21 14:01:09 -040020 , fMessageBusID(messageBusID)
21 , fPurgeBlobInbox(messageBusID) { }
Herb Derby7b4ea9b2020-05-20 17:31:36 -040022
Herb Derby7b4ea9b2020-05-20 17:31:36 -040023sk_sp<GrTextBlob>
24GrTextBlobCache::makeCachedBlob(const SkGlyphRunList& glyphRunList, const GrTextBlob::Key& key,
25 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derbybe202052020-06-05 17:22:38 -040026 const SkMatrix& viewMatrix) {
27 sk_sp<GrTextBlob> cacheBlob(GrTextBlob::Make(glyphRunList, viewMatrix));
Herb Derby7b4ea9b2020-05-20 17:31:36 -040028 cacheBlob->setupKey(key, blurRec, glyphRunList.paint());
Herb Derby13e3fae2020-07-23 13:24:53 -040029 SkAutoSpinlock lock{fSpinLock};
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
Herb Derby13e3fae2020-07-23 13:24:53 -040035sk_sp<GrTextBlob> GrTextBlobCache::find(const GrTextBlob::Key& key) {
36 SkAutoSpinlock lock{fSpinLock};
37 const BlobIDCacheEntry* idEntry = fBlobIDCache.find(key.fUniqueID);
38 if (idEntry == nullptr) {
39 return nullptr;
40 }
41
42 sk_sp<GrTextBlob> blob = idEntry->find(key);
43 GrTextBlob* blobPtr = blob.get();
44 if (blobPtr != nullptr && blobPtr != fBlobList.head()) {
45 fBlobList.remove(blobPtr);
46 fBlobList.addToHead(blobPtr);
47 }
48 return blob;
Herb Derby7b4ea9b2020-05-20 17:31:36 -040049}
50
51void GrTextBlobCache::remove(GrTextBlob* blob) {
Herb Derby13e3fae2020-07-23 13:24:53 -040052 SkAutoSpinlock lock{fSpinLock};
Herb Derbyc5f25bc2020-06-23 14:02:23 -040053 this->internalRemove(blob);
54}
55
56void GrTextBlobCache::internalRemove(GrTextBlob* blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -040057 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
58 auto* idEntry = fBlobIDCache.find(id);
59 SkASSERT(idEntry);
60
61 fCurrentSize -= blob->size();
62 fBlobList.remove(blob);
63 idEntry->removeBlob(blob);
64 if (idEntry->fBlobs.empty()) {
65 fBlobIDCache.remove(id);
66 }
67}
68
joshualitt26ffc002015-04-16 11:24:04 -070069void GrTextBlobCache::freeAll() {
Herb Derby13e3fae2020-07-23 13:24:53 -040070 SkAutoSpinlock lock{fSpinLock};
Florin Malita33fdb8d2017-03-07 16:51:57 -050071 fBlobIDCache.reset();
Herb Derbyadac2882020-06-23 12:45:15 -040072 fBlobList.reset();
Herb Derbyb12175f2018-05-23 16:38:09 -040073 fCurrentSize = 0;
joshualitt26ffc002015-04-16 11:24:04 -070074}
Florin Malita4a01ac92017-03-13 16:45:28 -040075
Jim Van Verth474d6872017-12-14 13:00:05 -050076void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
77 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040078 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040079}
Jim Van Verth76d917c2017-12-13 09:26:37 -050080
81void GrTextBlobCache::purgeStaleBlobs() {
Herb Derby13e3fae2020-07-23 13:24:53 -040082 SkAutoSpinlock lock{fSpinLock};
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 {
Herb Derby13e3fae2020-07-23 13:24:53 -0400109 SkAutoSpinlock lock{fSpinLock};
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400110 return fCurrentSize;
111}
112
Herb Derby1ca54d42020-06-26 12:50:38 -0400113bool GrTextBlobCache::isOverBudget() const {
Herb Derby13e3fae2020-07-23 13:24:53 -0400114 SkAutoSpinlock lock{fSpinLock};
Herb Derby1ca54d42020-06-26 12:50:38 -0400115 return fCurrentSize > fSizeBudget;
116}
117
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400118void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500119 // First, purge all stale blob IDs.
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400120 this->internalPurgeStaleBlobs();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500121
122 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -0400123 if (fCurrentSize > fSizeBudget) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400124 TextBlobList::Iter iter;
125 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -0400126 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -0400127 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500128 // Backup the iterator before removing and unrefing the blob
129 iter.prev();
130
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400131 this->internalRemove(lruBlob);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500132 }
133
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400134 #ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -0400135 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500136 SkDebugf("Single textblob is larger than our whole budget");
137 }
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400138 #endif
Jim Van Verth76d917c2017-12-13 09:26:37 -0500139 }
140}
141
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400142void GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400143 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
144 auto* idEntry = fBlobIDCache.find(id);
145 if (!idEntry) {
146 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
147 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500148
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400149 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
150 GrTextBlob* rawBlobPtr = blob.get();
151 fBlobList.addToHead(rawBlobPtr);
152 fCurrentSize += blob->size();
153 idEntry->addBlob(std::move(blob));
Jim Van Verth76d917c2017-12-13 09:26:37 -0500154
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400155 this->internalCheckPurge(rawBlobPtr);
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400156}
157
158GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
159
160GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
161
162uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) {
163 return entry.fID;
164}
165
166void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) {
167 SkASSERT(blob);
168 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
169 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
170
171 fBlobs.emplace_back(std::move(blob));
172}
173
174void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) {
175 SkASSERT(blob);
176 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
177
178 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
179 SkASSERT(index >= 0);
180
181 fBlobs.removeShuffle(index);
182}
183
184sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const {
185 auto index = this->findBlobIndex(key);
186 return index < 0 ? nullptr : fBlobs[index];
187}
188
189int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const {
190 for (int i = 0; i < fBlobs.count(); ++i) {
191 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
192 return i;
193 }
194 }
195 return -1;
196}