AsyncImageLoader.hpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include "../include/Storage.hpp"
  3. #include "../include/MessageQueue.hpp"
  4. #include "../include/FileAnalyzer.hpp"
  5. #include "../include/AsyncTask.hpp"
  6. #include <mglpp/system/vec.hpp>
  7. #include <mglpp/graphics/Texture.hpp>
  8. #include <mglpp/graphics/Image.hpp>
  9. #include <mglpp/system/Clock.hpp>
  10. #include <string>
  11. #include <memory>
  12. #include <unordered_map>
  13. namespace QuickMedia {
  14. enum class LoadingState {
  15. NOT_LOADED,
  16. LOADING,
  17. READY_TO_LOAD,
  18. FINISHED_LOADING,
  19. FAILED_TO_LOAD,
  20. APPLIED_TO_TEXTURE
  21. };
  22. struct ThumbnailData {
  23. LoadingState loading_state = LoadingState::NOT_LOADED;
  24. mgl::Texture texture;
  25. std::unique_ptr<mgl::Image> image; // Set in another thread. This should be .reset after loading it into |texture|, to save memory
  26. uint32_t counter = 0;
  27. Path thumbnail_path;
  28. };
  29. struct ThumbnailLoadData {
  30. Path path;
  31. Path thumbnail_path;
  32. bool local;
  33. std::shared_ptr<ThumbnailData> thumbnail_data;
  34. mgl::vec2i resize_target_size;
  35. };
  36. bool ffmpeg_convert_image_format(const Path &thumbnail_path, const Path &destination_path);
  37. // If |symlink_if_no_resize| is false then a copy is made from |thumbnail_path| to |thumbnail_path_resized| instead of a symlink if |thumbnail_path| is not larger than |resize_target_size|.
  38. // One example of why you might not want a symlink is if |thumbnail_path| is a temporary file.
  39. bool create_thumbnail(const Path &thumbnail_path, const Path &thumbnail_path_resized, mgl::vec2i resize_target_size, ContentType content_type);
  40. constexpr int NUM_IMAGE_DOWNLOAD_PARALLEL = 4;
  41. constexpr int NUM_IMAGE_LOAD_PARALLEL = 4;
  42. class AsyncImageLoader {
  43. public:
  44. static AsyncImageLoader& get_instance();
  45. // Never returns nullptr. Instead check the |loading_state| of the thumbnail data to see if it has finished loading.
  46. // This function should be called every frame for the objects that need to display this thumbnail, otherwise it can be unloaded.
  47. // set |resize_target_size| to {0, 0} to disable resizing.
  48. // Note: this method is not thread-safe
  49. std::shared_ptr<ThumbnailData> get_thumbnail(const std::string &url, bool local, mgl::vec2i resize_target_size);
  50. // Note: this should only be called once every frame.
  51. // Note: this method is not thread-safe
  52. void update();
  53. private:
  54. struct Download {
  55. ReadProgram read_program;
  56. int64_t download_start = 0;
  57. Path thumbnail_path;
  58. std::shared_ptr<ThumbnailData> thumbnail_data;
  59. mgl::vec2i resize_target_size;
  60. std::string url;
  61. };
  62. AsyncImageLoader();
  63. ~AsyncImageLoader();
  64. AsyncImageLoader(AsyncImageLoader &other) = delete;
  65. AsyncImageLoader& operator=(AsyncImageLoader &other) = delete;
  66. // set |resize_target_size| to {0, 0} to disable resizing.
  67. // Note: this method is not thread-safe
  68. bool load_thumbnail(const std::string &url, bool local, mgl::vec2i resize_target_size, std::shared_ptr<ThumbnailData> thumbnail_data, Path &thumbnail_path);
  69. // Returns -1 if all threads are busy
  70. int get_free_load_index() const;
  71. void load_create_thumbnail(const Path &thumbnail_path, const Path &thumbnail_path_resized, ThumbnailData *thumbnail_data, mgl::vec2i resize_target_size);
  72. void process_thumbnail(ThumbnailLoadData &thumbnail_load_data);
  73. private:
  74. void reset_download(Download &download);
  75. private:
  76. std::mutex download_mutex;
  77. // TODO: Use curl single-threaded multi-download feature instead
  78. Download downloads[NUM_IMAGE_DOWNLOAD_PARALLEL];
  79. AsyncTask<void> load_threads[NUM_IMAGE_LOAD_PARALLEL];
  80. MessageQueue<ThumbnailLoadData> image_thumbnail_create_queue;
  81. std::unordered_map<std::string, std::shared_ptr<ThumbnailData>> thumbnails;
  82. uint32_t counter = 0;
  83. };
  84. }