diff --git a/PortabilityLayer/AntiAliasTable.cpp b/PortabilityLayer/AntiAliasTable.cpp index 7380d92..435cc14 100644 --- a/PortabilityLayer/AntiAliasTable.cpp +++ b/PortabilityLayer/AntiAliasTable.cpp @@ -1,5 +1,8 @@ #include "AntiAliasTable.h" #include "RGBAColor.h" +#include "HostFileSystem.h" +#include "GpIOStream.h" +#include "PLBigEndian.h" #include #include @@ -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(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(baseColorRef.r), static_cast(baseColorRef.g), static_cast(baseColorRef.b), static_cast(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(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(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(floor(blendedColorGammaSpace * 255.0 + 0.5)); } } + + if (cacheable) + SaveToCache(cacheFileName); } #endif } diff --git a/PortabilityLayer/AntiAliasTable.h b/PortabilityLayer/AntiAliasTable.h index b5c8f2a..097c575 100644 --- a/PortabilityLayer/AntiAliasTable.h +++ b/PortabilityLayer/AntiAliasTable.h @@ -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; }; } diff --git a/PortabilityLayer/QDStandardPalette.cpp b/PortabilityLayer/QDStandardPalette.cpp index b099da8..173d7b3 100644 --- a/PortabilityLayer/QDStandardPalette.cpp +++ b/PortabilityLayer/QDStandardPalette.cpp @@ -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; }