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