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 | #ifndef GrTextBlobCache_DEFINED |
| 9 | #define GrTextBlobCache_DEFINED |
| 10 | |
| 11 | #include "GrAtlasTextContext.h" |
| 12 | #include "SkTDynamicHash.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 13 | #include "SkTextBlobRunIterator.h" |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 14 | |
| 15 | class GrTextBlobCache { |
| 16 | public: |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 17 | /** |
| 18 | * The callback function used by the cache when it is still over budget after a purge. The |
| 19 | * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
| 20 | */ |
| 21 | typedef void (*PFOverBudgetCB)(void* data); |
| 22 | |
| 23 | GrTextBlobCache(PFOverBudgetCB cb, void* data) |
| 24 | : fPool(kPreAllocSize, kMinGrowthSize) |
| 25 | , fCallback(cb) |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 26 | , fData(data) |
| 27 | , fBudget(kDefaultBudget) { |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 28 | SkASSERT(cb && data); |
| 29 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 30 | ~GrTextBlobCache(); |
| 31 | |
| 32 | // creates an uncached blob |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 33 | GrAtlasTextBlob* createBlob(int glyphCount, int runCount) { |
| 34 | return GrAtlasTextBlob::Create(&fPool, glyphCount, runCount); |
| 35 | } |
| 36 | GrAtlasTextBlob* createBlob(const SkTextBlob* blob) { |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 37 | int glyphCount = 0; |
| 38 | int runCount = 0; |
| 39 | BlobGlyphCount(&glyphCount, &runCount, blob); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 40 | GrAtlasTextBlob* cacheBlob = GrAtlasTextBlob::Create(&fPool, glyphCount, runCount); |
joshualitt | 2a0e9f3 | 2015-04-13 06:12:21 -0700 | [diff] [blame] | 41 | return cacheBlob; |
| 42 | } |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 43 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 44 | GrAtlasTextBlob* createCachedBlob(const SkTextBlob* blob, |
| 45 | const GrAtlasTextBlob::Key& key, |
| 46 | const SkMaskFilter::BlurRec& blurRec, |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 47 | const SkPaint& paint) { |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 48 | int glyphCount = 0; |
| 49 | int runCount = 0; |
| 50 | BlobGlyphCount(&glyphCount, &runCount, blob); |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 51 | GrAtlasTextBlob* cacheBlob = GrAtlasTextBlob::Create(&fPool, glyphCount, runCount); |
| 52 | cacheBlob->setupKey(key, blurRec, paint); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 53 | this->add(cacheBlob); |
| 54 | return cacheBlob; |
| 55 | } |
| 56 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 57 | GrAtlasTextBlob* find(const GrAtlasTextBlob::Key& key) { |
joshualitt | 53b5f44 | 2015-04-13 06:33:59 -0700 | [diff] [blame] | 58 | return fCache.find(key); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 59 | } |
| 60 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 61 | void remove(GrAtlasTextBlob* blob) { |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 62 | fCache.remove(blob->key()); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 63 | fBlobList.remove(blob); |
| 64 | blob->unref(); |
| 65 | } |
| 66 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 67 | void add(GrAtlasTextBlob* blob) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 68 | fCache.add(blob); |
| 69 | fBlobList.addToHead(blob); |
| 70 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 71 | this->checkPurge(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 72 | } |
| 73 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 74 | void makeMRU(GrAtlasTextBlob* blob) { |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 75 | if (fBlobList.head() == blob) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | fBlobList.remove(blob); |
| 80 | fBlobList.addToHead(blob); |
| 81 | } |
| 82 | |
joshualitt | 26ffc00 | 2015-04-16 11:24:04 -0700 | [diff] [blame] | 83 | void freeAll(); |
| 84 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 85 | // TODO move to SkTextBlob |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 86 | static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 87 | SkTextBlobRunIterator itCounter(blob); |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 88 | for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
| 89 | *glyphCount += itCounter.glyphCount(); |
| 90 | } |
| 91 | } |
| 92 | |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 93 | void setBudget(size_t budget) { |
| 94 | fBudget = budget; |
| 95 | this->checkPurge(); |
| 96 | } |
| 97 | |
joshualitt | 259fbf1 | 2015-07-21 11:39:34 -0700 | [diff] [blame] | 98 | private: |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 99 | typedef SkTInternalLList<GrAtlasTextBlob> BitmapBlobList; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 100 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | void checkPurge(GrAtlasTextBlob* blob = nullptr) { |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 102 | // If we are overbudget, then unref until we are below budget again |
| 103 | if (fPool.size() > fBudget) { |
| 104 | BitmapBlobList::Iter iter; |
| 105 | iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | GrAtlasTextBlob* lruBlob = nullptr; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 107 | while (fPool.size() > fBudget && (lruBlob = iter.get()) && lruBlob != blob) { |
joshualitt | 9230377 | 2016-02-10 11:55:52 -0800 | [diff] [blame] | 108 | fCache.remove(lruBlob->key()); |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 109 | |
| 110 | // Backup the iterator before removing and unrefing the blob |
| 111 | iter.prev(); |
| 112 | fBlobList.remove(lruBlob); |
| 113 | lruBlob->unref(); |
| 114 | } |
| 115 | |
| 116 | // If we break out of the loop with lruBlob == blob, then we haven't purged enough |
| 117 | // use the call back and try to free some more. If we are still overbudget after this, |
| 118 | // then this single textblob is over our budget |
| 119 | if (blob && lruBlob == blob) { |
| 120 | (*fCallback)(fData); |
| 121 | } |
| 122 | |
| 123 | #ifdef SPEW_BUDGET_MESSAGE |
| 124 | if (fPool.size() > fBudget) { |
| 125 | SkDebugf("Single textblob is larger than our whole budget"); |
| 126 | } |
| 127 | #endif |
| 128 | } |
| 129 | } |
| 130 | |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 131 | // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are |
| 132 | // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes). |
| 133 | static const int kPreAllocSize = 1 << 17; |
| 134 | static const int kMinGrowthSize = 1 << 17; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 135 | static const int kDefaultBudget = 1 << 22; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 136 | BitmapBlobList fBlobList; |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 137 | SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key> fCache; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 138 | GrMemoryPool fPool; |
joshualitt | 0db6dfa | 2015-04-10 07:01:30 -0700 | [diff] [blame] | 139 | PFOverBudgetCB fCallback; |
| 140 | void* fData; |
joshualitt | 17d833b | 2015-08-03 10:17:44 -0700 | [diff] [blame] | 141 | size_t fBudget; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | #endif |