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 | |
| 8 | #ifndef GrTextBlobCache_DEFINED |
| 9 | #define GrTextBlobCache_DEFINED |
| 10 | |
| 11 | #include "GrAtlasTextContext.h" |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 12 | #include "SkMessageBus.h" |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 14 | #include "SkTArray.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 15 | #include "SkTextBlobRunIterator.h" |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 16 | #include "SkTHash.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 17 | |
| 18 | class GrTextBlobCache { |
| 19 | public: |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 20 | /** |
| 21 | * The callback function used by the cache when it is still over budget after a purge. The |
| 22 | * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
| 23 | */ |
| 24 | typedef void (*PFOverBudgetCB)(void* data); |
| 25 | |
| 26 | GrTextBlobCache(PFOverBudgetCB cb, void* data) |
Florin Malita | 012893b | 2017-07-11 09:31:22 -0400 | [diff] [blame^] | 27 | : fPool(0u, kMinGrowthSize) |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 28 | , fCallback(cb) |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 29 | , fData(data) |
| 30 | , fBudget(kDefaultBudget) { |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 31 | SkASSERT(cb && data); |
| 32 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 33 | ~GrTextBlobCache(); |
| 34 | |
| 35 | // creates an uncached blob |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 36 | sk_sp<GrAtlasTextBlob> makeBlob(int glyphCount, int runCount) { |
| 37 | return GrAtlasTextBlob::Make(&fPool, glyphCount, runCount); |
Florin Malita | db3ceb8 | 2017-03-09 14:21:44 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 40 | sk_sp<GrAtlasTextBlob> makeBlob(const SkTextBlob* blob) { |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame] | 41 | int glyphCount = 0; |
| 42 | int runCount = 0; |
| 43 | BlobGlyphCount(&glyphCount, &runCount, blob); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 44 | return GrAtlasTextBlob::Make(&fPool, glyphCount, runCount); |
| 45 | } |
| 46 | |
| 47 | sk_sp<GrAtlasTextBlob> makeCachedBlob(const SkTextBlob* blob, |
| 48 | const GrAtlasTextBlob::Key& key, |
| 49 | const SkMaskFilter::BlurRec& blurRec, |
| 50 | const SkPaint& paint) { |
| 51 | sk_sp<GrAtlasTextBlob> cacheBlob(this->makeBlob(blob)); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 52 | cacheBlob->setupKey(key, blurRec, paint); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 53 | this->add(cacheBlob); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 54 | blob->notifyAddedToCache(); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 55 | return cacheBlob; |
| 56 | } |
| 57 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 58 | sk_sp<GrAtlasTextBlob> find(const GrAtlasTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 59 | const auto* idEntry = fBlobIDCache.find(key.fUniqueID); |
| 60 | return idEntry ? idEntry->find(key) : nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 61 | } |
| 62 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 63 | void remove(GrAtlasTextBlob* blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 64 | auto id = GrAtlasTextBlob::GetKey(*blob).fUniqueID; |
| 65 | auto* idEntry = fBlobIDCache.find(id); |
| 66 | SkASSERT(idEntry); |
| 67 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 68 | fBlobList.remove(blob); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 69 | idEntry->removeBlob(blob); |
| 70 | if (idEntry->fBlobs.empty()) { |
| 71 | fBlobIDCache.remove(id); |
| 72 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 73 | } |
| 74 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 75 | void makeMRU(GrAtlasTextBlob* blob) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 76 | if (fBlobList.head() == blob) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | fBlobList.remove(blob); |
| 81 | fBlobList.addToHead(blob); |
| 82 | } |
| 83 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 84 | void freeAll(); |
| 85 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 86 | // TODO move to SkTextBlob |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 87 | static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 88 | SkTextBlobRunIterator itCounter(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 89 | for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
| 90 | *glyphCount += itCounter.glyphCount(); |
| 91 | } |
| 92 | } |
| 93 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 94 | void setBudget(size_t budget) { |
| 95 | fBudget = budget; |
| 96 | this->checkPurge(); |
| 97 | } |
| 98 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 99 | struct PurgeBlobMessage { |
| 100 | uint32_t fID; |
| 101 | }; |
| 102 | |
| 103 | static void PostPurgeBlobMessage(uint32_t); |
| 104 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 105 | private: |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 106 | using BitmapBlobList = SkTInternalLList<GrAtlasTextBlob>; |
| 107 | |
| 108 | struct BlobIDCacheEntry { |
| 109 | BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 110 | explicit BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 111 | |
| 112 | static uint32_t GetKey(const BlobIDCacheEntry& entry) { |
| 113 | return entry.fID; |
| 114 | } |
| 115 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 116 | void addBlob(sk_sp<GrAtlasTextBlob> blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 117 | SkASSERT(blob); |
| 118 | SkASSERT(GrAtlasTextBlob::GetKey(*blob).fUniqueID == fID); |
| 119 | SkASSERT(!this->find(GrAtlasTextBlob::GetKey(*blob))); |
| 120 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 121 | fBlobs.emplace_back(std::move(blob)); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void removeBlob(GrAtlasTextBlob* blob) { |
| 125 | SkASSERT(blob); |
| 126 | SkASSERT(GrAtlasTextBlob::GetKey(*blob).fUniqueID == fID); |
| 127 | |
| 128 | auto index = this->findBlobIndex(GrAtlasTextBlob::GetKey(*blob)); |
| 129 | SkASSERT(index >= 0); |
| 130 | |
| 131 | fBlobs.removeShuffle(index); |
| 132 | } |
| 133 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 134 | sk_sp<GrAtlasTextBlob> find(const GrAtlasTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 135 | auto index = this->findBlobIndex(key); |
| 136 | return index < 0 ? nullptr : fBlobs[index]; |
| 137 | } |
| 138 | |
| 139 | int findBlobIndex(const GrAtlasTextBlob::Key& key) const{ |
| 140 | for (int i = 0; i < fBlobs.count(); ++i) { |
| 141 | if (GrAtlasTextBlob::GetKey(*fBlobs[i]) == key) { |
| 142 | return i; |
| 143 | } |
| 144 | } |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | uint32_t fID; |
| 149 | // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/ |
| 150 | // linear search is acceptable. If usage changes, we should re-evaluate this structure. |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 151 | SkSTArray<1, sk_sp<GrAtlasTextBlob>, true> fBlobs; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 152 | }; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 153 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 154 | void add(sk_sp<GrAtlasTextBlob> blob) { |
| 155 | auto id = GrAtlasTextBlob::GetKey(*blob).fUniqueID; |
| 156 | auto* idEntry = fBlobIDCache.find(id); |
| 157 | if (!idEntry) { |
| 158 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 159 | } |
| 160 | |
| 161 | // Safe to retain a raw ptr temporarily here, because the cache will hold a ref. |
| 162 | GrAtlasTextBlob* rawBlobPtr = blob.get(); |
| 163 | fBlobList.addToHead(rawBlobPtr); |
| 164 | idEntry->addBlob(std::move(blob)); |
| 165 | |
| 166 | this->checkPurge(rawBlobPtr); |
| 167 | } |
| 168 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 169 | void checkPurge(GrAtlasTextBlob* blob = nullptr) { |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 170 | // First, purge all stale blob IDs. |
| 171 | { |
Florin Malita | c500d9b | 2017-03-14 10:08:33 -0400 | [diff] [blame] | 172 | SkTArray<PurgeBlobMessage> msgs; |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 173 | fPurgeBlobInbox.poll(&msgs); |
| 174 | |
| 175 | for (const auto& msg : msgs) { |
| 176 | auto* idEntry = fBlobIDCache.find(msg.fID); |
| 177 | if (!idEntry) { |
| 178 | // no cache entries for id |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | // remove all blob entries from the LRU list |
| 183 | for (const auto& blob : idEntry->fBlobs) { |
| 184 | fBlobList.remove(blob.get()); |
| 185 | } |
| 186 | |
| 187 | // drop the idEntry itself (unrefs all blobs) |
| 188 | fBlobIDCache.remove(msg.fID); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // If we are still overbudget, then unref until we are below budget again |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 193 | if (fPool.size() > fBudget) { |
| 194 | BitmapBlobList::Iter iter; |
| 195 | iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | GrAtlasTextBlob* lruBlob = nullptr; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 197 | while (fPool.size() > fBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 198 | // Backup the iterator before removing and unrefing the blob |
| 199 | iter.prev(); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 200 | |
| 201 | this->remove(lruBlob); |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // If we break out of the loop with lruBlob == blob, then we haven't purged enough |
| 205 | // use the call back and try to free some more. If we are still overbudget after this, |
| 206 | // then this single textblob is over our budget |
| 207 | if (blob && lruBlob == blob) { |
| 208 | (*fCallback)(fData); |
| 209 | } |
| 210 | |
| 211 | #ifdef SPEW_BUDGET_MESSAGE |
| 212 | if (fPool.size() > fBudget) { |
| 213 | SkDebugf("Single textblob is larger than our whole budget"); |
| 214 | } |
| 215 | #endif |
| 216 | } |
| 217 | } |
| 218 | |
Florin Malita | 012893b | 2017-07-11 09:31:22 -0400 | [diff] [blame^] | 219 | static const int kMinGrowthSize = 1 << 16; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 220 | static const int kDefaultBudget = 1 << 22; |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 221 | GrMemoryPool fPool; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 222 | BitmapBlobList fBlobList; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 223 | SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache; |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 224 | PFOverBudgetCB fCallback; |
| 225 | void* fData; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 226 | size_t fBudget; |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 227 | SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | #endif |