[core] layermanager folder

Move `LayerManager` and `LayerFactory` abstract classes to a dedicated folder.
This commit is contained in:
Mikhail Pozdnyakov
2018-11-30 12:10:33 +02:00
parent 8bc362e3a2
commit 92352de87d
30 changed files with 204 additions and 152 deletions

View File

@@ -94,6 +94,12 @@ src/mbgl/gl/vertex_array.hpp
src/mbgl/gl/vertex_array_extension.hpp
src/mbgl/gl/vertex_buffer.hpp
# layermanager
include/mbgl/layermanager/layer_factory.hpp
include/mbgl/layermanager/layer_manager.hpp
src/mbgl/layermanager/layer_factory.cpp
src/mbgl/layermanager/layer_manager.cpp
# layout
src/mbgl/layout/clip_lines.cpp
src/mbgl/layout/clip_lines.hpp

View File

@@ -0,0 +1,29 @@
#pragma once
#include <mbgl/style/layer.hpp>
namespace mbgl {
class RenderLayer;
/**
* @brief The LayerFactory abstract class
*
* This class is responsible for creation of the layer objects that belong to a concrete layer type.
*/
class LayerFactory {
public:
virtual ~LayerFactory() = default;
/// Returns the layer type data.
virtual const style::LayerTypeInfo* getTypeInfo() const noexcept = 0;
/// Returns a new Layer instance on success call; returns `nulltptr` otherwise.
virtual std::unique_ptr<style::Layer> createLayer(const std::string& id, const style::conversion::Convertible& value) noexcept = 0;
/// Returns a new RenderLayer instance on success call; returns `nulltptr` otherwise.
virtual std::unique_ptr<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) noexcept = 0;
protected:
optional<std::string> getSource(const style::conversion::Convertible& value) const noexcept;
bool initSourceLayerAndFilter(style::Layer*, const style::conversion::Convertible& value) const noexcept;
};
} // namespace mbgl

View File

@@ -0,0 +1,56 @@
#pragma once
#include <mbgl/style/layer.hpp>
namespace mbgl {
class LayerFactory;
class RenderLayer;
/**
* @brief A singleton class responsible for creating layer instances.
*
* The LayerManager has implementation per platform. The LayerManager implementation
* defines what layer types are available and it can also disable annotations.
*
* Linker excludes the unreachable code for the disabled annotations and layers
* from the binaries, significantly reducing their size.
*/
class LayerManager {
public:
/**
* @brief A singleton getter.
*
* @return LayerManager*
*/
static LayerManager* get() noexcept;
/// Returns a new Layer instance on success call; returns `nulltptr` otherwise.
std::unique_ptr<style::Layer> createLayer(const std::string& type, const std::string& id,
const style::conversion::Convertible& value, style::conversion::Error& error) noexcept;
/// Returns a new RenderLayer instance on success call; returns `nulltptr` otherwise.
std::unique_ptr<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) noexcept;
/**
* @brief a build-time flag to enable/disable annotations in mapbox-gl-native core.
*
* At the moment, the annotations implementation in core is creating concrete
* layer instances apart from LayerManager/LayerFactory code path.
*
* So, annotations must be disabled if the LayerManager implementation does
* not provide line, fill or symbol layers (those, used by the annotations
* implementation).
*
* Note: in future, annotations implemantation will be moved from the core to platform
* SDK (see https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation)
* and this flag won't be needed any more.
*/
static const bool annotationsEnabled;
protected:
virtual ~LayerManager() = default;
virtual LayerFactory* getFactory(const std::string& type) noexcept = 0;
virtual LayerFactory* getFactory(const style::LayerTypeInfo*) noexcept = 0;
};
} // namespace mbgl

View File

