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

[core] Add Layer::serialize() method #16231

Merged
merged 8 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

This is a back porting from GL JS [#9333](https://github.com/mapbox/mapbox-gl-js/pull/9333)

- [core] Add Layer::serialize() method ([#16231](https://github.com/mapbox/mapbox-gl-native/pull/16231))

New method allows serialization of a layer into a Value type, including all modifications done via runtime style API. New method is also an enabler for Style object serialization (sources, layers, etc).

## maps-v1.2.0 (2020.02-release-vanillashake)

### ✨ New features
Expand Down
20 changes: 9 additions & 11 deletions include/mbgl/style/conversion_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,12 @@ struct ValueFactory<ColorRampPropertyValue> {

template <>
struct ValueFactory<TransitionOptions> {
static Value make(const TransitionOptions& value) {
return mapbox::base::ValueArray{
{std::chrono::duration_cast<std::chrono::milliseconds>(value.duration.value_or(mbgl::Duration::zero()))
.count(),
std::chrono::duration_cast<std::chrono::milliseconds>(value.delay.value_or(mbgl::Duration::zero()))
.count(),
value.enablePlacementTransitions}};
}
static Value make(const TransitionOptions& value) { return value.serialize(); }
};

template <>
struct ValueFactory<Color> {
static Value make(const Color& color) { return color.toObject(); }
static Value make(const Color& color) { return color.serialize(); }
};

template <typename T>
Expand Down Expand Up @@ -358,19 +351,24 @@ Value makeValue(T&& arg) {
template <typename T>
StyleProperty makeStyleProperty(const PropertyValue<T>& value) {
return value.match([](const Undefined&) -> StyleProperty { return {}; },
[](const T& t) -> StyleProperty {
return {makeValue(t), StyleProperty::Kind::Constant};
[](const Color& c) -> StyleProperty {
return {makeValue(c), StyleProperty::Kind::Expression};
},
[](const PropertyExpression<T>& fn) -> StyleProperty {
return {fn.getExpression().serialize(), StyleProperty::Kind::Expression};
},
[](const auto& t) -> StyleProperty {
return {makeValue(t), StyleProperty::Kind::Constant};
});
}

inline StyleProperty makeStyleProperty(const TransitionOptions& value) {
if (!value.isDefined()) return {};
return {makeValue(value), StyleProperty::Kind::Transition};
}

inline StyleProperty makeStyleProperty(const ColorRampPropertyValue& value) {
if (value.isUndefined()) return {};
return {makeValue(value), StyleProperty::Kind::Expression};
}

Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Filter {

bool operator()(const expression::EvaluationContext& context) const;

operator bool() const { return expression || legacyFilter; }

friend bool operator==(const Filter& lhs, const Filter& rhs) {
if (!lhs.expression || !rhs.expression) {
return lhs.expression == rhs.expression;
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Layer {
optional<conversion::Error> setVisibility(const conversion::Convertible& value);

virtual StyleProperty getProperty(const std::string&) const = 0;
virtual Value serialize() const;

// Private implementation
// TODO : We should not have public mutable data members.
Expand All @@ -140,6 +141,7 @@ class Layer {

protected:
virtual Mutable<Impl> mutableBaseImpl() const = 0;
void serializeProperty(Value&, const StyleProperty&, const char* propertyName, bool isPaint) const;

LayerObserver* observer;
mapbox::base::WeakPtrFactory<Layer> weakFactory {this};
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/background_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BackgroundLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Paint properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/circle_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CircleLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Paint properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/fill_extrusion_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FillExtrusionLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Paint properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/fill_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FillLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Layout properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/heatmap_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HeatmapLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Paint properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/hillshade_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class HillshadeLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Paint properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/layer.hpp.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public:
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

<% if (layoutProperties.length) { -%>
// Layout properties
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/line_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LineLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Layout properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/raster_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RasterLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Paint properties

Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/symbol_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SymbolLayer : public Layer {
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

// Layout properties

Expand Down
13 changes: 13 additions & 0 deletions include/mbgl/style/transition_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/optional.hpp>

#include <mapbox/value.hpp>

namespace mbgl {
namespace style {

Expand Down Expand Up @@ -30,6 +32,17 @@ class TransitionOptions {
bool isDefined() const {
return duration || delay;
}

mapbox::base::Value serialize() const {
mapbox::base::ValueObject result;
if (duration) {
result.emplace("duration", std::chrono::duration_cast<std::chrono::milliseconds>(*duration).count());
}
if (delay) {
result.emplace("delay", std::chrono::duration_cast<std::chrono::milliseconds>(*delay).count());
}
return result;
}
};

} // namespace style
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/util/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Color {
std::string stringify() const;
std::array<double, 4> toArray() const;
mbgl::Value toObject() const;
mbgl::Value serialize() const;
};

inline bool operator==(const Color& colorA, const Color& colorB) {
Expand Down
6 changes: 3 additions & 3 deletions metrics/binary-size/linux-gcc8/metrics.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
[
"mbgl-glfw",
"/tmp/attach/install/linux-gcc8-release/bin/mbgl-glfw",
7598440
7672168
],
[
"mbgl-offline",
"/tmp/attach/install/linux-gcc8-release/bin/mbgl-offline",
6684904
6758632
],
[
"mbgl-render",
"/tmp/attach/install/linux-gcc8-release/bin/mbgl-render",
7483752
7549288
]
]
}
11 changes: 1 addition & 10 deletions src/mbgl/style/expression/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,7 @@ Value ValueConverter<mbgl::Value>::toExpressionValue(const mbgl::Value& value) {

mbgl::Value ValueConverter<mbgl::Value>::fromExpressionValue(const Value& value) {
return value.match(
[&](const Color& color) -> mbgl::Value {
std::array<double, 4> array = color.toArray();
return std::vector<mbgl::Value>{
std::string("rgba"),
array[0],
array[1],
array[2],
array[3],
};
},
[&](const Color& color) -> mbgl::Value { return color.serialize(); },
[&](const Collator&) -> mbgl::Value {
// fromExpressionValue can't be used for Collator values,
// because they have no meaningful representation as an mbgl::Value
Expand Down
48 changes: 48 additions & 0 deletions src/mbgl/style/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,54 @@ void Layer::setMaxZoom(float maxZoom) {
observer->onLayerChanged(*this);
}

Value Layer::serialize() const {
mapbox::base::ValueObject result;
result.emplace(std::make_pair<std::string, Value>("id", getID()));
result.emplace(std::make_pair<std::string, Value>("type", Layer::getTypeInfo()->type));

auto source = getSourceID();
if (!source.empty()) {
result.emplace(std::make_pair<std::string, Value>("source", std::move(source)));
}

auto sourceLayer = getSourceLayer();
if (!sourceLayer.empty()) {
result.emplace(std::make_pair<std::string, Value>("source-layer", std::move(sourceLayer)));
}

if (getFilter()) {
result.emplace(std::make_pair<std::string, Value>("filter", getFilter().serialize()));
}

if (getMinZoom() != -std::numeric_limits<float>::infinity()) {
result.emplace(std::make_pair<std::string, Value>("minzoom", getMinZoom()));
}

if (getMaxZoom() != std::numeric_limits<float>::infinity()) {
result.emplace(std::make_pair<std::string, Value>("maxzoom", getMaxZoom()));
}

if (getVisibility() == VisibilityType::None) {
result["layout"] = mapbox::base::ValueObject{std::make_pair<std::string, Value>("visibility", "none")};
}

return result;
}

void Layer::serializeProperty(Value& out, const StyleProperty& property, const char* propertyName, bool isPaint) const {
assert(out.getObject());
auto& object = *(out.getObject());
std::string propertyType = isPaint ? "paint" : "layout";
auto it = object.find(propertyType);
auto pair = std::make_pair<std::string, Value>(std::string(propertyName), Value{property.getValue()});
if (it != object.end()) {
assert(it->second.getObject());
it->second.getObject()->emplace(std::move(pair));
} else {
object[propertyType] = mapbox::base::ValueObject{{std::move(pair)}};
}
}

void Layer::setObserver(LayerObserver* observer_) {
observer = observer_ ? observer_ : &nullObserver;
}
Expand Down
61 changes: 41 additions & 20 deletions src/mbgl/style/layers/background_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ using namespace conversion;

namespace {

constexpr uint8_t kPaintPropertyCount = 6u;

enum class Property : uint8_t {
BackgroundColor,
BackgroundOpacity,
Expand All @@ -169,8 +171,46 @@ MAPBOX_ETERNAL_CONSTEXPR const auto layerProperties = mapbox::eternal::hash_map<
{"background-color-transition", toUint8(Property::BackgroundColorTransition)},
{"background-opacity-transition", toUint8(Property::BackgroundOpacityTransition)},
{"background-pattern-transition", toUint8(Property::BackgroundPatternTransition)}});

StyleProperty getLayerProperty(const BackgroundLayer& layer, Property property) {
switch (property) {
case Property::BackgroundColor:
return makeStyleProperty(layer.getBackgroundColor());
case Property::BackgroundOpacity:
return makeStyleProperty(layer.getBackgroundOpacity());
case Property::BackgroundPattern:
return makeStyleProperty(layer.getBackgroundPattern());
case Property::BackgroundColorTransition:
return makeStyleProperty(layer.getBackgroundColorTransition());
case Property::BackgroundOpacityTransition:
return makeStyleProperty(layer.getBackgroundOpacityTransition());
case Property::BackgroundPatternTransition:
return makeStyleProperty(layer.getBackgroundPatternTransition());
}
return {};
}

StyleProperty getLayerProperty(const BackgroundLayer& layer, const std::string& name) {
const auto it = layerProperties.find(name.c_str());
if (it == layerProperties.end()) {
return {};
}
return getLayerProperty(layer, static_cast<Property>(it->second));
}

} // namespace

Value BackgroundLayer::serialize() const {
auto result = Layer::serialize();
assert(result.getObject());
for (const auto& property : layerProperties) {
auto styleProperty = getLayerProperty(*this, static_cast<Property>(property.second));
if (styleProperty.getKind() == StyleProperty::Kind::Undefined) continue;
serializeProperty(result, styleProperty, property.first.c_str(), property.second < kPaintPropertyCount);
}
return result;
}

optional<Error> BackgroundLayer::setProperty(const std::string& name, const Convertible& value) {
const auto it = layerProperties.find(name.c_str());
if (it == layerProperties.end()) {
Expand Down Expand Up @@ -236,26 +276,7 @@ optional<Error> BackgroundLayer::setProperty(const std::string& name, const Conv
}

StyleProperty BackgroundLayer::getProperty(const std::string& name) const {
const auto it = layerProperties.find(name.c_str());
if (it == layerProperties.end()) {
return {};
}

switch (static_cast<Property>(it->second)) {
case Property::BackgroundColor:
return makeStyleProperty(getBackgroundColor());
case Property::BackgroundOpacity:
return makeStyleProperty(getBackgroundOpacity());
case Property::BackgroundPattern:
return makeStyleProperty(getBackgroundPattern());
case Property::BackgroundColorTransition:
return makeStyleProperty(getBackgroundColorTransition());
case Property::BackgroundOpacityTransition:
return makeStyleProperty(getBackgroundOpacityTransition());
case Property::BackgroundPatternTransition:
return makeStyleProperty(getBackgroundPatternTransition());
}
return {};
return getLayerProperty(*this, name);
}

Mutable<Layer::Impl> BackgroundLayer::mutableBaseImpl() const {
Expand Down
Loading