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