// FIXME: to be further simplified when we centralize tile messages. int TileCache::isTileBeingRenderedIfSoSubscribe(const TileDesc& tile, const std::shared_ptr<ClientSession> &subscriber) { std::unique_lock<std::mutex> lock(_tilesBeingRenderedMutex); std::shared_ptr<TileBeingRendered> tileBeingRendered = findTileBeingRendered(tile); if (tileBeingRendered) { Log::debug() << "Tile (" << tile.getPart() << ',' << tile.getTilePosX() << ',' << tile.getTilePosY() << ") is already being rendered, subscribing." << Log::end; assert(subscriber->getKind() == LOOLSession::Kind::ToClient); for (const auto &s : tileBeingRendered->_subscribers) { if (s.lock().get() == subscriber.get()) { Log::debug("Redundant request to re-subscribe on a tile"); return 0; } } tileBeingRendered->_subscribers.push_back(subscriber); const auto duration = (std::chrono::steady_clock::now() - tileBeingRendered->getStartTime()); if (std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() > COMMAND_TIMEOUT_MS) { // Tile painting has stalled. Reissue. return tileBeingRendered->getVersion(); } return 0; } else { Log::debug() << "Tile (" << tile.getPart() << ',' << tile.getTilePosX() << ',' << tile.getTilePosY() << ") needs rendering, subscribing for ver: " << tile.getVersion() << "." << Log::end; const std::string cachedName = cacheFileName(tile); assert(_tilesBeingRendered.find(cachedName) == _tilesBeingRendered.end()); tileBeingRendered = std::make_shared<TileBeingRendered>(cachedName, tile.getVersion()); tileBeingRendered->_subscribers.push_back(subscriber); _tilesBeingRendered[cachedName] = tileBeingRendered; return tileBeingRendered->getVersion(); } }
std::string TileCache::cacheFileName(const TileDesc& tile) { std::ostringstream oss; oss << tile.getPart() << '_' << tile.getWidth() << 'x' << tile.getHeight() << '.' << tile.getTilePosX() << ',' << tile.getTilePosY() << '.' << tile.getTileWidth() << 'x' << tile.getTileHeight() << ".png"; return oss.str(); }
std::unique_ptr<std::fstream> TileCache::lookupTile(const TileDesc& tile) { const std::string fileName = _cacheDir + "/" + cacheFileName(tile); std::unique_ptr<std::fstream> result(new std::fstream(fileName, std::ios::in)); UnitWSD::get().lookupTile(tile.getPart(), tile.getWidth(), tile.getHeight(), tile.getTilePosX(), tile.getTilePosY(), tile.getTileWidth(), tile.getTileHeight(), result); if (result && result->is_open()) { Log::trace("Found cache tile: " + fileName); return result; } return nullptr; }