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