Skip to content

Commit

Permalink
Remove dependency on PropsParserContext (#42616)
Browse files Browse the repository at this point in the history
Summary:

PropsParserContext is an entity in Core with a lot of baggage. We used it in graphics only to access the `surfaceId` and the `contextContainer`. We don't need to bring to graphics the whole dependencies of core due to these two parts.

This change break the dependency by passing along only the elements that we actually need.

## Changelog
[Internal] - break dependencies between graphics and core by removing the include of the PropsParserContext

Differential Revision: D52999204
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Jan 26, 2024
1 parent 5f75e9b commit b726213
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Float.h>
#include <react/renderer/graphics/PlatformColorParser.h>
#include <react/renderer/graphics/Point.h>
#include <react/renderer/graphics/Rect.h>
Expand All @@ -29,29 +30,7 @@ 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 {
result = parsePlatformColor(context, value);
return;
}

result = colorFromComponents(colorComponents);
fromRawValue(context.contextContainer, context.surfaceId, value, result);
}

#ifdef ANDROID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.
*/

#include <react/debug/react_native_expect.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/utils/ContextContainer.h>

#pragma once

namespace facebook::react {
typedef SharedColor (
*parsePlatformColorFn)(const ContextContainer&, int32_t, const RawValue&);

inline void fromRawValueShared(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result,
parsePlatformColorFn parsePlatformColor) {
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;

result = colorFromComponents(colorComponents);
} 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;

result = colorFromComponents(colorComponents);
} else {
result = parsePlatformColor(contextContainer, surfaceId, value);
}
}
} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
#pragma once

#include <fbjni/fbjni.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/fromRawValueShared.h>
#include <react/utils/ContextContainer.h>
#include <unordered_map>

namespace facebook::react {

inline SharedColor parsePlatformColor(
const PropsParserContext& context,
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value) {
ColorComponents colorComponents = {0, 0, 0, 0};

if (value.hasType<
std::unordered_map<std::string, std::vector<std::string>>>()) {
const auto& fabricUIManager =
context.contextContainer.at<jni::global_ref<jobject>>(
"FabricUIManager");
contextContainer.at<jni::global_ref<jobject>>("FabricUIManager");
static auto getColorFromJava =
fabricUIManager->getClass()
->getMethod<jint(jint, jni::JArrayClass<jni::JString>)>("getColor");
Expand All @@ -37,8 +39,8 @@ inline SharedColor parsePlatformColor(
for (int i = 0; i < resourcePaths.size(); i++) {
javaResourcePaths->setElement(i, *jni::make_jstring(resourcePaths[i]));
}
auto color = getColorFromJava(
fabricUIManager, context.surfaceId, *javaResourcePaths);
auto color =
getColorFromJava(fabricUIManager, surfaceId, *javaResourcePaths);

auto argb = (int64_t)color;
auto ratio = 255.f;
Expand All @@ -51,4 +53,13 @@ inline SharedColor parsePlatformColor(
return {colorFromComponents(colorComponents)};
}

inline void fromRawValue(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result) {
fromRawValueShared(
contextContainer, surfaceId, value, result, parsePlatformColor);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

#pragma once

#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/fromRawValueShared.h>
#include <react/utils/ContextContainer.h>

namespace facebook::react {

inline SharedColor parsePlatformColor(
const PropsParserContext& context,
const RawValue& value) {
const ContextContainer& /*contextContainer*/,
int32_t /*surfaceId*/,
const RawValue& /*value*/) {
float alpha = 0;
float red = 0;
float green = 0;
Expand All @@ -24,4 +27,13 @@ inline SharedColor parsePlatformColor(
return {colorFromComponents({red, green, blue, alpha})};
}

inline void fromRawValue(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result) {
fromRawValueShared(
contextContainer, surfaceId, value, result, parsePlatformColor);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

#pragma once

#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/core/rawValue.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/RCTPlatformColorUtils.h>
#include <react/renderer/graphics/fromRawValueShared.h>
#include <react/utils/ContextContainer.h>
#include <unordered_map>

namespace facebook::react {

inline SharedColor parsePlatformColor(
const PropsParserContext& context,
const ContextContainer& /*contextContainer*/,
int32_t /*surfaceId*/,
const RawValue& value) {
if (value.hasType<std::unordered_map<std::string, RawValue>>()) {
auto items = (std::unordered_map<std::string, RawValue>)value;
Expand All @@ -31,4 +34,13 @@ inline SharedColor parsePlatformColor(
return clearColor();
}

inline void fromRawValue(
const ContextContainer& contextContainer,
int32_t surfaceId,
const RawValue& value,
SharedColor& result) {
fromRawValueShared(
contextContainer, surfaceId, value, result, parsePlatformColor);
}

} // namespace facebook::react

0 comments on commit b726213

Please sign in to comment.