From 9dfef8c22987c12a6aad47a8913e60943d8578e7 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 14 May 2021 16:40:49 +0200 Subject: Move thumbnail loading/unloading to AsyncImageLoader --- include/AsyncImageLoader.hpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'include/AsyncImageLoader.hpp') diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp index 0384a71..7fe7c07 100644 --- a/include/AsyncImageLoader.hpp +++ b/include/AsyncImageLoader.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace QuickMedia { enum class LoadingState { @@ -19,11 +20,10 @@ namespace QuickMedia { }; struct ThumbnailData { - bool referenced = false; LoadingState loading_state = LoadingState::NOT_LOADED; sf::Texture texture; std::unique_ptr image; // Set in another thread. This should be .reset after loading it into |texture|, to save memory - sf::Clock texture_applied_time; + size_t counter = 0; }; // This function is async @@ -34,10 +34,16 @@ namespace QuickMedia { class AsyncImageLoader { public: 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. + + // Never returns nullptr. Instead check the |loading_state| of the thumbnail data to see if it has finished loading. + // This function should be called every frame for the objects that need to display this thumbnail, otherwise it can be unloaded. // 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); + std::shared_ptr get_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size); + + // Note: this should only be called once every frame. + // Note: this method is not thread-safe + void update(); private: AsyncImageLoader(); ~AsyncImageLoader(); @@ -52,6 +58,10 @@ namespace QuickMedia { sf::Vector2i resize_target_size; }; + // 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); + // Returns -1 if all threads are busy int get_free_load_index() const; private: @@ -60,5 +70,7 @@ namespace QuickMedia { std::thread download_image_thread[NUM_IMAGE_LOAD_THREADS]; std::thread load_image_thread; MessageQueue image_load_queue; + std::unordered_map> thumbnails; + size_t counter = 0; }; -} \ No newline at end of file +} -- cgit v1.2.3