| 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 "SkTextBlobRunIterator.h" | |
| 14 | |
| 15 class GrTextBlobCache { | |
| 16 public: | |
| 17 /** | |
| 18 * The callback function used by the cache when it is still over budget afte
r 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) | |
| 26 , fData(data) | |
| 27 , fBudget(kDefaultBudget) { | |
| 28 SkASSERT(cb && data); | |
| 29 } | |
| 30 ~GrTextBlobCache(); | |
| 31 | |
| 32 // creates an uncached blob | |
| 33 GrAtlasTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); | |
| 34 GrAtlasTextBlob* createBlob(const SkTextBlob* blob, size_t maxVAStride) { | |
| 35 int glyphCount = 0; | |
| 36 int runCount = 0; | |
| 37 BlobGlyphCount(&glyphCount, &runCount, blob); | |
| 38 GrAtlasTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxV
AStride); | |
| 39 return cacheBlob; | |
| 40 } | |
| 41 | |
| 42 static void SetupCacheBlobKey(GrAtlasTextBlob* cacheBlob, | |
| 43 const GrAtlasTextBlob::Key& key, | |
| 44 const SkMaskFilter::BlurRec& blurRec, | |
| 45 const SkPaint& paint) { | |
| 46 cacheBlob->fKey = key; | |
| 47 if (key.fHasBlur) { | |
| 48 cacheBlob->fBlurRec = blurRec; | |
| 49 } | |
| 50 if (key.fStyle != SkPaint::kFill_Style) { | |
| 51 cacheBlob->fStrokeInfo.fFrameWidth = paint.getStrokeWidth(); | |
| 52 cacheBlob->fStrokeInfo.fMiterLimit = paint.getStrokeMiter(); | |
| 53 cacheBlob->fStrokeInfo.fJoin = paint.getStrokeJoin(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 GrAtlasTextBlob* createCachedBlob(const SkTextBlob* blob, | |
| 58 const GrAtlasTextBlob::Key& key, | |
| 59 const SkMaskFilter::BlurRec& blurRec, | |
| 60 const SkPaint& paint, | |
| 61 size_t maxVAStride) { | |
| 62 int glyphCount = 0; | |
| 63 int runCount = 0; | |
| 64 BlobGlyphCount(&glyphCount, &runCount, blob); | |
| 65 GrAtlasTextBlob* cacheBlob = this->createBlob(glyphCount, runCount, maxV
AStride); | |
| 66 SetupCacheBlobKey(cacheBlob, key, blurRec, paint); | |
| 67 this->add(cacheBlob); | |
| 68 return cacheBlob; | |
| 69 } | |
| 70 | |
| 71 GrAtlasTextBlob* find(const GrAtlasTextBlob::Key& key) { | |
| 72 return fCache.find(key); | |
| 73 } | |
| 74 | |
| 75 void remove(GrAtlasTextBlob* blob) { | |
| 76 fCache.remove(blob->fKey); | |
| 77 fBlobList.remove(blob); | |
| 78 blob->unref(); | |
| 79 } | |
| 80 | |
| 81 void add(GrAtlasTextBlob* blob) { | |
| 82 fCache.add(blob); | |
| 83 fBlobList.addToHead(blob); | |
| 84 | |
| 85 this->checkPurge(blob); | |
| 86 } | |
| 87 | |
| 88 void makeMRU(GrAtlasTextBlob* blob) { | |
| 89 if (fBlobList.head() == blob) { | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 fBlobList.remove(blob); | |
| 94 fBlobList.addToHead(blob); | |
| 95 } | |
| 96 | |
| 97 void freeAll(); | |
| 98 | |
| 99 // TODO move to SkTextBlob | |
| 100 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob*
blob) { | |
| 101 SkTextBlobRunIterator itCounter(blob); | |
| 102 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | |
| 103 *glyphCount += itCounter.glyphCount(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void setBudget(size_t budget) { | |
| 108 fBudget = budget; | |
| 109 this->checkPurge(); | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 typedef SkTInternalLList<GrAtlasTextBlob> BitmapBlobList; | |
| 114 | |
| 115 void checkPurge(GrAtlasTextBlob* blob = nullptr) { | |
| 116 // If we are overbudget, then unref until we are below budget again | |
| 117 if (fPool.size() > fBudget) { | |
| 118 BitmapBlobList::Iter iter; | |
| 119 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); | |
| 120 GrAtlasTextBlob* lruBlob = nullptr; | |
| 121 while (fPool.size() > fBudget && (lruBlob = iter.get()) && lruBlob !
= blob) { | |
| 122 fCache.remove(lruBlob->fKey); | |
| 123 | |
| 124 // Backup the iterator before removing and unrefing the blob | |
| 125 iter.prev(); | |
| 126 fBlobList.remove(lruBlob); | |
| 127 lruBlob->unref(); | |
| 128 } | |
| 129 | |
| 130 // If we break out of the loop with lruBlob == blob, then we haven't
purged enough | |
| 131 // use the call back and try to free some more. If we are still ove
rbudget after this, | |
| 132 // then this single textblob is over our budget | |
| 133 if (blob && lruBlob == blob) { | |
| 134 (*fCallback)(fData); | |
| 135 } | |
| 136 | |
| 137 #ifdef SPEW_BUDGET_MESSAGE | |
| 138 if (fPool.size() > fBudget) { | |
| 139 SkDebugf("Single textblob is larger than our whole budget"); | |
| 140 } | |
| 141 #endif | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes
in the pool are | |
| 146 // based off of the largest cached textblob I have seen in the skps(a couple
of kilobytes). | |
| 147 static const int kPreAllocSize = 1 << 17; | |
| 148 static const int kMinGrowthSize = 1 << 17; | |
| 149 static const int kDefaultBudget = 1 << 22; | |
| 150 BitmapBlobList fBlobList; | |
| 151 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key> fCache; | |
| 152 GrMemoryPool fPool; | |
| 153 PFOverBudgetCB fCallback; | |
| 154 void* fData; | |
| 155 size_t fBudget; | |
| 156 }; | |
| 157 | |
| 158 #endif | |
| OLD | NEW |