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]) { |
bsalomon | 265697d | 2015-07-22 10:17:26 -0700 | [diff] [blame] | 42 | GrPixelConfig config = MaskFormatToPixelConfig(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 | void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) { |
| 103 | GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr); |
| 104 | |
| 105 | SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCache); |
| 106 | for (; !iter.done(); ++iter) { |
| 107 | GrBatchTextStrike* strike = &*iter; |
| 108 | strike->removeID(id); |
| 109 | |
| 110 | // clear out any empty strikes. We will preserve the strike whose call to addToAtlas |
| 111 | // triggered the eviction |
| 112 | if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) { |
| 113 | fontCache->fCache.remove(*(strike->fFontScalerKey)); |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 114 | strike->fIsAbandoned = true; |
| 115 | strike->unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void GrBatchFontCache::dump() const { |
| 121 | static int gDumpCount = 0; |
| 122 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 123 | if (fAtlases[i]) { |
| 124 | GrTexture* texture = fAtlases[i]->getTexture(); |
| 125 | if (texture) { |
| 126 | SkString filename; |
| 127 | #ifdef SK_BUILD_FOR_ANDROID |
| 128 | filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i); |
| 129 | #else |
| 130 | filename.printf("fontcache_%d%d.png", gDumpCount, i); |
| 131 | #endif |
| 132 | texture->surfacePriv().savePixels(filename.c_str()); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | ++gDumpCount; |
| 137 | } |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | /* |
| 142 | The text strike is specific to a given font/style/matrix setup, which is |
| 143 | represented by the GrHostFontScaler object we are given in getGlyph(). |
| 144 | |
| 145 | We map a 32bit glyphID to a GrGlyph record, which in turn points to a |
| 146 | atlas and a position within that texture. |
| 147 | */ |
| 148 | |
| 149 | GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key) |
| 150 | : fFontScalerKey(SkRef(key)) |
| 151 | , fPool(9/*start allocations at 512 bytes*/) |
joshualitt | ae32c10 | 2015-04-21 09:37:57 -0700 | [diff] [blame] | 152 | , fAtlasedGlyphs(0) |
| 153 | , fIsAbandoned(false) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 154 | |
| 155 | fBatchFontCache = cache; // no need to ref, it won't go away before we do |
| 156 | } |
| 157 | |
| 158 | GrBatchTextStrike::~GrBatchTextStrike() { |
| 159 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 160 | while (!iter.done()) { |
| 161 | (*iter).free(); |
| 162 | ++iter; |
| 163 | } |
| 164 | } |
| 165 | |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 166 | GrGlyph* GrBatchTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed, |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 167 | GrFontScaler* scaler) { |
| 168 | SkIRect bounds; |
| 169 | if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 170 | if (!scaler->getPackedGlyphDFBounds(skGlyph, &bounds)) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 171 | return NULL; |
| 172 | } |
| 173 | } else { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 174 | if (!scaler->getPackedGlyphBounds(skGlyph, &bounds)) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
| 177 | } |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 178 | GrMaskFormat format = scaler->getPackedGlyphMaskFormat(skGlyph); |
| 179 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 180 | GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW); |
| 181 | glyph->init(packed, bounds, format); |
| 182 | fCache.add(glyph); |
| 183 | return glyph; |
| 184 | } |
| 185 | |
| 186 | void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) { |
| 187 | SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache); |
| 188 | while (!iter.done()) { |
| 189 | if (id == (*iter).fID) { |
| 190 | (*iter).fID = GrBatchAtlas::kInvalidAtlasID; |
| 191 | fAtlasedGlyphs--; |
| 192 | SkASSERT(fAtlasedGlyphs >= 0); |
| 193 | } |
| 194 | ++iter; |
| 195 | } |
| 196 | } |
| 197 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 198 | bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph, |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 199 | GrFontScaler* scaler, const SkGlyph& skGlyph, |
| 200 | GrMaskFormat expectedMaskFormat) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 201 | SkASSERT(glyph); |
| 202 | SkASSERT(scaler); |
| 203 | SkASSERT(fCache.find(glyph->fPackedID)); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 204 | |
| 205 | SkAutoUnref ar(SkSafeRef(scaler)); |
| 206 | |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 207 | int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 208 | |
| 209 | size_t size = glyph->fBounds.area() * bytesPerPixel; |
joshualitt | 29f8679 | 2015-05-29 08:06:48 -0700 | [diff] [blame] | 210 | SkAutoSMalloc<1024> storage(size); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 211 | |
| 212 | if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 213 | if (!scaler->getPackedGlyphDFImage(skGlyph, glyph->width(), glyph->height(), |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 214 | storage.get())) { |
| 215 | return false; |
| 216 | } |
| 217 | } else { |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 218 | if (!scaler->getPackedGlyphImage(skGlyph, glyph->width(), glyph->height(), |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 219 | glyph->width() * bytesPerPixel, expectedMaskFormat, |
| 220 | storage.get())) { |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | |
joshualitt | 4f19ca3 | 2015-07-30 07:59:20 -0700 | [diff] [blame] | 225 | bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, expectedMaskFormat, |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 226 | glyph->width(), glyph->height(), |
| 227 | storage.get(), &glyph->fAtlasLocation); |
| 228 | if (success) { |
| 229 | fAtlasedGlyphs++; |
| 230 | } |
| 231 | return success; |
| 232 | } |