blob: a174c9cf908f59ad52f9e32475095b0b0a24bfc2 [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 Derby26cbe512018-05-24 14:39:01 -040011#include "GrTextContext.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"
halcanary33779752015-10-27 14:01:05 -070015#include "SkTextBlobRunIterator.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
36 // creates an uncached blob
Herb Derby86240592018-05-24 16:12:31 -040037 sk_sp<GrTextBlob> makeBlob(int glyphCount, int runCount) {
38 return GrTextBlob::Make(glyphCount, runCount);
Florin Malitadb3ceb82017-03-09 14:21:44 -050039 }
40
Herb Derby86240592018-05-24 16:12:31 -040041 sk_sp<GrTextBlob> makeBlob(const SkTextBlob* blob) {
Florin Malita3304c442017-03-09 22:42:58 +000042 int glyphCount = 0;
43 int runCount = 0;
44 BlobGlyphCount(&glyphCount, &runCount, blob);
Herb Derby86240592018-05-24 16:12:31 -040045 return GrTextBlob::Make(glyphCount, runCount);
Florin Malitac337c9e2017-03-10 18:02:29 +000046 }
47
Herb Derby86240592018-05-24 16:12:31 -040048 sk_sp<GrTextBlob> makeCachedBlob(const SkTextBlob* blob,
49 const GrTextBlob::Key& key,
Mike Reed80747ef2018-01-23 15:29:32 -050050 const SkMaskFilterBase::BlurRec& blurRec,
Florin Malitac337c9e2017-03-10 18:02:29 +000051 const SkPaint& paint) {
Herb Derby86240592018-05-24 16:12:31 -040052 sk_sp<GrTextBlob> cacheBlob(this->makeBlob(blob));
joshualitt92303772016-02-10 11:55:52 -080053 cacheBlob->setupKey(key, blurRec, paint);
joshualittb7133be2015-04-08 09:08:31 -070054 this->add(cacheBlob);
Jim Van Verth474d6872017-12-14 13:00:05 -050055 blob->notifyAddedToCache(fUniqueID);
joshualittb7133be2015-04-08 09:08:31 -070056 return cacheBlob;
57 }
58
Herb Derby86240592018-05-24 16:12:31 -040059 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -050060 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
61 return idEntry ? idEntry->find(key) : nullptr;
joshualittb7133be2015-04-08 09:08:31 -070062 }
63
Herb Derby86240592018-05-24 16:12:31 -040064 void remove(GrTextBlob* blob) {
65 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malita33fdb8d2017-03-07 16:51:57 -050066 auto* idEntry = fBlobIDCache.find(id);
67 SkASSERT(idEntry);
68
Herb Derbyb12175f2018-05-23 16:38:09 -040069 fCurrentSize -= blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +000070 fBlobList.remove(blob);
Florin Malita33fdb8d2017-03-07 16:51:57 -050071 idEntry->removeBlob(blob);
72 if (idEntry->fBlobs.empty()) {
73 fBlobIDCache.remove(id);
74 }
joshualittb7133be2015-04-08 09:08:31 -070075 }
76
Herb Derby86240592018-05-24 16:12:31 -040077 void makeMRU(GrTextBlob* blob) {
joshualittb7133be2015-04-08 09:08:31 -070078 if (fBlobList.head() == blob) {
79 return;
80 }
81
82 fBlobList.remove(blob);
83 fBlobList.addToHead(blob);
84 }
85
joshualitt26ffc002015-04-16 11:24:04 -070086 void freeAll();
87
joshualittb7133be2015-04-08 09:08:31 -070088 // TODO move to SkTextBlob
joshualitt259fbf12015-07-21 11:39:34 -070089 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070090 SkTextBlobRunIterator itCounter(blob);
joshualittb7133be2015-04-08 09:08:31 -070091 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
92 *glyphCount += itCounter.glyphCount();
93 }
94 }
95
joshualitt17d833b2015-08-03 10:17:44 -070096 void setBudget(size_t budget) {
Herb Derbyb12175f2018-05-23 16:38:09 -040097 fSizeBudget = budget;
joshualitt17d833b2015-08-03 10:17:44 -070098 this->checkPurge();
99 }
100
Florin Malita4a01ac92017-03-13 16:45:28 -0400101 struct PurgeBlobMessage {
Brian Salomon238069b2018-07-11 15:58:57 -0400102 PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID)
103 : fBlobID(blobID), fContextID(contextUniqueID) {}
104 bool shouldSend(uint32_t inboxID) const { return fContextID == inboxID; }
105
106 uint32_t fBlobID;
107 uint32_t fContextID;
Florin Malita4a01ac92017-03-13 16:45:28 -0400108 };
109
Jim Van Verth474d6872017-12-14 13:00:05 -0500110 static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -0400111
Jim Van Verth76d917c2017-12-13 09:26:37 -0500112 void purgeStaleBlobs();
113
joshualitt259fbf12015-07-21 11:39:34 -0700114private:
Herb Derby86240592018-05-24 16:12:31 -0400115 using BitmapBlobList = SkTInternalLList<GrTextBlob>;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500116
117 struct BlobIDCacheEntry {
118 BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
119 explicit BlobIDCacheEntry(uint32_t id) : fID(id) {}
120
121 static uint32_t GetKey(const BlobIDCacheEntry& entry) {
122 return entry.fID;
123 }
124
Herb Derby86240592018-05-24 16:12:31 -0400125 void addBlob(sk_sp<GrTextBlob> blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500126 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400127 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
128 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500129
Florin Malitac337c9e2017-03-10 18:02:29 +0000130 fBlobs.emplace_back(std::move(blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500131 }
132
Herb Derby86240592018-05-24 16:12:31 -0400133 void removeBlob(GrTextBlob* blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500134 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400135 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
Florin Malita33fdb8d2017-03-07 16:51:57 -0500136
Herb Derby86240592018-05-24 16:12:31 -0400137 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500138 SkASSERT(index >= 0);
139
140 fBlobs.removeShuffle(index);
141 }
142
Herb Derby86240592018-05-24 16:12:31 -0400143 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500144 auto index = this->findBlobIndex(key);
145 return index < 0 ? nullptr : fBlobs[index];
146 }
147
Herb Derby86240592018-05-24 16:12:31 -0400148 int findBlobIndex(const GrTextBlob::Key& key) const{
Florin Malita33fdb8d2017-03-07 16:51:57 -0500149 for (int i = 0; i < fBlobs.count(); ++i) {
Herb Derby86240592018-05-24 16:12:31 -0400150 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500151 return i;
152 }
153 }
154 return -1;
155 }
156
157 uint32_t fID;
158 // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/
159 // linear search is acceptable. If usage changes, we should re-evaluate this structure.
Herb Derby86240592018-05-24 16:12:31 -0400160 SkSTArray<1, sk_sp<GrTextBlob>, true> fBlobs;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500161 };
joshualittb7133be2015-04-08 09:08:31 -0700162
Herb Derby86240592018-05-24 16:12:31 -0400163 void add(sk_sp<GrTextBlob> blob) {
164 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malitac337c9e2017-03-10 18:02:29 +0000165 auto* idEntry = fBlobIDCache.find(id);
166 if (!idEntry) {
167 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
168 }
169
170 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
Herb Derby86240592018-05-24 16:12:31 -0400171 GrTextBlob* rawBlobPtr = blob.get();
Florin Malitac337c9e2017-03-10 18:02:29 +0000172 fBlobList.addToHead(rawBlobPtr);
Herb Derbyb12175f2018-05-23 16:38:09 -0400173 fCurrentSize += blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +0000174 idEntry->addBlob(std::move(blob));
175
176 this->checkPurge(rawBlobPtr);
177 }
178
Herb Derby86240592018-05-24 16:12:31 -0400179 void checkPurge(GrTextBlob* blob = nullptr);
joshualitt17d833b2015-08-03 10:17:44 -0700180
Florin Malita012893b2017-07-11 09:31:22 -0400181 static const int kMinGrowthSize = 1 << 16;
joshualitt17d833b2015-08-03 10:17:44 -0700182 static const int kDefaultBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700183 BitmapBlobList fBlobList;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500184 SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache;
joshualitt0db6dfa2015-04-10 07:01:30 -0700185 PFOverBudgetCB fCallback;
186 void* fData;
Herb Derbyb12175f2018-05-23 16:38:09 -0400187 size_t fSizeBudget;
188 size_t fCurrentSize{0};
Jim Van Verth474d6872017-12-14 13:00:05 -0500189 uint32_t fUniqueID; // unique id to use for messaging
Florin Malita4a01ac92017-03-13 16:45:28 -0400190 SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox;
joshualittb7133be2015-04-08 09:08:31 -0700191};
192
193#endif