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

[core] Add runtime API for setting tile prefetch delta for a Source #16179

Merged
merged 7 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

This property allows to re-open the offline database in read-only mode and thus improves the performance for the set-ups that do not require the offline database modifications.

- [core] Add runtime API for setting tile prefetch delta for a Source ([#16179](https://github.com/mapbox/mapbox-gl-native/pull/16179))

The new `Source::setPrefetchZoomDelta(optional<uint8_t>)` method allows overriding default tile prefetch setting that is defined by the Map instance.

## maps-v1.0.1 (2020.01-release-unicorn)

### 🐞 Bug fixes
Expand Down
5 changes: 5 additions & 0 deletions include/mbgl/style/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Source : public mbgl::util::noncopyable {
SourceObserver* observer = nullptr;

virtual void loadDescription(FileSource&) = 0;
void setPrefetchZoomDelta(optional<uint8_t> delta) noexcept;
optional<uint8_t> getPrefetchZoomDelta() const noexcept;
void dumpDebugLogs() const;

virtual bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const = 0;
Expand All @@ -85,6 +87,9 @@ class Source : public mbgl::util::noncopyable {
mapbox::base::TypeWrapper peer;

virtual mapbox::base::WeakPtr<Source> makeWeakPtr() = 0;

protected:
virtual Mutable<Impl> createMutable() const noexcept = 0;
};

} // namespace style
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/style/sources/custom_geometry_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class CustomGeometrySource final : public Source {
mapbox::base::WeakPtr<Source> makeWeakPtr() override {
return weakFactory.makeWeakPtr();
}

protected:
Mutable<Source::Impl> createMutable() const noexcept final;

private:
std::shared_ptr<ThreadPool> threadPool;
std::unique_ptr<Actor<CustomTileLoader>> loader;
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/style/sources/geojson_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class GeoJSONSource final : public Source {
return weakFactory.makeWeakPtr();
}

protected:
Mutable<Source::Impl> createMutable() const noexcept final;

private:
optional<std::string> url;
std::unique_ptr<AsyncRequest> req;
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/style/sources/image_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class ImageSource final : public Source {
mapbox::base::WeakPtr<Source> makeWeakPtr() override {
return weakFactory.makeWeakPtr();
}

protected:
Mutable<Source::Impl> createMutable() const noexcept final;

private:
optional<std::string> url;
std::unique_ptr<AsyncRequest> req;
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/style/sources/raster_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class RasterSource : public Source {
return weakFactory.makeWeakPtr();
}

protected:
Mutable<Source::Impl> createMutable() const noexcept final;

private:
const variant<std::string, Tileset> urlOrTileset;
std::unique_ptr<AsyncRequest> req;
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/style/sources/vector_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class VectorSource final : public Source {
return weakFactory.makeWeakPtr();
}

protected:
Mutable<Source::Impl> createMutable() const noexcept final;

private:
const variant<std::string, Tileset> urlOrTileset;
std::unique_ptr<AsyncRequest> req;
Expand Down
8 changes: 4 additions & 4 deletions metrics/binary-size/macos-xcode11/metrics.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
[
"mbgl-glfw",
"/tmp/attach/install/macos-xcode11-release/bin/mbgl-glfw",
5562556
5616056
],
[
"mbgl-offline",
"/tmp/attach/install/macos-xcode11-release/bin/mbgl-offline",
5427032
5484604
],
[
"mbgl-render",
"/tmp/attach/install/macos-xcode11-release/bin/mbgl-render",
5477244
5530720
]
]
}
}
8 changes: 8 additions & 0 deletions src/mbgl/annotation/annotation_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ AnnotationSource::Impl::Impl()
: Source::Impl(SourceType::Annotations, AnnotationManager::SourceID) {
}

const AnnotationSource::Impl& AnnotationSource::impl() const {
return static_cast<const Impl&>(*baseImpl);
}

void AnnotationSource::loadDescription(FileSource&) {
loaded = true;
}
Expand All @@ -26,4 +30,8 @@ bool AnnotationSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info)
return !std::strcmp(info->type, "line") || !std::strcmp(info->type, "symbol") || !std::strcmp(info->type, "fill");
}

Mutable<Source::Impl> AnnotationSource::createMutable() const noexcept {
return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl()));
alexshalamov marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace mbgl
4 changes: 3 additions & 1 deletion src/mbgl/annotation/annotation_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class AnnotationSource final : public style::Source {
class Impl;
const Impl& impl() const;

protected:
Mutable<Source::Impl> createMutable() const noexcept final;

private:
void loadDescription(FileSource&) final;
bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override;
mapbox::base::WeakPtr<Source> makeWeakPtr() override {
return weakFactory.makeWeakPtr();
}
Mutable<Impl> mutableImpl() const;
mapbox::base::WeakPtrFactory<Source> weakFactory {this};
};

