| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #include "GrAtlas.h" |
| 9 | #include "GrGpu.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #include "GrRectanizer.h" |
| 11 | #include "GrTextStrike.h" |
| 12 | #include "GrTextStrike_impl.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 14 | SK_DEFINE_INST_COUNT(GrFontScaler) |
| 15 | SK_DEFINE_INST_COUNT(GrKey) |
| 16 | |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 19 | #define FONT_CACHE_STATS 0 |
| 20 | #if FONT_CACHE_STATS |
| 21 | static int g_PurgeCount = 0; |
| 22 | #endif |
| 23 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
| 25 | gpu->ref(); |
| 26 | fAtlasMgr = NULL; |
| 27 | |
| 28 | fHead = fTail = NULL; |
| 29 | } |
| 30 | |
| 31 | GrFontCache::~GrFontCache() { |
| 32 | fCache.deleteAll(); |
| 33 | delete fAtlasMgr; |
| 34 | fGpu->unref(); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 35 | #if FONT_CACHE_STATS |
| 36 | GrPrintf("Num purges: %d\n", g_PurgeCount); |
| 37 | #endif |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, |
| 41 | const Key& key) { |
| 42 | if (NULL == fAtlasMgr) { |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 43 | fAtlasMgr = SkNEW_ARGS(GrAtlasMgr, (fGpu)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | } |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 45 | GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, |
| 46 | (this, scaler->getKey(), |
| 47 | scaler->getMaskFormat(), fAtlasMgr)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | fCache.insert(key, strike); |
| 49 | |
| 50 | if (fHead) { |
| 51 | fHead->fPrev = strike; |
| 52 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 53 | SkASSERT(NULL == fTail); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | fTail = strike; |
| 55 | } |
| 56 | strike->fPrev = NULL; |
| 57 | strike->fNext = fHead; |
| 58 | fHead = strike; |
| 59 | |
| 60 | return strike; |
| 61 | } |
| 62 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | void GrFontCache::freeAll() { |
| 64 | fCache.deleteAll(); |
| 65 | delete fAtlasMgr; |
| 66 | fAtlasMgr = NULL; |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 67 | fHead = NULL; |
| 68 | fTail = NULL; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void GrFontCache::purgeExceptFor(GrTextStrike* preserveStrike) { |
| 72 | GrTextStrike* strike = fTail; |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 73 | bool purge = true; |
| bsalomon@google.com | 7359eae | 2011-06-21 21:18:25 +0000 | [diff] [blame] | 74 | while (strike) { |
| 75 | if (strike == preserveStrike) { |
| 76 | strike = strike->fPrev; |
| 77 | continue; |
| 78 | } |
| 79 | GrTextStrike* strikeToPurge = strike; |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 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); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 85 | SkASSERT(index >= 0); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 86 | fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()); |
| 87 | this->detachStrikeFromList(strikeToPurge); |
| 88 | delete strikeToPurge; |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | #if FONT_CACHE_STATS |
| 92 | ++g_PurgeCount; |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | void GrFontCache::freeAtlasExceptFor(GrTextStrike* preserveStrike) { |
| 97 | GrTextStrike* strike = fTail; |
| 98 | while (strike) { |
| 99 | if (strike == preserveStrike) { |
| 100 | strike = strike->fPrev; |
| 101 | continue; |
| 102 | } |
| 103 | GrTextStrike* strikeToPurge = strike; |
| 104 | strike = strikeToPurge->fPrev; |
| 105 | if (strikeToPurge->removeUnusedAtlases()) { |
| 106 | if (NULL == strikeToPurge->fAtlas) { |
| 107 | int index = fCache.slowFindIndex(strikeToPurge); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 108 | SkASSERT(index >= 0); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 109 | fCache.removeAt(index, strikeToPurge->fFontScalerKey->getHash()); |
| 110 | this->detachStrikeFromList(strikeToPurge); |
| 111 | delete strikeToPurge; |
| 112 | } |
| 113 | break; |
| 114 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 118 | #ifdef SK_DEBUG |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | void GrFontCache::validate() const { |
| 120 | int count = fCache.count(); |
| 121 | if (0 == count) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 122 | SkASSERT(!fHead); |
| 123 | SkASSERT(!fTail); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | } else if (1 == count) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 125 | SkASSERT(fHead == fTail); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 127 | SkASSERT(fHead != fTail); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int count2 = 0; |
| 131 | const GrTextStrike* strike = fHead; |
| 132 | while (strike) { |
| 133 | count2 += 1; |
| 134 | strike = strike->fNext; |
| 135 | } |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 136 | SkASSERT(count == count2); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | |
| 138 | count2 = 0; |
| 139 | strike = fTail; |
| 140 | while (strike) { |
| 141 | count2 += 1; |
| 142 | strike = strike->fPrev; |
| 143 | } |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 144 | SkASSERT(count == count2); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | } |
| 146 | #endif |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 150 | #ifdef SK_DEBUG |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | static int gCounter; |
| 152 | #endif |
| 153 | |
| 154 | /* |
| 155 | The text strike is specific to a given font/style/matrix setup, which is |
| 156 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 157 | |
| 158 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 159 | atlas and a position within that texture. |
| 160 | */ |
| 161 | |
| 162 | GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 163 | GrMaskFormat format, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | GrAtlasMgr* atlasMgr) : fPool(64) { |
| 165 | fFontScalerKey = key; |
| 166 | fFontScalerKey->ref(); |
| 167 | |
| 168 | fFontCache = cache; // no need to ref, it won't go away before we do |
| 169 | fAtlasMgr = atlasMgr; // no need to ref, it won't go away before we do |
| 170 | fAtlas = NULL; |
| 171 | |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 172 | fMaskFormat = format; |
| 173 | |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 174 | #ifdef SK_DEBUG |
| reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 175 | // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | gCounter += 1; |
| 177 | #endif |
| 178 | } |
| 179 | |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 180 | // these signatures are needed because they're used with |
| 181 | // SkTDArray::visitAll() (see destructor & removeUnusedAtlases()) |
| 182 | static void free_glyph(GrGlyph*& glyph) { glyph->free(); } |
| 183 | |
| 184 | static void invalidate_glyph(GrGlyph*& glyph) { |
| commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 185 | if (glyph->fAtlas && glyph->fAtlas->drawToken().isIssued()) { |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 186 | glyph->fAtlas = NULL; |
| 187 | } |
| 188 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 | |
| 190 | GrTextStrike::~GrTextStrike() { |
| 191 | GrAtlas::FreeLList(fAtlas); |
| 192 | fFontScalerKey->unref(); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 193 | fCache.getArray().visitAll(free_glyph); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 195 | #ifdef SK_DEBUG |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 196 | gCounter -= 1; |
| reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 197 | // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 198 | #endif |
| 199 | } |
| 200 | |
| 201 | GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
| 202 | GrFontScaler* scaler) { |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 203 | SkIRect bounds; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | GrGlyph* glyph = fPool.alloc(); |
| 209 | glyph->init(packed, bounds); |
| 210 | fCache.insert(packed, glyph); |
| 211 | return glyph; |
| 212 | } |
| 213 | |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 214 | bool GrTextStrike::removeUnusedAtlases() { |
| 215 | fCache.getArray().visitAll(invalidate_glyph); |
| 216 | return GrAtlas::RemoveUnusedAtlases(fAtlasMgr, &fAtlas); |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 219 | bool GrTextStrike::getGlyphAtlas(GrGlyph* glyph, GrFontScaler* scaler, |
| 220 | GrDrawTarget::DrawToken currentDrawToken) { |
| reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 221 | #if 0 // testing hack to force us to flush our cache often |
| 222 | static int gCounter; |
| 223 | if ((++gCounter % 10) == 0) return false; |
| 224 | #endif |
| 225 | |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 226 | SkASSERT(glyph); |
| 227 | SkASSERT(scaler); |
| 228 | SkASSERT(fCache.contains(glyph)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | if (glyph->fAtlas) { |
| commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 230 | glyph->fAtlas->setDrawToken(currentDrawToken); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 234 | SkAutoRef ar(scaler); |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 235 | |
| 236 | int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
| 237 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
| bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 238 | SkAutoSMalloc<1024> storage(size); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 240 | glyph->height(), |
| 241 | glyph->width() * bytesPerPixel, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | storage.get())) { |
| 243 | return false; |
| 244 | } |
| 245 | |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 246 | GrAtlas* atlas = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | glyph->height(), storage.get(), |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 248 | fMaskFormat, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | &glyph->fAtlasLocation); |
| 250 | if (NULL == atlas) { |
| 251 | return false; |
| 252 | } |
| 253 | |
| commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 254 | glyph->fAtlas = atlas; |
| commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 255 | atlas->setDrawToken(currentDrawToken); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 256 | return true; |
| 257 | } |