[core] Replace StyleLayerType with RTTI
This commit is contained in:
@@ -26,29 +26,6 @@ struct Faded {
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
enum class StyleLayerType : uint8_t {
|
||||
Unknown,
|
||||
Fill,
|
||||
Line,
|
||||
Circle,
|
||||
Symbol,
|
||||
Raster,
|
||||
Background
|
||||
};
|
||||
|
||||
MBGL_DEFINE_ENUM_CLASS(StyleLayerTypeClass, StyleLayerType, {
|
||||
{ StyleLayerType::Unknown, "unknown" },
|
||||
{ StyleLayerType::Fill, "fill" },
|
||||
{ StyleLayerType::Line, "line" },
|
||||
{ StyleLayerType::Circle, "circle" },
|
||||
{ StyleLayerType::Symbol, "symbol" },
|
||||
{ StyleLayerType::Raster, "raster" },
|
||||
{ StyleLayerType::Background, "background" },
|
||||
{ StyleLayerType(-1), "unknown" },
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
enum class SourceType : uint8_t {
|
||||
Vector,
|
||||
Raster,
|
||||
|
||||
@@ -120,8 +120,6 @@ void AnnotationManager::updateStyle(Style& style) {
|
||||
|
||||
std::unique_ptr<SymbolLayer> layer = std::make_unique<SymbolLayer>();
|
||||
layer->id = PointLayerID;
|
||||
layer->type = StyleLayerType::Symbol;
|
||||
|
||||
layer->source = SourceID;
|
||||
layer->sourceLayer = PointLayerID;
|
||||
layer->layout.icon.image = std::string("{sprite}");
|
||||
|
||||
@@ -30,7 +30,6 @@ void ShapeAnnotationImpl::updateStyle(Style& style) {
|
||||
type = ProjectedFeatureType::LineString;
|
||||
|
||||
std::unique_ptr<LineLayer> layer = std::make_unique<LineLayer>();
|
||||
layer->type = StyleLayerType::Line;
|
||||
layer->layout.join = JoinType::Round;
|
||||
|
||||
const LineAnnotationProperties& properties = shape.properties.get<LineAnnotationProperties>();
|
||||
@@ -48,7 +47,6 @@ void ShapeAnnotationImpl::updateStyle(Style& style) {
|
||||
type = ProjectedFeatureType::Polygon;
|
||||
|
||||
std::unique_ptr<FillLayer> layer = std::make_unique<FillLayer>();
|
||||
layer->type = StyleLayerType::Fill;
|
||||
|
||||
const FillAnnotationProperties& properties = shape.properties.get<FillAnnotationProperties>();
|
||||
layer->paint.opacity = properties.opacity;
|
||||
@@ -67,7 +65,7 @@ void ShapeAnnotationImpl::updateStyle(Style& style) {
|
||||
|
||||
std::unique_ptr<StyleLayer> layer = sourceLayer->clone();
|
||||
|
||||
type = layer->type == StyleLayerType::Line
|
||||
type = layer->is<LineLayer>()
|
||||
? ProjectedFeatureType::LineString
|
||||
: ProjectedFeatureType::Polygon;
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <mbgl/map/geometry_tile.hpp>
|
||||
#include <mbgl/style/style_layer.hpp>
|
||||
#include <mbgl/style/style_bucket_parameters.hpp>
|
||||
#include <mbgl/layer/background_layer.hpp>
|
||||
#include <mbgl/layer/symbol_layer.hpp>
|
||||
#include <mbgl/sprite/sprite_atlas.hpp>
|
||||
#include <mbgl/geometry/glyph_atlas.hpp>
|
||||
#include <mbgl/renderer/symbol_bucket.hpp>
|
||||
@@ -65,22 +67,18 @@ TileParseResult TileWorker::parsePendingLayers() {
|
||||
// dependencies.
|
||||
for (auto it = pending.begin(); it != pending.end();) {
|
||||
auto& layer = *it->first;
|
||||
auto& bucket = it->second;
|
||||
assert(bucket);
|
||||
auto bucket = dynamic_cast<SymbolBucket*>(it->second.get());
|
||||
assert(bucket); // Only symbol layers can be pending, so the dynamic cast should never fail.
|
||||
|
||||
if (layer.type == StyleLayerType::Symbol) {
|
||||
auto symbolBucket = dynamic_cast<SymbolBucket*>(bucket.get());
|
||||
if (!symbolBucket->needsDependencies(glyphStore, spriteStore)) {
|
||||
const SymbolLayer* symbolLayer = dynamic_cast<const SymbolLayer*>(&layer);
|
||||
symbolBucket->addFeatures(reinterpret_cast<uintptr_t>(this),
|
||||
*symbolLayer->spriteAtlas,
|
||||
glyphAtlas,
|
||||
glyphStore,
|
||||
*collisionTile);
|
||||
insertBucket(layer.bucketName(), std::move(bucket));
|
||||
pending.erase(it++);
|
||||
continue;
|
||||
}
|
||||
if (!bucket->needsDependencies(glyphStore, spriteStore)) {
|
||||
bucket->addFeatures(reinterpret_cast<uintptr_t>(this),
|
||||
*layer.spriteAtlas,
|
||||
glyphAtlas,
|
||||
glyphStore,
|
||||
*collisionTile);
|
||||
insertBucket(layer.bucketName(), std::move(it->second));
|
||||
pending.erase(it++);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Advance the iterator here; we're skipping this when erasing an element from this list.
|
||||
@@ -112,7 +110,7 @@ void TileWorker::parseLayer(const StyleLayer* layer, const GeometryTile& geometr
|
||||
return;
|
||||
|
||||
// Background is a special case.
|
||||
if (layer->type == StyleLayerType::Background)
|
||||
if (layer->is<BackgroundLayer>())
|
||||
return;
|
||||
|
||||
// Skip this bucket if we are to not render this
|
||||
@@ -145,9 +143,9 @@ void TileWorker::parseLayer(const StyleLayer* layer, const GeometryTile& geometr
|
||||
|
||||
std::unique_ptr<Bucket> bucket = layer->createBucket(parameters);
|
||||
|
||||
if (layer->type == StyleLayerType::Symbol && partialParse) {
|
||||
if (layer->is<SymbolLayer>() && partialParse) {
|
||||
// We cannot parse this bucket yet. Instead, we're saving it for later.
|
||||
pending.emplace_back(layer, std::move(bucket));
|
||||
pending.emplace_back(layer->as<SymbolLayer>(), std::move(bucket));
|
||||
} else {
|
||||
insertBucket(layer->bucketName(), std::move(bucket));
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ class GlyphAtlas;
|
||||
class GlyphStore;
|
||||
class Bucket;
|
||||
class StyleLayer;
|
||||
class SymbolLayer;
|
||||
|
||||
// We're using this class to shuttle the resulting buckets from the worker thread to the MapContext
|
||||
// thread. This class is movable-only because the vector contains movable-only value elements.
|
||||
@@ -73,7 +74,7 @@ private:
|
||||
|
||||
// Contains buckets that we couldn't parse so far due to missing resources.
|
||||
// They will be attempted on subsequent parses.
|
||||
std::list<std::pair<const StyleLayer*, std::unique_ptr<Bucket>>> pending;
|
||||
std::list<std::pair<const SymbolLayer*, std::unique_ptr<Bucket>>> pending;
|
||||
|
||||
// Temporary holder
|
||||
TileParseResultBuckets result;
|
||||
|
||||
@@ -23,7 +23,7 @@ void CircleBucket::render(Painter& painter,
|
||||
const StyleLayer& layer,
|
||||
const TileID& id,
|
||||
const mat4& matrix) {
|
||||
painter.renderCircle(*this, dynamic_cast<const CircleLayer&>(layer), id, matrix);
|
||||
painter.renderCircle(*this, *layer.as<CircleLayer>(), id, matrix);
|
||||
}
|
||||
|
||||
bool CircleBucket::hasData() const {
|
||||
|
||||
@@ -199,7 +199,7 @@ void FillBucket::render(Painter& painter,
|
||||
const StyleLayer& layer,
|
||||
const TileID& id,
|
||||
const mat4& matrix) {
|
||||
painter.renderFill(*this, dynamic_cast<const FillLayer&>(layer), id, matrix);
|
||||
painter.renderFill(*this, *layer.as<FillLayer>(), id, matrix);
|
||||
}
|
||||
|
||||
bool FillBucket::hasData() const {
|
||||
|
||||
@@ -390,7 +390,7 @@ void LineBucket::render(Painter& painter,
|
||||
const StyleLayer& layer,
|
||||
const TileID& id,
|
||||
const mat4& matrix) {
|
||||
painter.renderLine(*this, dynamic_cast<const LineLayer&>(layer), id, matrix);
|
||||
painter.renderLine(*this, *layer.as<LineLayer>(), id, matrix);
|
||||
}
|
||||
|
||||
bool LineBucket::hasData() const {
|
||||
|
||||
@@ -233,7 +233,7 @@ void Painter::renderPass(RenderPass pass_,
|
||||
}
|
||||
} else {
|
||||
MBGL_DEBUG_GROUP("background");
|
||||
renderBackground(dynamic_cast<const BackgroundLayer&>(item.layer));
|
||||
renderBackground(*item.layer.template as<BackgroundLayer>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ void RasterBucket::render(Painter& painter,
|
||||
const StyleLayer& layer,
|
||||
const TileID& id,
|
||||
const mat4& matrix) {
|
||||
painter.renderRaster(*this, dynamic_cast<const RasterLayer&>(layer), id, matrix);
|
||||
painter.renderRaster(*this, *layer.as<RasterLayer>(), id, matrix);
|
||||
}
|
||||
|
||||
bool RasterBucket::setImage(PremultipliedImage image) {
|
||||
|
||||
@@ -78,7 +78,7 @@ void SymbolBucket::render(Painter& painter,
|
||||
const StyleLayer& layer,
|
||||
const TileID& id,
|
||||
const mat4& matrix) {
|
||||
painter.renderSymbol(*this, dynamic_cast<const SymbolLayer&>(layer), id, matrix);
|
||||
painter.renderSymbol(*this, *layer.as<SymbolLayer>(), id, matrix);
|
||||
}
|
||||
|
||||
bool SymbolBucket::hasData() const { return hasTextData() || hasIconData() || !symbolInstances.empty(); }
|
||||
|
||||
@@ -101,7 +101,7 @@ StyleLayer* Style::getLayer(const std::string& id) const {
|
||||
}
|
||||
|
||||
void Style::addLayer(std::unique_ptr<StyleLayer> layer) {
|
||||
if (SymbolLayer* symbolLayer = dynamic_cast<SymbolLayer*>(layer.get())) {
|
||||
if (SymbolLayer* symbolLayer = layer->as<SymbolLayer>()) {
|
||||
if (!symbolLayer->spriteAtlas) {
|
||||
symbolLayer->spriteAtlas = spriteAtlas.get();
|
||||
}
|
||||
@@ -234,7 +234,7 @@ RenderData Style::getRenderData() const {
|
||||
if (layer->visibility == VisibilityType::None)
|
||||
continue;
|
||||
|
||||
if (const BackgroundLayer* background = dynamic_cast<const BackgroundLayer*>(layer.get())) {
|
||||
if (const BackgroundLayer* background = layer->as<BackgroundLayer>()) {
|
||||
if (background->paint.pattern.value.from.empty()) {
|
||||
// This is a solid background. We can use glClear().
|
||||
result.backgroundColor = background->paint.color;
|
||||
@@ -262,7 +262,7 @@ RenderData Style::getRenderData() const {
|
||||
// We're not clipping symbol layers, so when we have both parents and children of symbol
|
||||
// layers, we drop all children in favor of their parent to avoid duplicate labels.
|
||||
// See https://github.com/mapbox/mapbox-gl-native/issues/2482
|
||||
if (layer->type == StyleLayerType::Symbol) {
|
||||
if (layer->is<SymbolLayer>()) {
|
||||
bool skip = false;
|
||||
// Look back through the buckets we decided to render to find out whether there is
|
||||
// already a bucket from this layer that is a parent of this tile. Tiles are ordered
|
||||
|
||||
@@ -1,32 +1,7 @@
|
||||
#include <mbgl/style/style_layer.hpp>
|
||||
#include <mbgl/layer/fill_layer.hpp>
|
||||
#include <mbgl/layer/line_layer.hpp>
|
||||
#include <mbgl/layer/circle_layer.hpp>
|
||||
#include <mbgl/layer/symbol_layer.hpp>
|
||||
#include <mbgl/layer/raster_layer.hpp>
|
||||
#include <mbgl/layer/background_layer.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> StyleLayer::create(StyleLayerType type) {
|
||||
switch (type) {
|
||||
case StyleLayerType::Fill:
|
||||
return std::make_unique<FillLayer>();
|
||||
case StyleLayerType::Line:
|
||||
return std::make_unique<LineLayer>();
|
||||
case StyleLayerType::Circle:
|
||||
return std::make_unique<CircleLayer>();
|
||||
case StyleLayerType::Symbol:
|
||||
return std::make_unique<SymbolLayer>();
|
||||
case StyleLayerType::Raster:
|
||||
return std::make_unique<RasterLayer>();
|
||||
case StyleLayerType::Background:
|
||||
return std::make_unique<BackgroundLayer>();
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const std::string& StyleLayer::bucketName() const {
|
||||
return ref.empty() ? id : ref;
|
||||
}
|
||||
|
||||
@@ -23,11 +23,18 @@ using JSVal = rapidjson::Value;
|
||||
|
||||
class StyleLayer {
|
||||
public:
|
||||
static std::unique_ptr<StyleLayer> create(StyleLayerType);
|
||||
virtual std::unique_ptr<StyleLayer> clone() const = 0;
|
||||
|
||||
virtual ~StyleLayer() = default;
|
||||
|
||||
// Check whether this layer is of the given subtype.
|
||||
template <class T> bool is() const { return dynamic_cast<const T*>(this); }
|
||||
|
||||
// Dynamically cast this layer to the given subtype.
|
||||
template <class T> T* as() { return dynamic_cast< T*>(this); }
|
||||
template <class T> const T* as() const { return dynamic_cast<const T*>(this); }
|
||||
|
||||
// Create a copy of this layer.
|
||||
virtual std::unique_ptr<StyleLayer> clone() const = 0;
|
||||
|
||||
virtual void parseLayout(const JSVal& value) = 0;
|
||||
virtual void parsePaints(const JSVal& value) = 0;
|
||||
|
||||
@@ -47,7 +54,6 @@ public:
|
||||
bool hasRenderPass(RenderPass) const;
|
||||
|
||||
public:
|
||||
StyleLayerType type;
|
||||
std::string id;
|
||||
std::string ref;
|
||||
std::string source;
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
#include <mbgl/style/style_parser.hpp>
|
||||
#include <mbgl/layer/fill_layer.hpp>
|
||||
#include <mbgl/layer/line_layer.hpp>
|
||||
#include <mbgl/layer/circle_layer.hpp>
|
||||
#include <mbgl/layer/symbol_layer.hpp>
|
||||
#include <mbgl/layer/raster_layer.hpp>
|
||||
#include <mbgl/layer/background_layer.hpp>
|
||||
|
||||
#include <mbgl/platform/log.hpp>
|
||||
|
||||
@@ -213,16 +219,25 @@ void StyleParser::parseLayer(const std::string& id, const JSVal& value, std::uni
|
||||
}
|
||||
|
||||
std::string type { typeVal.GetString(), typeVal.GetStringLength() };
|
||||
StyleLayerType typeClass = StyleLayerTypeClass(type);
|
||||
layer = StyleLayer::create(typeClass);
|
||||
|
||||
if (!layer) {
|
||||
if (type == "fill") {
|
||||
layer = std::make_unique<FillLayer>();
|
||||
} else if (type == "line") {
|
||||
layer = std::make_unique<LineLayer>();
|
||||
} else if (type == "circle") {
|
||||
layer = std::make_unique<CircleLayer>();
|
||||
} else if (type == "symbol") {
|
||||
layer = std::make_unique<SymbolLayer>();
|
||||
} else if (type == "raster") {
|
||||
layer = std::make_unique<RasterLayer>();
|
||||
} else if (type == "background") {
|
||||
layer = std::make_unique<BackgroundLayer>();
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "unknown type '%s' for layer '%s'", type.c_str(), id.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
layer->id = id;
|
||||
layer->type = typeClass;
|
||||
|
||||
if (value.HasMember("source")) {
|
||||
const JSVal& value_source = value["source"];
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#include <iostream>
|
||||
#include "../fixtures/util.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <mbgl/style/types.hpp>
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
TEST(Enums, StyleLayerType) {
|
||||
ASSERT_EQ(StyleLayerType::Unknown, StyleLayerTypeClass("unknown"));
|
||||
ASSERT_EQ(StyleLayerType::Fill, StyleLayerTypeClass("fill"));
|
||||
ASSERT_EQ(StyleLayerType::Line, StyleLayerTypeClass("line"));
|
||||
ASSERT_EQ(StyleLayerType::Symbol, StyleLayerTypeClass("symbol"));
|
||||
ASSERT_EQ(StyleLayerType::Raster, StyleLayerTypeClass("raster"));
|
||||
ASSERT_EQ(StyleLayerType::Background, StyleLayerTypeClass("background"));
|
||||
|
||||
ASSERT_EQ(StyleLayerType::Unknown, StyleLayerTypeClass(StyleLayerType::Unknown));
|
||||
ASSERT_EQ(StyleLayerType::Fill, StyleLayerTypeClass(StyleLayerType::Fill));
|
||||
ASSERT_EQ(StyleLayerType::Line, StyleLayerTypeClass(StyleLayerType::Line));
|
||||
ASSERT_EQ(StyleLayerType::Symbol, StyleLayerTypeClass(StyleLayerType::Symbol));
|
||||
ASSERT_EQ(StyleLayerType::Raster, StyleLayerTypeClass(StyleLayerType::Raster));
|
||||
ASSERT_EQ(StyleLayerType::Background, StyleLayerTypeClass(StyleLayerType::Background));
|
||||
|
||||
ASSERT_EQ(StyleLayerTypeClass(StyleLayerType::Unknown), StyleLayerTypeClass(StyleLayerType::Unknown));
|
||||
ASSERT_EQ(StyleLayerTypeClass(StyleLayerType::Fill), StyleLayerTypeClass(StyleLayerType::Fill));
|
||||
ASSERT_EQ(StyleLayerTypeClass(StyleLayerType::Line), StyleLayerTypeClass(StyleLayerType::Line));
|
||||
ASSERT_EQ(StyleLayerTypeClass(StyleLayerType::Symbol), StyleLayerTypeClass(StyleLayerType::Symbol));
|
||||
ASSERT_EQ(StyleLayerTypeClass(StyleLayerType::Raster), StyleLayerTypeClass(StyleLayerType::Raster));
|
||||
ASSERT_EQ(StyleLayerTypeClass(StyleLayerType::Background), StyleLayerTypeClass(StyleLayerType::Background));
|
||||
|
||||
ASSERT_FALSE(StyleLayerTypeClass("").valid());
|
||||
ASSERT_FALSE(StyleLayerTypeClass("Backgrund").valid());
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
using namespace mbgl;
|
||||
|
||||
TEST(StyleLayer, Create) {
|
||||
std::unique_ptr<StyleLayer> layer = StyleLayer::create(StyleLayerType::Background);
|
||||
std::unique_ptr<StyleLayer> layer = std::make_unique<BackgroundLayer>();
|
||||
EXPECT_TRUE(reinterpret_cast<BackgroundLayer*>(layer.get()));
|
||||
}
|
||||
|
||||
TEST(StyleLayer, Clone) {
|
||||
std::unique_ptr<StyleLayer> layer = StyleLayer::create(StyleLayerType::Background);
|
||||
std::unique_ptr<StyleLayer> layer = std::make_unique<BackgroundLayer>();
|
||||
std::unique_ptr<StyleLayer> clone = layer->clone();
|
||||
EXPECT_NE(layer.get(), clone.get());
|
||||
EXPECT_TRUE(reinterpret_cast<BackgroundLayer*>(layer.get()));
|
||||
|
||||
@@ -50,7 +50,6 @@
|
||||
'miscellaneous/binpack.cpp',
|
||||
'miscellaneous/bilinear.cpp',
|
||||
'miscellaneous/comparisons.cpp',
|
||||
'miscellaneous/enums.cpp',
|
||||
'miscellaneous/functions.cpp',
|
||||
'miscellaneous/geo.cpp',
|
||||
'miscellaneous/image.cpp',
|
||||
|
||||
Reference in New Issue
Block a user