Experimental API to toggle tile cache in map view (#2590)

This commit is contained in:
Alex Cristici
2024-07-16 01:31:11 +03:00
committed by GitHub
parent 9119afc5a3
commit 1ae14a55d0
21 changed files with 93 additions and 4 deletions

View File

@@ -108,6 +108,7 @@ public:
const std::vector<PlacedSymbolData>& getPlacedSymbolsData() const;
// Memory
void setTileCacheEnabled(bool);
void reduceMemoryUse();
void clearData();

View File

@@ -5,6 +5,7 @@
### ✨ Features and improvements
- Avoid logging error for onMove(0,0) on Android ([#2580](https://github.com/maplibre/maplibre-native/pull/2580)).
- Experimental API to toggle tile cache in map view ([#2590](https://github.com/maplibre/maplibre-native/pull/2590)). This can reduce memory usage at the cost of having to parse tile data again when the zoom level changes.
### 🐞 Bug fixes

View File

@@ -86,6 +86,10 @@ const TaggedScheduler& AndroidRendererFrontend::getThreadPool() const {
return mapRenderer.getThreadPool();
}
void AndroidRendererFrontend::setTileCacheEnabled(bool enabled) {
mapRenderer.actor().invoke(&Renderer::setTileCacheEnabled, enabled);
}
void AndroidRendererFrontend::reduceMemoryUse() {
mapRenderer.actor().invoke(&Renderer::reduceMemoryUse);
}

View File

@@ -54,6 +54,7 @@ public:
const std::optional<std::map<std::string, mbgl::Value>>& args) const;
// Memory
void setTileCacheEnabled(bool);
void reduceMemoryUse();
private:

View File

@@ -676,6 +676,10 @@ jni::Local<jni::Array<jni::jlong>> NativeMapView::addMarkers(jni::JNIEnv& env,
return result;
}
void NativeMapView::setTileCacheEnabled(JNIEnv&, jni::jboolean enabled) {
rendererFrontend->setTileCacheEnabled(enabled);
}
void NativeMapView::onLowMemory(JNIEnv&) {
rendererFrontend->reduceMemoryUse();
}
@@ -1284,6 +1288,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
METHOD(&NativeMapView::setDebug, "nativeSetDebug"),
METHOD(&NativeMapView::getDebug, "nativeGetDebug"),
METHOD(&NativeMapView::isFullyLoaded, "nativeIsFullyLoaded"),
METHOD(&NativeMapView::setTileCacheEnabled, "nativeSetTileCacheEnabled"),
METHOD(&NativeMapView::onLowMemory, "nativeOnLowMemory"),
METHOD(&NativeMapView::getMetersPerPixelAtLatitude, "nativeGetMetersPerPixelAtLatitude"),
METHOD(&NativeMapView::projectedMetersForLatLng, "nativeProjectedMetersForLatLng"),

View File

@@ -192,6 +192,8 @@ public:
jni::Local<jni::Array<jni::jlong>> addMarkers(jni::JNIEnv&, const jni::Array<jni::Object<Marker>>&);
void setTileCacheEnabled(JNIEnv&, jni::jboolean);
void onLowMemory(JNIEnv& env);
void setDebug(JNIEnv&, jni::jboolean);

View File

@@ -2504,4 +2504,18 @@ public final class MapLibreMap {
listener.onDeveloperAnimationStarted();
}
}
//
// Tile cache
//
/**
* Indicating whether the map may cache tiles for different zoom levels or not.
*
* @param enabled true causes the map view to consume more memory and have a smoother user
* experience when zoom in/out. The default value of this property is `true`.
*/
public void experimental_setTileCacheEnabled(boolean enabled) {
nativeMapView.setTileCacheEnabled(enabled);
}
}

View File

@@ -33,6 +33,8 @@ interface NativeMap {
void resizeView(int width, int height);
void setTileCacheEnabled(boolean enabled);
void onLowMemory();
void destroy();

View File

@@ -619,6 +619,14 @@ final class NativeMapView implements NativeMap {
nativeSetVisibleCoordinateBounds(coordinates, padding, direction, duration);
}
@Override
public void setTileCacheEnabled(boolean enabled) {
if (checkState("setTileCacheEnabled")) {
return;
}
nativeSetTileCacheEnabled(enabled);
}
@Override
public void onLowMemory() {
if (checkState("onLowMemory")) {
@@ -1332,6 +1340,9 @@ final class NativeMapView implements NativeMap {
private native void nativeSetVisibleCoordinateBounds(LatLng[] coordinates, RectF padding,
double direction, long duration);
@Keep
private native void nativeSetTileCacheEnabled(boolean enabled);
@Keep
private native void nativeOnLowMemory();

View File

@@ -58,6 +58,11 @@ class MLNRenderFrontend : public mbgl::RendererFrontend {
mbgl::Renderer* getRenderer() { return renderer.get(); }
void setTileCacheEnabled(bool enable) {
if (!renderer) return;
renderer->setTileCacheEnabled(enable);
}
void reduceMemoryUse() {
if (!renderer) return;
renderer->reduceMemoryUse();

View File

@@ -4,6 +4,8 @@ MapLibre welcomes participation and contributions from everyone. Please read [`C
## main
- Experimental API to toggle tile cache in map view ([#2590](https://github.com/maplibre/maplibre-native/pull/2590)). This can reduce memory usage at the cost of having to parse tile data again when the zoom level changes.
## 6.5.1
- Enable LTO ([#2504](https://github.com/maplibre/maplibre-native/pull/2504)).

View File

@@ -2111,6 +2111,16 @@ MLN_EXPORT
predicate:(nullable NSPredicate *)predicate
NS_SWIFT_NAME(visibleFeatures(in:styleLayerIdentifiers:predicate:));
// MARK: Tile Cache
/**
Indicating whether the map may cache tiles for different zoom levels or not.
@param enabled causes the map view to consume more memory and have a smoother user
* experience when zoom in/out. The default value of this property is `YES`.
*/
- (void)experimental_setTileCacheEnabled:(BOOL)enabled;
// MARK: Debugging the Map
/**

View File

@@ -7340,6 +7340,12 @@ static void *windowScreenContext = &windowScreenContext;
return _mbglView->getObject();
}
// MARK: Tile Cache
- (void)experimental_setTileCacheEnabled:(BOOL)enabled {
_rendererFrontend->setTileCacheEnabled(enabled);
}
@end
// MARK: - IBAdditions methods

View File

@@ -330,6 +330,7 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
for (const auto& entry : sourceDiff.added) {
std::unique_ptr<RenderSource> renderSource = RenderSource::create(entry.second, threadPool);
renderSource->setObserver(this);
renderSource->enableCache(tileCacheEnabled);
renderSources.emplace(entry.first, std::move(renderSource));
}
transformState = updateParameters->transformState;
@@ -737,6 +738,14 @@ void RenderOrchestrator::removeFeatureState(const std::string& sourceID,
}
}
void RenderOrchestrator::setTileCacheEnabled(bool enable) {
tileCacheEnabled = enable;
for (const auto& entry : renderSources) {
entry.second->enableCache(enable);
}
}
void RenderOrchestrator::reduceMemoryUse() {
MLN_TRACE_FUNC();

View File

@@ -92,6 +92,7 @@ public:
const std::optional<std::string>& featureID,
const std::optional<std::string>& stateKey);
void setTileCacheEnabled(bool);
void reduceMemoryUse();
void dumpDebugLogs();
void collectPlacedSymbolData(bool);
@@ -206,6 +207,7 @@ private:
const bool backgroundLayerAsColor;
bool contextLost = false;
bool placedSymbolDataCollected = false;
bool tileCacheEnabled = true;
// Vectors with reserved capacity of layerImpls->size() to avoid
// reallocation on each frame.

View File

@@ -101,6 +101,8 @@ public:
const std::optional<std::string>&,
const std::optional<std::string>&) {}
virtual void enableCache(bool) {};
virtual void reduceMemoryUse() = 0;
virtual void dumpDebugLogs() const = 0;

View File

@@ -134,6 +134,10 @@ const std::vector<PlacedSymbolData>& Renderer::getPlacedSymbolsData() const {
return impl->orchestrator.getPlacedSymbolsData();
}
void Renderer::setTileCacheEnabled(bool enable) {
impl->orchestrator.setTileCacheEnabled(enable);
}
void Renderer::reduceMemoryUse() {
gfx::BackendScope guard{impl->backend};
impl->reduceMemoryUse();

View File

@@ -494,6 +494,10 @@ void RenderTileSource::removeFeatureState(const std::optional<std::string>& sour
featureState.removeState(sourceLayerID, featureID, stateKey);
}
void RenderTileSource::enableCache(bool enable) {
tilePyramid.enableCache(enable);
}
void RenderTileSource::reduceMemoryUse() {
tilePyramid.reduceMemoryUse();
}

View File

@@ -47,6 +47,7 @@ public:
const std::optional<std::string>&,
const std::optional<std::string>&) override;
void enableCache(bool) override;
void reduceMemoryUse() override;
void dumpDebugLogs() const override;

View File

@@ -215,12 +215,14 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l
}
}
if (type != SourceType::Annotations) {
if (type != SourceType::Annotations && cacheEnabled) {
auto conservativeCacheSize = static_cast<size_t>(
std::max(static_cast<double>(parameters.transformState.getSize().width) / tileSize, 1.0) *
std::max(static_cast<double>(parameters.transformState.getSize().height) / tileSize, 1.0) *
(parameters.transformState.getMaxZoom() - parameters.transformState.getMinZoom() + 1) * 0.5);
cache.setSize(conservativeCacheSize);
} else {
cache.setSize(0);
}
// Remove stale tiles. This goes through the (sorted!) tiles map and retain
@@ -388,8 +390,8 @@ std::vector<Feature> TilePyramid::querySourceFeatures(const SourceQueryOptions&
return result;
}
void TilePyramid::setCacheSize(size_t size) {
cache.setSize(size);
void TilePyramid::enableCache(bool enable) {
cacheEnabled = enable;
}
void TilePyramid::reduceMemoryUse() {

View File

@@ -60,7 +60,7 @@ public:
std::vector<Feature> querySourceFeatures(const SourceQueryOptions&) const;
void setCacheSize(size_t);
void enableCache(bool);
void reduceMemoryUse();
void setObserver(TileObserver*);
@@ -84,6 +84,7 @@ private:
float prevLng = 0;
bool fadingTiles = false;
bool cacheEnabled = true;
};
} // namespace mbgl