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