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 14 matching lines...) Expand all Loading... |
25 #define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT) | 25 #define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT) |
26 | 26 |
27 #define FONT_CACHE_STATS 0 | 27 #define FONT_CACHE_STATS 0 |
28 #if FONT_CACHE_STATS | 28 #if FONT_CACHE_STATS |
29 static int g_PurgeCount = 0; | 29 static int g_PurgeCount = 0; |
30 #endif | 30 #endif |
31 | 31 |
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 fAtlasMgr[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 fCache.deleteAll(); |
43 for (int i = 0; i < kAtlasCount; ++i) { | 43 for (int i = 0; i < kAtlasCount; ++i) { |
44 delete fAtlasMgr[i]; | 44 delete fAtlases[i]; |
45 } | 45 } |
46 fGpu->unref(); | 46 fGpu->unref(); |
47 #if FONT_CACHE_STATS | 47 #if FONT_CACHE_STATS |
48 GrPrintf("Num purges: %d\n", g_PurgeCount); | 48 GrPrintf("Num purges: %d\n", g_PurgeCount); |
49 #endif | 49 #endif |
50 } | 50 } |
51 | 51 |
52 static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { | 52 static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { |
53 static const GrPixelConfig sPixelConfigs[] = { | 53 static const GrPixelConfig sPixelConfigs[] = { |
54 kAlpha_8_GrPixelConfig, | 54 kAlpha_8_GrPixelConfig, |
(...skipping 17 matching lines...) Expand all Loading... |
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) { | 78 const Key& key) { |
79 GrMaskFormat format = scaler->getMaskFormat(); | 79 GrMaskFormat format = scaler->getMaskFormat(); |
80 GrPixelConfig config = mask_format_to_pixel_config(format); | 80 GrPixelConfig config = mask_format_to_pixel_config(format); |
81 int atlasIndex = mask_format_to_atlas_index(format); | 81 int atlasIndex = mask_format_to_atlas_index(format); |
82 if (NULL == fAtlasMgr[atlasIndex]) { | 82 if (NULL == fAtlases[atlasIndex]) { |
83 SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, | 83 SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, |
84 GR_ATLAS_TEXTURE_HEIGHT); | 84 GR_ATLAS_TEXTURE_HEIGHT); |
85 fAtlasMgr[atlasIndex] = SkNEW_ARGS(GrAtlasMgr, (fGpu, config, | 85 fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, |
86 textureSize, | 86 textureSize, |
87 GR_NUM_PLOTS_X, | 87 GR_NUM_PLOTS_X, |
88 GR_NUM_PLOTS_Y, | 88 GR_NUM_PLOTS_Y, |
89 true)); | 89 true)); |
90 } | 90 } |
91 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, | 91 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, |
92 (this, scaler->getKey(), format, fAtlasMgr
[atlasIndex])); | 92 (this, scaler->getKey(), format, fAtlases[
atlasIndex])); |
93 fCache.insert(key, strike); | 93 fCache.insert(key, strike); |
94 | 94 |
95 if (fHead) { | 95 if (fHead) { |
96 fHead->fPrev = strike; | 96 fHead->fPrev = strike; |
97 } else { | 97 } else { |
98 SkASSERT(NULL == fTail); | 98 SkASSERT(NULL == fTail); |
99 fTail = strike; | 99 fTail = strike; |
100 } | 100 } |
101 strike->fPrev = NULL; | 101 strike->fPrev = NULL; |
102 strike->fNext = fHead; | 102 strike->fNext = fHead; |
103 fHead = strike; | 103 fHead = strike; |
104 | 104 |
105 return strike; | 105 return strike; |
106 } | 106 } |
107 | 107 |
108 void GrFontCache::freeAll() { | 108 void GrFontCache::freeAll() { |
109 fCache.deleteAll(); | 109 fCache.deleteAll(); |
110 for (int i = 0; i < kAtlasCount; ++i) { | 110 for (int i = 0; i < kAtlasCount; ++i) { |
111 delete fAtlasMgr[i]; | 111 delete fAtlases[i]; |
112 fAtlasMgr[i] = NULL; | 112 fAtlases[i] = NULL; |
113 } | 113 } |
114 fHead = NULL; | 114 fHead = NULL; |
115 fTail = NULL; | 115 fTail = NULL; |
116 } | 116 } |
117 | 117 |
118 void GrFontCache::purgeStrike(GrTextStrike* strike) { | 118 void GrFontCache::purgeStrike(GrTextStrike* strike) { |
119 const GrFontCache::Key key(strike->fFontScalerKey); | 119 const GrFontCache::Key key(strike->fFontScalerKey); |
120 fCache.remove(key, strike); | 120 fCache.remove(key, strike); |
121 this->detachStrikeFromList(strike); | 121 this->detachStrikeFromList(strike); |
122 delete strike; | 122 delete strike; |
123 } | 123 } |
124 | 124 |
125 bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) { | 125 bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) { |
126 SkASSERT(NULL != preserveStrike); | 126 SkASSERT(NULL != preserveStrike); |
127 | 127 |
128 GrAtlasMgr* atlasMgr = preserveStrike->fAtlasMgr; | 128 GrAtlas* atlas = preserveStrike->fAtlas; |
129 GrPlot* plot = atlasMgr->getUnusedPlot(); | 129 GrPlot* plot = atlas->getUnusedPlot(); |
130 if (NULL == plot) { | 130 if (NULL == plot) { |
131 return false; | 131 return false; |
132 } | 132 } |
133 plot->resetRects(); | 133 plot->resetRects(); |
134 | 134 |
135 GrTextStrike* strike = fHead; | 135 GrTextStrike* strike = fHead; |
136 GrMaskFormat maskFormat = preserveStrike->fMaskFormat; | 136 GrMaskFormat maskFormat = preserveStrike->fMaskFormat; |
137 while (strike) { | 137 while (strike) { |
138 if (maskFormat != strike->fMaskFormat) { | 138 if (maskFormat != strike->fMaskFormat) { |
139 strike = strike->fNext; | 139 strike = strike->fNext; |
140 continue; | 140 continue; |
141 } | 141 } |
142 | 142 |
143 GrTextStrike* strikeToPurge = strike; | 143 GrTextStrike* strikeToPurge = strike; |
144 strike = strikeToPurge->fNext; | 144 strike = strikeToPurge->fNext; |
145 strikeToPurge->removePlot(plot); | 145 strikeToPurge->removePlot(plot); |
146 | 146 |
147 // clear out any empty strikes (except this one) | 147 // clear out any empty strikes (except this one) |
148 if (strikeToPurge != preserveStrike && strikeToPurge->fAtlas.isEmpty())
{ | 148 if (strikeToPurge != preserveStrike && strikeToPurge->fPlotUsage.isEmpty
()) { |
149 this->purgeStrike(strikeToPurge); | 149 this->purgeStrike(strikeToPurge); |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 #if FONT_CACHE_STATS | 153 #if FONT_CACHE_STATS |
154 ++g_PurgeCount; | 154 ++g_PurgeCount; |
155 #endif | 155 #endif |
156 | 156 |
157 return true; | 157 return true; |
158 } | 158 } |
(...skipping 24 matching lines...) Expand all Loading... |
183 count2 += 1; | 183 count2 += 1; |
184 strike = strike->fPrev; | 184 strike = strike->fPrev; |
185 } | 185 } |
186 SkASSERT(count == count2); | 186 SkASSERT(count == count2); |
187 } | 187 } |
188 #endif | 188 #endif |
189 | 189 |
190 void GrFontCache::dump() const { | 190 void GrFontCache::dump() const { |
191 static int gDumpCount = 0; | 191 static int gDumpCount = 0; |
192 for (int i = 0; i < kAtlasCount; ++i) { | 192 for (int i = 0; i < kAtlasCount; ++i) { |
193 if (NULL != fAtlasMgr[i]) { | 193 if (NULL != fAtlases[i]) { |
194 GrTexture* texture = fAtlasMgr[i]->getTexture(); | 194 GrTexture* texture = fAtlases[i]->getTexture(); |
195 if (NULL != texture) { | 195 if (NULL != texture) { |
196 SkString filename; | 196 SkString filename; |
197 #ifdef SK_BUILD_FOR_ANDROID | 197 #ifdef SK_BUILD_FOR_ANDROID |
198 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); | 198 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); |
199 #else | 199 #else |
200 filename.printf("fontcache_%d%d.png", gDumpCount, i); | 200 filename.printf("fontcache_%d%d.png", gDumpCount, i); |
201 #endif | 201 #endif |
202 texture->savePixels(filename.c_str()); | 202 texture->savePixels(filename.c_str()); |
203 } | 203 } |
204 } | 204 } |
(...skipping 10 matching lines...) Expand all Loading... |
215 /* | 215 /* |
216 The text strike is specific to a given font/style/matrix setup, which is | 216 The text strike is specific to a given font/style/matrix setup, which is |
217 represented by the GrHostFontScaler object we are given in getGlyph(). | 217 represented by the GrHostFontScaler object we are given in getGlyph(). |
218 | 218 |
219 We map a 32bit glyphID to a GrGlyph record, which in turn points to a | 219 We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
220 atlas and a position within that texture. | 220 atlas and a position within that texture. |
221 */ | 221 */ |
222 | 222 |
223 GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, | 223 GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, |
224 GrMaskFormat format, | 224 GrMaskFormat format, |
225 GrAtlasMgr* atlasMgr) : fPool(64) { | 225 GrAtlas* atlas) : fPool(64) { |
226 fFontScalerKey = key; | 226 fFontScalerKey = key; |
227 fFontScalerKey->ref(); | 227 fFontScalerKey->ref(); |
228 | 228 |
229 fFontCache = cache; // no need to ref, it won't go away before we do | 229 fFontCache = cache; // no need to ref, it won't go away before we do |
230 fAtlasMgr = atlasMgr; // no need to ref, it won't go away before we do | 230 fAtlas = atlas; // no need to ref, it won't go away before we do |
231 | 231 |
232 fMaskFormat = format; | 232 fMaskFormat = format; |
233 | 233 |
234 #ifdef SK_DEBUG | 234 #ifdef SK_DEBUG |
235 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); | 235 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
236 gCounter += 1; | 236 gCounter += 1; |
237 #endif | 237 #endif |
238 } | 238 } |
239 | 239 |
240 // this signature is needed because it's used with | 240 // this signature is needed because it's used with |
(...skipping 30 matching lines...) Expand all Loading... |
271 } | 271 } |
272 | 272 |
273 void GrTextStrike::removePlot(const GrPlot* plot) { | 273 void GrTextStrike::removePlot(const GrPlot* plot) { |
274 SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); | 274 SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); |
275 for (int i = 0; i < glyphArray.count(); ++i) { | 275 for (int i = 0; i < glyphArray.count(); ++i) { |
276 if (plot == glyphArray[i]->fPlot) { | 276 if (plot == glyphArray[i]->fPlot) { |
277 glyphArray[i]->fPlot = NULL; | 277 glyphArray[i]->fPlot = NULL; |
278 } | 278 } |
279 } | 279 } |
280 | 280 |
281 fAtlasMgr->removePlot(&fAtlas, plot); | 281 fAtlas->removePlot(&fPlotUsage, plot); |
282 } | 282 } |
283 | 283 |
284 | 284 |
285 bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { | 285 bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { |
286 #if 0 // testing hack to force us to flush our cache often | 286 #if 0 // testing hack to force us to flush our cache often |
287 static int gCounter; | 287 static int gCounter; |
288 if ((++gCounter % 10) == 0) return false; | 288 if ((++gCounter % 10) == 0) return false; |
289 #endif | 289 #endif |
290 | 290 |
291 SkASSERT(glyph); | 291 SkASSERT(glyph); |
(...skipping 15 matching lines...) Expand all Loading... |
307 } | 307 } |
308 } else { | 308 } else { |
309 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), | 309 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
310 glyph->height(), | 310 glyph->height(), |
311 glyph->width() * bytesPerPixel, | 311 glyph->width() * bytesPerPixel, |
312 storage.get())) { | 312 storage.get())) { |
313 return false; | 313 return false; |
314 } | 314 } |
315 } | 315 } |
316 | 316 |
317 GrPlot* plot = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), | 317 GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, glyph->width(), |
318 glyph->height(), storage.get(), | 318 glyph->height(), storage.get(), |
319 &glyph->fAtlasLocation); | 319 &glyph->fAtlasLocation); |
320 | 320 |
321 if (NULL == plot) { | 321 if (NULL == plot) { |
322 return false; | 322 return false; |
323 } | 323 } |
324 | 324 |
325 glyph->fPlot = plot; | 325 glyph->fPlot = plot; |
326 return true; | 326 return true; |
327 } | 327 } |
OLD | NEW |