From e0a2e36d290f5fd8d97319dfbb40cbb3ebd29fac Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Mon, 17 Dec 2018 16:03:38 +0200 Subject: [mac, ios] Enable core-only layers `LayerManagerDarwin` can add layer types that are enabled only for JSON style. It allows to exclude the SDK wrappers for these layers from the project and decrease binary size. --- .../android/src/style/layers/layer_manager.hpp | 2 +- platform/darwin/src/MGLStyleLayerManager.h | 16 ++++++++- platform/darwin/src/MGLStyleLayerManager.mm | 38 +++++++++++++++++----- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/platform/android/src/style/layers/layer_manager.hpp b/platform/android/src/style/layers/layer_manager.hpp index fb87721dd2..807ded1737 100644 --- a/platform/android/src/style/layers/layer_manager.hpp +++ b/platform/android/src/style/layers/layer_manager.hpp @@ -30,7 +30,7 @@ public: private: LayerManagerAndroid(); /** - * @brief Enables a layer type for both JSON style an runtime API. + * @brief Enables a layer type for both JSON style and runtime API. */ void addLayerType(std::unique_ptr); /** diff --git a/platform/darwin/src/MGLStyleLayerManager.h b/platform/darwin/src/MGLStyleLayerManager.h index cdd30f4fff..95fecd0252 100644 --- a/platform/darwin/src/MGLStyleLayerManager.h +++ b/platform/darwin/src/MGLStyleLayerManager.h @@ -20,13 +20,27 @@ public: private: LayerManagerDarwin(); + /** + * Enables a layer type for both JSON style and runtime API. + */ void addLayerType(std::unique_ptr); + /** + * Enables a layer type for JSON style only. + * + * We might not want to expose runtime API for some layer types + * in order to save binary size (the corresponding SDK layer wrappers + * should be excluded from the project build). + */ + void addLayerTypeCoreOnly(std::unique_ptr); + + void registerCoreFactory(LayerFactory*); LayerPeerFactory* getPeerFactory(const style::LayerTypeInfo* typeInfo); // mbgl::LayerManager overrides. LayerFactory* getFactory(const std::string& type) noexcept final; LayerFactory* getFactory(const mbgl::style::LayerTypeInfo* info) noexcept final; - std::vector> factories; + std::vector> peerFactories; + std::vector> coreFactories; std::map typeToFactory; }; diff --git a/platform/darwin/src/MGLStyleLayerManager.mm b/platform/darwin/src/MGLStyleLayerManager.mm index bdcc303de5..a05e24bd9d 100644 --- a/platform/darwin/src/MGLStyleLayerManager.mm +++ b/platform/darwin/src/MGLStyleLayerManager.mm @@ -38,22 +38,33 @@ MGLStyleLayer* LayerManagerDarwin::createPeer(style::Layer* layer) { } void LayerManagerDarwin::addLayerType(std::unique_ptr factory) { - auto* coreFactory = factory->getCoreLayerFactory(); - std::string type{coreFactory->getTypeInfo()->type}; + NSCAssert(getFactory(factory->getCoreLayerFactory()->getTypeInfo()) == nullptr, + @"A layer factory with the given info is already added."); + registerCoreFactory(factory->getCoreLayerFactory()); + peerFactories.emplace_back(std::move(factory)); +} + +void LayerManagerDarwin::addLayerTypeCoreOnly(std::unique_ptr factory) { + NSCAssert(getFactory(factory->getTypeInfo()) == nullptr, + @"A layer factory with the given info is already added."); + registerCoreFactory(factory.get()); + coreFactories.emplace_back(std::move(factory)); +} + +void LayerManagerDarwin::registerCoreFactory(LayerFactory* factory) { + std::string type{factory->getTypeInfo()->type}; if (!type.empty()) { - typeToFactory.emplace(std::make_pair(std::move(type), coreFactory)); + NSCAssert(typeToFactory.find(type) == typeToFactory.end(), @"A layer type can be registered only once."); + typeToFactory.emplace(std::make_pair(std::move(type), factory)); } - factories.emplace_back(std::move(factory)); } LayerPeerFactory* LayerManagerDarwin::getPeerFactory(const mbgl::style::LayerTypeInfo* typeInfo) { - assert(typeInfo); - for (const auto& factory: factories) { + for (const auto& factory: peerFactories) { if (factory->getCoreLayerFactory()->getTypeInfo() == typeInfo) { return factory.get(); } } - assert(false); return nullptr; } @@ -63,8 +74,17 @@ LayerFactory* LayerManagerDarwin::getFactory(const std::string& type) noexcept { } LayerFactory* LayerManagerDarwin::getFactory(const mbgl::style::LayerTypeInfo* info) noexcept { - LayerPeerFactory* peerFactory = getPeerFactory(info); - return (peerFactory != nullptr) ? peerFactory->getCoreLayerFactory() : nullptr; + if (LayerPeerFactory* peerFactory = getPeerFactory(info)) { + return peerFactory->getCoreLayerFactory(); + } + + for (const auto& factory: coreFactories) { + if (factory->getTypeInfo() == info) { + return factory.get(); + } + } + + return nullptr; } // static -- cgit v1.2.1