2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/text/gpu/TextBlobRedrawCoordinator.h"
10 #include "src/core/SkStrikeCache.h"
12 #include "src/gpu/ganesh/v1/SurfaceDrawContext_v1.h"
15 // This needs to be outside the namespace so we can declare SkMessageBus properly
16 DECLARE_SKMESSAGEBUS_MESSAGE(sktext::gpu::TextBlobRedrawCoordinator::PurgeBlobMessage,
18 namespace sktext::gpu {
19 // This function is captured by the above macro using implementations from SkMessageBus.h
20 static inline bool SkShouldPostMessageToBus(
21 const TextBlobRedrawCoordinator::PurgeBlobMessage& msg, uint32_t msgBusUniqueID) {
22 return msg.fContextID == msgBusUniqueID;
25 TextBlobRedrawCoordinator::TextBlobRedrawCoordinator(uint32_t messageBusID)
26 : fSizeBudget(kDefaultBudget)
27 , fMessageBusID(messageBusID)
28 , fPurgeBlobInbox(messageBusID) { }
31 void TextBlobRedrawCoordinator::drawGlyphRunList(SkCanvas* canvas,
33 const SkMatrixProvider& viewMatrix,
34 const SkGlyphRunList& glyphRunList,
36 SkStrikeDeviceInfo strikeDeviceInfo,
37 skgpu::v1::SurfaceDrawContext* sdc) {
38 sk_sp<TextBlob> blob = this->findOrCreateBlob(viewMatrix, glyphRunList, paint,
41 blob->draw(canvas, clip, viewMatrix, glyphRunList.origin(), paint, sdc);
45 sk_sp<TextBlob> TextBlobRedrawCoordinator::findOrCreateBlob(const SkMatrixProvider& viewMatrix,
46 const SkGlyphRunList& glyphRunList,
48 SkStrikeDeviceInfo strikeDeviceInfo) {
49 SkMatrix positionMatrix{viewMatrix.localToDevice()};
50 positionMatrix.preTranslate(glyphRunList.origin().x(), glyphRunList.origin().y());
52 auto [canCache, key] = TextBlob::Key::Make(
53 glyphRunList, paint, positionMatrix, strikeDeviceInfo);
56 blob = this->find(key);
59 if (blob == nullptr || !blob->canReuse(paint, positionMatrix)) {
60 if (blob != nullptr) {
61 // We have to remake the blob because changes may invalidate our masks.
62 this->remove(blob.get());
65 blob = TextBlob::Make(
66 glyphRunList, paint, positionMatrix,
67 strikeDeviceInfo, SkStrikeCache::GlobalStrikeCache());
71 // The blob may already have been created on a different thread. Use the first one
73 blob = this->addOrReturnExisting(glyphRunList, blob);
80 sk_sp<TextBlob> TextBlobRedrawCoordinator::addOrReturnExisting(
81 const SkGlyphRunList& glyphRunList, sk_sp<TextBlob> blob) {
82 SkAutoSpinlock lock{fSpinLock};
83 blob = this->internalAdd(std::move(blob));
84 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID);
88 sk_sp<TextBlob> TextBlobRedrawCoordinator::find(const TextBlob::Key& key) {
89 SkAutoSpinlock lock{fSpinLock};
90 const BlobIDCacheEntry* idEntry = fBlobIDCache.find(key.fUniqueID);
91 if (idEntry == nullptr) {
95 sk_sp<TextBlob> blob = idEntry->find(key);
96 TextBlob* blobPtr = blob.get();
97 if (blobPtr != nullptr && blobPtr != fBlobList.head()) {
98 fBlobList.remove(blobPtr);
99 fBlobList.addToHead(blobPtr);
104 void TextBlobRedrawCoordinator::remove(TextBlob* blob) {
105 SkAutoSpinlock lock{fSpinLock};
106 this->internalRemove(blob);
109 void TextBlobRedrawCoordinator::internalRemove(TextBlob* blob) {
110 auto id = blob->key().fUniqueID;
111 auto* idEntry = fBlobIDCache.find(id);
113 if (idEntry != nullptr) {
114 sk_sp<TextBlob> stillExists = idEntry->find(blob->key());
115 if (blob == stillExists.get()) {
116 fCurrentSize -= blob->size();
117 fBlobList.remove(blob);
118 idEntry->removeBlob(blob);
119 if (idEntry->fBlobs.empty()) {
120 fBlobIDCache.remove(id);
126 void TextBlobRedrawCoordinator::freeAll() {
127 SkAutoSpinlock lock{fSpinLock};
128 fBlobIDCache.reset();
133 void TextBlobRedrawCoordinator::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
134 SkASSERT(blobID != SK_InvalidGenID);
135 SkMessageBus<PurgeBlobMessage, uint32_t>::Post(PurgeBlobMessage(blobID, cacheID));
138 void TextBlobRedrawCoordinator::purgeStaleBlobs() {
139 SkAutoSpinlock lock{fSpinLock};
140 this->internalPurgeStaleBlobs();
143 void TextBlobRedrawCoordinator::internalPurgeStaleBlobs() {
144 SkTArray<PurgeBlobMessage> msgs;
145 fPurgeBlobInbox.poll(&msgs);
147 for (const auto& msg : msgs) {
148 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
150 // no cache entries for id
154 // remove all blob entries from the LRU list
155 for (const auto& blob : idEntry->fBlobs) {
156 fCurrentSize -= blob->size();
157 fBlobList.remove(blob.get());
160 // drop the idEntry itself (unrefs all blobs)
161 fBlobIDCache.remove(msg.fBlobID);
165 size_t TextBlobRedrawCoordinator::usedBytes() const {
166 SkAutoSpinlock lock{fSpinLock};
170 bool TextBlobRedrawCoordinator::isOverBudget() const {
171 SkAutoSpinlock lock{fSpinLock};
172 return fCurrentSize > fSizeBudget;
175 void TextBlobRedrawCoordinator::internalCheckPurge(TextBlob* blob) {
176 // First, purge all stale blob IDs.
177 this->internalPurgeStaleBlobs();
179 // If we are still over budget, then unref until we are below budget again
180 if (fCurrentSize > fSizeBudget) {
181 TextBlobList::Iter iter;
182 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
183 TextBlob* lruBlob = nullptr;
184 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
185 // Backup the iterator before removing and unrefing the blob
188 this->internalRemove(lruBlob);
191 #ifdef SPEW_BUDGET_MESSAGE
192 if (fCurrentSize > fSizeBudget) {
193 SkDebugf("Single textblob is larger than our whole budget");
199 sk_sp<TextBlob> TextBlobRedrawCoordinator::internalAdd(sk_sp<TextBlob> blob) {
200 auto id = blob->key().fUniqueID;
201 auto* idEntry = fBlobIDCache.find(id);
203 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
206 if (sk_sp<TextBlob> alreadyIn = idEntry->find(blob->key()); alreadyIn) {
207 blob = std::move(alreadyIn);
209 fBlobList.addToHead(blob.get());
210 fCurrentSize += blob->size();
211 idEntry->addBlob(blob);
214 this->internalCheckPurge(blob.get());
218 TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
220 TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
222 uint32_t TextBlobRedrawCoordinator::BlobIDCacheEntry::GetKey(
223 const TextBlobRedrawCoordinator::BlobIDCacheEntry& entry) {
227 void TextBlobRedrawCoordinator::BlobIDCacheEntry::addBlob(sk_sp<TextBlob> blob) {
229 SkASSERT(blob->key().fUniqueID == fID);
230 SkASSERT(!this->find(blob->key()));
232 fBlobs.emplace_back(std::move(blob));
235 void TextBlobRedrawCoordinator::BlobIDCacheEntry::removeBlob(TextBlob* blob) {
237 SkASSERT(blob->key().fUniqueID == fID);
239 auto index = this->findBlobIndex(blob->key());
240 SkASSERT(index >= 0);
242 fBlobs.removeShuffle(index);
246 TextBlobRedrawCoordinator::BlobIDCacheEntry::find(const TextBlob::Key& key) const {
247 auto index = this->findBlobIndex(key);
248 return index < 0 ? nullptr : fBlobs[index];
251 int TextBlobRedrawCoordinator::BlobIDCacheEntry::findBlobIndex(const TextBlob::Key& key) const {
252 for (int i = 0; i < fBlobs.count(); ++i) {
253 if (fBlobs[i]->key() == key) {
260 } // namespace sktext::gpu