|
| 1 | +#include "AsyncFetcher.hpp" |
| 2 | + |
| 3 | +#include <QMutexLocker> |
| 4 | +#include <QPoint> |
| 5 | + |
| 6 | +#include <thread> |
| 7 | +#include <chrono> |
| 8 | + |
| 9 | +using namespace aliceVision; |
| 10 | + |
| 11 | +namespace qtAliceVision { |
| 12 | +namespace imgserve { |
| 13 | + |
| 14 | +AsyncFetcher::AsyncFetcher() |
| 15 | +{ |
| 16 | + _resizeRatio = 0.001; |
| 17 | + _isAsynchronous = false; |
| 18 | + _requestSynchronous = false; |
| 19 | +} |
| 20 | + |
| 21 | +AsyncFetcher::~AsyncFetcher() {} |
| 22 | + |
| 23 | +void AsyncFetcher::setSequence(const std::vector<std::string>& paths) |
| 24 | +{ |
| 25 | + // Sequence can't be changed while thread is running |
| 26 | + if (_isAsynchronous) |
| 27 | + { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + _sequence = paths; |
| 32 | + _currentIndex = 0; |
| 33 | + |
| 34 | + for (unsigned idx = 0; idx < _sequence.size(); idx++) |
| 35 | + { |
| 36 | + _pathToSeqId[_sequence[idx]] = idx; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void AsyncFetcher::setResizeRatio(double ratio) |
| 41 | +{ |
| 42 | + QMutexLocker locker(&_mutexResizeRatio); |
| 43 | + _resizeRatio = ratio; |
| 44 | +} |
| 45 | + |
| 46 | +void AsyncFetcher::setCache(ImageCache::uptr&& cache) |
| 47 | +{ |
| 48 | + // Cache can't be changed while thread is running |
| 49 | + if (_isAsynchronous) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + _cache = std::move(cache); |
| 54 | +} |
| 55 | + |
| 56 | +void AsyncFetcher::run() |
| 57 | +{ |
| 58 | + using namespace std::chrono_literals; |
| 59 | + |
| 60 | + _isAsynchronous = true; |
| 61 | + _requestSynchronous = false; |
| 62 | + |
| 63 | + std::size_t previousCacheSize = getDiskLoads(); |
| 64 | + |
| 65 | + while (1) |
| 66 | + { |
| 67 | + if (_requestSynchronous) |
| 68 | + { |
| 69 | + _requestSynchronous = false; |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + if (_sequence.size() == 0) |
| 74 | + { |
| 75 | + std::this_thread::sleep_for(100ms); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + const std::string& lpath = _sequence[static_cast<std::size_t>(_currentIndex)]; |
| 80 | + |
| 81 | + // Load in cache |
| 82 | + if (_cache) |
| 83 | + { |
| 84 | + double ratio; |
| 85 | + { |
| 86 | + QMutexLocker locker(&_mutexResizeRatio); |
| 87 | + ratio = _resizeRatio; |
| 88 | + } |
| 89 | + |
| 90 | + _cache->get<image::RGBAfColor>(lpath, static_cast<unsigned int>(_currentIndex), ratio, false); |
| 91 | + } |
| 92 | + |
| 93 | + _currentIndex++; |
| 94 | + |
| 95 | + int size = static_cast<int>(_sequence.size()); |
| 96 | + if (_currentIndex >= size) |
| 97 | + { |
| 98 | + _currentIndex = 0; |
| 99 | + } |
| 100 | + |
| 101 | + std::this_thread::sleep_for(1ms); |
| 102 | + } |
| 103 | + |
| 104 | + std::size_t cacheSize = getDiskLoads(); |
| 105 | + if (cacheSize != previousCacheSize) |
| 106 | + { |
| 107 | + previousCacheSize = cacheSize; |
| 108 | + Q_EMIT onAsyncFetchProgressed(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + _requestSynchronous = false; |
| 113 | + _isAsynchronous = false; |
| 114 | +} |
| 115 | + |
| 116 | +void AsyncFetcher::stopAsync() |
| 117 | +{ |
| 118 | + _requestSynchronous = true; |
| 119 | +} |
| 120 | + |
| 121 | +void AsyncFetcher::updateCacheMemory(std::size_t maxMemory) |
| 122 | +{ |
| 123 | + if (_cache) |
| 124 | + { |
| 125 | + _cache->updateMaxMemory(maxMemory); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +std::size_t AsyncFetcher::getCacheSize() const |
| 130 | +{ |
| 131 | + return (_cache) ? static_cast<std::size_t>(_cache->info().getContentSize()) : 0; |
| 132 | +} |
| 133 | + |
| 134 | +std::size_t AsyncFetcher::getDiskLoads() const |
| 135 | +{ |
| 136 | + return (_cache) ? static_cast<std::size_t>(_cache->info().getLoadFromDisk()) : 0; |
| 137 | +} |
| 138 | + |
| 139 | +QVariantList AsyncFetcher::getCachedFrames() const |
| 140 | +{ |
| 141 | + QVariantList intervals; |
| 142 | + |
| 143 | + if (!_cache) |
| 144 | + { |
| 145 | + return intervals; |
| 146 | + } |
| 147 | + |
| 148 | + // Accumulator variables |
| 149 | + auto region = std::make_pair(-1, -1); |
| 150 | + bool regionOpen = false; |
| 151 | + |
| 152 | + size_t size = _sequence.size(); |
| 153 | + |
| 154 | + { |
| 155 | + // Build cached frames intervals |
| 156 | + for (std::size_t i = 0; i < size; ++i) |
| 157 | + { |
| 158 | + const int frame = static_cast<int>(i); |
| 159 | + |
| 160 | + // Check if current frame is in cache |
| 161 | + if (_cache->contains<aliceVision::image::RGBAfColor>(_sequence[i], _resizeRatio)) |
| 162 | + { |
| 163 | + // Either grow currently open region or create a new region |
| 164 | + if (regionOpen) |
| 165 | + { |
| 166 | + region.second = frame; |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + region.first = frame; |
| 171 | + region.second = frame; |
| 172 | + regionOpen = true; |
| 173 | + } |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + // Close currently open region |
| 178 | + if (regionOpen) |
| 179 | + { |
| 180 | + intervals.append(QPoint(region.first, region.second)); |
| 181 | + regionOpen = false; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // Last region may still be open |
| 188 | + if (regionOpen) |
| 189 | + { |
| 190 | + intervals.append(QPoint(region.first, region.second)); |
| 191 | + } |
| 192 | + |
| 193 | + return intervals; |
| 194 | +} |
| 195 | + |
| 196 | +bool AsyncFetcher::getFrame(const std::string& path, |
| 197 | + std::shared_ptr<image::Image<image::RGBAfColor>>& image, |
| 198 | + oiio::ParamValueList& metadatas, |
| 199 | + size_t& originalWidth, |
| 200 | + size_t& originalHeight) |
| 201 | +{ |
| 202 | + // Need a cache |
| 203 | + if (!_cache) |
| 204 | + { |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + // First try getting the image |
| 209 | + bool onlyCache = _isAsynchronous; |
| 210 | + |
| 211 | + // Upgrade the thread with the current Index |
| 212 | + for (std::size_t idx = 0; idx < _sequence.size(); ++idx) |
| 213 | + { |
| 214 | + if (_sequence[idx] == path) |
| 215 | + { |
| 216 | + _currentIndex = static_cast<int>(idx); |
| 217 | + break; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + std::optional<CacheValue> ovalue = _cache->get<aliceVision::image::RGBAfColor>(path, _currentIndex, _resizeRatio, onlyCache); |
| 222 | + |
| 223 | + if (ovalue.has_value()) |
| 224 | + { |
| 225 | + auto& value = ovalue.value(); |
| 226 | + image = value.get<aliceVision::image::RGBAfColor>(); |
| 227 | + |
| 228 | + oiio::ParamValueList copy_metadatas = value.getMetadatas(); |
| 229 | + metadatas = copy_metadatas; |
| 230 | + originalWidth = value.getOriginalWidth(); |
| 231 | + originalHeight = value.getOriginalHeight(); |
| 232 | + |
| 233 | + if (image) |
| 234 | + { |
| 235 | + _cache->setReferenceFrameId(_currentIndex); |
| 236 | + } |
| 237 | + |
| 238 | + return true; |
| 239 | + } |
| 240 | + |
| 241 | + return false; |
| 242 | +} |
| 243 | + |
| 244 | +} // namespace imgserve |
| 245 | +} // namespace qtAliceVision |
| 246 | + |
| 247 | +#include "AsyncFetcher.moc" |
0 commit comments