blob: 30400cb8810059e8eada6d45883458c58b315ef1 [file] [log] [blame]
Chris Dalton4da70192018-06-18 09:51:36 -06001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCCPathCache.h"
Chris Dalton4da70192018-06-18 09:51:36 -06009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkNx.h"
11#include "src/gpu/GrOnFlushResourceProvider.h"
12#include "src/gpu/GrProxyProvider.h"
Chris Dalton4da70192018-06-18 09:51:36 -060013
Chris Dalton9985a272018-10-30 14:29:39 -060014static constexpr int kMaxKeyDataCountU32 = 256; // 1kB of uint32_t's.
15
16DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<GrCCPathCache::Key>);
Chris Dalton9a986cf2018-10-18 15:27:59 -060017
Chris Dalton8429c792018-10-23 15:56:22 -060018static 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 Dalton9a986cf2018-10-18 15:27:59 -060028static inline bool SkShouldPostMessageToBus(
Chris Dalton9985a272018-10-30 14:29:39 -060029 const sk_sp<GrCCPathCache::Key>& key, uint32_t msgBusUniqueID) {
30 return key->pathCacheUniqueID() == msgBusUniqueID;
Chris Dalton9a986cf2018-10-18 15:27:59 -060031}
32
Chris Dalton4da70192018-06-18 09:51:36 -060033// The maximum number of cache entries we allow in our own cache.
34static constexpr int kMaxCacheCount = 1 << 16;
35
Chris Dalton8429c792018-10-23 15:56:22 -060036
Chris Dalton4da70192018-06-18 09:51:36 -060037GrCCPathCache::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 Dalton76c775f2018-10-01 23:08:06 -060041 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 Dalton644341a2018-06-18 19:14:16 -060048#endif
Chris Dalton76c775f2018-10-01 23:08:06 -060049 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 Dalton4da70192018-06-18 09:51:36 -060052}
53
54inline static bool fuzzy_equals(const GrCCPathCache::MaskTransform& a,
55 const GrCCPathCache::MaskTransform& b) {
Chris Dalton644341a2018-06-18 19:14:16 -060056 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 Dalton4da70192018-06-18 09:51:36 -060066}
67
Chris Dalton9985a272018-10-30 14:29:39 -060068sk_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
78const 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
83uint32_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 Dalton9985a272018-10-30 14:29:39 -060088void 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 Dalton351e80c2019-01-06 22:51:00 -070093GrCCPathCache::GrCCPathCache(uint32_t contextUniqueID)
94 : fContextUniqueID(contextUniqueID)
95 , fInvalidatedKeysInbox(next_path_cache_id())
Chris Dalton9985a272018-10-30 14:29:39 -060096 , fScratchKey(Key::Make(fInvalidatedKeysInbox.uniqueID(), kMaxKeyDataCountU32)) {
97}
98
99GrCCPathCache::~GrCCPathCache() {
Chris Dalton351e80c2019-01-06 22:51:00 -0700100 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 Dalton9985a272018-10-30 14:29:39 -0600115}
116
Chris Dalton8f8bf882018-07-18 10:55:51 -0600117namespace {
118
119// Produces a key that accounts both for a shape's path geometry, as well as any stroke/style.
Chris Dalton9985a272018-10-30 14:29:39 -0600120class WriteKeyHelper {
Chris Dalton8f8bf882018-07-18 10:55:51 -0600121public:
Chris Dalton9985a272018-10-30 14:29:39 -0600122 static constexpr int kStrokeWidthIdx = 0;
123 static constexpr int kStrokeMiterIdx = 1;
124 static constexpr int kStrokeCapJoinIdx = 2;
125 static constexpr int kShapeUnstyledKeyIdx = 3;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800126
Chris Dalton9985a272018-10-30 14:29:39 -0600127 WriteKeyHelper(const GrShape& shape) : fShapeUnstyledKeyCount(shape.unstyledKeySize()) {}
Chris Dalton8f8bf882018-07-18 10:55:51 -0600128
129 // Returns the total number of uint32_t's to allocate for the key.
Chris Dalton09a7bb22018-08-31 19:53:15 +0800130 int allocCountU32() const { return kShapeUnstyledKeyIdx + fShapeUnstyledKeyCount; }
Chris Dalton8f8bf882018-07-18 10:55:51 -0600131
Chris Dalton9985a272018-10-30 14:29:39 -0600132 // Writes the key data to out[].
Chris Dalton8f8bf882018-07-18 10:55:51 -0600133 void write(const GrShape& shape, uint32_t* out) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800134 // 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 Dalton8f8bf882018-07-18 10:55:51 -0600153 }
154
155private:
156 int fShapeUnstyledKeyCount;
Chris Dalton8f8bf882018-07-18 10:55:51 -0600157};
158
159}
160
Chris Daltonaaa77c12019-01-07 17:45:36 -0700161GrCCPathCache::OnFlushEntryRef GrCCPathCache::find(
162 GrOnFlushResourceProvider* onFlushRP, const GrShape& shape,
163 const SkIRect& clippedDrawBounds, const SkMatrix& viewMatrix, SkIVector* maskShift) {
Chris Dalton4da70192018-06-18 09:51:36 -0600164 if (!shape.hasUnstyledKey()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700165 return OnFlushEntryRef();
Chris Dalton4da70192018-06-18 09:51:36 -0600166 }
167
Chris Dalton9985a272018-10-30 14:29:39 -0600168 WriteKeyHelper writeKeyHelper(shape);
169 if (writeKeyHelper.allocCountU32() > kMaxKeyDataCountU32) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700170 return OnFlushEntryRef();
Chris Dalton9985a272018-10-30 14:29:39 -0600171 }
172
173 SkASSERT(fScratchKey->unique());
174 fScratchKey->resetDataCountU32(writeKeyHelper.allocCountU32());
175 writeKeyHelper.write(shape, fScratchKey->data());
Chris Dalton4da70192018-06-18 09:51:36 -0600176
Chris Daltonaaa77c12019-01-07 17:45:36 -0700177 MaskTransform m(viewMatrix, maskShift);
Chris Dalton4da70192018-06-18 09:51:36 -0600178 GrCCPathCacheEntry* entry = nullptr;
Chris Dalton9985a272018-10-30 14:29:39 -0600179 if (HashNode* node = fHashTable.find(*fScratchKey)) {
Chris Dalton4da70192018-06-18 09:51:36 -0600180 entry = node->entry();
Chris Dalton9a986cf2018-10-18 15:27:59 -0600181 SkASSERT(fLRU.isInList(entry));
Chris Dalton351e80c2019-01-06 22:51:00 -0700182
Chris Dalton6c3879d2018-11-01 11:13:19 -0600183 if (!fuzzy_equals(m, entry->fMaskTransform)) {
184 // The path was reused with an incompatible matrix.
Chris Daltonaaa77c12019-01-07 17:45:36 -0700185 if (entry->unique()) {
Chris Dalton6c3879d2018-11-01 11:13:19 -0600186 // This entry is unique: recycle it instead of deleting and malloc-ing a new one.
Chris Dalton351e80c2019-01-06 22:51:00 -0700187 SkASSERT(0 == entry->fOnFlushRefCnt); // Because we are unique.
Chris Dalton6c3879d2018-11-01 11:13:19 -0600188 entry->fMaskTransform = m;
189 entry->fHitCount = 0;
Chris Daltonaaa77c12019-01-07 17:45:36 -0700190 entry->fHitRect = SkIRect::MakeEmpty();
Chris Dalton351e80c2019-01-06 22:51:00 -0700191 entry->releaseCachedAtlas(this);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600192 } else {
193 this->evict(*fScratchKey);
194 entry = nullptr;
195 }
Chris Dalton4da70192018-06-18 09:51:36 -0600196 }
197 }
198
199 if (!entry) {
Chris Dalton4da70192018-06-18 09:51:36 -0600200 if (fHashTable.count() >= kMaxCacheCount) {
Chris Dalton9985a272018-10-30 14:29:39 -0600201 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 Dalton4da70192018-06-18 09:51:36 -0600204 }
Chris Dalton9985a272018-10-30 14:29:39 -0600205
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 Dalton4da70192018-06-18 09:51:36 -0600213 SkASSERT(fHashTable.count() <= kMaxCacheCount);
214 } else {
215 fLRU.remove(entry); // Will be re-added at head.
216 }
217
Chris Dalton9985a272018-10-30 14:29:39 -0600218 SkDEBUGCODE(HashNode* node = fHashTable.find(*fScratchKey));
Chris Dalton3b572792018-10-23 18:26:20 -0600219 SkASSERT(node && node->entry() == entry);
Chris Dalton4da70192018-06-18 09:51:36 -0600220 fLRU.addToHead(entry);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600221
Chris Dalton351e80c2019-01-06 22:51:00 -0700222 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) {
Chris Dalton45f6b3d2019-05-21 12:06:03 -0600229 SkASSERT(SkToBool(entry->fCachedAtlas->peekOnFlushRefCnt()) ==
230 SkToBool(entry->fCachedAtlas->getOnFlushProxy()));
Chris Dalton351e80c2019-01-06 22:51:00 -0700231 if (!entry->fCachedAtlas->getOnFlushProxy()) {
Chris Dalton45f6b3d2019-05-21 12:06:03 -0600232 if (sk_sp<GrTextureProxy> onFlushProxy = onFlushRP->findOrCreateProxyByUniqueKey(
233 entry->fCachedAtlas->textureKey(), GrCCAtlas::kTextureOrigin)) {
234 onFlushProxy->priv().setIgnoredByResourceAllocator();
235 entry->fCachedAtlas->setOnFlushProxy(std::move(onFlushProxy));
236 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700237 }
238 if (!entry->fCachedAtlas->getOnFlushProxy()) {
239 // Our atlas's backing texture got purged from the GrResourceCache. Release the
240 // cached atlas.
241 entry->releaseCachedAtlas(this);
242 }
243 }
244 }
Chris Daltonaaa77c12019-01-07 17:45:36 -0700245 entry->fHitRect.join(clippedDrawBounds.makeOffset(-maskShift->x(), -maskShift->y()));
Chris Dalton351e80c2019-01-06 22:51:00 -0700246 SkASSERT(!entry->fCachedAtlas || entry->fCachedAtlas->getOnFlushProxy());
247 return OnFlushEntryRef::OnFlushRef(entry);
Chris Dalton4da70192018-06-18 09:51:36 -0600248}
249
Chris Dalton351e80c2019-01-06 22:51:00 -0700250void GrCCPathCache::evict(const GrCCPathCache::Key& key, GrCCPathCacheEntry* entry) {
251 if (!entry) {
252 HashNode* node = fHashTable.find(key);
253 SkASSERT(node);
254 entry = node->entry();
255 }
256 SkASSERT(*entry->fCacheKey == key);
257 SkASSERT(!entry->hasBeenEvicted());
258 entry->fCacheKey->markShouldUnregisterFromPath(); // Unregister the path listener.
259 entry->releaseCachedAtlas(this);
260 fLRU.remove(entry);
261 fHashTable.remove(key);
262}
263
264void GrCCPathCache::doPreFlushProcessing() {
265 this->evictInvalidatedCacheKeys();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600266
267 // Mark the per-flush timestamp as needing to be updated with a newer clock reading.
268 fPerFlushTimestamp = GrStdSteadyClock::time_point::min();
269}
270
Chris Dalton351e80c2019-01-06 22:51:00 -0700271void GrCCPathCache::purgeEntriesOlderThan(GrProxyProvider* proxyProvider,
272 const GrStdSteadyClock::time_point& purgeTime) {
273 this->evictInvalidatedCacheKeys();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600274
275#ifdef SK_DEBUG
276 auto lastTimestamp = (fLRU.isEmpty())
277 ? GrStdSteadyClock::time_point::max()
278 : fLRU.tail()->fTimestamp;
279#endif
280
Chris Dalton351e80c2019-01-06 22:51:00 -0700281 // Evict every entry from our local path cache whose timestamp is older than purgeTime.
Chris Dalton6c3879d2018-11-01 11:13:19 -0600282 while (!fLRU.isEmpty() && fLRU.tail()->fTimestamp < purgeTime) {
283#ifdef SK_DEBUG
284 // Verify that fLRU is sorted by timestamp.
285 auto timestamp = fLRU.tail()->fTimestamp;
286 SkASSERT(timestamp >= lastTimestamp);
287 lastTimestamp = timestamp;
288#endif
289 this->evict(*fLRU.tail()->fCacheKey);
290 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700291
292 // Now take all the atlas textures we just invalidated and purge them from the GrResourceCache.
293 this->purgeInvalidatedAtlasTextures(proxyProvider);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600294}
295
Chris Dalton351e80c2019-01-06 22:51:00 -0700296void GrCCPathCache::purgeInvalidatedAtlasTextures(GrOnFlushResourceProvider* onFlushRP) {
297 for (sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) {
298 onFlushRP->removeUniqueKeyFromProxy(proxy.get());
299 }
300 fInvalidatedProxies.reset();
301
302 for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) {
303 onFlushRP->processInvalidUniqueKey(key);
304 }
305 fInvalidatedProxyUniqueKeys.reset();
306}
307
308void GrCCPathCache::purgeInvalidatedAtlasTextures(GrProxyProvider* proxyProvider) {
309 for (sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) {
310 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
311 }
312 fInvalidatedProxies.reset();
313
314 for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) {
315 proxyProvider->processInvalidUniqueKey(key, nullptr,
316 GrProxyProvider::InvalidateGPUResource::kYes);
317 }
318 fInvalidatedProxyUniqueKeys.reset();
319}
320
321void GrCCPathCache::evictInvalidatedCacheKeys() {
Chris Dalton9985a272018-10-30 14:29:39 -0600322 SkTArray<sk_sp<Key>> invalidatedKeys;
323 fInvalidatedKeysInbox.poll(&invalidatedKeys);
324 for (const sk_sp<Key>& key : invalidatedKeys) {
325 bool isInCache = !key->shouldUnregisterFromPath(); // Gets set upon exiting the cache.
326 if (isInCache) {
327 this->evict(*key);
328 }
Chris Dalton9a986cf2018-10-18 15:27:59 -0600329 }
Chris Dalton4da70192018-06-18 09:51:36 -0600330}
331
Chris Dalton351e80c2019-01-06 22:51:00 -0700332GrCCPathCache::OnFlushEntryRef
333GrCCPathCache::OnFlushEntryRef::OnFlushRef(GrCCPathCacheEntry* entry) {
334 entry->ref();
335 ++entry->fOnFlushRefCnt;
336 if (entry->fCachedAtlas) {
337 entry->fCachedAtlas->incrOnFlushRefCnt();
338 }
339 return OnFlushEntryRef(entry);
340}
Chris Dalton907102e2018-06-29 13:18:53 -0600341
Chris Dalton351e80c2019-01-06 22:51:00 -0700342GrCCPathCache::OnFlushEntryRef::~OnFlushEntryRef() {
343 if (!fEntry) {
344 return;
345 }
346 --fEntry->fOnFlushRefCnt;
347 SkASSERT(fEntry->fOnFlushRefCnt >= 0);
348 if (fEntry->fCachedAtlas) {
349 fEntry->fCachedAtlas->decrOnFlushRefCnt();
350 }
351 fEntry->unref();
352}
Chris Dalton4da70192018-06-18 09:51:36 -0600353
Chris Dalton351e80c2019-01-06 22:51:00 -0700354
355void GrCCPathCacheEntry::setCoverageCountAtlas(
356 GrOnFlushResourceProvider* onFlushRP, GrCCAtlas* atlas, const SkIVector& atlasOffset,
Chris Dalton8610e9c2019-05-09 11:07:10 -0600357 const GrOctoBounds& octoBounds, const SkIRect& devIBounds, const SkIVector& maskShift) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700358 SkASSERT(fOnFlushRefCnt > 0);
359 SkASSERT(!fCachedAtlas); // Otherwise we would need to call releaseCachedAtlas().
360
361 if (this->hasBeenEvicted()) {
362 // This entry will never be found in the path cache again. Don't bother trying to save an
363 // atlas texture for it in the GrResourceCache.
364 return;
365 }
366
367 fCachedAtlas = atlas->refOrMakeCachedAtlas(onFlushRP);
368 fCachedAtlas->incrOnFlushRefCnt(fOnFlushRefCnt);
369 fCachedAtlas->addPathPixels(devIBounds.height() * devIBounds.width());
370
Chris Dalton4da70192018-06-18 09:51:36 -0600371 fAtlasOffset = atlasOffset + maskShift;
Chris Dalton4da70192018-06-18 09:51:36 -0600372
Chris Dalton8610e9c2019-05-09 11:07:10 -0600373 fOctoBounds.setOffset(octoBounds, -maskShift.fX, -maskShift.fY);
Chris Dalton4da70192018-06-18 09:51:36 -0600374 fDevIBounds = devIBounds.makeOffset(-maskShift.fX, -maskShift.fY);
375}
376
Chris Dalton351e80c2019-01-06 22:51:00 -0700377GrCCPathCacheEntry::ReleaseAtlasResult GrCCPathCacheEntry::upgradeToLiteralCoverageAtlas(
378 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCAtlas* atlas,
379 const SkIVector& newAtlasOffset) {
380 SkASSERT(!this->hasBeenEvicted());
381 SkASSERT(fOnFlushRefCnt > 0);
382 SkASSERT(fCachedAtlas);
Chris Daltonc3318f02019-07-19 14:20:53 -0600383 SkASSERT(GrCCAtlas::CoverageType::kA8_LiteralCoverage != fCachedAtlas->coverageType());
Chris Dalton4da70192018-06-18 09:51:36 -0600384
Chris Dalton351e80c2019-01-06 22:51:00 -0700385 ReleaseAtlasResult releaseAtlasResult = this->releaseCachedAtlas(pathCache);
386
387 fCachedAtlas = atlas->refOrMakeCachedAtlas(onFlushRP);
388 fCachedAtlas->incrOnFlushRefCnt(fOnFlushRefCnt);
389 fCachedAtlas->addPathPixels(this->height() * this->width());
390
Chris Dalton4da70192018-06-18 09:51:36 -0600391 fAtlasOffset = newAtlasOffset;
Chris Dalton351e80c2019-01-06 22:51:00 -0700392 return releaseAtlasResult;
Chris Dalton4da70192018-06-18 09:51:36 -0600393}
394
Chris Dalton351e80c2019-01-06 22:51:00 -0700395GrCCPathCacheEntry::ReleaseAtlasResult GrCCPathCacheEntry::releaseCachedAtlas(
396 GrCCPathCache* pathCache) {
397 ReleaseAtlasResult result = ReleaseAtlasResult::kNone;
398 if (fCachedAtlas) {
399 result = fCachedAtlas->invalidatePathPixels(pathCache, this->height() * this->width());
400 if (fOnFlushRefCnt) {
401 SkASSERT(fOnFlushRefCnt > 0);
402 fCachedAtlas->decrOnFlushRefCnt(fOnFlushRefCnt);
Chris Dalton907102e2018-06-29 13:18:53 -0600403 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700404 fCachedAtlas = nullptr;
Chris Dalton907102e2018-06-29 13:18:53 -0600405 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700406 return result;
407}
Chris Dalton907102e2018-06-29 13:18:53 -0600408
Chris Dalton351e80c2019-01-06 22:51:00 -0700409GrCCPathCacheEntry::ReleaseAtlasResult GrCCCachedAtlas::invalidatePathPixels(
410 GrCCPathCache* pathCache, int numPixels) {
411 // Mark the pixels invalid in the cached atlas texture.
412 fNumInvalidatedPathPixels += numPixels;
413 SkASSERT(fNumInvalidatedPathPixels <= fNumPathPixels);
414 if (!fIsInvalidatedFromResourceCache && fNumInvalidatedPathPixels >= fNumPathPixels / 2) {
415 // Too many invalidated pixels: purge the atlas texture from the resource cache.
416 if (fOnFlushProxy) {
417 // Don't clear (or std::move) fOnFlushProxy. Other path cache entries might still have a
418 // reference on this atlas and expect to use our proxy during the current flush.
419 // fOnFlushProxy will be cleared once fOnFlushRefCnt decrements to zero.
420 pathCache->fInvalidatedProxies.push_back(fOnFlushProxy);
421 } else {
422 pathCache->fInvalidatedProxyUniqueKeys.push_back(fTextureKey);
423 }
424 fIsInvalidatedFromResourceCache = true;
425 return ReleaseAtlasResult::kDidInvalidateFromCache;
426 }
427 return ReleaseAtlasResult::kNone;
428}
429
430void GrCCCachedAtlas::decrOnFlushRefCnt(int count) const {
431 SkASSERT(count > 0);
432 fOnFlushRefCnt -= count;
433 SkASSERT(fOnFlushRefCnt >= 0);
434 if (0 == fOnFlushRefCnt) {
435 // Don't hold the actual proxy past the end of the current flush.
436 SkASSERT(fOnFlushProxy);
437 fOnFlushProxy = nullptr;
438 }
Chris Dalton907102e2018-06-29 13:18:53 -0600439}