blob: baee93ee20dcb13d09d8e5760a1f50c710233630 [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 Derby9553a572021-02-24 09:28:31 -050023sk_sp<GrTextBlob> GrTextBlobCache::addOrReturnExisting(
24 const SkGlyphRunList& glyphRunList, sk_sp<GrTextBlob> blob) {
Herb Derby13e3fae2020-07-23 13:24:53 -040025 SkAutoSpinlock lock{fSpinLock};
Herb Derby9553a572021-02-24 09:28:31 -050026 blob = this->internalAdd(std::move(blob));
Herb Derbya50830b2020-05-21 14:01:09 -040027 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID);
Herb Derby9553a572021-02-24 09:28:31 -050028 return blob;
Herb Derby7b4ea9b2020-05-20 17:31:36 -040029}
30
Herb Derby13e3fae2020-07-23 13:24:53 -040031sk_sp<GrTextBlob> GrTextBlobCache::find(const GrTextBlob::Key& key) {
32 SkAutoSpinlock lock{fSpinLock};
33 const BlobIDCacheEntry* idEntry = fBlobIDCache.find(key.fUniqueID);
34 if (idEntry == nullptr) {
35 return nullptr;
36 }
37
38 sk_sp<GrTextBlob> blob = idEntry->find(key);
39 GrTextBlob* blobPtr = blob.get();
40 if (blobPtr != nullptr && blobPtr != fBlobList.head()) {
41 fBlobList.remove(blobPtr);
42 fBlobList.addToHead(blobPtr);
43 }
44 return blob;
Herb Derby7b4ea9b2020-05-20 17:31:36 -040045}
46
47void GrTextBlobCache::remove(GrTextBlob* blob) {
Herb Derby13e3fae2020-07-23 13:24:53 -040048 SkAutoSpinlock lock{fSpinLock};
Herb Derbyc5f25bc2020-06-23 14:02:23 -040049 this->internalRemove(blob);
50}
51
52void GrTextBlobCache::internalRemove(GrTextBlob* blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -040053 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
54 auto* idEntry = fBlobIDCache.find(id);
55 SkASSERT(idEntry);
56
57 fCurrentSize -= blob->size();
58 fBlobList.remove(blob);
59 idEntry->removeBlob(blob);
60 if (idEntry->fBlobs.empty()) {
61 fBlobIDCache.remove(id);
62 }
63}
64
joshualitt26ffc002015-04-16 11:24:04 -070065void GrTextBlobCache::freeAll() {
Herb Derby13e3fae2020-07-23 13:24:53 -040066 SkAutoSpinlock lock{fSpinLock};
Florin Malita33fdb8d2017-03-07 16:51:57 -050067 fBlobIDCache.reset();
Herb Derbyadac2882020-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
Jim Van Verth474d6872017-12-14 13:00:05 -050072void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
73 SkASSERT(blobID != SK_InvalidGenID);
Brian Salomon238069b2018-07-11 15:58:57 -040074 SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
Florin Malita4a01ac92017-03-13 16:45:28 -040075}
Jim Van Verth76d917c2017-12-13 09:26:37 -050076
77void GrTextBlobCache::purgeStaleBlobs() {
Herb Derby13e3fae2020-07-23 13:24:53 -040078 SkAutoSpinlock lock{fSpinLock};
Herb Derbyc5f25bc2020-06-23 14:02:23 -040079 this->internalPurgeStaleBlobs();
80}
81
82void GrTextBlobCache::internalPurgeStaleBlobs() {
Jim Van Verth76d917c2017-12-13 09:26:37 -050083 SkTArray<PurgeBlobMessage> msgs;
84 fPurgeBlobInbox.poll(&msgs);
85
86 for (const auto& msg : msgs) {
Brian Salomon238069b2018-07-11 15:58:57 -040087 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -050088 if (!idEntry) {
89 // no cache entries for id
90 continue;
91 }
92
93 // remove all blob entries from the LRU list
94 for (const auto& blob : idEntry->fBlobs) {
Herb Derbyb12175f2018-05-23 16:38:09 -040095 fCurrentSize -= blob->size();
Jim Van Verth76d917c2017-12-13 09:26:37 -050096 fBlobList.remove(blob.get());
97 }
98
99 // drop the idEntry itself (unrefs all blobs)
Brian Salomon238069b2018-07-11 15:58:57 -0400100 fBlobIDCache.remove(msg.fBlobID);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500101 }
102}
103
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400104size_t GrTextBlobCache::usedBytes() const {
Herb Derby13e3fae2020-07-23 13:24:53 -0400105 SkAutoSpinlock lock{fSpinLock};
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400106 return fCurrentSize;
107}
108
Herb Derby1ca54d42020-06-26 12:50:38 -0400109bool GrTextBlobCache::isOverBudget() const {
Herb Derby13e3fae2020-07-23 13:24:53 -0400110 SkAutoSpinlock lock{fSpinLock};
Herb Derby1ca54d42020-06-26 12:50:38 -0400111 return fCurrentSize > fSizeBudget;
112}
113
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400114void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500115 // First, purge all stale blob IDs.
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400116 this->internalPurgeStaleBlobs();
Jim Van Verth76d917c2017-12-13 09:26:37 -0500117
118 // If we are still over budget, then unref until we are below budget again
Herb Derbyb12175f2018-05-23 16:38:09 -0400119 if (fCurrentSize > fSizeBudget) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400120 TextBlobList::Iter iter;
121 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
Herb Derby86240592018-05-24 16:12:31 -0400122 GrTextBlob* lruBlob = nullptr;
Herb Derbyb12175f2018-05-23 16:38:09 -0400123 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500124 // Backup the iterator before removing and unrefing the blob
125 iter.prev();
126
Herb Derbyc5f25bc2020-06-23 14:02:23 -0400127 this->internalRemove(lruBlob);
Jim Van Verth76d917c2017-12-13 09:26:37 -0500128 }
129
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400130 #ifdef SPEW_BUDGET_MESSAGE
Herb Derbyb12175f2018-05-23 16:38:09 -0400131 if (fCurrentSize > fSizeBudget) {
Jim Van Verth76d917c2017-12-13 09:26:37 -0500132 SkDebugf("Single textblob is larger than our whole budget");
133 }
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400134 #endif
Jim Van Verth76d917c2017-12-13 09:26:37 -0500135 }
136}
137
Herb Derby9553a572021-02-24 09:28:31 -0500138sk_sp<GrTextBlob> GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) {
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400139 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
140 auto* idEntry = fBlobIDCache.find(id);
141 if (!idEntry) {
142 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
143 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500144
Herb Derby9553a572021-02-24 09:28:31 -0500145 if (sk_sp<GrTextBlob> alreadyIn = idEntry->find(GrTextBlob::GetKey(*blob)); alreadyIn) {
146 blob = std::move(alreadyIn);
147 } else {
148 fBlobList.addToHead(blob.get());
149 fCurrentSize += blob->size();
150 idEntry->addBlob(blob);
151 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500152
Herb Derby9553a572021-02-24 09:28:31 -0500153 this->internalCheckPurge(blob.get());
154 return blob;
Herb Derby7b4ea9b2020-05-20 17:31:36 -0400155}
156
157GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
158
159GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
160
161uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) {
162 return entry.fID;
163}
164
165void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) {
166 SkASSERT(blob);
167 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
168 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
169
170 fBlobs.emplace_back(std::move(blob));
171}
172
173void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) {
174 SkASSERT(blob);
175 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
176
177 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
178 SkASSERT(index >= 0);
179
180 fBlobs.removeShuffle(index);
181}
182
183sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const {
184 auto index = this->findBlobIndex(key);
185 return index < 0 ? nullptr : fBlobs[index];
186}
187
188int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const {
189 for (int i = 0; i < fBlobs.count(); ++i) {
190 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
191 return i;
192 }
193 }
194 return -1;
195}