OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 #include "GrAtlas.h" | 8 #include "GrAtlas.h" |
9 #include "GrGpu.h" | 9 #include "GrGpu.h" |
10 #include "GrRectanizer.h" | 10 #include "GrRectanizer.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 GrTextStrike* strikeToPurge = strike; | 79 GrTextStrike* strikeToPurge = strike; |
80 strike = strikeToPurge->fPrev; | 80 strike = strikeToPurge->fPrev; |
81 if (purge) { | 81 if (purge) { |
82 // keep purging if we won't free up any atlases with this strike. | 82 // keep purging if we won't free up any atlases with this strike. |
83 purge = (NULL == strikeToPurge->fAtlas); | 83 purge = (NULL == strikeToPurge->fAtlas); |
84 int index = fCache.slowFindIndex(strikeToPurge); | 84 int index = fCache.slowFindIndex(strikeToPurge); |
85 GrAssert(index >= 0); | 85 GrAssert(index >= 0); |
86 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()); | 86 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()); |
87 this->detachStrikeFromList(strikeToPurge); | 87 this->detachStrikeFromList(strikeToPurge); |
88 delete strikeToPurge; | 88 delete strikeToPurge; |
89 } else { | |
90 // for the remaining strikes, we just mark them unused | |
91 GrAtlas::MarkAllUnused(strikeToPurge->fAtlas); | |
92 } | 89 } |
93 } | 90 } |
94 #if FONT_CACHE_STATS | 91 #if FONT_CACHE_STATS |
95 ++g_PurgeCount; | 92 ++g_PurgeCount; |
96 #endif | 93 #endif |
97 } | 94 } |
98 | 95 |
99 void GrFontCache::freeAtlasExceptFor(GrTextStrike* preserveStrike) { | 96 void GrFontCache::freeAtlasExceptFor(GrTextStrike* preserveStrike, uint64_t curr entFlushCount) { |
100 GrTextStrike* strike = fTail; | 97 GrTextStrike* strike = fTail; |
101 while (strike) { | 98 while (strike) { |
102 if (strike == preserveStrike) { | 99 if (strike == preserveStrike) { |
103 strike = strike->fPrev; | 100 strike = strike->fPrev; |
104 continue; | 101 continue; |
105 } | 102 } |
106 GrTextStrike* strikeToPurge = strike; | 103 GrTextStrike* strikeToPurge = strike; |
107 strike = strikeToPurge->fPrev; | 104 strike = strikeToPurge->fPrev; |
108 if (strikeToPurge->removeUnusedAtlases()) { | 105 if (strikeToPurge->removeUnusedAtlases(currentFlushCount)) { |
109 if (NULL == strikeToPurge->fAtlas) { | 106 if (NULL == strikeToPurge->fAtlas) { |
110 int index = fCache.slowFindIndex(strikeToPurge); | 107 int index = fCache.slowFindIndex(strikeToPurge); |
111 GrAssert(index >= 0); | 108 GrAssert(index >= 0); |
112 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()) ; | 109 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()) ; |
113 this->detachStrikeFromList(strikeToPurge); | 110 this->detachStrikeFromList(strikeToPurge); |
114 delete strikeToPurge; | 111 delete strikeToPurge; |
115 } | 112 } |
116 break; | 113 break; |
117 } | 114 } |
118 } | 115 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 #if GR_DEBUG | 174 #if GR_DEBUG |
178 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); | 175 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
179 gCounter += 1; | 176 gCounter += 1; |
180 #endif | 177 #endif |
181 } | 178 } |
182 | 179 |
183 // these signatures are needed because they're used with | 180 // these signatures are needed because they're used with |
184 // SkTDArray::visitAll() (see destructor & removeUnusedAtlases()) | 181 // SkTDArray::visitAll() (see destructor & removeUnusedAtlases()) |
185 static void free_glyph(GrGlyph*& glyph) { glyph->free(); } | 182 static void free_glyph(GrGlyph*& glyph) { glyph->free(); } |
186 | 183 |
184 static uint64_t gCurrentFlushCount = 0; | |
bsalomon
2013/08/13 21:18:28
wha? There can be multiple GrContexts
jvanverth1
2013/08/14 13:24:32
A bit of a hack to get around the fact that visitA
| |
185 | |
187 static void invalidate_glyph(GrGlyph*& glyph) { | 186 static void invalidate_glyph(GrGlyph*& glyph) { |
188 if (glyph->fAtlas && !glyph->fAtlas->used()) { | 187 if (glyph->fAtlas && glyph->fAtlas->lastFlush() < gCurrentFlushCount) { |
189 glyph->fAtlas = NULL; | 188 glyph->fAtlas = NULL; |
190 } | 189 } |
191 } | 190 } |
192 | 191 |
193 GrTextStrike::~GrTextStrike() { | 192 GrTextStrike::~GrTextStrike() { |
194 GrAtlas::FreeLList(fAtlas); | 193 GrAtlas::FreeLList(fAtlas); |
195 fFontScalerKey->unref(); | 194 fFontScalerKey->unref(); |
196 fCache.getArray().visitAll(free_glyph); | 195 fCache.getArray().visitAll(free_glyph); |
197 | 196 |
198 #if GR_DEBUG | 197 #if GR_DEBUG |
199 gCounter -= 1; | 198 gCounter -= 1; |
200 // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); | 199 // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
201 #endif | 200 #endif |
202 } | 201 } |
203 | 202 |
204 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, | 203 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
205 GrFontScaler* scaler) { | 204 GrFontScaler* scaler) { |
206 SkIRect bounds; | 205 SkIRect bounds; |
207 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { | 206 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
208 return NULL; | 207 return NULL; |
209 } | 208 } |
210 | 209 |
211 GrGlyph* glyph = fPool.alloc(); | 210 GrGlyph* glyph = fPool.alloc(); |
212 glyph->init(packed, bounds); | 211 glyph->init(packed, bounds); |
213 fCache.insert(packed, glyph); | 212 fCache.insert(packed, glyph); |
214 return glyph; | 213 return glyph; |
215 } | 214 } |
216 | 215 |
217 bool GrTextStrike::removeUnusedAtlases() { | 216 bool GrTextStrike::removeUnusedAtlases(uint64_t currentFlushCount) { |
217 gCurrentFlushCount = currentFlushCount; | |
218 fCache.getArray().visitAll(invalidate_glyph); | 218 fCache.getArray().visitAll(invalidate_glyph); |
219 return GrAtlas::RemoveUnusedAtlases(fAtlasMgr, &fAtlas); | 219 return GrAtlas::RemoveUnusedAtlases(fAtlasMgr, &fAtlas, currentFlushCount); |
220 | 220 |
221 return false; | 221 return false; |
222 } | 222 } |
223 | 223 |
224 bool GrTextStrike::getGlyphAtlas(GrGlyph* glyph, GrFontScaler* scaler) { | 224 bool GrTextStrike::getGlyphAtlas(GrGlyph* glyph, GrFontScaler* scaler, uint64_t currentFlushCount) { |
225 #if 0 // testing hack to force us to flush our cache often | 225 #if 0 // testing hack to force us to flush our cache often |
226 static int gCounter; | 226 static int gCounter; |
227 if ((++gCounter % 10) == 0) return false; | 227 if ((++gCounter % 10) == 0) return false; |
228 #endif | 228 #endif |
229 | 229 |
230 GrAssert(glyph); | 230 GrAssert(glyph); |
231 GrAssert(scaler); | 231 GrAssert(scaler); |
232 GrAssert(fCache.contains(glyph)); | 232 GrAssert(fCache.contains(glyph)); |
233 if (glyph->fAtlas) { | 233 if (glyph->fAtlas) { |
234 glyph->fAtlas->setUsed(true); | 234 glyph->fAtlas->setLastFlush(currentFlushCount); |
235 return true; | 235 return true; |
236 } | 236 } |
237 | 237 |
238 GrAutoRef ar(scaler); | 238 GrAutoRef ar(scaler); |
239 | 239 |
240 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); | 240 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
241 size_t size = glyph->fBounds.area() * bytesPerPixel; | 241 size_t size = glyph->fBounds.area() * bytesPerPixel; |
242 SkAutoSMalloc<1024> storage(size); | 242 SkAutoSMalloc<1024> storage(size); |
243 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), | 243 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
244 glyph->height(), | 244 glyph->height(), |
245 glyph->width() * bytesPerPixel, | 245 glyph->width() * bytesPerPixel, |
246 storage.get())) { | 246 storage.get())) { |
247 return false; | 247 return false; |
248 } | 248 } |
249 | 249 |
250 GrAtlas* atlas = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), | 250 GrAtlas* atlas = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), |
251 glyph->height(), storage.get(), | 251 glyph->height(), storage.get(), |
252 fMaskFormat, | 252 fMaskFormat, |
253 &glyph->fAtlasLocation); | 253 &glyph->fAtlasLocation); |
254 if (NULL == atlas) { | 254 if (NULL == atlas) { |
255 return false; | 255 return false; |
256 } | 256 } |
257 | 257 |
258 glyph->fAtlas = atlas; | 258 glyph->fAtlas = atlas; |
259 atlas->setUsed(true); | 259 atlas->setLastFlush(currentFlushCount); |
260 return true; | 260 return true; |
261 } | 261 } |
OLD | NEW |