blob: de8047b948bebf275844b9a3678ebc78363f3934 [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 "GrGpu.h"
11#include "GrRectanizer.h"
bsalomoneae62002015-07-31 13:59:30 -070012#include "GrResourceProvider.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070013#include "GrSurfacePriv.h"
14#include "SkString.h"
15
16#include "SkDistanceFieldGen.h"
17
18///////////////////////////////////////////////////////////////////////////////
19
joshualitt62db8ba2015-04-09 08:22:37 -070020bool GrBatchFontCache::initAtlas(GrMaskFormat format) {
21 int index = MaskFormatToAtlasIndex(format);
22 if (!fAtlases[index]) {
bsalomon265697d2015-07-22 10:17:26 -070023 GrPixelConfig config = MaskFormatToPixelConfig(format);
joshualittda04e0e2015-08-19 08:16:43 -070024 int width = fAtlasConfigs[index].fWidth;
25 int height = fAtlasConfigs[index].fHeight;
26 int numPlotsX = fAtlasConfigs[index].numPlotsX();
27 int numPlotsY = fAtlasConfigs[index].numPlotsY();
joshualitt7c3a2f82015-03-31 13:32:05 -070028
joshualittb356cbc2015-08-05 06:36:39 -070029 fAtlases[index] =
30 fContext->resourceProvider()->createAtlas(config, width, height,
31 numPlotsX, numPlotsY,
32 &GrBatchFontCache::HandleEviction,
33 (void*)this);
34 if (!fAtlases[index]) {
joshualitt62db8ba2015-04-09 08:22:37 -070035 return false;
joshualitt7c3a2f82015-03-31 13:32:05 -070036 }
37 }
joshualitt62db8ba2015-04-09 08:22:37 -070038 return true;
39}
40
41GrBatchFontCache::GrBatchFontCache(GrContext* context)
42 : fContext(context)
halcanary96fcdcc2015-08-27 07:41:13 -070043 , fPreserveStrike(nullptr) {
joshualitt62db8ba2015-04-09 08:22:37 -070044 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070045 fAtlases[i] = nullptr;
joshualitt62db8ba2015-04-09 08:22:37 -070046 }
joshualittda04e0e2015-08-19 08:16:43 -070047
48 // setup default atlas configs
49 fAtlasConfigs[kA8_GrMaskFormat].fWidth = 2048;
50 fAtlasConfigs[kA8_GrMaskFormat].fHeight = 2048;
51 fAtlasConfigs[kA8_GrMaskFormat].fPlotWidth = 512;
52 fAtlasConfigs[kA8_GrMaskFormat].fPlotHeight = 256;
53
54 fAtlasConfigs[kA565_GrMaskFormat].fWidth = 1024;
55 fAtlasConfigs[kA565_GrMaskFormat].fHeight = 2048;
56 fAtlasConfigs[kA565_GrMaskFormat].fPlotWidth = 256;
57 fAtlasConfigs[kA565_GrMaskFormat].fPlotHeight = 256;
58
59 fAtlasConfigs[kARGB_GrMaskFormat].fWidth = 1024;
60 fAtlasConfigs[kARGB_GrMaskFormat].fHeight = 2048;
61 fAtlasConfigs[kARGB_GrMaskFormat].fPlotWidth = 256;
62 fAtlasConfigs[kARGB_GrMaskFormat].fPlotHeight = 256;
joshualitt7c3a2f82015-03-31 13:32:05 -070063}
64
65GrBatchFontCache::~GrBatchFontCache() {
66 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
67 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070068 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070069 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070070 ++iter;
71 }
72 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070073 delete fAtlases[i];
joshualitt7c3a2f82015-03-31 13:32:05 -070074 }
75}
76
joshualitt7c3a2f82015-03-31 13:32:05 -070077void GrBatchFontCache::freeAll() {
78 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fCache);
79 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070080 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070081 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070082 ++iter;
83 }
84 fCache.rewind();
85 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070086 delete fAtlases[i];
halcanary96fcdcc2015-08-27 07:41:13 -070087 fAtlases[i] = nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -070088 }
89}
90
joshualitt7c3a2f82015-03-31 13:32:05 -070091void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
92 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
93
94 SkTDynamicHash<GrBatchTextStrike, GrFontDescKey>::Iter iter(&fontCache->fCache);
95 for (; !iter.done(); ++iter) {
96 GrBatchTextStrike* strike = &*iter;
97 strike->removeID(id);
98
99 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
100 // triggered the eviction
101 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
102 fontCache->fCache.remove(*(strike->fFontScalerKey));
joshualittae32c102015-04-21 09:37:57 -0700103 strike->fIsAbandoned = true;
104 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700105 }
106 }
107}
108
109void GrBatchFontCache::dump() const {
110 static int gDumpCount = 0;
111 for (int i = 0; i < kMaskFormatCount; ++i) {
112 if (fAtlases[i]) {
113 GrTexture* texture = fAtlases[i]->getTexture();
114 if (texture) {
115 SkString filename;
116#ifdef SK_BUILD_FOR_ANDROID
117 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
118#else
119 filename.printf("fontcache_%d%d.png", gDumpCount, i);
120#endif
121 texture->surfacePriv().savePixels(filename.c_str());
122 }
123 }
124 }
125 ++gDumpCount;
126}
127
joshualittda04e0e2015-08-19 08:16:43 -0700128void GrBatchFontCache::setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]) {
129 // delete any old atlases, this should be safe to do as long as we are not in the middle of a
130 // flush
131 for (int i = 0; i < kMaskFormatCount; i++) {
132 if (fAtlases[i]) {
halcanary385fe4d2015-08-26 13:07:48 -0700133 delete fAtlases[i];
halcanary96fcdcc2015-08-27 07:41:13 -0700134 fAtlases[i] = nullptr;
joshualittda04e0e2015-08-19 08:16:43 -0700135 }
136 }
137 memcpy(fAtlasConfigs, configs, sizeof(fAtlasConfigs));
138}
139
joshualitt7c3a2f82015-03-31 13:32:05 -0700140///////////////////////////////////////////////////////////////////////////////
141
142/*
143 The text strike is specific to a given font/style/matrix setup, which is
144 represented by the GrHostFontScaler object we are given in getGlyph().
145
146 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
147 atlas and a position within that texture.
148 */
149
150GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* cache, const GrFontDescKey* key)
151 : fFontScalerKey(SkRef(key))
152 , fPool(9/*start allocations at 512 bytes*/)
joshualittae32c102015-04-21 09:37:57 -0700153 , fAtlasedGlyphs(0)
154 , fIsAbandoned(false) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700155
156 fBatchFontCache = cache; // no need to ref, it won't go away before we do
157}
158
159GrBatchTextStrike::~GrBatchTextStrike() {
160 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
161 while (!iter.done()) {
162 (*iter).free();
163 ++iter;
164 }
165}
166
joshualitt6c2c2b02015-07-24 10:37:00 -0700167GrGlyph* GrBatchTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
joshualitt7c3a2f82015-03-31 13:32:05 -0700168 GrFontScaler* scaler) {
169 SkIRect bounds;
170 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700171 if (!scaler->getPackedGlyphDFBounds(skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700172 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700173 }
174 } else {
joshualitt6c2c2b02015-07-24 10:37:00 -0700175 if (!scaler->getPackedGlyphBounds(skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700177 }
178 }
joshualitt6c2c2b02015-07-24 10:37:00 -0700179 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(skGlyph);
180
joshualitt7c3a2f82015-03-31 13:32:05 -0700181 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph), SK_MALLOC_THROW);
182 glyph->init(packed, bounds, format);
183 fCache.add(glyph);
184 return glyph;
185}
186
187void GrBatchTextStrike::removeID(GrBatchAtlas::AtlasID id) {
188 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
189 while (!iter.done()) {
190 if (id == (*iter).fID) {
191 (*iter).fID = GrBatchAtlas::kInvalidAtlasID;
192 fAtlasedGlyphs--;
193 SkASSERT(fAtlasedGlyphs >= 0);
194 }
195 ++iter;
196 }
197}
198
bsalomon75398562015-08-17 12:55:38 -0700199bool GrBatchTextStrike::addGlyphToAtlas(GrDrawBatch::Target* target, GrGlyph* glyph,
joshualitt4f19ca32015-07-30 07:59:20 -0700200 GrFontScaler* scaler, const SkGlyph& skGlyph,
201 GrMaskFormat expectedMaskFormat) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700202 SkASSERT(glyph);
203 SkASSERT(scaler);
204 SkASSERT(fCache.find(glyph->fPackedID));
joshualitt7c3a2f82015-03-31 13:32:05 -0700205
206 SkAutoUnref ar(SkSafeRef(scaler));
207
joshualitt4f19ca32015-07-30 07:59:20 -0700208 int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -0700209
210 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700211 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700212
213 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700214 if (!scaler->getPackedGlyphDFImage(skGlyph, glyph->width(), glyph->height(),
joshualitt7c3a2f82015-03-31 13:32:05 -0700215 storage.get())) {
216 return false;
217 }
218 } else {
joshualitt6c2c2b02015-07-24 10:37:00 -0700219 if (!scaler->getPackedGlyphImage(skGlyph, glyph->width(), glyph->height(),
joshualitt4f19ca32015-07-30 07:59:20 -0700220 glyph->width() * bytesPerPixel, expectedMaskFormat,
221 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700222 return false;
223 }
224 }
225
bsalomon75398562015-08-17 12:55:38 -0700226 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, target, expectedMaskFormat,
joshualitt7c3a2f82015-03-31 13:32:05 -0700227 glyph->width(), glyph->height(),
228 storage.get(), &glyph->fAtlasLocation);
229 if (success) {
joshualitt65e96b42015-07-31 11:45:22 -0700230 SkASSERT(GrBatchAtlas::kInvalidAtlasID != glyph->fID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700231 fAtlasedGlyphs++;
232 }
233 return success;
234}