From fc49d40c0d2f6edbbe9dde1f1b53d6a17e9d9f7d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 17 Oct 2021 05:53:22 +0200 Subject: Limit image loading to one thread in async image loader --- include/AsyncImageLoader.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'include/AsyncImageLoader.hpp') diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp index 94b225d..7556fbc 100644 --- a/include/AsyncImageLoader.hpp +++ b/include/AsyncImageLoader.hpp @@ -15,6 +15,7 @@ namespace QuickMedia { enum class LoadingState { NOT_LOADED, LOADING, + READY_TO_LOAD, FINISHED_LOADING, APPLIED_TO_TEXTURE }; @@ -24,6 +25,7 @@ namespace QuickMedia { sf::Texture texture; std::unique_ptr image; // Set in another thread. This should be .reset after loading it into |texture|, to save memory size_t counter = 0; + Path thumbnail_path; }; struct ThumbnailLoadData { @@ -70,19 +72,25 @@ namespace QuickMedia { // set |resize_target_size| to {0, 0} to disable resizing. // Note: this method is not thread-safe - void load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, std::shared_ptr thumbnail_data); + bool load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, std::shared_ptr thumbnail_data, Path &thumbnail_path); // Returns -1 if all threads are busy int get_free_load_index() const; + void load_create_thumbnail(const Path &thumbnail_path, const Path &thumbnail_path_resized, ThumbnailData *thumbnail_data, sf::Vector2i resize_target_size); + void process_thumbnail(ThumbnailLoadData &thumbnail_load_data); + private: void reset_download(Download &download); private: std::mutex download_mutex; // TODO: Use curl single-threaded multi-download feature instead Download downloads[NUM_IMAGE_LOAD_PARALLEL]; AsyncTask load_thread; - MessageQueue image_load_queue; + MessageQueue image_thumbnail_create_queue; std::unordered_map> thumbnails; size_t counter = 0; + + std::mutex image_load_mutex; + ThumbnailLoadData current_loading_image; }; } -- cgit v1.2.3