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 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame^] | 26 | GrTextBlobCache(PFOverBudgetCB cb, void* data, uint32_t uniqueID) |
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) |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame^] | 30 | , fBudget(kDefaultBudget) |
| 31 | , fUniqueID(uniqueID) |
| 32 | , fPurgeBlobInbox(uniqueID) { |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 33 | SkASSERT(cb && data); |
| 34 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 35 | ~GrTextBlobCache(); |
| 36 | |
| 37 | // creates an uncached blob |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 38 | sk_sp<GrAtlasTextBlob> makeBlob(int glyphCount, int runCount) { |
| 39 | return GrAtlasTextBlob::Make(&fPool, glyphCount, runCount); |
Florin Malita | db3ceb8 | 2017-03-09 14:21:44 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 42 | sk_sp<GrAtlasTextBlob> makeBlob(const SkTextBlob* blob) { |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame] | 43 | int glyphCount = 0; |
| 44 | int runCount = 0; |
| 45 | BlobGlyphCount(&glyphCount, &runCount, blob); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 46 | return GrAtlasTextBlob::Make(&fPool, glyphCount, runCount); |
| 47 | } |
| 48 | |
| 49 | sk_sp<GrAtlasTextBlob> makeCachedBlob(const SkTextBlob* blob, |
| 50 | const GrAtlasTextBlob::Key& key, |
| 51 | const SkMaskFilter::BlurRec& blurRec, |
| 52 | const SkPaint& paint) { |
| 53 | sk_sp<GrAtlasTextBlob> cacheBlob(this->makeBlob(blob)); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 54 | cacheBlob->setupKey(key, blurRec, paint); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 55 | this->add(cacheBlob); |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame^] | 56 | blob->notifyAddedToCache(fUniqueID); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 57 | return cacheBlob; |
| 58 | } |
| 59 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 60 | sk_sp<GrAtlasTextBlob> find(const GrAtlasTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 61 | const auto* idEntry = fBlobIDCache.find(key.fUniqueID); |
| 62 | return idEntry ? idEntry->find(key) : nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 63 | } |
| 64 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 65 | void remove(GrAtlasTextBlob* blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 66 | auto id = GrAtlasTextBlob::GetKey(*blob).fUniqueID; |
| 67 | auto* idEntry = fBlobIDCache.find(id); |
| 68 | SkASSERT(idEntry); |
| 69 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 70 | fBlobList.remove(blob); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 71 | idEntry->removeBlob(blob); |
| 72 | if (idEntry->fBlobs.empty()) { |
| 73 | fBlobIDCache.remove(id); |
| 74 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 77 | void makeMRU(GrAtlasTextBlob* blob) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 78 | if (fBlobList.head() == blob) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | fBlobList.remove(blob); |
| 83 | fBlobList.addToHead(blob); |
| 84 | } |
| 85 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 86 | void freeAll(); |
| 87 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 88 | // TODO move to SkTextBlob |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 89 | static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 90 | SkTextBlobRunIterator itCounter(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 91 | for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
| 92 | *glyphCount += itCounter.glyphCount(); |
| 93 | } |
| 94 | } |
| 95 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 96 | void setBudget(size_t budget) { |
| 97 | fBudget = budget; |
| 98 | this->checkPurge(); |
| 99 | } |
| 100 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 101 | struct PurgeBlobMessage { |
| 102 | uint32_t fID; |
| 103 | }; |
| 104 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame^] | 105 | static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 106 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 107 | void purgeStaleBlobs(); |
| 108 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 109 | private: |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 110 | using BitmapBlobList = SkTInternalLList<GrAtlasTextBlob>; |
| 111 | |
| 112 | struct BlobIDCacheEntry { |
| 113 | BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 114 | explicit BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 115 | |
| 116 | static uint32_t GetKey(const BlobIDCacheEntry& entry) { |
| 117 | return entry.fID; |
| 118 | } |
| 119 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 120 | void addBlob(sk_sp<GrAtlasTextBlob> blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 121 | SkASSERT(blob); |
| 122 | SkASSERT(GrAtlasTextBlob::GetKey(*blob).fUniqueID == fID); |
| 123 | SkASSERT(!this->find(GrAtlasTextBlob::GetKey(*blob))); |
| 124 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 125 | fBlobs.emplace_back(std::move(blob)); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void removeBlob(GrAtlasTextBlob* blob) { |
| 129 | SkASSERT(blob); |
| 130 | SkASSERT(GrAtlasTextBlob::GetKey(*blob).fUniqueID == fID); |
| 131 | |
| 132 | auto index = this->findBlobIndex(GrAtlasTextBlob::GetKey(*blob)); |
| 133 | SkASSERT(index >= 0); |
| 134 | |
| 135 | fBlobs.removeShuffle(index); |
| 136 | } |
| 137 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 138 | sk_sp<GrAtlasTextBlob> find(const GrAtlasTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 139 | auto index = this->findBlobIndex(key); |
| 140 | return index < 0 ? nullptr : fBlobs[index]; |
| 141 | } |
| 142 | |
| 143 | int findBlobIndex(const GrAtlasTextBlob::Key& key) const{ |
| 144 | for (int i = 0; i < fBlobs.count(); ++i) { |
| 145 | if (GrAtlasTextBlob::GetKey(*fBlobs[i]) == key) { |
| 146 | return i; |
| 147 | } |
| 148 | } |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | uint32_t fID; |
| 153 | // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/ |
| 154 | // 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] | 155 | SkSTArray<1, sk_sp<GrAtlasTextBlob>, true> fBlobs; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 156 | }; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 157 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 158 | void add(sk_sp<GrAtlasTextBlob> blob) { |
| 159 | auto id = GrAtlasTextBlob::GetKey(*blob).fUniqueID; |
| 160 | auto* idEntry = fBlobIDCache.find(id); |
| 161 | if (!idEntry) { |
| 162 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 163 | } |
| 164 | |
| 165 | // Safe to retain a raw ptr temporarily here, because the cache will hold a ref. |
| 166 | GrAtlasTextBlob* rawBlobPtr = blob.get(); |
| 167 | fBlobList.addToHead(rawBlobPtr); |
| 168 | idEntry->addBlob(std::move(blob)); |
| 169 | |
| 170 | this->checkPurge(rawBlobPtr); |
| 171 | } |
| 172 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 173 | void checkPurge(GrAtlasTextBlob* blob = nullptr); |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 174 | |
Florin Malita | 012893b | 2017-07-11 09:31:22 -0400 | [diff] [blame] | 175 | static const int kMinGrowthSize = 1 << 16; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 176 | static const int kDefaultBudget = 1 << 22; |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 177 | GrMemoryPool fPool; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 178 | BitmapBlobList fBlobList; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 179 | SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache; |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 180 | PFOverBudgetCB fCallback; |
| 181 | void* fData; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 182 | size_t fBudget; |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame^] | 183 | uint32_t fUniqueID; // unique id to use for messaging |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 184 | SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | #endif |