From d2ca019a75c9b1b4c9a3c66503231baff307970e Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Thu, 6 Oct 2022 20:20:48 +0200 Subject: [PATCH] Avoided unnecessary SVG file parsing for images previously found to be invalid. --- es-core/src/components/ImageComponent.cpp | 24 ++++++++++++++--------- es-core/src/resources/TextureData.cpp | 3 ++- es-core/src/resources/TextureData.h | 7 ++++--- es-core/src/resources/TextureResource.cpp | 5 +++++ es-core/src/resources/TextureResource.h | 2 ++ 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/es-core/src/components/ImageComponent.cpp b/es-core/src/components/ImageComponent.cpp index 56e470b6a..d2117f2d1 100644 --- a/es-core/src/components/ImageComponent.cpp +++ b/es-core/src/components/ImageComponent.cpp @@ -80,15 +80,21 @@ void ImageComponent::setImage(const std::string& path, bool tile) if (isScalable) { mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic, mLinearInterpolation, mMipmapping, 0, 0, 0.0f, 0.0f); - if (tile && (mTileWidth == 0.0f || mTileHeight == 0.0f)) - setTileAxes(); - resize(false); - mTexture.reset(); - mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic, mLinearInterpolation, - mMipmapping, static_cast(mSize.x), - static_cast(mSize.y), mTileWidth, mTileHeight); - mTexture->rasterizeAt(mSize.x, mSize.y); - onSizeChanged(); + if (mTexture->getIsInvalidSVGFile()) { + mTexture.reset(); + } + else { + if (tile && (mTileWidth == 0.0f || mTileHeight == 0.0f)) + setTileAxes(); + resize(false); + mTexture.reset(); + mTexture = + TextureResource::get(path, tile, mForceLoad, mDynamic, mLinearInterpolation, + mMipmapping, static_cast(mSize.x), + static_cast(mSize.y), mTileWidth, mTileHeight); + mTexture->rasterizeAt(mSize.x, mSize.y); + onSizeChanged(); + } } else { mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic, mLinearInterpolation, diff --git a/es-core/src/resources/TextureData.cpp b/es-core/src/resources/TextureData.cpp index 553f38c3b..d46bf3069 100644 --- a/es-core/src/resources/TextureData.cpp +++ b/es-core/src/resources/TextureData.cpp @@ -61,7 +61,8 @@ bool TextureData::initSVGFromMemory(const std::string& fileData) auto svgImage = lunasvg::Document::loadFromData(fileData); if (svgImage == nullptr) { - // LOG(LogError) << "Couldn't parse SVG image:" << mPath; + LOG(LogDebug) << "TextureData::initSVGFromMemory(): Couldn't parse SVG image \"" << mPath + << "\""; mInvalidSVGFile = true; return false; } diff --git a/es-core/src/resources/TextureData.h b/es-core/src/resources/TextureData.h index 5c382650c..d5df27762 100644 --- a/es-core/src/resources/TextureData.h +++ b/es-core/src/resources/TextureData.h @@ -73,9 +73,10 @@ public: const bool getPendingRasterization() { return mPendingRasterization; } const bool getScalable() { return mScalable; } - std::vector& getRawRGBAData() { return mDataRGBA; } - std::string getTextureFilePath() { return mPath; } - bool getTiled() { return mTile; } + const std::vector& getRawRGBAData() { return mDataRGBA; } + const std::string& getTextureFilePath() { return mPath; } + const bool getTiled() { return mTile; } + const bool getIsInvalidSVGFile() { return mInvalidSVGFile; } private: Renderer* mRenderer; diff --git a/es-core/src/resources/TextureResource.cpp b/es-core/src/resources/TextureResource.cpp index e0b3362e7..5b36c446a 100644 --- a/es-core/src/resources/TextureResource.cpp +++ b/es-core/src/resources/TextureResource.cpp @@ -23,6 +23,7 @@ TextureResource::TextureResource(const std::string& path, bool mipmapping, bool scalable) : mTextureData {nullptr} + , mInvalidSVGFile {false} , mForceLoad {false} { // Create a texture data object for this texture. @@ -38,6 +39,8 @@ TextureResource::TextureResource(const std::string& path, data->setMipmapping(mipmapping); // Force the texture manager to load it using a blocking load. sTextureDataManager.load(data, true); + if (scalable) + mInvalidSVGFile = data->getIsInvalidSVGFile(); } else { mTextureData = std::shared_ptr(new TextureData(tile)); @@ -48,6 +51,8 @@ TextureResource::TextureResource(const std::string& path, data->setMipmapping(mipmapping); // Load it so we can read the width/height. data->load(); + if (scalable) + mInvalidSVGFile = data->getIsInvalidSVGFile(); } mSize = glm::ivec2 {static_cast(data->width()), static_cast(data->height())}; diff --git a/es-core/src/resources/TextureResource.h b/es-core/src/resources/TextureResource.h index c0dd80a3d..11cafd835 100644 --- a/es-core/src/resources/TextureResource.h +++ b/es-core/src/resources/TextureResource.h @@ -61,6 +61,7 @@ public: void rasterizeAt(float width, float height); glm::vec2 getSourceImageSize() const { return mSourceSize; } + const bool getIsInvalidSVGFile() const { return mInvalidSVGFile; } virtual ~TextureResource(); @@ -101,6 +102,7 @@ private: glm::ivec2 mSize; glm::vec2 mSourceSize; + bool mInvalidSVGFile; bool mForceLoad; // File path, tile, linear interpolation, scalable/SVG, width, height.