@@ -126,74 +126,4 @@ protected:
};
} // namespace style
class RenderLayer;
// TODO: The following classes shall not be here. Move layer factories and manager to a dedicated folder.
/**
* @brief The LayerFactory abstract class
*
* This class is responsible for creation of the layer objects that belong to a concrete layer type.
*/
class LayerFactory {
public:
virtual ~LayerFactory() = default;
/// Returns the layer type data.
virtual const style::LayerTypeInfo* getTypeInfo() const noexcept = 0;
/// Returns a new Layer instance on success call; returns `nulltptr` otherwise.
virtual std::unique_ptr<style::Layer> createLayer(const std::string& id, const style::conversion::Convertible& value) noexcept = 0;
/// Returns a new RenderLayer instance on success call; returns `nulltptr` otherwise.
virtual std::unique_ptr<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) noexcept = 0;
protected:
optional<std::string> getSource(const style::conversion::Convertible& value) const noexcept;
bool initSourceLayerAndFilter(style::Layer*, const style::conversion::Convertible& value) const noexcept;
};
/**
* @brief A singleton class responsible for creating layer instances.
*
* The LayerManager has implementation per platform. The LayerManager implementation
* defines what layer types are available and it can also disable annotations.
*
* Linker excludes the unreachable code for the disabled annotations and layers
* from the binaries, significantly reducing their size.
*/
class LayerManager {
public:
/**
* @brief A singleton getter.
*
* @return LayerManager*
*/
static LayerManager* get() noexcept;
/// Returns a new Layer instance on success call; returns `nulltptr` otherwise.
std::unique_ptr<style::Layer> createLayer(const std::string& type, const std::string& id,
const style::conversion::Convertible& value, style::conversion::Error& error) noexcept;
/// Returns a new RenderLayer instance on success call; returns `nulltptr` otherwise.
std::unique_ptr<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) noexcept;
/**
* @brief a build-time flag to enable/disable annotations in mapbox-gl-native core.
*
* At the moment, the annotations implementation in core is creating concrete
* layer instances apart from LayerManager/LayerFactory code path.
*
* So, annotations must be disabled if the LayerManager implementation does
* not provide line, fill or symbol layers (those, used by the annotations
* implementation).
*
* Note: in future, annotations implemantation will be moved from the core to platform
* SDK (see https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation)
* and this flag won't be needed any more.
*/
static const bool annotationsEnabled;
protected:
virtual ~LayerManager() = default;
virtual LayerFactory* getFactory(const std::string& type) noexcept = 0;
virtual LayerFactory* getFactory(const style::LayerTypeInfo*) noexcept = 0;
};
} // namespace mbgl

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -1,5 +1,6 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <array>

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -2,12 +2,12 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/color_ramp_property_value.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -7,6 +7,7 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
<% if (type === 'heatmap' || type === 'line') { -%>
#include <mbgl/style/color_ramp_property_value.hpp>
<% } -%>
@@ -14,7 +15,6 @@
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
<% if (type === 'line' || type === 'symbol') { -%>

View File

@@ -2,12 +2,12 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/color_ramp_property_value.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
#include <vector>

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {

View File

@@ -2,11 +2,11 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/util/color.hpp>
#include <vector>

View File

@@ -1,5 +1,6 @@
#pragma once
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/style/layer.hpp>
#include "../../gson/json_array.hpp"

View File

@@ -1,10 +1,9 @@
#pragma once
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include "layer.hpp"
#include <jni/jni.hpp>

View File

@@ -2,6 +2,7 @@
#import "MGLStyleLayer_Private.h"
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/style/layer.hpp>
#include <map>

View File

@@ -3,6 +3,7 @@
#import "MGLStyleLayer.h"
#import "MGLStyleValue_Private.h"
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/layer.hpp>
NS_ASSUME_NONNULL_BEGIN

View File

@@ -1,14 +1,14 @@
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/renderer/layers/render_background_layer.hpp>
#include <mbgl/renderer/layers/render_circle_layer.hpp>
#include <mbgl/renderer/layers/render_custom_layer.hpp>
#include <mbgl/renderer/layers/render_fill_extrusion_layer.hpp>
#include <mbgl/renderer/layers/render_fill_layer.hpp>
#include <mbgl/renderer/layers/render_hillshade_layer.hpp>
#include <mbgl/renderer/layers/render_line_layer.hpp>
#include <mbgl/renderer/layers/render_raster_layer.hpp>
#include <mbgl/renderer/layers/render_symbol_layer.hpp>
#include <mbgl/renderer/layers/render_heatmap_layer.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/circle_layer.hpp>
#include <mbgl/style/layers/custom_layer.hpp>
#include <mbgl/style/layers/fill_extrusion_layer.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
#include <mbgl/style/layers/heatmap_layer.hpp>
#include <mbgl/style/layers/hillshade_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
#include <map>
#include <memory>

View File

@@ -4,6 +4,7 @@
#include <mbgl/annotation/symbol_annotation_impl.hpp>
#include <mbgl/annotation/line_annotation_impl.hpp>
#include <mbgl/annotation/fill_annotation_impl.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_impl.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>

View File

@@ -0,0 +1,47 @@
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/layer.hpp>
namespace mbgl {
optional<std::string> LayerFactory::getSource(const style::conversion::Convertible& value) const noexcept {
auto sourceValue = objectMember(value, "source");
if (!sourceValue) {
return nullopt;
}
optional<std::string> source = toString(*sourceValue);
if (!source) {
return nullopt;
}
return source;
}
bool LayerFactory::initSourceLayerAndFilter(style::Layer* layer, const style::conversion::Convertible& value) const noexcept {
auto sourceLayerValue = objectMember(value, "source-layer");
if (sourceLayerValue) {
optional<std::string> sourceLayer = toString(*sourceLayerValue);
if (!sourceLayer) {
return false;
}
layer->setSourceLayer(*sourceLayer);
}
auto filterValue = objectMember(value, "filter");
if (filterValue) {
style::conversion::Error error;
optional<style::Filter> filter = style::conversion::convert<style::Filter>(*filterValue, error);
if (!filter) {
return false;
}
layer->setFilter(*filter);
}
return true;
}
} // namespace mbgl

View File

@@ -0,0 +1,31 @@
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/layermanager/layer_factory.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/conversion_impl.hpp>
namespace mbgl {
std::unique_ptr<style::Layer> LayerManager::createLayer(
const std::string& type, const std::string& id,
const style::conversion::Convertible& value, style::conversion::Error& error) noexcept {
if (LayerFactory* factory = getFactory(type)) {
auto layer = factory->createLayer(id, value);
if (!layer) {
error.message = "Error parsing a layer of type: " + type;
}
return layer;
}
error.message = "Unsupported layer type: " + type;
return nullptr;
}
std::unique_ptr<RenderLayer> LayerManager::createRenderLayer(Immutable<style::Layer::Impl> impl) noexcept {
LayerFactory* factory = getFactory(impl->getTypeInfo());
assert(factory);
return factory->createRenderLayer(std::move(impl));
}
} // namespace mbgl

View File

@@ -3,6 +3,7 @@
#include <mbgl/map/transform.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/annotation/annotation_manager.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/style/style_impl.hpp>
#include <mbgl/style/observer.hpp>
#include <mbgl/renderer/update_parameters.hpp>

View File

@@ -1,4 +1,6 @@
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/renderer/renderer_impl.hpp>
#include <mbgl/renderer/backend_scope.hpp>
#include <mbgl/annotation/annotation_manager.hpp>

View File

@@ -1,4 +1,5 @@
#include <mbgl/annotation/annotation_manager.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/renderer/renderer_impl.hpp>
#include <mbgl/renderer/renderer_backend.hpp>
#include <mbgl/renderer/renderer_observer.hpp>

View File

@@ -3,6 +3,8 @@
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
namespace mbgl {
namespace style {
namespace conversion {

View File

@@ -110,63 +110,4 @@ const LayerTypeInfo* Layer::getTypeInfo() const noexcept {
}
} // namespace style
optional<std::string> LayerFactory::getSource(const style::conversion::Convertible& value) const noexcept {
auto sourceValue = objectMember(value, "source");
if (!sourceValue) {
return nullopt;
}
optional<std::string> source = toString(*sourceValue);
if (!source) {
return nullopt;
}
return source;
}
bool LayerFactory::initSourceLayerAndFilter(style::Layer* layer, const style::conversion::Convertible& value) const noexcept {
auto sourceLayerValue = objectMember(value, "source-layer");
if (sourceLayerValue) {
optional<std::string> sourceLayer = toString(*sourceLayerValue);
if (!sourceLayer) {
return false;
}
layer->setSourceLayer(*sourceLayer);
}
auto filterValue = objectMember(value, "filter");
if (filterValue) {
style::conversion::Error error;
optional<style::Filter> filter = style::conversion::convert<style::Filter>(*filterValue, error);
if (!filter) {
return false;
}
layer->setFilter(*filter);
}
return true;
}
std::unique_ptr<style::Layer> LayerManager::createLayer(
const std::string& type, const std::string& id,
const style::conversion::Convertible& value, style::conversion::Error& error) noexcept {
if (LayerFactory* factory = getFactory(type)) {
auto layer = factory->createLayer(id, value);
if (!layer) {
error.message = "Error parsing a layer of type: " + type;
}
return layer;
}
error.message = "Unsupported layer type: " + type;
return nullptr;
}
std::unique_ptr<RenderLayer> LayerManager::createRenderLayer(Immutable<style::Layer::Impl> impl) noexcept {
LayerFactory* factory = getFactory(impl->getTypeInfo());
assert(factory);
return factory->createRenderLayer(std::move(impl));
}
} // namespace mbgl

View File

@@ -1,6 +1,7 @@
#include <mbgl/tile/geometry_tile_worker.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/layout/symbol_layout.hpp>
#include <mbgl/layout/pattern_layout.hpp>
#include <mbgl/renderer/bucket_parameters.hpp>

View File

@@ -1,5 +1,6 @@
#include <mbgl/test/util.hpp>
#include <mbgl/layermanager/layer_manager.hpp>
#include <mbgl/renderer/group_by_layout.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/style/layers/background_layer.hpp>