From 96111537e2709cb0307c77ba5314f225091901da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 10 Mar 2023 17:26:41 +0100 Subject: [PATCH] Move PopulateLevel to the texture --- GPU/Common/ReplacedTexture.cpp | 49 ++++++++++++++++++++++++++++++++ GPU/Common/ReplacedTexture.h | 2 ++ GPU/Common/TextureReplacer.cpp | 51 +--------------------------------- GPU/Common/TextureReplacer.h | 1 - 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/GPU/Common/ReplacedTexture.cpp b/GPU/Common/ReplacedTexture.cpp index 356cfc1858..9ec0fae2bb 100644 --- a/GPU/Common/ReplacedTexture.cpp +++ b/GPU/Common/ReplacedTexture.cpp @@ -101,6 +101,55 @@ bool ReplacedTexture::IsReady(double budget) { return false; } +bool ReplacedTexture::PopulateLevel(ReplacedTextureLevel &level, bool ignoreError) { + bool good = false; + + if (!level.fileRef) { + if (!ignoreError) + ERROR_LOG(G3D, "Error opening replacement texture file '%s' in textures.zip", level.file.c_str()); + return false; + } + + size_t fileSize; + VFSOpenFile *file = vfs_->OpenFileForRead(level.fileRef, &fileSize); + if (!file) { + return false; + } + + std::string magic; + auto imageType = Identify(vfs_, file, &magic); + + if (imageType == ReplacedImageType::ZIM) { + uint32_t ignore = 0; + struct ZimHeader { + uint32_t magic; + uint32_t w; + uint32_t h; + uint32_t flags; + } header; + good = vfs_->Read(file, &header, sizeof(header)) == sizeof(header); + level.w = header.w; + level.h = header.h; + good = (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888; + } else if (imageType == ReplacedImageType::PNG) { + PNGHeaderPeek headerPeek; + good = vfs_->Read(file, &headerPeek, sizeof(headerPeek)) == sizeof(headerPeek); + if (good && headerPeek.IsValidPNGHeader()) { + level.w = headerPeek.Width(); + level.h = headerPeek.Height(); + good = true; + } else { + ERROR_LOG(G3D, "Could not get PNG dimensions: %s (zip)", level.file.ToVisualString().c_str()); + good = false; + } + } else { + ERROR_LOG(G3D, "Could not load texture replacement info: %s - unsupported format %s", level.file.ToVisualString().c_str(), magic.c_str()); + } + vfs_->CloseFile(file); + + return good; +} + void ReplacedTexture::Prepare(VFSBackend *vfs) { std::unique_lock lock(mutex_); this->vfs_ = vfs; diff --git a/GPU/Common/ReplacedTexture.h b/GPU/Common/ReplacedTexture.h index b46261b46b..2e71b92210 100644 --- a/GPU/Common/ReplacedTexture.h +++ b/GPU/Common/ReplacedTexture.h @@ -112,6 +112,8 @@ struct ReplacedTexture { bool IsReady(double budget); bool CopyLevelTo(int level, void *out, int rowPitch); + bool PopulateLevel(ReplacedTextureLevel &level, bool ignoreError); + protected: void Prepare(VFSBackend *vfs); void PrepareData(int level); diff --git a/GPU/Common/TextureReplacer.cpp b/GPU/Common/TextureReplacer.cpp index a026d31c7b..846146fffc 100644 --- a/GPU/Common/TextureReplacer.cpp +++ b/GPU/Common/TextureReplacer.cpp @@ -561,7 +561,7 @@ void TextureReplacer::PopulateReplacement(ReplacedTexture *texture, u64 cachekey bool good; level.fileRef = fileRef; - good = PopulateLevel(level, false); + good = texture->PopulateLevel(level, false); // We pad files that have been hashrange'd so they are the same texture size. level.w = (level.w * w) / newW; @@ -594,55 +594,6 @@ void TextureReplacer::PopulateReplacement(ReplacedTexture *texture, u64 cachekey texture->SetState(ReplacementState::POPULATED); } -bool TextureReplacer::PopulateLevel(ReplacedTextureLevel &level, bool ignoreError) { - bool good = false; - - if (!level.fileRef) { - if (!ignoreError) - ERROR_LOG(G3D, "Error opening replacement texture file '%s' in textures.zip", level.file.c_str()); - return false; - } - - size_t fileSize; - VFSOpenFile *file = vfs_->OpenFileForRead(level.fileRef, &fileSize); - if (!file) { - return false; - } - - std::string magic; - auto imageType = Identify(vfs_, file, &magic); - - if (imageType == ReplacedImageType::ZIM) { - uint32_t ignore = 0; - struct ZimHeader { - uint32_t magic; - uint32_t w; - uint32_t h; - uint32_t flags; - } header; - good = vfs_->Read(file, &header, sizeof(header)) == sizeof(header); - level.w = header.w; - level.h = header.h; - good = (header.flags & ZIM_FORMAT_MASK) == ZIM_RGBA8888; - } else if (imageType == ReplacedImageType::PNG) { - PNGHeaderPeek headerPeek; - good = vfs_->Read(file, &headerPeek, sizeof(headerPeek)) == sizeof(headerPeek); - if (good && headerPeek.IsValidPNGHeader()) { - level.w = headerPeek.Width(); - level.h = headerPeek.Height(); - good = true; - } else { - ERROR_LOG(G3D, "Could not get PNG dimensions: %s (zip)", level.file.ToVisualString().c_str()); - good = false; - } - } else { - ERROR_LOG(G3D, "Could not load texture replacement info: %s - unsupported format %s", level.file.ToVisualString().c_str(), magic.c_str()); - } - vfs_->CloseFile(file); - - return good; -} - static bool WriteTextureToPNG(png_imagep image, const Path &filename, int convert_to_8bit, const void *buffer, png_int_32 row_stride, const void *colormap) { FILE *fp = File::OpenCFile(filename, "wb"); if (!fp) { diff --git a/GPU/Common/TextureReplacer.h b/GPU/Common/TextureReplacer.h index bb3c020a98..8c6ee6e60d 100644 --- a/GPU/Common/TextureReplacer.h +++ b/GPU/Common/TextureReplacer.h @@ -140,7 +140,6 @@ protected: std::string LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored); std::string HashName(u64 cachekey, u32 hash, int level); void PopulateReplacement(ReplacedTexture *result, u64 cachekey, u32 hash, int w, int h); - bool PopulateLevel(ReplacedTextureLevel &level, bool ignoreError); bool enabled_ = false; bool allowVideo_ = false;