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 /** | 17 /** |
18 * The callback function used by the cache when it is still over budget afte r a purge. The | 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. | 19 * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
20 */ | 20 */ |
21 typedef void (*PFOverBudgetCB)(void* data); | 21 typedef void (*PFOverBudgetCB)(void* data); |
22 | 22 |
23 GrTextBlobCache(PFOverBudgetCB cb, void* data) | 23 GrTextBlobCache(PFOverBudgetCB cb, void* data) |
24 : fPool(kPreAllocSize, kMinGrowthSize) | 24 : fPool(kPreAllocSize, kMinGrowthSize) |
25 , fCallback(cb) | 25 , fCallback(cb) |
26 , fData(data) { | 26 , fData(data) |
27 , fBudget(kDefaultBudget) { | |
27 SkASSERT(cb && data); | 28 SkASSERT(cb && data); |
28 } | 29 } |
29 ~GrTextBlobCache(); | 30 ~GrTextBlobCache(); |
30 | 31 |
31 // creates an uncached blob | 32 // creates an uncached blob |
32 GrAtlasTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); | 33 GrAtlasTextBlob* createBlob(int glyphCount, int runCount, size_t maxVASize); |
33 GrAtlasTextBlob* createBlob(const SkTextBlob* blob, size_t maxVAStride) { | 34 GrAtlasTextBlob* createBlob(const SkTextBlob* blob, size_t maxVAStride) { |
34 int glyphCount = 0; | 35 int glyphCount = 0; |
35 int runCount = 0; | 36 int runCount = 0; |
36 BlobGlyphCount(&glyphCount, &runCount, blob); | 37 BlobGlyphCount(&glyphCount, &runCount, blob); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 fCache.remove(blob->fKey); | 76 fCache.remove(blob->fKey); |
76 fBlobList.remove(blob); | 77 fBlobList.remove(blob); |
77 blob->unref(); | 78 blob->unref(); |
78 } | 79 } |
79 | 80 |
80 void add(GrAtlasTextBlob* blob) { | 81 void add(GrAtlasTextBlob* blob) { |
81 fCache.add(blob); | 82 fCache.add(blob); |
82 fBlobList.addToHead(blob); | 83 fBlobList.addToHead(blob); |
83 | 84 |
84 // If we are overbudget, then unref until we are below budget again | 85 // If we are overbudget, then unref until we are below budget again |
85 if (fPool.size() > kBudget) { | 86 if (fPool.size() > fBudget) { |
86 BitmapBlobList::Iter iter; | 87 BitmapBlobList::Iter iter; |
87 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); | 88 iter.init(fBlobList, BitmapBlobList::Iter::kTail_IterStart); |
88 GrAtlasTextBlob* lruBlob = iter.get(); | 89 GrAtlasTextBlob* lruBlob = iter.get(); |
89 SkASSERT(lruBlob); | 90 SkASSERT(lruBlob); |
90 while (fPool.size() > kBudget && (lruBlob = iter.get()) && lruBlob ! = blob) { | 91 while (fPool.size() > fBudget && (lruBlob = iter.get()) && lruBlob ! = blob) { |
91 fCache.remove(lruBlob->fKey); | 92 fCache.remove(lruBlob->fKey); |
92 | 93 |
93 // Backup the iterator before removing and unrefing the blob | 94 // Backup the iterator before removing and unrefing the blob |
94 iter.prev(); | 95 iter.prev(); |
95 fBlobList.remove(lruBlob); | 96 fBlobList.remove(lruBlob); |
96 lruBlob->unref(); | 97 lruBlob->unref(); |
97 } | 98 } |
98 | 99 |
99 // If we break out of the loop with lruBlob == blob, then we haven't purged enough | 100 // If we break out of the loop with lruBlob == blob, then we haven't purged enough |
100 // use the call back and try to free some more. If we are still ove rbudget after this, | 101 // use the call back and try to free some more. If we are still ove rbudget after this, |
101 // then this single textblob is over our budget | 102 // then this single textblob is over our budget |
102 if (lruBlob == blob) { | 103 if (lruBlob == blob) { |
103 (*fCallback)(fData); | 104 (*fCallback)(fData); |
104 } | 105 } |
105 | 106 |
106 #ifdef SK_DEBUG | 107 #ifdef SPEW_BUDGET_MESSAGE |
107 if (fPool.size() > kBudget) { | 108 if (fPool.size() > fBudget) { |
108 SkDebugf("Single textblob is larger than our whole budget"); | 109 SkDebugf("Single textblob is larger than our whole budget"); |
109 } | 110 } |
110 #endif | 111 #endif |
111 } | 112 } |
112 } | 113 } |
113 | 114 |
114 void makeMRU(GrAtlasTextBlob* blob) { | 115 void makeMRU(GrAtlasTextBlob* blob) { |
115 if (fBlobList.head() == blob) { | 116 if (fBlobList.head() == blob) { |
116 return; | 117 return; |
117 } | 118 } |
118 | 119 |
119 fBlobList.remove(blob); | 120 fBlobList.remove(blob); |
120 fBlobList.addToHead(blob); | 121 fBlobList.addToHead(blob); |
121 } | 122 } |
122 | 123 |
123 void freeAll(); | 124 void freeAll(); |
124 | 125 |
125 // TODO move to SkTextBlob | 126 // TODO move to SkTextBlob |
126 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { | 127 static void BlobGlyphCount(int* glyphCount, int* runCount, const SkTextBlob* blob) { |
127 SkTextBlob::RunIterator itCounter(blob); | 128 SkTextBlob::RunIterator itCounter(blob); |
128 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { | 129 for (; !itCounter.done(); itCounter.next(), (*runCount)++) { |
129 *glyphCount += itCounter.glyphCount(); | 130 *glyphCount += itCounter.glyphCount(); |
130 } | 131 } |
131 } | 132 } |
132 | 133 |
134 void setBudget(size_t budget) { fBudget = budget; } | |
bsalomon
2015/08/03 15:40:23
Do you need to purge if budget < fBudget?
| |
135 | |
133 private: | 136 private: |
134 typedef SkTInternalLList<GrAtlasTextBlob> BitmapBlobList; | 137 typedef SkTInternalLList<GrAtlasTextBlob> BitmapBlobList; |
135 | 138 |
136 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are | 139 // Budget was chosen to be ~4 megabytes. The min alloc and pre alloc sizes in the pool are |
137 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes). | 140 // based off of the largest cached textblob I have seen in the skps(a couple of kilobytes). |
138 static const int kPreAllocSize = 1 << 17; | 141 static const int kPreAllocSize = 1 << 17; |
139 static const int kMinGrowthSize = 1 << 17; | 142 static const int kMinGrowthSize = 1 << 17; |
140 static const int kBudget = 1 << 22; | 143 static const int kDefaultBudget = 1 << 22; |
141 BitmapBlobList fBlobList; | 144 BitmapBlobList fBlobList; |
142 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key> fCache; | 145 SkTDynamicHash<GrAtlasTextBlob, GrAtlasTextBlob::Key> fCache; |
143 GrMemoryPool fPool; | 146 GrMemoryPool fPool; |
144 PFOverBudgetCB fCallback; | 147 PFOverBudgetCB fCallback; |
145 void* fData; | 148 void* fData; |
149 size_t fBudget; | |
146 }; | 150 }; |
147 | 151 |
148 #endif | 152 #endif |
OLD | NEW |