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 "GrGpu.h" | 8 #include "GrGpu.h" |
9 #include "GrRectanizer.h" | 9 #include "GrRectanizer.h" |
10 #include "GrTextStrike.h" | 10 #include "GrTextStrike.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { | 32 GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
33 gpu->ref(); | 33 gpu->ref(); |
34 for (int i = 0; i < kAtlasCount; ++i) { | 34 for (int i = 0; i < kAtlasCount; ++i) { |
35 fAtlases[i] = NULL; | 35 fAtlases[i] = NULL; |
36 } | 36 } |
37 | 37 |
38 fHead = fTail = NULL; | 38 fHead = fTail = NULL; |
39 } | 39 } |
40 | 40 |
41 GrFontCache::~GrFontCache() { | 41 GrFontCache::~GrFontCache() { |
42 fCache.deleteAll(); | 42 SkTDynamicHash<GrTextStrike, GrFontDescKey>::Iter iter(&fCache); |
| 43 while (!iter.done()) { |
| 44 SkDELETE(&(*iter)); |
| 45 ++iter; |
| 46 } |
43 for (int i = 0; i < kAtlasCount; ++i) { | 47 for (int i = 0; i < kAtlasCount; ++i) { |
44 delete fAtlases[i]; | 48 delete fAtlases[i]; |
45 } | 49 } |
46 fGpu->unref(); | 50 fGpu->unref(); |
47 #if FONT_CACHE_STATS | 51 #if FONT_CACHE_STATS |
48 GrPrintf("Num purges: %d\n", g_PurgeCount); | 52 GrPrintf("Num purges: %d\n", g_PurgeCount); |
49 #endif | 53 #endif |
50 } | 54 } |
51 | 55 |
52 static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { | 56 static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { |
(...skipping 14 matching lines...) Expand all Loading... |
67 GrFontCache::k565_AtlasType, | 71 GrFontCache::k565_AtlasType, |
68 GrFontCache::k8888_AtlasType, | 72 GrFontCache::k8888_AtlasType, |
69 GrFontCache::k8888_AtlasType | 73 GrFontCache::k8888_AtlasType |
70 }; | 74 }; |
71 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_s
ize_mismatch); | 75 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_s
ize_mismatch); |
72 | 76 |
73 SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount); | 77 SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount); |
74 return sAtlasIndices[format]; | 78 return sAtlasIndices[format]; |
75 } | 79 } |
76 | 80 |
77 GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, | 81 GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler) { |
78 const Key& key) { | |
79 GrMaskFormat format = scaler->getMaskFormat(); | 82 GrMaskFormat format = scaler->getMaskFormat(); |
80 GrPixelConfig config = mask_format_to_pixel_config(format); | 83 GrPixelConfig config = mask_format_to_pixel_config(format); |
81 int atlasIndex = mask_format_to_atlas_index(format); | 84 int atlasIndex = mask_format_to_atlas_index(format); |
82 if (NULL == fAtlases[atlasIndex]) { | 85 if (NULL == fAtlases[atlasIndex]) { |
83 SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, | 86 SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, |
84 GR_ATLAS_TEXTURE_HEIGHT); | 87 GR_ATLAS_TEXTURE_HEIGHT); |
85 fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, kNone_GrTextur
eFlags, | 88 fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, kNone_GrTextur
eFlags, |
86 textureSize, | 89 textureSize, |
87 GR_NUM_PLOTS_X, | 90 GR_NUM_PLOTS_X, |
88 GR_NUM_PLOTS_Y, | 91 GR_NUM_PLOTS_Y, |
89 true)); | 92 true)); |
90 } | 93 } |
91 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, | 94 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, |
92 (this, scaler->getKey(), format, fAtlases[
atlasIndex])); | 95 (this, scaler->getKey(), format, fAtlases[
atlasIndex])); |
93 fCache.insert(key, strike); | 96 fCache.add(strike); |
94 | 97 |
95 if (fHead) { | 98 if (fHead) { |
96 fHead->fPrev = strike; | 99 fHead->fPrev = strike; |
97 } else { | 100 } else { |
98 SkASSERT(NULL == fTail); | 101 SkASSERT(NULL == fTail); |
99 fTail = strike; | 102 fTail = strike; |
100 } | 103 } |
101 strike->fPrev = NULL; | 104 strike->fPrev = NULL; |
102 strike->fNext = fHead; | 105 strike->fNext = fHead; |
103 fHead = strike; | 106 fHead = strike; |
104 | 107 |
105 return strike; | 108 return strike; |
106 } | 109 } |
107 | 110 |
108 void GrFontCache::freeAll() { | 111 void GrFontCache::freeAll() { |
109 fCache.deleteAll(); | 112 SkTDynamicHash<GrTextStrike, GrFontDescKey>::Iter iter(&fCache); |
| 113 while (!iter.done()) { |
| 114 SkDELETE(&(*iter)); |
| 115 ++iter; |
| 116 } |
| 117 fCache.rewind(); |
110 for (int i = 0; i < kAtlasCount; ++i) { | 118 for (int i = 0; i < kAtlasCount; ++i) { |
111 delete fAtlases[i]; | 119 delete fAtlases[i]; |
112 fAtlases[i] = NULL; | 120 fAtlases[i] = NULL; |
113 } | 121 } |
114 fHead = NULL; | 122 fHead = NULL; |
115 fTail = NULL; | 123 fTail = NULL; |
116 } | 124 } |
117 | 125 |
118 void GrFontCache::purgeStrike(GrTextStrike* strike) { | 126 void GrFontCache::purgeStrike(GrTextStrike* strike) { |
119 const GrFontCache::Key key(strike->fFontScalerKey); | 127 fCache.remove(*(strike->fFontScalerKey)); |
120 fCache.remove(key, strike); | |
121 this->detachStrikeFromList(strike); | 128 this->detachStrikeFromList(strike); |
122 delete strike; | 129 delete strike; |
123 } | 130 } |
124 | 131 |
125 bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) { | 132 bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) { |
126 SkASSERT(NULL != preserveStrike); | 133 SkASSERT(NULL != preserveStrike); |
127 | 134 |
128 GrAtlas* atlas = preserveStrike->fAtlas; | 135 GrAtlas* atlas = preserveStrike->fAtlas; |
129 GrPlot* plot = atlas->getUnusedPlot(); | 136 GrPlot* plot = atlas->getUnusedPlot(); |
130 if (NULL == plot) { | 137 if (NULL == plot) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 fAtlas = atlas; // no need to ref, it won't go away before we do | 237 fAtlas = atlas; // no need to ref, it won't go away before we do |
231 | 238 |
232 fMaskFormat = format; | 239 fMaskFormat = format; |
233 | 240 |
234 #ifdef SK_DEBUG | 241 #ifdef SK_DEBUG |
235 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); | 242 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
236 gCounter += 1; | 243 gCounter += 1; |
237 #endif | 244 #endif |
238 } | 245 } |
239 | 246 |
240 // this signature is needed because it's used with | |
241 // SkTDArray::visitAll() (see destructor) | |
242 static void free_glyph(GrGlyph*& glyph) { glyph->free(); } | |
243 | |
244 GrTextStrike::~GrTextStrike() { | 247 GrTextStrike::~GrTextStrike() { |
245 fFontScalerKey->unref(); | 248 fFontScalerKey->unref(); |
246 fCache.getArray().visitAll(free_glyph); | 249 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 250 while (!iter.done()) { |
| 251 (*iter).free(); |
| 252 ++iter; |
| 253 } |
247 | 254 |
248 #ifdef SK_DEBUG | 255 #ifdef SK_DEBUG |
249 gCounter -= 1; | 256 gCounter -= 1; |
250 // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); | 257 // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
251 #endif | 258 #endif |
252 } | 259 } |
253 | 260 |
254 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, | 261 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
255 GrFontScaler* scaler) { | 262 GrFontScaler* scaler) { |
256 SkIRect bounds; | 263 SkIRect bounds; |
257 if (fUseDistanceField) { | 264 if (fUseDistanceField) { |
258 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) { | 265 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) { |
259 return NULL; | 266 return NULL; |
260 } | 267 } |
261 } else { | 268 } else { |
262 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { | 269 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
263 return NULL; | 270 return NULL; |
264 } | 271 } |
265 } | 272 } |
266 | 273 |
267 GrGlyph* glyph = fPool.alloc(); | 274 GrGlyph* glyph = fPool.alloc(); |
268 glyph->init(packed, bounds); | 275 glyph->init(packed, bounds); |
269 fCache.insert(packed, glyph); | 276 fCache.add(glyph); |
270 return glyph; | 277 return glyph; |
271 } | 278 } |
272 | 279 |
273 void GrTextStrike::removePlot(const GrPlot* plot) { | 280 void GrTextStrike::removePlot(const GrPlot* plot) { |
274 SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); | 281 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
275 for (int i = 0; i < glyphArray.count(); ++i) { | 282 while (!iter.done()) { |
276 if (plot == glyphArray[i]->fPlot) { | 283 if (plot == (*iter).fPlot) { |
277 glyphArray[i]->fPlot = NULL; | 284 (*iter).fPlot = NULL; |
278 } | 285 } |
| 286 ++iter; |
279 } | 287 } |
280 | 288 |
281 GrAtlas::RemovePlot(&fPlotUsage, plot); | 289 GrAtlas::RemovePlot(&fPlotUsage, plot); |
282 } | 290 } |
283 | 291 |
284 | 292 |
285 bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { | 293 bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { |
286 #if 0 // testing hack to force us to flush our cache often | 294 #if 0 // testing hack to force us to flush our cache often |
287 static int gCounter; | 295 static int gCounter; |
288 if ((++gCounter % 10) == 0) return false; | 296 if ((++gCounter % 10) == 0) return false; |
289 #endif | 297 #endif |
290 | 298 |
291 SkASSERT(glyph); | 299 SkASSERT(glyph); |
292 SkASSERT(scaler); | 300 SkASSERT(scaler); |
293 SkASSERT(fCache.contains(glyph)); | 301 SkASSERT(fCache.find(glyph->fPackedID)); |
294 SkASSERT(NULL == glyph->fPlot); | 302 SkASSERT(NULL == glyph->fPlot); |
295 | 303 |
296 SkAutoUnref ar(SkSafeRef(scaler)); | 304 SkAutoUnref ar(SkSafeRef(scaler)); |
297 | 305 |
298 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); | 306 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
299 | 307 |
300 size_t size = glyph->fBounds.area() * bytesPerPixel; | 308 size_t size = glyph->fBounds.area() * bytesPerPixel; |
301 SkAutoSMalloc<1024> storage(size); | 309 SkAutoSMalloc<1024> storage(size); |
302 if (fUseDistanceField) { | 310 if (fUseDistanceField) { |
303 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(), | 311 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(), |
(...skipping 14 matching lines...) Expand all Loading... |
318 glyph->height(), storage.get(), | 326 glyph->height(), storage.get(), |
319 &glyph->fAtlasLocation); | 327 &glyph->fAtlasLocation); |
320 | 328 |
321 if (NULL == plot) { | 329 if (NULL == plot) { |
322 return false; | 330 return false; |
323 } | 331 } |
324 | 332 |
325 glyph->fPlot = plot; | 333 glyph->fPlot = plot; |
326 return true; | 334 return true; |
327 } | 335 } |
OLD | NEW |