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