blob: f45e8976c5ca0dd259c48ee7ee02c3dadf75654b [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 Derbyeba195f2019-12-03 16:44:47 -050037 GrStrikeCache* strikeCache,
Herb Derbya00da612019-03-04 17:10:01 -050038 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -050039 bool forceW) {
40 return GrTextBlob::Make(
41 glyphRunList.totalGlyphCount(), strikeCache, glyphRunList.origin(), color, forceW);
Herb Derbycddab252018-07-16 11:19:04 -040042 }
43
Herb Derbyb935cf82018-07-26 16:54:18 -040044 sk_sp<GrTextBlob> makeCachedBlob(const SkGlyphRunList& glyphRunList,
Herb Derbyeba195f2019-12-03 16:44:47 -050045 GrStrikeCache* strikeCache,
Herb Derbycddab252018-07-16 11:19:04 -040046 const GrTextBlob::Key& key,
47 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derbya00da612019-03-04 17:10:01 -050048 GrColor color,
Herb Derbyeba195f2019-12-03 16:44:47 -050049 bool forceW) {
50 sk_sp<GrTextBlob> cacheBlob(
51 this->makeBlob(glyphRunList, strikeCache, color, forceW));
52 cacheBlob->setupKey(key, blurRec, glyphRunList.paint());
Herb Derbycddab252018-07-16 11:19:04 -040053 this->add(cacheBlob);
Herb Derbyb935cf82018-07-26 16:54:18 -040054 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID);
Herb Derbycddab252018-07-16 11:19:04 -040055 return cacheBlob;
56 }
57
Herb Derby86240592018-05-24 16:12:31 -040058 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -050059 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
60 return idEntry ? idEntry->find(key) : nullptr;
joshualittb7133be2015-04-08 09:08:31 -070061 }
62
Herb Derby86240592018-05-24 16:12:31 -040063 void remove(GrTextBlob* blob) {
64 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malita33fdb8d2017-03-07 16:51:57 -050065 auto* idEntry = fBlobIDCache.find(id);
66 SkASSERT(idEntry);
67
Herb Derbyb12175f2018-05-23 16:38:09 -040068 fCurrentSize -= blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +000069 fBlobList.remove(blob);
Florin Malita33fdb8d2017-03-07 16:51:57 -050070 idEntry->removeBlob(blob);
71 if (idEntry->fBlobs.empty()) {
72 fBlobIDCache.remove(id);
73 }
joshualittb7133be2015-04-08 09:08:31 -070074 }
75
Herb Derby86240592018-05-24 16:12:31 -040076 void makeMRU(GrTextBlob* blob) {
joshualittb7133be2015-04-08 09:08:31 -070077 if (fBlobList.head() == blob) {
78 return;
79 }
80
81 fBlobList.remove(blob);
82 fBlobList.addToHead(blob);
83 }
84
joshualitt26ffc002015-04-16 11:24:04 -070085 void freeAll();
86
joshualittb7133be2015-04-08 09:08:31 -070087 // TODO move to SkTextBlob
joshualitt259fbf12015-07-21 11:39:34 -070088 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070089 SkTextBlobRunIterator itCounter(blob);
joshualittb7133be2015-04-08 09:08:31 -070090 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
91 *glyphCount += itCounter.glyphCount();
92 }
93 }
94
joshualitt17d833b2015-08-03 10:17:44 -070095 void setBudget(size_t budget) {
Herb Derbyb12175f2018-05-23 16:38:09 -040096 fSizeBudget = budget;
joshualitt17d833b2015-08-03 10:17:44 -070097 this->checkPurge();
98 }
99
Florin Malita4a01ac92017-03-13 16:45:28 -0400100 struct PurgeBlobMessage {
Brian Salomon238069b2018-07-11 15:58:57 -0400101 PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID)
102 : fBlobID(blobID), fContextID(contextUniqueID) {}
Brian Salomon238069b2018-07-11 15:58:57 -0400103
104 uint32_t fBlobID;
105 uint32_t fContextID;
Florin Malita4a01ac92017-03-13 16:45:28 -0400106 };
107
Jim Van Verth474d6872017-12-14 13:00:05 -0500108 static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -0400109
Jim Van Verth76d917c2017-12-13 09:26:37 -0500110 void purgeStaleBlobs();
111
Khushal71652e22018-10-29 13:05:36 -0700112 size_t usedBytes() const { return fCurrentSize; }
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