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" |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 15 | #include "SkDistanceFieldGen.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 16 | |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 19 | #define GR_ATLAS_TEXTURE_WIDTH 1024 |
| 20 | #define GR_ATLAS_TEXTURE_HEIGHT 2048 |
| 21 | |
| 22 | #define GR_PLOT_WIDTH 256 |
| 23 | #define GR_PLOT_HEIGHT 256 |
| 24 | |
| 25 | #define GR_NUM_PLOTS_X (GR_ATLAS_TEXTURE_WIDTH / GR_PLOT_WIDTH) |
| 26 | #define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT) |
| 27 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 28 | #define FONT_CACHE_STATS 0 |
| 29 | #if FONT_CACHE_STATS |
| 30 | static int g_PurgeCount = 0; |
| 31 | #endif |
| 32 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) { |
| 34 | gpu->ref(); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 35 | for (int i = 0; i < kAtlasCount; ++i) { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 36 | fAtlasMgr[i] = NULL; |
| 37 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | |
| 39 | fHead = fTail = NULL; |
| 40 | } |
| 41 | |
| 42 | GrFontCache::~GrFontCache() { |
| 43 | fCache.deleteAll(); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 44 | for (int i = 0; i < kAtlasCount; ++i) { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 45 | delete fAtlasMgr[i]; |
| 46 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | fGpu->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 48 | #if FONT_CACHE_STATS |
| 49 | GrPrintf("Num purges: %d\n", g_PurgeCount); |
| 50 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | } |
| 52 | |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 53 | static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) { |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 54 | static const GrPixelConfig sPixelConfigs[] = { |
| 55 | kAlpha_8_GrPixelConfig, |
| 56 | kRGB_565_GrPixelConfig, |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 57 | kSkia8888_GrPixelConfig, |
| 58 | kSkia8888_GrPixelConfig |
| 59 | }; |
| 60 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_size_mismatch); |
| 61 | |
| 62 | return sPixelConfigs[format]; |
| 63 | } |
| 64 | |
| 65 | static int mask_format_to_atlas_index(GrMaskFormat format) { |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 66 | static const int sAtlasIndices[] = { |
| 67 | GrFontCache::kA8_AtlasType, |
| 68 | GrFontCache::k565_AtlasType, |
| 69 | GrFontCache::k8888_AtlasType, |
| 70 | GrFontCache::k8888_AtlasType |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 71 | }; |
| 72 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch); |
| 73 | |
| 74 | SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount); |
| 75 | return sAtlasIndices[format]; |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 76 | } |
| 77 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 78 | GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler, |
| 79 | const Key& key) { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 80 | GrMaskFormat format = scaler->getMaskFormat(); |
commit-bot@chromium.org | 9529441 | 2013-09-26 15:28:40 +0000 | [diff] [blame] | 81 | GrPixelConfig config = mask_format_to_pixel_config(format); |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 82 | int atlasIndex = mask_format_to_atlas_index(format); |
| 83 | if (NULL == fAtlasMgr[atlasIndex]) { |
commit-bot@chromium.org | 53e1e4d | 2014-04-01 16:25:11 +0000 | [diff] [blame] | 84 | SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH, |
| 85 | GR_ATLAS_TEXTURE_HEIGHT); |
| 86 | fAtlasMgr[atlasIndex] = SkNEW_ARGS(GrAtlasMgr, (fGpu, config, |
| 87 | textureSize, |
| 88 | GR_NUM_PLOTS_X, |
| 89 | GR_NUM_PLOTS_Y)); |
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, |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 92 | (this, scaler->getKey(), format, fAtlasMgr[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) { |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 111 | delete fAtlasMgr[i]; |
| 112 | fAtlasMgr[i] = NULL; |
| 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 | |
| 128 | GrAtlasMgr* atlasMgr = preserveStrike->fAtlasMgr; |
| 129 | GrPlot* plot = atlasMgr->getUnusedPlot(); |
| 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) |
| 148 | if (strikeToPurge != preserveStrike && strikeToPurge->fAtlas.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 | #ifdef SK_DEVELOPER |
| 191 | void GrFontCache::dump() const { |
| 192 | static int gDumpCount = 0; |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 193 | for (int i = 0; i < kAtlasCount; ++i) { |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 194 | if (NULL != fAtlasMgr[i]) { |
| 195 | GrTexture* texture = fAtlasMgr[i]->getTexture(); |
| 196 | if (NULL != texture) { |
| 197 | SkString filename; |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 198 | #ifdef SK_BUILD_FOR_ANDROID |
| 199 | filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); |
| 200 | #else |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 201 | filename.printf("fontcache_%d%d.png", gDumpCount, i); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 202 | #endif |
commit-bot@chromium.org | 03e3e89 | 2013-10-02 18:19:17 +0000 | [diff] [blame] | 203 | texture->savePixels(filename.c_str()); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | ++gDumpCount; |
| 208 | } |
| 209 | #endif |
| 210 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | /////////////////////////////////////////////////////////////////////////////// |
| 212 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 213 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | static int gCounter; |
| 215 | #endif |
| 216 | |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 217 | // this acts as the max magnitude for the distance field, |
| 218 | // as well as the pad we need around the glyph |
| 219 | #define DISTANCE_FIELD_RANGE 4 |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 220 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | /* |
| 222 | The text strike is specific to a given font/style/matrix setup, which is |
| 223 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 224 | |
| 225 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 226 | atlas and a position within that texture. |
| 227 | */ |
| 228 | |
| 229 | GrTextStrike::GrTextStrike(GrFontCache* cache, const GrKey* key, |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 230 | GrMaskFormat format, |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 231 | GrAtlasMgr* atlasMgr) : fPool(64) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | fFontScalerKey = key; |
| 233 | fFontScalerKey->ref(); |
| 234 | |
| 235 | fFontCache = cache; // no need to ref, it won't go away before we do |
| 236 | fAtlasMgr = atlasMgr; // 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] | 237 | |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 238 | fMaskFormat = format; |
| 239 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 240 | #ifdef SK_DEBUG |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 241 | // GrPrintf(" GrTextStrike %p %d\n", this, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | gCounter += 1; |
| 243 | #endif |
| 244 | } |
| 245 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 246 | // this signature is needed because it's used with |
| 247 | // SkTDArray::visitAll() (see destructor) |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 248 | static void free_glyph(GrGlyph*& glyph) { glyph->free(); } |
| 249 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | GrTextStrike::~GrTextStrike() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 251 | fFontScalerKey->unref(); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 252 | fCache.getArray().visitAll(free_glyph); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 253 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 254 | #ifdef SK_DEBUG |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | gCounter -= 1; |
reed@google.com | 3ef80cf | 2011-07-05 19:09:47 +0000 | [diff] [blame] | 256 | // GrPrintf("~GrTextStrike %p %d\n", this, gCounter); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | #endif |
| 258 | } |
| 259 | |
| 260 | GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed, |
| 261 | GrFontScaler* scaler) { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 262 | SkIRect bounds; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 263 | if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | GrGlyph* glyph = fPool.alloc(); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 268 | // expand bounds to hold full distance field data |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 269 | // + room for bilerp |
| 270 | int pad = DISTANCE_FIELD_RANGE+1; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 271 | if (fUseDistanceField) { |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 272 | bounds.fLeft -= pad; |
| 273 | bounds.fRight += pad; |
| 274 | bounds.fTop -= pad; |
| 275 | bounds.fBottom += pad; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 276 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 277 | glyph->init(packed, bounds); |
| 278 | fCache.insert(packed, glyph); |
| 279 | return glyph; |
| 280 | } |
| 281 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 282 | void GrTextStrike::removePlot(const GrPlot* plot) { |
| 283 | SkTDArray<GrGlyph*>& glyphArray = fCache.getArray(); |
| 284 | for (int i = 0; i < glyphArray.count(); ++i) { |
| 285 | if (plot == glyphArray[i]->fPlot) { |
| 286 | glyphArray[i]->fPlot = NULL; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | fAtlasMgr->removePlot(&fAtlas, plot); |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 291 | } |
| 292 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 293 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 294 | bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) { |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 295 | #if 0 // testing hack to force us to flush our cache often |
| 296 | static int gCounter; |
| 297 | if ((++gCounter % 10) == 0) return false; |
| 298 | #endif |
| 299 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 300 | SkASSERT(glyph); |
| 301 | SkASSERT(scaler); |
| 302 | SkASSERT(fCache.contains(glyph)); |
commit-bot@chromium.org | 49e8083 | 2013-10-07 18:20:27 +0000 | [diff] [blame] | 303 | SkASSERT(NULL == glyph->fPlot); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 304 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 305 | SkAutoRef ar(scaler); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 306 | |
| 307 | int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 309 | GrPlot* plot; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 310 | if (fUseDistanceField) { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 311 | // we've already expanded the glyph dimensions to match the final size |
| 312 | // but must shrink back down to get the packed glyph data |
| 313 | int dfWidth = glyph->width(); |
| 314 | int dfHeight = glyph->height(); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 315 | int pad = DISTANCE_FIELD_RANGE+1; |
| 316 | int width = dfWidth - 2*pad; |
| 317 | int height = dfHeight - 2*pad; |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 318 | int stride = width*bytesPerPixel; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 319 | |
| 320 | size_t size = width * height * bytesPerPixel; |
| 321 | SkAutoSMalloc<1024> storage(size); |
| 322 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, width, height, stride, storage.get())) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | // alloc storage for distance field glyph |
| 327 | size_t dfSize = dfWidth * dfHeight * bytesPerPixel; |
| 328 | SkAutoSMalloc<1024> dfStorage(dfSize); |
skia.committer@gmail.com | 0b70816 | 2014-03-12 03:02:18 +0000 | [diff] [blame] | 329 | |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 330 | if (1 == bytesPerPixel) { |
| 331 | (void) SkGenerateDistanceFieldFromImage((unsigned char*)dfStorage.get(), |
| 332 | (unsigned char*)storage.get(), |
| 333 | width, height, DISTANCE_FIELD_RANGE); |
| 334 | } else { |
commit-bot@chromium.org | eefd8a0 | 2014-04-08 20:14:32 +0000 | [diff] [blame] | 335 | // distance fields should only be used to represent alpha masks |
| 336 | SkASSERT(false); |
| 337 | return false; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 338 | } |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 339 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 340 | // copy to atlas |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 341 | plot = fAtlasMgr->addToAtlas(&fAtlas, dfWidth, dfHeight, dfStorage.get(), |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 342 | &glyph->fAtlasLocation); |
| 343 | |
| 344 | } else { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 345 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
| 346 | SkAutoSMalloc<1024> storage(size); |
| 347 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
| 348 | glyph->height(), |
| 349 | glyph->width() * bytesPerPixel, |
| 350 | storage.get())) { |
| 351 | return false; |
| 352 | } |
| 353 | |
skia.committer@gmail.com | 11a253b | 2013-11-12 07:02:05 +0000 | [diff] [blame] | 354 | plot = fAtlasMgr->addToAtlas(&fAtlas, glyph->width(), |
| 355 | glyph->height(), storage.get(), |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 356 | &glyph->fAtlasLocation); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 357 | } |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 358 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 359 | if (NULL == plot) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 363 | glyph->fPlot = plot; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 364 | return true; |
| 365 | } |