aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-05-14 16:40:49 +0200
committerdec05eba <dec05eba@protonmail.com>2021-05-14 17:00:06 +0200
commit9dfef8c22987c12a6aad47a8913e60943d8578e7 (patch)
tree5d167931ec4496d9509c03679516803b5c570315 /include
parentacb6ac0a04e800a79876908fd1fdb98dc7e93678 (diff)
Move thumbnail loading/unloading to AsyncImageLoader
Diffstat (limited to 'include')
-rw-r--r--include/AsyncImageLoader.hpp22
-rw-r--r--include/Body.hpp3
2 files changed, 17 insertions, 8 deletions
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 <string>
#include <memory>
#include <thread>
+#include <unordered_map>
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<sf::Image> 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<ThumbnailData> thumbnail_data);
+ std::shared_ptr<ThumbnailData> 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<ThumbnailData> 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<ThumbnailLoadData> image_load_queue;
+ std::unordered_map<std::string, std::shared_ptr<ThumbnailData>> thumbnails;
+ size_t counter = 0;
};
-} \ No newline at end of file
+}
diff --git a/include/Body.hpp b/include/Body.hpp
index 48eb45f..b5ca210 100644
--- a/include/Body.hpp
+++ b/include/Body.hpp
@@ -206,7 +206,6 @@ namespace QuickMedia {
void insert_items_by_timestamps(BodyItems new_items);
void clear_cache();
void clear_text_cache();
- void clear_thumbnails();
BodyItem* get_selected() const;
std::shared_ptr<BodyItem> get_selected_shared();
@@ -278,7 +277,6 @@ namespace QuickMedia {
float get_offset_to_last_visible_item(sf::Vector2f body_size);
private:
Program *program;
- std::unordered_map<std::string, std::shared_ptr<ThumbnailData>> item_thumbnail_textures;
int selected_item;
int prev_selected_item;
double page_scroll;
@@ -316,7 +314,6 @@ namespace QuickMedia {
sf::Vector2f body_size;
float selected_item_height = 0.0f;
float selected_scrolled = 0.0f;
- bool loaded_textures_changed = false;
std::shared_ptr<BodyItem> clicked_body_item = nullptr;
RoundedRectangle item_background;
RoundedRectangle reaction_background;