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