@@ -818,3 +818,26 @@ void Map::render() {
|
||||
triggerUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
void Map::setSourceTileCacheSize(size_t size) {
|
||||
if (size != getSourceTileCacheSize()) {
|
||||
invokeTask([=] {
|
||||
sourceCacheSize = size;
|
||||
if (!style) return;
|
||||
for (const auto &source : style->sources) {
|
||||
source->setCacheSize(sourceCacheSize);
|
||||
}
|
||||
env->performCleanup();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Map::onLowMemory() {
|
||||
invokeTask([=] {
|
||||
if (!style) return;
|
||||
for (const auto &source : style->sources) {
|
||||
source->onLowMemory();
|
||||
}
|
||||
env->performCleanup();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -244,6 +244,10 @@ TileData::State Source::addTile(Map &map, Worker &worker,
|
||||
new_tile.data.reset();
|
||||
}
|
||||
|
||||
if (!new_tile.data) {
|
||||
new_tile.data = cache.get(id.to_uint64());
|
||||
}
|
||||
|
||||
if (!new_tile.data) {
|
||||
// If we don't find working tile data, we're just going to load it.
|
||||
if (info.type == SourceType::Vector) {
|
||||
@@ -394,20 +398,33 @@ void Source::update(Map &map,
|
||||
}
|
||||
}
|
||||
|
||||
if (info.type != SourceType::Raster && cache.getSize() == 0) {
|
||||
size_t conservativeCacheSize = ((float)map.getState().getWidth() / util::tileSize) *
|
||||
((float)map.getState().getHeight() / util::tileSize) *
|
||||
(map.getMaxZoom() - map.getMinZoom() + 1) *
|
||||
0.5;
|
||||
cache.setSize(conservativeCacheSize);
|
||||
}
|
||||
|
||||
auto& tileCache = cache;
|
||||
auto& type = info.type;
|
||||
|
||||
// Remove tiles that we definitely don't need, i.e. tiles that are not on
|
||||
// the required list.
|
||||
std::set<TileID> retain_data;
|
||||
util::erase_if(tiles, [&retain, &retain_data](std::pair<const TileID, std::unique_ptr<Tile>> &pair) {
|
||||
util::erase_if(tiles, [&retain, &retain_data, &tileCache, &type](std::pair<const TileID, std::unique_ptr<Tile>> &pair) {
|
||||
Tile &tile = *pair.second;
|
||||
bool obsolete = std::find(retain.begin(), retain.end(), tile.id) == retain.end();
|
||||
if (!obsolete) {
|
||||
retain_data.insert(tile.data->id);
|
||||
} else if (type != SourceType::Raster && tile.data->ready()) {
|
||||
tileCache.add(tile.id.to_uint64(), tile.data);
|
||||
}
|
||||
return obsolete;
|
||||
});
|
||||
|
||||
// Remove all the expired pointers from the set.
|
||||
util::erase_if(tile_data, [&retain_data](std::pair<const TileID, std::weak_ptr<TileData>> &pair) {
|
||||
util::erase_if(tile_data, [&retain_data, &tileCache](std::pair<const TileID, std::weak_ptr<TileData>> &pair) {
|
||||
const util::ptr<TileData> tile = pair.second.lock();
|
||||
if (!tile) {
|
||||
return true;
|
||||
@@ -415,7 +432,9 @@ void Source::update(Map &map,
|
||||
|
||||
bool obsolete = retain_data.find(tile->id) == retain_data.end();
|
||||
if (obsolete) {
|
||||
tile->cancel();
|
||||
if (!tileCache.has(tile->id.to_uint64())) {
|
||||
tile->cancel();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -426,10 +445,19 @@ void Source::update(Map &map,
|
||||
}
|
||||
|
||||
void Source::invalidateTiles(const std::vector<TileID>& ids) {
|
||||
cache.clear();
|
||||
for (auto& id : ids) {
|
||||
tiles.erase(id);
|
||||
tile_data.erase(id);
|
||||
}
|
||||
}
|
||||
|
||||
void Source::setCacheSize(size_t size) {
|
||||
cache.setSize(size);
|
||||
}
|
||||
|
||||
void Source::onLowMemory() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <mbgl/map/tile_id.hpp>
|
||||
#include <mbgl/map/tile_data.hpp>
|
||||
#include <mbgl/map/tile_cache.hpp>
|
||||
#include <mbgl/style/types.hpp>
|
||||
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
@@ -72,6 +73,9 @@ public:
|
||||
|
||||
std::forward_list<Tile *> getLoadedTiles() const;
|
||||
|
||||
void setCacheSize(size_t);
|
||||
void onLowMemory();
|
||||
|
||||
SourceInfo info;
|
||||
bool enabled;
|
||||
|
||||
@@ -96,6 +100,7 @@ private:
|
||||
|
||||
std::map<TileID, std::unique_ptr<Tile>> tiles;
|
||||
std::map<TileID, std::weak_ptr<TileData>> tile_data;
|
||||
TileCache cache;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
60
src/mbgl/map/tile_cache.cpp
Normal file
60
src/mbgl/map/tile_cache.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <mbgl/map/tile_cache.hpp>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
void TileCache::setSize(size_t size_) {
|
||||
size = size_;
|
||||
|
||||
while (orderedKeys.size() > size) {
|
||||
auto key = orderedKeys.front();
|
||||
orderedKeys.pop_front();
|
||||
tiles.erase(key);
|
||||
}
|
||||
|
||||
assert(orderedKeys.size() <= size);
|
||||
|
||||
tiles.reserve(size);
|
||||
orderedKeys.resize(size);
|
||||
}
|
||||
|
||||
void TileCache::add(uint64_t key, std::shared_ptr<TileData> data) {
|
||||
|
||||
assert(tiles.find(key) == tiles.end());
|
||||
|
||||
tiles.emplace(key, data);
|
||||
orderedKeys.push_back(key);
|
||||
|
||||
if (orderedKeys.size() > size) {
|
||||
get(orderedKeys.front());
|
||||
}
|
||||
|
||||
assert(orderedKeys.size() <= size);
|
||||
};
|
||||
|
||||
std::shared_ptr<TileData> TileCache::get(uint64_t key) {
|
||||
|
||||
std::shared_ptr<TileData> data;
|
||||
|
||||
auto it = tiles.find(key);
|
||||
if (it != tiles.end()) {
|
||||
data = it->second;
|
||||
tiles.erase(it);
|
||||
orderedKeys.remove(key);
|
||||
assert(data->ready());
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
bool TileCache::has(uint64_t key) {
|
||||
return tiles.find(key) != tiles.end();
|
||||
}
|
||||
|
||||
void TileCache::clear() {
|
||||
orderedKeys.clear();
|
||||
tiles.clear();
|
||||
}
|
||||
|
||||
};
|
||||
30
src/mbgl/map/tile_cache.hpp
Normal file
30
src/mbgl/map/tile_cache.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef MBGL_MAP_TILE_CACHE
|
||||
#define MBGL_MAP_TILE_CACHE
|
||||
|
||||
#include <mbgl/map/tile_data.hpp>
|
||||
|
||||
#include <list>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class TileCache {
|
||||
public:
|
||||
TileCache(size_t size_ = 0) : size(size_) {}
|
||||
|
||||
void setSize(size_t);
|
||||
size_t getSize() const { return size; };
|
||||
void add(uint64_t key, std::shared_ptr<TileData> data);
|
||||
std::shared_ptr<TileData> get(uint64_t key);
|
||||
bool has(uint64_t key);
|
||||
void clear();
|
||||
private:
|
||||
std::unordered_map<uint64_t, std::shared_ptr<TileData>> tiles;
|
||||
std::list<uint64_t> orderedKeys;
|
||||
|
||||
size_t size;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user