blob: 7e44fa52368fd671d45bc717c3a1d628267d0e22 [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()) {
joshualittae32c102015-04-21 09:37:57 -070077 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070078 ++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()) {
joshualittae32c102015-04-21 09:37:57 -070088 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070089 ++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));
joshualittae32c102015-04-21 09:37:57 -0700121 strike->fIsAbandoned = true;
122 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700123 }
124 }
125}
126
127void GrBatchFontCache::dump() const {
128 static int gDumpCount = 0;
129 for (int i = 0; i < kMaskFormatCount; ++i) {
130 if (fAtlases[i]) {
131 GrTexture* texture = fAtlases[i]->getTexture();
132 if (texture) {
133 SkString filename;
134#ifdef SK_BUILD_FOR_ANDROID
135 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
136#else
137 filename.printf("fontcache_%d%d.png", gDumpCount, i);
138#endif
139 texture->surfacePriv().savePixels(filename.c_str());
140 }
141 }
142 }
143 ++gDumpCount;
144}
145
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
156GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key)
157 : fFontScalerKey(SkRef(key))
158 , fPool(9/*start allocations at 512 bytes*/)
joshualittae32c102015-04-21 09:37:57 -0700159 , fAtlasedGlyphs(0)
160 , fIsAbandoned(false) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700161
162 fBatchFontCache = cache; // no need to ref, it won't go away before we do
163}
164
165GrBatchTextStrike::~GrBatchTextStrike() {
166 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
167 while (!iter.done()) {
168 (*iter).free();
169 ++iter;
170 }
171}
172
173GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed,
174 GrFontScaler* scaler) {
175 SkIRect bounds;
176 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
177 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
178 return NULL;
179 }
180 } else {
181 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
182 return NULL;
183 }
184 }
185 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
186
187 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
188 glyph->init(packed, bounds, format);
189 fCache.add(glyph);
190 return glyph;
191}
192
193void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) {
194 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
195 while (!iter.done()) {
196 if (id == (*iter).fID) {
197 (*iter).fID = GrBatchAtlas::kInvalidAtlasID;
198 fAtlasedGlyphs--;
199 SkASSERT(fAtlasedGlyphs >= 0);
200 }
201 ++iter;
202 }
203}
204
205bool GrBatchTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
206 int width = glyph->fBounds.width();
207 int height = glyph->fBounds.height();
208 bool useDistanceField =
209 (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID));
210 int pad = useDistanceField ? 2 * SK_DistanceFieldPad : 0;
211 int plotWidth = (kA8_GrMaskFormat == glyph->fMaskFormat) ? GR_FONT_ATLAS_A8_PLOT_WIDTH
212 : GR_FONT_ATLAS_PLOT_WIDTH;
213 if (width + pad > plotWidth) {
214 return true;
215 }
216 if (height + pad > GR_FONT_ATLAS_PLOT_HEIGHT) {
217 return true;
218 }
219
220 return false;
221}
222
223bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph,
224 GrFontScaler* scaler) {
225 SkASSERT(glyph);
226 SkASSERT(scaler);
227 SkASSERT(fCache.find(glyph->fPackedID));
228 SkASSERT(NULL == glyph->fPlot);
229
230 SkAutoUnref ar(SkSafeRef(scaler));
231
232 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
233
234 size_t size = glyph->fBounds.area() * bytesPerPixel;
235 GrAutoMalloc<1024> storage(size);
236
237 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
238 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
239 glyph->height(),
240 storage.get())) {
241 return false;
242 }
243 } else {
244 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
245 glyph->height(),
246 glyph->width() * bytesPerPixel,
247 storage.get())) {
248 return false;
249 }
250 }
251
252 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, glyph->fMaskFormat,
253 glyph->width(), glyph->height(),
254 storage.get(), &glyph->fAtlasLocation);
255 if (success) {
256 fAtlasedGlyphs++;
257 }
258 return success;
259}