blob: 4f16d80de2946fb2a388636aeb27d0b1a900fc18 [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
Robert Phillipse7a959d2021-03-11 14:44:42 -050016DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<GrCCPathCache::Key>, uint32_t, true);
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
Andy Weiss8ef18d32019-08-29 10:36:58 -070078void GrCCPathCache::Key::operator delete(void* p) { ::operator delete(p); }
79
Chris Dalton9985a272018-10-30 14:29:39 -060080const uint32_t* GrCCPathCache::Key::data() const {
81 // The shape key is a variable-length footer to the entry allocation.
82 return reinterpret_cast<const uint32_t*>(reinterpret_cast<const char*>(this) + sizeof(Key));
83}
84
85uint32_t* GrCCPathCache::Key::data() {
86 // The shape key is a variable-length footer to the entry allocation.
87 return reinterpret_cast<uint32_t*>(reinterpret_cast<char*>(this) + sizeof(Key));
88}
89
Brian Salomon99a813c2020-03-02 12:50:47 -050090void GrCCPathCache::Key::changed() {
Chris Dalton9985a272018-10-30 14:29:39 -060091 // Our key's corresponding path was invalidated. Post a thread-safe eviction message.
Robert Phillipse7a959d2021-03-11 14:44:42 -050092 SkMessageBus<sk_sp<Key>, uint32_t>::Post(sk_ref_sp(this));
Chris Dalton9985a272018-10-30 14:29:39 -060093}
94
Chris Dalton351e80c2019-01-06 22:51:00 -070095GrCCPathCache::GrCCPathCache(uint32_t contextUniqueID)
96 : fContextUniqueID(contextUniqueID)
97 , fInvalidatedKeysInbox(next_path_cache_id())
Chris Dalton9985a272018-10-30 14:29:39 -060098 , fScratchKey(Key::Make(fInvalidatedKeysInbox.uniqueID(), kMaxKeyDataCountU32)) {
99}
100
101GrCCPathCache::~GrCCPathCache() {
Chris Dalton351e80c2019-01-06 22:51:00 -0700102 while (!fLRU.isEmpty()) {
103 this->evict(*fLRU.tail()->fCacheKey, fLRU.tail());
104 }
105 SkASSERT(0 == fHashTable.count()); // Ensure the hash table and LRU list were coherent.
106
107 // Now take all the atlas textures we just invalidated and purge them from the GrResourceCache.
108 // We just purge via message bus since we don't have any access to the resource cache right now.
John Stilesbd3ffa42020-07-30 20:24:57 -0400109 for (const sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) {
Robert Phillipse7a959d2021-03-11 14:44:42 -0500110 SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(
Chris Dalton351e80c2019-01-06 22:51:00 -0700111 GrUniqueKeyInvalidatedMessage(proxy->getUniqueKey(), fContextUniqueID));
112 }
113 for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) {
Robert Phillipse7a959d2021-03-11 14:44:42 -0500114 SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(
Chris Dalton351e80c2019-01-06 22:51:00 -0700115 GrUniqueKeyInvalidatedMessage(key, fContextUniqueID));
116 }
Chris Dalton9985a272018-10-30 14:29:39 -0600117}
118
Chris Dalton8f8bf882018-07-18 10:55:51 -0600119namespace {
120
121// 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 -0600122class WriteKeyHelper {
Chris Dalton8f8bf882018-07-18 10:55:51 -0600123public:
Chris Dalton9985a272018-10-30 14:29:39 -0600124 static constexpr int kStrokeWidthIdx = 0;
125 static constexpr int kStrokeMiterIdx = 1;
126 static constexpr int kStrokeCapJoinIdx = 2;
127 static constexpr int kShapeUnstyledKeyIdx = 3;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800128
Michael Ludwig2686d692020-04-17 20:21:37 +0000129 WriteKeyHelper(const GrStyledShape& shape) : fShapeUnstyledKeyCount(shape.unstyledKeySize()) {}
Chris Dalton8f8bf882018-07-18 10:55:51 -0600130
131 // Returns the total number of uint32_t's to allocate for the key.
Chris Dalton09a7bb22018-08-31 19:53:15 +0800132 int allocCountU32() const { return kShapeUnstyledKeyIdx + fShapeUnstyledKeyCount; }
Chris Dalton8f8bf882018-07-18 10:55:51 -0600133
Chris Dalton9985a272018-10-30 14:29:39 -0600134 // Writes the key data to out[].
Michael Ludwig2686d692020-04-17 20:21:37 +0000135 void write(const GrStyledShape& shape, uint32_t* out) {
Chris Dalton09a7bb22018-08-31 19:53:15 +0800136 // Stroke key.
137 // We don't use GrStyle::WriteKey() because it does not account for hairlines.
138 // http://skbug.com/8273
139 SkASSERT(!shape.style().hasPathEffect());
140 const SkStrokeRec& stroke = shape.style().strokeRec();
141 if (stroke.isFillStyle()) {
142 // Use a value for width that won't collide with a valid fp32 value >= 0.
143 out[kStrokeWidthIdx] = ~0;
144 out[kStrokeMiterIdx] = out[kStrokeCapJoinIdx] = 0;
145 } else {
146 float width = stroke.getWidth(), miterLimit = stroke.getMiter();
147 memcpy(&out[kStrokeWidthIdx], &width, sizeof(float));
148 memcpy(&out[kStrokeMiterIdx], &miterLimit, sizeof(float));
149 out[kStrokeCapJoinIdx] = (stroke.getCap() << 16) | stroke.getJoin();
Brian Salomon4dea72a2019-12-18 10:43:10 -0500150 static_assert(sizeof(out[kStrokeWidthIdx]) == sizeof(float));
Chris Dalton09a7bb22018-08-31 19:53:15 +0800151 }
152
153 // Shape unstyled key.
154 shape.writeUnstyledKey(&out[kShapeUnstyledKeyIdx]);
Chris Dalton8f8bf882018-07-18 10:55:51 -0600155 }
156
157private:
158 int fShapeUnstyledKeyCount;
Chris Dalton8f8bf882018-07-18 10:55:51 -0600159};
160
John Stilesa6841be2020-08-06 14:11:56 -0400161} // namespace
Chris Dalton8f8bf882018-07-18 10:55:51 -0600162
Chris Daltonaaa77c12019-01-07 17:45:36 -0700163GrCCPathCache::OnFlushEntryRef GrCCPathCache::find(
Michael Ludwig2686d692020-04-17 20:21:37 +0000164 GrOnFlushResourceProvider* onFlushRP, const GrStyledShape& shape,
Chris Daltonaaa77c12019-01-07 17:45:36 -0700165 const SkIRect& clippedDrawBounds, const SkMatrix& viewMatrix, SkIVector* maskShift) {
Chris Dalton4da70192018-06-18 09:51:36 -0600166 if (!shape.hasUnstyledKey()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700167 return OnFlushEntryRef();
Chris Dalton4da70192018-06-18 09:51:36 -0600168 }
169
Chris Dalton9985a272018-10-30 14:29:39 -0600170 WriteKeyHelper writeKeyHelper(shape);
171 if (writeKeyHelper.allocCountU32() > kMaxKeyDataCountU32) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700172 return OnFlushEntryRef();
Chris Dalton9985a272018-10-30 14:29:39 -0600173 }
174
175 SkASSERT(fScratchKey->unique());
176 fScratchKey->resetDataCountU32(writeKeyHelper.allocCountU32());
177 writeKeyHelper.write(shape, fScratchKey->data());
Chris Dalton4da70192018-06-18 09:51:36 -0600178
Chris Daltonaaa77c12019-01-07 17:45:36 -0700179 MaskTransform m(viewMatrix, maskShift);
Chris Dalton4da70192018-06-18 09:51:36 -0600180 GrCCPathCacheEntry* entry = nullptr;
Chris Dalton9985a272018-10-30 14:29:39 -0600181 if (HashNode* node = fHashTable.find(*fScratchKey)) {
Chris Dalton4da70192018-06-18 09:51:36 -0600182 entry = node->entry();
Chris Dalton9a986cf2018-10-18 15:27:59 -0600183 SkASSERT(fLRU.isInList(entry));
Chris Dalton351e80c2019-01-06 22:51:00 -0700184
Chris Dalton6c3879d2018-11-01 11:13:19 -0600185 if (!fuzzy_equals(m, entry->fMaskTransform)) {
186 // The path was reused with an incompatible matrix.
Chris Daltonaaa77c12019-01-07 17:45:36 -0700187 if (entry->unique()) {
Chris Dalton6c3879d2018-11-01 11:13:19 -0600188 // This entry is unique: recycle it instead of deleting and malloc-ing a new one.
Chris Dalton351e80c2019-01-06 22:51:00 -0700189 SkASSERT(0 == entry->fOnFlushRefCnt); // Because we are unique.
Chris Dalton6c3879d2018-11-01 11:13:19 -0600190 entry->fMaskTransform = m;
191 entry->fHitCount = 0;
Chris Daltonaaa77c12019-01-07 17:45:36 -0700192 entry->fHitRect = SkIRect::MakeEmpty();
Chris Dalton351e80c2019-01-06 22:51:00 -0700193 entry->releaseCachedAtlas(this);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600194 } else {
195 this->evict(*fScratchKey);
196 entry = nullptr;
197 }
Chris Dalton4da70192018-06-18 09:51:36 -0600198 }
199 }
200
201 if (!entry) {
Chris Dalton4da70192018-06-18 09:51:36 -0600202 if (fHashTable.count() >= kMaxCacheCount) {
Chris Dalton9985a272018-10-30 14:29:39 -0600203 SkDEBUGCODE(HashNode* node = fHashTable.find(*fLRU.tail()->fCacheKey));
204 SkASSERT(node && node->entry() == fLRU.tail());
205 this->evict(*fLRU.tail()->fCacheKey); // We've exceeded our limit.
Chris Dalton4da70192018-06-18 09:51:36 -0600206 }
Chris Dalton9985a272018-10-30 14:29:39 -0600207
208 // Create a new entry in the cache.
209 sk_sp<Key> permanentKey = Key::Make(fInvalidatedKeysInbox.uniqueID(),
210 writeKeyHelper.allocCountU32(), fScratchKey->data());
211 SkASSERT(*permanentKey == *fScratchKey);
212 SkASSERT(!fHashTable.find(*permanentKey));
213 entry = fHashTable.set(HashNode(this, std::move(permanentKey), m, shape))->entry();
214
Chris Dalton4da70192018-06-18 09:51:36 -0600215 SkASSERT(fHashTable.count() <= kMaxCacheCount);
216 } else {
217 fLRU.remove(entry); // Will be re-added at head.
218 }
219
Chris Dalton9985a272018-10-30 14:29:39 -0600220 SkDEBUGCODE(HashNode* node = fHashTable.find(*fScratchKey));
Chris Dalton3b572792018-10-23 18:26:20 -0600221 SkASSERT(node && node->entry() == entry);
Chris Dalton4da70192018-06-18 09:51:36 -0600222 fLRU.addToHead(entry);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600223
Chris Dalton351e80c2019-01-06 22:51:00 -0700224 if (0 == entry->fOnFlushRefCnt) {
225 // Only update the time stamp and hit count if we haven't seen this entry yet during the
226 // current flush.
227 entry->fTimestamp = this->quickPerFlushTimestamp();
228 ++entry->fHitCount;
229
230 if (entry->fCachedAtlas) {
Chris Dalton45f6b3d2019-05-21 12:06:03 -0600231 SkASSERT(SkToBool(entry->fCachedAtlas->peekOnFlushRefCnt()) ==
232 SkToBool(entry->fCachedAtlas->getOnFlushProxy()));
Chris Dalton351e80c2019-01-06 22:51:00 -0700233 if (!entry->fCachedAtlas->getOnFlushProxy()) {
Chris Dalton45f6b3d2019-05-21 12:06:03 -0600234 if (sk_sp<GrTextureProxy> onFlushProxy = onFlushRP->findOrCreateProxyByUniqueKey(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400235 entry->fCachedAtlas->textureKey(), GrSurfaceProxy::UseAllocator::kNo)) {
Chris Dalton45f6b3d2019-05-21 12:06:03 -0600236 entry->fCachedAtlas->setOnFlushProxy(std::move(onFlushProxy));
237 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700238 }
239 if (!entry->fCachedAtlas->getOnFlushProxy()) {
240 // Our atlas's backing texture got purged from the GrResourceCache. Release the
241 // cached atlas.
242 entry->releaseCachedAtlas(this);
243 }
244 }
245 }
Brian Salomon9bd947d2019-10-03 14:57:07 -0400246 entry->fHitRect.join(clippedDrawBounds.makeOffset(-*maskShift));
Chris Dalton351e80c2019-01-06 22:51:00 -0700247 SkASSERT(!entry->fCachedAtlas || entry->fCachedAtlas->getOnFlushProxy());
248 return OnFlushEntryRef::OnFlushRef(entry);
Chris Dalton4da70192018-06-18 09:51:36 -0600249}
250
Chris Dalton351e80c2019-01-06 22:51:00 -0700251void GrCCPathCache::evict(const GrCCPathCache::Key& key, GrCCPathCacheEntry* entry) {
252 if (!entry) {
253 HashNode* node = fHashTable.find(key);
254 SkASSERT(node);
255 entry = node->entry();
256 }
257 SkASSERT(*entry->fCacheKey == key);
258 SkASSERT(!entry->hasBeenEvicted());
Brian Salomon99a813c2020-03-02 12:50:47 -0500259 entry->fCacheKey->markShouldDeregister(); // Unregister the path listener.
Chris Dalton351e80c2019-01-06 22:51:00 -0700260 entry->releaseCachedAtlas(this);
261 fLRU.remove(entry);
262 fHashTable.remove(key);
263}
264
265void GrCCPathCache::doPreFlushProcessing() {
266 this->evictInvalidatedCacheKeys();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600267
268 // Mark the per-flush timestamp as needing to be updated with a newer clock reading.
269 fPerFlushTimestamp = GrStdSteadyClock::time_point::min();
270}
271
Chris Dalton351e80c2019-01-06 22:51:00 -0700272void GrCCPathCache::purgeEntriesOlderThan(GrProxyProvider* proxyProvider,
273 const GrStdSteadyClock::time_point& purgeTime) {
274 this->evictInvalidatedCacheKeys();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600275
276#ifdef SK_DEBUG
277 auto lastTimestamp = (fLRU.isEmpty())
278 ? GrStdSteadyClock::time_point::max()
279 : fLRU.tail()->fTimestamp;
280#endif
281
Chris Dalton351e80c2019-01-06 22:51:00 -0700282 // Evict every entry from our local path cache whose timestamp is older than purgeTime.
Chris Dalton6c3879d2018-11-01 11:13:19 -0600283 while (!fLRU.isEmpty() && fLRU.tail()->fTimestamp < purgeTime) {
284#ifdef SK_DEBUG
285 // Verify that fLRU is sorted by timestamp.
286 auto timestamp = fLRU.tail()->fTimestamp;
287 SkASSERT(timestamp >= lastTimestamp);
288 lastTimestamp = timestamp;
289#endif
290 this->evict(*fLRU.tail()->fCacheKey);
291 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700292
293 // Now take all the atlas textures we just invalidated and purge them from the GrResourceCache.
294 this->purgeInvalidatedAtlasTextures(proxyProvider);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600295}
296
Chris Dalton351e80c2019-01-06 22:51:00 -0700297void GrCCPathCache::purgeInvalidatedAtlasTextures(GrOnFlushResourceProvider* onFlushRP) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400298 for (const sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700299 onFlushRP->removeUniqueKeyFromProxy(proxy.get());
300 }
301 fInvalidatedProxies.reset();
302
303 for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) {
304 onFlushRP->processInvalidUniqueKey(key);
305 }
306 fInvalidatedProxyUniqueKeys.reset();
307}
308
309void GrCCPathCache::purgeInvalidatedAtlasTextures(GrProxyProvider* proxyProvider) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400310 for (const sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700311 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
312 }
313 fInvalidatedProxies.reset();
314
315 for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) {
316 proxyProvider->processInvalidUniqueKey(key, nullptr,
317 GrProxyProvider::InvalidateGPUResource::kYes);
318 }
319 fInvalidatedProxyUniqueKeys.reset();
320}
321
322void GrCCPathCache::evictInvalidatedCacheKeys() {
Chris Dalton9985a272018-10-30 14:29:39 -0600323 SkTArray<sk_sp<Key>> invalidatedKeys;
324 fInvalidatedKeysInbox.poll(&invalidatedKeys);
325 for (const sk_sp<Key>& key : invalidatedKeys) {
Brian Salomon99a813c2020-03-02 12:50:47 -0500326 bool isInCache = !key->shouldDeregister(); // Gets set upon exiting the cache.
Chris Dalton9985a272018-10-30 14:29:39 -0600327 if (isInCache) {
328 this->evict(*key);
329 }
Chris Dalton9a986cf2018-10-18 15:27:59 -0600330 }
Chris Dalton4da70192018-06-18 09:51:36 -0600331}
332
Chris Dalton351e80c2019-01-06 22:51:00 -0700333GrCCPathCache::OnFlushEntryRef
334GrCCPathCache::OnFlushEntryRef::OnFlushRef(GrCCPathCacheEntry* entry) {
335 entry->ref();
336 ++entry->fOnFlushRefCnt;
337 if (entry->fCachedAtlas) {
338 entry->fCachedAtlas->incrOnFlushRefCnt();
339 }
340 return OnFlushEntryRef(entry);
341}
Chris Dalton907102e2018-06-29 13:18:53 -0600342
Chris Dalton351e80c2019-01-06 22:51:00 -0700343GrCCPathCache::OnFlushEntryRef::~OnFlushEntryRef() {
344 if (!fEntry) {
345 return;
346 }
347 --fEntry->fOnFlushRefCnt;
348 SkASSERT(fEntry->fOnFlushRefCnt >= 0);
349 if (fEntry->fCachedAtlas) {
350 fEntry->fCachedAtlas->decrOnFlushRefCnt();
351 }
352 fEntry->unref();
353}
Chris Dalton4da70192018-06-18 09:51:36 -0600354
Chris Dalton351e80c2019-01-06 22:51:00 -0700355
356void GrCCPathCacheEntry::setCoverageCountAtlas(
357 GrOnFlushResourceProvider* onFlushRP, GrCCAtlas* atlas, const SkIVector& atlasOffset,
Chris Dalton8610e9c2019-05-09 11:07:10 -0600358 const GrOctoBounds& octoBounds, const SkIRect& devIBounds, const SkIVector& maskShift) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700359 SkASSERT(fOnFlushRefCnt > 0);
360 SkASSERT(!fCachedAtlas); // Otherwise we would need to call releaseCachedAtlas().
361
362 if (this->hasBeenEvicted()) {
363 // This entry will never be found in the path cache again. Don't bother trying to save an
364 // atlas texture for it in the GrResourceCache.
365 return;
366 }
367
368 fCachedAtlas = atlas->refOrMakeCachedAtlas(onFlushRP);
369 fCachedAtlas->incrOnFlushRefCnt(fOnFlushRefCnt);
370 fCachedAtlas->addPathPixels(devIBounds.height() * devIBounds.width());
371
Chris Dalton4da70192018-06-18 09:51:36 -0600372 fAtlasOffset = atlasOffset + maskShift;
Chris Dalton4da70192018-06-18 09:51:36 -0600373
Chris Dalton8610e9c2019-05-09 11:07:10 -0600374 fOctoBounds.setOffset(octoBounds, -maskShift.fX, -maskShift.fY);
Brian Salomon9bd947d2019-10-03 14:57:07 -0400375 fDevIBounds = devIBounds.makeOffset(-maskShift);
Chris Dalton4da70192018-06-18 09:51:36 -0600376}
377
Chris Dalton351e80c2019-01-06 22:51:00 -0700378GrCCPathCacheEntry::ReleaseAtlasResult GrCCPathCacheEntry::upgradeToLiteralCoverageAtlas(
379 GrCCPathCache* pathCache, GrOnFlushResourceProvider* onFlushRP, GrCCAtlas* atlas,
380 const SkIVector& newAtlasOffset) {
381 SkASSERT(!this->hasBeenEvicted());
382 SkASSERT(fOnFlushRefCnt > 0);
383 SkASSERT(fCachedAtlas);
Chris Daltonc3318f02019-07-19 14:20:53 -0600384 SkASSERT(GrCCAtlas::CoverageType::kA8_LiteralCoverage != fCachedAtlas->coverageType());
Chris Dalton4da70192018-06-18 09:51:36 -0600385
Chris Dalton351e80c2019-01-06 22:51:00 -0700386 ReleaseAtlasResult releaseAtlasResult = this->releaseCachedAtlas(pathCache);
387
388 fCachedAtlas = atlas->refOrMakeCachedAtlas(onFlushRP);
389 fCachedAtlas->incrOnFlushRefCnt(fOnFlushRefCnt);
390 fCachedAtlas->addPathPixels(this->height() * this->width());
391
Chris Dalton4da70192018-06-18 09:51:36 -0600392 fAtlasOffset = newAtlasOffset;
Chris Dalton351e80c2019-01-06 22:51:00 -0700393 return releaseAtlasResult;
Chris Dalton4da70192018-06-18 09:51:36 -0600394}
395
Chris Dalton351e80c2019-01-06 22:51:00 -0700396GrCCPathCacheEntry::ReleaseAtlasResult GrCCPathCacheEntry::releaseCachedAtlas(
397 GrCCPathCache* pathCache) {
398 ReleaseAtlasResult result = ReleaseAtlasResult::kNone;
399 if (fCachedAtlas) {
400 result = fCachedAtlas->invalidatePathPixels(pathCache, this->height() * this->width());
401 if (fOnFlushRefCnt) {
402 SkASSERT(fOnFlushRefCnt > 0);
403 fCachedAtlas->decrOnFlushRefCnt(fOnFlushRefCnt);
Chris Dalton907102e2018-06-29 13:18:53 -0600404 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700405 fCachedAtlas = nullptr;
Chris Dalton907102e2018-06-29 13:18:53 -0600406 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700407 return result;
408}
Chris Dalton907102e2018-06-29 13:18:53 -0600409
Chris Dalton351e80c2019-01-06 22:51:00 -0700410GrCCPathCacheEntry::ReleaseAtlasResult GrCCCachedAtlas::invalidatePathPixels(
411 GrCCPathCache* pathCache, int numPixels) {
412 // Mark the pixels invalid in the cached atlas texture.
413 fNumInvalidatedPathPixels += numPixels;
414 SkASSERT(fNumInvalidatedPathPixels <= fNumPathPixels);
415 if (!fIsInvalidatedFromResourceCache && fNumInvalidatedPathPixels >= fNumPathPixels / 2) {
416 // Too many invalidated pixels: purge the atlas texture from the resource cache.
417 if (fOnFlushProxy) {
418 // Don't clear (or std::move) fOnFlushProxy. Other path cache entries might still have a
419 // reference on this atlas and expect to use our proxy during the current flush.
420 // fOnFlushProxy will be cleared once fOnFlushRefCnt decrements to zero.
421 pathCache->fInvalidatedProxies.push_back(fOnFlushProxy);
422 } else {
423 pathCache->fInvalidatedProxyUniqueKeys.push_back(fTextureKey);
424 }
425 fIsInvalidatedFromResourceCache = true;
426 return ReleaseAtlasResult::kDidInvalidateFromCache;
427 }
428 return ReleaseAtlasResult::kNone;
429}
430
431void GrCCCachedAtlas::decrOnFlushRefCnt(int count) const {
432 SkASSERT(count > 0);
433 fOnFlushRefCnt -= count;
434 SkASSERT(fOnFlushRefCnt >= 0);
435 if (0 == fOnFlushRefCnt) {
436 // Don't hold the actual proxy past the end of the current flush.
437 SkASSERT(fOnFlushProxy);
438 fOnFlushProxy = nullptr;
439 }
Chris Dalton907102e2018-06-29 13:18:53 -0600440}