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 |
| 80 #ifdef SK_DEBUG |
| 81 if (fPool.size() > kBudget) { |
| 82 SkDebugf("Single textblob is larger than our whole budget"); |
| 83 } |
| 84 #endif |
61 } | 85 } |
62 } | 86 } |
63 | 87 |
64 void makeMRU(BitmapTextBlob* blob) { | 88 void makeMRU(BitmapTextBlob* blob) { |
65 if (fBlobList.head() == blob) { | 89 if (fBlobList.head() == blob) { |
66 return; | 90 return; |
67 } | 91 } |
68 | 92 |
69 fBlobList.remove(blob); | 93 fBlobList.remove(blob); |
70 fBlobList.addToHead(blob); | 94 fBlobList.addToHead(blob); |
71 } | 95 } |
72 | 96 |
73 private: | 97 private: |
74 // TODO move to SkTextBlob | 98 // TODO move to SkTextBlob |
75 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob)
{ | 99 void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob)
{ |
76 SkTextBlob::RunIterator itCounter(blob); | 100 SkTextBlob::RunIterator itCounter(blob); |
77 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | 101 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
78 *glyphCount += itCounter.glyphCount(); | 102 *glyphCount += itCounter.glyphCount(); |
79 } | 103 } |
80 } | 104 } |
81 | 105 |
82 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; | 106 typedef SkTInternalLList<BitmapTextBlob> BitmapBlobList; |
83 | 107 |
84 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes
in the pool are | 108 // 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). | 109 // based off of the largest cached textblob I have seen in the skps(a couple
of kilobytes). |
86 static const int kPreAllocSize = 1 << 17; | 110 static const int kPreAllocSize = 1 << 17; |
87 static const int kMinGrowthSize = 1 << 17; | 111 static const int kMinGrowthSize = 1 << 17; |
88 static const int kBudget = 1 << 20; | 112 static const int kBudget = 1 << 22; |
89 BitmapBlobList fBlobList; | 113 BitmapBlobList fBlobList; |
90 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache; | 114 SkTDynamicHash<BitmapTextBlob, uint32_t> fCache; |
91 GrMemoryPool fPool; | 115 GrMemoryPool fPool; |
| 116 PFOverBudgetCB fCallback; |
| 117 void* fData; |
92 }; | 118 }; |
93 | 119 |
94 #endif | 120 #endif |
OLD | NEW |