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