Made an optimization for SVG graphics to avoid a lot of unnecessary re-rasterizations.

This commit is contained in:
Leon Styhre 2021-09-27 20:59:33 +02:00
parent ba07a0b24c
commit 081fbc5665
3 changed files with 11 additions and 3 deletions

View file

@ -72,8 +72,8 @@ bool TextureData::initSVGFromMemory(const std::string& fileData)
mSourceHeight = svgImage->height;
}
mWidth = static_cast<size_t>(std::round(mSourceWidth * mScaleDuringLoad));
mHeight = static_cast<size_t>(std::round(mSourceHeight * mScaleDuringLoad));
mWidth = static_cast<size_t>(floorf(floorf(mSourceWidth) * mScaleDuringLoad));
mHeight = static_cast<size_t>(floorf(floorf(mSourceHeight) * mScaleDuringLoad));
if (mWidth == 0) {
// Auto scale width to keep aspect ratio.

View file

@ -9,6 +9,8 @@
#ifndef ES_CORE_RESOURCES_TEXTURE_DATA_H
#define ES_CORE_RESOURCES_TEXTURE_DATA_H
#include "utils/MathUtil.h"
#include <cmath>
#include <mutex>
#include <string>
@ -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; }

View file

@ -194,9 +194,14 @@ std::shared_ptr<TextureResource> 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<TextureData> data;
if (mTextureData != nullptr)
data = mTextureData;