Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
|
robertphillips
2015/03/23 19:03:12
2010?
joshualitt
2015/03/23 19:56:08
Acknowledged.
| |
| 2 * Copyright 2010 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "GrBatchFontCache.h" | |
| 9 #include "GrFontAtlasSizes.h" | |
| 10 #include "GrGpu.h" | |
| 11 #include "GrRectanizer.h" | |
| 12 #include "GrSurfacePriv.h" | |
| 13 #include "SkString.h" | |
| 14 | |
| 15 #include "SkDistanceFieldGen.h" | |
| 16 | |
| 17 /////////////////////////////////////////////////////////////////////////////// | |
| 18 | |
| 19 static GrBatchAtlas* make_atlas(GrContext* context, GrPixelConfig config, | |
| 20 int textureWidth, int textureHeight, | |
| 21 int numPlotsX, int numPlotsY) { | |
| 22 GrSurfaceDesc desc; | |
| 23 desc.fFlags = kNone_GrSurfaceFlags; | |
| 24 desc.fWidth = textureWidth; | |
| 25 desc.fHeight = textureHeight; | |
| 26 desc.fConfig = config; | |
| 27 | |
| 28 // We don't want to flush the context so we claim we're in the middle of flu shing so as to | |
| 29 // guarantee we do not recieve a texture with pending IO | |
| 30 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox_Scr atchTexMatch, | |
|
robertphillips
2015/03/23 19:03:11
xtra space ?
joshualitt
2015/03/23 19:56:08
Acknowledged.
| |
| 31 true); | |
| 32 return SkNEW_ARGS(GrBatchAtlas, (texture, numPlotsX, numPlotsY)); | |
| 33 } | |
| 34 | |
| 35 int GrBatchFontCache::MaskFormatToAtlasIndex(GrMaskFormat format) { | |
| 36 static const int sAtlasIndices[] = { | |
| 37 GrBatchFontCache::kA8_AtlasType, | |
| 38 GrBatchFontCache::k565_AtlasType, | |
| 39 GrBatchFontCache::k8888_AtlasType | |
| 40 }; | |
| 41 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_s ize_mismatch); | |
| 42 | |
| 43 SkASSERT(sAtlasIndices[format] < GrBatchFontCache::kAtlasCount); | |
| 44 return sAtlasIndices[format]; | |
| 45 } | |
| 46 | |
| 47 GrMaskFormat GrBatchFontCache::AtlasIndexToMaskFormat(int atlasIndex) { | |
| 48 static GrMaskFormat sMaskFormats[] = { | |
| 49 kA8_GrMaskFormat, | |
| 50 kA565_GrMaskFormat, | |
| 51 kARGB_GrMaskFormat, | |
| 52 }; | |
| 53 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sMaskFormats) == kMaskFormatCount, array_si ze_mismatch); | |
| 54 | |
| 55 SkASSERT(sMaskFormats[atlasIndex] < GrBatchFontCache::kAtlasCount); | |
| 56 return sMaskFormats[atlasIndex]; | |
| 57 } | |
| 58 | |
| 59 GrBatchFontCache::GrBatchFontCache(GrContext* context) | |
| 60 : fPreserveStrike(NULL) { | |
| 61 for (int i = 0; i < kAtlasCount; i++) { | |
| 62 GrMaskFormat format = AtlasIndexToMaskFormat(i); | |
| 63 GrPixelConfig config = this->getPixelConfig(format); | |
| 64 | |
| 65 if (kA8_GrMaskFormat == format) { | |
| 66 fAtlases[i] = make_atlas(context, config, | |
| 67 GR_FONT_ATLAS_A8_TEXTURE_WIDTH, | |
| 68 GR_FONT_ATLAS_TEXTURE_HEIGHT, | |
| 69 GR_FONT_ATLAS_A8_NUM_PLOTS_X, | |
| 70 GR_FONT_ATLAS_NUM_PLOTS_Y); | |
| 71 } else { | |
| 72 fAtlases[i] = make_atlas(context, config, | |
| 73 GR_FONT_ATLAS_TEXTURE_WIDTH, | |
| 74 GR_FONT_ATLAS_TEXTURE_HEIGHT, | |
| 75 GR_FONT_ATLAS_NUM_PLOTS_X, | |
| 76 GR_FONT_ATLAS_NUM_PLOTS_Y); | |
| 77 } | |
| 78 | |
| 79 fAtlases[i]->registerEvictionCallback(&GrBatchFontCache::HandleEviction, (void*)this); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 GrBatchFontCache::~GrBatchFontCache() { | |
| 84 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache); | |
| 85 while (!iter.done()) { | |
| 86 SkDELETE(&(*iter)); | |
| 87 ++iter; | |
| 88 } | |
| 89 for (int i = 0; i < kAtlasCount; ++i) { | |
|
robertphillips
2015/03/23 19:03:11
SkDELETE ?
joshualitt
2015/03/23 19:56:08
Acknowledged.
| |
| 90 delete fAtlases[i]; | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 GrBatchTextStrike* GrBatchFontCache::generateStrike(GrFontScaler* scaler) { | |
| 95 GrBatchTextStrike* strike = SkNEW_ARGS(GrBatchTextStrike, (this, scaler->get Key())); | |
| 96 fCache.add(strike); | |
| 97 fStrikeList.addToHead(strike); | |
| 98 return strike; | |
| 99 } | |
| 100 | |
| 101 void GrBatchFontCache::freeAll() { | |
| 102 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache); | |
| 103 while (!iter.done()) { | |
| 104 SkDELETE(&(*iter)); | |
| 105 ++iter; | |
| 106 } | |
| 107 fCache.rewind(); | |
| 108 for (int i = 0; i < kAtlasCount; ++i) { | |
|
robertphillips
2015/03/23 19:03:12
SkDELETE ?
joshualitt
2015/03/23 19:56:08
Acknowledged.
| |
| 109 delete fAtlases[i]; | |
| 110 fAtlases[i] = NULL; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void GrBatchFontCache::purgeStrike(GrBatchTextStrike* strike) { | |
| 115 fCache.remove(*(strike->fFontScalerKey)); | |
| 116 fStrikeList.remove(strike); | |
|
robertphillips
2015/03/23 19:03:11
SkDELETE ?
joshualitt
2015/03/23 19:56:08
Acknowledged.
| |
| 117 delete strike; | |
| 118 } | |
| 119 | |
| 120 inline GrBatchAtlas* GrBatchFontCache::getAtlas(GrMaskFormat format) const { | |
| 121 int atlasIndex = MaskFormatToAtlasIndex(format); | |
| 122 SkASSERT(fAtlases[atlasIndex]); | |
| 123 return fAtlases[atlasIndex]; | |
| 124 } | |
| 125 | |
| 126 bool GrBatchFontCache::hasGlyph(GrGlyph* glyph) { | |
| 127 SkASSERT(glyph); | |
|
robertphillips
2015/03/23 19:03:11
this-> ?
joshualitt
2015/03/23 19:56:08
Acknowledged.
| |
| 128 return getAtlas(glyph->fMaskFormat)->hasID(glyph->fID); | |
| 129 } | |
| 130 | |
| 131 void GrBatchFontCache::refGlyph(GrGlyph* glyph, GrBatchAtlas::BatchToken batchTo ken) { | |
| 132 SkASSERT(glyph); | |
|
robertphillips
2015/03/23 19:03:11
2x this-> ?
| |
| 133 SkASSERT(getAtlas(glyph->fMaskFormat)->hasID(glyph->fID)); | |
| 134 getAtlas(glyph->fMaskFormat)->setLastRefToken(glyph->fID, batchToken); | |
| 135 } | |
| 136 | |
| 137 bool GrBatchFontCache::addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::Atlas ID* id, | |
| 138 GrBatchTarget* batchTarget, | |
| 139 GrMaskFormat format, int width, int height, co nst void* image, | |
| 140 SkIPoint16* loc) { | |
| 141 fPreserveStrike = strike; | |
|
robertphillips
2015/03/23 19:03:11
this-> ?
| |
| 142 return getAtlas(format)->addToAtlas(id, batchTarget, width, height, image, l oc); | |
| 143 } | |
| 144 | |
| 145 uint32_t GrBatchFontCache::atlasGeneration(GrMaskFormat format) const { | |
|
robertphillips
2015/03/23 19:03:11
this-> ?
| |
| 146 return getAtlas(format)->atlasGeneration(); | |
| 147 } | |
| 148 | |
| 149 GrTexture* GrBatchFontCache::getTexture(GrMaskFormat format) { | |
| 150 int atlasIndex = MaskFormatToAtlasIndex(format); | |
| 151 SkASSERT(fAtlases[atlasIndex]); | |
| 152 return fAtlases[atlasIndex]->getTexture(); | |
| 153 } | |
| 154 | |
| 155 GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const { | |
| 156 static const GrPixelConfig sPixelConfigs[] = { | |
| 157 kAlpha_8_GrPixelConfig, | |
| 158 kRGB_565_GrPixelConfig, | |
| 159 kSkia8888_GrPixelConfig | |
| 160 }; | |
| 161 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_s ize_mismatch); | |
| 162 | |
| 163 return sPixelConfigs[format]; | |
| 164 } | |
| 165 | |
| 166 void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) { | |
| 167 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr); | |
| 168 | |
| 169 GrBatchStrikeList::Iter plotIter; | |
| 170 plotIter.init(fontCache->fStrikeList, GrBatchStrikeList::Iter::kHead_IterSta rt); | |
| 171 | |
| 172 GrBatchTextStrike* strike; | |
| 173 while ((strike = plotIter.get())) { | |
| 174 // We advance the pointer here because we might delete the strike | |
| 175 plotIter.next(); | |
| 176 strike->removeID(id); | |
| 177 | |
| 178 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas | |
| 179 // triggered the eviction | |
| 180 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) { | |
| 181 fontCache->purgeStrike(strike); | |
| 182 } | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 #ifdef SK_DEBUG | |
| 187 void GrBatchFontCache::validate() const { | |
| 188 SkASSERT(fCache.count() == fStrikeList.countEntries()); | |
| 189 } | |
| 190 #endif | |
| 191 | |
| 192 void GrBatchFontCache::dump() const { | |
| 193 static int gDumpCount = 0; | |
| 194 for (int i = 0; i < kAtlasCount; ++i) { | |
| 195 if (fAtlases[i]) { | |
| 196 GrTexture* texture = fAtlases[i]->getTexture(); | |
| 197 if (texture) { | |
| 198 SkString filename; | |
| 199 #ifdef SK_BUILD_FOR_ANDROID | |
| 200 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); | |
| 201 #else | |
| 202 filename.printf("fontcache_%d%d.png", gDumpCount, i); | |
| 203 #endif | |
| 204 texture->surfacePriv().savePixels(filename.c_str()); | |
| 205 } | |
| 206 } | |
| 207 } | |
| 208 ++gDumpCount; | |
| 209 } | |
| 210 | |
| 211 /////////////////////////////////////////////////////////////////////////////// | |
| 212 | |
| 213 /* | |
| 214 The text strike is specific to a given font/style/matrix setup, which is | |
| 215 represented by the GrHostFontScaler object we are given in getGlyph(). | |
| 216 | |
| 217 We map a 32bit glyphID to a GrGlyph record, which in turn points to a | |
| 218 atlas and a position within that texture. | |
| 219 */ | |
| 220 | |
| 221 GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKe y* key) | |
| 222 : fPool(9/*start allocations at 512 bytes*/) | |
| 223 , fAtlasedGlyphs(0) { | |
|
robertphillips
2015/03/23 19:03:11
use SkRef(key) and remove manual ref ?
Could fFont
| |
| 224 fFontScalerKey = key; | |
| 225 fFontScalerKey->ref(); | |
| 226 | |
| 227 fBatchFontCache = cache; // no need to ref, it won't go away before we d o | |
| 228 } | |
| 229 | |
| 230 GrBatchTextStrike::~GrBatchTextStrike() { | |
| 231 fFontScalerKey->unref(); | |
| 232 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); | |
| 233 while (!iter.done()) { | |
| 234 (*iter).free(); | |
| 235 ++iter; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed, | |
| 240 GrFontScaler* scaler) { | |
| 241 SkIRect bounds; | |
| 242 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) { | |
| 243 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) { | |
| 244 return NULL; | |
| 245 } | |
| 246 } else { | |
| 247 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { | |
| 248 return NULL; | |
| 249 } | |
| 250 } | |
| 251 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed); | |
| 252 | |
| 253 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW); | |
| 254 glyph->init(packed, bounds, format); | |
| 255 fCache.add(glyph); | |
| 256 return glyph; | |
| 257 } | |
| 258 | |
| 259 void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) { | |
| 260 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); | |
| 261 while (!iter.done()) { | |
| 262 if (id == (*iter).fID) { | |
| 263 (*iter).fID = GrBatchAtlas::kInvalidAtlasID; | |
| 264 fAtlasedGlyphs--; | |
| 265 SkASSERT(fAtlasedGlyphs >= 0); | |
| 266 } | |
| 267 ++iter; | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 bool GrBatchTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) { | |
| 272 int width = glyph->fBounds.width(); | |
| 273 int height = glyph->fBounds.height(); | |
| 274 bool useDistanceField = | |
| 275 (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPa ckedID)); | |
| 276 int pad = useDistanceField ? 2 * SK_DistanceFieldPad : 0; | |
| 277 int plotWidth = (kA8_GrMaskFormat == glyph->fMaskFormat) ? GR_FONT_ATLAS_A8_ PLOT_WIDTH | |
| 278 : GR_FONT_ATLAS_PLO T_WIDTH; | |
| 279 if (width + pad > plotWidth) { | |
| 280 return true; | |
| 281 } | |
| 282 if (height + pad > GR_FONT_ATLAS_PLOT_HEIGHT) { | |
| 283 return true; | |
| 284 } | |
| 285 | |
| 286 return false; | |
| 287 } | |
| 288 | |
| 289 bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* gly ph, | |
| 290 GrFontScaler* scaler) { | |
| 291 SkASSERT(glyph); | |
| 292 SkASSERT(scaler); | |
| 293 SkASSERT(fCache.find(glyph->fPackedID)); | |
| 294 SkASSERT(NULL == glyph->fPlot); | |
| 295 | |
| 296 SkAutoUnref ar(SkSafeRef(scaler)); | |
| 297 | |
| 298 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat); | |
| 299 | |
| 300 size_t size = glyph->fBounds.area() * bytesPerPixel; | |
| 301 GrAutoMalloc<1024> storage(size); | |
| 302 | |
| 303 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedI D)) { | |
| 304 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(), | |
| 305 glyph->height(), | |
| 306 storage.get())) { | |
| 307 return false; | |
| 308 } | |
| 309 } else { | |
| 310 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), | |
| 311 glyph->height(), | |
| 312 glyph->width() * bytesPerPixel, | |
| 313 storage.get())) { | |
| 314 return false; | |
| 315 } | |
| 316 } | |
| 317 | |
| 318 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, g lyph->fMaskFormat, | |
| 319 glyph->width(), glyph->height(), | |
| 320 storage.get(), &glyph->fAtlasLoca tion); | |
| 321 if (success) { | |
| 322 fAtlasedGlyphs++; | |
| 323 } | |
| 324 return success; | |
| 325 } | |
| OLD | NEW |