blob: 49f24306801fb379ab918291ff0e6534f668d124 [file] [log] [blame]
joshualittb7133be2015-04-08 09:08:31 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTArray.h"
13#include "include/private/SkTHash.h"
Ben Wagner21bca282019-05-15 10:15:52 -040014#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkTextBlobPriv.h"
16#include "src/gpu/text/GrTextBlob.h"
joshualittb7133be2015-04-08 09:08:31 -070017
18class GrTextBlobCache {
19public:
joshualitt0db6dfa2015-04-10 07:01:30 -070020 /**
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 Derbyb12175f2018-05-23 16:38:09 -040026 GrTextBlobCache(PFOverBudgetCB cb, void* data, uint32_t uniqueID)
27 : fCallback(cb)
joshualitt17d833b2015-08-03 10:17:44 -070028 , fData(data)
Herb Derbyb12175f2018-05-23 16:38:09 -040029 , fSizeBudget(kDefaultBudget)
Jim Van Verth474d6872017-12-14 13:00:05 -050030 , fUniqueID(uniqueID)
31 , fPurgeBlobInbox(uniqueID) {
joshualitt0db6dfa2015-04-10 07:01:30 -070032 SkASSERT(cb && data);
33 }
joshualittb7133be2015-04-08 09:08:31 -070034 ~GrTextBlobCache();
35
Herb Derbya00da612019-03-04 17:10:01 -050036 sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList,
Herb Derby1b8dcd12019-11-15 15:21:15 -050037 bool forceW,
Herb Derbya00da612019-03-04 17:10:01 -050038 GrColor color,
39 GrStrikeCache* strikeCache) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050040 return GrTextBlob::Make(glyphRunList.totalGlyphCount(), forceW, color, strikeCache);
Herb Derbycddab252018-07-16 11:19:04 -040041 }
42
Herb Derbyb935cf82018-07-26 16:54:18 -040043 sk_sp<GrTextBlob> makeCachedBlob(const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -040044 const GrTextBlob::Key& key,
45 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5424a5e2018-11-14 12:04:38 -050046 const SkPaint& paint,
Herb Derby1b8dcd12019-11-15 15:21:15 -050047 bool forceW,
Herb Derbya00da612019-03-04 17:10:01 -050048 GrColor color,
49 GrStrikeCache* strikeCache) {
Herb Derby1b8dcd12019-11-15 15:21:15 -050050 sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList, forceW, color, strikeCache));
Herb Derbycddab252018-07-16 11:19:04 -040051 cacheBlob->setupKey(key, blurRec, paint);
52 this->add(cacheBlob);
Herb Derbyb935cf82018-07-26 16:54:18 -040053 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID);
Herb Derbycddab252018-07-16 11:19:04 -040054 return cacheBlob;
55 }
56
Herb Derby86240592018-05-24 16:12:31 -040057 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -050058 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
59 return idEntry ? idEntry->find(key) : nullptr;
joshualittb7133be2015-04-08 09:08:31 -070060 }
61
Herb Derby86240592018-05-24 16:12:31 -040062 void remove(GrTextBlob* blob) {
63 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malita33fdb8d2017-03-07 16:51:57 -050064 auto* idEntry = fBlobIDCache.find(id);
65 SkASSERT(idEntry);
66
Herb Derbyb12175f2018-05-23 16:38:09 -040067 fCurrentSize -= blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +000068 fBlobList.remove(blob);
Florin Malita33fdb8d2017-03-07 16:51:57 -050069 idEntry->removeBlob(blob);
70 if (idEntry->fBlobs.empty()) {
71 fBlobIDCache.remove(id);
72 }
joshualittb7133be2015-04-08 09:08:31 -070073 }
74
Herb Derby86240592018-05-24 16:12:31 -040075 void makeMRU(GrTextBlob* blob) {
joshualittb7133be2015-04-08 09:08:31 -070076 if (fBlobList.head() == blob) {
77 return;
78 }
79
80 fBlobList.remove(blob);
81 fBlobList.addToHead(blob);
82 }
83
joshualitt26ffc002015-04-16 11:24:04 -070084 void freeAll();
85
joshualittb7133be2015-04-08 09:08:31 -070086 // TODO move to SkTextBlob
joshualitt259fbf12015-07-21 11:39:34 -070087 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070088 SkTextBlobRunIterator itCounter(blob);
joshualittb7133be2015-04-08 09:08:31 -070089 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
90 *glyphCount += itCounter.glyphCount();
91 }
92 }
93
joshualitt17d833b2015-08-03 10:17:44 -070094 void setBudget(size_t budget) {
Herb Derbyb12175f2018-05-23 16:38:09 -040095 fSizeBudget = budget;
joshualitt17d833b2015-08-03 10:17:44 -070096 this->checkPurge();
97 }
98
Florin Malita4a01ac92017-03-13 16:45:28 -040099 struct PurgeBlobMessage {
Brian Salomon238069b2018-07-11 15:58:57 -0400100 PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID)
101 : fBlobID(blobID), fContextID(contextUniqueID) {}
Brian Salomon238069b2018-07-11 15:58:57 -0400102
103 uint32_t fBlobID;
104 uint32_t fContextID;
Florin Malita4a01ac92017-03-13 16:45:28 -0400105 };
106
Jim Van Verth474d6872017-12-14 13:00:05 -0500107 static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -0400108
Jim Van Verth76d917c2017-12-13 09:26:37 -0500109 void purgeStaleBlobs();
110
Khushal71652e22018-10-29 13:05:36 -0700111 size_t usedBytes() const { return fCurrentSize; }
112
joshualitt259fbf12015-07-21 11:39:34 -0700113private:
Herb Derby86240592018-05-24 16:12:31 -0400114 using BitmapBlobList = SkTInternalLList<GrTextBlob>;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500115
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 Derby86240592018-05-24 16:12:31 -0400124 void addBlob(sk_sp<GrTextBlob> blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500125 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400126 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
127 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500128
Florin Malitac337c9e2017-03-10 18:02:29 +0000129 fBlobs.emplace_back(std::move(blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500130 }
131
Herb Derby86240592018-05-24 16:12:31 -0400132 void removeBlob(GrTextBlob* blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500133 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400134 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
Florin Malita33fdb8d2017-03-07 16:51:57 -0500135
Herb Derby86240592018-05-24 16:12:31 -0400136 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500137 SkASSERT(index >= 0);
138
139 fBlobs.removeShuffle(index);
140 }
141
Herb Derby86240592018-05-24 16:12:31 -0400142 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500143 auto index = this->findBlobIndex(key);
144 return index < 0 ? nullptr : fBlobs[index];
145 }
146
Herb Derby86240592018-05-24 16:12:31 -0400147 int findBlobIndex(const GrTextBlob::Key& key) const{
Florin Malita33fdb8d2017-03-07 16:51:57 -0500148 for (int i = 0; i < fBlobs.count(); ++i) {
Herb Derby86240592018-05-24 16:12:31 -0400149 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500150 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 Derby86240592018-05-24 16:12:31 -0400159 SkSTArray<1, sk_sp<GrTextBlob>, true> fBlobs;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500160 };
joshualittb7133be2015-04-08 09:08:31 -0700161
Herb Derby86240592018-05-24 16:12:31 -0400162 void add(sk_sp<GrTextBlob> blob) {
163 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malitac337c9e2017-03-10 18:02:29 +0000164 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 Derby86240592018-05-24 16:12:31 -0400170 GrTextBlob* rawBlobPtr = blob.get();
Florin Malitac337c9e2017-03-10 18:02:29 +0000171 fBlobList.addToHead(rawBlobPtr);
Herb Derbyb12175f2018-05-23 16:38:09 -0400172 fCurrentSize += blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +0000173 idEntry->addBlob(std::move(blob));
174
175 this->checkPurge(rawBlobPtr);
176 }
177
Herb Derby86240592018-05-24 16:12:31 -0400178 void checkPurge(GrTextBlob* blob = nullptr);
joshualitt17d833b2015-08-03 10:17:44 -0700179
Florin Malita012893b2017-07-11 09:31:22 -0400180 static const int kMinGrowthSize = 1 << 16;
joshualitt17d833b2015-08-03 10:17:44 -0700181 static const int kDefaultBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700182 BitmapBlobList fBlobList;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500183 SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache;
joshualitt0db6dfa2015-04-10 07:01:30 -0700184 PFOverBudgetCB fCallback;
185 void* fData;
Herb Derbyb12175f2018-05-23 16:38:09 -0400186 size_t fSizeBudget;
187 size_t fCurrentSize{0};
Jim Van Verth474d6872017-12-14 13:00:05 -0500188 uint32_t fUniqueID; // unique id to use for messaging
Florin Malita4a01ac92017-03-13 16:45:28 -0400189 SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox;
joshualittb7133be2015-04-08 09:08:31 -0700190};
191
192#endif