blob: 8ae10cc8ddafc937230b61be62408e830914012c [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"
bsalomonafbf2d62014-09-30 12:18:44 -070010#include "GrSurfacePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrTextStrike.h"
12#include "GrTextStrike_impl.h"
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +000013#include "SkString.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000015#include "SkDistanceFieldGen.h"
jvanverth@google.comd830d132013-11-11 20:54:09 +000016
reed@google.comfa35e3d2012-06-26 20:16:17 +000017///////////////////////////////////////////////////////////////////////////////
18
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000019#define GR_ATLAS_TEXTURE_WIDTH 1024
20#define GR_ATLAS_TEXTURE_HEIGHT 2048
21
22#define GR_PLOT_WIDTH 256
23#define GR_PLOT_HEIGHT 256
24
25#define GR_NUM_PLOTS_X (GR_ATLAS_TEXTURE_WIDTH / GR_PLOT_WIDTH)
26#define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT)
27
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000028#define FONT_CACHE_STATS 0
29#if FONT_CACHE_STATS
30static int g_PurgeCount = 0;
31#endif
32
reed@google.comac10a2d2010-12-22 21:39:39 +000033GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) {
34 gpu->ref();
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000035 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070036 fAtlases[i] = NULL;
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000037 }
reed@google.comac10a2d2010-12-22 21:39:39 +000038
39 fHead = fTail = NULL;
40}
41
42GrFontCache::~GrFontCache() {
jvanverthdd6d2272014-07-22 13:25:26 -070043 SkTDynamicHash<GrTextStrike, GrFontDescKey>::Iter iter(&fCache);
44 while (!iter.done()) {
45 SkDELETE(&(*iter));
46 ++iter;
47 }
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000048 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070049 delete fAtlases[i];
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000050 }
reed@google.comac10a2d2010-12-22 21:39:39 +000051 fGpu->unref();
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000052#if FONT_CACHE_STATS
53 GrPrintf("Num purges: %d\n", g_PurgeCount);
54#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000055}
56
commit-bot@chromium.org95294412013-09-26 15:28:40 +000057static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) {
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +000058 static const GrPixelConfig sPixelConfigs[] = {
59 kAlpha_8_GrPixelConfig,
60 kRGB_565_GrPixelConfig,
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000061 kSkia8888_GrPixelConfig,
62 kSkia8888_GrPixelConfig
63 };
64 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_size_mismatch);
65
66 return sPixelConfigs[format];
67}
68
69static int mask_format_to_atlas_index(GrMaskFormat format) {
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +000070 static const int sAtlasIndices[] = {
71 GrFontCache::kA8_AtlasType,
72 GrFontCache::k565_AtlasType,
73 GrFontCache::k8888_AtlasType,
74 GrFontCache::k8888_AtlasType
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000075 };
76 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch);
77
78 SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount);
79 return sAtlasIndices[format];
commit-bot@chromium.org95294412013-09-26 15:28:40 +000080}
81
jvanverthdd6d2272014-07-22 13:25:26 -070082GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler) {
jvanverthe817dbb2014-10-10 08:52:03 -070083 GrMaskFormat format = scaler->getMaskFormat();
84 GrPixelConfig config = mask_format_to_pixel_config(format);
85 int atlasIndex = mask_format_to_atlas_index(format);
86 if (NULL == fAtlases[atlasIndex]) {
87 SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH,
88 GR_ATLAS_TEXTURE_HEIGHT);
89 fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, kNone_GrTextureFlags,
90 textureSize,
91 GR_NUM_PLOTS_X,
92 GR_NUM_PLOTS_Y,
93 true));
94 }
95 GrTextStrike* strike = SkNEW_ARGS(GrTextStrike,
96 (this, scaler->getKey(), format, fAtlases[atlasIndex]));
jvanverthdd6d2272014-07-22 13:25:26 -070097 fCache.add(strike);
reed@google.comac10a2d2010-12-22 21:39:39 +000098
99 if (fHead) {
100 fHead->fPrev = strike;
101 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(NULL == fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 fTail = strike;
104 }
105 strike->fPrev = NULL;
106 strike->fNext = fHead;
107 fHead = strike;
108
109 return strike;
110}
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112void GrFontCache::freeAll() {
jvanverthdd6d2272014-07-22 13:25:26 -0700113 SkTDynamicHash<GrTextStrike, GrFontDescKey>::Iter iter(&fCache);
114 while (!iter.done()) {
115 SkDELETE(&(*iter));
116 ++iter;
117 }
118 fCache.rewind();
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000119 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -0700120 delete fAtlases[i];
121 fAtlases[i] = NULL;
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000122 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000123 fHead = NULL;
124 fTail = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000125}
126
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000127void GrFontCache::purgeStrike(GrTextStrike* strike) {
jvanverthdd6d2272014-07-22 13:25:26 -0700128 fCache.remove(*(strike->fFontScalerKey));
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000129 this->detachStrikeFromList(strike);
130 delete strike;
131}
132
jvanverthe817dbb2014-10-10 08:52:03 -0700133bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike) {
bsalomon49f085d2014-09-05 13:34:00 -0700134 SkASSERT(preserveStrike);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000135
jvanverthe817dbb2014-10-10 08:52:03 -0700136 GrAtlas* atlas = preserveStrike->fAtlas;
robertphillips1d86ee82014-06-24 15:08:49 -0700137 GrPlot* plot = atlas->getUnusedPlot();
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000138 if (NULL == plot) {
139 return false;
140 }
141 plot->resetRects();
142
143 GrTextStrike* strike = fHead;
jvanverthe817dbb2014-10-10 08:52:03 -0700144 GrMaskFormat maskFormat = preserveStrike->fMaskFormat;
bsalomon@google.com7359eae2011-06-21 21:18:25 +0000145 while (strike) {
jvanverthe817dbb2014-10-10 08:52:03 -0700146 if (maskFormat != strike->fMaskFormat) {
147 strike = strike->fNext;
148 continue;
149 }
150
bsalomon@google.com7359eae2011-06-21 21:18:25 +0000151 GrTextStrike* strikeToPurge = strike;
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000152 strike = strikeToPurge->fNext;
153 strikeToPurge->removePlot(plot);
154
155 // clear out any empty strikes (except this one)
robertphillips1d86ee82014-06-24 15:08:49 -0700156 if (strikeToPurge != preserveStrike && strikeToPurge->fPlotUsage.isEmpty()) {
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000157 this->purgeStrike(strikeToPurge);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000158 }
159 }
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000160
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000161#if FONT_CACHE_STATS
162 ++g_PurgeCount;
163#endif
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000164
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000165 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166}
167
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000168#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000169void GrFontCache::validate() const {
170 int count = fCache.count();
171 if (0 == count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000172 SkASSERT(!fHead);
173 SkASSERT(!fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 } else if (1 == count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000175 SkASSERT(fHead == fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000177 SkASSERT(fHead != fTail);
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 }
179
180 int count2 = 0;
181 const GrTextStrike* strike = fHead;
182 while (strike) {
183 count2 += 1;
184 strike = strike->fNext;
185 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000186 SkASSERT(count == count2);
reed@google.comac10a2d2010-12-22 21:39:39 +0000187
188 count2 = 0;
189 strike = fTail;
190 while (strike) {
191 count2 += 1;
192 strike = strike->fPrev;
193 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000194 SkASSERT(count == count2);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195}
196#endif
197
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000198void GrFontCache::dump() const {
199 static int gDumpCount = 0;
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000200 for (int i = 0; i < kAtlasCount; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700201 if (fAtlases[i]) {
robertphillips1d86ee82014-06-24 15:08:49 -0700202 GrTexture* texture = fAtlases[i]->getTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700203 if (texture) {
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000204 SkString filename;
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000205#ifdef SK_BUILD_FOR_ANDROID
206 filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
207#else
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000208 filename.printf("fontcache_%d%d.png", gDumpCount, i);
commit-bot@chromium.org4362a382014-03-26 19:49:03 +0000209#endif
bsalomonafbf2d62014-09-30 12:18:44 -0700210 texture->surfacePriv().savePixels(filename.c_str());
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000211 }
212 }
213 }
214 ++gDumpCount;
215}
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000216
reed@google.comac10a2d2010-12-22 21:39:39 +0000217///////////////////////////////////////////////////////////////////////////////
218
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000219#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 static int gCounter;
221#endif
222
223/*
224 The text strike is specific to a given font/style/matrix setup, which is
225 represented by the GrHostFontScaler object we are given in getGlyph().
226
227 We map a 32bit glyphID to a GrGlyph record, which in turn points to a
228 atlas and a position within that texture.
229 */
230
jvanverthe817dbb2014-10-10 08:52:03 -0700231GrTextStrike::GrTextStrike(GrFontCache* cache, const GrFontDescKey* key,
232 GrMaskFormat format,
233 GrAtlas* atlas) : fPool(64) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 fFontScalerKey = key;
235 fFontScalerKey->ref();
236
237 fFontCache = cache; // no need to ref, it won't go away before we do
jvanverthe817dbb2014-10-10 08:52:03 -0700238 fAtlas = atlas; // no need to ref, it won't go away before we do
239
240 fMaskFormat = format;
reed@google.com98539c62011-03-15 15:40:16 +0000241
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000242#ifdef SK_DEBUG
reed@google.com3ef80cf2011-07-05 19:09:47 +0000243// GrPrintf(" GrTextStrike %p %d\n", this, gCounter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 gCounter += 1;
245#endif
246}
247
reed@google.comac10a2d2010-12-22 21:39:39 +0000248GrTextStrike::~GrTextStrike() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000249 fFontScalerKey->unref();
jvanverthdd6d2272014-07-22 13:25:26 -0700250 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
251 while (!iter.done()) {
252 (*iter).free();
253 ++iter;
254 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000255
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000256#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 gCounter -= 1;
reed@google.com3ef80cf2011-07-05 19:09:47 +0000258// GrPrintf("~GrTextStrike %p %d\n", this, gCounter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000259#endif
260}
261
262GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed,
263 GrFontScaler* scaler) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000264 SkIRect bounds;
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000265 if (fUseDistanceField) {
266 if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
267 return NULL;
268 }
269 } else {
270 if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
271 return NULL;
272 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 }
jvanverthe817dbb2014-10-10 08:52:03 -0700274
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 GrGlyph* glyph = fPool.alloc();
jvanverthe817dbb2014-10-10 08:52:03 -0700276 glyph->init(packed, bounds);
jvanverthdd6d2272014-07-22 13:25:26 -0700277 fCache.add(glyph);
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 return glyph;
279}
280
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000281void GrTextStrike::removePlot(const GrPlot* plot) {
jvanverthdd6d2272014-07-22 13:25:26 -0700282 SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
283 while (!iter.done()) {
284 if (plot == (*iter).fPlot) {
285 (*iter).fPlot = NULL;
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000286 }
jvanverthdd6d2272014-07-22 13:25:26 -0700287 ++iter;
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000288 }
289
robertphillipsc4f30b12014-07-13 10:09:42 -0700290 GrAtlas::RemovePlot(&fPlotUsage, plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +0000291}
292
jvanverth681e65b2014-09-19 13:07:38 -0700293bool GrTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
294 int width = glyph->fBounds.width();
295 int height = glyph->fBounds.height();
296 int pad = fUseDistanceField ? 2 * SK_DistanceFieldPad : 0;
297 if (width + pad > GR_PLOT_WIDTH) {
298 return true;
299 }
300 if (height + pad > GR_PLOT_HEIGHT) {
301 return true;
302 }
303
304 return false;
305}
jvanverth@google.comd830d132013-11-11 20:54:09 +0000306
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000307bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) {
reed@google.com0ebe81a2011-04-04 20:06:59 +0000308#if 0 // testing hack to force us to flush our cache often
309 static int gCounter;
310 if ((++gCounter % 10) == 0) return false;
311#endif
312
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000313 SkASSERT(glyph);
314 SkASSERT(scaler);
jvanverthdd6d2272014-07-22 13:25:26 -0700315 SkASSERT(fCache.find(glyph->fPackedID));
commit-bot@chromium.org49e80832013-10-07 18:20:27 +0000316 SkASSERT(NULL == glyph->fPlot);
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
mtkleina179a1e2014-07-15 13:29:34 -0700318 SkAutoUnref ar(SkSafeRef(scaler));
reed@google.com98539c62011-03-15 15:40:16 +0000319
jvanverthe817dbb2014-10-10 08:52:03 -0700320 int bytesPerPixel = GrMaskFormatBytesPerPixel(fMaskFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +0000321
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000322 size_t size = glyph->fBounds.area() * bytesPerPixel;
georgeb62508b2014-08-12 18:00:47 -0700323 GrAutoMalloc<1024> storage(size);
324
jvanverth@google.comd830d132013-11-11 20:54:09 +0000325 if (fUseDistanceField) {
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000326 if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
327 glyph->height(),
328 storage.get())) {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000329 return false;
330 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000331 } else {
jvanverth@google.comd830d132013-11-11 20:54:09 +0000332 if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
333 glyph->height(),
334 glyph->width() * bytesPerPixel,
335 storage.get())) {
336 return false;
337 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000338 }
jvanverth@google.comd830d132013-11-11 20:54:09 +0000339
jvanverthe817dbb2014-10-10 08:52:03 -0700340 GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, glyph->width(),
341 glyph->height(), storage.get(),
342 &glyph->fAtlasLocation);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000343
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000344 if (NULL == plot) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 return false;
346 }
347
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000348 glyph->fPlot = plot;
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 return true;
350}