blob: 653649ac7832b8c10cdd77fc961be214571749fc [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"
9#include "GrFontAtlasSizes.h"
10#include "GrGpu.h"
11#include "GrRectanizer.h"
12#include "GrSurfacePriv.h"
13#include "SkString.h"
14
15#include "SkDistanceFieldGen.h"
16
17///////////////////////////////////////////////////////////////////////////////
18
19static GrBatchAtlas* make_atlas(GrContext* context, GrPixelConfig config,
20 int textureWidth, int textureHeight,
21 int numPlotsX, int numPlotsY) {
22 GrSurfaceDesc desc;
23 desc.fFlags = kNone_GrSurfaceFlags;
24 desc.fWidth = textureWidth;
25 desc.fHeight = textureHeight;
26 desc.fConfig = config;
27
28 // We don't want to flush the context so we claim we're in the middle of flushing so as to
29 // guarantee we do not recieve a texture with pending IO
30 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch, true);
31 if (!texture) {
32 return NULL;
33 }
34 return SkNEW_ARGS(GrBatchAtlas, (texture, numPlotsX, numPlotsY));
35}
36
joshualitt62db8ba2015-04-09 08:22:37 -070037bool GrBatchFontCache::initAtlas(GrMaskFormat format) {
38 int index = MaskFormatToAtlasIndex(format);
39 if (!fAtlases[index]) {
joshualitt7c3a2f82015-03-31 13:32:05 -070040 GrPixelConfig config = this->getPixelConfig(format);
joshualitt7c3a2f82015-03-31 13:32:05 -070041 if (kA8_GrMaskFormat == format) {
joshualitt62db8ba2015-04-09 08:22:37 -070042 fAtlases[index] = make_atlas(fContext, config,
43 GR_FONT_ATLAS_A8_TEXTURE_WIDTH,
44 GR_FONT_ATLAS_TEXTURE_HEIGHT,
45 GR_FONT_ATLAS_A8_NUM_PLOTS_X,
46 GR_FONT_ATLAS_NUM_PLOTS_Y);
joshualitt7c3a2f82015-03-31 13:32:05 -070047 } else {
joshualitt62db8ba2015-04-09 08:22:37 -070048 fAtlases[index] = make_atlas(fContext, config,
49 GR_FONT_ATLAS_TEXTURE_WIDTH,
50 GR_FONT_ATLAS_TEXTURE_HEIGHT,
51 GR_FONT_ATLAS_NUM_PLOTS_X,
52 GR_FONT_ATLAS_NUM_PLOTS_Y);
joshualitt7c3a2f82015-03-31 13:32:05 -070053 }
54
joshualitt62db8ba2015-04-09 08:22:37 -070055 // Atlas creation can fail
56 if (fAtlases[index]) {
57 fAtlases[index]->registerEvictionCallback(&GrBatchFontCache::HandleEviction,
58 (void*)this);
59 } else {
60 return false;
joshualitt7c3a2f82015-03-31 13:32:05 -070061 }
62 }
joshualitt62db8ba2015-04-09 08:22:37 -070063 return true;
64}
65
66GrBatchFontCache::GrBatchFontCache(GrContext* context)
67 : fContext(context)
68 , fPreserveStrike(NULL) {
69 for (int i = 0; i < kMaskFormatCount; ++i) {
70 fAtlases[i] = NULL;
71 }
joshualitt7c3a2f82015-03-31 13:32:05 -070072}
73
74GrBatchFontCache::~GrBatchFontCache() {
75 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
76 while (!iter.done()) {
77 SkDELETE(&(*iter));
78 ++iter;
79 }
80 for (int i = 0; i < kMaskFormatCount; ++i) {
81 SkDELETE(fAtlases[i]);
82 }
83}
84
joshualitt7c3a2f82015-03-31 13:32:05 -070085void GrBatchFontCache::freeAll() {
86 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
87 while (!iter.done()) {
88 SkDELETE(&(*iter));
89 ++iter;
90 }
91 fCache.rewind();
92 for (int i = 0; i < kMaskFormatCount; ++i) {
93 SkDELETE(fAtlases[i]);
94 fAtlases[i] = NULL;
95 }
96}
97
joshualitt7c3a2f82015-03-31 13:32:05 -070098GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const {
99 static const GrPixelConfig kPixelConfigs[] = {
100 kAlpha_8_GrPixelConfig,
101 kRGB_565_GrPixelConfig,
102 kSkia8888_GrPixelConfig
103 };
104 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_size_mismatch);
105
106 return kPixelConfigs[format];
107}
108
109void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
110 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
111
112 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCache);
113 for (; !iter.done(); ++iter) {
114 GrBatchTextStrike* strike = &*iter;
115 strike->removeID(id);
116
117 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
118 // triggered the eviction
119 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
120 fontCache->fCache.remove(*(strike->fFontScalerKey));
121 SkDELETE(strike);
122 }
123 }
124}
125
126void GrBatchFontCache::dump() const {
127 static int gDumpCount = 0;
128 for (int i = 0; i < kMaskFormatCount; ++i) {
129 if (fAtlases[i]) {
130 GrTexture* texture = fAtlases[i]->getTexture();
131 if (texture) {
132 SkString filename;
133#ifdef SK_BUILD_FOR_ANDROID
134 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
135#else
136 filename.printf("fontcache_%d%d.png", gDumpCount, i);
137#endif
138 texture->surfacePriv().savePixels(filename.c_str());
139 }
140 }
141 }
142 ++gDumpCount;
143}
144
145///////////////////////////////////////////////////////////////////////////////
146
147/*
148 The text strike is specific to a given font/style/matrix setup, which is
149 represented by the GrHostFontScaler object we are given in getGlyph().
150
151 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
152 atlas and a position within that texture.
153 */
154
155GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key)
156 : fFontScalerKey(SkRef(key))
157 , fPool(9/*start allocations at 512 bytes*/)
158 , fAtlasedGlyphs(0) {
159
160 fBatchFontCache = cache; // no need to ref, it won't go away before we do
161}
162
163GrBatchTextStrike::~GrBatchTextStrike() {
164 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
165 while (!iter.done()) {
166 (*iter).free();
167 ++iter;
168 }
169}
170
171GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed,
172 GrFontScaler* scaler) {
173 SkIRect bounds;
174 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
175 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
176 return NULL;
177 }
178 } else {
179 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
180 return NULL;
181 }
182 }
183 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
184
185 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
186 glyph->init(packed, bounds, format);
187 fCache.add(glyph);
188 return glyph;
189}
190
191void 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
203bool GrBatchTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
204 int width = glyph->fBounds.width();
205 int height = glyph->fBounds.height();
206 bool useDistanceField =
207 (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID));
208 int pad = useDistanceField ? 2 * SK_DistanceFieldPad : 0;
209 int plotWidth = (kA8_GrMaskFormat == glyph->fMaskFormat) ? GR_FONT_ATLAS_A8_PLOT_WIDTH
210 : GR_FONT_ATLAS_PLOT_WIDTH;
211 if (width + pad > plotWidth) {
212 return true;
213 }
214 if (height + pad > GR_FONT_ATLAS_PLOT_HEIGHT) {
215 return true;
216 }
217
218 return false;
219}
220
221bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph,
222 GrFontScaler* scaler) {
223 SkASSERT(glyph);
224 SkASSERT(scaler);
225 SkASSERT(fCache.find(glyph->fPackedID));
226 SkASSERT(NULL == glyph->fPlot);
227
228 SkAutoUnref ar(SkSafeRef(scaler));
229
230 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
231
232 size_t size = glyph->fBounds.area() * bytesPerPixel;
233 GrAutoMalloc<1024> storage(size);
234
235 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
236 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
237 glyph->height(),
238 storage.get())) {
239 return false;
240 }
241 } else {
242 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
243 glyph->height(),
244 glyph->width() * bytesPerPixel,
245 storage.get())) {
246 return false;
247 }
248 }
249
250 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, glyph->fMaskFormat,
251 glyph->width(), glyph->height(),
252 storage.get(), &glyph->fAtlasLocation);
253 if (success) {
254 fAtlasedGlyphs++;
255 }
256 return success;
257}