core: add CustomVectorSource for binary tile data delivery (#4377)

Co-authored-by: Vladyslav Odobesku <VladyslavOdobesku@intellias.com>
Co-authored-by: Claude Opus 4 <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Vladyslav Odobesku
2026-08-04 19:23:41 +03:00
committed by GitHub
parent 25e4f6acbf
commit 9ef0f1c790
29 changed files with 1307 additions and 1 deletions

View File

@@ -4,6 +4,7 @@
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/renderer/render_source_observer.hpp>
#include <mbgl/renderer/sources/render_custom_geometry_source.hpp>
#include <mbgl/renderer/sources/render_custom_vector_source.hpp>
#include <mbgl/renderer/sources/render_geojson_source.hpp>
#include <mbgl/renderer/sources/render_image_source.hpp>
#include <mbgl/renderer/sources/render_raster_dem_source.hpp>
@@ -49,6 +50,9 @@ std::unique_ptr<RenderSource> RenderSource::create(const Immutable<Source::Impl>
case SourceType::CustomVector:
return std::make_unique<RenderCustomGeometrySource>(staticImmutableCast<CustomGeometrySource::Impl>(impl),
threadPool);
case SourceType::CustomMVTVector:
return std::make_unique<RenderCustomVectorSource>(staticImmutableCast<CustomVectorSource::Impl>(impl),
threadPool);
}
// Not reachable, but placate GCC.

View File

@@ -0,0 +1,57 @@
#include <mbgl/renderer/sources/render_custom_vector_source.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/tile/custom_vector_tile.hpp>
#include <mbgl/util/constants.hpp>
namespace mbgl {
using namespace style;
RenderCustomVectorSource::RenderCustomVectorSource(Immutable<style::CustomVectorSource::Impl> impl_,
const TaggedScheduler& threadPool_)
: RenderTileSource(std::move(impl_), threadPool_) {
tilePyramid.setObserver(this);
}
const style::CustomVectorSource::Impl& RenderCustomVectorSource::impl() const {
return static_cast<const style::CustomVectorSource::Impl&>(*baseImpl);
}
void RenderCustomVectorSource::update(Immutable<style::Source::Impl> baseImpl_,
const std::vector<Immutable<style::LayerProperties>>& layers,
const bool needsRendering,
const bool needsRelayout,
const TileParameters& parameters) {
if (baseImpl != baseImpl_) {
std::swap(baseImpl, baseImpl_);
auto newImpl = staticImmutableCast<style::CustomVectorSource::Impl>(baseImpl);
auto currentImpl = staticImmutableCast<style::CustomVectorSource::Impl>(baseImpl_);
if (*newImpl != *currentImpl) {
tilePyramid.clearAll();
}
}
enabled = needsRendering;
auto tileLoader = impl().getTileLoader();
if (!tileLoader) {
return;
}
tilePyramid.update(layers,
needsRendering,
needsRelayout,
parameters,
*baseImpl,
util::tileSize_I,
impl().getZoomRange(),
{},
[&](const OverscaledTileID& tileID, TileObserver* observer_) {
return std::make_unique<CustomVectorTile>(
tileID, impl().id, parameters, *tileLoader, observer_);
});
}
} // namespace mbgl

View File

@@ -0,0 +1,22 @@
#pragma once
#include <mbgl/renderer/sources/render_tile_source.hpp>
#include <mbgl/style/sources/custom_vector_source_impl.hpp>
namespace mbgl {
class RenderCustomVectorSource final : public RenderTileSource {
public:
explicit RenderCustomVectorSource(Immutable<style::CustomVectorSource::Impl>, const TaggedScheduler&);
void update(Immutable<style::Source::Impl>,
const std::vector<Immutable<style::LayerProperties>>&,
bool needsRendering,
bool needsRelayout,
const TileParameters&) override;
private:
const style::CustomVectorSource::Impl& impl() const;
};
} // namespace mbgl

View File

@@ -0,0 +1,114 @@
#include <mbgl/style/custom_vector_tile_loader.hpp>
#include <mbgl/tile/custom_vector_tile.hpp>
namespace mbgl {
namespace style {
CustomVectorTileLoader::CustomVectorTileLoader(const TileFunction& fetchTileFn, const TileFunction& cancelTileFn)
: fetchTileFunction(fetchTileFn),
cancelTileFunction(cancelTileFn) {}
void CustomVectorTileLoader::fetchTile(const OverscaledTileID& tileID, const ActorRef<CustomVectorTile>& tileRef) {
std::scoped_lock guard(dataMutex);
auto cachedTileData = dataCache.find(tileID.canonical);
if (cachedTileData != dataCache.end()) {
tileRef.invoke(&CustomVectorTile::setTileData, cachedTileData->second.first, cachedTileData->second.second);
}
auto tileCallbacks = tileCallbackMap.find(tileID.canonical);
if (tileCallbacks == tileCallbackMap.end()) {
auto tuple = std::make_tuple(tileID.overscaledZ, tileID.wrap, tileRef);
tileCallbackMap.insert({tileID.canonical, std::vector<OverscaledIDFunctionTuple>(1, tuple)});
} else {
for (auto& iter : tileCallbacks->second) {
if (std::get<0>(iter) == tileID.overscaledZ && std::get<1>(iter) == tileID.wrap) {
std::get<2>(iter) = tileRef;
return;
}
}
tileCallbacks->second.emplace_back(std::make_tuple(tileID.overscaledZ, tileID.wrap, tileRef));
}
if (cachedTileData == dataCache.end()) {
invokeTileFetch(tileID.canonical);
}
}
void CustomVectorTileLoader::cancelTile(const OverscaledTileID& tileID) {
std::scoped_lock guard(dataMutex);
if (tileCallbackMap.contains(tileID.canonical)) {
invokeTileCancel(tileID.canonical);
// Erase so a subsequent fetchTile for the same tile re-issues the fetch.
// Intentionally diverges from CustomTileLoader which omits this — that is a
// latent bug there that needs a separate cleanup.
tileCallbackMap.erase(tileID.canonical);
}
}
void CustomVectorTileLoader::removeTile(const OverscaledTileID& tileID) {
std::scoped_lock guard(dataMutex);
auto tileCallbacks = tileCallbackMap.find(tileID.canonical);
if (tileCallbacks == tileCallbackMap.end()) return;
for (auto iter = tileCallbacks->second.begin(); iter != tileCallbacks->second.end(); iter++) {
if (std::get<0>(*iter) == tileID.overscaledZ && std::get<1>(*iter) == tileID.wrap) {
tileCallbacks->second.erase(iter);
invokeTileCancel(tileID.canonical);
break;
}
}
if (tileCallbacks->second.empty()) {
tileCallbackMap.erase(tileCallbacks);
dataCache.erase(tileID.canonical);
}
}
void CustomVectorTileLoader::setTileData(const CanonicalTileID& tileID,
const std::shared_ptr<const std::string>& data,
TileDataFormat format) {
std::scoped_lock guard(dataMutex);
auto iter = tileCallbackMap.find(tileID);
if (iter == tileCallbackMap.end()) return;
for (auto& tuple : iter->second) {
auto actor = std::get<2>(tuple);
actor.invoke(&CustomVectorTile::setTileData, data, format);
}
dataCache[tileID] = std::make_pair(data, format);
}
void CustomVectorTileLoader::setTileError(const CanonicalTileID& tileID, std::exception_ptr error) {
std::scoped_lock guard(dataMutex);
auto iter = tileCallbackMap.find(tileID);
if (iter == tileCallbackMap.end()) return;
for (auto& tuple : iter->second) {
auto actor = std::get<2>(tuple);
actor.invoke(&CustomVectorTile::setTileError, error);
}
tileCallbackMap.erase(iter);
dataCache.erase(tileID);
}
void CustomVectorTileLoader::invalidateTile(const CanonicalTileID& tileID) {
std::scoped_lock guard(dataMutex);
auto tileCallbacks = tileCallbackMap.find(tileID);
if (tileCallbacks == tileCallbackMap.end()) return;
for (auto& iter : tileCallbacks->second) {
auto actor = std::get<2>(iter);
actor.invoke(&CustomVectorTile::invalidateTileData);
invokeTileCancel(tileID);
}
tileCallbackMap.erase(tileCallbacks);
dataCache.erase(tileID);
}
void CustomVectorTileLoader::invokeTileFetch(const CanonicalTileID& tileID) {
if (fetchTileFunction != nullptr) {
fetchTileFunction(tileID);
}
}
void CustomVectorTileLoader::invokeTileCancel(const CanonicalTileID& tileID) {
if (cancelTileFunction != nullptr) {
cancelTileFunction(tileID);
}
}
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,52 @@
#pragma once
#include <mbgl/actor/actor_ref.hpp>
#include <mbgl/style/sources/custom_vector_source.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
namespace mbgl {
class CustomVectorTile;
namespace style {
class CustomVectorTileLoader {
public:
CustomVectorTileLoader(const CustomVectorTileLoader&) = delete;
CustomVectorTileLoader& operator=(const CustomVectorTileLoader&) = delete;
using OverscaledIDFunctionTuple = std::tuple<uint8_t, int16_t, ActorRef<CustomVectorTile>>;
CustomVectorTileLoader(const TileFunction& fetchTileFn, const TileFunction& cancelTileFn);
void fetchTile(const OverscaledTileID& tileID, const ActorRef<CustomVectorTile>& tileRef);
void cancelTile(const OverscaledTileID& tileID);
void removeTile(const OverscaledTileID& tileID);
void setTileData(const CanonicalTileID& tileID,
const std::shared_ptr<const std::string>& data,
TileDataFormat format);
void setTileError(const CanonicalTileID& tileID, std::exception_ptr error);
void invalidateTile(const CanonicalTileID&);
private:
void invokeTileFetch(const CanonicalTileID& tileID);
void invokeTileCancel(const CanonicalTileID& tileID);
TileFunction fetchTileFunction;
TileFunction cancelTileFunction;
std::unordered_map<CanonicalTileID, std::vector<OverscaledIDFunctionTuple>> tileCallbackMap;
std::map<CanonicalTileID, std::pair<std::shared_ptr<const std::string>, TileDataFormat>> dataCache;
std::mutex dataMutex;
};
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,56 @@
#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/style/custom_vector_tile_loader.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/sources/custom_vector_source.hpp>
#include <mbgl/style/sources/custom_vector_source_impl.hpp>
#include <mbgl/tile/tile.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <utility>
namespace mbgl {
namespace style {
CustomVectorSource::CustomVectorSource(std::string id, const CustomVectorSource::Options& options)
: Source(makeMutable<CustomVectorSource::Impl>(std::move(id), options)),
loader(std::make_unique<Actor<CustomVectorTileLoader>>(
Scheduler::GetBackground(), options.fetchTileFunction, options.cancelTileFunction)) {}
CustomVectorSource::~CustomVectorSource() = default;
const CustomVectorSource::Impl& CustomVectorSource::impl() const {
return static_cast<const CustomVectorSource::Impl&>(*baseImpl);
}
void CustomVectorSource::loadDescription(FileSource&) {
baseImpl = makeMutable<Impl>(impl(), loader->self());
loaded = true;
observer->onSourceLoaded(*this);
}
bool CustomVectorSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const {
return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind);
}
void CustomVectorSource::setTileData(const CanonicalTileID& tileID,
const std::shared_ptr<const std::string>& data,
TileDataFormat format) {
loader->self().invoke(&CustomVectorTileLoader::setTileData, tileID, data, format);
}
void CustomVectorSource::setTileError(const CanonicalTileID& tileID, std::exception_ptr error) {
loader->self().invoke(&CustomVectorTileLoader::setTileError, tileID, error);
}
void CustomVectorSource::invalidateTile(const CanonicalTileID& tileID) {
loader->self().invoke(&CustomVectorTileLoader::invalidateTile, tileID);
}
Mutable<Source::Impl> CustomVectorSource::createMutable() const noexcept {
return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl()));
}
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,33 @@
#include <mbgl/style/sources/custom_vector_source_impl.hpp>
namespace mbgl {
namespace style {
CustomVectorSource::Impl::Impl(std::string id_, const CustomVectorSource::Options& options)
: Source::Impl(SourceType::CustomMVTVector, std::move(id_)),
zoomRange(options.zoomRange),
loaderRef({}) {}
CustomVectorSource::Impl::Impl(const Impl& impl, const ActorRef<CustomVectorTileLoader>& loaderRef_)
: Source::Impl(impl),
zoomRange(impl.zoomRange),
loaderRef(loaderRef_) {}
bool CustomVectorSource::Impl::operator!=(const Impl& other) const noexcept {
return zoomRange != other.zoomRange || bool(loaderRef) != bool(other.loaderRef);
}
std::optional<std::string> CustomVectorSource::Impl::getAttribution() const {
return {};
}
Range<uint8_t> CustomVectorSource::Impl::getZoomRange() const {
return zoomRange;
}
std::optional<ActorRef<CustomVectorTileLoader>> CustomVectorSource::Impl::getTileLoader() const {
return loaderRef;
}
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,28 @@
#pragma once
#include <mbgl/style/source_impl.hpp>
#include <mbgl/style/sources/custom_vector_source.hpp>
#include <mbgl/style/custom_vector_tile_loader.hpp>
#include <mbgl/actor/actor_ref.hpp>
namespace mbgl {
namespace style {
class CustomVectorSource::Impl : public Source::Impl {
public:
Impl(std::string id, const CustomVectorSource::Options& options);
Impl(const Impl&, const ActorRef<CustomVectorTileLoader>&);
std::optional<std::string> getAttribution() const final;
Range<uint8_t> getZoomRange() const;
std::optional<ActorRef<CustomVectorTileLoader>> getTileLoader() const;
bool operator!=(const Impl&) const noexcept;
private:
Range<uint8_t> zoomRange;
std::optional<ActorRef<CustomVectorTileLoader>> loaderRef;
};
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,63 @@
#include <mbgl/tile/custom_vector_tile.hpp>
#include <mbgl/tile/vector_mvt_tile_data.hpp>
#include <mbgl/renderer/tile_parameters.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/style/custom_vector_tile_loader.hpp>
#include <mbgl/tile/tile_observer.hpp>
#include <utility>
namespace mbgl {
CustomVectorTile::CustomVectorTile(const OverscaledTileID& overscaledTileID,
std::string sourceID_,
const TileParameters& parameters,
ActorRef<style::CustomVectorTileLoader> loader_,
TileObserver* observer_)
: GeometryTile(overscaledTileID, std::move(sourceID_), parameters, observer_),
necessity(TileNecessity::Optional),
loader(std::move(loader_)),
mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
actorRef(*this, mailbox) {}
CustomVectorTile::~CustomVectorTile() {
loader.invoke(&style::CustomVectorTileLoader::removeTile, id);
}
void CustomVectorTile::setTileData(const std::shared_ptr<const std::string>& data, style::TileDataFormat format) {
if (obsolete) return;
switch (format) {
case style::TileDataFormat::MVT:
GeometryTile::setData(data ? std::make_unique<VectorMVTTileData>(data) : nullptr);
break;
default:
assert(false && "Unhandled TileDataFormat");
GeometryTile::setData(nullptr);
break;
}
}
void CustomVectorTile::setTileError(std::exception_ptr error) {
if (obsolete) return;
GeometryTile::setError(std::move(error));
}
void CustomVectorTile::invalidateTileData() {
stale = true;
observer->onTileChanged(*this);
}
void CustomVectorTile::setNecessity(TileNecessity newNecessity) {
if (newNecessity != necessity || stale) {
necessity = newNecessity;
if (necessity == TileNecessity::Required) {
loader.invoke(&style::CustomVectorTileLoader::fetchTile, id, actorRef);
stale = false;
} else if (!isRenderable()) {
loader.invoke(&style::CustomVectorTileLoader::cancelTile, id);
}
}
}
} // namespace mbgl

View File

@@ -0,0 +1,37 @@
#pragma once
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/style/sources/custom_vector_source.hpp>
#include <mbgl/actor/mailbox.hpp>
namespace mbgl {
class TileParameters;
namespace style {
class CustomVectorTileLoader;
} // namespace style
class CustomVectorTile : public GeometryTile {
public:
CustomVectorTile(const OverscaledTileID&,
std::string sourceID,
const TileParameters&,
ActorRef<style::CustomVectorTileLoader> loader,
TileObserver* observer = nullptr);
~CustomVectorTile() override;
void setTileData(const std::shared_ptr<const std::string>& data, style::TileDataFormat format);
void setTileError(std::exception_ptr error);
void invalidateTileData();
void setNecessity(TileNecessity) final;
private:
bool stale = true;
TileNecessity necessity;
ActorRef<style::CustomVectorTileLoader> loader;
std::shared_ptr<Mailbox> mailbox;
ActorRef<CustomVectorTile> actorRef;
};
} // namespace mbgl