blob: 4e9b5cef954f6a428a29b9cbb6ea38c571358d23 [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 Derby5424a5e2018-11-14 12:04:38 -050036 sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList, GrColor color) {
37 return GrTextBlob::Make(glyphRunList.totalGlyphCount(), glyphRunList.size(), color);
Herb Derbycddab252018-07-16 11:19:04 -040038 }
39
Herb Derbyb935cf82018-07-26 16:54:18 -040040 sk_sp<GrTextBlob> makeCachedBlob(const SkGlyphRunList& glyphRunList,
Herb Derbycddab252018-07-16 11:19:04 -040041 const GrTextBlob::Key& key,
42 const SkMaskFilterBase::BlurRec& blurRec,
Herb Derby5424a5e2018-11-14 12:04:38 -050043 const SkPaint& paint,
44 GrColor color) {
45 sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList, color));
Herb Derbycddab252018-07-16 11:19:04 -040046 cacheBlob->setupKey(key, blurRec, paint);
47 this->add(cacheBlob);
Herb Derbyb935cf82018-07-26 16:54:18 -040048 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID);
Herb Derbycddab252018-07-16 11:19:04 -040049 return cacheBlob;
50 }
51
Herb Derby86240592018-05-24 16:12:31 -040052 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -050053 const auto* idEntry = fBlobIDCache.find(key.fUniqueID);
54 return idEntry ? idEntry->find(key) : nullptr;
joshualittb7133be2015-04-08 09:08:31 -070055 }
56
Herb Derby86240592018-05-24 16:12:31 -040057 void remove(GrTextBlob* blob) {
58 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malita33fdb8d2017-03-07 16:51:57 -050059 auto* idEntry = fBlobIDCache.find(id);
60 SkASSERT(idEntry);
61
Herb Derbyb12175f2018-05-23 16:38:09 -040062 fCurrentSize -= blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +000063 fBlobList.remove(blob);
Florin Malita33fdb8d2017-03-07 16:51:57 -050064 idEntry->removeBlob(blob);
65 if (idEntry->fBlobs.empty()) {
66 fBlobIDCache.remove(id);
67 }
joshualittb7133be2015-04-08 09:08:31 -070068 }
69
Herb Derby86240592018-05-24 16:12:31 -040070 void makeMRU(GrTextBlob* blob) {
joshualittb7133be2015-04-08 09:08:31 -070071 if (fBlobList.head() == blob) {
72 return;
73 }
74
75 fBlobList.remove(blob);
76 fBlobList.addToHead(blob);
77 }
78
joshualitt26ffc002015-04-16 11:24:04 -070079 void freeAll();
80
joshualittb7133be2015-04-08 09:08:31 -070081 // TODO move to SkTextBlob
joshualitt259fbf12015-07-21 11:39:34 -070082 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) {
halcanary33779752015-10-27 14:01:05 -070083 SkTextBlobRunIterator itCounter(blob);
joshualittb7133be2015-04-08 09:08:31 -070084 for (; !itCounter.done(); itCounter.next(), (*runCount)++) {
85 *glyphCount += itCounter.glyphCount();
86 }
87 }
88
joshualitt17d833b2015-08-03 10:17:44 -070089 void setBudget(size_t budget) {
Herb Derbyb12175f2018-05-23 16:38:09 -040090 fSizeBudget = budget;
joshualitt17d833b2015-08-03 10:17:44 -070091 this->checkPurge();
92 }
93
Florin Malita4a01ac92017-03-13 16:45:28 -040094 struct PurgeBlobMessage {
Brian Salomon238069b2018-07-11 15:58:57 -040095 PurgeBlobMessage(uint32_t blobID, uint32_t contextUniqueID)
96 : fBlobID(blobID), fContextID(contextUniqueID) {}
Brian Salomon238069b2018-07-11 15:58:57 -040097
98 uint32_t fBlobID;
99 uint32_t fContextID;
Florin Malita4a01ac92017-03-13 16:45:28 -0400100 };
101
Jim Van Verth474d6872017-12-14 13:00:05 -0500102 static void PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID);
Florin Malita4a01ac92017-03-13 16:45:28 -0400103
Jim Van Verth76d917c2017-12-13 09:26:37 -0500104 void purgeStaleBlobs();
105
Khushal71652e22018-10-29 13:05:36 -0700106 size_t usedBytes() const { return fCurrentSize; }
107
joshualitt259fbf12015-07-21 11:39:34 -0700108private:
Herb Derby86240592018-05-24 16:12:31 -0400109 using BitmapBlobList = SkTInternalLList<GrTextBlob>;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500110
111 struct BlobIDCacheEntry {
112 BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
113 explicit BlobIDCacheEntry(uint32_t id) : fID(id) {}
114
115 static uint32_t GetKey(const BlobIDCacheEntry& entry) {
116 return entry.fID;
117 }
118
Herb Derby86240592018-05-24 16:12:31 -0400119 void addBlob(sk_sp<GrTextBlob> blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500120 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400121 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
122 SkASSERT(!this->find(GrTextBlob::GetKey(*blob)));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500123
Florin Malitac337c9e2017-03-10 18:02:29 +0000124 fBlobs.emplace_back(std::move(blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500125 }
126
Herb Derby86240592018-05-24 16:12:31 -0400127 void removeBlob(GrTextBlob* blob) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500128 SkASSERT(blob);
Herb Derby86240592018-05-24 16:12:31 -0400129 SkASSERT(GrTextBlob::GetKey(*blob).fUniqueID == fID);
Florin Malita33fdb8d2017-03-07 16:51:57 -0500130
Herb Derby86240592018-05-24 16:12:31 -0400131 auto index = this->findBlobIndex(GrTextBlob::GetKey(*blob));
Florin Malita33fdb8d2017-03-07 16:51:57 -0500132 SkASSERT(index >= 0);
133
134 fBlobs.removeShuffle(index);
135 }
136
Herb Derby86240592018-05-24 16:12:31 -0400137 sk_sp<GrTextBlob> find(const GrTextBlob::Key& key) const {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500138 auto index = this->findBlobIndex(key);
139 return index < 0 ? nullptr : fBlobs[index];
140 }
141
Herb Derby86240592018-05-24 16:12:31 -0400142 int findBlobIndex(const GrTextBlob::Key& key) const{
Florin Malita33fdb8d2017-03-07 16:51:57 -0500143 for (int i = 0; i < fBlobs.count(); ++i) {
Herb Derby86240592018-05-24 16:12:31 -0400144 if (GrTextBlob::GetKey(*fBlobs[i]) == key) {
Florin Malita33fdb8d2017-03-07 16:51:57 -0500145 return i;
146 }
147 }
148 return -1;
149 }
150
151 uint32_t fID;
152 // Current clients don't generate multiple GrAtlasTextBlobs per SkTextBlob, so an array w/
153 // linear search is acceptable. If usage changes, we should re-evaluate this structure.
Herb Derby86240592018-05-24 16:12:31 -0400154 SkSTArray<1, sk_sp<GrTextBlob>, true> fBlobs;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500155 };
joshualittb7133be2015-04-08 09:08:31 -0700156
Herb Derby86240592018-05-24 16:12:31 -0400157 void add(sk_sp<GrTextBlob> blob) {
158 auto id = GrTextBlob::GetKey(*blob).fUniqueID;
Florin Malitac337c9e2017-03-10 18:02:29 +0000159 auto* idEntry = fBlobIDCache.find(id);
160 if (!idEntry) {
161 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
162 }
163
164 // Safe to retain a raw ptr temporarily here, because the cache will hold a ref.
Herb Derby86240592018-05-24 16:12:31 -0400165 GrTextBlob* rawBlobPtr = blob.get();
Florin Malitac337c9e2017-03-10 18:02:29 +0000166 fBlobList.addToHead(rawBlobPtr);
Herb Derbyb12175f2018-05-23 16:38:09 -0400167 fCurrentSize += blob->size();
Florin Malitac337c9e2017-03-10 18:02:29 +0000168 idEntry->addBlob(std::move(blob));
169
170 this->checkPurge(rawBlobPtr);
171 }
172
Herb Derby86240592018-05-24 16:12:31 -0400173 void checkPurge(GrTextBlob* blob = nullptr);
joshualitt17d833b2015-08-03 10:17:44 -0700174
Florin Malita012893b2017-07-11 09:31:22 -0400175 static const int kMinGrowthSize = 1 << 16;
joshualitt17d833b2015-08-03 10:17:44 -0700176 static const int kDefaultBudget = 1 << 22;
joshualittb7133be2015-04-08 09:08:31 -0700177 BitmapBlobList fBlobList;
Florin Malita33fdb8d2017-03-07 16:51:57 -0500178 SkTHashMap<uint32_t, BlobIDCacheEntry> fBlobIDCache;
joshualitt0db6dfa2015-04-10 07:01:30 -0700179 PFOverBudgetCB fCallback;
180 void* fData;
Herb Derbyb12175f2018-05-23 16:38:09 -0400181 size_t fSizeBudget;
182 size_t fCurrentSize{0};
Jim Van Verth474d6872017-12-14 13:00:05 -0500183 uint32_t fUniqueID; // unique id to use for messaging
Florin Malita4a01ac92017-03-13 16:45:28 -0400184 SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox;
joshualittb7133be2015-04-08 09:08:31 -0700185};
186
187#endif