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 "GrGpu.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "GrRectanizer.h" |
| 10 | #include "GrTextStrike.h" |
| 11 | #include "GrTextStrike_impl.h" |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 14 | #include "SkDistanceFieldGen.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 15 | |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 18 | #define GR_ATLAS_TEXTURE_WIDTH 1024 |
| 19 | #define GR_ATLAS_TEXTURE_HEIGHT 2048 |
| 20 | |
| 21 | #define GR_PLOT_WIDTH 256 |
| 22 | #define GR_PLOT_HEIGHT 256 |
| 23 | |
| 24 | #define GR_NUM_PLOTS_X (GR_ATLAS_TEXTURE_WIDTH / GR_PLOT_WIDTH) |
| 25 | #define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT) |
| 26 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 27 | #define FONT_CACHE_STATS 0 |
| 28 | #if FONT_CACHE_STATS |
| 29 | static int g_PurgeCount = 0; |
| 30 | #endif |
| 31 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
| 33 | gpu->ref(); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 34 | for (int i = 0; i < kAtlasCount; ++i) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 35 | fAtlases[i] = NULL; |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 36 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | |
| 38 | fHead = fTail = NULL; |
| 39 | } |
| 40 | |
| 41 | GrFontCache::~GrFontCache() { |
| 42 | fCache.deleteAll(); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 43 | for (int i = 0; i < kAtlasCount; ++i) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 44 | delete fAtlases[i]; |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 45 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | fGpu->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 47 | #if FONT_CACHE_STATS |
| 48 | GrPrintf("Num purges: %d\n", g_PurgeCount); |
| 49 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | } |
| 51 | |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 52 | static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 53 | static const GrPixelConfig sPixelConfigs[] = { |
| 54 | kAlpha_8_GrPixelConfig, |
| 55 | kRGB_565_GrPixelConfig, |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 56 | kSkia8888_GrPixelConfig, |
| 57 | kSkia8888_GrPixelConfig |
| 58 | }; |
| 59 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_size_mismatch); |
| 60 | |
| 61 | return sPixelConfigs[format]; |
| 62 | } |
| 63 | |
| 64 | static int mask_format_to_atlas_index(GrMaskFormat format) { |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 65 | static const int sAtlasIndices[] = { |
| 66 | GrFontCache::kA8_AtlasType, |
| 67 | GrFontCache::k565_AtlasType, |
| 68 | GrFontCache::k8888_AtlasType, |
| 69 | GrFontCache::k8888_AtlasType |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 70 | }; |
| 71 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch); |
| 72 | |
| 73 | SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount); |
| 74 | return sAtlasIndices[format]; |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 75 | } |
| 76 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, |
| 78 | const Key& key) { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 79 | GrMaskFormat format = scaler->getMaskFormat(); |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 80 | GrPixelConfig config = mask_format_to_pixel_config(format); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 81 | int atlasIndex = mask_format_to_atlas_index(format); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 82 | if (NULL == fAtlases[atlasIndex]) { |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 83 | SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, |
| 84 | GR_ATLAS_TEXTURE_HEIGHT); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 85 | fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, kNone_GrTextureFlags, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 86 | textureSize, |
| 87 | GR_NUM_PLOTS_X, |
| 88 | GR_NUM_PLOTS_Y, |
| 89 | true)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | } |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 91 | GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 92 | (this, scaler->getKey(), format, fAtlases[atlasIndex])); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | fCache.insert(key, strike); |
| 94 | |
| 95 | if (fHead) { |
| 96 | fHead->fPrev = strike; |
| 97 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 98 | SkASSERT(NULL == fTail); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | fTail = strike; |
| 100 | } |
| 101 | strike->fPrev = NULL; |
| 102 | strike->fNext = fHead; |
| 103 | fHead = strike; |
| 104 | |
| 105 | return strike; |
| 106 | } |
| 107 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | void GrFontCache::freeAll() { |
| 109 | fCache.deleteAll(); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 110 | for (int i = 0; i < kAtlasCount; ++i) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 111 | delete fAtlases[i]; |
| 112 | fAtlases[i] = NULL; |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 113 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 114 | fHead = NULL; |
| 115 | fTail = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | } |
| 117 | |
commit-bot@chromium.org | b2e9fa5 | 2013-10-27 20:50:23 +0000 | [diff] [blame] | 118 | void GrFontCache::purgeStrike(GrTextStrike* strike) { |
| 119 | const GrFontCache::Key key(strike->fFontScalerKey); |
| 120 | fCache.remove(key, strike); |
| 121 | this->detachStrikeFromList(strike); |
| 122 | delete strike; |
| 123 | } |
| 124 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 125 | bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) { |
jvanverth@google.com | bbe55fd | 2013-09-16 20:28:37 +0000 | [diff] [blame] | 126 | SkASSERT(NULL != preserveStrike); |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 127 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 128 | GrAtlas* atlas = preserveStrike->fAtlas; |
| 129 | GrPlot* plot = atlas->getUnusedPlot(); |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 130 | if (NULL == plot) { |
| 131 | return false; |
| 132 | } |
| 133 | plot->resetRects(); |
| 134 | |
| 135 | GrTextStrike* strike = fHead; |
jvanverth@google.com | bbe55fd | 2013-09-16 20:28:37 +0000 | [diff] [blame] | 136 | GrMaskFormat maskFormat = preserveStrike->fMaskFormat; |
bsalomon@google.com | 7359eae | 2011-06-21 21:18:25 +0000 | [diff] [blame] | 137 | while (strike) { |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 138 | if (maskFormat != strike->fMaskFormat) { |
| 139 | strike = strike->fNext; |
bsalomon@google.com | 7359eae | 2011-06-21 21:18:25 +0000 | [diff] [blame] | 140 | continue; |
| 141 | } |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | 7359eae | 2011-06-21 21:18:25 +0000 | [diff] [blame] | 143 | GrTextStrike* strikeToPurge = strike; |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 144 | strike = strikeToPurge->fNext; |
| 145 | strikeToPurge->removePlot(plot); |
| 146 | |
| 147 | // clear out any empty strikes (except this one) |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 148 | if (strikeToPurge != preserveStrike && strikeToPurge->fPlotUsage.isEmpty()) { |
commit-bot@chromium.org | b2e9fa5 | 2013-10-27 20:50:23 +0000 | [diff] [blame] | 149 | this->purgeStrike(strikeToPurge); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 152 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 153 | #if FONT_CACHE_STATS |
| 154 | ++g_PurgeCount; |
| 155 | #endif |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 156 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 157 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 160 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | void GrFontCache::validate() const { |
| 162 | int count = fCache.count(); |
| 163 | if (0 == count) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 164 | SkASSERT(!fHead); |
| 165 | SkASSERT(!fTail); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 166 | } else if (1 == count) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 167 | SkASSERT(fHead == fTail); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 169 | SkASSERT(fHead != fTail); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int count2 = 0; |
| 173 | const GrTextStrike* strike = fHead; |
| 174 | while (strike) { |
| 175 | count2 += 1; |
| 176 | strike = strike->fNext; |
| 177 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 178 | SkASSERT(count == count2); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | |
| 180 | count2 = 0; |
| 181 | strike = fTail; |
| 182 | while (strike) { |
| 183 | count2 += 1; |
| 184 | strike = strike->fPrev; |
| 185 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 186 | SkASSERT(count == count2); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | } |
| 188 | #endif |
| 189 | |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 190 | void GrFontCache::dump() const { |
| 191 | static int gDumpCount = 0; |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 192 | for (int i = 0; i < kAtlasCount; ++i) { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 193 | if (NULL != fAtlases[i]) { |
| 194 | GrTexture* texture = fAtlases[i]->getTexture(); |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 195 | if (NULL != texture) { |
| 196 | SkString filename; |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 197 | #ifdef SK_BUILD_FOR_ANDROID |
| 198 | filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); |
| 199 | #else |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 200 | filename.printf("fontcache_%d%d.png", gDumpCount, i); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 201 | #endif |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 202 | texture->savePixels(filename.c_str()); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | ++gDumpCount; |
| 207 | } |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 208 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | /////////////////////////////////////////////////////////////////////////////// |
| 210 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 211 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | static int gCounter; |
| 213 | #endif |
| 214 | |
| 215 | /* |
| 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(). |
| 218 | |
| 219 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 220 | atlas and a position within that texture. |
| 221 | */ |
| 222 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 223 | GrTextStrike::GrTextStrike(GrFontCache* cache, const GrFontDescKey* key, |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 224 | GrMaskFormat format, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 225 | GrAtlas* atlas) : fPool(64) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | fFontScalerKey = key; |
| 227 | fFontScalerKey->ref(); |
| 228 | |
| 229 | fFontCache = cache; // no need to ref, it won't go away before we do |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 230 | fAtlas = atlas; // no need to ref, it won't go away before we do |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 232 | fMaskFormat = format; |
| 233 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 234 | #ifdef SK_DEBUG |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 235 | // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 236 | gCounter += 1; |
| 237 | #endif |
| 238 | } |
| 239 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 240 | // this signature is needed because it's used with |
| 241 | // SkTDArray::visitAll() (see destructor) |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 242 | static void free_glyph(GrGlyph*& glyph) { glyph->free(); } |
| 243 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | GrTextStrike::~GrTextStrike() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | fFontScalerKey->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 246 | fCache.getArray().visitAll(free_glyph); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 248 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 249 | gCounter -= 1; |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 250 | // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 251 | #endif |
| 252 | } |
| 253 | |
| 254 | GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
| 255 | GrFontScaler* scaler) { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 256 | SkIRect bounds; |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 257 | if (fUseDistanceField) { |
| 258 | if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) { |
| 259 | return NULL; |
| 260 | } |
| 261 | } else { |
| 262 | if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
| 263 | return NULL; |
| 264 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | GrGlyph* glyph = fPool.alloc(); |
| 268 | glyph->init(packed, bounds); |
| 269 | fCache.insert(packed, glyph); |
| 270 | return glyph; |
| 271 | } |
| 272 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 273 | void GrTextStrike::removePlot(const GrPlot* plot) { |
| 274 | SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); |
| 275 | for (int i = 0; i < glyphArray.count(); ++i) { |
| 276 | if (plot == glyphArray[i]->fPlot) { |
| 277 | glyphArray[i]->fPlot = NULL; |
| 278 | } |
| 279 | } |
| 280 | |
robertphillips | c4f30b1 | 2014-07-13 10:09:42 -0700 | [diff] [blame^] | 281 | GrAtlas::RemovePlot(&fPlotUsage, plot); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 282 | } |
| 283 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 284 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 285 | bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 286 | #if 0 // testing hack to force us to flush our cache often |
| 287 | static int gCounter; |
| 288 | if ((++gCounter % 10) == 0) return false; |
| 289 | #endif |
| 290 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 291 | SkASSERT(glyph); |
| 292 | SkASSERT(scaler); |
| 293 | SkASSERT(fCache.contains(glyph)); |
commit-bot@chromium.org | 49e8083 | 2013-10-07 18:20:27 +0000 | [diff] [blame] | 294 | SkASSERT(NULL == glyph->fPlot); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 295 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 296 | SkAutoRef ar(scaler); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 297 | |
| 298 | int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 299 | |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 300 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
| 301 | SkAutoSMalloc<1024> storage(size); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 302 | if (fUseDistanceField) { |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 303 | if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(), |
| 304 | glyph->height(), |
| 305 | storage.get())) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 308 | } else { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 309 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
| 310 | glyph->height(), |
| 311 | glyph->width() * bytesPerPixel, |
| 312 | storage.get())) { |
| 313 | return false; |
| 314 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 315 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 316 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 317 | GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, glyph->width(), |
| 318 | glyph->height(), storage.get(), |
| 319 | &glyph->fAtlasLocation); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 320 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 321 | if (NULL == plot) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 325 | glyph->fPlot = plot; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 326 | return true; |
| 327 | } |