Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 19 GrTextBlobCache() : fPool(2 << 17, 2 << 17), fBudget(2 << 22) {} | |
|
jvanverth1
2015/04/06 20:44:12
These seem like magic numbers. I'm not sure why th
| |
| 20 ~GrTextBlobCache(); | |
| 21 | |
| 22 // creates an uncached blob | |
| 23 BitmapTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); | |
| 24 | |
| 25 BitmapTextBlob* createCachedBlob(const SkTextBlob* blob, size_t maxVAStride) { | |
| 26 int glyphCount = 0; | |
| 27 int runCount = 0; | |
| 28 BlobGlyphCount(&glyphCount, &runCount, blob); | |
| 29 BitmapTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxVA Stride); | |
| 30 cacheBlob->fUniqueID = blob->uniqueID(); | |
| 31 this->add(cacheBlob); | |
| 32 return cacheBlob; | |
| 33 } | |
| 34 | |
| 35 BitmapTextBlob* find(uint32_t uniqueID) { | |
| 36 return fCache.find(uniqueID); | |
| 37 } | |
| 38 | |
| 39 void remove(BitmapTextBlob* blob) { | |
| 40 fCache.remove(blob->fUniqueID); | |
| 41 fBlobList.remove(blob); | |
| 42 blob->unref(); | |
| 43 } | |
| 44 | |
| 45 void add(BitmapTextBlob* blob) { | |
| 46 fCache.add(blob); | |
| 47 fBlobList.addToHead(blob); | |
| 48 | |
| 49 // If we are overbudget, then unref until we are below budget again | |
| 50 if (fPool.size() > fBudget) { | |
| 51 BitmapBlobList::Iter iter; | |
| 52 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); | |
| 53 BitmapTextBlob* lruBlob = iter.get(); | |
| 54 SkASSERT(lruBlob); | |
| 55 do { | |
| 56 fCache.remove(lruBlob->fUniqueID); | |
| 57 fBlobList.remove(lruBlob); | |
| 58 lruBlob->unref(); | |
| 59 iter.prev(); | |
| 60 } while (fPool.size() > fBudget && (lruBlob = iter.get())); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void makeMRU(BitmapTextBlob* blob) { | |
| 65 if (fBlobList.head() == blob) { | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 fBlobList.remove(blob); | |
| 70 fBlobList.addToHead(blob); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 // TODO move to SkTextBlob | |
| 75 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { | |
| 76 SkTextBlob::RunIterator itCounter(blob); | |
| 77 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | |
| 78 *glyphCount += itCounter.glyphCount(); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; | |
| 83 BitmapBlobList fBlobList; | |
| 84 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache; | |
| 85 GrMemoryPool fPool; | |
| 86 size_t fBudget; | |
| 87 }; | |
| 88 | |
| 89 #endif | |
| OLD | NEW |