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 | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 12 | #include "SkTArray.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 13 | #include "SkTextBlobRunIterator.h" |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 14 | #include "SkTHash.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 15 | |
| 16 | class GrTextBlobCache { |
| 17 | public: |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 18 | /** |
| 19 | * The callback function used by the cache when it is still over budget after a purge. The |
| 20 | * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
| 21 | */ |
| 22 | typedef void (*PFOverBudgetCB)(void* data); |
| 23 | |
| 24 | GrTextBlobCache(PFOverBudgetCB cb, void* data) |
| 25 | : fPool(kPreAllocSize, kMinGrowthSize) |
| 26 | , fCallback(cb) |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 27 | , fData(data) |
| 28 | , fBudget(kDefaultBudget) { |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 29 | SkASSERT(cb && data); |
| 30 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 31 | ~GrTextBlobCache(); |
| 32 | |
| 33 | // creates an uncached blob |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 34 | GrAtlasTextBlob* createBlob(int glyphCount, int runCount) { |
| 35 | return GrAtlasTextBlob::Create(&fPool, glyphCount, runCount); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 36 | } |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 37 | GrAtlasTextBlob* createBlob(const SkTextBlob* blob) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 38 | int glyphCount = 0; |
| 39 | int runCount = 0; |
| 40 | BlobGlyphCount(&glyphCount, &runCount, blob); |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 41 | GrAtlasTextBlob* cacheBlob = GrAtlasTextBlob::Create(&fPool, glyphCount, runCount); |
| 42 | return cacheBlob; |
Florin Malita | db3ceb8 | 2017-03-09 14:21:44 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 45 | GrAtlasTextBlob* createCachedBlob(const SkTextBlob* blob, |
| 46 | const GrAtlasTextBlob::Key& key, |
| 47 | const SkMaskFilter::BlurRec& blurRec, |
| 48 | const SkPaint& paint) { |
| 49 | int glyphCount = 0; |
| 50 | int runCount = 0; |
| 51 | BlobGlyphCount(&glyphCount, &runCount, blob); |
| 52 | GrAtlasTextBlob* cacheBlob = GrAtlasTextBlob::Create(&fPool, glyphCount, runCount); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 53 | cacheBlob->setupKey(key, blurRec, paint); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 54 | this->add(cacheBlob); |
| 55 | return cacheBlob; |
| 56 | } |
| 57 | |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 58 | 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 | |
| 68 | idEntry->removeBlob(blob); |
| 69 | if (idEntry->fBlobs.empty()) { |
| 70 | fBlobIDCache.remove(id); |
| 71 | } |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 72 | |
| 73 | fBlobList.remove(blob); |
| 74 | blob->unref(); |
| 75 | } |
| 76 | |
| 77 | void add(GrAtlasTextBlob* blob) { |
| 78 | auto id = GrAtlasTextBlob::GetKey(*blob).fUniqueID; |
| 79 | auto* idEntry = fBlobIDCache.find(id); |
| 80 | if (!idEntry) { |
| 81 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 82 | } |
| 83 | |
| 84 | idEntry->addBlob(blob); |
| 85 | fBlobList.addToHead(blob); |
| 86 | |
| 87 | this->checkPurge(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 88 | } |
| 89 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 90 | void makeMRU(GrAtlasTextBlob* blob) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 91 | if (fBlobList.head() == blob) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | fBlobList.remove(blob); |
| 96 | fBlobList.addToHead(blob); |
| 97 | } |
| 98 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 99 | void freeAll(); |
| 100 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 101 | // TODO move to SkTextBlob |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 102 | static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 103 | SkTextBlobRunIterator itCounter(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 104 | for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
| 105 | *glyphCount += itCounter.glyphCount(); |
| 106 | } |
| 107 | } |
| 108 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 109 | void setBudget(size_t budget) { |
| 110 | fBudget = budget; |
| 111 | this->checkPurge(); |
| 112 | } |
| 113 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 114 | private: |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 115 | using BitmapBlobList = SkTInternalLList<GrAtlasTextBlob>; |
| 116 | |
| 117 | struct BlobIDCacheEntry { |
| 118 | BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 119 | explicit BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 120 | |
| 121 | static uint32_t GetKey(const BlobIDCacheEntry& entry) { |
| 122 | return entry.fID; |
| 123 | } |
| 124 | |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 125 | void addBlob(GrAtlasTextBlob* blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 126 | SkASSERT(blob); |
| 127 | SkASSERT(GrAtlasTextBlob::GetKey(*blob).fUniqueID == fID); |
| 128 | SkASSERT(!this->find(GrAtlasTextBlob::GetKey(*blob))); |
| 129 | |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 130 | fBlobs.push_back(blob); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void removeBlob(GrAtlasTextBlob* blob) { |
| 134 | SkASSERT(blob); |
| 135 | SkASSERT(GrAtlasTextBlob::GetKey(*blob).fUniqueID == fID); |
| 136 | |
| 137 | auto index = this->findBlobIndex(GrAtlasTextBlob::GetKey(*blob)); |
| 138 | SkASSERT(index >= 0); |
| 139 | |
| 140 | fBlobs.removeShuffle(index); |
| 141 | } |
| 142 | |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 143 | GrAtlasTextBlob* find(const GrAtlasTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 144 | auto index = this->findBlobIndex(key); |
| 145 | return index < 0 ? nullptr : fBlobs[index]; |
| 146 | } |
| 147 | |
| 148 | int findBlobIndex(const GrAtlasTextBlob::Key& key) const{ |
| 149 | for (int i = 0; i < fBlobs.count(); ++i) { |
| 150 | if (GrAtlasTextBlob::GetKey(*fBlobs[i]) == key) { |
| 151 | return i; |
| 152 | } |
| 153 | } |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | uint32_t fID; |
| 158 | // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/ |
| 159 | // linear search is acceptable. If usage changes, we should re-evaluate this structure. |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 160 | SkSTArray<1, GrAtlasTextBlob*, true> fBlobs; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 161 | }; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 162 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | void checkPurge(GrAtlasTextBlob* blob = nullptr) { |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 164 | // If we are overbudget, then unref until we are below budget again |
| 165 | if (fPool.size() > fBudget) { |
| 166 | BitmapBlobList::Iter iter; |
| 167 | iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 168 | GrAtlasTextBlob* lruBlob = nullptr; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 169 | while (fPool.size() > fBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 170 | // Backup the iterator before removing and unrefing the blob |
| 171 | iter.prev(); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 172 | |
| 173 | this->remove(lruBlob); |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // If we break out of the loop with lruBlob == blob, then we haven't purged enough |
| 177 | // use the call back and try to free some more. If we are still overbudget after this, |
| 178 | // then this single textblob is over our budget |
| 179 | if (blob && lruBlob == blob) { |
| 180 | (*fCallback)(fData); |
| 181 | } |
| 182 | |
| 183 | #ifdef SPEW_BUDGET_MESSAGE |
| 184 | if (fPool.size() > fBudget) { |
| 185 | SkDebugf("Single textblob is larger than our whole budget"); |
| 186 | } |
| 187 | #endif |
| 188 | } |
| 189 | } |
| 190 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 191 | // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are |
| 192 | // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes). |
| 193 | static const int kPreAllocSize = 1 << 17; |
| 194 | static const int kMinGrowthSize = 1 << 17; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 195 | static const int kDefaultBudget = 1 << 22; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 196 | BitmapBlobList fBlobList; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 197 | SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache; |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame^] | 198 | GrMemoryPool fPool; |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 199 | PFOverBudgetCB fCallback; |
| 200 | void* fData; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 201 | size_t fBudget; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | #endif |