Prepare to break the Dependency Cycle between React-graphics and React-Fabric (#36385)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36385

This change moves the `graphics/conversions.h` files from `ReactCommon/react/renderer/graphics` to `ReactCommon/react/renderer/core`, renaming it to `graphicsConversions.h`.

This is required because React-Fabric imports graphics, but graphics imports React-Fabric due to this file.

The change would be breaking, but we don't want to break the ecosystem without even a warning. So, we put back the `conversions.h` file, which now just `include` the new one and outputs a warning when building. This actually maintain the dep cycle for the current version, but at least users are warned.

## Changelog:
[iOS][Deprecated] - Deprecate the `ReactCommon/react/renderer/graphics/conversions.h` in favor of `ReactCommon/react/core/graphicsConversions.h`

Reviewed By: cortinico, dmytrorykun

Differential Revision: D43836261

fbshipit-source-id: ffe53a8ce2b0ea2dd1e1e5aaf6b3d3c5b57ad46d
This commit is contained in:
Riccardo Cipolleschi
2023-03-08 03:59:55 -08:00
committed by Facebook GitHub Bot
parent a055e07c3e
commit d72697ca95
20 changed files with 291 additions and 264 deletions
@@ -10,7 +10,7 @@
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/components/view/ViewPropsMapBuffer.h>
#include <react/renderer/components/view/conversions.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
@@ -8,8 +8,8 @@
#include "ParagraphAttributes.h"
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/debug/debugStringConvertibleUtils.h>
#include <react/renderer/graphics/conversions.h>
#include <react/utils/FloatComparison.h>
namespace facebook::react {
@@ -9,7 +9,7 @@
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/core/conversions.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/utils/FloatComparison.h>
#include <cmath>
@@ -19,8 +19,8 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/core/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>
#include <react/renderer/graphics/conversions.h>
#include <cmath>
#ifdef ANDROID
@@ -9,7 +9,7 @@
#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/attributedstring/primitives.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
namespace facebook {
namespace react {
@@ -9,7 +9,7 @@
#include <react/renderer/attributedstring/TextAttributes.h>
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/attributedstring/primitives.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
namespace facebook {
namespace react {
@@ -12,7 +12,7 @@
#include <glog/logging.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/imagemanager/primitives.h>
namespace facebook {
@@ -7,8 +7,8 @@
#pragma once
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook {
namespace react {
@@ -7,8 +7,8 @@
#pragma once
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/graphics/conversions.h>
#ifdef ANDROID
#include <folly/dynamic.h>
@@ -9,8 +9,8 @@
#include <react/renderer/components/scrollview/conversions.h>
#include <react/renderer/core/CoreFeatures.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/debug/debugStringConvertibleUtils.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/propsConversions.h>
@@ -9,9 +9,9 @@
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/core/CoreFeatures.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>
#include <react/renderer/debug/DebugStringConvertibleItem.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook::react {
@@ -8,8 +8,8 @@
#include "AndroidTextInputProps.h"
#include <react/renderer/components/image/conversions.h>
#include <react/renderer/core/CoreFeatures.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook::react {
@@ -9,8 +9,8 @@
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/components/iostextinput/propsConversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook::react {
@@ -12,9 +12,9 @@
#include <react/renderer/components/view/conversions.h>
#include <react/renderer/components/view/propsConversions.h>
#include <react/renderer/core/CoreFeatures.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/core/propsConversions.h>
#include <react/renderer/debug/debugStringConvertibleUtils.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook::react {
@@ -12,7 +12,7 @@
#include "viewPropConversions.h"
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
namespace facebook {
@@ -10,7 +10,7 @@
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/components/view/ViewPropsMapBuffer.h>
#include <react/renderer/components/view/conversions.h>
#include <react/renderer/graphics/conversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
@@ -12,8 +12,8 @@
#include <react/renderer/core/LayoutMetrics.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/core/TraitCast.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/debug/DebugStringConvertibleItem.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook::react {
@@ -0,0 +1,257 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <butter/map.h>
#include <glog/logging.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/PlatformColorParser.h>
#include <react/renderer/graphics/Point.h>
#include <react/renderer/graphics/Rect.h>
#include <react/renderer/graphics/RectangleCorners.h>
#include <react/renderer/graphics/RectangleEdges.h>
#include <react/renderer/graphics/Size.h>
namespace facebook {
namespace react {
#pragma mark - Color
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
SharedColor &result) {
ColorComponents colorComponents = {0, 0, 0, 0};
if (value.hasType<int>()) {
auto argb = (int64_t)value;
auto ratio = 255.f;
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
colorComponents.blue = (argb & 0xFF) / ratio;
} else if (value.hasType<std::vector<float>>()) {
auto items = (std::vector<float>)value;
auto length = items.size();
react_native_expect(length == 3 || length == 4);
colorComponents.red = items.at(0);
colorComponents.green = items.at(1);
colorComponents.blue = items.at(2);
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;
} else {
colorComponents = parsePlatformColor(context, value);
}
result = colorFromComponents(colorComponents);
}
#ifdef ANDROID
inline int toAndroidRepr(const SharedColor &color) {
return *color;
}
#endif
inline std::string toString(const SharedColor &value) {
ColorComponents components = colorComponentsFromColor(value);
auto ratio = 255.f;
return "rgba(" + folly::to<std::string>(round(components.red * ratio)) +
", " + folly::to<std::string>(round(components.green * ratio)) + ", " +
folly::to<std::string>(round(components.blue * ratio)) + ", " +
folly::to<std::string>(round(components.alpha * ratio)) + ")";
}
#pragma mark - Geometry
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
Point &result) {
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "x") {
result.x = pair.second;
} else if (pair.first == "y") {
result.y = pair.second;
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 2);
if (array.size() >= 2) {
result = {array.at(0), array.at(1)};
} else {
result = {0, 0};
LOG(ERROR) << "Unsupported Point vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported Point type";
}
}
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
Size &result) {
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "width") {
result.width = pair.second;
} else if (pair.first == "height") {
result.height = pair.second;
} else {
LOG(ERROR) << "Unsupported Size map key: " << pair.first;
react_native_expect(false);
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 2);
if (array.size() >= 2) {
result = {array.at(0), array.at(1)};
} else {
result = {0, 0};
LOG(ERROR) << "Unsupported Size vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported Size type";
}
}
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
EdgeInsets &result) {
if (value.hasType<Float>()) {
auto number = (Float)value;
result = {number, number, number, number};
return;
}
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "top") {
result.top = pair.second;
} else if (pair.first == "left") {
result.left = pair.second;
} else if (pair.first == "bottom") {
result.bottom = pair.second;
} else if (pair.first == "right") {
result.right = pair.second;
} else {
LOG(ERROR) << "Unsupported EdgeInsets map key: " << pair.first;
react_native_expect(false);
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 4);
if (array.size() >= 4) {
result = {array.at(0), array.at(1), array.at(2), array.at(3)};
} else {
result = {0, 0, 0, 0};
LOG(ERROR) << "Unsupported EdgeInsets vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported EdgeInsets type";
}
}
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
CornerInsets &result) {
if (value.hasType<Float>()) {
auto number = (Float)value;
result = {number, number, number, number};
return;
}
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "topLeft") {
result.topLeft = pair.second;
} else if (pair.first == "topRight") {
result.topRight = pair.second;
} else if (pair.first == "bottomLeft") {
result.bottomLeft = pair.second;
} else if (pair.first == "bottomRight") {
result.bottomRight = pair.second;
} else {
LOG(ERROR) << "Unsupported CornerInsets map key: " << pair.first;
react_native_expect(false);
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 4);
if (array.size() >= 4) {
result = {array.at(0), array.at(1), array.at(2), array.at(3)};
} else {
LOG(ERROR) << "Unsupported CornerInsets vector size: " << array.size();
}
}
// Error case - we should only here if all other supported cases fail
// In dev we would crash on assert before this point
result = {0, 0, 0, 0};
LOG(ERROR) << "Unsupported CornerInsets type";
}
inline std::string toString(const Point &point) {
return "{" + folly::to<std::string>(point.x) + ", " +
folly::to<std::string>(point.y) + "}";
}
inline std::string toString(const Size &size) {
return "{" + folly::to<std::string>(size.width) + ", " +
folly::to<std::string>(size.height) + "}";
}
inline std::string toString(const Rect &rect) {
return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}";
}
inline std::string toString(const EdgeInsets &edgeInsets) {
return "{" + folly::to<std::string>(edgeInsets.left) + ", " +
folly::to<std::string>(edgeInsets.top) + ", " +
folly::to<std::string>(edgeInsets.right) + ", " +
folly::to<std::string>(edgeInsets.bottom) + "}";
}
inline std::string toString(const CornerInsets &cornerInsets) {
return "{" + folly::to<std::string>(cornerInsets.topLeft) + ", " +
folly::to<std::string>(cornerInsets.topRight) + ", " +
folly::to<std::string>(cornerInsets.bottomLeft) + ", " +
folly::to<std::string>(cornerInsets.bottomRight) + "}";
}
} // namespace react
} // namespace facebook
@@ -13,8 +13,8 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/core/RawPropsKey.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook {
namespace react {
+16 -246
View File
@@ -7,251 +7,21 @@
#pragma once
#include <butter/map.h>
#include <glog/logging.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/PlatformColorParser.h>
#include <react/renderer/graphics/Point.h>
#include <react/renderer/graphics/Rect.h>
#include <react/renderer/graphics/RectangleCorners.h>
#include <react/renderer/graphics/RectangleEdges.h>
#include <react/renderer/graphics/Size.h>
#include <react/renderer/core/graphicsConversions.h>
namespace facebook {
namespace react {
// This file belongs to the React-graphics module.
// This file also used to have a reference to two files that are located in the
// react/renderer/core folder. That folder belongs to a module that is called
// React-Fabric.
// The React-Fabric module declares an explicit dependency on
// React-graphics. Including those files in a React-graphics' file created a
// circular dependency because React-Fabric was explicitly depending on
// React-graphics, which was implicitly depending on React-Fabric. We break that
// dependency by moving the old `graphics/conversions.h` file to the
// React-Fabric module and renaming it `core/graphicsConversions.h`.
#pragma mark - Color
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
SharedColor &result) {
ColorComponents colorComponents = {0, 0, 0, 0};
if (value.hasType<int>()) {
auto argb = (int64_t)value;
auto ratio = 255.f;
colorComponents.alpha = ((argb >> 24) & 0xFF) / ratio;
colorComponents.red = ((argb >> 16) & 0xFF) / ratio;
colorComponents.green = ((argb >> 8) & 0xFF) / ratio;
colorComponents.blue = (argb & 0xFF) / ratio;
} else if (value.hasType<std::vector<float>>()) {
auto items = (std::vector<float>)value;
auto length = items.size();
react_native_expect(length == 3 || length == 4);
colorComponents.red = items.at(0);
colorComponents.green = items.at(1);
colorComponents.blue = items.at(2);
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;
} else {
colorComponents = parsePlatformColor(context, value);
}
result = colorFromComponents(colorComponents);
}
#ifdef ANDROID
inline int toAndroidRepr(const SharedColor &color) {
return *color;
}
#endif
inline std::string toString(const SharedColor &value) {
ColorComponents components = colorComponentsFromColor(value);
auto ratio = 255.f;
return "rgba(" + folly::to<std::string>(round(components.red * ratio)) +
", " + folly::to<std::string>(round(components.green * ratio)) + ", " +
folly::to<std::string>(round(components.blue * ratio)) + ", " +
folly::to<std::string>(round(components.alpha * ratio)) + ")";
}
#pragma mark - Geometry
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
Point &result) {
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "x") {
result.x = pair.second;
} else if (pair.first == "y") {
result.y = pair.second;
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 2);
if (array.size() >= 2) {
result = {array.at(0), array.at(1)};
} else {
result = {0, 0};
LOG(ERROR) << "Unsupported Point vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported Point type";
}
}
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
Size &result) {
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "width") {
result.width = pair.second;
} else if (pair.first == "height") {
result.height = pair.second;
} else {
LOG(ERROR) << "Unsupported Size map key: " << pair.first;
react_native_expect(false);
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 2);
if (array.size() >= 2) {
result = {array.at(0), array.at(1)};
} else {
result = {0, 0};
LOG(ERROR) << "Unsupported Size vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported Size type";
}
}
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
EdgeInsets &result) {
if (value.hasType<Float>()) {
auto number = (Float)value;
result = {number, number, number, number};
return;
}
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "top") {
result.top = pair.second;
} else if (pair.first == "left") {
result.left = pair.second;
} else if (pair.first == "bottom") {
result.bottom = pair.second;
} else if (pair.first == "right") {
result.right = pair.second;
} else {
LOG(ERROR) << "Unsupported EdgeInsets map key: " << pair.first;
react_native_expect(false);
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 4);
if (array.size() >= 4) {
result = {array.at(0), array.at(1), array.at(2), array.at(3)};
} else {
result = {0, 0, 0, 0};
LOG(ERROR) << "Unsupported EdgeInsets vector size: " << array.size();
}
} else {
LOG(ERROR) << "Unsupported EdgeInsets type";
}
}
inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
CornerInsets &result) {
if (value.hasType<Float>()) {
auto number = (Float)value;
result = {number, number, number, number};
return;
}
if (value.hasType<butter::map<std::string, Float>>()) {
auto map = (butter::map<std::string, Float>)value;
for (const auto &pair : map) {
if (pair.first == "topLeft") {
result.topLeft = pair.second;
} else if (pair.first == "topRight") {
result.topRight = pair.second;
} else if (pair.first == "bottomLeft") {
result.bottomLeft = pair.second;
} else if (pair.first == "bottomRight") {
result.bottomRight = pair.second;
} else {
LOG(ERROR) << "Unsupported CornerInsets map key: " << pair.first;
react_native_expect(false);
}
}
return;
}
react_native_expect(value.hasType<std::vector<Float>>());
if (value.hasType<std::vector<Float>>()) {
auto array = (std::vector<Float>)value;
react_native_expect(array.size() == 4);
if (array.size() >= 4) {
result = {array.at(0), array.at(1), array.at(2), array.at(3)};
} else {
LOG(ERROR) << "Unsupported CornerInsets vector size: " << array.size();
}
}
// Error case - we should only here if all other supported cases fail
// In dev we would crash on assert before this point
result = {0, 0, 0, 0};
LOG(ERROR) << "Unsupported CornerInsets type";
}
inline std::string toString(const Point &point) {
return "{" + folly::to<std::string>(point.x) + ", " +
folly::to<std::string>(point.y) + "}";
}
inline std::string toString(const Size &size) {
return "{" + folly::to<std::string>(size.width) + ", " +
folly::to<std::string>(size.height) + "}";
}
inline std::string toString(const Rect &rect) {
return "{" + toString(rect.origin) + ", " + toString(rect.size) + "}";
}
inline std::string toString(const EdgeInsets &edgeInsets) {
return "{" + folly::to<std::string>(edgeInsets.left) + ", " +
folly::to<std::string>(edgeInsets.top) + ", " +
folly::to<std::string>(edgeInsets.right) + ", " +
folly::to<std::string>(edgeInsets.bottom) + "}";
}
inline std::string toString(const CornerInsets &cornerInsets) {
return "{" + folly::to<std::string>(cornerInsets.topLeft) + ", " +
folly::to<std::string>(cornerInsets.topRight) + ", " +
folly::to<std::string>(cornerInsets.bottomLeft) + ", " +
folly::to<std::string>(cornerInsets.bottomRight) + "}";
}
} // namespace react
} // namespace facebook
#warning \
"[DEPRECATION] `graphics/conversions.h` is deprecated and will be removed in the future. \
If this warning appears due to a library, please open an issue in that library, and ask for an update. \
Please, replace the `#include <react/renderer/graphics/conversions.h>` statements \
with `#include <react/renderer/core/graphicsConversions.h>`."