Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / text / gpu / TextBlobRedrawCoordinator.cpp
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 #if SK_SUPPORT_GPU
12 #include "src/gpu/ganesh/v1/SurfaceDrawContext_v1.h"
13 #endif
14
15 // This needs to be outside the namespace so we can declare SkMessageBus properly
16 DECLARE_SKMESSAGEBUS_MESSAGE(sktext::gpu::TextBlobRedrawCoordinator::PurgeBlobMessage,
17                              uint32_t, true)
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;
23 }
24
25 TextBlobRedrawCoordinator::TextBlobRedrawCoordinator(uint32_t messageBusID)
26         : fSizeBudget(kDefaultBudget)
27         , fMessageBusID(messageBusID)
28         , fPurgeBlobInbox(messageBusID) { }
29
30 #if SK_SUPPORT_GPU
31 void TextBlobRedrawCoordinator::drawGlyphRunList(SkCanvas* canvas,
32                                                  const GrClip* clip,
33                                                  const SkMatrixProvider& viewMatrix,
34                                                  const SkGlyphRunList& glyphRunList,
35                                                  const SkPaint& paint,
36                                                  SkStrikeDeviceInfo strikeDeviceInfo,
37                                                  skgpu::v1::SurfaceDrawContext* sdc) {
38     sk_sp<TextBlob> blob = this->findOrCreateBlob(viewMatrix, glyphRunList, paint,
39                                                   strikeDeviceInfo);
40
41     blob->draw(canvas, clip, viewMatrix, glyphRunList.origin(), paint, sdc);
42 }
43 #endif
44
45 sk_sp<TextBlob> TextBlobRedrawCoordinator::findOrCreateBlob(const SkMatrixProvider& viewMatrix,
46                                                             const SkGlyphRunList& glyphRunList,
47                                                             const SkPaint& paint,
48                                                             SkStrikeDeviceInfo strikeDeviceInfo) {
49     SkMatrix positionMatrix{viewMatrix.localToDevice()};
50     positionMatrix.preTranslate(glyphRunList.origin().x(), glyphRunList.origin().y());
51
52     auto [canCache, key] = TextBlob::Key::Make(
53             glyphRunList, paint, positionMatrix, strikeDeviceInfo);
54     sk_sp<TextBlob> blob;
55     if (canCache) {
56         blob = this->find(key);
57     }
58
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());
63         }
64
65         blob = TextBlob::Make(
66                 glyphRunList, paint, positionMatrix,
67                 strikeDeviceInfo, SkStrikeCache::GlobalStrikeCache());
68
69         if (canCache) {
70             blob->addKey(key);
71             // The blob may already have been created on a different thread. Use the first one
72             // that was there.
73             blob = this->addOrReturnExisting(glyphRunList, blob);
74         }
75     }
76
77     return blob;
78 }
79
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);
85     return blob;
86 }
87
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) {
92         return nullptr;
93     }
94
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);
100     }
101     return blob;
102 }
103
104 void TextBlobRedrawCoordinator::remove(TextBlob* blob) {
105     SkAutoSpinlock lock{fSpinLock};
106     this->internalRemove(blob);
107 }
108
109 void TextBlobRedrawCoordinator::internalRemove(TextBlob* blob) {
110     auto  id      = blob->key().fUniqueID;
111     auto* idEntry = fBlobIDCache.find(id);
112
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);
121             }
122         }
123     }
124 }
125
126 void TextBlobRedrawCoordinator::freeAll() {
127     SkAutoSpinlock lock{fSpinLock};
128     fBlobIDCache.reset();
129     fBlobList.reset();
130     fCurrentSize = 0;
131 }
132
133 void TextBlobRedrawCoordinator::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
134     SkASSERT(blobID != SK_InvalidGenID);
135     SkMessageBus<PurgeBlobMessage, uint32_t>::Post(PurgeBlobMessage(blobID, cacheID));
136 }
137
138 void TextBlobRedrawCoordinator::purgeStaleBlobs() {
139     SkAutoSpinlock lock{fSpinLock};
140     this->internalPurgeStaleBlobs();
141 }
142
143 void TextBlobRedrawCoordinator::internalPurgeStaleBlobs() {
144     SkTArray<PurgeBlobMessage> msgs;
145     fPurgeBlobInbox.poll(&msgs);
146
147     for (const auto& msg : msgs) {
148         auto* idEntry = fBlobIDCache.find(msg.fBlobID);
149         if (!idEntry) {
150             // no cache entries for id
151             continue;
152         }
153
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());
158         }
159
160         // drop the idEntry itself (unrefs all blobs)
161         fBlobIDCache.remove(msg.fBlobID);
162     }
163 }
164
165 size_t TextBlobRedrawCoordinator::usedBytes() const {
166     SkAutoSpinlock lock{fSpinLock};
167     return fCurrentSize;
168 }
169
170 bool TextBlobRedrawCoordinator::isOverBudget() const {
171     SkAutoSpinlock lock{fSpinLock};
172     return fCurrentSize > fSizeBudget;
173 }
174
175 void TextBlobRedrawCoordinator::internalCheckPurge(TextBlob* blob) {
176     // First, purge all stale blob IDs.
177     this->internalPurgeStaleBlobs();
178
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
186             iter.prev();
187
188             this->internalRemove(lruBlob);
189         }
190
191     #ifdef SPEW_BUDGET_MESSAGE
192         if (fCurrentSize > fSizeBudget) {
193             SkDebugf("Single textblob is larger than our whole budget");
194         }
195     #endif
196     }
197 }
198
199 sk_sp<TextBlob> TextBlobRedrawCoordinator::internalAdd(sk_sp<TextBlob> blob) {
200     auto  id      = blob->key().fUniqueID;
201     auto* idEntry = fBlobIDCache.find(id);
202     if (!idEntry) {
203         idEntry = fBlobIDCache.set(id, BlobIDCacheEntry(id));
204     }
205
206     if (sk_sp<TextBlob> alreadyIn = idEntry->find(blob->key()); alreadyIn) {
207         blob = std::move(alreadyIn);
208     } else {
209         fBlobList.addToHead(blob.get());
210         fCurrentSize += blob->size();
211         idEntry->addBlob(blob);
212     }
213
214     this->internalCheckPurge(blob.get());
215     return blob;
216 }
217
218 TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {}
219
220 TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {}
221
222 uint32_t TextBlobRedrawCoordinator::BlobIDCacheEntry::GetKey(
223         const TextBlobRedrawCoordinator::BlobIDCacheEntry& entry) {
224     return entry.fID;
225 }
226
227 void TextBlobRedrawCoordinator::BlobIDCacheEntry::addBlob(sk_sp<TextBlob> blob) {
228     SkASSERT(blob);
229     SkASSERT(blob->key().fUniqueID == fID);
230     SkASSERT(!this->find(blob->key()));
231
232     fBlobs.emplace_back(std::move(blob));
233 }
234
235 void TextBlobRedrawCoordinator::BlobIDCacheEntry::removeBlob(TextBlob* blob) {
236     SkASSERT(blob);
237     SkASSERT(blob->key().fUniqueID == fID);
238
239     auto index = this->findBlobIndex(blob->key());
240     SkASSERT(index >= 0);
241
242     fBlobs.removeShuffle(index);
243 }
244
245 sk_sp<TextBlob>
246 TextBlobRedrawCoordinator::BlobIDCacheEntry::find(const TextBlob::Key& key) const {
247     auto index = this->findBlobIndex(key);
248     return index < 0 ? nullptr : fBlobs[index];
249 }
250
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) {
254             return i;
255         }
256     }
257     return -1;
258 }
259
260 }  // namespace sktext::gpu