Flutter Engine
The Flutter Engine
TextBlobRedrawCoordinator.cpp
Go to the documentation of this file.
1/*
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
9
13#include "src/core/SkDevice.h"
15#include "src/text/GlyphRun.h"
16
17#include <utility>
18
19class SkCanvas;
20class SkPaint;
21
22using namespace skia_private;
23
24// This needs to be outside the namespace so we can declare SkMessageBus properly
26 uint32_t, true)
27namespace sktext::gpu {
28// This function is captured by the above macro using implementations from SkMessageBus.h
29static inline bool SkShouldPostMessageToBus(
30 const TextBlobRedrawCoordinator::PurgeBlobMessage& msg, uint32_t msgBusUniqueID) {
31 return msg.fContextID == msgBusUniqueID;
32}
33
35 : fSizeBudget(kDefaultBudget)
36 , fMessageBusID(messageBusID)
37 , fPurgeBlobInbox(messageBusID) { }
38
40 const SkMatrix& viewMatrix,
41 const sktext::GlyphRunList& glyphRunList,
42 const SkPaint& paint,
43 SkStrikeDeviceInfo strikeDeviceInfo,
44 const AtlasDrawDelegate& atlasDelegate) {
45 sk_sp<TextBlob> blob = this->findOrCreateBlob(viewMatrix, glyphRunList, paint,
46 strikeDeviceInfo);
47
48 blob->draw(canvas, glyphRunList.origin(), paint, atlasDelegate);
49}
50
51sk_sp<TextBlob> TextBlobRedrawCoordinator::findOrCreateBlob(const SkMatrix& viewMatrix,
52 const GlyphRunList& glyphRunList,
53 const SkPaint& paint,
54 SkStrikeDeviceInfo strikeDeviceInfo) {
55 SkMatrix positionMatrix{viewMatrix};
56 positionMatrix.preTranslate(glyphRunList.origin().x(), glyphRunList.origin().y());
57
58 auto [canCache, key] = TextBlob::Key::Make(
59 glyphRunList, paint, positionMatrix, strikeDeviceInfo);
60 sk_sp<TextBlob> blob;
61 if (canCache) {
62 blob = this->find(key);
63 }
64
65 if (blob == nullptr || !blob->canReuse(paint, positionMatrix)) {
66 if (blob != nullptr) {
67 // We have to remake the blob because changes may invalidate our masks.
68 this->remove(blob.get());
69 }
70
71 blob = TextBlob::Make(
72 glyphRunList, paint, positionMatrix,
73 strikeDeviceInfo, SkStrikeCache::GlobalStrikeCache());
74
75 if (canCache) {
76 blob->addKey(key);
77 // The blob may already have been created on a different thread. Use the first one
78 // that was there.
79 blob = this->addOrReturnExisting(glyphRunList, blob);
80 }
81 }
82
83 return blob;
84}
85
86static void post_purge_blob_message(uint32_t blobID, uint32_t cacheID) {
87 using PurgeBlobMessage = TextBlobRedrawCoordinator::PurgeBlobMessage;
88 SkASSERT(blobID != SK_InvalidGenID);
89 SkMessageBus<PurgeBlobMessage, uint32_t>::Post(PurgeBlobMessage(blobID, cacheID));
90}
91
92sk_sp<TextBlob> TextBlobRedrawCoordinator::addOrReturnExisting(
93 const GlyphRunList& glyphRunList, sk_sp<TextBlob> blob) {
94 SkAutoSpinlock lock{fSpinLock};
95 blob = this->internalAdd(std::move(blob));
96 glyphRunList.temporaryShuntBlobNotifyAddedToCache(fMessageBusID, post_purge_blob_message);
97 return blob;
98}
99
100sk_sp<TextBlob> TextBlobRedrawCoordinator::find(const TextBlob::Key& key) {
101 SkAutoSpinlock lock{fSpinLock};
102 const BlobIDCacheEntry* idEntry = fBlobIDCache.find(key.fUniqueID);
103 if (idEntry == nullptr) {
104 return nullptr;
105 }
106
107 sk_sp<TextBlob> blob = idEntry->find(key);
108 TextBlob* blobPtr = blob.get();
109 if (blobPtr != nullptr && blobPtr != fBlobList.head()) {
110 fBlobList.remove(blobPtr);
111 fBlobList.addToHead(blobPtr);
112 }
113 return blob;
114}
115
116void TextBlobRedrawCoordinator::remove(TextBlob* blob) {
117 SkAutoSpinlock lock{fSpinLock};
118 this->internalRemove(blob);
119}
120
121void TextBlobRedrawCoordinator::internalRemove(TextBlob* blob) {
122 auto id = blob->key().fUniqueID;
123 auto* idEntry = fBlobIDCache.find(id);
124
125 if (idEntry != nullptr) {
126 sk_sp<TextBlob> stillExists = idEntry->find(blob->key());
127 if (blob == stillExists.get()) {
128 fCurrentSize -= blob->size();
129 fBlobList.remove(blob);
130 idEntry->removeBlob(blob);
131 if (idEntry->fBlobs.empty()) {
132 fBlobIDCache.remove(id);
133 }
134 }
135 }
136}
137
139 SkAutoSpinlock lock{fSpinLock};
140 fBlobIDCache.reset();
141 fBlobList.reset();
142 fCurrentSize = 0;
143}
144
146 SkAutoSpinlock lock{fSpinLock};
147 this->internalPurgeStaleBlobs();
148}
149
150void TextBlobRedrawCoordinator::internalPurgeStaleBlobs() {
152 fPurgeBlobInbox.poll(&msgs);
153
154 for (const auto& msg : msgs) {
155 auto* idEntry = fBlobIDCache.find(msg.fBlobID);
156 if (!idEntry) {
157 // no cache entries for id
158 continue;
159 }
160
161 // remove all blob entries from the LRU list
162 for (const auto& blob : idEntry->fBlobs) {
163 fCurrentSize -= blob->size();
164 fBlobList.remove(blob.get());
165 }
166
167 // drop the idEntry itself (unrefs all blobs)
168 fBlobIDCache.remove(msg.fBlobID);
169 }
170}
171
173 SkAutoSpinlock lock{fSpinLock};
174 return fCurrentSize;
175}
176
178 SkAutoSpinlock lock{fSpinLock};
179 return fCurrentSize > fSizeBudget;
180}
181
182void TextBlobRedrawCoordinator::internalCheckPurge(TextBlob* blob) {
183 // First, purge all stale blob IDs.
184 this->internalPurgeStaleBlobs();
185
186 // If we are still over budget, then unref until we are below budget again
187 if (fCurrentSize > fSizeBudget) {
188 TextBlobList::Iter iter;
189 iter.init(fBlobList, TextBlobList::Iter::kTail_IterStart);
190 TextBlob* lruBlob = nullptr;
191 while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) {
192 // Backup the iterator before removing and unrefing the blob
193 iter.prev();
194
195 this->internalRemove(lruBlob);
196 }
197
198 #ifdef SPEW_BUDGET_MESSAGE
199 if (fCurrentSize > fSizeBudget) {
200 SkDebugf("Single textblob is larger than our whole budget");
201 }
202 #endif
203 }
204}
205
206sk_sp<TextBlob> TextBlobRedrawCoordinator::internalAdd(sk_sp<TextBlob> blob) {
207 auto id = blob->key().fUniqueID;
208 auto* idEntry = fBlobIDCache.find(id);
209 if (!idEntry) {
210 idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
211 }
212
213 if (sk_sp<TextBlob> alreadyIn = idEntry->find(blob->key()); alreadyIn) {
214 blob = std::move(alreadyIn);
215 } else {
216 fBlobList.addToHead(blob.get());
217 fCurrentSize += blob->size();
218 idEntry->addBlob(blob);
219 }
220
221 this->internalCheckPurge(blob.get());
222 return blob;
223}
224
225TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
226
227TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
228
229uint32_t TextBlobRedrawCoordinator::BlobIDCacheEntry::GetKey(
230 const TextBlobRedrawCoordinator::BlobIDCacheEntry& entry) {
231 return entry.fID;
232}
233
234void TextBlobRedrawCoordinator::BlobIDCacheEntry::addBlob(sk_sp<TextBlob> blob) {
235 SkASSERT(blob);
236 SkASSERT(blob->key().fUniqueID == fID);
237 SkASSERT(!this->find(blob->key()));
238
239 fBlobs.emplace_back(std::move(blob));
240}
241
242void TextBlobRedrawCoordinator::BlobIDCacheEntry::removeBlob(TextBlob* blob) {
243 SkASSERT(blob);
244 SkASSERT(blob->key().fUniqueID == fID);
245
246 auto index = this->findBlobIndex(blob->key());
247 SkASSERT(index >= 0);
248
249 fBlobs.removeShuffle(index);
250}
251
254 auto index = this->findBlobIndex(key);
255 return index < 0 ? nullptr : fBlobs[index];
256}
257
258int TextBlobRedrawCoordinator::BlobIDCacheEntry::findBlobIndex(const TextBlob::Key& key) const {
259 for (int i = 0; i < fBlobs.size(); ++i) {
260 if (fBlobs[i]->key() == key) {
261 return i;
262 }
263 }
264 return -1;
265}
266
267} // namespace sktext::gpu
bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage &, GrDirectContext::DirectContextID potentialRecipient)
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define DECLARE_SKMESSAGEBUS_MESSAGE(Message, IDType, AllowCopyableMessage)
Definition: SkMessageBus.h:74
static constexpr uint32_t SK_InvalidGenID
Definition: SkTypes.h:192
int find(T *array, int N, T item)
SkMatrix & preTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:263
static void Post(Message m)
Definition: SkMessageBus.h:130
static SkStrikeCache * GlobalStrikeCache()
T * get() const
Definition: SkRefCnt.h:303
SkPoint origin() const
Definition: GlyphRun.h:114
size_t usedBytes() const SK_EXCLUDES(fSpinLock)
bool isOverBudget() const SK_EXCLUDES(fSpinLock)
TextBlobRedrawCoordinator(uint32_t messageBusID)
void drawGlyphRunList(SkCanvas *canvas, const SkMatrix &viewMatrix, const GlyphRunList &glyphRunList, const SkPaint &paint, SkStrikeDeviceInfo strikeDeviceInfo, const AtlasDrawDelegate &)
void freeAll() SK_EXCLUDES(fSpinLock)
void purgeStaleBlobs() SK_EXCLUDES(fSpinLock)
const Key & key()
Definition: TextBlob.h:106
size_t size() const
Definition: TextBlob.h:113
static sk_sp< TextBlob > Make(const sktext::GlyphRunList &glyphRunList, const SkPaint &paint, const SkMatrix &positionMatrix, SkStrikeDeviceInfo strikeDeviceInfo, StrikeForGPUCacheInterface *strikeCache)
Definition: TextBlob.cpp:194
const Paint & paint
Definition: color_source.cc:38
def remove(*paths)
std::function< void(const sktext::gpu::AtlasSubRun *subRun, SkPoint drawOrigin, const SkPaint &paint, sk_sp< SkRefCnt > subRunStorage, sktext::gpu::RendererData)> AtlasDrawDelegate
static std::tuple< bool, Key > Make(const GlyphRunList &glyphRunList, const SkPaint &paint, const SkMatrix &drawMatrix, const SkStrikeDeviceInfo &strikeDevice)
Definition: TextBlob.cpp:84
const uintptr_t id