Serialize ART state into folly::dynamic

Summary:
This diff serializes ART state into folly::dynamic. this is necessary to send this data to Android

changelog: [Internal]

Reviewed By: shergin

Differential Revision: D21657608

fbshipit-source-id: 6c1b69af7d1dbe7de15e509f83c508a38294d89e
This commit is contained in:
David Vacca
2020-05-21 00:11:51 -07:00
committed by Facebook GitHub Bot
parent 0305ff8ee0
commit 21afa62517
5 changed files with 101 additions and 4 deletions
@@ -0,0 +1,93 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <Glog/logging.h>
#include <folly/dynamic.h>
#include <react/components/art/ARTSurfaceViewState.h>
#include <react/components/art/Group.h>
#include <react/components/art/Shape.h>
#include <react/components/art/Text.h>
#include <react/components/art/primitives.h>
namespace facebook {
namespace react {
#ifdef ANDROID
inline folly::dynamic toDynamic(Group const &group);
inline folly::dynamic toDynamic(Shape const &shape);
inline folly::dynamic toDynamic(Text const &text);
inline folly::dynamic toDynamic(Element const &element);
inline folly::dynamic toDynamic(std::vector<Float> const &elements) {
folly::dynamic result = folly::dynamic::array();
for (auto const &element : elements) {
result.push_back(element);
}
return result;
}
inline folly::dynamic toDynamic(Element::ListOfShared const &elements) {
folly::dynamic children = folly::dynamic::array();
for (auto const &element : elements) {
children.push_back(element->getDynamic());
}
return children;
}
inline folly::dynamic toDynamic(Group const &group) {
folly::dynamic result = folly::dynamic::object();
result["opacity"] = group.opacity;
result["type"] = 1;
if (group.elements.size() > 0) {
result["elements"] = toDynamic(group.elements);
}
result["transform"] = toDynamic(group.transform);
return result;
}
inline folly::dynamic toDynamic(Shape const &shape) {
folly::dynamic result = folly::dynamic::object();
result["type"] = 2;
result["opacity"] = shape.opacity;
result["transform"] = toDynamic(shape.transform);
result["d"] = toDynamic(shape.d);
result["stroke"] = toDynamic(shape.stroke);
result["strokeDash"] = toDynamic(shape.strokeDash);
result["fill"] = toDynamic(shape.fill);
result["strokeWidth"] = shape.strokeWidth;
result["strokeCap"] = shape.strokeCap;
result["strokeJoin"] = shape.strokeJoin;
return result;
}
inline folly::dynamic toDynamic(Text const &text) {
folly::dynamic result = folly::dynamic::object();
result["type"] = 3;
result["opacity"] = text.opacity;
result["transform"] = toDynamic(text.transform);
result["d"] = toDynamic(text.d);
result["stroke"] = toDynamic(text.stroke);
result["strokeDash"] = toDynamic(text.strokeDash);
result["fill"] = toDynamic(text.fill);
result["strokeWidth"] = text.strokeWidth;
result["strokeCap"] = text.strokeCap;
result["strokeJoin"] = text.strokeJoin;
result["aligment"] = text.aligment;
// TODO T64130144: add serialization of Frame
// result["aligment"] = toDynamic(text.frame);
return result;
}
inline folly::dynamic toDynamic(ARTSurfaceViewState const &surfaceViewState) {
folly::dynamic result = folly::dynamic::object();
result["elements"] = toDynamic(surfaceViewState.elements);
return result;
}
#endif
} // namespace react
} // namespace facebook
@@ -7,13 +7,14 @@
#include <react/components/art/Group.h>
#include <react/components/art/Element.h>
#include <react/components/art/conversions.h>
namespace facebook {
namespace react {
#ifdef ANDROID
folly::dynamic Group::getDynamic() const {
return folly::dynamic::object();
return toDynamic(*this);
}
#endif
@@ -7,13 +7,14 @@
#include <react/components/art/Shape.h>
#include <react/components/art/Element.h>
#include <react/components/art/conversions.h>
namespace facebook {
namespace react {
#ifdef ANDROID
folly::dynamic Shape::getDynamic() const {
return folly::dynamic::object();
return toDynamic(*this);
}
#endif
@@ -7,13 +7,14 @@
#include <react/components/art/Text.h>
#include <react/components/art/Element.h>
#include <react/components/art/conversions.h>
namespace facebook {
namespace react {
#ifdef ANDROID
folly::dynamic Shape::getDynamic() const {
return folly::dynamic::object();
return toDynamic(*this);
}
#endif
@@ -6,6 +6,7 @@
*/
#include <react/components/art/ARTSurfaceViewState.h>
#include <react/components/art/conversions.h>
#include <react/debug/debugStringConvertibleUtils.h>
namespace facebook {
@@ -13,7 +14,7 @@ namespace react {
#ifdef ANDROID
folly::dynamic ARTSurfaceViewState::getDynamic() const {
return folly::dynamic::object();
return toDynamic(*this);
}
#endif