joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/text/GrTextBlobCache.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 9 | |
Peng Huang | 0408afc | 2021-03-06 21:16:15 -0500 | [diff] [blame] | 10 | DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage, true) |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 11 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 12 | // This function is captured by the above macro using implementations from SkMessageBus.h |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 13 | static inline bool SkShouldPostMessageToBus( |
| 14 | const GrTextBlobCache::PurgeBlobMessage& msg, uint32_t msgBusUniqueID) { |
| 15 | return msg.fContextID == msgBusUniqueID; |
| 16 | } |
| 17 | |
Herb Derby | 1ca54d4 | 2020-06-26 12:50:38 -0400 | [diff] [blame] | 18 | GrTextBlobCache::GrTextBlobCache(uint32_t messageBusID) |
| 19 | : fSizeBudget(kDefaultBudget) |
Herb Derby | a50830b | 2020-05-21 14:01:09 -0400 | [diff] [blame] | 20 | , fMessageBusID(messageBusID) |
| 21 | , fPurgeBlobInbox(messageBusID) { } |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 22 | |
Herb Derby | 9553a57 | 2021-02-24 09:28:31 -0500 | [diff] [blame] | 23 | sk_sp<GrTextBlob> GrTextBlobCache::addOrReturnExisting( |
| 24 | const SkGlyphRunList& glyphRunList, sk_sp<GrTextBlob> blob) { |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 25 | SkAutoSpinlock lock{fSpinLock}; |
Herb Derby | 9553a57 | 2021-02-24 09:28:31 -0500 | [diff] [blame] | 26 | blob = this->internalAdd(std::move(blob)); |
Herb Derby | a50830b | 2020-05-21 14:01:09 -0400 | [diff] [blame] | 27 | glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID); |
Herb Derby | 9553a57 | 2021-02-24 09:28:31 -0500 | [diff] [blame] | 28 | return blob; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 31 | sk_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 Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void GrTextBlobCache::remove(GrTextBlob* blob) { |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 48 | SkAutoSpinlock lock{fSpinLock}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 49 | this->internalRemove(blob); |
| 50 | } |
| 51 | |
| 52 | void GrTextBlobCache::internalRemove(GrTextBlob* blob) { |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 53 | auto id = blob->key().fUniqueID; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 54 | auto* idEntry = fBlobIDCache.find(id); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 55 | |
Herb Derby | c15c19c | 2021-02-24 14:54:24 -0500 | [diff] [blame] | 56 | if (idEntry != nullptr) { |
| 57 | sk_sp<GrTextBlob> stillExists = idEntry->find(blob->key()); |
| 58 | if (blob == stillExists.get()) { |
| 59 | fCurrentSize -= blob->size(); |
| 60 | fBlobList.remove(blob); |
| 61 | idEntry->removeBlob(blob); |
| 62 | if (idEntry->fBlobs.empty()) { |
| 63 | fBlobIDCache.remove(id); |
| 64 | } |
| 65 | } |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 69 | void GrTextBlobCache::freeAll() { |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 70 | SkAutoSpinlock lock{fSpinLock}; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 71 | fBlobIDCache.reset(); |
Herb Derby | adac288 | 2020-06-23 12:45:15 -0400 | [diff] [blame] | 72 | fBlobList.reset(); |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 73 | fCurrentSize = 0; |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 74 | } |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 75 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 76 | void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) { |
| 77 | SkASSERT(blobID != SK_InvalidGenID); |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 78 | SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID)); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 79 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 80 | |
| 81 | void GrTextBlobCache::purgeStaleBlobs() { |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 82 | SkAutoSpinlock lock{fSpinLock}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 83 | this->internalPurgeStaleBlobs(); |
| 84 | } |
| 85 | |
| 86 | void GrTextBlobCache::internalPurgeStaleBlobs() { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 87 | SkTArray<PurgeBlobMessage> msgs; |
| 88 | fPurgeBlobInbox.poll(&msgs); |
| 89 | |
| 90 | for (const auto& msg : msgs) { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 91 | auto* idEntry = fBlobIDCache.find(msg.fBlobID); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 92 | 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 Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 99 | fCurrentSize -= blob->size(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 100 | fBlobList.remove(blob.get()); |
| 101 | } |
| 102 | |
| 103 | // drop the idEntry itself (unrefs all blobs) |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 104 | fBlobIDCache.remove(msg.fBlobID); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 108 | size_t GrTextBlobCache::usedBytes() const { |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 109 | SkAutoSpinlock lock{fSpinLock}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 110 | return fCurrentSize; |
| 111 | } |
| 112 | |
Herb Derby | 1ca54d4 | 2020-06-26 12:50:38 -0400 | [diff] [blame] | 113 | bool GrTextBlobCache::isOverBudget() const { |
Herb Derby | 13e3fae | 2020-07-23 13:24:53 -0400 | [diff] [blame] | 114 | SkAutoSpinlock lock{fSpinLock}; |
Herb Derby | 1ca54d4 | 2020-06-26 12:50:38 -0400 | [diff] [blame] | 115 | return fCurrentSize > fSizeBudget; |
| 116 | } |
| 117 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 118 | void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 119 | // First, purge all stale blob IDs. |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 120 | this->internalPurgeStaleBlobs(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 121 | |
| 122 | // If we are still over budget, then unref until we are below budget again |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 123 | if (fCurrentSize > fSizeBudget) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 124 | TextBlobList::Iter iter; |
| 125 | iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 126 | GrTextBlob* lruBlob = nullptr; |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 127 | while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 128 | // Backup the iterator before removing and unrefing the blob |
| 129 | iter.prev(); |
| 130 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 131 | this->internalRemove(lruBlob); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 134 | #ifdef SPEW_BUDGET_MESSAGE |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 135 | if (fCurrentSize > fSizeBudget) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 136 | SkDebugf("Single textblob is larger than our whole budget"); |
| 137 | } |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 138 | #endif |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Herb Derby | 9553a57 | 2021-02-24 09:28:31 -0500 | [diff] [blame] | 142 | sk_sp<GrTextBlob> GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) { |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 143 | auto id = blob->key().fUniqueID; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 144 | auto* idEntry = fBlobIDCache.find(id); |
| 145 | if (!idEntry) { |
| 146 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 147 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 148 | |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 149 | if (sk_sp<GrTextBlob> alreadyIn = idEntry->find(blob->key()); alreadyIn) { |
Herb Derby | 9553a57 | 2021-02-24 09:28:31 -0500 | [diff] [blame] | 150 | blob = std::move(alreadyIn); |
| 151 | } else { |
| 152 | fBlobList.addToHead(blob.get()); |
| 153 | fCurrentSize += blob->size(); |
| 154 | idEntry->addBlob(blob); |
| 155 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 156 | |
Herb Derby | 9553a57 | 2021-02-24 09:28:31 -0500 | [diff] [blame] | 157 | this->internalCheckPurge(blob.get()); |
| 158 | return blob; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 162 | |
| 163 | GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 164 | |
| 165 | uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) { |
| 166 | return entry.fID; |
| 167 | } |
| 168 | |
| 169 | void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) { |
| 170 | SkASSERT(blob); |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 171 | SkASSERT(blob->key().fUniqueID == fID); |
| 172 | SkASSERT(!this->find(blob->key())); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 173 | |
| 174 | fBlobs.emplace_back(std::move(blob)); |
| 175 | } |
| 176 | |
| 177 | void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) { |
| 178 | SkASSERT(blob); |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 179 | SkASSERT(blob->key().fUniqueID == fID); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 180 | |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 181 | auto index = this->findBlobIndex(blob->key()); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 182 | SkASSERT(index >= 0); |
| 183 | |
| 184 | fBlobs.removeShuffle(index); |
| 185 | } |
| 186 | |
| 187 | sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const { |
| 188 | auto index = this->findBlobIndex(key); |
| 189 | return index < 0 ? nullptr : fBlobs[index]; |
| 190 | } |
| 191 | |
| 192 | int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const { |
| 193 | for (int i = 0; i < fBlobs.count(); ++i) { |
Herb Derby | c8e31ea | 2021-03-08 12:42:17 -0500 | [diff] [blame] | 194 | if (fBlobs[i]->key() == key) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 195 | return i; |
| 196 | } |
| 197 | } |
| 198 | return -1; |
| 199 | } |