joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 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 "GrTextBlobCache.h" |
| 9 | |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 10 | DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage) |
| 11 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 12 | GrTextBlobCache::~GrTextBlobCache() { |
Robert Phillips | 303cd58 | 2018-02-14 18:54:01 -0500 | [diff] [blame] | 13 | this->freeAll(); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 14 | } |
| 15 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 16 | void GrTextBlobCache::freeAll() { |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 17 | fBlobIDCache.foreach([this](uint32_t, BlobIDCacheEntry* entry) { |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 18 | for (const auto& blob : entry->fBlobs) { |
| 19 | fBlobList.remove(blob.get()); |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 20 | } |
| 21 | }); |
| 22 | |
| 23 | fBlobIDCache.reset(); |
joshualitt | 20ccd40 | 2016-01-05 08:56:56 -0800 | [diff] [blame] | 24 | |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame^] | 25 | fCurrentSize = 0; |
| 26 | |
joshualitt | 20ccd40 | 2016-01-05 08:56:56 -0800 | [diff] [blame] | 27 | // There should be no allocations in the memory pool at this point |
Florin Malita | 33fdb8d | 2017-03-07 16:51:57 -0500 | [diff] [blame] | 28 | SkASSERT(fBlobList.isEmpty()); |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 29 | } |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 30 | |
Jim Van Verth | 474d687 | 2017-12-14 13:00:05 -0500 | [diff] [blame] | 31 | void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) { |
| 32 | SkASSERT(blobID != SK_InvalidGenID); |
| 33 | SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage({blobID}), cacheID); |
Florin Malita | 4a01ac9 | 2017-03-13 16:45:28 -0400 | [diff] [blame] | 34 | } |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 35 | |
| 36 | void GrTextBlobCache::purgeStaleBlobs() { |
| 37 | SkTArray<PurgeBlobMessage> msgs; |
| 38 | fPurgeBlobInbox.poll(&msgs); |
| 39 | |
| 40 | for (const auto& msg : msgs) { |
| 41 | auto* idEntry = fBlobIDCache.find(msg.fID); |
| 42 | if (!idEntry) { |
| 43 | // no cache entries for id |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | // remove all blob entries from the LRU list |
| 48 | for (const auto& blob : idEntry->fBlobs) { |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame^] | 49 | fCurrentSize -= blob->size(); |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 50 | fBlobList.remove(blob.get()); |
| 51 | } |
| 52 | |
| 53 | // drop the idEntry itself (unrefs all blobs) |
| 54 | fBlobIDCache.remove(msg.fID); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void GrTextBlobCache::checkPurge(GrAtlasTextBlob* blob) { |
| 59 | // First, purge all stale blob IDs. |
| 60 | this->purgeStaleBlobs(); |
| 61 | |
| 62 | // If we are still over budget, then unref until we are below budget again |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame^] | 63 | if (fCurrentSize > fSizeBudget) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 64 | BitmapBlobList::Iter iter; |
| 65 | iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
| 66 | GrAtlasTextBlob* lruBlob = nullptr; |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame^] | 67 | while (fCurrentSize > fSizeBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 68 | // Backup the iterator before removing and unrefing the blob |
| 69 | iter.prev(); |
| 70 | |
| 71 | this->remove(lruBlob); |
| 72 | } |
| 73 | |
| 74 | // If we break out of the loop with lruBlob == blob, then we haven't purged enough |
| 75 | // use the call back and try to free some more. If we are still overbudget after this, |
| 76 | // then this single textblob is over our budget |
| 77 | if (blob && lruBlob == blob) { |
| 78 | (*fCallback)(fData); |
| 79 | } |
| 80 | |
| 81 | #ifdef SPEW_BUDGET_MESSAGE |
Herb Derby | b12175f | 2018-05-23 16:38:09 -0400 | [diff] [blame^] | 82 | if (fCurrentSize > fSizeBudget) { |
Jim Van Verth | 76d917c | 2017-12-13 09:26:37 -0500 | [diff] [blame] | 83 | SkDebugf("Single textblob is larger than our whole budget"); |
| 84 | } |
| 85 | #endif |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |