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 | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 30 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 31 | this->internalAdd(cacheBlob); |
Herb Derby | a50830b | 2020-05-21 14:01:09 -0400 | [diff] [blame] | 32 | glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 33 | return cacheBlob; |
| 34 | } |
| 35 | |
| 36 | sk_sp<GrTextBlob> GrTextBlobCache::find(const GrTextBlob::Key& key) const { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 37 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 38 | const auto* idEntry = fBlobIDCache.find(key.fUniqueID); |
| 39 | return idEntry ? idEntry->find(key) : nullptr; |
| 40 | } |
| 41 | |
| 42 | void GrTextBlobCache::remove(GrTextBlob* blob) { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 43 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 44 | this->internalRemove(blob); |
| 45 | } |
| 46 | |
| 47 | void GrTextBlobCache::internalRemove(GrTextBlob* blob) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 48 | auto id = GrTextBlob::GetKey(*blob).fUniqueID; |
| 49 | auto* idEntry = fBlobIDCache.find(id); |
| 50 | SkASSERT(idEntry); |
| 51 | |
| 52 | fCurrentSize -= blob->size(); |
| 53 | fBlobList.remove(blob); |
| 54 | idEntry->removeBlob(blob); |
| 55 | if (idEntry->fBlobs.empty()) { |
| 56 | fBlobIDCache.remove(id); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void GrTextBlobCache::makeMRU(GrTextBlob* blob) { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 61 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 62 | if (fBlobList.head() == blob) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | fBlobList.remove(blob); |
| 67 | fBlobList.addToHead(blob); |
| 68 | } |
| 69 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 70 | void GrTextBlobCache::freeAll() { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 71 | SkAutoMutexExclusive lock{fMutex}; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 72 | fBlobIDCache.reset(); |
Herb Derby | adac288 | 2020-06-23 12:45:15 -0400 | [diff] [blame] | 73 | fBlobList.reset(); |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 74 | fCurrentSize = 0; |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 75 | } |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 76 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 77 | void GrTextBlobCache::setBudget(size_t budget) { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 78 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 79 | fSizeBudget = budget; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 80 | this->internalCheckPurge(); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 83 | void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) { |
| 84 | SkASSERT(blobID != SK_InvalidGenID); |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 85 | SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID)); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 86 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 87 | |
| 88 | void GrTextBlobCache::purgeStaleBlobs() { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 89 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 90 | this->internalPurgeStaleBlobs(); |
| 91 | } |
| 92 | |
| 93 | void GrTextBlobCache::internalPurgeStaleBlobs() { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 94 | SkTArray<PurgeBlobMessage> msgs; |
| 95 | fPurgeBlobInbox.poll(&msgs); |
| 96 | |
| 97 | for (const auto& msg : msgs) { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 98 | auto* idEntry = fBlobIDCache.find(msg.fBlobID); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 99 | if (!idEntry) { |
| 100 | // no cache entries for id |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | // remove all blob entries from the LRU list |
| 105 | for (const auto& blob : idEntry->fBlobs) { |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 106 | fCurrentSize -= blob->size(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 107 | fBlobList.remove(blob.get()); |
| 108 | } |
| 109 | |
| 110 | // drop the idEntry itself (unrefs all blobs) |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 111 | fBlobIDCache.remove(msg.fBlobID); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 115 | size_t GrTextBlobCache::usedBytes() const { |
Herb Derby | d0fa3ee | 2020-06-23 12:23:51 -0400 | [diff] [blame^] | 116 | SkAutoMutexExclusive lock{fMutex}; |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 117 | return fCurrentSize; |
| 118 | } |
| 119 | |
| 120 | void GrTextBlobCache::internalCheckPurge(GrTextBlob* blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 121 | // First, purge all stale blob IDs. |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 122 | this->internalPurgeStaleBlobs(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 123 | |
| 124 | // 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] | 125 | if (fCurrentSize > fSizeBudget) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 126 | TextBlobList::Iter iter; |
| 127 | iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 128 | GrTextBlob* lruBlob = nullptr; |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 129 | while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 130 | // Backup the iterator before removing and unrefing the blob |
| 131 | iter.prev(); |
| 132 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 133 | this->internalRemove(lruBlob); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // If we break out of the loop with lruBlob == blob, then we haven't purged enough |
| 137 | // use the call back and try to free some more. If we are still overbudget after this, |
| 138 | // then this single textblob is over our budget |
| 139 | if (blob && lruBlob == blob) { |
Herb Derby | d6fe76a | 2020-05-21 12:05:04 -0400 | [diff] [blame] | 140 | fPurgeMore(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 143 | #ifdef SPEW_BUDGET_MESSAGE |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 144 | if (fCurrentSize > fSizeBudget) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 145 | SkDebugf("Single textblob is larger than our whole budget"); |
| 146 | } |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 147 | #endif |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 151 | void GrTextBlobCache::internalAdd(sk_sp<GrTextBlob> blob) { |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 152 | auto id = GrTextBlob::GetKey(*blob).fUniqueID; |
| 153 | auto* idEntry = fBlobIDCache.find(id); |
| 154 | if (!idEntry) { |
| 155 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 156 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 157 | |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 158 | // Safe to retain a raw ptr temporarily here, because the cache will hold a ref. |
| 159 | GrTextBlob* rawBlobPtr = blob.get(); |
| 160 | fBlobList.addToHead(rawBlobPtr); |
| 161 | fCurrentSize += blob->size(); |
| 162 | idEntry->addBlob(std::move(blob)); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 163 | |
Herb Derby | c5f25bc | 2020-06-23 14:02:23 -0400 | [diff] [blame] | 164 | this->internalCheckPurge(rawBlobPtr); |
Herb Derby | 7b4ea9b | 2020-05-20 17:31:36 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 168 | |
| 169 | GrTextBlobCache::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 170 | |
| 171 | uint32_t GrTextBlobCache::BlobIDCacheEntry::GetKey(const GrTextBlobCache::BlobIDCacheEntry& entry) { |
| 172 | return entry.fID; |
| 173 | } |
| 174 | |
| 175 | void GrTextBlobCache::BlobIDCacheEntry::addBlob(sk_sp<GrTextBlob> blob) { |
| 176 | SkASSERT(blob); |
| 177 | SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID); |
| 178 | SkASSERT(!this->find(GrTextBlob::GetKey(*blob))); |
| 179 | |
| 180 | fBlobs.emplace_back(std::move(blob)); |
| 181 | } |
| 182 | |
| 183 | void GrTextBlobCache::BlobIDCacheEntry::removeBlob(GrTextBlob* blob) { |
| 184 | SkASSERT(blob); |
| 185 | SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID); |
| 186 | |
| 187 | auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob)); |
| 188 | SkASSERT(index >= 0); |
| 189 | |
| 190 | fBlobs.removeShuffle(index); |
| 191 | } |
| 192 | |
| 193 | sk_sp<GrTextBlob> GrTextBlobCache::BlobIDCacheEntry::find(const GrTextBlob::Key& key) const { |
| 194 | auto index = this->findBlobIndex(key); |
| 195 | return index < 0 ? nullptr : fBlobs[index]; |
| 196 | } |
| 197 | |
| 198 | int GrTextBlobCache::BlobIDCacheEntry::findBlobIndex(const GrTextBlob::Key& key) const { |
| 199 | for (int i = 0; i < fBlobs.count(); ++i) { |
| 200 | if (GrTextBlob::GetKey(*fBlobs[i]) == key) { |
| 201 | return i; |
| 202 | } |
| 203 | } |
| 204 | return -1; |
| 205 | } |