blob: c429d539b83abebcbbae2589aeba63a9ca86ce3f [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
37int GrBatchFontCache::MaskFormatToAtlasIndex(GrMaskFormat format) {
38 static const int sAtlasIndices[] = {
39 kA8_GrMaskFormat,
40 kA565_GrMaskFormat,
41 kARGB_GrMaskFormat,
42 };
43 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch);
44
45 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
46 return sAtlasIndices[format];
47}
48
49GrMaskFormat GrBatchFontCache::AtlasIndexToMaskFormat(int atlasIndex) {
50 static GrMaskFormat sMaskFormats[] = {
51 kA8_GrMaskFormat,
52 kA565_GrMaskFormat,
53 kARGB_GrMaskFormat,
54 };
55 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sMaskFormats) == kMaskFormatCount, array_size_mismatch);
56
57 SkASSERT(sMaskFormats[atlasIndex] < kMaskFormatCount);
58 return sMaskFormats[atlasIndex];
59}
60
61GrBatchFontCache::GrBatchFontCache()
62 : fPreserveStrike(NULL) {
63}
64
65void GrBatchFontCache::init(GrContext* context) {
66 for (int i = 0; i < kMaskFormatCount; i++) {
67 GrMaskFormat format = AtlasIndexToMaskFormat(i);
68 GrPixelConfig config = this->getPixelConfig(format);
69
70 if (kA8_GrMaskFormat == format) {
71 fAtlases[i] = make_atlas(context, config,
72 GR_FONT_ATLAS_A8_TEXTURE_WIDTH,
73 GR_FONT_ATLAS_TEXTURE_HEIGHT,
74 GR_FONT_ATLAS_A8_NUM_PLOTS_X,
75 GR_FONT_ATLAS_NUM_PLOTS_Y);
76 } else {
77 fAtlases[i] = make_atlas(context, config,
78 GR_FONT_ATLAS_TEXTURE_WIDTH,
79 GR_FONT_ATLAS_TEXTURE_HEIGHT,
80 GR_FONT_ATLAS_NUM_PLOTS_X,
81 GR_FONT_ATLAS_NUM_PLOTS_Y);
82 }
83
84 if (fAtlases[i]) {
85 fAtlases[i]->registerEvictionCallback(&GrBatchFontCache::HandleEviction, (void*)this);
86 }
87 }
88}
89
90GrBatchFontCache::~GrBatchFontCache() {
91 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
92 while (!iter.done()) {
93 SkDELETE(&(*iter));
94 ++iter;
95 }
96 for (int i = 0; i < kMaskFormatCount; ++i) {
97 SkDELETE(fAtlases[i]);
98 }
99}
100
101GrBatchTextStrike* GrBatchFontCache::generateStrike(GrFontScaler* scaler) {
102 GrBatchTextStrike* strike = SkNEW_ARGS(GrBatchTextStrike, (this, scaler->getKey()));
103 fCache.add(strike);
104 return strike;
105}
106
107void GrBatchFontCache::freeAll() {
108 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
109 while (!iter.done()) {
110 SkDELETE(&(*iter));
111 ++iter;
112 }
113 fCache.rewind();
114 for (int i = 0; i < kMaskFormatCount; ++i) {
115 SkDELETE(fAtlases[i]);
116 fAtlases[i] = NULL;
117 }
118}
119
120inline GrBatchAtlas* GrBatchFontCache::getAtlas(GrMaskFormat format) const {
121 int atlasIndex = MaskFormatToAtlasIndex(format);
122 SkASSERT(fAtlases[atlasIndex]);
123 return fAtlases[atlasIndex];
124}
125
126bool GrBatchFontCache::hasGlyph(GrGlyph* glyph) {
127 SkASSERT(glyph);
128 return this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID);
129}
130
joshualitt3cf98632015-04-07 10:21:27 -0700131void GrBatchFontCache::setGlyphRefToken(GrGlyph* glyph, GrBatchAtlas::BatchToken batchToken) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700132 SkASSERT(glyph);
joshualitt3cf98632015-04-07 10:21:27 -0700133 SkASSERT(this->getAtlas(glyph->fMaskFormat)->hasID(glyph->fID));
134 this->getAtlas(glyph->fMaskFormat)->setLastRefToken(glyph->fID, batchToken);
joshualitt7c3a2f82015-03-31 13:32:05 -0700135}
136
137bool GrBatchFontCache::addToAtlas(GrBatchTextStrike* strike, GrBatchAtlas::AtlasID* id,
138 GrBatchTarget* batchTarget,
139 GrMaskFormat format, int width, int height, const void* image,
140 SkIPoint16* loc) {
141 fPreserveStrike = strike;
142 return this->getAtlas(format)->addToAtlas(id, batchTarget, width, height, image, loc);
143}
144
145uint64_t GrBatchFontCache::atlasGeneration(GrMaskFormat format) const {
146 return this->getAtlas(format)->atlasGeneration();
147}
148
149GrTexture* GrBatchFontCache::getTexture(GrMaskFormat format) {
150 int atlasIndex = MaskFormatToAtlasIndex(format);
151 SkASSERT(fAtlases[atlasIndex]);
152 return fAtlases[atlasIndex]->getTexture();
153}
154
155GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const {
156 static const GrPixelConfig kPixelConfigs[] = {
157 kAlpha_8_GrPixelConfig,
158 kRGB_565_GrPixelConfig,
159 kSkia8888_GrPixelConfig
160 };
161 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_size_mismatch);
162
163 return kPixelConfigs[format];
164}
165
166void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
167 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
168
169 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCache);
170 for (; !iter.done(); ++iter) {
171 GrBatchTextStrike* strike = &*iter;
172 strike->removeID(id);
173
174 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
175 // triggered the eviction
176 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
177 fontCache->fCache.remove(*(strike->fFontScalerKey));
178 SkDELETE(strike);
179 }
180 }
181}
182
183void GrBatchFontCache::dump() const {
184 static int gDumpCount = 0;
185 for (int i = 0; i < kMaskFormatCount; ++i) {
186 if (fAtlases[i]) {
187 GrTexture* texture = fAtlases[i]->getTexture();
188 if (texture) {
189 SkString filename;
190#ifdef SK_BUILD_FOR_ANDROID
191 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
192#else
193 filename.printf("fontcache_%d%d.png", gDumpCount, i);
194#endif
195 texture->surfacePriv().savePixels(filename.c_str());
196 }
197 }
198 }
199 ++gDumpCount;
200}
201
202///////////////////////////////////////////////////////////////////////////////
203
204/*
205 The text strike is specific to a given font/style/matrix setup, which is
206 represented by the GrHostFontScaler object we are given in getGlyph().
207
208 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
209 atlas and a position within that texture.
210 */
211
212GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key)
213 : fFontScalerKey(SkRef(key))
214 , fPool(9/*start allocations at 512 bytes*/)
215 , fAtlasedGlyphs(0) {
216
217 fBatchFontCache = cache; // no need to ref, it won't go away before we do
218}
219
220GrBatchTextStrike::~GrBatchTextStrike() {
221 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
222 while (!iter.done()) {
223 (*iter).free();
224 ++iter;
225 }
226}
227
228GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed,
229 GrFontScaler* scaler) {
230 SkIRect bounds;
231 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
232 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
233 return NULL;
234 }
235 } else {
236 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
237 return NULL;
238 }
239 }
240 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
241
242 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
243 glyph->init(packed, bounds, format);
244 fCache.add(glyph);
245 return glyph;
246}
247
248void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) {
249 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
250 while (!iter.done()) {
251 if (id == (*iter).fID) {
252 (*iter).fID = GrBatchAtlas::kInvalidAtlasID;
253 fAtlasedGlyphs--;
254 SkASSERT(fAtlasedGlyphs >= 0);
255 }
256 ++iter;
257 }
258}
259
260bool GrBatchTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
261 int width = glyph->fBounds.width();
262 int height = glyph->fBounds.height();
263 bool useDistanceField =
264 (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID));
265 int pad = useDistanceField ? 2 * SK_DistanceFieldPad : 0;
266 int plotWidth = (kA8_GrMaskFormat == glyph->fMaskFormat) ? GR_FONT_ATLAS_A8_PLOT_WIDTH
267 : GR_FONT_ATLAS_PLOT_WIDTH;
268 if (width + pad > plotWidth) {
269 return true;
270 }
271 if (height + pad > GR_FONT_ATLAS_PLOT_HEIGHT) {
272 return true;
273 }
274
275 return false;
276}
277
278bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph,
279 GrFontScaler* scaler) {
280 SkASSERT(glyph);
281 SkASSERT(scaler);
282 SkASSERT(fCache.find(glyph->fPackedID));
283 SkASSERT(NULL == glyph->fPlot);
284
285 SkAutoUnref ar(SkSafeRef(scaler));
286
287 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
288
289 size_t size = glyph->fBounds.area() * bytesPerPixel;
290 GrAutoMalloc<1024> storage(size);
291
292 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
293 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
294 glyph->height(),
295 storage.get())) {
296 return false;
297 }
298 } else {
299 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
300 glyph->height(),
301 glyph->width() * bytesPerPixel,
302 storage.get())) {
303 return false;
304 }
305 }
306
307 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, glyph->fMaskFormat,
308 glyph->width(), glyph->height(),
309 storage.get(), &glyph->fAtlasLocation);
310 if (success) {
311 fAtlasedGlyphs++;
312 }
313 return success;
314}