Expand Down
26 changes: 13 additions & 13 deletions src/mbgl/annotation/render_annotation_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ void RenderAnnotationSource::update(Immutable<style::Source::Impl> baseImpl_,

enabled = needsRendering;

tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
SourceType::Annotations,
util::tileSize,
// Zoom level 16 is typically sufficient for annotations.
// See https://github.com/mapbox/mapbox-gl-native/issues/10197
{ 0, 16 },
optional<LatLngBounds> {},
[&] (const OverscaledTileID& tileID) {
return std::make_unique<AnnotationTile>(tileID, parameters);
});
tilePyramid.update(
layers,
needsRendering,
needsRelayout,
parameters,
SourceType::Annotations,
util::tileSize,
// Zoom level 16 is typically sufficient for annotations.
// See https://github.com/mapbox/mapbox-gl-native/issues/10197
{0, 16},
optional<LatLngBounds>{},
[&](const OverscaledTileID& tileID) { return std::make_unique<AnnotationTile>(tileID, parameters); },
baseImpl->getPrefetchZoomDelta());
}

std::unordered_map<std::string, std::vector<Feature>>
Expand Down
34 changes: 22 additions & 12 deletions src/mbgl/renderer/sources/render_custom_geometry_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ void RenderCustomGeometrySource::update(Immutable<style::Source::Impl> baseImpl_
const TileParameters& parameters) {
if (baseImpl != baseImpl_) {
std::swap(baseImpl, baseImpl_);
tilePyramid.clearAll();

// Clear tile pyramid only if updated source has different tile options,
// zoom range or initialization state for a custom tile loader.
auto newImpl = staticImmutableCast<style::CustomGeometrySource::Impl>(baseImpl);
auto currentImpl = staticImmutableCast<style::CustomGeometrySource::Impl>(baseImpl_);
if (*newImpl != *currentImpl) {
tilePyramid.clearAll();
}
}

enabled = needsRendering;
Expand All @@ -33,17 +40,20 @@ void RenderCustomGeometrySource::update(Immutable<style::Source::Impl> baseImpl_
return;
}

tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
SourceType::CustomVector,
util::tileSize,
impl().getZoomRange(),
{},
[&] (const OverscaledTileID& tileID) {
return std::make_unique<CustomGeometryTile>(tileID, impl().id, parameters, impl().getTileOptions(), *tileLoader);
});
tilePyramid.update(
layers,
needsRendering,
needsRelayout,
parameters,
SourceType::CustomVector,
util::tileSize,
impl().getZoomRange(),
{},
[&](const OverscaledTileID& tileID) {
return std::make_unique<CustomGeometryTile>(
tileID, impl().id, parameters, impl().getTileOptions(), *tileLoader);
},
baseImpl->getPrefetchZoomDelta());
}

} // namespace mbgl
24 changes: 13 additions & 11 deletions src/mbgl/renderer/sources/render_geojson_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,19 @@ void RenderGeoJSONSource::update(Immutable<style::Source::Impl> baseImpl_,

if (!data_) return;

tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
SourceType::GeoJSON,
util::tileSize,
impl().getZoomRange(),
optional<LatLngBounds>{},
[&, data_](const OverscaledTileID& tileID) {
return std::make_unique<GeoJSONTile>(tileID, impl().id, parameters, data_);
});
tilePyramid.update(
layers,
needsRendering,
needsRelayout,
parameters,
SourceType::GeoJSON,
util::tileSize,
impl().getZoomRange(),
optional<LatLngBounds>{},
[&, data_](const OverscaledTileID& tileID) {
return std::make_unique<GeoJSONTile>(tileID, impl().id, parameters, data_);
},
baseImpl->getPrefetchZoomDelta());
}

mapbox::util::variant<Value, FeatureCollection>
Expand Down
22 changes: 11 additions & 11 deletions src/mbgl/renderer/sources/render_raster_dem_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ void RenderRasterDEMSource::updateInternal(const Tileset& tileset,
const bool needsRendering,
const bool needsRelayout,
const TileParameters& parameters) {
tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
SourceType::RasterDEM,
impl().getTileSize(),
tileset.zoomRange,
tileset.bounds,
[&] (const OverscaledTileID& tileID) {
return std::make_unique<RasterDEMTile>(tileID, parameters, tileset);
});
tilePyramid.update(
layers,
needsRendering,
needsRelayout,
parameters,
SourceType::RasterDEM,
impl().getTileSize(),
tileset.zoomRange,
tileset.bounds,
[&](const OverscaledTileID& tileID) { return std::make_unique<RasterDEMTile>(tileID, parameters, tileset); },
baseImpl->getPrefetchZoomDelta());
algorithm::updateTileMasks(tilePyramid.getRenderedTiles());
}

