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" |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 15 | #include "SkTextBlobPriv.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 | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 59 | sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList) { |
| 60 | return GrTextBlob::Make(glyphRunList.totalGlyphCount(), glyphRunList.size()); |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 63 | sk_sp<GrTextBlob> makeCachedBlob(const SkGlyphRunList& glyphRunList, |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 64 | const GrTextBlob::Key& key, |
| 65 | const SkMaskFilterBase::BlurRec& blurRec, |
| 66 | const SkPaint& paint) { |
| 67 | sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList)); |
| 68 | cacheBlob->setupKey(key, blurRec, paint); |
| 69 | this->add(cacheBlob); |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 70 | glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID); |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 71 | return cacheBlob; |
| 72 | } |
| 73 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 74 | sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 75 | const auto* idEntry = fBlobIDCache.find(key.fUniqueID); |
| 76 | return idEntry ? idEntry->find(key) : nullptr; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 79 | void remove(GrTextBlob* blob) { |
| 80 | auto id = GrTextBlob::GetKey(*blob).fUniqueID; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 81 | auto* idEntry = fBlobIDCache.find(id); |
| 82 | SkASSERT(idEntry); |
| 83 | |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 84 | fCurrentSize -= blob->size(); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 85 | fBlobList.remove(blob); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 86 | idEntry->removeBlob(blob); |
| 87 | if (idEntry->fBlobs.empty()) { |
| 88 | fBlobIDCache.remove(id); |
| 89 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 92 | void makeMRU(GrTextBlob* blob) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 93 | if (fBlobList.head() == blob) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | fBlobList.remove(blob); |
| 98 | fBlobList.addToHead(blob); |
| 99 | } |
| 100 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 101 | void freeAll(); |
| 102 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 103 | // TODO move to SkTextBlob |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 104 | static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 105 | SkTextBlobRunIterator itCounter(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 106 | for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
| 107 | *glyphCount += itCounter.glyphCount(); |
| 108 | } |
| 109 | } |
| 110 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 111 | void setBudget(size_t budget) { |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 112 | fSizeBudget = budget; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 113 | this->checkPurge(); |
| 114 | } |
| 115 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 116 | struct PurgeBlobMessage { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 117 | PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID) |
| 118 | : fBlobID(blobID), fContextID(contextUniqueID) {} |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 119 | |
| 120 | uint32_t fBlobID; |
| 121 | uint32_t fContextID; |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 124 | static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 125 | |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 126 | void purgeStaleBlobs(); |
| 127 | |
Khushal | 71652e2 | 2018-10-29 13:05:36 -0700 | [diff] [blame^] | 128 | size_t usedBytes() const { return fCurrentSize; } |
| 129 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 130 | private: |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 131 | using BitmapBlobList = SkTInternalLList<GrTextBlob>; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 132 | |
| 133 | struct BlobIDCacheEntry { |
| 134 | BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 135 | explicit BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 136 | |
| 137 | static uint32_t GetKey(const BlobIDCacheEntry& entry) { |
| 138 | return entry.fID; |
| 139 | } |
| 140 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 141 | void addBlob(sk_sp<GrTextBlob> blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 142 | SkASSERT(blob); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 143 | SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID); |
| 144 | SkASSERT(!this->find(GrTextBlob::GetKey(*blob))); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 145 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 146 | fBlobs.emplace_back(std::move(blob)); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 149 | void removeBlob(GrTextBlob* blob) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 150 | SkASSERT(blob); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 151 | SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 152 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 153 | auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob)); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 154 | SkASSERT(index >= 0); |
| 155 | |
| 156 | fBlobs.removeShuffle(index); |
| 157 | } |
| 158 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 159 | sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 160 | auto index = this->findBlobIndex(key); |
| 161 | return index < 0 ? nullptr : fBlobs[index]; |
| 162 | } |
| 163 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 164 | int findBlobIndex(const GrTextBlob::Key& key) const{ |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 165 | for (int i = 0; i < fBlobs.count(); ++i) { |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 166 | if (GrTextBlob::GetKey(*fBlobs[i]) == key) { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 167 | return i; |
| 168 | } |
| 169 | } |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | uint32_t fID; |
| 174 | // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/ |
| 175 | // 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] | 176 | SkSTArray<1, sk_sp<GrTextBlob>, true> fBlobs; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 177 | }; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 178 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 179 | void add(sk_sp<GrTextBlob> blob) { |
| 180 | auto id = GrTextBlob::GetKey(*blob).fUniqueID; |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 181 | auto* idEntry = fBlobIDCache.find(id); |
| 182 | if (!idEntry) { |
| 183 | idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id)); |
| 184 | } |
| 185 | |
| 186 | // 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] | 187 | GrTextBlob* rawBlobPtr = blob.get(); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 188 | fBlobList.addToHead(rawBlobPtr); |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 189 | fCurrentSize += blob->size(); |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 190 | idEntry->addBlob(std::move(blob)); |
| 191 | |
| 192 | this->checkPurge(rawBlobPtr); |
| 193 | } |
| 194 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 195 | void checkPurge(GrTextBlob* blob = nullptr); |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 196 | |
Florin Malita | 012893b | 2017-07-11 09:31:22 -0400 | [diff] [blame] | 197 | static const int kMinGrowthSize = 1 << 16; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 198 | static const int kDefaultBudget = 1 << 22; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 199 | BitmapBlobList fBlobList; |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 200 | SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache; |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 201 | PFOverBudgetCB fCallback; |
| 202 | void* fData; |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame] | 203 | size_t fSizeBudget; |
| 204 | size_t fCurrentSize{0}; |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 205 | uint32_t fUniqueID; // unique id to use for messaging |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 206 | SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | #endif |