From 97e9fcb00af17d0fd1220ce2a5b4f264bf83e8cf Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 24 Nov 2020 15:12:49 +0100 Subject: Make async image loader static to reduce number of process threads when using multiple instances --- include/AsyncImageLoader.hpp | 8 ++++++-- include/Body.hpp | 1 - src/AsyncImageLoader.cpp | 7 +++++++ src/Body.cpp | 3 ++- src/QuickMedia.cpp | 3 +-- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp index c1c2e11..5de9215 100644 --- a/include/AsyncImageLoader.hpp +++ b/include/AsyncImageLoader.hpp @@ -29,14 +29,18 @@ namespace QuickMedia { class AsyncImageLoader { public: - AsyncImageLoader(); - ~AsyncImageLoader(); + static AsyncImageLoader& get_instance(); // Returns false if the image loader is already loading an image. In that case, this function should be called again later. // set |resize_target_size| to {0, 0} to disable resizing. // |thumbnail_data.loading_state| has to be LoadingState::NOT_LOADED when calling this! // Note: this method is not thread-safe void load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, bool use_tor, std::shared_ptr thumbnail_data); private: + AsyncImageLoader(); + ~AsyncImageLoader(); + AsyncImageLoader(AsyncImageLoader &other) = delete; + AsyncImageLoader& operator=(AsyncImageLoader &other) = delete; + struct ThumbnailLoadData { Path path; Path thumbnail_path; diff --git a/include/Body.hpp b/include/Body.hpp index 56ef262..4bfd36f 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -258,7 +258,6 @@ namespace QuickMedia { private: Program *program; std::unordered_map> item_thumbnail_textures; - AsyncImageLoader async_image_loader; int selected_item; int prev_selected_item; float page_scroll; diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index 136bd5b..d0a67c4 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -94,6 +94,13 @@ namespace QuickMedia { } } + AsyncImageLoader& AsyncImageLoader::get_instance() { + static AsyncImageLoader *instance = nullptr; + if(!instance) + instance = new AsyncImageLoader(); + return *instance; + } + AsyncImageLoader::AsyncImageLoader() { for(int i = 0; i < NUM_IMAGE_LOAD_THREADS; ++i) { loading_image[i] = false; diff --git a/src/Body.cpp b/src/Body.cpp index 04330b8..0143fe2 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -2,6 +2,7 @@ #include "../include/QuickMedia.hpp" #include "../include/Scale.hpp" #include "../include/ResourceLoader.hpp" +#include "../include/AsyncImageLoader.hpp" #include "../plugins/Plugin.hpp" #include #include @@ -899,7 +900,7 @@ namespace QuickMedia { item_thumbnail->referenced = true; if(!item->thumbnail_url.empty() && item_thumbnail->loading_state == LoadingState::NOT_LOADED) - async_image_loader.load_thumbnail(item->thumbnail_url, item->thumbnail_is_local, content_size, program->is_tor_enabled(), item_thumbnail); + AsyncImageLoader::get_instance().load_thumbnail(item->thumbnail_url, item->thumbnail_is_local, content_size, program->is_tor_enabled(), item_thumbnail); if(item_thumbnail->loading_state == LoadingState::FINISHED_LOADING && item_thumbnail->image->getSize().x > 0 && item_thumbnail->image->getSize().y > 0) { if(!item_thumbnail->texture.loadFromImage(*item_thumbnail->image)) diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index f93dde9..866d27d 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3171,7 +3171,6 @@ namespace QuickMedia { sf::Sprite room_avatar_sprite; auto room_avatar_thumbnail_data = std::make_shared(); - AsyncImageLoader async_image_loader; sf::Clock read_marker_timer; const sf::Int32 read_marker_timeout_ms_default = 3000; @@ -4351,7 +4350,7 @@ namespace QuickMedia { } if(current_room && current_room->userdata && room_avatar_thumbnail_data->loading_state == LoadingState::NOT_LOADED) - async_image_loader.load_thumbnail(static_cast(current_room->userdata)->thumbnail_url, false, sf::Vector2i(32, 32), use_tor, room_avatar_thumbnail_data); + AsyncImageLoader::get_instance().load_thumbnail(static_cast(current_room->userdata)->thumbnail_url, false, sf::Vector2i(32, 32), use_tor, room_avatar_thumbnail_data); if(room_avatar_thumbnail_data->loading_state == LoadingState::FINISHED_LOADING && room_avatar_thumbnail_data->image->getSize().x > 0 && room_avatar_thumbnail_data->image->getSize().y > 0) { if(!room_avatar_thumbnail_data->texture.loadFromImage(*room_avatar_thumbnail_data->image)) -- cgit v1.2.3