blob: 4079893b3a0b9cc74475675cc17bc774903d34be [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "GrGpu.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrRectanizer.h"
10#include "GrTextStrike.h"
11#include "GrTextStrike_impl.h"
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +000012#include "SkString.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000014#include "SkDistanceFieldGen.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000015
reed@google.comfa35e3d2012-06-26 20:16:17 +000016///////////////////////////////////////////////////////////////////////////////
17
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000018#define GR_ATLAS_TEXTURE_WIDTH 1024
19#define GR_ATLAS_TEXTURE_HEIGHT 2048
20
21#define GR_PLOT_WIDTH 256
22#define GR_PLOT_HEIGHT 256
23
24#define GR_NUM_PLOTS_X (GR_ATLAS_TEXTURE_WIDTH / GR_PLOT_WIDTH)
25#define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT)
26
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000027#define FONT_CACHE_STATS 0
28#if FONT_CACHE_STATS
29static int g_PurgeCount = 0;
30#endif
31
reed@google.comac10a2d2010-12-22 21:39:39 +000032GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) {
33 gpu->ref();
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000034 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070035 fAtlases[i] = NULL;
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000036 }
reed@google.comac10a2d2010-12-22 21:39:39 +000037
38 fHead = fTail = NULL;
39}
40
41GrFontCache::~GrFontCache() {
42 fCache.deleteAll();
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000043 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070044 delete fAtlases[i];
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000045 }
reed@google.comac10a2d2010-12-22 21:39:39 +000046 fGpu->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000047#if FONT_CACHE_STATS
48 GrPrintf("Num purges: %d\n", g_PurgeCount);
49#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000050}
51
commit-bot@chromium.org95294412013-09-26 15:28:40 +000052static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) {
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +000053 static const GrPixelConfig sPixelConfigs[] = {
54 kAlpha_8_GrPixelConfig,
55 kRGB_565_GrPixelConfig,
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000056 kSkia8888_GrPixelConfig,
57 kSkia8888_GrPixelConfig
58 };
59 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_size_mismatch);
60
61 return sPixelConfigs[format];
62}
63
64static int mask_format_to_atlas_index(GrMaskFormat format) {
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +000065 static const int sAtlasIndices[] = {
66 GrFontCache::kA8_AtlasType,
67 GrFontCache::k565_AtlasType,
68 GrFontCache::k8888_AtlasType,
69 GrFontCache::k8888_AtlasType
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000070 };
71 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch);
72
73 SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount);
74 return sAtlasIndices[format];
commit-bot@chromium.org95294412013-09-26 15:28:40 +000075}
76
reed@google.comac10a2d2010-12-22 21:39:39 +000077GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler,
78 const Key& key) {
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000079 GrMaskFormat format = scaler->getMaskFormat();
commit-bot@chromium.org95294412013-09-26 15:28:40 +000080 GrPixelConfig config = mask_format_to_pixel_config(format);
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000081 int atlasIndex = mask_format_to_atlas_index(format);
robertphillips1d86ee82014-06-24 15:08:49 -070082 if (NULL == fAtlases[atlasIndex]) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000083 SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH,
84 GR_ATLAS_TEXTURE_HEIGHT);
robertphillips952841b2014-06-30 08:26:50 -070085 fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, kNone_GrTextureFlags,
robertphillips1d86ee82014-06-24 15:08:49 -070086 textureSize,
87 GR_NUM_PLOTS_X,
88 GR_NUM_PLOTS_Y,
89 true));
reed@google.comac10a2d2010-12-22 21:39:39 +000090 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +000091 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike,
robertphillips1d86ee82014-06-24 15:08:49 -070092 (this, scaler->getKey(), format, fAtlases[atlasIndex]));
reed@google.comac10a2d2010-12-22 21:39:39 +000093 fCache.insert(key, strike);
94
95 if (fHead) {
96 fHead->fPrev = strike;
97 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000098 SkASSERT(NULL == fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +000099 fTail = strike;
100 }
101 strike->fPrev = NULL;
102 strike->fNext = fHead;
103 fHead = strike;
104
105 return strike;
106}
107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108void GrFontCache::freeAll() {
109 fCache.deleteAll();
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000110 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -0700111 delete fAtlases[i];
112 fAtlases[i] = NULL;
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000113 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114 fHead = NULL;
115 fTail = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000116}
117
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000118void GrFontCache::purgeStrike(GrTextStrike* strike) {
119 const GrFontCache::Key key(strike->fFontScalerKey);
120 fCache.remove(key, strike);
121 this->detachStrikeFromList(strike);
122 delete strike;
123}
124
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000125bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) {
jvanverth@google.combbe55fd2013-09-16 20:28:37 +0000126 SkASSERT(NULL != preserveStrike);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000127
robertphillips1d86ee82014-06-24 15:08:49 -0700128 GrAtlas* atlas = preserveStrike->fAtlas;
129 GrPlot* plot = atlas->getUnusedPlot();
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000130 if (NULL == plot) {
131 return false;
132 }
133 plot->resetRects();
134
135 GrTextStrike* strike = fHead;
jvanverth@google.combbe55fd2013-09-16 20:28:37 +0000136 GrMaskFormat maskFormat = preserveStrike->fMaskFormat;
bsalomon@google.com7359eae2011-06-21 21:18:25 +0000137 while (strike) {
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000138 if (maskFormat != strike->fMaskFormat) {
139 strike = strike->fNext;
bsalomon@google.com7359eae2011-06-21 21:18:25 +0000140 continue;
141 }
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000142
bsalomon@google.com7359eae2011-06-21 21:18:25 +0000143 GrTextStrike* strikeToPurge = strike;
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000144 strike = strikeToPurge->fNext;
145 strikeToPurge->removePlot(plot);
146
147 // clear out any empty strikes (except this one)
robertphillips1d86ee82014-06-24 15:08:49 -0700148 if (strikeToPurge != preserveStrike && strikeToPurge->fPlotUsage.isEmpty()) {
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000149 this->purgeStrike(strikeToPurge);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000150 }
151 }
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000152
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000153#if FONT_CACHE_STATS
154 ++g_PurgeCount;
155#endif
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000156
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000157 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000160#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000161void GrFontCache::validate() const {
162 int count = fCache.count();
163 if (0 == count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000164 SkASSERT(!fHead);
165 SkASSERT(!fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 } else if (1 == count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000167 SkASSERT(fHead == fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000169 SkASSERT(fHead != fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 }
171
172 int count2 = 0;
173 const GrTextStrike* strike = fHead;
174 while (strike) {
175 count2 += 1;
176 strike = strike->fNext;
177 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000178 SkASSERT(count == count2);
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
180 count2 = 0;
181 strike = fTail;
182 while (strike) {
183 count2 += 1;
184 strike = strike->fPrev;
185 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000186 SkASSERT(count == count2);
reed@google.comac10a2d2010-12-22 21:39:39 +0000187}
188#endif
189
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000190void GrFontCache::dump() const {
191 static int gDumpCount = 0;
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000192 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -0700193 if (NULL != fAtlases[i]) {
194 GrTexture* texture = fAtlases[i]->getTexture();
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000195 if (NULL != texture) {
196 SkString filename;
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000197#ifdef SK_BUILD_FOR_ANDROID
198 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
199#else
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000200 filename.printf("fontcache_%d%d.png", gDumpCount, i);
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000201#endif
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000202 texture->savePixels(filename.c_str());
203 }
204 }
205 }
206 ++gDumpCount;
207}
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000208
reed@google.comac10a2d2010-12-22 21:39:39 +0000209///////////////////////////////////////////////////////////////////////////////
210
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000211#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 static int gCounter;
213#endif
214
215/*
216 The text strike is specific to a given font/style/matrix setup, which is
217 represented by the GrHostFontScaler object we are given in getGlyph().
218
219 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
220 atlas and a position within that texture.
221 */
222
jvanverth733f5f52014-07-11 19:45:16 -0700223GrTextStrike::GrTextStrike(GrFontCache* cache, const GrFontDescKey* key,
reed@google.com98539c62011-03-15 15:40:16 +0000224 GrMaskFormat format,
robertphillips1d86ee82014-06-24 15:08:49 -0700225 GrAtlas* atlas) : fPool(64) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 fFontScalerKey = key;
227 fFontScalerKey->ref();
228
229 fFontCache = cache; // no need to ref, it won't go away before we do
robertphillips1d86ee82014-06-24 15:08:49 -0700230 fAtlas = atlas; // no need to ref, it won't go away before we do
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.com98539c62011-03-15 15:40:16 +0000232 fMaskFormat = format;
233
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000234#ifdef SK_DEBUG
reed@google.com3ef80cf2011-07-05 19:09:47 +0000235// GrPrintf(" GrTextStrike %p %d\n", this, gCounter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236 gCounter += 1;
237#endif
238}
239
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000240// this signature is needed because it's used with
241// SkTDArray::visitAll() (see destructor)
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000242static void free_glyph(GrGlyph*& glyph) { glyph->free(); }
243
reed@google.comac10a2d2010-12-22 21:39:39 +0000244GrTextStrike::~GrTextStrike() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 fFontScalerKey->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000246 fCache.getArray().visitAll(free_glyph);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000248#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000249 gCounter -= 1;
reed@google.com3ef80cf2011-07-05 19:09:47 +0000250// GrPrintf("~GrTextStrike %p %d\n", this, gCounter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251#endif
252}
253
254GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed,
255 GrFontScaler* scaler) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000256 SkIRect bounds;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000257 if (fUseDistanceField) {
258 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
259 return NULL;
260 }
261 } else {
262 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
263 return NULL;
264 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 }
266
267 GrGlyph* glyph = fPool.alloc();
268 glyph->init(packed, bounds);
269 fCache.insert(packed, glyph);
270 return glyph;
271}
272
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000273void GrTextStrike::removePlot(const GrPlot* plot) {
274 SkTDArray<GrGlyph*>& glyphArray = fCache.getArray();
275 for (int i = 0; i < glyphArray.count(); ++i) {
276 if (plot == glyphArray[i]->fPlot) {
277 glyphArray[i]->fPlot = NULL;
278 }
279 }
280
robertphillipsc4f30b12014-07-13 10:09:42 -0700281 GrAtlas::RemovePlot(&fPlotUsage, plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000282}
283
jvanverth@google.comd830d132013-11-11 20:54:09 +0000284
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000285bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) {
reed@google.com0ebe81a2011-04-04 20:06:59 +0000286#if 0 // testing hack to force us to flush our cache often
287 static int gCounter;
288 if ((++gCounter % 10) == 0) return false;
289#endif
290
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000291 SkASSERT(glyph);
292 SkASSERT(scaler);
293 SkASSERT(fCache.contains(glyph));
commit-bot@chromium.org49e80832013-10-07 18:20:27 +0000294 SkASSERT(NULL == glyph->fPlot);
reed@google.comac10a2d2010-12-22 21:39:39 +0000295
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000296 SkAutoRef ar(scaler);
reed@google.com98539c62011-03-15 15:40:16 +0000297
298 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000300 size_t size = glyph->fBounds.area() * bytesPerPixel;
301 SkAutoSMalloc<1024> storage(size);
jvanverth@google.comd830d132013-11-11 20:54:09 +0000302 if (fUseDistanceField) {
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000303 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
304 glyph->height(),
305 storage.get())) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000306 return false;
307 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000308 } else {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000309 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
310 glyph->height(),
311 glyph->width() * bytesPerPixel,
312 storage.get())) {
313 return false;
314 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000315 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000316
robertphillips1d86ee82014-06-24 15:08:49 -0700317 GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, glyph->width(),
318 glyph->height(), storage.get(),
319 &glyph->fAtlasLocation);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000320
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000321 if (NULL == plot) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000322 return false;
323 }
324
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000325 glyph->fPlot = plot;
reed@google.comac10a2d2010-12-22 21:39:39 +0000326 return true;
327}