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