From bb80d168af2c5e36903dd61473fdbd3840846d7f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 27 Oct 2021 15:20:30 +0200 Subject: Stop download of thumbnail if the thumbnail is no longer visible on the screen --- src/AsyncImageLoader.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index f926a8d..762408c 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -236,14 +236,7 @@ namespace QuickMedia { } else { fprintf(stderr, "Thumbnail download failed for %s\n", download.url.c_str()); } - - std::lock_guard lock(download_mutex); - close(download.read_program.read_fd); - download.read_program.pid = -1; - download.read_program.read_fd = -1; - download.thumbnail_path.data.clear(); - download.url.c_str(); - download.thumbnail_data = nullptr; + reset_download(download); } } @@ -336,6 +329,19 @@ namespace QuickMedia { bool loaded_textures_changed = false; for(auto it = thumbnails.begin(); it != thumbnails.end();) { if(it->second->counter != counter) { + { + for(int i = 0; i < NUM_IMAGE_LOAD_PARALLEL; ++i) { + Download &download = downloads[i]; + if(download.read_program.pid == -1) + continue; + + if(download.url == it->first) { + reset_download(download); + break; + } + } + } + image_load_queue.erase_if([&it](ThumbnailLoadData &load_data) { return load_data.path.data == it->first; }); @@ -358,4 +364,19 @@ namespace QuickMedia { } return -1; } + + void AsyncImageLoader::reset_download(Download &download) { + std::lock_guard lock(download_mutex); + if(download.read_program.pid != -1) { + kill(download.read_program.pid, SIGTERM); + download.read_program.pid = -1; + } + if(download.read_program.read_fd != -1) { + close(download.read_program.read_fd); + download.read_program.read_fd = -1; + } + download.thumbnail_path.data.clear(); + download.url.c_str(); + download.thumbnail_data = nullptr; + } } -- cgit v1.2.3