Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Prefetch low resolution tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpsantos committed Jan 17, 2017
1 parent 7983e3a commit f2754f9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
4 changes: 4 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ class Map : private util::noncopyable {
std::vector<Feature> queryRenderedFeatures(const ScreenBox&, const optional<std::vector<std::string>>& layerIDs = {});
AnnotationIDs queryPointAnnotations(const ScreenBox&);

// Tile prefatching
void setFixedPrefetchZoom(uint32_t);
void setDynamicPrefetchZoom(uint32_t);

// Memory
void setSourceTileCacheSize(size_t);
void onLowMemory();
Expand Down
15 changes: 14 additions & 1 deletion src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class Map::Impl : public style::Observer {

std::unique_ptr<AsyncRequest> styleRequest;

uint32_t fixedPrefetchZoom = 0;
uint32_t dynamicPrefetchZoom = 0;

size_t sourceCacheSize;
bool loading = false;

Expand Down Expand Up @@ -251,7 +254,9 @@ void Map::Impl::render(View& view) {
fileSource,
mode,
*annotationManager,
*style);
*style,
fixedPrefetchZoom,
dynamicPrefetchZoom);

style->updateTiles(parameters);

Expand Down Expand Up @@ -1064,6 +1069,14 @@ void Map::setSourceTileCacheSize(size_t size) {
}
}

void Map::setFixedPrefetchZoom(uint32_t zoom) {
impl->fixedPrefetchZoom = zoom;
}

void Map::setDynamicPrefetchZoom(uint32_t zoom) {
impl->dynamicPrefetchZoom = zoom;
}

void Map::onLowMemory() {
if (impl->painter) {
BackendScope guard(impl->backend);
Expand Down
37 changes: 30 additions & 7 deletions src/mbgl/style/source_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,34 @@ void Source::Impl::updateTiles(const UpdateParameters& parameters) {
const Range<uint8_t> zoomRange = getZoomRange();

// Determine the overzooming/underzooming amounts and required tiles.
int32_t overscaledZoom = util::coveringZoomLevel(parameters.transformState.getZoom(), type, tileSize);
int32_t tileZoom = overscaledZoom;
uint32_t overscaledZoom = util::coveringZoomLevel(parameters.transformState.getZoom(), type, tileSize);
uint32_t tileZoom = overscaledZoom;
uint32_t lowResolutionZoom = 0;

std::vector<UnwrappedTileID> idealTiles;
std::vector<UnwrappedTileID> lowResolutionTiles;

if (overscaledZoom >= zoomRange.min) {
int32_t idealZoom = std::min<int32_t>(zoomRange.max, overscaledZoom);
uint32_t idealZoom = std::min<int32_t>(zoomRange.max, overscaledZoom);

// Make sure we're not reparsing overzoomed raster tiles.
if (type == SourceType::Raster) {
tileZoom = idealZoom;
}

idealTiles = util::tileCover(parameters.transformState, idealZoom);

// Request lower zoom level tiles (if configure to do so) in an attempt
// to show something on the screen faster at the cost of a little of bandwidth.
lowResolutionZoom = parameters.fixedPrefetchZoom ?
parameters.fixedPrefetchZoom :
idealZoom - parameters.dynamicPrefetchZoom;

lowResolutionZoom = lowResolutionZoom < zoomRange.min ? zoomRange.min : lowResolutionZoom;

if (lowResolutionZoom < idealZoom) {
lowResolutionTiles = util::tileCover(parameters.transformState, lowResolutionZoom);
}
}

// Stores a list of all the tiles that we're definitely going to retain. There are two
Expand All @@ -111,8 +126,10 @@ void Source::Impl::updateTiles(const UpdateParameters& parameters) {
std::set<OverscaledTileID> retain;

auto retainTileFn = [&retain](Tile& tile, Resource::Necessity necessity) -> void {
retain.emplace(tile.id);
tile.setNecessity(necessity);
if (retain.find(tile.id) == retain.end()) {
retain.emplace(tile.id);
tile.setNecessity(necessity);
}
};
auto getTileFn = [this](const OverscaledTileID& tileID) -> Tile* {
auto it = tiles.find(tileID);
Expand All @@ -136,8 +153,14 @@ void Source::Impl::updateTiles(const UpdateParameters& parameters) {
};

renderTiles.clear();
algorithm::updateRenderables(getTileFn, createTileFn, retainTileFn, renderTileFn,
idealTiles, zoomRange, tileZoom);

if (!lowResolutionTiles.empty()) {
algorithm::updateRenderables(getTileFn, createTileFn, retainTileFn,
[](const UnwrappedTileID&, Tile&) {}, lowResolutionTiles, zoomRange, lowResolutionZoom);
}

algorithm::updateRenderables(getTileFn, createTileFn, retainTileFn,
renderTileFn, idealTiles, zoomRange, tileZoom);

if (type != SourceType::Annotations && cache.getSize() == 0) {
size_t conservativeCacheSize =
Expand Down
11 changes: 9 additions & 2 deletions src/mbgl/style/update_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ class UpdateParameters {
FileSource& fileSource_,
const MapMode mode_,
AnnotationManager& annotationManager_,
Style& style_)
Style& style_,
uint32_t fixedPrefetchZoom_,
uint32_t dynamicPrefetchZoom_)
: pixelRatio(pixelRatio_),
debugOptions(debugOptions_),
transformState(transformState_),
workerScheduler(workerScheduler_),
fileSource(fileSource_),
mode(mode_),
annotationManager(annotationManager_),
style(style_) {}
style(style_),
fixedPrefetchZoom(fixedPrefetchZoom_),
dynamicPrefetchZoom(dynamicPrefetchZoom_) {}

float pixelRatio;
MapDebugOptions debugOptions;
Expand All @@ -42,6 +46,9 @@ class UpdateParameters {

// TODO: remove
Style& style;

const uint32_t fixedPrefetchZoom;
const uint32_t dynamicPrefetchZoom;
};

} // namespace style
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/util/tile_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ std::vector<UnwrappedTileID> tileCover(const Point<double>& tl,

} // namespace

int32_t coveringZoomLevel(double zoom, SourceType type, uint16_t size) {
zoom += std::log(util::tileSize / size) / std::log(2);
uint32_t coveringZoomLevel(double zoom, SourceType type, uint16_t size) {
zoom = std::max(0., zoom) + std::log(util::tileSize / size) / std::log(2);
if (type == SourceType::Raster || type == SourceType::Video) {
return ::round(zoom);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/util/tile_cover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LatLngBounds;

namespace util {

int32_t coveringZoomLevel(double z, SourceType type, uint16_t tileSize);
uint32_t coveringZoomLevel(double z, SourceType type, uint16_t tileSize);

std::vector<UnwrappedTileID> tileCover(const TransformState&, int32_t z);
std::vector<UnwrappedTileID> tileCover(const LatLngBounds&, int32_t z);
Expand Down

0 comments on commit f2754f9

Please sign in to comment.