2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GrGpu.h"
|
|
|
|
#include "GrRectanizer.h"
|
2014-09-30 19:18:44 +00:00
|
|
|
#include "GrSurfacePriv.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
#include "GrTextStrike.h"
|
|
|
|
#include "GrTextStrike_impl.h"
|
2013-10-02 18:19:17 +00:00
|
|
|
#include "SkString.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2014-03-11 15:57:40 +00:00
|
|
|
#include "SkDistanceFieldGen.h"
|
2013-11-11 20:54:09 +00:00
|
|
|
|
2012-06-26 20:16:17 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-04-01 16:25:11 +00:00
|
|
|
#define GR_ATLAS_TEXTURE_WIDTH 1024
|
|
|
|
#define GR_ATLAS_TEXTURE_HEIGHT 2048
|
|
|
|
|
|
|
|
#define GR_PLOT_WIDTH 256
|
|
|
|
#define GR_PLOT_HEIGHT 256
|
|
|
|
|
|
|
|
#define GR_NUM_PLOTS_X (GR_ATLAS_TEXTURE_WIDTH / GR_PLOT_WIDTH)
|
|
|
|
#define GR_NUM_PLOTS_Y (GR_ATLAS_TEXTURE_HEIGHT / GR_PLOT_HEIGHT)
|
|
|
|
|
2013-08-05 19:42:56 +00:00
|
|
|
#define FONT_CACHE_STATS 0
|
|
|
|
#if FONT_CACHE_STATS
|
|
|
|
static int g_PurgeCount = 0;
|
|
|
|
#endif
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
GrFontCache::GrFontCache(GrGpu* gpu) : fGpu(gpu) {
|
|
|
|
gpu->ref();
|
2013-12-03 19:45:22 +00:00
|
|
|
for (int i = 0; i < kAtlasCount; ++i) {
|
2014-06-24 22:08:49 +00:00
|
|
|
fAtlases[i] = NULL;
|
2013-09-26 12:57:19 +00:00
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
fHead = fTail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrFontCache::~GrFontCache() {
|
2014-07-22 20:25:26 +00:00
|
|
|
SkTDynamicHash<GrTextStrike, GrFontDescKey>::Iter iter(&fCache);
|
|
|
|
while (!iter.done()) {
|
|
|
|
SkDELETE(&(*iter));
|
|
|
|
++iter;
|
|
|
|
}
|
2013-12-03 19:45:22 +00:00
|
|
|
for (int i = 0; i < kAtlasCount; ++i) {
|
2014-06-24 22:08:49 +00:00
|
|
|
delete fAtlases[i];
|
2013-09-26 12:57:19 +00:00
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
fGpu->unref();
|
2013-08-05 19:42:56 +00:00
|
|
|
#if FONT_CACHE_STATS
|
|
|
|
GrPrintf("Num purges: %d\n", g_PurgeCount);
|
|
|
|
#endif
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 15:28:40 +00:00
|
|
|
static GrPixelConfig mask_format_to_pixel_config(GrMaskFormat format) {
|
2013-12-04 07:02:26 +00:00
|
|
|
static const GrPixelConfig sPixelConfigs[] = {
|
|
|
|
kAlpha_8_GrPixelConfig,
|
|
|
|
kRGB_565_GrPixelConfig,
|
2013-12-03 19:45:22 +00:00
|
|
|
kSkia8888_GrPixelConfig,
|
|
|
|
kSkia8888_GrPixelConfig
|
|
|
|
};
|
|
|
|
SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sPixelConfigs) == kMaskFormatCount, array_size_mismatch);
|
|
|
|
|
|
|
|
return sPixelConfigs[format];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mask_format_to_atlas_index(GrMaskFormat format) {
|
2013-12-04 07:02:26 +00:00
|
|
|
static const int sAtlasIndices[] = {
|
|
|
|
GrFontCache::kA8_AtlasType,
|
|
|
|
GrFontCache::k565_AtlasType,
|
|
|
|
GrFontCache::k8888_AtlasType,
|
|
|
|
GrFontCache::k8888_AtlasType
|
2013-12-03 19:45:22 +00:00
|
|
|
};
|
|
|
|
SK_COMPILE_ASSERT(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, array_size_mismatch);
|
|
|
|
|
|
|
|
SkASSERT(sAtlasIndices[format] < GrFontCache::kAtlasCount);
|
|
|
|
return sAtlasIndices[format];
|
2013-09-26 15:28:40 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 20:25:26 +00:00
|
|
|
GrTextStrike* GrFontCache::generateStrike(GrFontScaler* scaler) {
|
2014-10-10 18:36:12 +00:00
|
|
|
GrTextStrike* strike = SkNEW_ARGS(GrTextStrike, (this, scaler->getKey()));
|
2014-07-22 20:25:26 +00:00
|
|
|
fCache.add(strike);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
if (fHead) {
|
|
|
|
fHead->fPrev = strike;
|
|
|
|
} else {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(NULL == fTail);
|
2010-12-22 21:39:39 +00:00
|
|
|
fTail = strike;
|
|
|
|
}
|
|
|
|
strike->fPrev = NULL;
|
|
|
|
strike->fNext = fHead;
|
|
|
|
fHead = strike;
|
|
|
|
|
|
|
|
return strike;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GrFontCache::freeAll() {
|
2014-07-22 20:25:26 +00:00
|
|
|
SkTDynamicHash<GrTextStrike, GrFontDescKey>::Iter iter(&fCache);
|
|
|
|
while (!iter.done()) {
|
|
|
|
SkDELETE(&(*iter));
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
fCache.rewind();
|
2013-12-03 19:45:22 +00:00
|
|
|
for (int i = 0; i < kAtlasCount; ++i) {
|
2014-06-24 22:08:49 +00:00
|
|
|
delete fAtlases[i];
|
|
|
|
fAtlases[i] = NULL;
|
2013-09-26 12:57:19 +00:00
|
|
|
}
|
2011-03-30 21:26:44 +00:00
|
|
|
fHead = NULL;
|
|
|
|
fTail = NULL;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2013-10-27 20:50:23 +00:00
|
|
|
void GrFontCache::purgeStrike(GrTextStrike* strike) {
|
2014-07-22 20:25:26 +00:00
|
|
|
fCache.remove(*(strike->fFontScalerKey));
|
2013-10-27 20:50:23 +00:00
|
|
|
this->detachStrikeFromList(strike);
|
|
|
|
delete strike;
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:36:12 +00:00
|
|
|
|
|
|
|
GrPlot* GrFontCache::addToAtlas(GrMaskFormat format, GrAtlas::ClientPlotUsage* usage,
|
|
|
|
int width, int height, const void* image,
|
|
|
|
SkIPoint16* loc) {
|
|
|
|
GrPixelConfig config = mask_format_to_pixel_config(format);
|
|
|
|
int atlasIndex = mask_format_to_atlas_index(format);
|
|
|
|
if (NULL == fAtlases[atlasIndex]) {
|
|
|
|
SkISize textureSize = SkISize::Make(GR_ATLAS_TEXTURE_WIDTH,
|
|
|
|
GR_ATLAS_TEXTURE_HEIGHT);
|
2014-10-28 21:33:06 +00:00
|
|
|
fAtlases[atlasIndex] = SkNEW_ARGS(GrAtlas, (fGpu, config, kNone_GrSurfaceFlags,
|
2014-10-10 18:36:12 +00:00
|
|
|
textureSize,
|
|
|
|
GR_NUM_PLOTS_X,
|
|
|
|
GR_NUM_PLOTS_Y,
|
|
|
|
true));
|
|
|
|
}
|
|
|
|
return fAtlases[atlasIndex]->addToAtlas(usage, width, height, image, loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GrFontCache::freeUnusedPlot(GrTextStrike* preserveStrike, const GrGlyph* glyph) {
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(preserveStrike);
|
2014-03-03 14:30:25 +00:00
|
|
|
|
2014-10-10 18:36:12 +00:00
|
|
|
int index = mask_format_to_atlas_index(glyph->fMaskFormat);
|
|
|
|
GrAtlas* atlas = fAtlases[index];
|
2014-06-24 22:08:49 +00:00
|
|
|
GrPlot* plot = atlas->getUnusedPlot();
|
2014-03-03 14:30:25 +00:00
|
|
|
if (NULL == plot) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
plot->resetRects();
|
|
|
|
|
|
|
|
GrTextStrike* strike = fHead;
|
2013-08-05 19:42:56 +00:00
|
|
|
while (strike) {
|
|
|
|
GrTextStrike* strikeToPurge = strike;
|
2014-03-03 14:30:25 +00:00
|
|
|
strike = strikeToPurge->fNext;
|
|
|
|
strikeToPurge->removePlot(plot);
|
|
|
|
|
|
|
|
// clear out any empty strikes (except this one)
|
2014-06-24 22:08:49 +00:00
|
|
|
if (strikeToPurge != preserveStrike && strikeToPurge->fPlotUsage.isEmpty()) {
|
2013-10-27 20:50:23 +00:00
|
|
|
this->purgeStrike(strikeToPurge);
|
2013-08-05 19:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-03 14:30:25 +00:00
|
|
|
|
2013-08-05 19:42:56 +00:00
|
|
|
#if FONT_CACHE_STATS
|
|
|
|
++g_PurgeCount;
|
|
|
|
#endif
|
|
|
|
|
2014-03-03 14:30:25 +00:00
|
|
|
return true;
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 14:17:03 +00:00
|
|
|
#ifdef SK_DEBUG
|
2010-12-22 21:39:39 +00:00
|
|
|
void GrFontCache::validate() const {
|
|
|
|
int count = fCache.count();
|
|
|
|
if (0 == count) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(!fHead);
|
|
|
|
SkASSERT(!fTail);
|
2010-12-22 21:39:39 +00:00
|
|
|
} else if (1 == count) {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(fHead == fTail);
|
2010-12-22 21:39:39 +00:00
|
|
|
} else {
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(fHead != fTail);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int count2 = 0;
|
|
|
|
const GrTextStrike* strike = fHead;
|
|
|
|
while (strike) {
|
|
|
|
count2 += 1;
|
|
|
|
strike = strike->fNext;
|
|
|
|
}
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(count == count2);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
count2 = 0;
|
|
|
|
strike = fTail;
|
|
|
|
while (strike) {
|
|
|
|
count2 += 1;
|
|
|
|
strike = strike->fPrev;
|
|
|
|
}
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(count == count2);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-02 18:19:17 +00:00
|
|
|
void GrFontCache::dump() const {
|
|
|
|
static int gDumpCount = 0;
|
2013-12-03 19:45:22 +00:00
|
|
|
for (int i = 0; i < kAtlasCount; ++i) {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fAtlases[i]) {
|
2014-06-24 22:08:49 +00:00
|
|
|
GrTexture* texture = fAtlases[i]->getTexture();
|
2014-09-05 20:34:00 +00:00
|
|
|
if (texture) {
|
2013-10-02 18:19:17 +00:00
|
|
|
SkString filename;
|
2014-03-26 19:49:03 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
|
|
|
filename.printf("/sdcard/fontcache_%d%d.png", gDumpCount, i);
|
|
|
|
#else
|
2013-10-02 18:19:17 +00:00
|
|
|
filename.printf("fontcache_%d%d.png", gDumpCount, i);
|
2014-03-26 19:49:03 +00:00
|
|
|
#endif
|
2014-09-30 19:18:44 +00:00
|
|
|
texture->surfacePriv().savePixels(filename.c_str());
|
2013-10-02 18:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++gDumpCount;
|
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-08-28 14:17:03 +00:00
|
|
|
#ifdef SK_DEBUG
|
2010-12-22 21:39:39 +00:00
|
|
|
static int gCounter;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
The text strike is specific to a given font/style/matrix setup, which is
|
|
|
|
represented by the GrHostFontScaler object we are given in getGlyph().
|
|
|
|
|
|
|
|
We map a 32bit glyphID to a GrGlyph record, which in turn points to a
|
|
|
|
atlas and a position within that texture.
|
|
|
|
*/
|
|
|
|
|
2014-10-10 18:36:12 +00:00
|
|
|
GrTextStrike::GrTextStrike(GrFontCache* cache, const GrFontDescKey* key) : fPool(64) {
|
2010-12-22 21:39:39 +00:00
|
|
|
fFontScalerKey = key;
|
|
|
|
fFontScalerKey->ref();
|
|
|
|
|
|
|
|
fFontCache = cache; // no need to ref, it won't go away before we do
|
2011-03-15 15:40:16 +00:00
|
|
|
|
2013-08-28 14:17:03 +00:00
|
|
|
#ifdef SK_DEBUG
|
2011-07-05 19:09:47 +00:00
|
|
|
// GrPrintf(" GrTextStrike %p %d\n", this, gCounter);
|
2010-12-22 21:39:39 +00:00
|
|
|
gCounter += 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTextStrike::~GrTextStrike() {
|
|
|
|
fFontScalerKey->unref();
|
2014-07-22 20:25:26 +00:00
|
|
|
SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
|
|
|
|
while (!iter.done()) {
|
|
|
|
(*iter).free();
|
|
|
|
++iter;
|
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2013-08-28 14:17:03 +00:00
|
|
|
#ifdef SK_DEBUG
|
2010-12-22 21:39:39 +00:00
|
|
|
gCounter -= 1;
|
2011-07-05 19:09:47 +00:00
|
|
|
// GrPrintf("~GrTextStrike %p %d\n", this, gCounter);
|
2010-12-22 21:39:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GrGlyph* GrTextStrike::generateGlyph(GrGlyph::PackedID packed,
|
|
|
|
GrFontScaler* scaler) {
|
2013-07-17 21:39:42 +00:00
|
|
|
SkIRect bounds;
|
2014-04-14 22:05:07 +00:00
|
|
|
if (fUseDistanceField) {
|
|
|
|
if (!scaler->getPackedGlyphDFBounds(packed, &bounds)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!scaler->getPackedGlyphBounds(packed, &bounds)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
2014-10-10 18:36:12 +00:00
|
|
|
GrMaskFormat format = scaler->getPackedGlyphMaskFormat(packed);
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
GrGlyph* glyph = fPool.alloc();
|
2014-10-10 18:36:12 +00:00
|
|
|
glyph->init(packed, bounds, format);
|
2014-07-22 20:25:26 +00:00
|
|
|
fCache.add(glyph);
|
2010-12-22 21:39:39 +00:00
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:30:25 +00:00
|
|
|
void GrTextStrike::removePlot(const GrPlot* plot) {
|
2014-07-22 20:25:26 +00:00
|
|
|
SkTDynamicHash<GrGlyph, GrGlyph::PackedID>::Iter iter(&fCache);
|
|
|
|
while (!iter.done()) {
|
|
|
|
if (plot == (*iter).fPlot) {
|
|
|
|
(*iter).fPlot = NULL;
|
2014-03-03 14:30:25 +00:00
|
|
|
}
|
2014-07-22 20:25:26 +00:00
|
|
|
++iter;
|
2014-03-03 14:30:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 17:09:42 +00:00
|
|
|
GrAtlas::RemovePlot(&fPlotUsage, plot);
|
2013-08-05 19:42:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 20:07:38 +00:00
|
|
|
bool GrTextStrike::glyphTooLargeForAtlas(GrGlyph* glyph) {
|
|
|
|
int width = glyph->fBounds.width();
|
|
|
|
int height = glyph->fBounds.height();
|
|
|
|
int pad = fUseDistanceField ? 2 * SK_DistanceFieldPad : 0;
|
|
|
|
if (width + pad > GR_PLOT_WIDTH) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (height + pad > GR_PLOT_HEIGHT) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-11 20:54:09 +00:00
|
|
|
|
2014-03-03 14:30:25 +00:00
|
|
|
bool GrTextStrike::addGlyphToAtlas(GrGlyph* glyph, GrFontScaler* scaler) {
|
2011-04-04 20:06:59 +00:00
|
|
|
#if 0 // testing hack to force us to flush our cache often
|
|
|
|
static int gCounter;
|
|
|
|
if ((++gCounter % 10) == 0) return false;
|
|
|
|
#endif
|
|
|
|
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(glyph);
|
|
|
|
SkASSERT(scaler);
|
2014-07-22 20:25:26 +00:00
|
|
|
SkASSERT(fCache.find(glyph->fPackedID));
|
2013-10-07 18:20:27 +00:00
|
|
|
SkASSERT(NULL == glyph->fPlot);
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2014-07-15 20:29:34 +00:00
|
|
|
SkAutoUnref ar(SkSafeRef(scaler));
|
2011-03-15 15:40:16 +00:00
|
|
|
|
2014-10-10 18:36:12 +00:00
|
|
|
int bytesPerPixel = GrMaskFormatBytesPerPixel(glyph->fMaskFormat);
|
2013-11-11 20:54:09 +00:00
|
|
|
|
2014-04-14 22:05:07 +00:00
|
|
|
size_t size = glyph->fBounds.area() * bytesPerPixel;
|
2014-08-13 01:00:47 +00:00
|
|
|
GrAutoMalloc<1024> storage(size);
|
|
|
|
|
2013-11-11 20:54:09 +00:00
|
|
|
if (fUseDistanceField) {
|
2014-04-14 22:05:07 +00:00
|
|
|
if (!scaler->getPackedGlyphDFImage(glyph->fPackedID, glyph->width(),
|
|
|
|
glyph->height(),
|
|
|
|
storage.get())) {
|
2013-11-11 20:54:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!scaler->getPackedGlyphImage(glyph->fPackedID, glyph->width(),
|
|
|
|
glyph->height(),
|
|
|
|
glyph->width() * bytesPerPixel,
|
|
|
|
storage.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 18:36:12 +00:00
|
|
|
GrPlot* plot = fFontCache->addToAtlas(glyph->fMaskFormat, &fPlotUsage,
|
|
|
|
glyph->width(), glyph->height(),
|
|
|
|
storage.get(), &glyph->fAtlasLocation);
|
2014-04-14 22:05:07 +00:00
|
|
|
|
2013-09-27 19:39:38 +00:00
|
|
|
if (NULL == plot) {
|
2010-12-22 21:39:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-27 19:39:38 +00:00
|
|
|
glyph->fPlot = plot;
|
2010-12-22 21:39:39 +00:00
|
|
|
return true;
|
|
|
|
}
|