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