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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCPathCache.h" |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/SkNx.h" |
| 11 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 12 | #include "src/gpu/GrProxyProvider.h" |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 13 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 14 | static constexpr int kMaxKeyDataCountU32 = 256; // 1kB of uint32_t's. |
| 15 | |
| 16 | DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<GrCCPathCache::Key>); |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 17 | |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 18 | static inline uint32_t next_path_cache_id() { |
| 19 | static std::atomic<uint32_t> gNextID(1); |
| 20 | for (;;) { |
| 21 | uint32_t id = gNextID.fetch_add(+1, std::memory_order_acquire); |
| 22 | if (SK_InvalidUniqueID != id) { |
| 23 | return id; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 28 | static inline bool SkShouldPostMessageToBus( |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 29 | const sk_sp<GrCCPathCache::Key>& key, uint32_t msgBusUniqueID) { |
| 30 | return key->pathCacheUniqueID() == msgBusUniqueID; |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 31 | } |
| 32 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 33 | // The maximum number of cache entries we allow in our own cache. |
| 34 | static constexpr int kMaxCacheCount = 1 << 16; |
| 35 | |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 36 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 37 | GrCCPathCache::MaskTransform::MaskTransform(const SkMatrix& m, SkIVector* shift) |
| 38 | : fMatrix2x2{m.getScaleX(), m.getSkewX(), m.getSkewY(), m.getScaleY()} { |
| 39 | SkASSERT(!m.hasPerspective()); |
| 40 | Sk2f translate = Sk2f(m.getTranslateX(), m.getTranslateY()); |
Chris Dalton | 76c775f | 2018-10-01 23:08:06 -0600 | [diff] [blame] | 41 | Sk2f transFloor; |
| 42 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 43 | // On Android framework we pre-round view matrix translates to integers for better caching. |
| 44 | transFloor = translate; |
| 45 | #else |
| 46 | transFloor = translate.floor(); |
| 47 | (translate - transFloor).store(fSubpixelTranslate); |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 48 | #endif |
Chris Dalton | 76c775f | 2018-10-01 23:08:06 -0600 | [diff] [blame] | 49 | shift->set((int)transFloor[0], (int)transFloor[1]); |
| 50 | SkASSERT((float)shift->fX == transFloor[0]); // Make sure transFloor had integer values. |
| 51 | SkASSERT((float)shift->fY == transFloor[1]); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | inline static bool fuzzy_equals(const GrCCPathCache::MaskTransform& a, |
| 55 | const GrCCPathCache::MaskTransform& b) { |
Chris Dalton | 644341a | 2018-06-18 19:14:16 -0600 | [diff] [blame] | 56 | if ((Sk4f::Load(a.fMatrix2x2) != Sk4f::Load(b.fMatrix2x2)).anyTrue()) { |
| 57 | return false; |
| 58 | } |
| 59 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 60 | if (((Sk2f::Load(a.fSubpixelTranslate) - |
| 61 | Sk2f::Load(b.fSubpixelTranslate)).abs() > 1.f/256).anyTrue()) { |
| 62 | return false; |
| 63 | } |
| 64 | #endif |
| 65 | return true; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 68 | sk_sp<GrCCPathCache::Key> GrCCPathCache::Key::Make(uint32_t pathCacheUniqueID, |
| 69 | int dataCountU32, const void* data) { |
| 70 | void* memory = ::operator new (sizeof(Key) + dataCountU32 * sizeof(uint32_t)); |
| 71 | sk_sp<GrCCPathCache::Key> key(new (memory) Key(pathCacheUniqueID, dataCountU32)); |
| 72 | if (data) { |
| 73 | memcpy(key->data(), data, key->dataSizeInBytes()); |
| 74 | } |
| 75 | return key; |
| 76 | } |
| 77 | |
| 78 | const uint32_t* GrCCPathCache::Key::data() const { |
| 79 | // The shape key is a variable-length footer to the entry allocation. |
| 80 | return reinterpret_cast<const uint32_t*>(reinterpret_cast<const char*>(this) + sizeof(Key)); |
| 81 | } |
| 82 | |
| 83 | uint32_t* GrCCPathCache::Key::data() { |
| 84 | // The shape key is a variable-length footer to the entry allocation. |
| 85 | return reinterpret_cast<uint32_t*>(reinterpret_cast<char*>(this) + sizeof(Key)); |
| 86 | } |
| 87 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 88 | void GrCCPathCache::Key::onChange() { |
| 89 | // Our key's corresponding path was invalidated. Post a thread-safe eviction message. |
| 90 | SkMessageBus<sk_sp<Key>>::Post(sk_ref_sp(this)); |
| 91 | } |
| 92 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 93 | GrCCPathCache::GrCCPathCache(uint32_t contextUniqueID) |
| 94 | : fContextUniqueID(contextUniqueID) |
| 95 | , fInvalidatedKeysInbox(next_path_cache_id()) |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 96 | , fScratchKey(Key::Make(fInvalidatedKeysInbox.uniqueID(), kMaxKeyDataCountU32)) { |
| 97 | } |
| 98 | |
| 99 | GrCCPathCache::~GrCCPathCache() { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 100 | while (!fLRU.isEmpty()) { |
| 101 | this->evict(*fLRU.tail()->fCacheKey, fLRU.tail()); |
| 102 | } |
| 103 | SkASSERT(0 == fHashTable.count()); // Ensure the hash table and LRU list were coherent. |
| 104 | |
| 105 | // Now take all the atlas textures we just invalidated and purge them from the GrResourceCache. |
| 106 | // We just purge via message bus since we don't have any access to the resource cache right now. |
| 107 | for (sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) { |
| 108 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post( |
| 109 | GrUniqueKeyInvalidatedMessage(proxy->getUniqueKey(), fContextUniqueID)); |
| 110 | } |
| 111 | for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) { |
| 112 | SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post( |
| 113 | GrUniqueKeyInvalidatedMessage(key, fContextUniqueID)); |
| 114 | } |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 117 | namespace { |
| 118 | |
| 119 | // Produces a key that accounts both for a shape's path geometry, as well as any stroke/style. |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 120 | class WriteKeyHelper { |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 121 | public: |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 122 | static constexpr int kStrokeWidthIdx = 0; |
| 123 | static constexpr int kStrokeMiterIdx = 1; |
| 124 | static constexpr int kStrokeCapJoinIdx = 2; |
| 125 | static constexpr int kShapeUnstyledKeyIdx = 3; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 126 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 127 | WriteKeyHelper(const GrShape& shape) : fShapeUnstyledKeyCount(shape.unstyledKeySize()) {} |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 128 | |
| 129 | // 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] | 130 | int allocCountU32() const { return kShapeUnstyledKeyIdx + fShapeUnstyledKeyCount; } |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 131 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 132 | // Writes the key data to out[]. |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 133 | void write(const GrShape& shape, uint32_t* out) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 134 | // Stroke key. |
| 135 | // We don't use GrStyle::WriteKey() because it does not account for hairlines. |
| 136 | // http://skbug.com/8273 |
| 137 | SkASSERT(!shape.style().hasPathEffect()); |
| 138 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 139 | if (stroke.isFillStyle()) { |
| 140 | // Use a value for width that won't collide with a valid fp32 value >= 0. |
| 141 | out[kStrokeWidthIdx] = ~0; |
| 142 | out[kStrokeMiterIdx] = out[kStrokeCapJoinIdx] = 0; |
| 143 | } else { |
| 144 | float width = stroke.getWidth(), miterLimit = stroke.getMiter(); |
| 145 | memcpy(&out[kStrokeWidthIdx], &width, sizeof(float)); |
| 146 | memcpy(&out[kStrokeMiterIdx], &miterLimit, sizeof(float)); |
| 147 | out[kStrokeCapJoinIdx] = (stroke.getCap() << 16) | stroke.getJoin(); |
| 148 | GR_STATIC_ASSERT(sizeof(out[kStrokeWidthIdx]) == sizeof(float)); |
| 149 | } |
| 150 | |
| 151 | // Shape unstyled key. |
| 152 | shape.writeUnstyledKey(&out[kShapeUnstyledKeyIdx]); |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
| 156 | int fShapeUnstyledKeyCount; |
Chris Dalton | 8f8bf88 | 2018-07-18 10:55:51 -0600 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } |
| 160 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 161 | GrCCPathCache::OnFlushEntryRef GrCCPathCache::find( |
| 162 | GrOnFlushResourceProvider* onFlushRP, const GrShape& shape, |
| 163 | const SkIRect& clippedDrawBounds, const SkMatrix& viewMatrix, SkIVector* maskShift) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 164 | if (!shape.hasUnstyledKey()) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 165 | return OnFlushEntryRef(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 168 | WriteKeyHelper writeKeyHelper(shape); |
| 169 | if (writeKeyHelper.allocCountU32() > kMaxKeyDataCountU32) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 170 | return OnFlushEntryRef(); |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | SkASSERT(fScratchKey->unique()); |
| 174 | fScratchKey->resetDataCountU32(writeKeyHelper.allocCountU32()); |
| 175 | writeKeyHelper.write(shape, fScratchKey->data()); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 176 | |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 177 | MaskTransform m(viewMatrix, maskShift); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 178 | GrCCPathCacheEntry* entry = nullptr; |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 179 | if (HashNode* node = fHashTable.find(*fScratchKey)) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 180 | entry = node->entry(); |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 181 | SkASSERT(fLRU.isInList(entry)); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 182 | |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 183 | if (!fuzzy_equals(m, entry->fMaskTransform)) { |
| 184 | // The path was reused with an incompatible matrix. |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 185 | if (entry->unique()) { |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 186 | // This entry is unique: recycle it instead of deleting and malloc-ing a new one. |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 187 | SkASSERT(0 == entry->fOnFlushRefCnt); // Because we are unique. |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 188 | entry->fMaskTransform = m; |
| 189 | entry->fHitCount = 0; |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 190 | entry->fHitRect = SkIRect::MakeEmpty(); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 191 | entry->releaseCachedAtlas(this); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 192 | } else { |
| 193 | this->evict(*fScratchKey); |
| 194 | entry = nullptr; |
| 195 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | if (!entry) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 200 | if (fHashTable.count() >= kMaxCacheCount) { |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 201 | SkDEBUGCODE(HashNode* node = fHashTable.find(*fLRU.tail()->fCacheKey)); |
| 202 | SkASSERT(node && node->entry() == fLRU.tail()); |
| 203 | this->evict(*fLRU.tail()->fCacheKey); // We've exceeded our limit. |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 204 | } |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 205 | |
| 206 | // Create a new entry in the cache. |
| 207 | sk_sp<Key> permanentKey = Key::Make(fInvalidatedKeysInbox.uniqueID(), |
| 208 | writeKeyHelper.allocCountU32(), fScratchKey->data()); |
| 209 | SkASSERT(*permanentKey == *fScratchKey); |
| 210 | SkASSERT(!fHashTable.find(*permanentKey)); |
| 211 | entry = fHashTable.set(HashNode(this, std::move(permanentKey), m, shape))->entry(); |
| 212 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 213 | SkASSERT(fHashTable.count() <= kMaxCacheCount); |
| 214 | } else { |
| 215 | fLRU.remove(entry); // Will be re-added at head. |
| 216 | } |
| 217 | |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 218 | SkDEBUGCODE(HashNode* node = fHashTable.find(*fScratchKey)); |
Chris Dalton | 3b57279 | 2018-10-23 18:26:20 -0600 | [diff] [blame] | 219 | SkASSERT(node && node->entry() == entry); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 220 | fLRU.addToHead(entry); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 221 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 222 | if (0 == entry->fOnFlushRefCnt) { |
| 223 | // Only update the time stamp and hit count if we haven't seen this entry yet during the |
| 224 | // current flush. |
| 225 | entry->fTimestamp = this->quickPerFlushTimestamp(); |
| 226 | ++entry->fHitCount; |
| 227 | |
| 228 | if (entry->fCachedAtlas) { |
| 229 | SkASSERT(SkToBool(entry->fCachedAtlas->peekOnFlushRefCnt()) |
| 230 | == SkToBool(entry->fCachedAtlas->getOnFlushProxy())); |
| 231 | if (!entry->fCachedAtlas->getOnFlushProxy()) { |
| 232 | entry->fCachedAtlas->setOnFlushProxy( |
| 233 | onFlushRP->findOrCreateProxyByUniqueKey(entry->fCachedAtlas->textureKey(), |
| 234 | GrCCAtlas::kTextureOrigin)); |
| 235 | } |
| 236 | if (!entry->fCachedAtlas->getOnFlushProxy()) { |
| 237 | // Our atlas's backing texture got purged from the GrResourceCache. Release the |
| 238 | // cached atlas. |
| 239 | entry->releaseCachedAtlas(this); |
| 240 | } |
| 241 | } |
| 242 | } |
Chris Dalton | aaa77c1 | 2019-01-07 17:45:36 -0700 | [diff] [blame] | 243 | entry->fHitRect.join(clippedDrawBounds.makeOffset(-maskShift->x(), -maskShift->y())); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 244 | SkASSERT(!entry->fCachedAtlas || entry->fCachedAtlas->getOnFlushProxy()); |
| 245 | return OnFlushEntryRef::OnFlushRef(entry); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 246 | } |
| 247 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 248 | void GrCCPathCache::evict(const GrCCPathCache::Key& key, GrCCPathCacheEntry* entry) { |
| 249 | if (!entry) { |
| 250 | HashNode* node = fHashTable.find(key); |
| 251 | SkASSERT(node); |
| 252 | entry = node->entry(); |
| 253 | } |
| 254 | SkASSERT(*entry->fCacheKey == key); |
| 255 | SkASSERT(!entry->hasBeenEvicted()); |
| 256 | entry->fCacheKey->markShouldUnregisterFromPath(); // Unregister the path listener. |
| 257 | entry->releaseCachedAtlas(this); |
| 258 | fLRU.remove(entry); |
| 259 | fHashTable.remove(key); |
| 260 | } |
| 261 | |
| 262 | void GrCCPathCache::doPreFlushProcessing() { |
| 263 | this->evictInvalidatedCacheKeys(); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 264 | |
| 265 | // Mark the per-flush timestamp as needing to be updated with a newer clock reading. |
| 266 | fPerFlushTimestamp = GrStdSteadyClock::time_point::min(); |
| 267 | } |
| 268 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 269 | void GrCCPathCache::purgeEntriesOlderThan(GrProxyProvider* proxyProvider, |
| 270 | const GrStdSteadyClock::time_point& purgeTime) { |
| 271 | this->evictInvalidatedCacheKeys(); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 272 | |
| 273 | #ifdef SK_DEBUG |
| 274 | auto lastTimestamp = (fLRU.isEmpty()) |
| 275 | ? GrStdSteadyClock::time_point::max() |
| 276 | : fLRU.tail()->fTimestamp; |
| 277 | #endif |
| 278 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 279 | // Evict every entry from our local path cache whose timestamp is older than purgeTime. |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 280 | while (!fLRU.isEmpty() && fLRU.tail()->fTimestamp < purgeTime) { |
| 281 | #ifdef SK_DEBUG |
| 282 | // Verify that fLRU is sorted by timestamp. |
| 283 | auto timestamp = fLRU.tail()->fTimestamp; |
| 284 | SkASSERT(timestamp >= lastTimestamp); |
| 285 | lastTimestamp = timestamp; |
| 286 | #endif |
| 287 | this->evict(*fLRU.tail()->fCacheKey); |
| 288 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 289 | |
| 290 | // Now take all the atlas textures we just invalidated and purge them from the GrResourceCache. |
| 291 | this->purgeInvalidatedAtlasTextures(proxyProvider); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 292 | } |
| 293 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 294 | void GrCCPathCache::purgeInvalidatedAtlasTextures(GrOnFlushResourceProvider* onFlushRP) { |
| 295 | for (sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) { |
| 296 | onFlushRP->removeUniqueKeyFromProxy(proxy.get()); |
| 297 | } |
| 298 | fInvalidatedProxies.reset(); |
| 299 | |
| 300 | for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) { |
| 301 | onFlushRP->processInvalidUniqueKey(key); |
| 302 | } |
| 303 | fInvalidatedProxyUniqueKeys.reset(); |
| 304 | } |
| 305 | |
| 306 | void GrCCPathCache::purgeInvalidatedAtlasTextures(GrProxyProvider* proxyProvider) { |
| 307 | for (sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) { |
| 308 | proxyProvider->removeUniqueKeyFromProxy(proxy.get()); |
| 309 | } |
| 310 | fInvalidatedProxies.reset(); |
| 311 | |
| 312 | for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) { |
| 313 | proxyProvider->processInvalidUniqueKey(key, nullptr, |
| 314 | GrProxyProvider::InvalidateGPUResource::kYes); |
| 315 | } |
| 316 | fInvalidatedProxyUniqueKeys.reset(); |
| 317 | } |
| 318 | |
| 319 | void GrCCPathCache::evictInvalidatedCacheKeys() { |
Chris Dalton | 9985a27 | 2018-10-30 14:29:39 -0600 | [diff] [blame] | 320 | SkTArray<sk_sp<Key>> invalidatedKeys; |
| 321 | fInvalidatedKeysInbox.poll(&invalidatedKeys); |
| 322 | for (const sk_sp<Key>& key : invalidatedKeys) { |
| 323 | bool isInCache = !key->shouldUnregisterFromPath(); // Gets set upon exiting the cache. |
| 324 | if (isInCache) { |
| 325 | this->evict(*key); |
| 326 | } |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 327 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 328 | } |
| 329 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 330 | GrCCPathCache::OnFlushEntryRef |
| 331 | GrCCPathCache::OnFlushEntryRef::OnFlushRef(GrCCPathCacheEntry* entry) { |
| 332 | entry->ref(); |
| 333 | ++entry->fOnFlushRefCnt; |
| 334 | if (entry->fCachedAtlas) { |
| 335 | entry->fCachedAtlas->incrOnFlushRefCnt(); |
| 336 | } |
| 337 | return OnFlushEntryRef(entry); |
| 338 | } |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 339 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 340 | GrCCPathCache::OnFlushEntryRef::~OnFlushEntryRef() { |
| 341 | if (!fEntry) { |
| 342 | return; |
| 343 | } |
| 344 | --fEntry->fOnFlushRefCnt; |
| 345 | SkASSERT(fEntry->fOnFlushRefCnt >= 0); |
| 346 | if (fEntry->fCachedAtlas) { |
| 347 | fEntry->fCachedAtlas->decrOnFlushRefCnt(); |
| 348 | } |
| 349 | fEntry->unref(); |
| 350 | } |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 351 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 352 | |
| 353 | void GrCCPathCacheEntry::setCoverageCountAtlas( |
| 354 | GrOnFlushResourceProvider* onFlushRP, GrCCAtlas* atlas, const SkIVector& atlasOffset, |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 355 | const GrOctoBounds& octoBounds, const SkIRect& devIBounds, const SkIVector& maskShift) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 356 | SkASSERT(fOnFlushRefCnt > 0); |
| 357 | SkASSERT(!fCachedAtlas); // Otherwise we would need to call releaseCachedAtlas(). |
| 358 | |
| 359 | if (this->hasBeenEvicted()) { |
| 360 | // This entry will never be found in the path cache again. Don't bother trying to save an |
| 361 | // atlas texture for it in the GrResourceCache. |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | fCachedAtlas = atlas->refOrMakeCachedAtlas(onFlushRP); |
| 366 | fCachedAtlas->incrOnFlushRefCnt(fOnFlushRefCnt); |
| 367 | fCachedAtlas->addPathPixels(devIBounds.height() * devIBounds.width()); |
| 368 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 369 | fAtlasOffset = atlasOffset + maskShift; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 370 | |
Chris Dalton | 8610e9c | 2019-05-09 11:07:10 -0600 | [diff] [blame] | 371 | fOctoBounds.setOffset(octoBounds, -maskShift.fX, -maskShift.fY); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 372 | fDevIBounds = devIBounds.makeOffset(-maskShift.fX, -maskShift.fY); |
| 373 | } |
| 374 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 375 | GrCCPathCacheEntry::ReleaseAtlasResult GrCCPathCacheEntry::upgradeToLiteralCoverageAtlas( |
| 376 | GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCAtlas* atlas, |
| 377 | const SkIVector& newAtlasOffset) { |
| 378 | SkASSERT(!this->hasBeenEvicted()); |
| 379 | SkASSERT(fOnFlushRefCnt > 0); |
| 380 | SkASSERT(fCachedAtlas); |
| 381 | SkASSERT(GrCCAtlas::CoverageType::kFP16_CoverageCount == fCachedAtlas->coverageType()); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 382 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 383 | ReleaseAtlasResult releaseAtlasResult = this->releaseCachedAtlas(pathCache); |
| 384 | |
| 385 | fCachedAtlas = atlas->refOrMakeCachedAtlas(onFlushRP); |
| 386 | fCachedAtlas->incrOnFlushRefCnt(fOnFlushRefCnt); |
| 387 | fCachedAtlas->addPathPixels(this->height() * this->width()); |
| 388 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 389 | fAtlasOffset = newAtlasOffset; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 390 | return releaseAtlasResult; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 391 | } |
| 392 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 393 | GrCCPathCacheEntry::ReleaseAtlasResult GrCCPathCacheEntry::releaseCachedAtlas( |
| 394 | GrCCPathCache* pathCache) { |
| 395 | ReleaseAtlasResult result = ReleaseAtlasResult::kNone; |
| 396 | if (fCachedAtlas) { |
| 397 | result = fCachedAtlas->invalidatePathPixels(pathCache, this->height() * this->width()); |
| 398 | if (fOnFlushRefCnt) { |
| 399 | SkASSERT(fOnFlushRefCnt > 0); |
| 400 | fCachedAtlas->decrOnFlushRefCnt(fOnFlushRefCnt); |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 401 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 402 | fCachedAtlas = nullptr; |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 403 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 404 | return result; |
| 405 | } |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 406 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 407 | GrCCPathCacheEntry::ReleaseAtlasResult GrCCCachedAtlas::invalidatePathPixels( |
| 408 | GrCCPathCache* pathCache, int numPixels) { |
| 409 | // Mark the pixels invalid in the cached atlas texture. |
| 410 | fNumInvalidatedPathPixels += numPixels; |
| 411 | SkASSERT(fNumInvalidatedPathPixels <= fNumPathPixels); |
| 412 | if (!fIsInvalidatedFromResourceCache && fNumInvalidatedPathPixels >= fNumPathPixels / 2) { |
| 413 | // Too many invalidated pixels: purge the atlas texture from the resource cache. |
| 414 | if (fOnFlushProxy) { |
| 415 | // Don't clear (or std::move) fOnFlushProxy. Other path cache entries might still have a |
| 416 | // reference on this atlas and expect to use our proxy during the current flush. |
| 417 | // fOnFlushProxy will be cleared once fOnFlushRefCnt decrements to zero. |
| 418 | pathCache->fInvalidatedProxies.push_back(fOnFlushProxy); |
| 419 | } else { |
| 420 | pathCache->fInvalidatedProxyUniqueKeys.push_back(fTextureKey); |
| 421 | } |
| 422 | fIsInvalidatedFromResourceCache = true; |
| 423 | return ReleaseAtlasResult::kDidInvalidateFromCache; |
| 424 | } |
| 425 | return ReleaseAtlasResult::kNone; |
| 426 | } |
| 427 | |
| 428 | void GrCCCachedAtlas::decrOnFlushRefCnt(int count) const { |
| 429 | SkASSERT(count > 0); |
| 430 | fOnFlushRefCnt -= count; |
| 431 | SkASSERT(fOnFlushRefCnt >= 0); |
| 432 | if (0 == fOnFlushRefCnt) { |
| 433 | // Don't hold the actual proxy past the end of the current flush. |
| 434 | SkASSERT(fOnFlushProxy); |
| 435 | fOnFlushProxy = nullptr; |
| 436 | } |
Chris Dalton | 907102e | 2018-06-29 13:18:53 -0600 | [diff] [blame] | 437 | } |