Browse Source

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

Ivan Savenko 7 months ago
parent
commit
8a0fed7b3a

+ 10 - 5
client/renderSDL/RenderHandler.cpp

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

+ 2 - 2
client/renderSDL/RenderHandler.h

@@ -24,9 +24,9 @@ class RenderHandler final : public IRenderHandler
 {
 {
 	using AnimationLayoutMap = std::map<size_t, std::vector<ImageLocator>>;
 	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<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::map<EFonts, std::shared_ptr<const IFont>> fonts;
 	std::unique_ptr<AssetGenerator> assetGenerator;
 	std::unique_ptr<AssetGenerator> assetGenerator;
 
 

+ 12 - 9
client/renderSDL/SDLImage.cpp

@@ -246,7 +246,7 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL
 	else
 	else
 		algorithm = EScalingAlgorithm::XBRZ_ALPHA;
 		algorithm = EScalingAlgorithm::XBRZ_ALPHA;
 
 
-	auto result = std::make_shared<SDLImageShared>(this, factor, algorithm);
+	auto result = SDLImageShared::createScaled(this, factor, algorithm);
 
 
 	if (surf->format->palette)
 	if (surf->format->palette)
 		SDL_SetSurfacePalette(surf, originalPalette);
 		SDL_SetSurfacePalette(surf, originalPalette);
@@ -254,28 +254,31 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL
 	return result;
 	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;
 	static tbb::task_arena upscalingArena;
 
 
-	upscalingInProgress = true;
+	self->upscalingInProgress = true;
 
 
 	auto scaler = std::make_shared<SDLImageScaler>(from->surf, Rect(from->margins, from->fullSize), 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);
 		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())
 	if(settings["video"]["asyncUpscaling"].Bool())
 		upscalingArena.enqueue(scalingTask);
 		upscalingArena.enqueue(scalingTask);
 	else
 	else
 		scalingTask();
 		scalingTask();
+
+	return self;
 }
 }
 
 
 bool SDLImageShared::isLoading() const
 bool SDLImageShared::isLoading() const

+ 3 - 2
client/renderSDL/SDLImage.h

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