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