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