aboutsummaryrefslogtreecommitdiff
path: root/include/AsyncImageLoader.hpp
blob: 3189565224626b8537936afa98f95cf535d6d9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include "../include/Storage.hpp"
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/Clock.hpp>
#include <string>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <deque>

namespace QuickMedia {
    enum class LoadingState {
        NOT_LOADED,
        LOADING,
        FINISHED_LOADING,
        APPLIED_TO_TEXTURE
    };

    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;
    };

    constexpr int NUM_IMAGE_LOAD_THREADS = 4;

    class AsyncImageLoader {
    public:
        AsyncImageLoader();
        ~AsyncImageLoader();
        // 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<ThumbnailData> thumbnail_data);
    private:
        struct ThumbnailLoadData {
            Path path;
            Path thumbnail_path;
            bool local;
            std::shared_ptr<ThumbnailData> thumbnail_data;
            sf::Vector2i resize_target_size;
        };

        // Returns -1 if all threads are busy
        int get_free_load_index() const;
    private:
        bool loading_image[NUM_IMAGE_LOAD_THREADS];
        // TODO: Use curl single-threaded multi-download feature instead
        std::thread download_image_thread[NUM_IMAGE_LOAD_THREADS];
        std::thread load_image_thread;
        std::mutex load_image_mutex;
        std::condition_variable load_image_cv;
        std::deque<ThumbnailLoadData> images_to_load;
        bool running = true;
    };
}