Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrTextBlobCache_DEFINED | 8 #ifndef GrTextBlobCache_DEFINED |
| 9 #define GrTextBlobCache_DEFINED | 9 #define GrTextBlobCache_DEFINED |
| 10 | 10 |
| 11 #include "GrAtlasTextContext.h" | 11 #include "GrAtlasTextContext.h" |
| 12 #include "SkTDynamicHash.h" | 12 #include "SkTDynamicHash.h" |
| 13 #include "SkTextBlob.h" | 13 #include "SkTextBlob.h" |
| 14 | 14 |
| 15 class GrTextBlobCache { | 15 class GrTextBlobCache { |
| 16 public: | 16 public: |
| 17 typedef GrAtlasTextContext::BitmapTextBlob BitmapTextBlob; | 17 typedef GrAtlasTextContext::BitmapTextBlob BitmapTextBlob; |
| 18 | 18 |
| 19 GrTextBlobCache() : fPool(kPreAllocSize, kMinGrowthSize) {} | 19 /** |
| 20 * The callback function used by the cache when it is still over budget afte r 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 } | |
| 20 ~GrTextBlobCache(); | 31 ~GrTextBlobCache(); |
| 21 | 32 |
| 22 // creates an uncached blob | 33 // creates an uncached blob |
| 23 BitmapTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); | 34 BitmapTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); |
| 24 | 35 |
| 25 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, size_t maxVAStride) { | 36 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, size_t maxVAStride) { |
| 26 int glyphCount = 0; | 37 int glyphCount = 0; |
| 27 int runCount = 0; | 38 int runCount = 0; |
| 28 BlobGlyphCount(&glyphCount, &runCount, blob); | 39 BlobGlyphCount(&glyphCount, &runCount, blob); |
| 29 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA Stride); | 40 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA Stride); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 45 void add(BitmapTextBlob* blob) { | 56 void add(BitmapTextBlob* blob) { |
| 46 fCache.add(blob); | 57 fCache.add(blob); |
| 47 fBlobList.addToHead(blob); | 58 fBlobList.addToHead(blob); |
| 48 | 59 |
| 49 // If we are overbudget, then unref until we are below budget again | 60 // If we are overbudget, then unref until we are below budget again |
| 50 if (fPool.size() > kBudget) { | 61 if (fPool.size() > kBudget) { |
| 51 BitmapBlobList::Iter iter; | 62 BitmapBlobList::Iter iter; |
| 52 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); | 63 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
| 53 BitmapTextBlob* lruBlob = iter.get(); | 64 BitmapTextBlob* lruBlob = iter.get(); |
| 54 SkASSERT(lruBlob); | 65 SkASSERT(lruBlob); |
| 55 do { | 66 while (fPool.size() > kBudget && (lruBlob = iter.get()) && lruBlob ! = blob) { |
| 56 fCache.remove(lruBlob->fUniqueID); | 67 fCache.remove(lruBlob->fUniqueID); |
| 57 fBlobList.remove(lruBlob); | 68 fBlobList.remove(lruBlob); |
| 69 iter.prev(); | |
| 58 lruBlob->unref(); | 70 lruBlob->unref(); |
| 59 iter.prev(); | 71 } |
| 60 } while (fPool.size() > kBudget && (lruBlob = iter.get())); | 72 |
| 73 // If we break out of the loop with lruBlob == blob, then we haven't purged enough | |
| 74 // use the call back and try to free some more. If we are still ove rbudget after this, | |
| 75 // then this single textblob is over our budget | |
| 76 if (lruBlob == blob) { | |
| 77 (*fCallback)(fData); | |
| 78 } | |
| 79 | |
|
robertphillips
2015/04/10 12:22:46
use an "#ifdef SK_DEBUG/#endif" block for this rat
| |
| 80 SkDEBUGCODE( | |
| 81 if (fPool.size() > kBudget) { | |
| 82 SkDebugf("Single textblob is larger than our whole budget"); | |
| 83 }); | |
| 61 } | 84 } |
| 62 } | 85 } |
| 63 | 86 |
| 64 void makeMRU(BitmapTextBlob* blob) { | 87 void makeMRU(BitmapTextBlob* blob) { |
| 65 if (fBlobList.head() == blob) { | 88 if (fBlobList.head() == blob) { |
| 66 return; | 89 return; |
| 67 } | 90 } |
| 68 | 91 |
| 69 fBlobList.remove(blob); | 92 fBlobList.remove(blob); |
| 70 fBlobList.addToHead(blob); | 93 fBlobList.addToHead(blob); |
| 71 } | 94 } |
| 72 | 95 |
| 73 private: | 96 private: |
| 74 // TODO move to SkTextBlob | 97 // TODO move to SkTextBlob |
| 75 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { | 98 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
| 76 SkTextBlob::RunIterator itCounter(blob); | 99 SkTextBlob::RunIterator itCounter(blob); |
| 77 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | 100 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
| 78 *glyphCount += itCounter.glyphCount(); | 101 *glyphCount += itCounter.glyphCount(); |
| 79 } | 102 } |
| 80 } | 103 } |
| 81 | 104 |
| 82 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; | 105 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; |
| 83 | 106 |
| 84 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are | 107 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are |
| 85 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes). | 108 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes). |
| 86 static const int kPreAllocSize = 1 << 17; | 109 static const int kPreAllocSize = 1 << 17; |
| 87 static const int kMinGrowthSize = 1 << 17; | 110 static const int kMinGrowthSize = 1 << 17; |
| 88 static const int kBudget = 1 << 20; | 111 static const int kBudget = 1 << 22; |
| 89 BitmapBlobList fBlobList; | 112 BitmapBlobList fBlobList; |
| 90 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache; | 113 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache; |
| 91 GrMemoryPool fPool; | 114 GrMemoryPool fPool; |
| 115 PFOverBudgetCB fCallback; | |
| 116 void* fData; | |
| 92 }; | 117 }; |
| 93 | 118 |
| 94 #endif | 119 #endif |
| OLD | NEW |