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