blob: aa0526007d39978e046e32bbc0e4286190406f86 [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]) {
joshualitt7c3a2f82015-03-31 13:32:05 -070042 GrPixelConfig config = this->getPixelConfig(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 -0700102GrPixelConfig GrBatchFontCache::getPixelConfig(GrMaskFormat format) const {
103 static const GrPixelConfig kPixelConfigs[] = {
104 kAlpha_8_GrPixelConfig,
105 kRGB_565_GrPixelConfig,
106 kSkia8888_GrPixelConfig
107 };
108 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(kPixelConfigs) == kMaskFormatCount, array_size_mismatch);
109
110 return kPixelConfigs[format];
111}
112
113void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
114 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
115
116 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCache);
117 for (; !iter.done(); ++iter) {
118 GrBatchTextStrike* strike = &*iter;
119 strike->removeID(id);
120
121 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
122 // triggered the eviction
123 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
124 fontCache->fCache.remove(*(strike->fFontScalerKey));
joshualittae32c102015-04-21 09:37:57 -0700125 strike->fIsAbandoned = true;
126 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700127 }
128 }
129}
130
131void GrBatchFontCache::dump() const {
132 static int gDumpCount = 0;
133 for (int i = 0; i < kMaskFormatCount; ++i) {
134 if (fAtlases[i]) {
135 GrTexture* texture = fAtlases[i]->getTexture();
136 if (texture) {
137 SkString filename;
138#ifdef SK_BUILD_FOR_ANDROID
139 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
140#else
141 filename.printf("fontcache_%d%d.png", gDumpCount, i);
142#endif
143 texture->surfacePriv().savePixels(filename.c_str());
144 }
145 }
146 }
147 ++gDumpCount;
148}
149
150///////////////////////////////////////////////////////////////////////////////
151
152/*
153 The text strike is specific to a given font/style/matrix setup, which is
154 represented by the GrHostFontScaler object we are given in getGlyph().
155
156 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
157 atlas and a position within that texture.
158 */
159
160GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key)
161 : fFontScalerKey(SkRef(key))
162 , fPool(9/*start allocations at 512 bytes*/)
joshualittae32c102015-04-21 09:37:57 -0700163 , fAtlasedGlyphs(0)
164 , fIsAbandoned(false) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700165
166 fBatchFontCache = cache; // no need to ref, it won't go away before we do
167}
168
169GrBatchTextStrike::~GrBatchTextStrike() {
170 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
171 while (!iter.done()) {
172 (*iter).free();
173 ++iter;
174 }
175}
176
177GrGlyph* GrBatchTextStrike::generateGlyph(GrGlyph::PackedID packed,
178 GrFontScaler* scaler) {
179 SkIRect bounds;
180 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
181 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
182 return NULL;
183 }
184 } else {
185 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
186 return NULL;
187 }
188 }
189 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
190
191 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
192 glyph->init(packed, bounds, format);
193 fCache.add(glyph);
194 return glyph;
195}
196
197void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) {
198 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
199 while (!iter.done()) {
200 if (id == (*iter).fID) {
201 (*iter).fID = GrBatchAtlas::kInvalidAtlasID;
202 fAtlasedGlyphs--;
203 SkASSERT(fAtlasedGlyphs >= 0);
204 }
205 ++iter;
206 }
207}
208
joshualitt7c3a2f82015-03-31 13:32:05 -0700209bool GrBatchTextStrike::addGlyphToAtlas(GrBatchTarget* batchTarget, GrGlyph* glyph,
210 GrFontScaler* scaler) {
211 SkASSERT(glyph);
212 SkASSERT(scaler);
213 SkASSERT(fCache.find(glyph->fPackedID));
214 SkASSERT(NULL == glyph->fPlot);
215
216 SkAutoUnref ar(SkSafeRef(scaler));
217
218 int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
219
220 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700221 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700222
223 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
224 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
225 glyph->height(),
226 storage.get())) {
227 return false;
228 }
229 } else {
230 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
231 glyph->height(),
232 glyph->width() * bytesPerPixel,
233 storage.get())) {
234 return false;
235 }
236 }
237
238 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, batchTarget, glyph->fMaskFormat,
239 glyph->width(), glyph->height(),
240 storage.get(), &glyph->fAtlasLocation);
241 if (success) {
242 fAtlasedGlyphs++;
243 }
244 return success;
245}