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 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 10 | DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage) |
| 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 | a50830b | 2020-05-21 14:01:09 -0400 | [diff] [blame] | 18 | GrTextBlobCache::GrTextBlobCache(PurgeMore purgeMore, uint32_t messageBusID) |
Herb Derby | d6fe76a | 2020-05-21 12:05:04 -0400 | [diff] [blame] | 19 | : fPurgeMore(purgeMore) |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 20 | , fSizeBudget(kDefaultBudget) |
Herb Derby | a50830b | 2020-05-21 14:01:09 -0400 | [diff] [blame] | 21 | , fMessageBusID(messageBusID) |
| 22 | , fPurgeBlobInbox(messageBusID) { } |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 23 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 24 | sk_sp<GrTextBlob> |
| 25 | GrTextBlobCache::makeCachedBlob(const SkGlyphRunList& glyphRunList, const GrTextBlob::Key& key, |
| 26 | const SkMaskFilterBase::BlurRec& blurRec, |
Herb Derby | be20205 | 2020-06-05 17:22:38 -0400 | [diff] [blame] | 27 | const SkMatrix& viewMatrix) { |
| 28 | sk_sp<GrTextBlob> cacheBlob(GrTextBlob::Make(glyphRunList, viewMatrix)); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 29 | cacheBlob->setupKey(key, blurRec, glyphRunList.paint()); |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 30 | this->internalAdd(cacheBlob); |
Herb Derby | a50830b | 2020-05-21 14:01:09 -0400 | [diff] [blame] | 31 | glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 32 | return cacheBlob; |
| 33 | } |
| 34 | |
| 35 | sk_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 | |
| 40 | void GrTextBlobCache::remove(GrTextBlob* blob) { |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 41 | this->internalRemove(blob); |
| 42 | } |
| 43 | |
| 44 | void GrTextBlobCache::internalRemove(GrTextBlob* blob) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 45 | 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 | |
| 57 | void GrTextBlobCache::makeMRU(GrTextBlob* blob) { |
| 58 | if (fBlobList.head() == blob) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | fBlobList.remove(blob); |
| 63 | fBlobList.addToHead(blob); |
| 64 | } |
| 65 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 66 | void GrTextBlobCache::freeAll() { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 67 | fBlobIDCache.reset(); |
Herb Derby | adac288 | 2020-06-23 12:45:15 -0400 | [diff] [blame^] | 68 | fBlobList.reset(); |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 69 | fCurrentSize = 0; |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 70 | } |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 71 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 72 | void GrTextBlobCache::setBudget(size_t budget) { |
| 73 | fSizeBudget = budget; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 74 | this->internalCheckPurge(); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 77 | void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) { |
| 78 | SkASSERT(blobID != SK_InvalidGenID); |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 79 | SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID)); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 80 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 81 | |
| 82 | void GrTextBlobCache::purgeStaleBlobs() { |
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 { |
| 109 | return fCurrentSize; |
| 110 | } |
| 111 | |
| 112 | void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 113 | // First, purge all stale blob IDs. |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 114 | this->internalPurgeStaleBlobs(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 115 | |
| 116 | // 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] | 117 | if (fCurrentSize > fSizeBudget) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 118 | TextBlobList::Iter iter; |
| 119 | iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 120 | GrTextBlob* lruBlob = nullptr; |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 121 | while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 122 | // Backup the iterator before removing and unrefing the blob |
| 123 | iter.prev(); |
| 124 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 125 | this->internalRemove(lruBlob); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 126 | } |
| 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 Derby | d6fe76a | 2020-05-21 12:05:04 -0400 | [diff] [blame] | 132 | fPurgeMore(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 135 | #ifdef SPEW_BUDGET_MESSAGE |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 136 | if (fCurrentSize > fSizeBudget) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 137 | SkDebugf("Single textblob is larger than our whole budget"); |
| 138 | } |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 139 | #endif |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 143 | void GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 144 | 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 Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 149 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 150 | // 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 Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 155 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 156 | this->internalCheckPurge(rawBlobPtr); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 160 | |
| 161 | GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 162 | |
| 163 | uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) { |
| 164 | return entry.fID; |
| 165 | } |
| 166 | |
| 167 | void 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 | |
| 175 | void 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 | |
| 185 | sk_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 | |
| 190 | int 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 | } |