1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-10 22:31:40 +02:00

Temporarily(?) use weak_ptr to reduce ram usage increase during long

game sessions
This commit is contained in:
Ivan Savenko
2025-03-16 17:35:31 +00:00
parent ded12f2df9
commit 8a0fed7b3a
4 changed files with 27 additions and 18 deletions

View File

@@ -58,13 +58,14 @@ std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath &
auto it = animationFiles.find(actualPath);
if (it != animationFiles.end())
return it->second;
{
auto locked = it->second.lock();
if (locked)
return locked;
}
if (!CResourceHandler::get()->existsResource(actualPath))
{
animationFiles[actualPath] = nullptr;
return nullptr;
}
auto result = std::make_shared<CDefFile>(actualPath);
@@ -200,7 +201,11 @@ std::shared_ptr<ScalableImageShared> RenderHandler::loadImageImpl(const ImageLoc
{
auto it = imageFiles.find(locator);
if (it != imageFiles.end())
return it->second;
{
auto locked = it->second.lock();
if (locked)
return locked;
}
auto sdlImage = loadImageFromFileUncached(locator);
auto scaledImage = std::make_shared<ScalableImageShared>(locator, sdlImage);

View File

@@ -24,9 +24,9 @@ class RenderHandler final : public IRenderHandler
{
using AnimationLayoutMap = std::map<size_t, std::vector<ImageLocator>>;
std::map<AnimationPath, std::shared_ptr<CDefFile>> animationFiles;
std::map<AnimationPath, std::weak_ptr<CDefFile>> animationFiles;
std::map<AnimationPath, AnimationLayoutMap> animationLayouts;
std::map<SharedImageLocator, std::shared_ptr<ScalableImageShared>> imageFiles;
std::map<SharedImageLocator, std::weak_ptr<ScalableImageShared>> imageFiles;
std::map<EFonts, std::shared_ptr<const IFont>> fonts;
std::unique_ptr<AssetGenerator> assetGenerator;

View File

@@ -246,7 +246,7 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL
else
algorithm = EScalingAlgorithm::XBRZ_ALPHA;
auto result = std::make_shared<SDLImageShared>(this, factor, algorithm);
auto result = SDLImageShared::createScaled(this, factor, algorithm);
if (surf->format->palette)
SDL_SetSurfacePalette(surf, originalPalette);
@@ -254,28 +254,31 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL
return result;
}
SDLImageShared::SDLImageShared(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm)
std::shared_ptr<SDLImageShared> SDLImageShared::createScaled(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm)
{
auto self = std::make_shared<SDLImageShared>(nullptr);
static tbb::task_arena upscalingArena;
upscalingInProgress = true;
self->upscalingInProgress = true;
auto scaler = std::make_shared<SDLImageScaler>(from->surf, Rect(from->margins, from->fullSize), true);
const auto & scalingTask = [this, algorithm, scaler]()
const auto & scalingTask = [self, algorithm, scaler]()
{
scaler->scaleSurfaceIntegerFactor(GH.screenHandler().getScalingFactor(), algorithm);
surf = scaler->acquireResultSurface();
fullSize = scaler->getResultDimensions().dimensions();
margins = scaler->getResultDimensions().topLeft();
upscalingInProgress = false;
self->surf = scaler->acquireResultSurface();
self->fullSize = scaler->getResultDimensions().dimensions();
self->margins = scaler->getResultDimensions().topLeft();
self->upscalingInProgress = false;
};
if(settings["video"]["asyncUpscaling"].Bool())
upscalingArena.enqueue(scalingTask);
else
scalingTask();
return self;
}
bool SDLImageShared::isLoading() const

View File

@@ -49,10 +49,11 @@ public:
SDLImageShared(const ImagePath & filename);
//Create using existing surface, extraRef will increase refcount on SDL_Surface
SDLImageShared(SDL_Surface * from);
/// Creates image at specified scaling factor from source image
SDLImageShared(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm);
~SDLImageShared();
/// Creates image at specified scaling factor from source image
static std::shared_ptr<SDLImageShared> createScaled(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm);
void scaledDraw(SDL_Surface * where, SDL_Palette * palette, const Point & scaling, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const override;
void draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const override;