From 081fbc566582c368d10565b291cd8e010faa9510 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Mon, 27 Sep 2021 20:59:33 +0200 Subject: [PATCH] Made an optimization for SVG graphics to avoid a lot of unnecessary re-rasterizations. --- es-core/src/resources/TextureData.cpp | 4 ++-- es-core/src/resources/TextureData.h | 3 +++ es-core/src/resources/TextureResource.cpp | 7 ++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/es-core/src/resources/TextureData.cpp b/es-core/src/resources/TextureData.cpp index 5760111e0..924abd87f 100644 --- a/es-core/src/resources/TextureData.cpp +++ b/es-core/src/resources/TextureData.cpp @@ -72,8 +72,8 @@ bool TextureData::initSVGFromMemory(const std::string& fileData) mSourceHeight = svgImage->height; } - mWidth = static_cast(std::round(mSourceWidth * mScaleDuringLoad)); - mHeight = static_cast(std::round(mSourceHeight * mScaleDuringLoad)); + mWidth = static_cast(floorf(floorf(mSourceWidth) * mScaleDuringLoad)); + mHeight = static_cast(floorf(floorf(mSourceHeight) * mScaleDuringLoad)); if (mWidth == 0) { // Auto scale width to keep aspect ratio. diff --git a/es-core/src/resources/TextureData.h b/es-core/src/resources/TextureData.h index ebb27b3e8..c50cd19d3 100644 --- a/es-core/src/resources/TextureData.h +++ b/es-core/src/resources/TextureData.h @@ -9,6 +9,8 @@ #ifndef ES_CORE_RESOURCES_TEXTURE_DATA_H #define ES_CORE_RESOURCES_TEXTURE_DATA_H +#include "utils/MathUtil.h" + #include #include #include @@ -53,6 +55,7 @@ public: float sourceWidth(); float sourceHeight(); void setSourceSize(float width, float height); + glm::vec2 getSize() { return glm::vec2{mWidth, mHeight}; } // Define a factor for scaling the file when loading it (1.0f = no scaling). void setScaleDuringLoad(float scale) { mScaleDuringLoad = scale; } diff --git a/es-core/src/resources/TextureResource.cpp b/es-core/src/resources/TextureResource.cpp index 33fec7e8a..74e5d5bfe 100644 --- a/es-core/src/resources/TextureResource.cpp +++ b/es-core/src/resources/TextureResource.cpp @@ -194,9 +194,14 @@ std::shared_ptr TextureResource::get(const std::string& path, return tex; } -// For scalable source images in textures we want to set the resolution to rasterize at. void TextureResource::rasterizeAt(size_t width, size_t height) { + if (mTextureData != nullptr) { + glm::vec2 textureSize = mTextureData.get()->getSize(); + if (textureSize.x == width && textureSize.y == height) + return; + } + std::shared_ptr data; if (mTextureData != nullptr) data = mTextureData;