blob: 06e199418ba92b326afca0fa637f5a21f5d12a2d [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;
jvanverth7023a002016-02-22 11:25:32 -080051 fAtlasConfigs[kA8_GrMaskFormat].fLog2Width = 11;
52 fAtlasConfigs[kA8_GrMaskFormat].fLog2Height = 11;
joshualittda04e0e2015-08-19 08:16:43 -070053 fAtlasConfigs[kA8_GrMaskFormat].fPlotWidth = 512;
54 fAtlasConfigs[kA8_GrMaskFormat].fPlotHeight = 256;
55
56 fAtlasConfigs[kA565_GrMaskFormat].fWidth = 1024;
57 fAtlasConfigs[kA565_GrMaskFormat].fHeight = 2048;
jvanverth7023a002016-02-22 11:25:32 -080058 fAtlasConfigs[kA565_GrMaskFormat].fLog2Width = 10;
59 fAtlasConfigs[kA565_GrMaskFormat].fLog2Height = 11;
joshualittda04e0e2015-08-19 08:16:43 -070060 fAtlasConfigs[kA565_GrMaskFormat].fPlotWidth = 256;
61 fAtlasConfigs[kA565_GrMaskFormat].fPlotHeight = 256;
62
63 fAtlasConfigs[kARGB_GrMaskFormat].fWidth = 1024;
64 fAtlasConfigs[kARGB_GrMaskFormat].fHeight = 2048;
jvanverth7023a002016-02-22 11:25:32 -080065 fAtlasConfigs[kARGB_GrMaskFormat].fLog2Width = 10;
66 fAtlasConfigs[kARGB_GrMaskFormat].fLog2Height = 11;
joshualittda04e0e2015-08-19 08:16:43 -070067 fAtlasConfigs[kARGB_GrMaskFormat].fPlotWidth = 256;
68 fAtlasConfigs[kARGB_GrMaskFormat].fPlotHeight = 256;
joshualitt7c3a2f82015-03-31 13:32:05 -070069}
70
71GrBatchFontCache::~GrBatchFontCache() {
bsalomonc5fd5c42016-05-17 11:58:24 -070072 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070073 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070074 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070075 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070076 ++iter;
77 }
78 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070079 delete fAtlases[i];
joshualitt7c3a2f82015-03-31 13:32:05 -070080 }
81}
82
joshualitt7c3a2f82015-03-31 13:32:05 -070083void GrBatchFontCache::freeAll() {
bsalomonc5fd5c42016-05-17 11:58:24 -070084 StrikeHash::Iter iter(&fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -070085 while (!iter.done()) {
joshualitta5f1d5a2015-05-22 13:09:57 -070086 (*iter).fIsAbandoned = true;
joshualittae32c102015-04-21 09:37:57 -070087 (*iter).unref();
joshualitt7c3a2f82015-03-31 13:32:05 -070088 ++iter;
89 }
90 fCache.rewind();
91 for (int i = 0; i < kMaskFormatCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -070092 delete fAtlases[i];
halcanary96fcdcc2015-08-27 07:41:13 -070093 fAtlases[i] = nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -070094 }
95}
96
joshualitt7c3a2f82015-03-31 13:32:05 -070097void GrBatchFontCache::HandleEviction(GrBatchAtlas::AtlasID id, void* ptr) {
98 GrBatchFontCache* fontCache = reinterpret_cast<GrBatchFontCache*>(ptr);
99
bsalomonc5fd5c42016-05-17 11:58:24 -0700100 StrikeHash::Iter iter(&fontCache->fCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700101 for (; !iter.done(); ++iter) {
102 GrBatchTextStrike* strike = &*iter;
103 strike->removeID(id);
104
105 // clear out any empty strikes. We will preserve the strike whose call to addToAtlas
106 // triggered the eviction
107 if (strike != fontCache->fPreserveStrike && 0 == strike->fAtlasedGlyphs) {
bsalomonc5fd5c42016-05-17 11:58:24 -0700108 fontCache->fCache.remove(GrBatchTextStrike::GetKey(*strike));
joshualittae32c102015-04-21 09:37:57 -0700109 strike->fIsAbandoned = true;
110 strike->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700111 }
112 }
113}
114
115void GrBatchFontCache::dump() const {
116 static int gDumpCount = 0;
117 for (int i = 0; i < kMaskFormatCount; ++i) {
118 if (fAtlases[i]) {
119 GrTexture* texture = fAtlases[i]->getTexture();
120 if (texture) {
121 SkString filename;
122#ifdef SK_BUILD_FOR_ANDROID
123 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
124#else
125 filename.printf("fontcache_%d%d.png", gDumpCount, i);
126#endif
127 texture->surfacePriv().savePixels(filename.c_str());
128 }
129 }
130 }
131 ++gDumpCount;
132}
133
joshualittda04e0e2015-08-19 08:16:43 -0700134void GrBatchFontCache::setAtlasSizes_ForTesting(const GrBatchAtlasConfig configs[3]) {
135 // delete any old atlases, this should be safe to do as long as we are not in the middle of a
136 // flush
137 for (int i = 0; i < kMaskFormatCount; i++) {
138 if (fAtlases[i]) {
halcanary385fe4d2015-08-26 13:07:48 -0700139 delete fAtlases[i];
halcanary96fcdcc2015-08-27 07:41:13 -0700140 fAtlases[i] = nullptr;
joshualittda04e0e2015-08-19 08:16:43 -0700141 }
142 }
143 memcpy(fAtlasConfigs, configs, sizeof(fAtlasConfigs));
144}
145
joshualitt7c3a2f82015-03-31 13:32:05 -0700146///////////////////////////////////////////////////////////////////////////////
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
bsalomonc5fd5c42016-05-17 11:58:24 -0700156GrBatchTextStrike::GrBatchTextStrike(GrBatchFontCache* owner, const SkDescriptor& key)
157 : fFontScalerKey(key)
joshualitt7c3a2f82015-03-31 13:32:05 -0700158 , fPool(9/*start allocations at 512 bytes*/)
bsalomonc5fd5c42016-05-17 11:58:24 -0700159 , fBatchFontCache(owner) // no need to ref, it won't go away before we do
joshualittae32c102015-04-21 09:37:57 -0700160 , fAtlasedGlyphs(0)
bsalomonc5fd5c42016-05-17 11:58:24 -0700161 , fIsAbandoned(false) {}
joshualitt7c3a2f82015-03-31 13:32:05 -0700162
163GrBatchTextStrike::~GrBatchTextStrike() {
164 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
165 while (!iter.done()) {
mtklein852f15d2016-03-17 10:51:27 -0700166 (*iter).reset();
joshualitt7c3a2f82015-03-31 13:32:05 -0700167 ++iter;
168 }
169}
170
joshualitt6c2c2b02015-07-24 10:37:00 -0700171GrGlyph* GrBatchTextStrike::generateGlyph(const SkGlyph& skGlyph, GrGlyph::PackedID packed,
joshualitt7c3a2f82015-03-31 13:32:05 -0700172 GrFontScaler* scaler) {
173 SkIRect bounds;
174 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(packed)) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700175 if (!scaler->getPackedGlyphDFBounds(skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700177 }
178 } else {
joshualitt6c2c2b02015-07-24 10:37:00 -0700179 if (!scaler->getPackedGlyphBounds(skGlyph, &bounds)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700180 return nullptr;
joshualitt7c3a2f82015-03-31 13:32:05 -0700181 }
182 }
joshualitt6c2c2b02015-07-24 10:37:00 -0700183 GrMaskFormat format = scaler->getPackedGlyphMaskFormat(skGlyph);
184
mtklein5a744b72015-09-14 11:11:17 -0700185 GrGlyph* glyph = (GrGlyph*)fPool.alloc(sizeof(GrGlyph));
joshualitt7c3a2f82015-03-31 13:32:05 -0700186 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
joshualitt50aa15b2015-11-23 14:09:55 -0800203bool GrBatchTextStrike::addGlyphToAtlas(GrDrawBatch::Target* target,
204 GrGlyph* glyph,
205 GrFontScaler* scaler,
joshualitt4f19ca32015-07-30 07:59:20 -0700206 GrMaskFormat expectedMaskFormat) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700207 SkASSERT(glyph);
208 SkASSERT(scaler);
209 SkASSERT(fCache.find(glyph->fPackedID));
joshualitt7c3a2f82015-03-31 13:32:05 -0700210
joshualitt4f19ca32015-07-30 07:59:20 -0700211 int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
joshualitt7c3a2f82015-03-31 13:32:05 -0700212
213 size_t size = glyph->fBounds.area() * bytesPerPixel;
joshualitt29f86792015-05-29 08:06:48 -0700214 SkAutoSMalloc<1024> storage(size);
joshualitt7c3a2f82015-03-31 13:32:05 -0700215
joshualitt50aa15b2015-11-23 14:09:55 -0800216 const SkGlyph& skGlyph = scaler->grToSkGlyph(glyph->fPackedID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700217 if (GrGlyph::kDistance_MaskStyle == GrGlyph::UnpackMaskStyle(glyph->fPackedID)) {
joshualitt6c2c2b02015-07-24 10:37:00 -0700218 if (!scaler->getPackedGlyphDFImage(skGlyph, glyph->width(), glyph->height(),
joshualitt7c3a2f82015-03-31 13:32:05 -0700219 storage.get())) {
220 return false;
221 }
222 } else {
joshualitt6c2c2b02015-07-24 10:37:00 -0700223 if (!scaler->getPackedGlyphImage(skGlyph, glyph->width(), glyph->height(),
joshualitt4f19ca32015-07-30 07:59:20 -0700224 glyph->width() * bytesPerPixel, expectedMaskFormat,
225 storage.get())) {
joshualitt7c3a2f82015-03-31 13:32:05 -0700226 return false;
227 }
228 }
229
bsalomon75398562015-08-17 12:55:38 -0700230 bool success = fBatchFontCache->addToAtlas(this, &glyph->fID, target, expectedMaskFormat,
joshualitt7c3a2f82015-03-31 13:32:05 -0700231 glyph->width(), glyph->height(),
232 storage.get(), &glyph->fAtlasLocation);
233 if (success) {
joshualitt65e96b42015-07-31 11:45:22 -0700234 SkASSERT(GrBatchAtlas::kInvalidAtlasID != glyph->fID);
joshualitt7c3a2f82015-03-31 13:32:05 -0700235 fAtlasedGlyphs++;
236 }
237 return success;
238}