Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "GrCCPathCache.h" |
| 9 | |
| 10 | #include "GrShape.h" |
| 11 | #include "SkNx.h" |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 12 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 13 | DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<GrCCPathCacheEntry>); |
| 14 | |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 15 | static inline uint32_t next_path_cache_id() { |
| 16 | static std::atomic<uint32_t> gNextID(1); |
| 17 | for (;;) { |
| 18 | uint32_t id = gNextID.fetch_add(+1, std::memory_order_acquire); |
| 19 | if (SK_InvalidUniqueID != id) { |
| 20 | return id; |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 25 | static inline bool SkShouldPostMessageToBus( |
| 26 | const sk_sp<GrCCPathCacheEntry>& entry, uint32_t msgBusUniqueID) { |
| 27 | return entry->pathCacheUniqueID() == msgBusUniqueID; |
| 28 | } |
| 29 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 30 | // The maximum number of cache entries we allow in our own cache. |
| 31 | static constexpr int kMaxCacheCount = 1 << 16; |
| 32 | |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 33 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 34 | GrCCPathCache::MaskTransform::MaskTransform(const SkMatrix& m, SkIVector* shift) |
| 35 | : fMatrix2x2{m.getScaleX(), m.getSkewX(), m.getSkewY(), m.getScaleY()} { |
| 36 | SkASSERT(!m.hasPerspective()); |
| 37 | Sk2f translate = Sk2f(m.getTranslateX(), m.getTranslateY()); |
Chris Dalton | 76c775f | 2018-10-01 23:08:06 -0600 | [diff] [blame] | 38 | Sk2f transFloor; |
| 39 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 40 | // On Android framework we pre-round view matrix translates to integers for better caching. |
| 41 | transFloor = translate; |
| 42 | #else |
| 43 | transFloor = translate.floor(); |
| 44 | (translate - transFloor).store(fSubpixelTranslate); |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 45 | #endif |
Chris Dalton | 76c775f | 2018-10-01 23:08:06 -0600 | [diff] [blame] | 46 | shift->set((int)transFloor[0], (int)transFloor[1]); |
| 47 | SkASSERT((float)shift->fX == transFloor[0]); // Make sure transFloor had integer values. |
| 48 | SkASSERT((float)shift->fY == transFloor[1]); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | inline static bool fuzzy_equals(const GrCCPathCache::MaskTransform& a, |
| 52 | const GrCCPathCache::MaskTransform& b) { |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 53 | if ((Sk4f::Load(a.fMatrix2x2) != Sk4f::Load(b.fMatrix2x2)).anyTrue()) { |
| 54 | return false; |
| 55 | } |
| 56 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 57 | if (((Sk2f::Load(a.fSubpixelTranslate) - |
| 58 | Sk2f::Load(b.fSubpixelTranslate)).abs() > 1.f/256).anyTrue()) { |
| 59 | return false; |
| 60 | } |
| 61 | #endif |
| 62 | return true; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 65 | namespace { |
| 66 | |
| 67 | // Produces a key that accounts both for a shape's path geometry, as well as any stroke/style. |
| 68 | class WriteStyledKey { |
| 69 | public: |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 70 | static constexpr int kStyledKeySizeInBytesIdx = 0; |
| 71 | static constexpr int kStrokeWidthIdx = 1; |
| 72 | static constexpr int kStrokeMiterIdx = 2; |
| 73 | static constexpr int kStrokeCapJoinIdx = 3; |
| 74 | static constexpr int kShapeUnstyledKeyIdx = 4; |
| 75 | |
| 76 | static constexpr int kStrokeKeyCount = 3; // [width, miterLimit, cap|join]. |
| 77 | |
| 78 | WriteStyledKey(const GrShape& shape) : fShapeUnstyledKeyCount(shape.unstyledKeySize()) {} |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 79 | |
| 80 | // Returns the total number of uint32_t's to allocate for the key. |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 81 | int allocCountU32() const { return kShapeUnstyledKeyIdx + fShapeUnstyledKeyCount; } |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 82 | |
| 83 | // Writes the key to out[]. |
| 84 | void write(const GrShape& shape, uint32_t* out) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 85 | out[kStyledKeySizeInBytesIdx] = |
| 86 | (kStrokeKeyCount + fShapeUnstyledKeyCount) * sizeof(uint32_t); |
| 87 | |
| 88 | // Stroke key. |
| 89 | // We don't use GrStyle::WriteKey() because it does not account for hairlines. |
| 90 | // http://skbug.com/8273 |
| 91 | SkASSERT(!shape.style().hasPathEffect()); |
| 92 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 93 | if (stroke.isFillStyle()) { |
| 94 | // Use a value for width that won't collide with a valid fp32 value >= 0. |
| 95 | out[kStrokeWidthIdx] = ~0; |
| 96 | out[kStrokeMiterIdx] = out[kStrokeCapJoinIdx] = 0; |
| 97 | } else { |
| 98 | float width = stroke.getWidth(), miterLimit = stroke.getMiter(); |
| 99 | memcpy(&out[kStrokeWidthIdx], &width, sizeof(float)); |
| 100 | memcpy(&out[kStrokeMiterIdx], &miterLimit, sizeof(float)); |
| 101 | out[kStrokeCapJoinIdx] = (stroke.getCap() << 16) | stroke.getJoin(); |
| 102 | GR_STATIC_ASSERT(sizeof(out[kStrokeWidthIdx]) == sizeof(float)); |
| 103 | } |
| 104 | |
| 105 | // Shape unstyled key. |
| 106 | shape.writeUnstyledKey(&out[kShapeUnstyledKeyIdx]); |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private: |
| 110 | int fShapeUnstyledKeyCount; |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } |
| 114 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 115 | inline bool operator==(const GrCCPathCache::HashKey& key1, const GrCCPathCache::HashKey& key2) { |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 116 | return key1.fData[0] == key2.fData[0] && !memcmp(&key1.fData[1], &key2.fData[1], key1.fData[0]); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | inline GrCCPathCache::HashKey GrCCPathCache::HashNode::GetKey(const GrCCPathCacheEntry* entry) { |
| 120 | // The shape key is a variable-length footer to the entry allocation. |
| 121 | return HashKey{(const uint32_t*)((const char*)entry + sizeof(GrCCPathCacheEntry))}; |
| 122 | } |
| 123 | |
| 124 | inline uint32_t GrCCPathCache::HashNode::Hash(HashKey key) { |
| 125 | return GrResourceKeyHash(&key.fData[1], key.fData[0]); |
| 126 | } |
| 127 | |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 128 | inline GrCCPathCache::HashNode::HashNode(GrCCPathCache* pathCache, const MaskTransform& m, |
| 129 | const GrShape& shape) |
| 130 | : fPathCache(pathCache) { |
| 131 | SkASSERT(SkGetThreadID() == fPathCache->fGraphicsThreadID); |
| 132 | SkASSERT(shape.hasUnstyledKey()); |
| 133 | |
| 134 | WriteStyledKey writeKey(shape); |
| 135 | void* mem = ::operator new (sizeof(GrCCPathCacheEntry) + |
| 136 | writeKey.allocCountU32() * sizeof(uint32_t)); |
| 137 | fEntry.reset(new (mem) GrCCPathCacheEntry(fPathCache->fInvalidatedEntriesInbox.uniqueID(), m)); |
| 138 | |
| 139 | // The shape key is a variable-length footer to the entry allocation. |
| 140 | uint32_t* keyData = (uint32_t*)((char*)mem + sizeof(GrCCPathCacheEntry)); |
| 141 | writeKey.write(shape, keyData); |
| 142 | } |
| 143 | |
| 144 | inline GrCCPathCache::HashNode::~HashNode() { |
| 145 | this->willExitHashTable(); |
| 146 | } |
| 147 | |
| 148 | inline GrCCPathCache::HashNode& GrCCPathCache::HashNode::operator=(HashNode&& node) { |
| 149 | this->willExitHashTable(); |
| 150 | fPathCache = node.fPathCache; |
| 151 | fEntry = std::move(node.fEntry); |
| 152 | SkASSERT(!node.fEntry); |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | inline void GrCCPathCache::HashNode::willExitHashTable() { |
| 157 | if (!fEntry) { |
| 158 | return; // We were moved. |
| 159 | } |
| 160 | |
| 161 | SkASSERT(fPathCache); |
| 162 | SkASSERT(SkGetThreadID() == fPathCache->fGraphicsThreadID); |
| 163 | SkASSERT(fPathCache->fLRU.isInList(fEntry.get())); |
| 164 | |
| 165 | fEntry->invalidateAtlas(); // Must happen on graphics thread. |
| 166 | fPathCache->fLRU.remove(fEntry.get()); |
| 167 | } |
| 168 | |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 169 | |
| 170 | GrCCPathCache::GrCCPathCache() |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 171 | : fInvalidatedEntriesInbox(next_path_cache_id()) { |
| 172 | SkDEBUGCODE(fGraphicsThreadID = SkGetThreadID()); |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 175 | GrCCPathCache::~GrCCPathCache() { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 176 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
| 177 | |
| 178 | fHashTable.reset(); // Must be cleared first; ~HashNode calls fLRU.remove() on us. |
| 179 | SkASSERT(fLRU.isEmpty()); // Ensure the hash table and LRU list were coherent. |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 180 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 181 | |
| 182 | sk_sp<GrCCPathCacheEntry> GrCCPathCache::find(const GrShape& shape, const MaskTransform& m, |
| 183 | CreateIfAbsent createIfAbsent) { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 184 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
| 185 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 186 | if (!shape.hasUnstyledKey()) { |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 190 | WriteStyledKey writeKey(shape); |
| 191 | SkAutoSTMalloc<GrShape::kMaxKeyFromDataVerbCnt * 4, uint32_t> keyData(writeKey.allocCountU32()); |
| 192 | writeKey.write(shape, keyData.get()); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 193 | |
| 194 | GrCCPathCacheEntry* entry = nullptr; |
| 195 | if (HashNode* node = fHashTable.find({keyData.get()})) { |
| 196 | entry = node->entry(); |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 197 | SkASSERT(fLRU.isInList(entry)); |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 198 | if (fuzzy_equals(m, entry->fMaskTransform)) { |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 199 | ++entry->fHitCount; // The path was reused with a compatible matrix. |
| 200 | } else if (CreateIfAbsent::kYes == createIfAbsent && entry->unique()) { |
| 201 | // This entry is unique: we can recycle it instead of deleting and malloc-ing a new one. |
| 202 | entry->fMaskTransform = m; |
| 203 | entry->fHitCount = 1; |
| 204 | entry->invalidateAtlas(); |
| 205 | SkASSERT(!entry->fCurrFlushAtlas); // Should be null because 'entry' is unique. |
Chris Dalton | a8429cf | 2018-06-22 11:43:31 -0600 | [diff] [blame] | 206 | } else { |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 207 | this->evict(entry); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 208 | entry = nullptr; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (!entry) { |
| 213 | if (CreateIfAbsent::kNo == createIfAbsent) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | if (fHashTable.count() >= kMaxCacheCount) { |
| 217 | this->evict(fLRU.tail()); // We've exceeded our limit. |
| 218 | } |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 219 | SkASSERT(!fHashTable.find({keyData.get()})); |
| 220 | entry = fHashTable.set(HashNode(this, m, shape))->entry(); |
Chris Dalton | f40ddfa | 2018-10-09 14:19:21 -0600 | [diff] [blame] | 221 | shape.addGenIDChangeListener(sk_ref_sp(entry)); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 222 | SkASSERT(fHashTable.count() <= kMaxCacheCount); |
| 223 | } else { |
| 224 | fLRU.remove(entry); // Will be re-added at head. |
| 225 | } |
| 226 | |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 227 | SkDEBUGCODE(HashNode* node = fHashTable.find(HashNode::GetKey(entry))); |
| 228 | SkASSERT(node && node->entry() == entry); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 229 | fLRU.addToHead(entry); |
| 230 | return sk_ref_sp(entry); |
| 231 | } |
| 232 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 233 | void GrCCPathCache::evict(GrCCPathCacheEntry* entry) { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 234 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
| 235 | |
Greg Daniel | eb772c0 | 2018-10-19 15:00:06 +0000 | [diff] [blame] | 236 | bool isInCache = entry->fNext || (fLRU.tail() == entry); |
| 237 | SkASSERT(isInCache == fLRU.isInList(entry)); |
| 238 | if (isInCache) { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 239 | SkDEBUGCODE(HashNode* node = fHashTable.find(HashNode::GetKey(entry))); |
| 240 | SkASSERT(node && node->entry() == entry); |
| 241 | fHashTable.remove(HashNode::GetKey(entry)); |
| 242 | // HashNode::willExitHashTable() takes care of the rest. |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 243 | } |
| 244 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 245 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 246 | void GrCCPathCache::purgeAsNeeded() { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 247 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
| 248 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 249 | SkTArray<sk_sp<GrCCPathCacheEntry>> invalidatedEntries; |
| 250 | fInvalidatedEntriesInbox.poll(&invalidatedEntries); |
| 251 | for (const sk_sp<GrCCPathCacheEntry>& entry : invalidatedEntries) { |
| 252 | this->evict(entry.get()); |
| 253 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 254 | } |
| 255 | |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 256 | |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 257 | GrCCPathCacheEntry::GrCCPathCacheEntry(uint32_t pathCacheUniqueID, |
| 258 | const MaskTransform& maskTransform) |
| 259 | : fPathCacheUniqueID(pathCacheUniqueID), fMaskTransform(maskTransform) { |
| 260 | SkASSERT(SK_InvalidUniqueID != fPathCacheUniqueID); |
| 261 | SkDEBUGCODE(fGraphicsThreadID = SkGetThreadID()); |
| 262 | } |
| 263 | |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 264 | GrCCPathCacheEntry::~GrCCPathCacheEntry() { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 265 | // This might get called from a different thread. |
| 266 | SkASSERT(!fCachedAtlasInfo); // Should have been cleared when the entry was evicted. |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 267 | SkASSERT(!fCurrFlushAtlas); // Client is required to reset fCurrFlushAtlas back to null. |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 268 | } |
| 269 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 270 | void GrCCPathCacheEntry::initAsStashedAtlas(const GrUniqueKey& atlasKey, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 271 | const SkIVector& atlasOffset, const SkRect& devBounds, |
| 272 | const SkRect& devBounds45, const SkIRect& devIBounds, |
| 273 | const SkIVector& maskShift) { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 274 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 275 | SkASSERT(atlasKey.isValid()); |
| 276 | SkASSERT(!fCurrFlushAtlas); // Otherwise we should reuse the atlas from last time. |
| 277 | |
| 278 | fAtlasKey = atlasKey; |
| 279 | fAtlasOffset = atlasOffset + maskShift; |
| 280 | SkASSERT(!fCachedAtlasInfo); // Otherwise they should have reused the cached atlas instead. |
| 281 | |
| 282 | float dx = (float)maskShift.fX, dy = (float)maskShift.fY; |
| 283 | fDevBounds = devBounds.makeOffset(-dx, -dy); |
| 284 | fDevBounds45 = GrCCPathProcessor::MakeOffset45(devBounds45, -dx, -dy); |
| 285 | fDevIBounds = devIBounds.makeOffset(-maskShift.fX, -maskShift.fY); |
| 286 | } |
| 287 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 288 | void GrCCPathCacheEntry::updateToCachedAtlas(const GrUniqueKey& atlasKey, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 289 | const SkIVector& newAtlasOffset, |
| 290 | sk_sp<GrCCAtlas::CachedAtlasInfo> info) { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 291 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 292 | SkASSERT(atlasKey.isValid()); |
| 293 | SkASSERT(!fCurrFlushAtlas); // Otherwise we should reuse the atlas from last time. |
| 294 | |
| 295 | fAtlasKey = atlasKey; |
| 296 | fAtlasOffset = newAtlasOffset; |
| 297 | |
| 298 | SkASSERT(!fCachedAtlasInfo); // Otherwise we need to invalidate our pixels in the old info. |
| 299 | fCachedAtlasInfo = std::move(info); |
| 300 | fCachedAtlasInfo->fNumPathPixels += this->height() * this->width(); |
| 301 | } |
| 302 | |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 303 | void GrCCPathCacheEntry::invalidateAtlas() { |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame^] | 304 | SkASSERT(SkGetThreadID() == fGraphicsThreadID); |
| 305 | |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 306 | if (fCachedAtlasInfo) { |
| 307 | // Mark our own pixels invalid in the cached atlas texture. |
| 308 | fCachedAtlasInfo->fNumInvalidatedPathPixels += this->height() * this->width(); |
| 309 | if (!fCachedAtlasInfo->fIsPurgedFromResourceCache && |
| 310 | fCachedAtlasInfo->fNumInvalidatedPathPixels >= fCachedAtlasInfo->fNumPathPixels / 2) { |
| 311 | // Too many invalidated pixels: purge the atlas texture from the resource cache. |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 312 | // The GrContext and CCPR path cache both share the same unique ID. |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 313 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post( |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 314 | GrUniqueKeyInvalidatedMessage(fAtlasKey, fCachedAtlasInfo->fContextUniqueID)); |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 315 | fCachedAtlasInfo->fIsPurgedFromResourceCache = true; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | fAtlasKey.reset(); |
| 320 | fCachedAtlasInfo = nullptr; |
| 321 | } |
| 322 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 323 | void GrCCPathCacheEntry::onChange() { |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 324 | // Post a thread-safe eviction message. |
| 325 | SkMessageBus<sk_sp<GrCCPathCacheEntry>>::Post(sk_ref_sp(this)); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 326 | } |