Add AA table caching

This commit is contained in:
elasota
2020-11-13 00:52:10 -05:00
parent 9dafba1092
commit 30b39c6991
3 changed files with 70 additions and 6 deletions

View File

@@ -1,5 +1,8 @@
#include "AntiAliasTable.h"
#include "RGBAColor.h"
#include "HostFileSystem.h"
#include "GpIOStream.h"
#include "PLBigEndian.h"
#include <algorithm>
#include <math.h>
@@ -58,8 +61,49 @@ namespace PortabilityLayer
}
}
#else
void AntiAliasTable::GenerateForPalette(const RGBAColor &baseColorRef, const RGBAColor *colors, size_t numColors)
bool AntiAliasTable::LoadFromCache(const char *cacheFileName)
{
GpIOStream *stream = PortabilityLayer::HostFileSystem::GetInstance()->OpenFile(PortabilityLayer::VirtualDirectories::kFontCache, cacheFileName, false, GpFileCreationDispositions::kOpenExisting);
if (!stream)
return false;
BEUInt32_t cacheVersion;
if (stream->Read(&cacheVersion, sizeof(cacheVersion)) != sizeof(cacheVersion) || cacheVersion != kCacheVersion)
{
stream->Close();
return false;
}
const size_t readSize = sizeof(m_aaTranslate);
const bool readOK = (stream->Read(m_aaTranslate, readSize) == readSize);
stream->Close();
return readOK;
}
void AntiAliasTable::SaveToCache(const char *cacheFileName)
{
GpIOStream *stream = PortabilityLayer::HostFileSystem::GetInstance()->OpenFile(PortabilityLayer::VirtualDirectories::kFontCache, cacheFileName, true, GpFileCreationDispositions::kCreateOrOverwrite);
if (!stream)
return;
BEUInt32_t cacheVersion(static_cast<uint32_t>(kCacheVersion));
stream->Write(&cacheVersion, sizeof(cacheVersion));
stream->Write(m_aaTranslate, sizeof(m_aaTranslate));
stream->Close();
}
void AntiAliasTable::GenerateForPalette(const RGBAColor &baseColorRef, const RGBAColor *colors, size_t numColors, bool cacheable)
{
char cacheFileName[256];
if (cacheable)
{
sprintf(cacheFileName, "aa_p_%02x%02x%02x%02x.cache", static_cast<int>(baseColorRef.r), static_cast<int>(baseColorRef.g), static_cast<int>(baseColorRef.b), static_cast<int>(baseColorRef.a));
if (LoadFromCache(cacheFileName))
return;
}
const RGBAColor baseColor = baseColorRef;
if (numColors > 256)
@@ -118,10 +162,21 @@ namespace PortabilityLayer
m_aaTranslate[i][b] = static_cast<uint8_t>(bestColor);
}
}
if (cacheable)
SaveToCache(cacheFileName);
}
void AntiAliasTable::GenerateForSimpleScale(uint8_t colorChannel)
void AntiAliasTable::GenerateForSimpleScale(uint8_t colorChannel, bool cacheable)
{
char cacheFileName[256];
if (cacheable)
{
sprintf(cacheFileName, "aa_t_%02x.cache", static_cast<int>(colorChannel));
if (LoadFromCache(cacheFileName))
return;
}
const double gamma = 1.8;
const double rcpGamma = 1.0 / gamma;
const double rcp255 = 1.0 / 255.0;
@@ -142,6 +197,9 @@ namespace PortabilityLayer
m_aaTranslate[baseColor][opacity] = static_cast<uint8_t>(floor(blendedColorGammaSpace * 255.0 + 0.5));
}
}
if (cacheable)
SaveToCache(cacheFileName);
}
#endif
}

View File

@@ -11,7 +11,13 @@ namespace PortabilityLayer
// Striped 256x16 because constant background color is more likely than constant sample
uint8_t m_aaTranslate[256][16];
void GenerateForPalette(const RGBAColor &baseColor, const RGBAColor *colors, size_t numColors);
void GenerateForSimpleScale(uint8_t colorChannel);
void GenerateForPalette(const RGBAColor &baseColor, const RGBAColor *colors, size_t numColors, bool cacheable);
void GenerateForSimpleScale(uint8_t colorChannel, bool cacheable);
private:
bool LoadFromCache(const char *path);
void SaveToCache(const char *path);
static const unsigned int kCacheVersion = 1;
};
}

View File

@@ -250,7 +250,7 @@ namespace PortabilityLayer
entry.m_rgb[0] = rgb[0];
entry.m_rgb[1] = rgb[1];
entry.m_rgb[2] = rgb[2];
entry.m_aaTable.GenerateForPalette(color, m_colors, 256);
entry.m_aaTable.GenerateForPalette(color, m_colors, 256, true);
return entry.m_aaTable;
}
@@ -269,7 +269,7 @@ namespace PortabilityLayer
CachedToneTableEntry &entry = m_cachedToneTables[m_numCachedToneTables++];
entry.m_tone = tone;
entry.m_aaTable.GenerateForSimpleScale(tone);
entry.m_aaTable.GenerateForSimpleScale(tone, true);
return entry.m_aaTable;
}