blob: d6ffc507e74560957e09b7b33c152223393a6c0d [file] [log] [blame]
joshualitt7c3a2f82015-03-31 13:32:05 -07001/*
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"
kkinnunencabe20c2015-06-01 01:37:26 -07009#include "GrContext.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070010#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
20static 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
bsalomond309e7a2015-04-30 14:18:54 -070031 GrTexture* texture = context->textureProvider()->refScratchTexture(
32 desc, GrTextureProvider::kApprox_ScratchTexMatch, true);
joshualitt7c3a2f82015-03-31 13:32:05 -070033 if (!texture) {
34 return NULL;
35 }
36 return SkNEW_ARGS(GrBatchAtlas, (texture, numPlotsX, numPlotsY));
37}
38
joshualitt62db8ba2015-04-09 08:22:37 -070039bool GrBatchFontCache::initAtlas(GrMaskFormat format) {
40 int index = MaskFormatToAtlasIndex(format);
41 if (!fAtlases[index]) {
bsalomon265697d2015-07-22 10:17:26 -070042 GrPixelConfig config = MaskFormatToPixelConfig(format);
joshualitt7c3a2f82015-03-31 13:32:05 -070043 if (kA8_GrMaskFormat == format) {
joshualitt62db8ba2015-04-09 08:22:37 -070044 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);
joshualitt7c3a2f82015-03-31 13:32:05 -070049 } else {
joshualitt62db8ba2015-04-09 08:22:37 -070050 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);
joshualitt7c3a2f82015-03-31 13:32:05 -070055 }
56
joshualitt62db8ba2015-04-09 08:22:37 -070057 // Atlas creation can fail
58 if (fAtlases[index]) {
59 fAtlases[index]->registerEvictionCallback(&GrBatchFontCache::HandleEviction,
60 (void*)this);
61 } else {
62 return false;
joshualitt7c3a2f82015-03-31 13:32:05 -070063 }
64 }
joshualitt62db8ba2015-04-09 08:22:37 -070065 return true;
66}
67
68GrBatchFontCache::GrBatchFontCache(GrContext* context)
69 : fContext(context)
70 , fPreserveStrike(NULL) {
71 for (int i = 0; i < kMaskFormatCount; ++i) {
72 fAtlases[i] = NULL;
73 }
joshualitt7c3a2f82015-03-31 13:32:05 -070074}
75
76GrBatchFontCache::~GrBatchFontCache() {
77 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
78 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070079 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070080 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070081 ++iter;
82 }
83 for (int i = 0; i < kMaskFormatCount; ++i) {
84 SkDELETE(fAtlases[i]);
85 }
86}
87
joshualitt7c3a2f82015-03-31 13:32:05 -070088void GrBatchFontCache::freeAll() {
89 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
90 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070091 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070092 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070093 ++iter;
94 }
95 fCache.rewind();
96 for (int i = 0; i < kMaskFormatCount; ++i) {
97 SkDELETE(fAtlases[i]);
98 fAtlases[i] = NULL;
99 }
100}
101
joshualitt7c3a2f82015-03-31 13:32:05 -0700102void 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));
joshualittae32c102015-04-21 09:37:57 -0700114 strike->fIsAbandoned = true;
115 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700116 }
117 }
118}
119
120void 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
149GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key)
150 : fFontScalerKey(SkRef(key))
151 , fPool(9/*start allocations at 512 bytes*/)
joshualittae32c102015-04-21 09:37:57 -0700152 , fAtlasedGlyphs(0)
153 , fIsAbandoned(false) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700154
155 fBatchFontCache = cache; // no need to ref, it won't go away before we do
156}
157
158GrBatchTextStrike::~GrBatchTextStrike() {
159 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
160 while (!iter.done()) {
161 (*iter).free();
162 ++iter;
163 }
164}
165
joshualitt6c2c2b02015-07-24 10:37:00 -0700166GrGlyph* GrBatchTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
joshualitt7c3a2f82015-03-31 13:32:05 -0700167 GrFontScaler* scaler) {
168 SkIRect bounds;
169 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700170 if (!scaler->getPackedGlyphDFBounds(skGlyph, &bounds)) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700171 return NULL;
172 }
173 } else {
joshualitt6c2c2b02015-07-24 10:37:00 -0700174 if (!scaler->getPackedGlyphBounds(skGlyph, &bounds)) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700175 return NULL;
176 }
177 }
joshualitt6c2c2b02015-07-24 10:37:00 -0700178 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(skGlyph);
179
joshualitt7c3a2f82015-03-31 13:32:05 -0700180 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
186void 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
joshualitt7c3a2f82015-03-31 13:32:05 -0700198bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph,
joshualitt4f19ca32015-07-30 07:59:20 -0700199 GrFontScaler* scaler, const SkGlyph& skGlyph,
200 GrMaskFormat expectedMaskFormat) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700201 SkASSERT(glyph);
202 SkASSERT(scaler);
203 SkASSERT(fCache.find(glyph->fPackedID));
joshualitt7c3a2f82015-03-31 13:32:05 -0700204
205 SkAutoUnref ar(SkSafeRef(scaler));
206
joshualitt4f19ca32015-07-30 07:59:20 -0700207 int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -0700208
209 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700210 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700211
212 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700213 if (!scaler->getPackedGlyphDFImage(skGlyph, glyph->width(), glyph->height(),
joshualitt7c3a2f82015-03-31 13:32:05 -0700214 storage.get())) {
215 return false;
216 }
217 } else {
joshualitt6c2c2b02015-07-24 10:37:00 -0700218 if (!scaler->getPackedGlyphImage(skGlyph, glyph->width(), glyph->height(),
joshualitt4f19ca32015-07-30 07:59:20 -0700219 glyph->width() * bytesPerPixel, expectedMaskFormat,
220 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700221 return false;
222 }
223 }
224
joshualitt4f19ca32015-07-30 07:59:20 -0700225 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, expectedMaskFormat,
joshualitt7c3a2f82015-03-31 13:32:05 -0700226 glyph->width(), glyph->height(),
227 storage.get(), &glyph->fAtlasLocation);
228 if (success) {
229 fAtlasedGlyphs++;
230 }
231 return success;
232}