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