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