Hold a reference to current images in the placefile manager, removing old, unused images

This commit is contained in:
Dan Paulat 2023-09-17 00:28:54 -05:00
parent 9165d66a33
commit bbaae5d1ba
5 changed files with 68 additions and 63 deletions

View file

@ -51,7 +51,8 @@ public:
void ReadPlacefileSettings();
void WritePlacefileSettings();
static void LoadResources(const std::shared_ptr<gr::Placefile>& placefile);
static std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>
LoadResources(const std::shared_ptr<gr::Placefile>& placefile);
boost::asio::thread_pool threadPool_ {1u};
@ -134,6 +135,8 @@ public:
std::mutex refreshMutex_ {};
std::mutex timerMutex_ {};
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>> images_ {};
std::string lastRadarSite_ {};
std::chrono::system_clock::time_point lastUpdateTime_ {};
};
@ -276,6 +279,7 @@ void PlacefileManager::set_placefile_url(const std::string& name,
auto placefileRecord = it->second;
placefileRecord->name_ = normalizedUrl;
placefileRecord->placefile_ = nullptr;
placefileRecord->images_.clear();
p->placefileRecordMap_.erase(it);
p->placefileRecordMap_.insert_or_assign(normalizedUrl, placefileRecord);
@ -633,7 +637,7 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
if (updatedPlacefile != nullptr)
{
// Load placefile resources
Impl::LoadResources(updatedPlacefile);
auto newImages = Impl::LoadResources(updatedPlacefile);
// Check the name matches, in case the name updated
if (name_ == name)
@ -643,6 +647,10 @@ void PlacefileManager::Impl::PlacefileRecord::Update()
title_ = placefile_->title();
lastUpdateTime_ = std::chrono::system_clock::now();
// Update image resources
images_.swap(newImages);
newImages.clear();
if (p->radarSite_ != nullptr)
{
lastRadarSite_ = p->radarSite_->id();
@ -736,7 +744,8 @@ std::shared_ptr<PlacefileManager> PlacefileManager::Instance()
return placefileManager;
}
void PlacefileManager::Impl::LoadResources(
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>
PlacefileManager::Impl::LoadResources(
const std::shared_ptr<gr::Placefile>& placefile)
{
const auto iconFiles = placefile->icon_files();
@ -790,7 +799,7 @@ void PlacefileManager::Impl::LoadResources(
}
}
ResourceManager::LoadImageResources(urlStrings);
return ResourceManager::LoadImageResources(urlStrings);
}
} // namespace manager

View file

@ -64,36 +64,40 @@ std::shared_ptr<util::Font> Font(types::Font font)
return nullptr;
}
bool LoadImageResource(const std::string& urlString)
std::shared_ptr<boost::gil::rgba8_image_t>
LoadImageResource(const std::string& urlString)
{
util::TextureAtlas& textureAtlas = util::TextureAtlas::Instance();
return textureAtlas.CacheTexture(urlString, urlString);
}
void LoadImageResources(const std::vector<std::string>& urlStrings)
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>
LoadImageResources(const std::vector<std::string>& urlStrings)
{
std::mutex m {};
bool textureCached = false;
std::mutex m {};
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>> images {};
std::for_each(std::execution::par_unseq,
urlStrings.begin(),
urlStrings.end(),
[&](auto& urlString)
{
bool value = LoadImageResource(urlString);
auto image = LoadImageResource(urlString);
if (value)
if (image != nullptr)
{
std::unique_lock lock {m};
textureCached = true;
images.emplace_back(std::move(image));
}
});
if (textureCached)
if (!images.empty())
{
util::TextureAtlas& textureAtlas = util::TextureAtlas::Instance();
textureAtlas.BuildAtlas(2048, 2048);
}
return images;
}
static void LoadFonts()

View file

@ -3,6 +3,10 @@
#include <scwx/qt/types/font_types.hpp>
#include <scwx/qt/util/font.hpp>
#include <vector>
#include <boost/gil/typedefs.hpp>
namespace scwx
{
namespace qt
@ -18,8 +22,10 @@ void Shutdown();
int FontId(types::Font font);
std::shared_ptr<util::Font> Font(types::Font font);
bool LoadImageResource(const std::string& urlString);
void LoadImageResources(const std::vector<std::string>& urlStrings);
std::shared_ptr<boost::gil::rgba8_image_t>
LoadImageResource(const std::string& urlString);
std::vector<std::shared_ptr<boost::gil::rgba8_image_t>>
LoadImageResources(const std::vector<std::string>& urlStrings);
} // namespace ResourceManager
} // namespace manager