blob: 03d48ad295cf26cc5f43475d23f73ba2cc304f18 [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
Herb Derbydc214c22018-11-08 13:31:39 -050011#include "GrTextBlob.h"
Florin Malita4a01ac92017-03-13 16:45:28 -040012#include "SkMessageBus.h"
Florin Malitac337c9e2017-03-10 18:02:29 +000013#include "SkRefCnt.h"
Florin Malita33fdb8d2017-03-07 16:51:57 -050014#include "SkTArray.h"
Florin Malitaab54e732018-07-27 09:47:15 -040015#include "SkTextBlobPriv.h"
Florin Malita33fdb8d2017-03-07 16:51:57 -050016#include "SkTHash.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,
37 GrColor color,
38 GrStrikeCache* strikeCache) {
39 return GrTextBlob::Make(
40 glyphRunList.totalGlyphCount(), glyphRunList.size(), 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 Derbya00da612019-03-04 17:10:01 -050047 GrColor color,
48 GrStrikeCache* strikeCache) {
49 sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList, color, strikeCache));
Herb Derbycddab252018-07-16 11:19:04 -040050 cacheBlob->setupKey(key, blurRec, paint);
51 this->add(cacheBlob);
Herb Derbyb935cf82018-07-26 16:54:18 -040052 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID);
Herb Derbycddab252018-07-16 11:19:04 -040053 return cacheBlob;
54 }
55
Herb Derby86240592018-05-24 16:12:31 -040056 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -050057 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
58 return idEntry ? idEntry->find(key) : nullptr;
joshualittb7133be2015-04-08 09:08:31 -070059 }
60
Herb Derby86240592018-05-24 16:12:31 -040061 void remove(GrTextBlob* blob) {
62 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malita33fdb8d2017-03-07 16:51:57 -050063 auto* idEntry = fBlobIDCache.find(id);
64 SkASSERT(idEntry);
65
Herb Derbyb12175f2018-05-23 16:38:09 -040066 fCurrentSize -= blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +000067 fBlobList.remove(blob);
Florin Malita33fdb8d2017-03-07 16:51:57 -050068 idEntry->removeBlob(blob);
69 if (idEntry->fBlobs.empty()) {
70 fBlobIDCache.remove(id);
71 }
joshualittb7133be2015-04-08 09:08:31 -070072 }
73
Herb Derby86240592018-05-24 16:12:31 -040074 void makeMRU(GrTextBlob* blob) {
joshualittb7133be2015-04-08 09:08:31 -070075 if (fBlobList.head() == blob) {
76 return;
77 }
78
79 fBlobList.remove(blob);
80 fBlobList.addToHead(blob);
81 }
82
joshualitt26ffc002015-04-16 11:24:04 -070083 void freeAll();
84
joshualittb7133be2015-04-08 09:08:31 -070085 // TODO move to SkTextBlob
joshualitt259fbf12015-07-21 11:39:34 -070086 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070087 SkTextBlobRunIterator itCounter(blob);
joshualittb7133be2015-04-08 09:08:31 -070088 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
89 *glyphCount += itCounter.glyphCount();
90 }
91 }
92
joshualitt17d833b2015-08-03 10:17:44 -070093 void setBudget(size_t budget) {
Herb Derbyb12175f2018-05-23 16:38:09 -040094 fSizeBudget = budget;
joshualitt17d833b2015-08-03 10:17:44 -070095 this->checkPurge();
96 }
97
Florin Malita4a01ac92017-03-13 16:45:28 -040098 struct PurgeBlobMessage {
Brian Salomon238069b2018-07-11 15:58:57 -040099 PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID)
100 : fBlobID(blobID), fContextID(contextUniqueID) {}
Brian Salomon238069b2018-07-11 15:58:57 -0400101
102 uint32_t fBlobID;
103 uint32_t fContextID;
Florin Malita4a01ac92017-03-13 16:45:28 -0400104 };
105
Jim Van Verth474d6872017-12-14 13:00:05 -0500106 static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -0400107
Jim Van Verth76d917c2017-12-13 09:26:37 -0500108 void purgeStaleBlobs();
109
Khushal71652e22018-10-29 13:05:36 -0700110 size_t usedBytes() const { return fCurrentSize; }
111
joshualitt259fbf12015-07-21 11:39:34 -0700112private:
Herb Derby86240592018-05-24 16:12:31 -0400113 using BitmapBlobList = SkTInternalLList<GrTextBlob>;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500114
115 struct BlobIDCacheEntry {
116 BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
117 explicit BlobIDCacheEntry(uint32_t id) : fID(id) {}
118
119 static uint32_t GetKey(const BlobIDCacheEntry& entry) {
120 return entry.fID;
121 }
122
Herb Derby86240592018-05-24 16:12:31 -0400123 void addBlob(sk_sp<GrTextBlob> blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500124 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400125 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
126 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500127
Florin Malitac337c9e2017-03-10 18:02:29 +0000128 fBlobs.emplace_back(std::move(blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500129 }
130
Herb Derby86240592018-05-24 16:12:31 -0400131 void removeBlob(GrTextBlob* blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500132 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400133 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
Florin Malita33fdb8d2017-03-07 16:51:57 -0500134
Herb Derby86240592018-05-24 16:12:31 -0400135 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500136 SkASSERT(index >= 0);
137
138 fBlobs.removeShuffle(index);
139 }
140
Herb Derby86240592018-05-24 16:12:31 -0400141 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500142 auto index = this->findBlobIndex(key);
143 return index < 0 ? nullptr : fBlobs[index];
144 }
145
Herb Derby86240592018-05-24 16:12:31 -0400146 int findBlobIndex(const GrTextBlob::Key& key) const{
Florin Malita33fdb8d2017-03-07 16:51:57 -0500147 for (int i = 0; i < fBlobs.count(); ++i) {
Herb Derby86240592018-05-24 16:12:31 -0400148 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500149 return i;
150 }
151 }
152 return -1;
153 }
154
155 uint32_t fID;
156 // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/
157 // linear search is acceptable. If usage changes, we should re-evaluate this structure.
Herb Derby86240592018-05-24 16:12:31 -0400158 SkSTArray<1, sk_sp<GrTextBlob>, true> fBlobs;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500159 };
joshualittb7133be2015-04-08 09:08:31 -0700160
Herb Derby86240592018-05-24 16:12:31 -0400161 void add(sk_sp<GrTextBlob> blob) {
162 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malitac337c9e2017-03-10 18:02:29 +0000163 auto* idEntry = fBlobIDCache.find(id);
164 if (!idEntry) {
165 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
166 }
167
168 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
Herb Derby86240592018-05-24 16:12:31 -0400169 GrTextBlob* rawBlobPtr = blob.get();
Florin Malitac337c9e2017-03-10 18:02:29 +0000170 fBlobList.addToHead(rawBlobPtr);
Herb Derbyb12175f2018-05-23 16:38:09 -0400171 fCurrentSize += blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +0000172 idEntry->addBlob(std::move(blob));
173
174 this->checkPurge(rawBlobPtr);
175 }
176
Herb Derby86240592018-05-24 16:12:31 -0400177 void checkPurge(GrTextBlob* blob = nullptr);
joshualitt17d833b2015-08-03 10:17:44 -0700178
Florin Malita012893b2017-07-11 09:31:22 -0400179 static const int kMinGrowthSize = 1 << 16;
joshualitt17d833b2015-08-03 10:17:44 -0700180 static const int kDefaultBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700181 BitmapBlobList fBlobList;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500182 SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache;
joshualitt0db6dfa2015-04-10 07:01:30 -0700183 PFOverBudgetCB fCallback;
184 void* fData;
Herb Derbyb12175f2018-05-23 16:38:09 -0400185 size_t fSizeBudget;
186 size_t fCurrentSize{0};
Jim Van Verth474d6872017-12-14 13:00:05 -0500187 uint32_t fUniqueID; // unique id to use for messaging
Florin Malita4a01ac92017-03-13 16:45:28 -0400188 SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox;
joshualittb7133be2015-04-08 09:08:31 -0700189};
190
191#endif