| 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 "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "src/core/SkDevice.h" |
| 14 |
#include "src/core/SkStrikeCache.h" |
| 15 |
#include "src/text/GlyphRun.h" |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
class SkCanvas; |
| 20 |
class SkPaint; |
| 21 |
|
| 22 |
using namespace skia_private; |
| 23 |
|
| 24 |
// This needs to be outside the namespace so we can declare SkMessageBus properly |
| 25 |
DECLARE_SKMESSAGEBUS_MESSAGE(sktext::gpu::TextBlobRedrawCoordinator::PurgeBlobMessage, |
| 26 |
uint32_t, true) |
| 27 |
namespace sktext::gpu { |
| 28 |
// This function is captured by the above macro using implementations from SkMessageBus.h |
| 29 |
static inline bool SkShouldPostMessageToBus( |
| 30 |
const TextBlobRedrawCoordinator::PurgeBlobMessage& msg, uint32_t msgBusUniqueID) { |
| 31 |
return msg.fContextID == msgBusUniqueID; |
| 32 |
} |
| 33 |
|
| 34 |
TextBlobRedrawCoordinator::TextBlobRedrawCoordinator(uint32_t messageBusID) |
| 35 |
: fSizeBudget(kDefaultBudget) |
| 36 |
, fMessageBusID(messageBusID) |
| 37 |
, fPurgeBlobInbox(messageBusID) { } |
| 38 |
|
| 39 |
void TextBlobRedrawCoordinator::drawGlyphRunList(SkCanvas* canvas, |
| 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 |
|
| 51 |
sk_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 |
|
| 86 |
static 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 |
|
| 92 |
sk_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 |
|
| 100 |
sk_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 |
|
| 116 |
void TextBlobRedrawCoordinator::remove(TextBlob* blob) { |
| 117 |
SkAutoSpinlock lock{fSpinLock}; |
| 118 |
this->internalRemove(blob); |
| 119 |
} |
| 120 |
|
| 121 |
void 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 |
|
| 138 |
void TextBlobRedrawCoordinator::freeAll() { |
| 139 |
SkAutoSpinlock lock{fSpinLock}; |
| 140 |
fBlobIDCache.reset(); |
| 141 |
fBlobList.reset(); |
| 142 |
fCurrentSize = 0; |
| 143 |
} |
| 144 |
|
| 145 |
void TextBlobRedrawCoordinator::purgeStaleBlobs() { |
| 146 |
SkAutoSpinlock lock{fSpinLock}; |
| 147 |
this->internalPurgeStaleBlobs(); |
| 148 |
} |
| 149 |
|
| 150 |
void TextBlobRedrawCoordinator::internalPurgeStaleBlobs() { |
| 151 |
TArray<PurgeBlobMessage> msgs; |
| 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 |
|
| 172 |
size_t TextBlobRedrawCoordinator::usedBytes() const { |
| 173 |
SkAutoSpinlock lock{fSpinLock}; |
| 174 |
return fCurrentSize; |
| 175 |
} |
| 176 |
|
| 177 |
bool TextBlobRedrawCoordinator::isOverBudget() const { |
| 178 |
SkAutoSpinlock lock{fSpinLock}; |
| 179 |
return fCurrentSize > fSizeBudget; |
| 180 |
} |
| 181 |
|
| 182 |
void 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 |
|
| 206 |
sk_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 |
|
| 225 |
TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry() : fID(SK_InvalidGenID) {} |
| 226 |
|
| 227 |
TextBlobRedrawCoordinator::BlobIDCacheEntry::BlobIDCacheEntry(uint32_t id) : fID(id) {} |
| 228 |
|
| 229 |
uint32_t TextBlobRedrawCoordinator::BlobIDCacheEntry::GetKey( |
| 230 |
const TextBlobRedrawCoordinator::BlobIDCacheEntry& entry) { |
| 231 |
return entry.fID; |
| 232 |
} |
| 233 |
|
| 234 |
void 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 |
|
| 242 |
void 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 |
|
| 252 |
sk_sp<TextBlob> |
| 253 |
TextBlobRedrawCoordinator::BlobIDCacheEntry::find(const TextBlob::Key& key) const { |
| 254 |
auto index = this->findBlobIndex(key); |
| 255 |
return index < 0 ? nullptr : fBlobs[index]; |
| 256 |
} |
| 257 |
|
| 258 |
int 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 |