| 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" |
| 11 #include "GrTextStrike.h" | 11 #include "GrTextStrike.h" |
| 12 #include "GrTextStrike_impl.h" | 12 #include "GrTextStrike_impl.h" |
| 13 | 13 |
| 14 SK_DEFINE_INST_COUNT(GrFontScaler) | 14 SK_DEFINE_INST_COUNT(GrFontScaler) |
| 15 SK_DEFINE_INST_COUNT(GrKey) | 15 SK_DEFINE_INST_COUNT(GrKey) |
| 16 | 16 |
| 17 /////////////////////////////////////////////////////////////////////////////// | 17 /////////////////////////////////////////////////////////////////////////////// |
| 18 | 18 |
| 19 #define FONT_CACHE_STATS 0 |
| 20 #if FONT_CACHE_STATS |
| 21 static int g_PurgeCount = 0; |
| 22 #endif |
| 23 |
| 19 GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { | 24 GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
| 20 gpu->ref(); | 25 gpu->ref(); |
| 21 fAtlasMgr = NULL; | 26 fAtlasMgr = NULL; |
| 22 | 27 |
| 23 fHead = fTail = NULL; | 28 fHead = fTail = NULL; |
| 24 } | 29 } |
| 25 | 30 |
| 26 GrFontCache::~GrFontCache() { | 31 GrFontCache::~GrFontCache() { |
| 27 fCache.deleteAll(); | 32 fCache.deleteAll(); |
| 28 delete fAtlasMgr; | 33 delete fAtlasMgr; |
| 29 fGpu->unref(); | 34 fGpu->unref(); |
| 35 #if FONT_CACHE_STATS |
| 36 GrPrintf("Num purges: %d\n", g_PurgeCount); |
| 37 #endif |
| 30 } | 38 } |
| 31 | 39 |
| 32 GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, | 40 GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, |
| 33 const Key& key) { | 41 const Key& key) { |
| 34 if (NULL == fAtlasMgr) { | 42 if (NULL == fAtlasMgr) { |
| 35 fAtlasMgr = SkNEW_ARGS(GrAtlasMgr, (fGpu)); | 43 fAtlasMgr = SkNEW_ARGS(GrAtlasMgr, (fGpu)); |
| 36 } | 44 } |
| 37 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, | 45 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, |
| 38 (this, scaler->getKey(), | 46 (this, scaler->getKey(), |
| 39 scaler->getMaskFormat(), fAtlasMgr)); | 47 scaler->getMaskFormat(), fAtlasMgr)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 55 void GrFontCache::freeAll() { | 63 void GrFontCache::freeAll() { |
| 56 fCache.deleteAll(); | 64 fCache.deleteAll(); |
| 57 delete fAtlasMgr; | 65 delete fAtlasMgr; |
| 58 fAtlasMgr = NULL; | 66 fAtlasMgr = NULL; |
| 59 fHead = NULL; | 67 fHead = NULL; |
| 60 fTail = NULL; | 68 fTail = NULL; |
| 61 } | 69 } |
| 62 | 70 |
| 63 void GrFontCache::purgeExceptFor(GrTextStrike* preserveStrike) { | 71 void GrFontCache::purgeExceptFor(GrTextStrike* preserveStrike) { |
| 64 GrTextStrike* strike = fTail; | 72 GrTextStrike* strike = fTail; |
| 73 bool purge = true; |
| 74 while (strike) { |
| 75 if (strike == preserveStrike) { |
| 76 strike = strike->fPrev; |
| 77 continue; |
| 78 } |
| 79 GrTextStrike* strikeToPurge = strike; |
| 80 strike = strikeToPurge->fPrev; |
| 81 if (purge) { |
| 82 // keep purging if we won't free up any atlases with this strike. |
| 83 purge = (NULL == strikeToPurge->fAtlas); |
| 84 int index = fCache.slowFindIndex(strikeToPurge); |
| 85 GrAssert(index >= 0); |
| 86 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()); |
| 87 this->detachStrikeFromList(strikeToPurge); |
| 88 delete strikeToPurge; |
| 89 } else { |
| 90 // for the remaining strikes, we just mark them unused |
| 91 GrAtlas::MarkAllUnused(strikeToPurge->fAtlas); |
| 92 } |
| 93 } |
| 94 #if FONT_CACHE_STATS |
| 95 ++g_PurgeCount; |
| 96 #endif |
| 97 } |
| 98 |
| 99 void GrFontCache::freeAtlasExceptFor(GrTextStrike* preserveStrike) { |
| 100 GrTextStrike* strike = fTail; |
| 65 while (strike) { | 101 while (strike) { |
| 66 if (strike == preserveStrike) { | 102 if (strike == preserveStrike) { |
| 67 strike = strike->fPrev; | 103 strike = strike->fPrev; |
| 68 continue; | 104 continue; |
| 69 } | 105 } |
| 70 GrTextStrike* strikeToPurge = strike; | 106 GrTextStrike* strikeToPurge = strike; |
| 71 // keep going if we won't free up any atlases with this strike. | 107 strike = strikeToPurge->fPrev; |
| 72 strike = (NULL == strikeToPurge->fAtlas) ? strikeToPurge->fPrev : NULL; | 108 if (strikeToPurge->removeUnusedAtlases()) { |
| 73 int index = fCache.slowFindIndex(strikeToPurge); | 109 if (NULL == strikeToPurge->fAtlas) { |
| 74 GrAssert(index >= 0); | 110 int index = fCache.slowFindIndex(strikeToPurge); |
| 75 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()); | 111 GrAssert(index >= 0); |
| 76 this->detachStrikeFromList(strikeToPurge); | 112 fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash())
; |
| 77 delete strikeToPurge; | 113 this->detachStrikeFromList(strikeToPurge); |
| 114 delete strikeToPurge; |
| 115 } |
| 116 break; |
| 117 } |
| 78 } | 118 } |
| 79 } | 119 } |
| 80 | 120 |
| 81 #if GR_DEBUG | 121 #if GR_DEBUG |
| 82 void GrFontCache::validate() const { | 122 void GrFontCache::validate() const { |
| 83 int count = fCache.count(); | 123 int count = fCache.count(); |
| 84 if (0 == count) { | 124 if (0 == count) { |
| 85 GrAssert(!fHead); | 125 GrAssert(!fHead); |
| 86 GrAssert(!fTail); | 126 GrAssert(!fTail); |
| 87 } else if (1 == count) { | 127 } else if (1 == count) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 fAtlas = NULL; | 173 fAtlas = NULL; |
| 134 | 174 |
| 135 fMaskFormat = format; | 175 fMaskFormat = format; |
| 136 | 176 |
| 137 #if GR_DEBUG | 177 #if GR_DEBUG |
| 138 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); | 178 // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
| 139 gCounter += 1; | 179 gCounter += 1; |
| 140 #endif | 180 #endif |
| 141 } | 181 } |
| 142 | 182 |
| 143 static void FreeGlyph(GrGlyph*& glyph) { glyph->free(); } | 183 // these signatures are needed because they're used with |
| 184 // SkTDArray::visitAll() (see destructor & removeUnusedAtlases()) |
| 185 static void free_glyph(GrGlyph*& glyph) { glyph->free(); } |
| 186 |
| 187 static void invalidate_glyph(GrGlyph*& glyph) { |
| 188 if (glyph->fAtlas && !glyph->fAtlas->used()) { |
| 189 glyph->fAtlas = NULL; |
| 190 } |
| 191 } |
| 144 | 192 |
| 145 GrTextStrike::~GrTextStrike() { | 193 GrTextStrike::~GrTextStrike() { |
| 146 GrAtlas::FreeLList(fAtlas); | 194 GrAtlas::FreeLList(fAtlas); |
| 147 fFontScalerKey->unref(); | 195 fFontScalerKey->unref(); |
| 148 fCache.getArray().visitAll(FreeGlyph); | 196 fCache.getArray().visitAll(free_glyph); |
| 149 | 197 |
| 150 #if GR_DEBUG | 198 #if GR_DEBUG |
| 151 gCounter -= 1; | 199 gCounter -= 1; |
| 152 // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); | 200 // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
| 153 #endif | 201 #endif |
| 154 } | 202 } |
| 155 | 203 |
| 156 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, | 204 GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
| 157 GrFontScaler* scaler) { | 205 GrFontScaler* scaler) { |
| 158 SkIRect bounds; | 206 SkIRect bounds; |
| 159 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { | 207 if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
| 160 return NULL; | 208 return NULL; |
| 161 } | 209 } |
| 162 | 210 |
| 163 GrGlyph* glyph = fPool.alloc(); | 211 GrGlyph* glyph = fPool.alloc(); |
| 164 glyph->init(packed, bounds); | 212 glyph->init(packed, bounds); |
| 165 fCache.insert(packed, glyph); | 213 fCache.insert(packed, glyph); |
| 166 return glyph; | 214 return glyph; |
| 167 } | 215 } |
| 168 | 216 |
| 217 bool GrTextStrike::removeUnusedAtlases() { |
| 218 fCache.getArray().visitAll(invalidate_glyph); |
| 219 return GrAtlas::RemoveUnusedAtlases(fAtlasMgr, &fAtlas); |
| 220 |
| 221 return false; |
| 222 } |
| 223 |
| 169 bool GrTextStrike::getGlyphAtlas(GrGlyph* glyph, GrFontScaler* scaler) { | 224 bool GrTextStrike::getGlyphAtlas(GrGlyph* glyph, GrFontScaler* scaler) { |
| 170 #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 |
| 171 static int gCounter; | 226 static int gCounter; |
| 172 if ((++gCounter % 10) == 0) return false; | 227 if ((++gCounter % 10) == 0) return false; |
| 173 #endif | 228 #endif |
| 174 | 229 |
| 175 GrAssert(glyph); | 230 GrAssert(glyph); |
| 176 GrAssert(scaler); | 231 GrAssert(scaler); |
| 177 GrAssert(fCache.contains(glyph)); | 232 GrAssert(fCache.contains(glyph)); |
| 178 if (glyph->fAtlas) { | 233 if (glyph->fAtlas) { |
| 234 glyph->fAtlas->setUsed(true); |
| 179 return true; | 235 return true; |
| 180 } | 236 } |
| 181 | 237 |
| 182 GrAutoRef ar(scaler); | 238 GrAutoRef ar(scaler); |
| 183 | 239 |
| 184 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); | 240 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
| 185 size_t size = glyph->fBounds.area() * bytesPerPixel; | 241 size_t size = glyph->fBounds.area() * bytesPerPixel; |
| 186 SkAutoSMalloc<1024> storage(size); | 242 SkAutoSMalloc<1024> storage(size); |
| 187 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), | 243 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
| 188 glyph->height(), | 244 glyph->height(), |
| 189 glyph->width() * bytesPerPixel, | 245 glyph->width() * bytesPerPixel, |
| 190 storage.get())) { | 246 storage.get())) { |
| 191 return false; | 247 return false; |
| 192 } | 248 } |
| 193 | 249 |
| 194 GrAtlas* atlas = fAtlasMgr->addToAtlas(fAtlas, glyph->width(), | 250 GrAtlas* atlas = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), |
| 195 glyph->height(), storage.get(), | 251 glyph->height(), storage.get(), |
| 196 fMaskFormat, | 252 fMaskFormat, |
| 197 &glyph->fAtlasLocation); | 253 &glyph->fAtlasLocation); |
| 198 if (NULL == atlas) { | 254 if (NULL == atlas) { |
| 199 return false; | 255 return false; |
| 200 } | 256 } |
| 201 | 257 |
| 202 // update fAtlas as well, since they may be chained in a linklist | 258 glyph->fAtlas = atlas; |
| 203 glyph->fAtlas = fAtlas = atlas; | 259 atlas->setUsed(true); |
| 204 return true; | 260 return true; |
| 205 } | 261 } |
| OLD | NEW |