joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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. |
| 6 | */ |
| 7 | |
| 8 | #include "GrBatchFontCache.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 9 | #include "GrContext.h" |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 10 | #include "GrFontAtlasSizes.h" |
| 11 | #include "GrGpu.h" |
| 12 | #include "GrRectanizer.h" |
| 13 | #include "GrSurfacePriv.h" |
| 14 | #include "SkString.h" |
| 15 | |
| 16 | #include "SkDistanceFieldGen.h" |
| 17 | |
| 18 | /////////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | static GrBatchAtlas* make_atlas(GrContext* context, GrPixelConfig config, |
| 21 | int textureWidth, int textureHeight, |
| 22 | int numPlotsX, int numPlotsY) { |
| 23 | GrSurfaceDesc desc; |
| 24 | desc.fFlags = kNone_GrSurfaceFlags; |
| 25 | desc.fWidth = textureWidth; |
| 26 | desc.fHeight = textureHeight; |
| 27 | desc.fConfig = config; |
| 28 | |
| 29 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 30 | // guarantee we do not recieve a texture with pending IO |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 31 | GrTexture* texture = context->textureProvider()->refScratchTexture( |
| 32 | desc, GrTextureProvider::kApprox_ScratchTexMatch, true); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 33 | if (!texture) { |
| 34 | return NULL; |
| 35 | } |
| 36 | return SkNEW_ARGS(GrBatchAtlas, (texture, numPlotsX, numPlotsY)); |
| 37 | } |
| 38 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 39 | bool GrBatchFontCache::initAtlas(GrMaskFormat format) { |
| 40 | int index = MaskFormatToAtlasIndex(format); |
| 41 | if (!fAtlases[index]) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 42 | GrPixelConfig config = this->getPixelConfig(format); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 43 | if (kA8_GrMaskFormat == format) { |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 44 | fAtlases[index] = make_atlas(fContext, config, |
| 45 | GR_FONT_ATLAS_A8_TEXTURE_WIDTH, |
| 46 | GR_FONT_ATLAS_TEXTURE_HEIGHT, |
| 47 | GR_FONT_ATLAS_A8_NUM_PLOTS_X, |
| 48 | GR_FONT_ATLAS_NUM_PLOTS_Y); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 49 | } else { |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 50 | fAtlases[index] = make_atlas(fContext, config, |
| 51 | GR_FONT_ATLAS_TEXTURE_WIDTH, |
| 52 | GR_FONT_ATLAS_TEXTURE_HEIGHT, |
| 53 | GR_FONT_ATLAS_NUM_PLOTS_X, |
| 54 | GR_FONT_ATLAS_NUM_PLOTS_Y); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 55 | } |
| 56 | |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 57 | // Atlas creation can fail |
| 58 | if (fAtlases[index]) { |
| 59 | fAtlases[index]->registerEvictionCallback(&GrBatchFontCache::HandleEviction, |
| 60 | (void*)this); |
| 61 | } else { |
| 62 | return false; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
joshualitt | 62db8ba | 2015-04-09 08:22:37 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | GrBatchFontCache::GrBatchFontCache(GrContext* context) |
| 69 | : fContext(context) |
| 70 | , fPreserveStrike(NULL) { |
| 71 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 72 | fAtlases[i] = NULL; |
| 73 | } |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | GrBatchFontCache::~GrBatchFontCache() { |
| 77 | SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache); |
| 78 | while (!iter.done()) { |
joshualitt | a5f1d5a | 2015-05-22 13:09:57 -0700 | [diff] [blame] | 79 | (*iter).fIsAbandoned = true; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 80 | (*iter).unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 81 | ++iter; |
| 82 | } |
| 83 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 84 | SkDELETE(fAtlases[i]); |
| 85 | } |
| 86 | } |
| 87 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 88 | void GrBatchFontCache::freeAll() { |
| 89 | SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache); |
| 90 | while (!iter.done()) { |
joshualitt | a5f1d5a | 2015-05-22 13:09:57 -0700 | [diff] [blame] | 91 | (*iter).fIsAbandoned = true; |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 92 | (*iter).unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 93 | ++iter; |
| 94 | } |
| 95 | fCache.rewind(); |
| 96 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 97 | SkDELETE(fAtlases[i]); |
| 98 | fAtlases[i] = NULL; |
| 99 | } |
| 100 | } |
| 101 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 102 | GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const { |
| 103 | static const GrPixelConfig kPixelConfigs[] = { |
| 104 | kAlpha_8_GrPixelConfig, |
| 105 | kRGB_565_GrPixelConfig, |
| 106 | kSkia8888_GrPixelConfig |
| 107 | }; |
| 108 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_size_mismatch); |
| 109 | |
| 110 | return kPixelConfigs[format]; |
| 111 | } |
| 112 | |
| 113 | void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) { |
| 114 | GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr); |
| 115 | |
| 116 | SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCache); |
| 117 | for (; !iter.done(); ++iter) { |
| 118 | GrBatchTextStrike* strike = &*iter; |
| 119 | strike->removeID(id); |
| 120 | |
| 121 | // clear out any empty strikes. We will preserve the strike whose call to addToAtlas |
| 122 | // triggered the eviction |
| 123 | if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) { |
| 124 | fontCache->fCache.remove(*(strike->fFontScalerKey)); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 125 | strike->fIsAbandoned = true; |
| 126 | strike->unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void GrBatchFontCache::dump() const { |
| 132 | static int gDumpCount = 0; |
| 133 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 134 | if (fAtlases[i]) { |
| 135 | GrTexture* texture = fAtlases[i]->getTexture(); |
| 136 | if (texture) { |
| 137 | SkString filename; |
| 138 | #ifdef SK_BUILD_FOR_ANDROID |
| 139 | filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); |
| 140 | #else |
| 141 | filename.printf("fontcache_%d%d.png", gDumpCount, i); |
| 142 | #endif |
| 143 | texture->surfacePriv().savePixels(filename.c_str()); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | ++gDumpCount; |
| 148 | } |
| 149 | |
| 150 | /////////////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | /* |
| 153 | The text strike is specific to a given font/style/matrix setup, which is |
| 154 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 155 | |
| 156 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 157 | atlas and a position within that texture. |
| 158 | */ |
| 159 | |
| 160 | GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key) |
| 161 | : fFontScalerKey(SkRef(key)) |
| 162 | , fPool(9/*start allocations at 512 bytes*/) |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 163 | , fAtlasedGlyphs(0) |
| 164 | , fIsAbandoned(false) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 165 | |
| 166 | fBatchFontCache = cache; // no need to ref, it won't go away before we do |
| 167 | } |
| 168 | |
| 169 | GrBatchTextStrike::~GrBatchTextStrike() { |
| 170 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 171 | while (!iter.done()) { |
| 172 | (*iter).free(); |
| 173 | ++iter; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed, |
| 178 | GrFontScaler* scaler) { |
| 179 | SkIRect bounds; |
| 180 | if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) { |
| 181 | if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) { |
| 182 | return NULL; |
| 183 | } |
| 184 | } else { |
| 185 | if (!scaler->getPackedGlyphBounds(packed, &bounds)) { |
| 186 | return NULL; |
| 187 | } |
| 188 | } |
| 189 | GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed); |
| 190 | |
| 191 | GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW); |
| 192 | glyph->init(packed, bounds, format); |
| 193 | fCache.add(glyph); |
| 194 | return glyph; |
| 195 | } |
| 196 | |
| 197 | void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) { |
| 198 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 199 | while (!iter.done()) { |
| 200 | if (id == (*iter).fID) { |
| 201 | (*iter).fID = GrBatchAtlas::kInvalidAtlasID; |
| 202 | fAtlasedGlyphs--; |
| 203 | SkASSERT(fAtlasedGlyphs >= 0); |
| 204 | } |
| 205 | ++iter; |
| 206 | } |
| 207 | } |
| 208 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 209 | bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph, |
| 210 | GrFontScaler* scaler) { |
| 211 | SkASSERT(glyph); |
| 212 | SkASSERT(scaler); |
| 213 | SkASSERT(fCache.find(glyph->fPackedID)); |
| 214 | SkASSERT(NULL == glyph->fPlot); |
| 215 | |
| 216 | SkAutoUnref ar(SkSafeRef(scaler)); |
| 217 | |
| 218 | int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat); |
| 219 | |
| 220 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 221 | SkAutoSMalloc<1024> storage(size); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 222 | |
| 223 | if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) { |
| 224 | if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(), |
| 225 | glyph->height(), |
| 226 | storage.get())) { |
| 227 | return false; |
| 228 | } |
| 229 | } else { |
| 230 | if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(), |
| 231 | glyph->height(), |
| 232 | glyph->width() * bytesPerPixel, |
| 233 | storage.get())) { |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, glyph->fMaskFormat, |
| 239 | glyph->width(), glyph->height(), |
| 240 | storage.get(), &glyph->fAtlasLocation); |
| 241 | if (success) { |
| 242 | fAtlasedGlyphs++; |
| 243 | } |
| 244 | return success; |
| 245 | } |