Expand Down
22 changes: 11 additions & 11 deletions src/mbgl/renderer/sources/render_raster_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ void RenderRasterSource::updateInternal(const Tileset& tileset,
const bool needsRendering,
const bool needsRelayout,
const TileParameters& parameters) {
tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
SourceType::Raster,
impl().getTileSize(),
tileset.zoomRange,
tileset.bounds,
[&] (const OverscaledTileID& tileID) {
return std::make_unique<RasterTile>(tileID, parameters, tileset);
});
tilePyramid.update(
layers,
needsRendering,
needsRelayout,
parameters,
SourceType::Raster,
impl().getTileSize(),
tileset.zoomRange,
tileset.bounds,
[&](const OverscaledTileID& tileID) { return std::make_unique<RasterTile>(tileID, parameters, tileset); },
baseImpl->getPrefetchZoomDelta());
algorithm::updateTileMasks(tilePyramid.getRenderedTiles());
}

Expand Down
24 changes: 13 additions & 11 deletions src/mbgl/renderer/sources/render_vector_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ void RenderVectorSource::updateInternal(const Tileset& tileset,
const bool needsRendering,
const bool needsRelayout,
const TileParameters& parameters) {
tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
SourceType::Vector,
util::tileSize,
tileset.zoomRange,
tileset.bounds,
[&] (const OverscaledTileID& tileID) {
return std::make_unique<VectorTile>(tileID, baseImpl->id, parameters, tileset);
});
tilePyramid.update(
layers,
needsRendering,
needsRelayout,
parameters,
SourceType::Vector,
util::tileSize,
tileset.zoomRange,
tileset.bounds,
[&](const OverscaledTileID& tileID) {
return std::make_unique<VectorTile>(tileID, baseImpl->id, parameters, tileset);
},
baseImpl->getPrefetchZoomDelta());
}

} // namespace mbgl
9 changes: 6 additions & 3 deletions src/mbgl/renderer/tile_pyramid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l
const uint16_t tileSize,
const Range<uint8_t> zoomRange,
optional<LatLngBounds> bounds,
std::function<std::unique_ptr<Tile> (const OverscaledTileID&)> createTile) {
std::function<std::unique_ptr<Tile>(const OverscaledTileID&)> createTile,
optional<uint8_t> sourcePrefetchZoomDelta) {
// If we need a relayout, abandon any cached tiles; they're now stale.
if (needsRelayout) {
cache.clear();
Expand Down Expand Up @@ -105,8 +106,10 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l
if (parameters.mode == MapMode::Continuous && type != style::SourceType::GeoJSON && type != style::SourceType::Annotations) {
// Request lower zoom level tiles (if configured to do so) in an attempt
// to show something on the screen faster at the cost of a little of bandwidth.
if (parameters.prefetchZoomDelta) {
panZoom = std::max<int32_t>(tileZoom - parameters.prefetchZoomDelta, zoomRange.min);
const uint8_t prefetchZoomDelta =
sourcePrefetchZoomDelta ? *sourcePrefetchZoomDelta : parameters.prefetchZoomDelta;
if (prefetchZoomDelta) {
panZoom = std::max<int32_t>(tileZoom - prefetchZoomDelta, zoomRange.min);
}

if (panZoom < idealZoom) {
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/tile_pyramid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class TilePyramid {
uint16_t tileSize,
Range<uint8_t> zoomRange,
optional<LatLngBounds> bounds,
std::function<std::unique_ptr<Tile> (const OverscaledTileID&)> createTile);
std::function<std::unique_ptr<Tile>(const OverscaledTileID&)> createTile,
optional<uint8_t> sourcePrefetchZoomDelta);

const std::map<UnwrappedTileID, std::reference_wrapper<Tile>>& getRenderedTiles() const { return renderedTiles; }
Tile* getTile(const OverscaledTileID&);
Expand Down
12 changes: 12 additions & 0 deletions src/mbgl/style/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ void Source::setObserver(SourceObserver* observer_) {
observer = observer_ ? observer_ : &nullObserver;
}

void Source::setPrefetchZoomDelta(optional<uint8_t> delta) noexcept {
if (getPrefetchZoomDelta() == delta) return;
auto newImpl = createMutable();
newImpl->setPrefetchZoomDelta(std::move(delta));
baseImpl = std::move(newImpl);
observer->onSourceChanged(*this);
}

optional<uint8_t> Source::getPrefetchZoomDelta() const noexcept {
return baseImpl->getPrefetchZoomDelta();
}

void Source::dumpDebugLogs() const {
Log::Info(Event::General, "Source::id: %s", getID().c_str());
Log::Info(Event::General, "Source::loaded: %d", loaded);
Expand Down
Loading