参照:https://blog.youkuaiyun.com/ghost0620/article/details/52574020
1:添加打印接口
参照图片内存打印的方法,添加ttf文本纹理打印,方便和策划沟通如何在文本这一块进行优化
std::string FontAtlasCache::getCachedTextureInfo()
{
std::string buffer;
char buftmp[4096];
unsigned int count = 0;
unsigned int totalBytes = 0;
auto atlasMapCopy = _atlasMap;
for (auto&& atlas : atlasMapCopy) {
std::unordered_map<ssize_t, Texture2D*> _textures = atlas.second->getTextures();
for (auto& texture : _textures) {
memset(buftmp, 0, sizeof(buftmp));
Texture2D* tex = texture.second;
unsigned int bpp = tex->getBitsPerPixelForFormat();
// Each texture takes up width * height * bytesPerPixel bytes.
auto bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8;
totalBytes += bytes;
count++;
snprintf(buftmp, sizeof(buftmp) - 1, "\"%s\" page = %lu rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB\n",
atlas.first.c_str(),
(long)texture.first,
(long)tex->getReferenceCount(),
(long)tex->getName(),
(long)tex->getPixelsWide(),
(long)tex->getPixelsHigh(),
(long)bpp,
(long)bytes / 1024);
buffer += buftmp;
}
}
snprintf(buftmp, sizeof(buftmp) - 1, "FontAtlasCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)\n", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f));
buffer += buftmp;
return buffer;
}
2:对比优化效果
纹路默认大小改之前:
改之后, 可以看到占用内存确实是少了一大截,虽然某个字体可能因为内容过多,申请了5张纹理图