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

Commit

Permalink
[core] Improve performance for query rendered features
Browse files Browse the repository at this point in the history
- query rendered symbols only from layers that support it
- remove unnecessary iterations over vectors
  • Loading branch information
alexshalamov committed Jun 17, 2019
1 parent 5d6b06a commit 1cf350e
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/mbgl/annotation/render_annotation_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void RenderAnnotationSource::update(Immutable<style::Source::Impl> baseImpl_,
std::unordered_map<std::string, std::vector<Feature>>
RenderAnnotationSource::queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const {
return tilePyramid.queryRenderedFeatures(geometry, transformState, layers, options, projMatrix);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/annotation/render_annotation_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RenderAnnotationSource final : public RenderTileSource {
std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const final;

Expand Down
21 changes: 7 additions & 14 deletions src/mbgl/geometry/feature_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void FeatureIndex::query(
const double scale,
const RenderedQueryOptions& queryOptions,
const UnwrappedTileID& tileID,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const float additionalQueryPadding) const {

if (!tileData) {
Expand Down Expand Up @@ -81,7 +81,7 @@ void FeatureIndex::query(
std::unordered_map<std::string, std::vector<Feature>>
FeatureIndex::lookupSymbolFeatures(const std::vector<IndexedSubfeature>& symbolFeatures,
const RenderedQueryOptions& queryOptions,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const OverscaledTileID& tileID,
const std::shared_ptr<std::vector<size_t>>& featureSortOrder) const {
std::unordered_map<std::string, std::vector<Feature>> result;
Expand Down Expand Up @@ -123,31 +123,24 @@ void FeatureIndex::addFeature(
const IndexedSubfeature& indexedFeature,
const RenderedQueryOptions& options,
const CanonicalTileID& tileID,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const GeometryCoordinates& queryGeometry,
const TransformState& transformState,
const float pixelsToTileUnits,
const mat4& posMatrix) const {

auto getRenderLayer = [&] (const std::string& layerID) -> const RenderLayer* {
for (const auto& layer : layers) {
if (layer->getID() == layerID) {
return layer;
}
}
return nullptr;
};

// Lazily calculated.
std::unique_ptr<GeometryTileLayer> sourceLayer;
std::unique_ptr<GeometryTileFeature> geometryTileFeature;

for (const std::string& layerID : bucketLayerIDs.at(indexedFeature.bucketLeaderID)) {
const RenderLayer* renderLayer = getRenderLayer(layerID);
if (!renderLayer) {
const auto it = layers.find(layerID);
if (it == layers.end()) {
continue;
}

const RenderLayer* renderLayer = it->second;

if (!geometryTileFeature) {
sourceLayer = tileData->getLayer(indexedFeature.sourceLayerName);
assert(sourceLayer);
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/geometry/feature_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FeatureIndex {
const double scale,
const RenderedQueryOptions& options,
const UnwrappedTileID&,
const std::vector<const RenderLayer*>&,
const std::unordered_map<std::string, const RenderLayer*>&,
const float additionalQueryPadding) const;

static optional<GeometryCoordinates> translateQueryGeometry(
Expand All @@ -82,7 +82,7 @@ class FeatureIndex {
std::unordered_map<std::string, std::vector<Feature>> lookupSymbolFeatures(
const std::vector<IndexedSubfeature>& symbolFeatures,
const RenderedQueryOptions& options,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const OverscaledTileID& tileID,
const std::shared_ptr<std::vector<size_t>>& featureSortOrder) const;

Expand All @@ -92,7 +92,7 @@ class FeatureIndex {
const IndexedSubfeature&,
const RenderedQueryOptions& options,
const CanonicalTileID&,
const std::vector<const RenderLayer*>&,
const std::unordered_map<std::string, const RenderLayer*>&,
const GeometryCoordinates& queryGeometry,
const TransformState& transformState,
const float pixelsToTileUnits,
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/render_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class RenderSource : protected TileObserver {
virtual std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const = 0;

Expand Down
43 changes: 28 additions & 15 deletions src/mbgl/renderer/renderer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) {
}

// Create render layers for newly added layers.
renderLayers.reserve(renderLayers.size() + layerDiff.added.size());
for (const auto& entry : layerDiff.added) {
auto renderLayer = LayerManager::get()->createRenderLayer(entry.second);
renderLayer->transition(transitionParameters);
Expand Down Expand Up @@ -550,16 +551,16 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) {
}

std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineString& geometry, const RenderedQueryOptions& options) const {
std::vector<const RenderLayer*> layers;
std::unordered_map<std::string, const RenderLayer*> layers;
if (options.layerIDs) {
for (const auto& layerID : *options.layerIDs) {
if (const RenderLayer* layer = getRenderLayer(layerID)) {
layers.emplace_back(layer);
layers.emplace(layer->getID(), layer);
}
}
} else {
for (const auto& entry : renderLayers) {
layers.emplace_back(entry.second.get());
layers.emplace(entry.second->getID(), entry.second.get());
}
}

Expand All @@ -568,9 +569,22 @@ std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineStrin

void Renderer::Impl::queryRenderedSymbols(std::unordered_map<std::string, std::vector<Feature>>& resultsByLayer,
const ScreenLineString& geometry,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options) const {

const auto hasCrossTileIndex = [] (const auto& pair) {
return pair.second->baseImpl->getTypeInfo()->crossTileIndex == style::LayerTypeInfo::CrossTileIndex::Required;
};

std::unordered_map<std::string, const RenderLayer*> crossTileSymbolIndexLayers;
std::copy_if(layers.begin(),
layers.end(),
std::inserter(crossTileSymbolIndexLayers, crossTileSymbolIndexLayers.begin()),
hasCrossTileIndex);

if (crossTileSymbolIndexLayers.empty()) {
return;
}

auto renderedSymbols = placement->getCollisionIndex().queryRenderedSymbols(geometry);
std::vector<std::reference_wrapper<const RetainedQueryData>> bucketQueryData;
for (auto entry : renderedSymbols) {
Expand All @@ -588,7 +602,7 @@ void Renderer::Impl::queryRenderedSymbols(std::unordered_map<std::string, std::v
auto& queryData = wrappedQueryData.get();
auto bucketSymbols = queryData.featureIndex->lookupSymbolFeatures(renderedSymbols[queryData.bucketInstanceId],
options,
layers,
crossTileSymbolIndexLayers,
queryData.tileID,
queryData.featureSortOrder);

Expand All @@ -599,10 +613,10 @@ void Renderer::Impl::queryRenderedSymbols(std::unordered_map<std::string, std::v
}
}

std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineString& geometry, const RenderedQueryOptions& options, const std::vector<const RenderLayer*>& layers) const {
std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineString& geometry, const RenderedQueryOptions& options, const std::unordered_map<std::string, const RenderLayer*>& layers) const {
std::unordered_set<std::string> sourceIDs;
for (const RenderLayer* layer : layers) {
sourceIDs.emplace(layer->baseImpl->source);
for (const auto& pair : layers) {
sourceIDs.emplace(pair.second->baseImpl->source);
}

mat4 projMatrix;
Expand All @@ -625,13 +639,12 @@ std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineStrin
}

// Combine all results based on the style layer renderItems.
for (const auto& layerImpl : *layerImpls) {
const RenderLayer* layer = getRenderLayer(layerImpl->id);
if (!layer->needsRendering() || !layer->supportsZoom(zoomHistory.lastZoom)) {
for (const auto& pair : layers) {
if (!pair.second->needsRendering() || !pair.second->supportsZoom(zoomHistory.lastZoom)) {
continue;
}

auto it = resultsByLayer.find(layer->baseImpl->id);
auto it = resultsByLayer.find(pair.second->baseImpl->id);
if (it != resultsByLayer.end()) {
std::move(it->second.begin(), it->second.end(), std::back_inserter(result));
}
Expand All @@ -642,13 +655,13 @@ std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineStrin

std::vector<Feature> Renderer::Impl::queryShapeAnnotations(const ScreenLineString& geometry) const {
assert(LayerManager::annotationsEnabled);
std::vector<const RenderLayer*> shapeAnnotationLayers;
std::unordered_map<std::string, const RenderLayer*> shapeAnnotationLayers;
RenderedQueryOptions options;
for (const auto& layerImpl : *layerImpls) {
if (std::mismatch(layerImpl->id.begin(), layerImpl->id.end(),
AnnotationManager::ShapeLayerID.begin(), AnnotationManager::ShapeLayerID.end()).second == AnnotationManager::ShapeLayerID.end()) {
if (const RenderLayer* layer = getRenderLayer(layerImpl->id)) {
shapeAnnotationLayers.emplace_back(layer);
shapeAnnotationLayers.emplace(layer->getID(), layer);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/renderer/renderer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ class Renderer::Impl : public GlyphManagerObserver,

void queryRenderedSymbols(std::unordered_map<std::string, std::vector<Feature>>& resultsByLayer,
const ScreenLineString& geometry,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options) const;

std::vector<Feature> queryRenderedFeatures(const ScreenLineString&, const RenderedQueryOptions&, const std::vector<const RenderLayer*>&) const;
std::vector<Feature> queryRenderedFeatures(const ScreenLineString&, const RenderedQueryOptions&, const std::unordered_map<std::string, const RenderLayer*>&) const;

// GlyphManagerObserver implementation.
void onGlyphsError(const FontStack&, const GlyphRange&, std::exception_ptr) override;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_image_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void RenderImageSource::finishRender(PaintParameters& parameters) {
std::unordered_map<std::string, std::vector<Feature>>
RenderImageSource::queryRenderedFeatures(const ScreenLineString&,
const TransformState&,
const std::vector<const RenderLayer*>&,
const std::unordered_map<std::string, const RenderLayer*>&,
const RenderedQueryOptions&,
const mat4&) const {
return std::unordered_map<std::string, std::vector<Feature>> {};
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_image_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RenderImageSource : public RenderSource {
std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const final;

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_raster_dem_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void RenderRasterDEMSource::onTileChanged(Tile& tile){
std::unordered_map<std::string, std::vector<Feature>>
RenderRasterDEMSource::queryRenderedFeatures(const ScreenLineString&,
const TransformState&,
const std::vector<const RenderLayer*>&,
const std::unordered_map<std::string, const RenderLayer*>&,
const RenderedQueryOptions&,
const mat4&) const {
return std::unordered_map<std::string, std::vector<Feature>>{};
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_raster_dem_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RenderRasterDEMSource final : public RenderTileSource {
std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const override;

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_raster_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void RenderRasterSource::prepare(const SourcePrepareParameters& parameters) {
std::unordered_map<std::string, std::vector<Feature>>
RenderRasterSource::queryRenderedFeatures(const ScreenLineString&,
const TransformState&,
const std::vector<const RenderLayer*>&,
const std::unordered_map<std::string, const RenderLayer*>&,
const RenderedQueryOptions&,
const mat4&) const {
return std::unordered_map<std::string, std::vector<Feature>>{};
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_raster_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RenderRasterSource final : public RenderTileSource {
std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const override;

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_tile_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ std::vector<std::reference_wrapper<RenderTile>> RenderTileSource::getRenderedTil
std::unordered_map<std::string, std::vector<Feature>>
RenderTileSource::queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const {
return tilePyramid.queryRenderedFeatures(geometry, transformState, layers, options, projMatrix);
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/sources/render_tile_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RenderTileSource : public RenderSource {
std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const override;

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/tile_pyramid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void TilePyramid::handleWrapJump(float lng) {

std::unordered_map<std::string, std::vector<Feature>> TilePyramid::queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) const {
std::unordered_map<std::string, std::vector<Feature>> result;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/tile_pyramid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TilePyramid {
std::unordered_map<std::string, std::vector<Feature>>
queryRenderedFeatures(const ScreenLineString& geometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>&,
const std::unordered_map<std::string, const RenderLayer*>&,
const RenderedQueryOptions& options,
const mat4& projMatrix) const;

Expand Down
10 changes: 5 additions & 5 deletions src/mbgl/tile/geometry_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ LayerRenderData* GeometryTile::getMutableLayerRenderData(const style::Layer::Imp
return &result;
}

float GeometryTile::getQueryPadding(const std::vector<const RenderLayer*>& layers) {
float GeometryTile::getQueryPadding(const std::unordered_map<std::string, const RenderLayer*>& layers) {
float queryPadding = 0;
for (const RenderLayer* layer : layers) {
auto bucket = getBucket(*layer->baseImpl);
for (const auto& pair : layers) {
auto bucket = getBucket(*pair.second->baseImpl);
if (bucket && bucket->hasData()) {
queryPadding = std::max(queryPadding, bucket->getQueryRadius(*layer));
queryPadding = std::max(queryPadding, bucket->getQueryRadius(*pair.second));
}
}
return queryPadding;
Expand All @@ -253,7 +253,7 @@ void GeometryTile::queryRenderedFeatures(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCoordinates& queryGeometry,
const TransformState& transformState,
const std::vector<const RenderLayer*>& layers,
const std::unordered_map<std::string, const RenderLayer*>& layers,
const RenderedQueryOptions& options,
const mat4& projMatrix) {

Expand Down
Loading

0 comments on commit 1cf350e

Please sign in to comment.