diff options
Diffstat (limited to 'src/AsyncImageLoader.cpp')
-rw-r--r-- | src/AsyncImageLoader.cpp | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index 98c7fee..d3aa287 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -28,8 +28,8 @@ namespace QuickMedia { int scaled_y_start = ((float)y / (float)destination_size.y) * source_size.y; int scaled_x_end = ((float)(x + 1) / (float)destination_size.x) * source_size.x; int scaled_y_end = ((float)(y + 1) / (float)destination_size.y) * source_size.y; - if(scaled_x_end > (int)source_size.x) scaled_x_end = source_size.x; - if(scaled_y_end > (int)source_size.y) scaled_y_end = source_size.y; + if(scaled_x_end > (int)source_size.x - 1) scaled_x_end = source_size.x - 1; + if(scaled_y_end > (int)source_size.y - 1) scaled_y_end = source_size.y - 1; //float scaled_x = x * width_ratio; //float scaled_y = y * height_ratio; @@ -99,16 +99,13 @@ namespace QuickMedia { } load_image_thread = std::thread([this]{ - ThumbnailLoadData thumbnail_load_data; + std::optional<ThumbnailLoadData> thumbnail_load_data_opt; while(true) { - { - std::unique_lock<std::mutex> lock(load_image_mutex); - while(images_to_load.empty() && running) load_image_cv.wait(lock); - if(!running) - break; - thumbnail_load_data = images_to_load.front(); - images_to_load.pop_front(); - } + thumbnail_load_data_opt = image_load_queue.pop_wait(); + if(!thumbnail_load_data_opt) + break; + + ThumbnailLoadData &thumbnail_load_data = thumbnail_load_data_opt.value(); thumbnail_load_data.thumbnail_data->image = std::make_unique<sf::Image>(); if(load_image_from_file(*thumbnail_load_data.thumbnail_data->image, thumbnail_load_data.thumbnail_path.data)) { @@ -132,12 +129,9 @@ namespace QuickMedia { } AsyncImageLoader::~AsyncImageLoader() { - running = false; - { - std::unique_lock<std::mutex> lock(load_image_mutex); - load_image_cv.notify_one(); - } - load_image_thread.join(); + image_load_queue.close(); + if(load_image_thread.joinable()) + load_image_thread.join(); // TODO: Find a way to kill the threads instead. We need to do this right now because creating a new thread before the last one has died causes a crash for(size_t i = 0; i < NUM_IMAGE_LOAD_THREADS; ++i) { @@ -161,15 +155,11 @@ namespace QuickMedia { Path thumbnail_path = get_cache_dir().join("thumbnails").join(sha256.getHash()); if(get_file_type(thumbnail_path) == FileType::REGULAR) { thumbnail_data->loading_state = LoadingState::LOADING; - std::unique_lock<std::mutex> lock(load_image_mutex); - images_to_load.push_back({ url, thumbnail_path, local, thumbnail_data, resize_target_size }); - load_image_cv.notify_one(); + image_load_queue.push({ url, thumbnail_path, local, thumbnail_data, resize_target_size }); return; } else if(local && get_file_type(url) == FileType::REGULAR) { thumbnail_data->loading_state = LoadingState::LOADING; - std::unique_lock<std::mutex> lock(load_image_mutex); - images_to_load.push_back({ url, thumbnail_path, true, thumbnail_data, resize_target_size }); - load_image_cv.notify_one(); + image_load_queue.push({ url, thumbnail_path, true, thumbnail_data, resize_target_size }); return; } |