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