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 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 11 | #include "GrTextContext.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 | |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 26 | GrTextBlobCache(PFOverBudgetCB cb, void* data, uint32_t uniqueID) |
| 27 | : fCallback(cb) |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 28 | , fData(data) |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 29 | , fSizeBudget(kDefaultBudget) |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 30 | , fUniqueID(uniqueID) |
| 31 | , fPurgeBlobInbox(uniqueID) { |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 32 | SkASSERT(cb && data); |
| 33 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 34 | ~GrTextBlobCache(); |
| 35 | |
| 36 | // creates an uncached blob |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 37 | sk_sp<GrTextBlob> makeBlob(int glyphCount, int runCount) { |
| 38 | return GrTextBlob::Make(glyphCount, runCount); |
Florin Malita | db3ceb8 | 2017-03-09 14:21:44 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 41 | sk_sp<GrTextBlob> makeBlob(const SkTextBlob* blob) { |
Florin Malita | 3304c44 | 2017-03-09 22:42:58 +0000 | [diff] [blame] | 42 | int glyphCount = 0; |
| 43 | int runCount = 0; |
| 44 | BlobGlyphCount(&glyphCount, &runCount, blob); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 45 | return GrTextBlob::Make(glyphCount, runCount); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 48 | sk_sp<GrTextBlob> makeCachedBlob(const SkTextBlob* blob, |
| 49 | const GrTextBlob::Key& key, |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 50 | const SkMaskFilterBase::BlurRec& blurRec, |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 51 | const SkPaint& paint) { |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 52 | sk_sp<GrTextBlob> cacheBlob(this->makeBlob(blob)); |
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); |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 55 | blob->notifyAddedToCache(fUniqueID); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 56 | return cacheBlob; |
| 57 | } |
| 58 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 59 | sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 60 | const auto* idEntry = fBlobIDCache.find(key.fUniqueID); |
| 61 | return idEntry ? idEntry->find(key) : nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 64 | void remove(GrTextBlob* blob) { |
| 65 | auto id = GrTextBlob::GetKey(*blob).fUniqueID; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 66 | auto* idEntry = fBlobIDCache.find(id); |
| 67 | SkASSERT(idEntry); |
| 68 | |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 69 | fCurrentSize -= blob->size(); |
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 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 77 | void makeMRU(GrTextBlob* 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) { |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 97 | fSizeBudget = budget; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 98 | this->checkPurge(); |
| 99 | } |
| 100 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 101 | struct PurgeBlobMessage { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 102 | PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID) |
| 103 | : fBlobID(blobID), fContextID(contextUniqueID) {} |
| 104 | bool shouldSend(uint32_t inboxID) const { return fContextID == inboxID; } |
| 105 | |
| 106 | uint32_t fBlobID; |
| 107 | uint32_t fContextID; |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 110 | static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 111 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 112 | void purgeStaleBlobs(); |
| 113 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 114 | private: |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 115 | using BitmapBlobList = SkTInternalLList<GrTextBlob>; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 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 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 125 | void addBlob(sk_sp<GrTextBlob> blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 126 | SkASSERT(blob); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 127 | SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID); |
| 128 | SkASSERT(!this->find(GrTextBlob::GetKey(*blob))); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 129 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 130 | fBlobs.emplace_back(std::move(blob)); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 133 | void removeBlob(GrTextBlob* blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 134 | SkASSERT(blob); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 135 | SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 136 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 137 | auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob)); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 138 | SkASSERT(index >= 0); |
| 139 | |
| 140 | fBlobs.removeShuffle(index); |
| 141 | } |
| 142 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 143 | sk_sp<GrTextBlob> find(const GrTextBlob::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 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 148 | int findBlobIndex(const GrTextBlob::Key& key) const{ |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 149 | for (int i = 0; i < fBlobs.count(); ++i) { |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 150 | if (GrTextBlob::GetKey(*fBlobs[i]) == key) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 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. |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 160 | SkSTArray<1, sk_sp<GrTextBlob>, 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 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 163 | void add(sk_sp<GrTextBlob> blob) { |
| 164 | auto id = GrTextBlob::GetKey(*blob).fUniqueID; |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 165 | auto* idEntry = fBlobIDCache.find(id); |
| 166 | if (!idEntry) { |
| 167 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 168 | } |
| 169 | |
| 170 | // Safe to retain a raw ptr temporarily here, because the cache will hold a ref. |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 171 | GrTextBlob* rawBlobPtr = blob.get(); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 172 | fBlobList.addToHead(rawBlobPtr); |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 173 | fCurrentSize += blob->size(); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 174 | idEntry->addBlob(std::move(blob)); |
| 175 | |
| 176 | this->checkPurge(rawBlobPtr); |
| 177 | } |
| 178 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 179 | void checkPurge(GrTextBlob* blob = nullptr); |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 180 | |
Florin Malita | 012893b | 2017-07-11 09:31:22 -0400 | [diff] [blame] | 181 | static const int kMinGrowthSize = 1 << 16; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 182 | static const int kDefaultBudget = 1 << 22; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 183 | BitmapBlobList fBlobList; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 184 | SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache; |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 185 | PFOverBudgetCB fCallback; |
| 186 | void* fData; |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 187 | size_t fSizeBudget; |
| 188 | size_t fCurrentSize{0}; |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 189 | uint32_t fUniqueID; // unique id to use for messaging |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 190 | SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | #endif |